1 /* Block-relocating memory allocator.
2 Copyright (C) 1993, 1995, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010, 2011 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 /* Declared in dispnew.c, this version doesn't screw up if regions
43 extern void safe_bcopy ();
45 #ifdef DOUG_LEA_MALLOC
47 extern int mallopt ();
48 #else /* not DOUG_LEA_MALLOC */
50 extern size_t __malloc_extra_blocks
;
51 #endif /* SYSTEM_MALLOC */
52 #endif /* not DOUG_LEA_MALLOC */
59 typedef void *POINTER
;
64 #define safe_bcopy(x, y, z) memmove (y, x, z)
65 #define bzero(x, len) memset (x, 0, len)
67 #endif /* not emacs */
70 #include "getpagesize.h"
72 #define NIL ((POINTER) 0)
74 /* A flag to indicate whether we have initialized ralloc yet. For
75 Emacs's sake, please do not make this local to malloc_init; on some
76 machines, the dumping procedure makes all static variables
77 read-only. On these machines, the word static is #defined to be
78 the empty string, meaning that r_alloc_initialized becomes an
79 automatic variable, and loses its value each time Emacs is started
82 static int r_alloc_initialized
= 0;
84 static void r_alloc_init ();
87 /* Declarations for working with the malloc, ralloc, and system breaks. */
89 /* Function to set the real break value. */
90 POINTER (*real_morecore
) ();
92 /* The break value, as seen by malloc. */
93 static POINTER virtual_break_value
;
95 /* The address of the end of the last data in use by ralloc,
96 including relocatable blocs as well as malloc data. */
97 static POINTER break_value
;
99 /* This is the size of a page. We round memory requests to this boundary. */
100 static int page_size
;
102 /* Whenever we get memory from the system, get this many extra bytes. This
103 must be a multiple of page_size. */
104 static int extra_bytes
;
106 /* Macros for rounding. Note that rounding to any value is possible
107 by changing the definition of PAGE. */
108 #define PAGE (getpagesize ())
109 #define ALIGNED(addr) (((unsigned long int) (addr) & (page_size - 1)) == 0)
110 #define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \
112 #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
114 #define MEM_ALIGN sizeof(double)
115 #define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \
118 /* The hook `malloc' uses for the function which gets more space
121 #ifndef SYSTEM_MALLOC
122 extern POINTER (*__morecore
) ();
127 /***********************************************************************
128 Implementation using sbrk
129 ***********************************************************************/
131 /* Data structures of heaps and blocs. */
133 /* The relocatable objects, or blocs, and the malloc data
134 both reside within one or more heaps.
135 Each heap contains malloc data, running from `start' to `bloc_start',
136 and relocatable objects, running from `bloc_start' to `free'.
138 Relocatable objects may relocate within the same heap
139 or may move into another heap; the heaps themselves may grow
142 We try to make just one heap and make it larger as necessary.
143 But sometimes we can't do that, because we can't get contiguous
144 space to add onto the heap. When that happens, we start a new heap. */
150 /* Start of memory range of this heap. */
152 /* End of memory range of this heap. */
154 /* Start of relocatable data in this heap. */
156 /* Start of unused space in this heap. */
158 /* First bloc in this heap. */
159 struct bp
*first_bloc
;
160 /* Last bloc in this heap. */
161 struct bp
*last_bloc
;
164 #define NIL_HEAP ((heap_ptr) 0)
165 #define HEAP_PTR_SIZE (sizeof (struct heap))
167 /* This is the first heap object.
168 If we need additional heap objects, each one resides at the beginning of
169 the space it covers. */
170 static struct heap heap_base
;
172 /* Head and tail of the list of heaps. */
173 static heap_ptr first_heap
, last_heap
;
175 /* These structures are allocated in the malloc arena.
176 The linked list is kept in order of increasing '.data' members.
177 The data blocks abut each other; if b->next is non-nil, then
178 b->data + b->size == b->next->data.
180 An element with variable==NIL denotes a freed block, which has not yet
181 been collected. They may only appear while r_alloc_freeze_level > 0,
182 and will be freed when the arena is thawed. Currently, these blocs are
183 not reusable, while the arena is frozen. Very inefficient. */
192 POINTER new_data
; /* temporarily used for relocation */
193 struct heap
*heap
; /* Heap this bloc is in. */
196 #define NIL_BLOC ((bloc_ptr) 0)
197 #define BLOC_PTR_SIZE (sizeof (struct bp))
199 /* Head and tail of the list of relocatable blocs. */
200 static bloc_ptr first_bloc
, last_bloc
;
202 static int use_relocatable_buffers
;
204 /* If >0, no relocation whatsoever takes place. */
205 static int r_alloc_freeze_level
;
208 /* Functions to get and return memory from the system. */
210 /* Find the heap that ADDRESS falls within. */
218 for (heap
= last_heap
; heap
; heap
= heap
->prev
)
220 if (heap
->start
<= address
&& address
<= heap
->end
)
227 /* Find SIZE bytes of space in a heap.
228 Try to get them at ADDRESS (which must fall within some heap's range)
229 if we can get that many within one heap.
231 If enough space is not presently available in our reserve, this means
232 getting more page-aligned space from the system. If the returned space
233 is not contiguous to the last heap, allocate a new heap, and append it
235 obtain does not try to keep track of whether space is in use
236 or not in use. It just returns the address of SIZE bytes that
237 fall within a single heap. If you call obtain twice in a row
238 with the same arguments, you typically get the same value.
239 to the heap list. It's the caller's responsibility to keep
240 track of what space is in use.
242 Return the address of the space if all went well, or zero if we couldn't
243 allocate the memory. */
246 obtain (address
, size
)
251 SIZE already_available
;
253 /* Find the heap that ADDRESS falls within. */
254 for (heap
= last_heap
; heap
; heap
= heap
->prev
)
256 if (heap
->start
<= address
&& address
<= heap
->end
)
263 /* If we can't fit SIZE bytes in that heap,
264 try successive later heaps. */
265 while (heap
&& (char *) address
+ size
> (char *) heap
->end
)
268 if (heap
== NIL_HEAP
)
270 address
= heap
->bloc_start
;
273 /* If we can't fit them within any existing heap,
275 if (heap
== NIL_HEAP
)
277 POINTER
new = (*real_morecore
)(0);
280 already_available
= (char *)last_heap
->end
- (char *)address
;
282 if (new != last_heap
->end
)
284 /* Someone else called sbrk. Make a new heap. */
286 heap_ptr new_heap
= (heap_ptr
) MEM_ROUNDUP (new);
287 POINTER bloc_start
= (POINTER
) MEM_ROUNDUP ((POINTER
)(new_heap
+ 1));
289 if ((*real_morecore
) ((char *) bloc_start
- (char *) new) != new)
292 new_heap
->start
= new;
293 new_heap
->end
= bloc_start
;
294 new_heap
->bloc_start
= bloc_start
;
295 new_heap
->free
= bloc_start
;
296 new_heap
->next
= NIL_HEAP
;
297 new_heap
->prev
= last_heap
;
298 new_heap
->first_bloc
= NIL_BLOC
;
299 new_heap
->last_bloc
= NIL_BLOC
;
300 last_heap
->next
= new_heap
;
301 last_heap
= new_heap
;
303 address
= bloc_start
;
304 already_available
= 0;
307 /* Add space to the last heap (which we may have just created).
308 Get some extra, so we can come here less often. */
310 get
= size
+ extra_bytes
- already_available
;
311 get
= (char *) ROUNDUP ((char *)last_heap
->end
+ get
)
312 - (char *) last_heap
->end
;
314 if ((*real_morecore
) (get
) != last_heap
->end
)
317 last_heap
->end
= (char *) last_heap
->end
+ get
;
323 /* Return unused heap space to the system
324 if there is a lot of unused space now.
325 This can make the last heap smaller;
326 it can also eliminate the last heap entirely. */
334 /* Add the amount of space beyond break_value
335 in all heaps which have extend beyond break_value at all. */
337 for (h
= last_heap
; h
&& break_value
< h
->end
; h
= h
->prev
)
339 excess
+= (char *) h
->end
- (char *) ((break_value
< h
->bloc_start
)
340 ? h
->bloc_start
: break_value
);
343 if (excess
> extra_bytes
* 2 && (*real_morecore
) (0) == last_heap
->end
)
345 /* Keep extra_bytes worth of empty space.
346 And don't free anything unless we can free at least extra_bytes. */
347 excess
-= extra_bytes
;
349 if ((char *)last_heap
->end
- (char *)last_heap
->bloc_start
<= excess
)
351 /* This heap should have no blocs in it. */
352 if (last_heap
->first_bloc
!= NIL_BLOC
353 || last_heap
->last_bloc
!= NIL_BLOC
)
356 /* Return the last heap, with its header, to the system. */
357 excess
= (char *)last_heap
->end
- (char *)last_heap
->start
;
358 last_heap
= last_heap
->prev
;
359 last_heap
->next
= NIL_HEAP
;
363 excess
= (char *) last_heap
->end
364 - (char *) ROUNDUP ((char *)last_heap
->end
- excess
);
365 last_heap
->end
= (char *) last_heap
->end
- excess
;
368 if ((*real_morecore
) (- excess
) == 0)
370 /* If the system didn't want that much memory back, adjust
371 the end of the last heap to reflect that. This can occur
372 if break_value is still within the original data segment. */
373 last_heap
->end
= (char *) last_heap
->end
+ excess
;
374 /* Make sure that the result of the adjustment is accurate.
375 It should be, for the else clause above; the other case,
376 which returns the entire last heap to the system, seems
377 unlikely to trigger this mode of failure. */
378 if (last_heap
->end
!= (*real_morecore
) (0))
384 /* Return the total size in use by relocating allocator,
385 above where malloc gets space. */
388 r_alloc_size_in_use ()
390 return (char *) break_value
- (char *) virtual_break_value
;
393 /* The meat - allocating, freeing, and relocating blocs. */
395 /* Find the bloc referenced by the address in PTR. Returns a pointer
402 register bloc_ptr p
= first_bloc
;
404 while (p
!= NIL_BLOC
)
406 /* Consistency check. Don't return inconsistent blocs.
407 Don't abort here, as callers might be expecting this, but
408 callers that always expect a bloc to be returned should abort
409 if one isn't to avoid a memory corruption bug that is
410 difficult to track down. */
411 if (p
->variable
== ptr
&& p
->data
== *ptr
)
420 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
421 Returns a pointer to the new bloc, or zero if we couldn't allocate
422 memory for the new block. */
428 register bloc_ptr new_bloc
;
429 register heap_ptr heap
;
431 if (! (new_bloc
= (bloc_ptr
) malloc (BLOC_PTR_SIZE
))
432 || ! (new_bloc
->data
= obtain (break_value
, size
)))
439 break_value
= (char *) new_bloc
->data
+ size
;
441 new_bloc
->size
= size
;
442 new_bloc
->next
= NIL_BLOC
;
443 new_bloc
->variable
= (POINTER
*) NIL
;
444 new_bloc
->new_data
= 0;
446 /* Record in the heap that this space is in use. */
447 heap
= find_heap (new_bloc
->data
);
448 heap
->free
= break_value
;
450 /* Maintain the correspondence between heaps and blocs. */
451 new_bloc
->heap
= heap
;
452 heap
->last_bloc
= new_bloc
;
453 if (heap
->first_bloc
== NIL_BLOC
)
454 heap
->first_bloc
= new_bloc
;
456 /* Put this bloc on the doubly-linked list of blocs. */
459 new_bloc
->prev
= last_bloc
;
460 last_bloc
->next
= new_bloc
;
461 last_bloc
= new_bloc
;
465 first_bloc
= last_bloc
= new_bloc
;
466 new_bloc
->prev
= NIL_BLOC
;
472 /* Calculate new locations of blocs in the list beginning with BLOC,
473 relocating it to start at ADDRESS, in heap HEAP. If enough space is
474 not presently available in our reserve, call obtain for
477 Store the new location of each bloc in its new_data field.
478 Do not touch the contents of blocs or break_value. */
481 relocate_blocs (bloc
, heap
, address
)
486 register bloc_ptr b
= bloc
;
488 /* No need to ever call this if arena is frozen, bug somewhere! */
489 if (r_alloc_freeze_level
)
494 /* If bloc B won't fit within HEAP,
495 move to the next heap and try again. */
496 while (heap
&& (char *) address
+ b
->size
> (char *) heap
->end
)
499 if (heap
== NIL_HEAP
)
501 address
= heap
->bloc_start
;
504 /* If BLOC won't fit in any heap,
505 get enough new space to hold BLOC and all following blocs. */
506 if (heap
== NIL_HEAP
)
508 register bloc_ptr tb
= b
;
511 /* Add up the size of all the following blocs. */
512 while (tb
!= NIL_BLOC
)
520 /* Get that space. */
521 address
= obtain (address
, s
);
528 /* Record the new address of this bloc
529 and update where the next bloc can start. */
530 b
->new_data
= address
;
532 address
= (char *) address
+ b
->size
;
539 /* Reorder the bloc BLOC to go before bloc BEFORE in the doubly linked list.
540 This is necessary if we put the memory of space of BLOC
541 before that of BEFORE. */
544 reorder_bloc (bloc
, before
)
545 bloc_ptr bloc
, before
;
549 /* Splice BLOC out from where it is. */
558 /* Splice it in before BEFORE. */
569 /* Update the records of which heaps contain which blocs, starting
570 with heap HEAP and bloc BLOC. */
573 update_heap_bloc_correspondence (bloc
, heap
)
579 /* Initialize HEAP's status to reflect blocs before BLOC. */
580 if (bloc
!= NIL_BLOC
&& bloc
->prev
!= NIL_BLOC
&& bloc
->prev
->heap
== heap
)
582 /* The previous bloc is in HEAP. */
583 heap
->last_bloc
= bloc
->prev
;
584 heap
->free
= (char *) bloc
->prev
->data
+ bloc
->prev
->size
;
588 /* HEAP contains no blocs before BLOC. */
589 heap
->first_bloc
= NIL_BLOC
;
590 heap
->last_bloc
= NIL_BLOC
;
591 heap
->free
= heap
->bloc_start
;
594 /* Advance through blocs one by one. */
595 for (b
= bloc
; b
!= NIL_BLOC
; b
= b
->next
)
597 /* Advance through heaps, marking them empty,
598 till we get to the one that B is in. */
601 if (heap
->bloc_start
<= b
->data
&& b
->data
<= heap
->end
)
604 /* We know HEAP is not null now,
605 because there has to be space for bloc B. */
606 heap
->first_bloc
= NIL_BLOC
;
607 heap
->last_bloc
= NIL_BLOC
;
608 heap
->free
= heap
->bloc_start
;
611 /* Update HEAP's status for bloc B. */
612 heap
->free
= (char *) b
->data
+ b
->size
;
614 if (heap
->first_bloc
== NIL_BLOC
)
615 heap
->first_bloc
= b
;
617 /* Record that B is in HEAP. */
621 /* If there are any remaining heaps and no blocs left,
622 mark those heaps as empty. */
626 heap
->first_bloc
= NIL_BLOC
;
627 heap
->last_bloc
= NIL_BLOC
;
628 heap
->free
= heap
->bloc_start
;
633 /* Resize BLOC to SIZE bytes. This relocates the blocs
634 that come after BLOC in memory. */
637 resize_bloc (bloc
, size
)
646 /* No need to ever call this if arena is frozen, bug somewhere! */
647 if (r_alloc_freeze_level
)
650 if (bloc
== NIL_BLOC
|| size
== bloc
->size
)
653 for (heap
= first_heap
; heap
!= NIL_HEAP
; heap
= heap
->next
)
655 if (heap
->bloc_start
<= bloc
->data
&& bloc
->data
<= heap
->end
)
659 if (heap
== NIL_HEAP
)
662 old_size
= bloc
->size
;
665 /* Note that bloc could be moved into the previous heap. */
666 address
= (bloc
->prev
? (char *) bloc
->prev
->data
+ bloc
->prev
->size
667 : (char *) first_heap
->bloc_start
);
670 if (heap
->bloc_start
<= address
&& address
<= heap
->end
)
675 if (! relocate_blocs (bloc
, heap
, address
))
677 bloc
->size
= old_size
;
683 for (b
= last_bloc
; b
!= bloc
; b
= b
->prev
)
688 b
->data
= b
->new_data
;
692 safe_bcopy (b
->data
, b
->new_data
, b
->size
);
693 *b
->variable
= b
->data
= b
->new_data
;
699 bloc
->data
= bloc
->new_data
;
703 safe_bcopy (bloc
->data
, bloc
->new_data
, old_size
);
704 bzero ((char *) bloc
->new_data
+ old_size
, size
- old_size
);
705 *bloc
->variable
= bloc
->data
= bloc
->new_data
;
710 for (b
= bloc
; b
!= NIL_BLOC
; b
= b
->next
)
715 b
->data
= b
->new_data
;
719 safe_bcopy (b
->data
, b
->new_data
, b
->size
);
720 *b
->variable
= b
->data
= b
->new_data
;
725 update_heap_bloc_correspondence (bloc
, heap
);
727 break_value
= (last_bloc
? (char *) last_bloc
->data
+ last_bloc
->size
728 : (char *) first_heap
->bloc_start
);
732 /* Free BLOC from the chain of blocs, relocating any blocs above it.
733 This may return space to the system. */
739 heap_ptr heap
= bloc
->heap
;
741 if (r_alloc_freeze_level
)
743 bloc
->variable
= (POINTER
*) NIL
;
747 resize_bloc (bloc
, 0);
749 if (bloc
== first_bloc
&& bloc
== last_bloc
)
751 first_bloc
= last_bloc
= NIL_BLOC
;
753 else if (bloc
== last_bloc
)
755 last_bloc
= bloc
->prev
;
756 last_bloc
->next
= NIL_BLOC
;
758 else if (bloc
== first_bloc
)
760 first_bloc
= bloc
->next
;
761 first_bloc
->prev
= NIL_BLOC
;
765 bloc
->next
->prev
= bloc
->prev
;
766 bloc
->prev
->next
= bloc
->next
;
769 /* Update the records of which blocs are in HEAP. */
770 if (heap
->first_bloc
== bloc
)
772 if (bloc
->next
!= 0 && bloc
->next
->heap
== heap
)
773 heap
->first_bloc
= bloc
->next
;
775 heap
->first_bloc
= heap
->last_bloc
= NIL_BLOC
;
777 if (heap
->last_bloc
== bloc
)
779 if (bloc
->prev
!= 0 && bloc
->prev
->heap
== heap
)
780 heap
->last_bloc
= bloc
->prev
;
782 heap
->first_bloc
= heap
->last_bloc
= NIL_BLOC
;
789 /* Interface routines. */
791 /* Obtain SIZE bytes of storage from the free pool, or the system, as
792 necessary. If relocatable blocs are in use, this means relocating
793 them. This function gets plugged into the GNU malloc's __morecore
796 We provide hysteresis, never relocating by less than extra_bytes.
798 If we're out of memory, we should return zero, to imitate the other
799 __morecore hook values - in particular, __default_morecore in the
800 GNU malloc package. */
809 if (! r_alloc_initialized
)
812 if (! use_relocatable_buffers
)
813 return (*real_morecore
) (size
);
816 return virtual_break_value
;
820 /* Allocate a page-aligned space. GNU malloc would reclaim an
821 extra space if we passed an unaligned one. But we could
822 not always find a space which is contiguous to the previous. */
823 POINTER new_bloc_start
;
824 heap_ptr h
= first_heap
;
825 SIZE get
= ROUNDUP (size
);
827 address
= (POINTER
) ROUNDUP (virtual_break_value
);
829 /* Search the list upward for a heap which is large enough. */
830 while ((char *) h
->end
< (char *) MEM_ROUNDUP ((char *)address
+ get
))
835 address
= (POINTER
) ROUNDUP (h
->start
);
838 /* If not found, obtain more space. */
841 get
+= extra_bytes
+ page_size
;
843 if (! obtain (address
, get
))
846 if (first_heap
== last_heap
)
847 address
= (POINTER
) ROUNDUP (virtual_break_value
);
849 address
= (POINTER
) ROUNDUP (last_heap
->start
);
853 new_bloc_start
= (POINTER
) MEM_ROUNDUP ((char *)address
+ get
);
855 if (first_heap
->bloc_start
< new_bloc_start
)
857 /* This is no clean solution - no idea how to do it better. */
858 if (r_alloc_freeze_level
)
861 /* There is a bug here: if the above obtain call succeeded, but the
862 relocate_blocs call below does not succeed, we need to free
863 the memory that we got with obtain. */
865 /* Move all blocs upward. */
866 if (! relocate_blocs (first_bloc
, h
, new_bloc_start
))
869 /* Note that (POINTER)(h+1) <= new_bloc_start since
870 get >= page_size, so the following does not destroy the heap
872 for (b
= last_bloc
; b
!= NIL_BLOC
; b
= b
->prev
)
874 safe_bcopy (b
->data
, b
->new_data
, b
->size
);
875 *b
->variable
= b
->data
= b
->new_data
;
878 h
->bloc_start
= new_bloc_start
;
880 update_heap_bloc_correspondence (first_bloc
, h
);
884 /* Give up managing heaps below the one the new
885 virtual_break_value points to. */
886 first_heap
->prev
= NIL_HEAP
;
887 first_heap
->next
= h
->next
;
888 first_heap
->start
= h
->start
;
889 first_heap
->end
= h
->end
;
890 first_heap
->free
= h
->free
;
891 first_heap
->first_bloc
= h
->first_bloc
;
892 first_heap
->last_bloc
= h
->last_bloc
;
893 first_heap
->bloc_start
= h
->bloc_start
;
895 if (first_heap
->next
)
896 first_heap
->next
->prev
= first_heap
;
898 last_heap
= first_heap
;
901 bzero (address
, size
);
905 SIZE excess
= (char *)first_heap
->bloc_start
906 - ((char *)virtual_break_value
+ size
);
908 address
= virtual_break_value
;
910 if (r_alloc_freeze_level
== 0 && excess
> 2 * extra_bytes
)
912 excess
-= extra_bytes
;
913 first_heap
->bloc_start
914 = (POINTER
) MEM_ROUNDUP ((char *)first_heap
->bloc_start
- excess
);
916 relocate_blocs (first_bloc
, first_heap
, first_heap
->bloc_start
);
918 for (b
= first_bloc
; b
!= NIL_BLOC
; b
= b
->next
)
920 safe_bcopy (b
->data
, b
->new_data
, b
->size
);
921 *b
->variable
= b
->data
= b
->new_data
;
925 if ((char *)virtual_break_value
+ size
< (char *)first_heap
->start
)
927 /* We found an additional space below the first heap */
928 first_heap
->start
= (POINTER
) ((char *)virtual_break_value
+ size
);
932 virtual_break_value
= (POINTER
) ((char *)address
+ size
);
933 break_value
= (last_bloc
934 ? (char *) last_bloc
->data
+ last_bloc
->size
935 : (char *) first_heap
->bloc_start
);
943 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
944 the data is returned in *PTR. PTR is thus the address of some variable
945 which will use the data area.
947 The allocation of 0 bytes is valid.
948 In case r_alloc_freeze_level is set, a best fit of unused blocs could be
949 done before allocating a new area. Not yet done.
951 If we can't allocate the necessary memory, set *PTR to zero, and
959 register bloc_ptr new_bloc
;
961 if (! r_alloc_initialized
)
964 new_bloc
= get_bloc (MEM_ROUNDUP (size
));
967 new_bloc
->variable
= ptr
;
968 *ptr
= new_bloc
->data
;
976 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
977 Store 0 in *PTR to show there's no block allocated. */
981 register POINTER
*ptr
;
983 register bloc_ptr dead_bloc
;
985 if (! r_alloc_initialized
)
988 dead_bloc
= find_bloc (ptr
);
989 if (dead_bloc
== NIL_BLOC
)
990 abort (); /* Double free? PTR not originally used to allocate? */
992 free_bloc (dead_bloc
);
996 refill_memory_reserve ();
1000 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
1001 Do this by shifting all blocks above this one up in memory, unless
1002 SIZE is less than or equal to the current bloc size, in which case
1005 In case r_alloc_freeze_level is set, a new bloc is allocated, and the
1006 memory copied to it. Not very efficient. We could traverse the
1007 bloc_list for a best fit of free blocs first.
1009 Change *PTR to reflect the new bloc, and return this value.
1011 If more memory cannot be allocated, then leave *PTR unchanged, and
1015 r_re_alloc (ptr
, size
)
1019 register bloc_ptr bloc
;
1021 if (! r_alloc_initialized
)
1025 return r_alloc (ptr
, size
);
1029 return r_alloc (ptr
, 0);
1032 bloc
= find_bloc (ptr
);
1033 if (bloc
== NIL_BLOC
)
1034 abort (); /* Already freed? PTR not originally used to allocate? */
1036 if (size
< bloc
->size
)
1038 /* Wouldn't it be useful to actually resize the bloc here? */
1039 /* I think so too, but not if it's too expensive... */
1040 if ((bloc
->size
- MEM_ROUNDUP (size
) >= page_size
)
1041 && r_alloc_freeze_level
== 0)
1043 resize_bloc (bloc
, MEM_ROUNDUP (size
));
1044 /* Never mind if this fails, just do nothing... */
1045 /* It *should* be infallible! */
1048 else if (size
> bloc
->size
)
1050 if (r_alloc_freeze_level
)
1053 new_bloc
= get_bloc (MEM_ROUNDUP (size
));
1056 new_bloc
->variable
= ptr
;
1057 *ptr
= new_bloc
->data
;
1058 bloc
->variable
= (POINTER
*) NIL
;
1065 if (! resize_bloc (bloc
, MEM_ROUNDUP (size
)))
1072 /* Disable relocations, after making room for at least SIZE bytes
1073 of non-relocatable heap if possible. The relocatable blocs are
1074 guaranteed to hold still until thawed, even if this means that
1075 malloc must return a null pointer. */
1078 r_alloc_freeze (size
)
1081 if (! r_alloc_initialized
)
1084 /* If already frozen, we can't make any more room, so don't try. */
1085 if (r_alloc_freeze_level
> 0)
1087 /* If we can't get the amount requested, half is better than nothing. */
1088 while (size
> 0 && r_alloc_sbrk (size
) == 0)
1090 ++r_alloc_freeze_level
;
1092 r_alloc_sbrk (-size
);
1099 if (! r_alloc_initialized
)
1102 if (--r_alloc_freeze_level
< 0)
1105 /* This frees all unused blocs. It is not too inefficient, as the resize
1106 and bcopy is done only once. Afterwards, all unreferenced blocs are
1107 already shrunk to zero size. */
1108 if (!r_alloc_freeze_level
)
1110 bloc_ptr
*b
= &first_bloc
;
1112 if (!(*b
)->variable
)
1120 #if defined (emacs) && defined (DOUG_LEA_MALLOC)
1122 /* Reinitialize the morecore hook variables after restarting a dumped
1123 Emacs. This is needed when using Doug Lea's malloc from GNU libc. */
1127 /* Only do this if the hook has been reset, so that we don't get an
1128 infinite loop, in case Emacs was linked statically. */
1129 if (__morecore
!= r_alloc_sbrk
)
1131 real_morecore
= __morecore
;
1132 __morecore
= r_alloc_sbrk
;
1136 #endif /* emacs && DOUG_LEA_MALLOC */
1149 if (!r_alloc_initialized
)
1152 assert (first_heap
);
1153 assert (last_heap
->end
<= (POINTER
) sbrk (0));
1154 assert ((POINTER
) first_heap
< first_heap
->start
);
1155 assert (first_heap
->start
<= virtual_break_value
);
1156 assert (virtual_break_value
<= first_heap
->end
);
1158 for (h
= first_heap
; h
; h
= h
->next
)
1160 assert (h
->prev
== ph
);
1161 assert ((POINTER
) ROUNDUP (h
->end
) == h
->end
);
1162 #if 0 /* ??? The code in ralloc.c does not really try to ensure
1163 the heap start has any sort of alignment.
1164 Perhaps it should. */
1165 assert ((POINTER
) MEM_ROUNDUP (h
->start
) == h
->start
);
1167 assert ((POINTER
) MEM_ROUNDUP (h
->bloc_start
) == h
->bloc_start
);
1168 assert (h
->start
<= h
->bloc_start
&& h
->bloc_start
<= h
->end
);
1172 assert (ph
->end
< h
->start
);
1173 assert (h
->start
<= (POINTER
)h
&& (POINTER
)(h
+1) <= h
->bloc_start
);
1176 if (h
->bloc_start
<= break_value
&& break_value
<= h
->end
)
1183 assert (last_heap
== ph
);
1185 for (b
= first_bloc
; b
; b
= b
->next
)
1187 assert (b
->prev
== pb
);
1188 assert ((POINTER
) MEM_ROUNDUP (b
->data
) == b
->data
);
1189 assert ((SIZE
) MEM_ROUNDUP (b
->size
) == b
->size
);
1192 for (h
= first_heap
; h
; h
= h
->next
)
1194 if (h
->bloc_start
<= b
->data
&& b
->data
+ b
->size
<= h
->end
)
1201 if (pb
&& pb
->data
+ pb
->size
!= b
->data
)
1203 assert (ph
&& b
->data
== h
->bloc_start
);
1206 if (ph
->bloc_start
<= pb
->data
1207 && pb
->data
+ pb
->size
<= ph
->end
)
1209 assert (pb
->data
+ pb
->size
+ b
->size
> ph
->end
);
1214 assert (ph
->bloc_start
+ b
->size
> ph
->end
);
1222 assert (last_bloc
== pb
);
1225 assert (last_bloc
->data
+ last_bloc
->size
== break_value
);
1227 assert (first_heap
->bloc_start
== break_value
);
1232 /* Update the internal record of which variable points to some data to NEW.
1233 Used by buffer-swap-text in Emacs to restore consistency after it
1234 swaps the buffer text between two buffer objects. The OLD pointer
1235 is checked to ensure that memory corruption does not occur due to
1238 r_alloc_reset_variable (old
, new)
1241 bloc_ptr bloc
= first_bloc
;
1243 /* Find the bloc that corresponds to the data pointed to by pointer.
1244 find_bloc cannot be used, as it has internal consistency checks
1245 which fail when the variable needs reseting. */
1246 while (bloc
!= NIL_BLOC
)
1248 if (bloc
->data
== *new)
1254 if (bloc
== NIL_BLOC
|| bloc
->variable
!= old
)
1255 abort (); /* Already freed? OLD not originally used to allocate? */
1257 /* Update variable to point to the new location. */
1258 bloc
->variable
= new;
1262 /***********************************************************************
1264 ***********************************************************************/
1266 /* Initialize various things for memory allocation. */
1271 if (r_alloc_initialized
)
1273 r_alloc_initialized
= 1;
1276 #ifndef SYSTEM_MALLOC
1277 real_morecore
= __morecore
;
1278 __morecore
= r_alloc_sbrk
;
1280 first_heap
= last_heap
= &heap_base
;
1281 first_heap
->next
= first_heap
->prev
= NIL_HEAP
;
1282 first_heap
->start
= first_heap
->bloc_start
1283 = virtual_break_value
= break_value
= (*real_morecore
) (0);
1284 if (break_value
== NIL
)
1287 extra_bytes
= ROUNDUP (50000);
1290 #ifdef DOUG_LEA_MALLOC
1292 mallopt (M_TOP_PAD
, 64 * 4096);
1295 #ifndef SYSTEM_MALLOC
1296 /* Give GNU malloc's morecore some hysteresis
1297 so that we move all the relocatable blocks much less often. */
1298 __malloc_extra_blocks
= 64;
1302 #ifndef SYSTEM_MALLOC
1303 first_heap
->end
= (POINTER
) ROUNDUP (first_heap
->start
);
1305 /* The extra call to real_morecore guarantees that the end of the
1306 address space is a multiple of page_size, even if page_size is
1307 not really the page size of the system running the binary in
1308 which page_size is stored. This allows a binary to be built on a
1309 system with one page size and run on a system with a smaller page
1311 (*real_morecore
) ((char *) first_heap
->end
- (char *) first_heap
->start
);
1313 /* Clear the rest of the last page; this memory is in our address space
1314 even though it is after the sbrk value. */
1315 /* Doubly true, with the additional call that explicitly adds the
1316 rest of that page to the address space. */
1317 bzero (first_heap
->start
,
1318 (char *) first_heap
->end
- (char *) first_heap
->start
);
1319 virtual_break_value
= break_value
= first_heap
->bloc_start
= first_heap
->end
;
1322 use_relocatable_buffers
= 1;
1325 /* arch-tag: 6a524a15-faff-44c8-95d4-a5da6f55110f
1326 (do not change this comment) */