1 /* Block-relocating memory allocator.
2 Copyright (C) 1992 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 Only relocate the blocs neccessary for SIZE in r_alloc_sbrk,
23 rather than all of them. This means allowing for a possible
24 hole between the first bloc and the end of malloc storage. */
27 #include "lisp.h" /* Needed for VALBITS. */
29 #include "mem_limits.h"
31 #define NIL ((POINTER) 0)
34 /* Declarations for working with the malloc, ralloc, and system breaks. */
36 /* System call to set the break value. */
37 extern POINTER
sbrk ();
39 /* The break value, as seen by malloc (). */
40 static POINTER virtual_break_value
;
42 /* The break value, viewed by the relocatable blocs. */
43 static POINTER break_value
;
45 /* The REAL (i.e., page aligned) break value of the process. */
46 static POINTER page_break_value
;
48 /* Macros for rounding. Note that rounding to any value is possible
49 by changing the definition of PAGE. */
50 #define PAGE (getpagesize ())
51 #define ALIGNED(addr) (((unsigned int) (addr) & (PAGE - 1)) == 0)
52 #define ROUNDUP(size) (((unsigned int) (size) + PAGE) & ~(PAGE - 1))
53 #define ROUND_TO_PAGE(addr) (addr & (~(PAGE - 1)))
54 #define EXCEEDS_ELISP_PTR(ptr) ((unsigned int) (ptr) >> VALBITS)
56 /* Managing "almost out of memory" warnings. */
58 /* Level of warnings issued. */
61 /* Function to call to issue a warning;
62 0 means don't issue them. */
63 static void (*warnfunction
) ();
66 check_memory_limits (address
)
69 SIZE data_size
= address
- data_space_start
;
74 if (data_size
> (lim_data
/ 4) * 3)
77 (*warnfunction
) ("Warning: past 75% of memory limit");
82 if (data_size
> (lim_data
/ 20) * 17)
85 (*warnfunction
) ("Warning: past 85% of memory limit");
90 if (data_size
> (lim_data
/ 20) * 19)
93 (*warnfunction
) ("Warning: past 95% of memory limit");
98 (*warnfunction
) ("Warning: past acceptable memory limits");
102 if (EXCEEDS_ELISP_PTR (address
))
106 /* Functions to get and return memory from the system. */
108 /* Obtain SIZE bytes of space. If enough space is not presently available
109 in our process reserve, (i.e., (page_break_value - break_value)),
110 this means getting more page-aligned space from the system. */
116 SIZE already_available
= page_break_value
- break_value
;
118 if (already_available
< size
)
120 SIZE get
= ROUNDUP (size
- already_available
);
123 check_memory_limits (page_break_value
);
125 if (((int) sbrk (get
)) < 0)
128 page_break_value
+= get
;
134 /* Obtain SIZE bytes of space and return a pointer to the new area. */
137 get_more_space (size
)
140 POINTER ptr
= break_value
;
145 /* Note that SIZE bytes of space have been relinquished by the process.
146 If SIZE is more than a page, return the space to the system. */
152 POINTER new_page_break
;
155 new_page_break
= (POINTER
) ROUNDUP (break_value
);
157 if (new_page_break
!= page_break_value
)
159 if (((int) (sbrk ((char *) new_page_break
160 - (char *) page_break_value
))) < 0)
163 page_break_value
= new_page_break
;
166 /* Zero the space from the end of the "official" break to the actual
167 break, so that bugs show up faster. */
168 bzero (break_value
, ((char *) page_break_value
- (char *) break_value
));
171 /* The meat - allocating, freeing, and relocating blocs. */
173 /* These structures are allocated in the malloc arena.
174 The linked list is kept in order of increasing '.data' members.
175 The data blocks abut each other; if b->next is non-nil, then
176 b->data + b->size == b->next->data. */
186 #define NIL_BLOC ((bloc_ptr) 0)
187 #define BLOC_PTR_SIZE (sizeof (struct bp))
189 /* Head and tail of the list of relocatable blocs. */
190 static bloc_ptr first_bloc
, last_bloc
;
192 /* Declared in dispnew.c, this version doesn't screw up if regions
194 extern void safe_bcopy ();
196 /* Find the bloc referenced by the address in PTR. Returns a pointer
203 register bloc_ptr p
= first_bloc
;
205 while (p
!= NIL_BLOC
)
207 if (p
->variable
== ptr
&& p
->data
== *ptr
)
216 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
217 Returns a pointer to the new bloc. */
223 register bloc_ptr new_bloc
= (bloc_ptr
) malloc (BLOC_PTR_SIZE
);
225 new_bloc
->data
= get_more_space (size
);
226 new_bloc
->size
= size
;
227 new_bloc
->next
= NIL_BLOC
;
228 new_bloc
->variable
= NIL
;
232 new_bloc
->prev
= last_bloc
;
233 last_bloc
->next
= new_bloc
;
234 last_bloc
= new_bloc
;
238 first_bloc
= last_bloc
= new_bloc
;
239 new_bloc
->prev
= NIL_BLOC
;
245 /* Relocate all blocs from BLOC on upward in the list to the zone
246 indicated by ADDRESS. Direction of relocation is determined by
247 the position of ADDRESS relative to BLOC->data.
249 Note that ordering of blocs is not affected by this function. */
252 relocate_some_blocs (bloc
, address
)
257 POINTER data_zone
= bloc
->data
;
258 register SIZE data_zone_size
= 0;
259 register SIZE offset
= bloc
->data
- address
;
260 POINTER new_data_zone
= data_zone
- offset
;
262 for (b
= bloc
; b
!= NIL_BLOC
; b
= b
->next
)
264 data_zone_size
+= b
->size
;
266 *b
->variable
= b
->data
;
269 safe_bcopy (data_zone
, new_data_zone
, data_zone_size
);
272 /* Free BLOC from the chain of blocs, relocating any blocs above it
273 and returning BLOC->size bytes to the free area. */
279 if (bloc
== first_bloc
&& bloc
== last_bloc
)
281 first_bloc
= last_bloc
= NIL_BLOC
;
283 else if (bloc
== last_bloc
)
285 last_bloc
= bloc
->prev
;
286 last_bloc
->next
= NIL_BLOC
;
288 else if (bloc
== first_bloc
)
290 first_bloc
= bloc
->next
;
291 first_bloc
->prev
= NIL_BLOC
;
292 relocate_some_blocs (bloc
->next
, bloc
->data
);
296 bloc
->next
->prev
= bloc
->prev
;
297 bloc
->prev
->next
= bloc
->next
;
298 relocate_some_blocs (bloc
->next
, bloc
->data
);
301 relinquish (bloc
->size
);
305 /* Interface routines. */
307 static int use_relocatable_buffers
;
309 /* Obtain SIZE bytes of storage from the free pool, or the system,
310 as neccessary. If relocatable blocs are in use, this means
319 if (! use_relocatable_buffers
)
327 relocate_some_blocs (first_bloc
, first_bloc
->data
+ size
);
329 /* Zero out the space we just allocated, to help catch bugs
331 bzero (virtual_break_value
, size
);
337 relocate_some_blocs (first_bloc
, first_bloc
->data
+ size
);
341 ptr
= virtual_break_value
;
342 virtual_break_value
+= size
;
346 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
347 the data is returned in *PTR. PTR is thus the address of some variable
348 which will use the data area. */
355 register bloc_ptr new_bloc
;
357 new_bloc
= get_bloc (size
);
358 new_bloc
->variable
= ptr
;
359 *ptr
= new_bloc
->data
;
364 /* Free a bloc of relocatable storage whose data is pointed to by PTR. */
368 register POINTER
*ptr
;
370 register bloc_ptr dead_bloc
;
372 dead_bloc
= find_bloc (ptr
);
373 if (dead_bloc
== NIL_BLOC
)
376 free_bloc (dead_bloc
);
379 /* Given a pointer at address PTR to relocatable data, resize it
380 to SIZE. This is done by obtaining a new block and freeing the
381 old, unless SIZE is less than or equal to the current bloc size,
382 in which case nothing happens and the current value is returned.
384 The contents of PTR is changed to reflect the new bloc, and this
385 value is returned. */
388 r_re_alloc (ptr
, size
)
392 register bloc_ptr old_bloc
, new_bloc
;
394 old_bloc
= find_bloc (ptr
);
395 if (old_bloc
== NIL_BLOC
)
398 if (size
<= old_bloc
->size
)
399 /* Wouldn't it be useful to actually resize the bloc here? */
402 new_bloc
= get_bloc (size
);
403 new_bloc
->variable
= ptr
;
404 safe_bcopy (old_bloc
->data
, new_bloc
->data
, old_bloc
->size
);
405 *ptr
= new_bloc
->data
;
407 free_bloc (old_bloc
);
412 /* The hook `malloc' uses for the function which gets more space
414 extern POINTER (*__morecore
) ();
416 /* A flag to indicate whether we have initialized ralloc yet. For
417 Emacs's sake, please do not make this local to malloc_init; on some
418 machines, the dumping procedure makes all static variables
419 read-only. On these machines, the word static is #defined to be
420 the empty string, meaning that malloc_initialized becomes an
421 automatic variable, and loses its value each time Emacs is started
423 static int malloc_initialized
= 0;
425 /* Intialize various things for memory allocation. */
428 malloc_init (start
, warn_func
)
430 void (*warn_func
) ();
433 data_space_start
= start
;
435 if (malloc_initialized
)
438 malloc_initialized
= 1;
439 __morecore
= r_alloc_sbrk
;
440 virtual_break_value
= break_value
= sbrk (0);
441 page_break_value
= (POINTER
) ROUNDUP (break_value
);
442 bzero (break_value
, (page_break_value
- break_value
));
443 use_relocatable_buffers
= 1;
447 warnfunction
= warn_func
;