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. */
29 #include "lisp.h" /* Needed for VALBITS. */
33 /* Declared in dispnew.c, this version doesn't screw up if regions
35 extern void safe_bcopy ();
37 #include "getpagesize.h"
39 #else /* Not emacs. */
44 typedef void *POINTER
;
46 #define EXCEEDS_LISP_PTR(x) 0
52 #define safe_bcopy(x, y, z) memmove (y, x, z)
56 #define NIL ((POINTER) 0)
58 /* A flag to indicate whether we have initialized ralloc yet. For
59 Emacs's sake, please do not make this local to malloc_init; on some
60 machines, the dumping procedure makes all static variables
61 read-only. On these machines, the word static is #defined to be
62 the empty string, meaning that r_alloc_initialized becomes an
63 automatic variable, and loses its value each time Emacs is started up. */
64 static int r_alloc_initialized
= 0;
66 static void r_alloc_init ();
68 /* Declarations for working with the malloc, ralloc, and system breaks. */
70 /* Function to set the real break value. */
71 static POINTER (*real_morecore
) ();
73 /* The break value, as seen by malloc (). */
74 static POINTER virtual_break_value
;
76 /* The break value, viewed by the relocatable blocs. */
77 static POINTER break_value
;
79 /* The REAL (i.e., page aligned) break value of the process. */
80 static POINTER page_break_value
;
82 /* Macros for rounding. Note that rounding to any value is possible
83 by changing the definition of PAGE. */
84 #define PAGE (getpagesize ())
85 #define ALIGNED(addr) (((unsigned int) (addr) & (PAGE - 1)) == 0)
86 #define ROUNDUP(size) (((unsigned int) (size) + PAGE - 1) & ~(PAGE - 1))
87 #define ROUND_TO_PAGE(addr) (addr & (~(PAGE - 1)))
89 /* Functions to get and return memory from the system. */
91 /* Obtain SIZE bytes of space. If enough space is not presently available
92 in our process reserve, (i.e., (page_break_value - break_value)),
93 this means getting more page-aligned space from the system.
95 Return non-zero if all went well, or zero if we couldn't allocate
101 SIZE already_available
= page_break_value
- break_value
;
103 if (already_available
< size
)
105 SIZE get
= ROUNDUP (size
- already_available
);
107 if ((*real_morecore
) (get
) == 0)
110 page_break_value
+= get
;
118 /* Obtain SIZE bytes of space and return a pointer to the new area.
119 If we could not allocate the space, return zero. */
122 get_more_space (size
)
125 POINTER ptr
= break_value
;
132 /* Note that SIZE bytes of space have been relinquished by the process.
133 If SIZE is more than a page, return the space to the system. */
139 POINTER new_page_break
;
142 new_page_break
= (POINTER
) ROUNDUP (break_value
);
144 if (new_page_break
!= page_break_value
)
146 if ((*real_morecore
) ((char *) new_page_break
147 - (char *) page_break_value
) == 0)
150 page_break_value
= new_page_break
;
153 /* Zero the space from the end of the "official" break to the actual
154 break, so that bugs show up faster. */
155 bzero (break_value
, ((char *) page_break_value
- (char *) break_value
));
158 /* The meat - allocating, freeing, and relocating blocs. */
160 /* These structures are allocated in the malloc arena.
161 The linked list is kept in order of increasing '.data' members.
162 The data blocks abut each other; if b->next is non-nil, then
163 b->data + b->size == b->next->data. */
173 #define NIL_BLOC ((bloc_ptr) 0)
174 #define BLOC_PTR_SIZE (sizeof (struct bp))
176 /* Head and tail of the list of relocatable blocs. */
177 static bloc_ptr first_bloc
, last_bloc
;
179 /* Find the bloc referenced by the address in PTR. Returns a pointer
186 register bloc_ptr p
= first_bloc
;
188 while (p
!= NIL_BLOC
)
190 if (p
->variable
== ptr
&& p
->data
== *ptr
)
199 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
200 Returns a pointer to the new bloc, or zero if we couldn't allocate
201 memory for the new block. */
207 register bloc_ptr new_bloc
;
209 if (! (new_bloc
= (bloc_ptr
) malloc (BLOC_PTR_SIZE
))
210 || ! (new_bloc
->data
= get_more_space (size
)))
218 new_bloc
->size
= size
;
219 new_bloc
->next
= NIL_BLOC
;
220 new_bloc
->variable
= (POINTER
*) NIL
;
224 new_bloc
->prev
= last_bloc
;
225 last_bloc
->next
= new_bloc
;
226 last_bloc
= new_bloc
;
230 first_bloc
= last_bloc
= new_bloc
;
231 new_bloc
->prev
= NIL_BLOC
;
237 /* Relocate all blocs from BLOC on upward in the list to the zone
238 indicated by ADDRESS. Direction of relocation is determined by
239 the position of ADDRESS relative to BLOC->data.
241 Note that ordering of blocs is not affected by this function. */
244 relocate_some_blocs (bloc
, address
)
249 POINTER data_zone
= bloc
->data
;
250 register SIZE data_zone_size
= 0;
251 register SIZE offset
= bloc
->data
- address
;
252 POINTER new_data_zone
= data_zone
- offset
;
254 for (b
= bloc
; b
!= NIL_BLOC
; b
= b
->next
)
256 data_zone_size
+= b
->size
;
258 *b
->variable
= b
->data
;
261 safe_bcopy (data_zone
, new_data_zone
, data_zone_size
);
264 /* Free BLOC from the chain of blocs, relocating any blocs above it
265 and returning BLOC->size bytes to the free area. */
271 if (bloc
== first_bloc
&& bloc
== last_bloc
)
273 first_bloc
= last_bloc
= NIL_BLOC
;
275 else if (bloc
== last_bloc
)
277 last_bloc
= bloc
->prev
;
278 last_bloc
->next
= NIL_BLOC
;
280 else if (bloc
== first_bloc
)
282 first_bloc
= bloc
->next
;
283 first_bloc
->prev
= NIL_BLOC
;
284 relocate_some_blocs (bloc
->next
, bloc
->data
);
288 bloc
->next
->prev
= bloc
->prev
;
289 bloc
->prev
->next
= bloc
->next
;
290 relocate_some_blocs (bloc
->next
, bloc
->data
);
293 relinquish (bloc
->size
);
297 /* Interface routines. */
299 static int use_relocatable_buffers
;
301 /* Obtain SIZE bytes of storage from the free pool, or the system, as
302 necessary. If relocatable blocs are in use, this means relocating
303 them. This function gets plugged into the GNU malloc's __morecore
306 If we're out of memory, we should return zero, to imitate the other
307 __morecore hook values - in particular, __default_morecore in the
308 GNU malloc package. */
316 if (! use_relocatable_buffers
)
317 return (*real_morecore
) (size
);
326 relocate_some_blocs (first_bloc
, first_bloc
->data
+ size
);
328 /* Zero out the space we just allocated, to help catch bugs
330 bzero (virtual_break_value
, size
);
336 relocate_some_blocs (first_bloc
, first_bloc
->data
+ size
);
340 ptr
= virtual_break_value
;
341 virtual_break_value
+= size
;
345 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
346 the data is returned in *PTR. PTR is thus the address of some variable
347 which will use the data area.
349 If we can't allocate the necessary memory, set *PTR to zero, and
357 register bloc_ptr new_bloc
;
359 if (! r_alloc_initialized
)
362 new_bloc
= get_bloc (size
);
365 new_bloc
->variable
= ptr
;
366 *ptr
= new_bloc
->data
;
374 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
375 Store 0 in *PTR to show there's no block allocated. */
379 register POINTER
*ptr
;
381 register bloc_ptr dead_bloc
;
383 dead_bloc
= find_bloc (ptr
);
384 if (dead_bloc
== NIL_BLOC
)
387 free_bloc (dead_bloc
);
391 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
392 Do this by shifting all blocks above this one up in memory, unless
393 SIZE is less than or equal to the current bloc size, in which case
396 Change *PTR to reflect the new bloc, and return this value.
398 If more memory cannot be allocated, then leave *PTR unchanged, and
402 r_re_alloc (ptr
, size
)
406 register bloc_ptr bloc
;
408 bloc
= find_bloc (ptr
);
409 if (bloc
== NIL_BLOC
)
412 if (size
<= bloc
->size
)
413 /* Wouldn't it be useful to actually resize the bloc here? */
416 if (! obtain (size
- bloc
->size
))
419 relocate_some_blocs (bloc
->next
, bloc
->data
+ size
);
421 /* Zero out the new space in the bloc, to help catch bugs faster. */
422 bzero (bloc
->data
+ bloc
->size
, size
- bloc
->size
);
424 /* Indicate that this block has a new size. */
430 /* The hook `malloc' uses for the function which gets more space
432 extern POINTER (*__morecore
) ();
434 /* Intialize various things for memory allocation. */
439 if (r_alloc_initialized
)
442 r_alloc_initialized
= 1;
443 real_morecore
= __morecore
;
444 __morecore
= r_alloc_sbrk
;
446 virtual_break_value
= break_value
= (*real_morecore
) (0);
447 if (break_value
== NIL
)
450 page_break_value
= (POINTER
) ROUNDUP (break_value
);
451 /* Clear the rest of the last page; this memory is in our address space
452 even though it is after the sbrk value. */
453 bzero (break_value
, (page_break_value
- break_value
));
454 use_relocatable_buffers
= 1;