1 /* Block-relocating memory allocator.
2 Copyright (C) 1993, 1995, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 Only relocate the blocs necessary 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. */
30 #include "lisp.h" /* Needed for VALBITS. */
31 #include "blockinput.h"
37 typedef POINTER_TYPE
*POINTER
;
40 #ifdef DOUG_LEA_MALLOC
42 extern int mallopt (int, int);
43 #else /* not DOUG_LEA_MALLOC */
45 extern size_t __malloc_extra_blocks
;
46 #endif /* SYSTEM_MALLOC */
47 #endif /* not DOUG_LEA_MALLOC */
54 typedef void *POINTER
;
59 #endif /* not emacs */
62 #include "getpagesize.h"
64 #define NIL ((POINTER) 0)
66 /* A flag to indicate whether we have initialized ralloc yet. For
67 Emacs's sake, please do not make this local to malloc_init; on some
68 machines, the dumping procedure makes all static variables
69 read-only. On these machines, the word static is #defined to be
70 the empty string, meaning that r_alloc_initialized becomes an
71 automatic variable, and loses its value each time Emacs is started
74 static int r_alloc_initialized
= 0;
76 static void r_alloc_init (void);
79 /* Declarations for working with the malloc, ralloc, and system breaks. */
81 /* Function to set the real break value. */
82 POINTER (*real_morecore
) ();
84 /* The break value, as seen by malloc. */
85 static POINTER virtual_break_value
;
87 /* The address of the end of the last data in use by ralloc,
88 including relocatable blocs as well as malloc data. */
89 static POINTER break_value
;
91 /* This is the size of a page. We round memory requests to this boundary. */
94 /* Whenever we get memory from the system, get this many extra bytes. This
95 must be a multiple of page_size. */
96 static int extra_bytes
;
98 /* Macros for rounding. Note that rounding to any value is possible
99 by changing the definition of PAGE. */
100 #define PAGE (getpagesize ())
101 #define ALIGNED(addr) (((unsigned long int) (addr) & (page_size - 1)) == 0)
102 #define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \
104 #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
106 #define MEM_ALIGN sizeof(double)
107 #define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \
110 /* The hook `malloc' uses for the function which gets more space
113 #ifndef SYSTEM_MALLOC
114 extern POINTER (*__morecore
) ();
119 /***********************************************************************
120 Implementation using sbrk
121 ***********************************************************************/
123 /* Data structures of heaps and blocs. */
125 /* The relocatable objects, or blocs, and the malloc data
126 both reside within one or more heaps.
127 Each heap contains malloc data, running from `start' to `bloc_start',
128 and relocatable objects, running from `bloc_start' to `free'.
130 Relocatable objects may relocate within the same heap
131 or may move into another heap; the heaps themselves may grow
134 We try to make just one heap and make it larger as necessary.
135 But sometimes we can't do that, because we can't get contiguous
136 space to add onto the heap. When that happens, we start a new heap. */
142 /* Start of memory range of this heap. */
144 /* End of memory range of this heap. */
146 /* Start of relocatable data in this heap. */
148 /* Start of unused space in this heap. */
150 /* First bloc in this heap. */
151 struct bp
*first_bloc
;
152 /* Last bloc in this heap. */
153 struct bp
*last_bloc
;
156 #define NIL_HEAP ((heap_ptr) 0)
157 #define HEAP_PTR_SIZE (sizeof (struct heap))
159 /* This is the first heap object.
160 If we need additional heap objects, each one resides at the beginning of
161 the space it covers. */
162 static struct heap heap_base
;
164 /* Head and tail of the list of heaps. */
165 static heap_ptr first_heap
, last_heap
;
167 /* These structures are allocated in the malloc arena.
168 The linked list is kept in order of increasing '.data' members.
169 The data blocks abut each other; if b->next is non-nil, then
170 b->data + b->size == b->next->data.
172 An element with variable==NIL denotes a freed block, which has not yet
173 been collected. They may only appear while r_alloc_freeze_level > 0,
174 and will be freed when the arena is thawed. Currently, these blocs are
175 not reusable, while the arena is frozen. Very inefficient. */
184 POINTER new_data
; /* temporarily used for relocation */
185 struct heap
*heap
; /* Heap this bloc is in. */
188 #define NIL_BLOC ((bloc_ptr) 0)
189 #define BLOC_PTR_SIZE (sizeof (struct bp))
191 /* Head and tail of the list of relocatable blocs. */
192 static bloc_ptr first_bloc
, last_bloc
;
194 static int use_relocatable_buffers
;
196 /* If >0, no relocation whatsoever takes place. */
197 static int r_alloc_freeze_level
;
200 /* Functions to get and return memory from the system. */
202 /* Find the heap that ADDRESS falls within. */
205 find_heap (POINTER address
)
209 for (heap
= last_heap
; heap
; heap
= heap
->prev
)
211 if (heap
->start
<= address
&& address
<= heap
->end
)
218 /* Find SIZE bytes of space in a heap.
219 Try to get them at ADDRESS (which must fall within some heap's range)
220 if we can get that many within one heap.
222 If enough space is not presently available in our reserve, this means
223 getting more page-aligned space from the system. If the returned space
224 is not contiguous to the last heap, allocate a new heap, and append it
226 obtain does not try to keep track of whether space is in use
227 or not in use. It just returns the address of SIZE bytes that
228 fall within a single heap. If you call obtain twice in a row
229 with the same arguments, you typically get the same value.
230 to the heap list. It's the caller's responsibility to keep
231 track of what space is in use.
233 Return the address of the space if all went well, or zero if we couldn't
234 allocate the memory. */
237 obtain (POINTER address
, SIZE size
)
240 SIZE already_available
;
242 /* Find the heap that ADDRESS falls within. */
243 for (heap
= last_heap
; heap
; heap
= heap
->prev
)
245 if (heap
->start
<= address
&& address
<= heap
->end
)
252 /* If we can't fit SIZE bytes in that heap,
253 try successive later heaps. */
254 while (heap
&& (char *) address
+ size
> (char *) heap
->end
)
257 if (heap
== NIL_HEAP
)
259 address
= heap
->bloc_start
;
262 /* If we can't fit them within any existing heap,
264 if (heap
== NIL_HEAP
)
266 POINTER
new = (*real_morecore
)(0);
269 already_available
= (char *)last_heap
->end
- (char *)address
;
271 if (new != last_heap
->end
)
273 /* Someone else called sbrk. Make a new heap. */
275 heap_ptr new_heap
= (heap_ptr
) MEM_ROUNDUP (new);
276 POINTER bloc_start
= (POINTER
) MEM_ROUNDUP ((POINTER
)(new_heap
+ 1));
278 if ((*real_morecore
) ((char *) bloc_start
- (char *) new) != new)
281 new_heap
->start
= new;
282 new_heap
->end
= bloc_start
;
283 new_heap
->bloc_start
= bloc_start
;
284 new_heap
->free
= bloc_start
;
285 new_heap
->next
= NIL_HEAP
;
286 new_heap
->prev
= last_heap
;
287 new_heap
->first_bloc
= NIL_BLOC
;
288 new_heap
->last_bloc
= NIL_BLOC
;
289 last_heap
->next
= new_heap
;
290 last_heap
= new_heap
;
292 address
= bloc_start
;
293 already_available
= 0;
296 /* Add space to the last heap (which we may have just created).
297 Get some extra, so we can come here less often. */
299 get
= size
+ extra_bytes
- already_available
;
300 get
= (char *) ROUNDUP ((char *)last_heap
->end
+ get
)
301 - (char *) last_heap
->end
;
303 if ((*real_morecore
) (get
) != last_heap
->end
)
306 last_heap
->end
= (char *) last_heap
->end
+ get
;
312 /* Return unused heap space to the system
313 if there is a lot of unused space now.
314 This can make the last heap smaller;
315 it can also eliminate the last heap entirely. */
323 /* Add the amount of space beyond break_value
324 in all heaps which have extend beyond break_value at all. */
326 for (h
= last_heap
; h
&& break_value
< h
->end
; h
= h
->prev
)
328 excess
+= (char *) h
->end
- (char *) ((break_value
< h
->bloc_start
)
329 ? h
->bloc_start
: break_value
);
332 if (excess
> extra_bytes
* 2 && (*real_morecore
) (0) == last_heap
->end
)
334 /* Keep extra_bytes worth of empty space.
335 And don't free anything unless we can free at least extra_bytes. */
336 excess
-= extra_bytes
;
338 if ((char *)last_heap
->end
- (char *)last_heap
->bloc_start
<= excess
)
340 /* This heap should have no blocs in it. */
341 if (last_heap
->first_bloc
!= NIL_BLOC
342 || last_heap
->last_bloc
!= NIL_BLOC
)
345 /* Return the last heap, with its header, to the system. */
346 excess
= (char *)last_heap
->end
- (char *)last_heap
->start
;
347 last_heap
= last_heap
->prev
;
348 last_heap
->next
= NIL_HEAP
;
352 excess
= (char *) last_heap
->end
353 - (char *) ROUNDUP ((char *)last_heap
->end
- excess
);
354 last_heap
->end
= (char *) last_heap
->end
- excess
;
357 if ((*real_morecore
) (- excess
) == 0)
359 /* If the system didn't want that much memory back, adjust
360 the end of the last heap to reflect that. This can occur
361 if break_value is still within the original data segment. */
362 last_heap
->end
= (char *) last_heap
->end
+ excess
;
363 /* Make sure that the result of the adjustment is accurate.
364 It should be, for the else clause above; the other case,
365 which returns the entire last heap to the system, seems
366 unlikely to trigger this mode of failure. */
367 if (last_heap
->end
!= (*real_morecore
) (0))
373 /* Return the total size in use by relocating allocator,
374 above where malloc gets space. */
377 r_alloc_size_in_use (void)
379 return (char *) break_value
- (char *) virtual_break_value
;
382 /* The meat - allocating, freeing, and relocating blocs. */
384 /* Find the bloc referenced by the address in PTR. Returns a pointer
388 find_bloc (POINTER
*ptr
)
390 register bloc_ptr p
= first_bloc
;
392 while (p
!= NIL_BLOC
)
394 /* Consistency check. Don't return inconsistent blocs.
395 Don't abort here, as callers might be expecting this, but
396 callers that always expect a bloc to be returned should abort
397 if one isn't to avoid a memory corruption bug that is
398 difficult to track down. */
399 if (p
->variable
== ptr
&& p
->data
== *ptr
)
408 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
409 Returns a pointer to the new bloc, or zero if we couldn't allocate
410 memory for the new block. */
415 register bloc_ptr new_bloc
;
416 register heap_ptr heap
;
418 if (! (new_bloc
= (bloc_ptr
) malloc (BLOC_PTR_SIZE
))
419 || ! (new_bloc
->data
= obtain (break_value
, size
)))
426 break_value
= (char *) new_bloc
->data
+ size
;
428 new_bloc
->size
= size
;
429 new_bloc
->next
= NIL_BLOC
;
430 new_bloc
->variable
= (POINTER
*) NIL
;
431 new_bloc
->new_data
= 0;
433 /* Record in the heap that this space is in use. */
434 heap
= find_heap (new_bloc
->data
);
435 heap
->free
= break_value
;
437 /* Maintain the correspondence between heaps and blocs. */
438 new_bloc
->heap
= heap
;
439 heap
->last_bloc
= new_bloc
;
440 if (heap
->first_bloc
== NIL_BLOC
)
441 heap
->first_bloc
= new_bloc
;
443 /* Put this bloc on the doubly-linked list of blocs. */
446 new_bloc
->prev
= last_bloc
;
447 last_bloc
->next
= new_bloc
;
448 last_bloc
= new_bloc
;
452 first_bloc
= last_bloc
= new_bloc
;
453 new_bloc
->prev
= NIL_BLOC
;
459 /* Calculate new locations of blocs in the list beginning with BLOC,
460 relocating it to start at ADDRESS, in heap HEAP. If enough space is
461 not presently available in our reserve, call obtain for
464 Store the new location of each bloc in its new_data field.
465 Do not touch the contents of blocs or break_value. */
468 relocate_blocs (bloc_ptr bloc
, heap_ptr heap
, POINTER address
)
470 register bloc_ptr b
= bloc
;
472 /* No need to ever call this if arena is frozen, bug somewhere! */
473 if (r_alloc_freeze_level
)
478 /* If bloc B won't fit within HEAP,
479 move to the next heap and try again. */
480 while (heap
&& (char *) address
+ b
->size
> (char *) heap
->end
)
483 if (heap
== NIL_HEAP
)
485 address
= heap
->bloc_start
;
488 /* If BLOC won't fit in any heap,
489 get enough new space to hold BLOC and all following blocs. */
490 if (heap
== NIL_HEAP
)
492 register bloc_ptr tb
= b
;
495 /* Add up the size of all the following blocs. */
496 while (tb
!= NIL_BLOC
)
504 /* Get that space. */
505 address
= obtain (address
, s
);
512 /* Record the new address of this bloc
513 and update where the next bloc can start. */
514 b
->new_data
= address
;
516 address
= (char *) address
+ b
->size
;
523 /* Reorder the bloc BLOC to go before bloc BEFORE in the doubly linked list.
524 This is necessary if we put the memory of space of BLOC
525 before that of BEFORE. */
528 reorder_bloc (bloc_ptr bloc
, bloc_ptr before
)
532 /* Splice BLOC out from where it is. */
541 /* Splice it in before BEFORE. */
552 /* Update the records of which heaps contain which blocs, starting
553 with heap HEAP and bloc BLOC. */
556 update_heap_bloc_correspondence (bloc_ptr bloc
, heap_ptr heap
)
560 /* Initialize HEAP's status to reflect blocs before BLOC. */
561 if (bloc
!= NIL_BLOC
&& bloc
->prev
!= NIL_BLOC
&& bloc
->prev
->heap
== heap
)
563 /* The previous bloc is in HEAP. */
564 heap
->last_bloc
= bloc
->prev
;
565 heap
->free
= (char *) bloc
->prev
->data
+ bloc
->prev
->size
;
569 /* HEAP contains no blocs before BLOC. */
570 heap
->first_bloc
= NIL_BLOC
;
571 heap
->last_bloc
= NIL_BLOC
;
572 heap
->free
= heap
->bloc_start
;
575 /* Advance through blocs one by one. */
576 for (b
= bloc
; b
!= NIL_BLOC
; b
= b
->next
)
578 /* Advance through heaps, marking them empty,
579 till we get to the one that B is in. */
582 if (heap
->bloc_start
<= b
->data
&& b
->data
<= heap
->end
)
585 /* We know HEAP is not null now,
586 because there has to be space for bloc B. */
587 heap
->first_bloc
= NIL_BLOC
;
588 heap
->last_bloc
= NIL_BLOC
;
589 heap
->free
= heap
->bloc_start
;
592 /* Update HEAP's status for bloc B. */
593 heap
->free
= (char *) b
->data
+ b
->size
;
595 if (heap
->first_bloc
== NIL_BLOC
)
596 heap
->first_bloc
= b
;
598 /* Record that B is in HEAP. */
602 /* If there are any remaining heaps and no blocs left,
603 mark those heaps as empty. */
607 heap
->first_bloc
= NIL_BLOC
;
608 heap
->last_bloc
= NIL_BLOC
;
609 heap
->free
= heap
->bloc_start
;
614 /* Resize BLOC to SIZE bytes. This relocates the blocs
615 that come after BLOC in memory. */
618 resize_bloc (bloc_ptr bloc
, SIZE size
)
625 /* No need to ever call this if arena is frozen, bug somewhere! */
626 if (r_alloc_freeze_level
)
629 if (bloc
== NIL_BLOC
|| size
== bloc
->size
)
632 for (heap
= first_heap
; heap
!= NIL_HEAP
; heap
= heap
->next
)
634 if (heap
->bloc_start
<= bloc
->data
&& bloc
->data
<= heap
->end
)
638 if (heap
== NIL_HEAP
)
641 old_size
= bloc
->size
;
644 /* Note that bloc could be moved into the previous heap. */
645 address
= (bloc
->prev
? (char *) bloc
->prev
->data
+ bloc
->prev
->size
646 : (char *) first_heap
->bloc_start
);
649 if (heap
->bloc_start
<= address
&& address
<= heap
->end
)
654 if (! relocate_blocs (bloc
, heap
, address
))
656 bloc
->size
= old_size
;
662 for (b
= last_bloc
; b
!= bloc
; b
= b
->prev
)
667 b
->data
= b
->new_data
;
671 memmove (b
->new_data
, b
->data
, b
->size
);
672 *b
->variable
= b
->data
= b
->new_data
;
678 bloc
->data
= bloc
->new_data
;
682 memmove (bloc
->new_data
, bloc
->data
, old_size
);
683 memset (bloc
->new_data
+ old_size
, 0, size
- old_size
);
684 *bloc
->variable
= bloc
->data
= bloc
->new_data
;
689 for (b
= bloc
; b
!= NIL_BLOC
; b
= b
->next
)
694 b
->data
= b
->new_data
;
698 memmove (b
->new_data
, b
->data
, b
->size
);
699 *b
->variable
= b
->data
= b
->new_data
;
704 update_heap_bloc_correspondence (bloc
, heap
);
706 break_value
= (last_bloc
? (char *) last_bloc
->data
+ last_bloc
->size
707 : (char *) first_heap
->bloc_start
);
711 /* Free BLOC from the chain of blocs, relocating any blocs above it.
712 This may return space to the system. */
715 free_bloc (bloc_ptr bloc
)
717 heap_ptr heap
= bloc
->heap
;
719 if (r_alloc_freeze_level
)
721 bloc
->variable
= (POINTER
*) NIL
;
725 resize_bloc (bloc
, 0);
727 if (bloc
== first_bloc
&& bloc
== last_bloc
)
729 first_bloc
= last_bloc
= NIL_BLOC
;
731 else if (bloc
== last_bloc
)
733 last_bloc
= bloc
->prev
;
734 last_bloc
->next
= NIL_BLOC
;
736 else if (bloc
== first_bloc
)
738 first_bloc
= bloc
->next
;
739 first_bloc
->prev
= NIL_BLOC
;
743 bloc
->next
->prev
= bloc
->prev
;
744 bloc
->prev
->next
= bloc
->next
;
747 /* Update the records of which blocs are in HEAP. */
748 if (heap
->first_bloc
== bloc
)
750 if (bloc
->next
!= 0 && bloc
->next
->heap
== heap
)
751 heap
->first_bloc
= bloc
->next
;
753 heap
->first_bloc
= heap
->last_bloc
= NIL_BLOC
;
755 if (heap
->last_bloc
== bloc
)
757 if (bloc
->prev
!= 0 && bloc
->prev
->heap
== heap
)
758 heap
->last_bloc
= bloc
->prev
;
760 heap
->first_bloc
= heap
->last_bloc
= NIL_BLOC
;
767 /* Interface routines. */
769 /* Obtain SIZE bytes of storage from the free pool, or the system, as
770 necessary. If relocatable blocs are in use, this means relocating
771 them. This function gets plugged into the GNU malloc's __morecore
774 We provide hysteresis, never relocating by less than extra_bytes.
776 If we're out of memory, we should return zero, to imitate the other
777 __morecore hook values - in particular, __default_morecore in the
778 GNU malloc package. */
781 r_alloc_sbrk (long int size
)
786 if (! r_alloc_initialized
)
789 if (! use_relocatable_buffers
)
790 return (*real_morecore
) (size
);
793 return virtual_break_value
;
797 /* Allocate a page-aligned space. GNU malloc would reclaim an
798 extra space if we passed an unaligned one. But we could
799 not always find a space which is contiguous to the previous. */
800 POINTER new_bloc_start
;
801 heap_ptr h
= first_heap
;
802 SIZE get
= ROUNDUP (size
);
804 address
= (POINTER
) ROUNDUP (virtual_break_value
);
806 /* Search the list upward for a heap which is large enough. */
807 while ((char *) h
->end
< (char *) MEM_ROUNDUP ((char *)address
+ get
))
812 address
= (POINTER
) ROUNDUP (h
->start
);
815 /* If not found, obtain more space. */
818 get
+= extra_bytes
+ page_size
;
820 if (! obtain (address
, get
))
823 if (first_heap
== last_heap
)
824 address
= (POINTER
) ROUNDUP (virtual_break_value
);
826 address
= (POINTER
) ROUNDUP (last_heap
->start
);
830 new_bloc_start
= (POINTER
) MEM_ROUNDUP ((char *)address
+ get
);
832 if (first_heap
->bloc_start
< new_bloc_start
)
834 /* This is no clean solution - no idea how to do it better. */
835 if (r_alloc_freeze_level
)
838 /* There is a bug here: if the above obtain call succeeded, but the
839 relocate_blocs call below does not succeed, we need to free
840 the memory that we got with obtain. */
842 /* Move all blocs upward. */
843 if (! relocate_blocs (first_bloc
, h
, new_bloc_start
))
846 /* Note that (POINTER)(h+1) <= new_bloc_start since
847 get >= page_size, so the following does not destroy the heap
849 for (b
= last_bloc
; b
!= NIL_BLOC
; b
= b
->prev
)
851 memmove (b
->new_data
, b
->data
, b
->size
);
852 *b
->variable
= b
->data
= b
->new_data
;
855 h
->bloc_start
= new_bloc_start
;
857 update_heap_bloc_correspondence (first_bloc
, h
);
861 /* Give up managing heaps below the one the new
862 virtual_break_value points to. */
863 first_heap
->prev
= NIL_HEAP
;
864 first_heap
->next
= h
->next
;
865 first_heap
->start
= h
->start
;
866 first_heap
->end
= h
->end
;
867 first_heap
->free
= h
->free
;
868 first_heap
->first_bloc
= h
->first_bloc
;
869 first_heap
->last_bloc
= h
->last_bloc
;
870 first_heap
->bloc_start
= h
->bloc_start
;
872 if (first_heap
->next
)
873 first_heap
->next
->prev
= first_heap
;
875 last_heap
= first_heap
;
878 memset (address
, 0, size
);
882 SIZE excess
= (char *)first_heap
->bloc_start
883 - ((char *)virtual_break_value
+ size
);
885 address
= virtual_break_value
;
887 if (r_alloc_freeze_level
== 0 && excess
> 2 * extra_bytes
)
889 excess
-= extra_bytes
;
890 first_heap
->bloc_start
891 = (POINTER
) MEM_ROUNDUP ((char *)first_heap
->bloc_start
- excess
);
893 relocate_blocs (first_bloc
, first_heap
, first_heap
->bloc_start
);
895 for (b
= first_bloc
; b
!= NIL_BLOC
; b
= b
->next
)
897 memmove (b
->new_data
, b
->data
, b
->size
);
898 *b
->variable
= b
->data
= b
->new_data
;
902 if ((char *)virtual_break_value
+ size
< (char *)first_heap
->start
)
904 /* We found an additional space below the first heap */
905 first_heap
->start
= (POINTER
) ((char *)virtual_break_value
+ size
);
909 virtual_break_value
= (POINTER
) ((char *)address
+ size
);
910 break_value
= (last_bloc
911 ? (char *) last_bloc
->data
+ last_bloc
->size
912 : (char *) first_heap
->bloc_start
);
920 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
921 the data is returned in *PTR. PTR is thus the address of some variable
922 which will use the data area.
924 The allocation of 0 bytes is valid.
925 In case r_alloc_freeze_level is set, a best fit of unused blocs could be
926 done before allocating a new area. Not yet done.
928 If we can't allocate the necessary memory, set *PTR to zero, and
932 r_alloc (POINTER
*ptr
, SIZE size
)
934 register bloc_ptr new_bloc
;
936 if (! r_alloc_initialized
)
939 new_bloc
= get_bloc (MEM_ROUNDUP (size
));
942 new_bloc
->variable
= ptr
;
943 *ptr
= new_bloc
->data
;
951 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
952 Store 0 in *PTR to show there's no block allocated. */
955 r_alloc_free (register POINTER
*ptr
)
957 register bloc_ptr dead_bloc
;
959 if (! r_alloc_initialized
)
962 dead_bloc
= find_bloc (ptr
);
963 if (dead_bloc
== NIL_BLOC
)
964 abort (); /* Double free? PTR not originally used to allocate? */
966 free_bloc (dead_bloc
);
970 refill_memory_reserve ();
974 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
975 Do this by shifting all blocks above this one up in memory, unless
976 SIZE is less than or equal to the current bloc size, in which case
979 In case r_alloc_freeze_level is set, a new bloc is allocated, and the
980 memory copied to it. Not very efficient. We could traverse the
981 bloc_list for a best fit of free blocs first.
983 Change *PTR to reflect the new bloc, and return this value.
985 If more memory cannot be allocated, then leave *PTR unchanged, and
989 r_re_alloc (POINTER
*ptr
, SIZE size
)
991 register bloc_ptr bloc
;
993 if (! r_alloc_initialized
)
997 return r_alloc (ptr
, size
);
1001 return r_alloc (ptr
, 0);
1004 bloc
= find_bloc (ptr
);
1005 if (bloc
== NIL_BLOC
)
1006 abort (); /* Already freed? PTR not originally used to allocate? */
1008 if (size
< bloc
->size
)
1010 /* Wouldn't it be useful to actually resize the bloc here? */
1011 /* I think so too, but not if it's too expensive... */
1012 if ((bloc
->size
- MEM_ROUNDUP (size
) >= page_size
)
1013 && r_alloc_freeze_level
== 0)
1015 resize_bloc (bloc
, MEM_ROUNDUP (size
));
1016 /* Never mind if this fails, just do nothing... */
1017 /* It *should* be infallible! */
1020 else if (size
> bloc
->size
)
1022 if (r_alloc_freeze_level
)
1025 new_bloc
= get_bloc (MEM_ROUNDUP (size
));
1028 new_bloc
->variable
= ptr
;
1029 *ptr
= new_bloc
->data
;
1030 bloc
->variable
= (POINTER
*) NIL
;
1037 if (! resize_bloc (bloc
, MEM_ROUNDUP (size
)))
1044 /* Disable relocations, after making room for at least SIZE bytes
1045 of non-relocatable heap if possible. The relocatable blocs are
1046 guaranteed to hold still until thawed, even if this means that
1047 malloc must return a null pointer. */
1050 r_alloc_freeze (long int size
)
1052 if (! r_alloc_initialized
)
1055 /* If already frozen, we can't make any more room, so don't try. */
1056 if (r_alloc_freeze_level
> 0)
1058 /* If we can't get the amount requested, half is better than nothing. */
1059 while (size
> 0 && r_alloc_sbrk (size
) == 0)
1061 ++r_alloc_freeze_level
;
1063 r_alloc_sbrk (-size
);
1070 if (! r_alloc_initialized
)
1073 if (--r_alloc_freeze_level
< 0)
1076 /* This frees all unused blocs. It is not too inefficient, as the resize
1077 and memcpy is done only once. Afterwards, all unreferenced blocs are
1078 already shrunk to zero size. */
1079 if (!r_alloc_freeze_level
)
1081 bloc_ptr
*b
= &first_bloc
;
1083 if (!(*b
)->variable
)
1091 #if defined (emacs) && defined (DOUG_LEA_MALLOC)
1093 /* Reinitialize the morecore hook variables after restarting a dumped
1094 Emacs. This is needed when using Doug Lea's malloc from GNU libc. */
1096 r_alloc_reinit (void)
1098 /* Only do this if the hook has been reset, so that we don't get an
1099 infinite loop, in case Emacs was linked statically. */
1100 if (__morecore
!= r_alloc_sbrk
)
1102 real_morecore
= __morecore
;
1103 __morecore
= r_alloc_sbrk
;
1107 #endif /* emacs && DOUG_LEA_MALLOC */
1120 if (!r_alloc_initialized
)
1123 assert (first_heap
);
1124 assert (last_heap
->end
<= (POINTER
) sbrk (0));
1125 assert ((POINTER
) first_heap
< first_heap
->start
);
1126 assert (first_heap
->start
<= virtual_break_value
);
1127 assert (virtual_break_value
<= first_heap
->end
);
1129 for (h
= first_heap
; h
; h
= h
->next
)
1131 assert (h
->prev
== ph
);
1132 assert ((POINTER
) ROUNDUP (h
->end
) == h
->end
);
1133 #if 0 /* ??? The code in ralloc.c does not really try to ensure
1134 the heap start has any sort of alignment.
1135 Perhaps it should. */
1136 assert ((POINTER
) MEM_ROUNDUP (h
->start
) == h
->start
);
1138 assert ((POINTER
) MEM_ROUNDUP (h
->bloc_start
) == h
->bloc_start
);
1139 assert (h
->start
<= h
->bloc_start
&& h
->bloc_start
<= h
->end
);
1143 assert (ph
->end
< h
->start
);
1144 assert (h
->start
<= (POINTER
)h
&& (POINTER
)(h
+1) <= h
->bloc_start
);
1147 if (h
->bloc_start
<= break_value
&& break_value
<= h
->end
)
1154 assert (last_heap
== ph
);
1156 for (b
= first_bloc
; b
; b
= b
->next
)
1158 assert (b
->prev
== pb
);
1159 assert ((POINTER
) MEM_ROUNDUP (b
->data
) == b
->data
);
1160 assert ((SIZE
) MEM_ROUNDUP (b
->size
) == b
->size
);
1163 for (h
= first_heap
; h
; h
= h
->next
)
1165 if (h
->bloc_start
<= b
->data
&& b
->data
+ b
->size
<= h
->end
)
1172 if (pb
&& pb
->data
+ pb
->size
!= b
->data
)
1174 assert (ph
&& b
->data
== h
->bloc_start
);
1177 if (ph
->bloc_start
<= pb
->data
1178 && pb
->data
+ pb
->size
<= ph
->end
)
1180 assert (pb
->data
+ pb
->size
+ b
->size
> ph
->end
);
1185 assert (ph
->bloc_start
+ b
->size
> ph
->end
);
1193 assert (last_bloc
== pb
);
1196 assert (last_bloc
->data
+ last_bloc
->size
== break_value
);
1198 assert (first_heap
->bloc_start
== break_value
);
1203 /* Update the internal record of which variable points to some data to NEW.
1204 Used by buffer-swap-text in Emacs to restore consistency after it
1205 swaps the buffer text between two buffer objects. The OLD pointer
1206 is checked to ensure that memory corruption does not occur due to
1209 r_alloc_reset_variable (POINTER
*old
, POINTER
*new)
1211 bloc_ptr bloc
= first_bloc
;
1213 /* Find the bloc that corresponds to the data pointed to by pointer.
1214 find_bloc cannot be used, as it has internal consistency checks
1215 which fail when the variable needs reseting. */
1216 while (bloc
!= NIL_BLOC
)
1218 if (bloc
->data
== *new)
1224 if (bloc
== NIL_BLOC
|| bloc
->variable
!= old
)
1225 abort (); /* Already freed? OLD not originally used to allocate? */
1227 /* Update variable to point to the new location. */
1228 bloc
->variable
= new;
1232 /***********************************************************************
1234 ***********************************************************************/
1236 /* Initialize various things for memory allocation. */
1241 if (r_alloc_initialized
)
1243 r_alloc_initialized
= 1;
1246 #ifndef SYSTEM_MALLOC
1247 real_morecore
= __morecore
;
1248 __morecore
= r_alloc_sbrk
;
1250 first_heap
= last_heap
= &heap_base
;
1251 first_heap
->next
= first_heap
->prev
= NIL_HEAP
;
1252 first_heap
->start
= first_heap
->bloc_start
1253 = virtual_break_value
= break_value
= (*real_morecore
) (0);
1254 if (break_value
== NIL
)
1257 extra_bytes
= ROUNDUP (50000);
1260 #ifdef DOUG_LEA_MALLOC
1262 mallopt (M_TOP_PAD
, 64 * 4096);
1265 #ifndef SYSTEM_MALLOC
1266 /* Give GNU malloc's morecore some hysteresis
1267 so that we move all the relocatable blocks much less often. */
1268 __malloc_extra_blocks
= 64;
1272 #ifndef SYSTEM_MALLOC
1273 first_heap
->end
= (POINTER
) ROUNDUP (first_heap
->start
);
1275 /* The extra call to real_morecore guarantees that the end of the
1276 address space is a multiple of page_size, even if page_size is
1277 not really the page size of the system running the binary in
1278 which page_size is stored. This allows a binary to be built on a
1279 system with one page size and run on a system with a smaller page
1281 (*real_morecore
) ((char *) first_heap
->end
- (char *) first_heap
->start
);
1283 /* Clear the rest of the last page; this memory is in our address space
1284 even though it is after the sbrk value. */
1285 /* Doubly true, with the additional call that explicitly adds the
1286 rest of that page to the address space. */
1287 memset (first_heap
->start
, 0,
1288 (char *) first_heap
->end
- (char *) first_heap
->start
);
1289 virtual_break_value
= break_value
= first_heap
->bloc_start
= first_heap
->end
;
1292 use_relocatable_buffers
= 1;
1295 /* arch-tag: 6a524a15-faff-44c8-95d4-a5da6f55110f
1296 (do not change this comment) */