Merge from trunk
[emacs.git] / src / ralloc.c
blob5f2b52fcc4be004ea80f1a2154a78cf592c0d970
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/>. */
20 /* NOTES:
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. */
26 #ifdef emacs
28 #include <config.h>
29 #include <setjmp.h>
30 #include "lisp.h" /* Needed for VALBITS. */
31 #include "blockinput.h"
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
37 typedef POINTER_TYPE *POINTER;
38 typedef size_t SIZE;
40 #ifdef DOUG_LEA_MALLOC
41 #define M_TOP_PAD -2
42 extern int mallopt (int, int);
43 #else /* not DOUG_LEA_MALLOC */
44 #ifndef SYSTEM_MALLOC
45 extern size_t __malloc_extra_blocks;
46 #endif /* SYSTEM_MALLOC */
47 #endif /* not DOUG_LEA_MALLOC */
49 #else /* not emacs */
51 #include <stddef.h>
53 typedef size_t SIZE;
54 typedef void *POINTER;
56 #include <unistd.h>
57 #include <malloc.h>
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
72 up. */
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) (long int);
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. */
92 static int page_size;
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) \
103 & ~(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) \
108 & ~(MEM_ALIGN - 1))
110 /* The hook `malloc' uses for the function which gets more space
111 from the system. */
113 #ifndef SYSTEM_MALLOC
114 extern POINTER (*__morecore) (long int);
115 #endif
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
132 but they never move.
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. */
138 typedef struct heap
140 struct heap *next;
141 struct heap *prev;
142 /* Start of memory range of this heap. */
143 POINTER start;
144 /* End of memory range of this heap. */
145 POINTER end;
146 /* Start of relocatable data in this heap. */
147 POINTER bloc_start;
148 /* Start of unused space in this heap. */
149 POINTER free;
150 /* First bloc in this heap. */
151 struct bp *first_bloc;
152 /* Last bloc in this heap. */
153 struct bp *last_bloc;
154 } *heap_ptr;
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. */
177 typedef struct bp
179 struct bp *next;
180 struct bp *prev;
181 POINTER *variable;
182 POINTER data;
183 SIZE size;
184 POINTER new_data; /* temporarily used for relocation */
185 struct heap *heap; /* Heap this bloc is in. */
186 } *bloc_ptr;
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. */
204 static heap_ptr
205 find_heap (POINTER address)
207 heap_ptr heap;
209 for (heap = last_heap; heap; heap = heap->prev)
211 if (heap->start <= address && address <= heap->end)
212 return heap;
215 return NIL_HEAP;
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. */
236 static POINTER
237 obtain (POINTER address, SIZE size)
239 heap_ptr heap;
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)
246 break;
249 if (! heap)
250 abort ();
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)
256 heap = heap->next;
257 if (heap == NIL_HEAP)
258 break;
259 address = heap->bloc_start;
262 /* If we can't fit them within any existing heap,
263 get more space. */
264 if (heap == NIL_HEAP)
266 POINTER new = (*real_morecore)(0);
267 SIZE get;
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)
279 return 0;
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)
304 return 0;
306 last_heap->end = (char *) last_heap->end + get;
309 return address;
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. */
317 static void
318 relinquish (void)
320 register heap_ptr h;
321 long excess = 0;
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)
343 abort ();
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;
350 else
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))
368 abort ();
373 /* Return the total size in use by relocating allocator,
374 above where malloc gets space. */
376 long
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
385 to that block. */
387 static bloc_ptr
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)
400 return p;
402 p = p->next;
405 return p;
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. */
412 static bloc_ptr
413 get_bloc (SIZE size)
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)))
421 free (new_bloc);
423 return 0;
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. */
444 if (first_bloc)
446 new_bloc->prev = last_bloc;
447 last_bloc->next = new_bloc;
448 last_bloc = new_bloc;
450 else
452 first_bloc = last_bloc = new_bloc;
453 new_bloc->prev = NIL_BLOC;
456 return new_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
462 more space.
464 Store the new location of each bloc in its new_data field.
465 Do not touch the contents of blocs or break_value. */
467 static int
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)
474 abort();
476 while (b)
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)
482 heap = heap->next;
483 if (heap == NIL_HEAP)
484 break;
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;
493 register SIZE s = 0;
495 /* Add up the size of all the following blocs. */
496 while (tb != NIL_BLOC)
498 if (tb->variable)
499 s += tb->size;
501 tb = tb->next;
504 /* Get that space. */
505 address = obtain (address, s);
506 if (address == 0)
507 return 0;
509 heap = last_heap;
512 /* Record the new address of this bloc
513 and update where the next bloc can start. */
514 b->new_data = address;
515 if (b->variable)
516 address = (char *) address + b->size;
517 b = b->next;
520 return 1;
523 /* Update the records of which heaps contain which blocs, starting
524 with heap HEAP and bloc BLOC. */
526 static void
527 update_heap_bloc_correspondence (bloc_ptr bloc, heap_ptr heap)
529 register bloc_ptr b;
531 /* Initialize HEAP's status to reflect blocs before BLOC. */
532 if (bloc != NIL_BLOC && bloc->prev != NIL_BLOC && bloc->prev->heap == heap)
534 /* The previous bloc is in HEAP. */
535 heap->last_bloc = bloc->prev;
536 heap->free = (char *) bloc->prev->data + bloc->prev->size;
538 else
540 /* HEAP contains no blocs before BLOC. */
541 heap->first_bloc = NIL_BLOC;
542 heap->last_bloc = NIL_BLOC;
543 heap->free = heap->bloc_start;
546 /* Advance through blocs one by one. */
547 for (b = bloc; b != NIL_BLOC; b = b->next)
549 /* Advance through heaps, marking them empty,
550 till we get to the one that B is in. */
551 while (heap)
553 if (heap->bloc_start <= b->data && b->data <= heap->end)
554 break;
555 heap = heap->next;
556 /* We know HEAP is not null now,
557 because there has to be space for bloc B. */
558 heap->first_bloc = NIL_BLOC;
559 heap->last_bloc = NIL_BLOC;
560 heap->free = heap->bloc_start;
563 /* Update HEAP's status for bloc B. */
564 heap->free = (char *) b->data + b->size;
565 heap->last_bloc = b;
566 if (heap->first_bloc == NIL_BLOC)
567 heap->first_bloc = b;
569 /* Record that B is in HEAP. */
570 b->heap = heap;
573 /* If there are any remaining heaps and no blocs left,
574 mark those heaps as empty. */
575 heap = heap->next;
576 while (heap)
578 heap->first_bloc = NIL_BLOC;
579 heap->last_bloc = NIL_BLOC;
580 heap->free = heap->bloc_start;
581 heap = heap->next;
585 /* Resize BLOC to SIZE bytes. This relocates the blocs
586 that come after BLOC in memory. */
588 static int
589 resize_bloc (bloc_ptr bloc, SIZE size)
591 register bloc_ptr b;
592 heap_ptr heap;
593 POINTER address;
594 SIZE old_size;
596 /* No need to ever call this if arena is frozen, bug somewhere! */
597 if (r_alloc_freeze_level)
598 abort();
600 if (bloc == NIL_BLOC || size == bloc->size)
601 return 1;
603 for (heap = first_heap; heap != NIL_HEAP; heap = heap->next)
605 if (heap->bloc_start <= bloc->data && bloc->data <= heap->end)
606 break;
609 if (heap == NIL_HEAP)
610 abort ();
612 old_size = bloc->size;
613 bloc->size = size;
615 /* Note that bloc could be moved into the previous heap. */
616 address = (bloc->prev ? (char *) bloc->prev->data + bloc->prev->size
617 : (char *) first_heap->bloc_start);
618 while (heap)
620 if (heap->bloc_start <= address && address <= heap->end)
621 break;
622 heap = heap->prev;
625 if (! relocate_blocs (bloc, heap, address))
627 bloc->size = old_size;
628 return 0;
631 if (size > old_size)
633 for (b = last_bloc; b != bloc; b = b->prev)
635 if (!b->variable)
637 b->size = 0;
638 b->data = b->new_data;
640 else
642 memmove (b->new_data, b->data, b->size);
643 *b->variable = b->data = b->new_data;
646 if (!bloc->variable)
648 bloc->size = 0;
649 bloc->data = bloc->new_data;
651 else
653 memmove (bloc->new_data, bloc->data, old_size);
654 memset (bloc->new_data + old_size, 0, size - old_size);
655 *bloc->variable = bloc->data = bloc->new_data;
658 else
660 for (b = bloc; b != NIL_BLOC; b = b->next)
662 if (!b->variable)
664 b->size = 0;
665 b->data = b->new_data;
667 else
669 memmove (b->new_data, b->data, b->size);
670 *b->variable = b->data = b->new_data;
675 update_heap_bloc_correspondence (bloc, heap);
677 break_value = (last_bloc ? (char *) last_bloc->data + last_bloc->size
678 : (char *) first_heap->bloc_start);
679 return 1;
682 /* Free BLOC from the chain of blocs, relocating any blocs above it.
683 This may return space to the system. */
685 static void
686 free_bloc (bloc_ptr bloc)
688 heap_ptr heap = bloc->heap;
690 if (r_alloc_freeze_level)
692 bloc->variable = (POINTER *) NIL;
693 return;
696 resize_bloc (bloc, 0);
698 if (bloc == first_bloc && bloc == last_bloc)
700 first_bloc = last_bloc = NIL_BLOC;
702 else if (bloc == last_bloc)
704 last_bloc = bloc->prev;
705 last_bloc->next = NIL_BLOC;
707 else if (bloc == first_bloc)
709 first_bloc = bloc->next;
710 first_bloc->prev = NIL_BLOC;
712 else
714 bloc->next->prev = bloc->prev;
715 bloc->prev->next = bloc->next;
718 /* Update the records of which blocs are in HEAP. */
719 if (heap->first_bloc == bloc)
721 if (bloc->next != 0 && bloc->next->heap == heap)
722 heap->first_bloc = bloc->next;
723 else
724 heap->first_bloc = heap->last_bloc = NIL_BLOC;
726 if (heap->last_bloc == bloc)
728 if (bloc->prev != 0 && bloc->prev->heap == heap)
729 heap->last_bloc = bloc->prev;
730 else
731 heap->first_bloc = heap->last_bloc = NIL_BLOC;
734 relinquish ();
735 free (bloc);
738 /* Interface routines. */
740 /* Obtain SIZE bytes of storage from the free pool, or the system, as
741 necessary. If relocatable blocs are in use, this means relocating
742 them. This function gets plugged into the GNU malloc's __morecore
743 hook.
745 We provide hysteresis, never relocating by less than extra_bytes.
747 If we're out of memory, we should return zero, to imitate the other
748 __morecore hook values - in particular, __default_morecore in the
749 GNU malloc package. */
751 POINTER
752 r_alloc_sbrk (long int size)
754 register bloc_ptr b;
755 POINTER address;
757 if (! r_alloc_initialized)
758 r_alloc_init ();
760 if (! use_relocatable_buffers)
761 return (*real_morecore) (size);
763 if (size == 0)
764 return virtual_break_value;
766 if (size > 0)
768 /* Allocate a page-aligned space. GNU malloc would reclaim an
769 extra space if we passed an unaligned one. But we could
770 not always find a space which is contiguous to the previous. */
771 POINTER new_bloc_start;
772 heap_ptr h = first_heap;
773 SIZE get = ROUNDUP (size);
775 address = (POINTER) ROUNDUP (virtual_break_value);
777 /* Search the list upward for a heap which is large enough. */
778 while ((char *) h->end < (char *) MEM_ROUNDUP ((char *)address + get))
780 h = h->next;
781 if (h == NIL_HEAP)
782 break;
783 address = (POINTER) ROUNDUP (h->start);
786 /* If not found, obtain more space. */
787 if (h == NIL_HEAP)
789 get += extra_bytes + page_size;
791 if (! obtain (address, get))
792 return 0;
794 if (first_heap == last_heap)
795 address = (POINTER) ROUNDUP (virtual_break_value);
796 else
797 address = (POINTER) ROUNDUP (last_heap->start);
798 h = last_heap;
801 new_bloc_start = (POINTER) MEM_ROUNDUP ((char *)address + get);
803 if (first_heap->bloc_start < new_bloc_start)
805 /* This is no clean solution - no idea how to do it better. */
806 if (r_alloc_freeze_level)
807 return NIL;
809 /* There is a bug here: if the above obtain call succeeded, but the
810 relocate_blocs call below does not succeed, we need to free
811 the memory that we got with obtain. */
813 /* Move all blocs upward. */
814 if (! relocate_blocs (first_bloc, h, new_bloc_start))
815 return 0;
817 /* Note that (POINTER)(h+1) <= new_bloc_start since
818 get >= page_size, so the following does not destroy the heap
819 header. */
820 for (b = last_bloc; b != NIL_BLOC; b = b->prev)
822 memmove (b->new_data, b->data, b->size);
823 *b->variable = b->data = b->new_data;
826 h->bloc_start = new_bloc_start;
828 update_heap_bloc_correspondence (first_bloc, h);
830 if (h != first_heap)
832 /* Give up managing heaps below the one the new
833 virtual_break_value points to. */
834 first_heap->prev = NIL_HEAP;
835 first_heap->next = h->next;
836 first_heap->start = h->start;
837 first_heap->end = h->end;
838 first_heap->free = h->free;
839 first_heap->first_bloc = h->first_bloc;
840 first_heap->last_bloc = h->last_bloc;
841 first_heap->bloc_start = h->bloc_start;
843 if (first_heap->next)
844 first_heap->next->prev = first_heap;
845 else
846 last_heap = first_heap;
849 memset (address, 0, size);
851 else /* size < 0 */
853 SIZE excess = (char *)first_heap->bloc_start
854 - ((char *)virtual_break_value + size);
856 address = virtual_break_value;
858 if (r_alloc_freeze_level == 0 && excess > 2 * extra_bytes)
860 excess -= extra_bytes;
861 first_heap->bloc_start
862 = (POINTER) MEM_ROUNDUP ((char *)first_heap->bloc_start - excess);
864 relocate_blocs (first_bloc, first_heap, first_heap->bloc_start);
866 for (b = first_bloc; b != NIL_BLOC; b = b->next)
868 memmove (b->new_data, b->data, b->size);
869 *b->variable = b->data = b->new_data;
873 if ((char *)virtual_break_value + size < (char *)first_heap->start)
875 /* We found an additional space below the first heap */
876 first_heap->start = (POINTER) ((char *)virtual_break_value + size);
880 virtual_break_value = (POINTER) ((char *)address + size);
881 break_value = (last_bloc
882 ? (char *) last_bloc->data + last_bloc->size
883 : (char *) first_heap->bloc_start);
884 if (size < 0)
885 relinquish ();
887 return address;
891 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
892 the data is returned in *PTR. PTR is thus the address of some variable
893 which will use the data area.
895 The allocation of 0 bytes is valid.
896 In case r_alloc_freeze_level is set, a best fit of unused blocs could be
897 done before allocating a new area. Not yet done.
899 If we can't allocate the necessary memory, set *PTR to zero, and
900 return zero. */
902 POINTER
903 r_alloc (POINTER *ptr, SIZE size)
905 register bloc_ptr new_bloc;
907 if (! r_alloc_initialized)
908 r_alloc_init ();
910 new_bloc = get_bloc (MEM_ROUNDUP (size));
911 if (new_bloc)
913 new_bloc->variable = ptr;
914 *ptr = new_bloc->data;
916 else
917 *ptr = 0;
919 return *ptr;
922 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
923 Store 0 in *PTR to show there's no block allocated. */
925 void
926 r_alloc_free (register POINTER *ptr)
928 register bloc_ptr dead_bloc;
930 if (! r_alloc_initialized)
931 r_alloc_init ();
933 dead_bloc = find_bloc (ptr);
934 if (dead_bloc == NIL_BLOC)
935 abort (); /* Double free? PTR not originally used to allocate? */
937 free_bloc (dead_bloc);
938 *ptr = 0;
940 #ifdef emacs
941 refill_memory_reserve ();
942 #endif
945 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
946 Do this by shifting all blocks above this one up in memory, unless
947 SIZE is less than or equal to the current bloc size, in which case
948 do nothing.
950 In case r_alloc_freeze_level is set, a new bloc is allocated, and the
951 memory copied to it. Not very efficient. We could traverse the
952 bloc_list for a best fit of free blocs first.
954 Change *PTR to reflect the new bloc, and return this value.
956 If more memory cannot be allocated, then leave *PTR unchanged, and
957 return zero. */
959 POINTER
960 r_re_alloc (POINTER *ptr, SIZE size)
962 register bloc_ptr bloc;
964 if (! r_alloc_initialized)
965 r_alloc_init ();
967 if (!*ptr)
968 return r_alloc (ptr, size);
969 if (!size)
971 r_alloc_free (ptr);
972 return r_alloc (ptr, 0);
975 bloc = find_bloc (ptr);
976 if (bloc == NIL_BLOC)
977 abort (); /* Already freed? PTR not originally used to allocate? */
979 if (size < bloc->size)
981 /* Wouldn't it be useful to actually resize the bloc here? */
982 /* I think so too, but not if it's too expensive... */
983 if ((bloc->size - MEM_ROUNDUP (size) >= page_size)
984 && r_alloc_freeze_level == 0)
986 resize_bloc (bloc, MEM_ROUNDUP (size));
987 /* Never mind if this fails, just do nothing... */
988 /* It *should* be infallible! */
991 else if (size > bloc->size)
993 if (r_alloc_freeze_level)
995 bloc_ptr new_bloc;
996 new_bloc = get_bloc (MEM_ROUNDUP (size));
997 if (new_bloc)
999 new_bloc->variable = ptr;
1000 *ptr = new_bloc->data;
1001 bloc->variable = (POINTER *) NIL;
1003 else
1004 return NIL;
1006 else
1008 if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
1009 return NIL;
1012 return *ptr;
1015 /* Disable relocations, after making room for at least SIZE bytes
1016 of non-relocatable heap if possible. The relocatable blocs are
1017 guaranteed to hold still until thawed, even if this means that
1018 malloc must return a null pointer. */
1020 void
1021 r_alloc_freeze (long int size)
1023 if (! r_alloc_initialized)
1024 r_alloc_init ();
1026 /* If already frozen, we can't make any more room, so don't try. */
1027 if (r_alloc_freeze_level > 0)
1028 size = 0;
1029 /* If we can't get the amount requested, half is better than nothing. */
1030 while (size > 0 && r_alloc_sbrk (size) == 0)
1031 size /= 2;
1032 ++r_alloc_freeze_level;
1033 if (size > 0)
1034 r_alloc_sbrk (-size);
1037 void
1038 r_alloc_thaw (void)
1041 if (! r_alloc_initialized)
1042 r_alloc_init ();
1044 if (--r_alloc_freeze_level < 0)
1045 abort ();
1047 /* This frees all unused blocs. It is not too inefficient, as the resize
1048 and memcpy is done only once. Afterwards, all unreferenced blocs are
1049 already shrunk to zero size. */
1050 if (!r_alloc_freeze_level)
1052 bloc_ptr *b = &first_bloc;
1053 while (*b)
1054 if (!(*b)->variable)
1055 free_bloc (*b);
1056 else
1057 b = &(*b)->next;
1062 #if defined (emacs) && defined (DOUG_LEA_MALLOC)
1064 /* Reinitialize the morecore hook variables after restarting a dumped
1065 Emacs. This is needed when using Doug Lea's malloc from GNU libc. */
1066 void
1067 r_alloc_reinit (void)
1069 /* Only do this if the hook has been reset, so that we don't get an
1070 infinite loop, in case Emacs was linked statically. */
1071 if (__morecore != r_alloc_sbrk)
1073 real_morecore = __morecore;
1074 __morecore = r_alloc_sbrk;
1078 #endif /* emacs && DOUG_LEA_MALLOC */
1080 #ifdef DEBUG
1082 #include <assert.h>
1084 void
1085 r_alloc_check ()
1087 int found = 0;
1088 heap_ptr h, ph = 0;
1089 bloc_ptr b, pb = 0;
1091 if (!r_alloc_initialized)
1092 return;
1094 assert (first_heap);
1095 assert (last_heap->end <= (POINTER) sbrk (0));
1096 assert ((POINTER) first_heap < first_heap->start);
1097 assert (first_heap->start <= virtual_break_value);
1098 assert (virtual_break_value <= first_heap->end);
1100 for (h = first_heap; h; h = h->next)
1102 assert (h->prev == ph);
1103 assert ((POINTER) ROUNDUP (h->end) == h->end);
1104 #if 0 /* ??? The code in ralloc.c does not really try to ensure
1105 the heap start has any sort of alignment.
1106 Perhaps it should. */
1107 assert ((POINTER) MEM_ROUNDUP (h->start) == h->start);
1108 #endif
1109 assert ((POINTER) MEM_ROUNDUP (h->bloc_start) == h->bloc_start);
1110 assert (h->start <= h->bloc_start && h->bloc_start <= h->end);
1112 if (ph)
1114 assert (ph->end < h->start);
1115 assert (h->start <= (POINTER)h && (POINTER)(h+1) <= h->bloc_start);
1118 if (h->bloc_start <= break_value && break_value <= h->end)
1119 found = 1;
1121 ph = h;
1124 assert (found);
1125 assert (last_heap == ph);
1127 for (b = first_bloc; b; b = b->next)
1129 assert (b->prev == pb);
1130 assert ((POINTER) MEM_ROUNDUP (b->data) == b->data);
1131 assert ((SIZE) MEM_ROUNDUP (b->size) == b->size);
1133 ph = 0;
1134 for (h = first_heap; h; h = h->next)
1136 if (h->bloc_start <= b->data && b->data + b->size <= h->end)
1137 break;
1138 ph = h;
1141 assert (h);
1143 if (pb && pb->data + pb->size != b->data)
1145 assert (ph && b->data == h->bloc_start);
1146 while (ph)
1148 if (ph->bloc_start <= pb->data
1149 && pb->data + pb->size <= ph->end)
1151 assert (pb->data + pb->size + b->size > ph->end);
1152 break;
1154 else
1156 assert (ph->bloc_start + b->size > ph->end);
1158 ph = ph->prev;
1161 pb = b;
1164 assert (last_bloc == pb);
1166 if (last_bloc)
1167 assert (last_bloc->data + last_bloc->size == break_value);
1168 else
1169 assert (first_heap->bloc_start == break_value);
1172 #endif /* DEBUG */
1174 /* Update the internal record of which variable points to some data to NEW.
1175 Used by buffer-swap-text in Emacs to restore consistency after it
1176 swaps the buffer text between two buffer objects. The OLD pointer
1177 is checked to ensure that memory corruption does not occur due to
1178 misuse. */
1179 void
1180 r_alloc_reset_variable (POINTER *old, POINTER *new)
1182 bloc_ptr bloc = first_bloc;
1184 /* Find the bloc that corresponds to the data pointed to by pointer.
1185 find_bloc cannot be used, as it has internal consistency checks
1186 which fail when the variable needs reseting. */
1187 while (bloc != NIL_BLOC)
1189 if (bloc->data == *new)
1190 break;
1192 bloc = bloc->next;
1195 if (bloc == NIL_BLOC || bloc->variable != old)
1196 abort (); /* Already freed? OLD not originally used to allocate? */
1198 /* Update variable to point to the new location. */
1199 bloc->variable = new;
1203 /***********************************************************************
1204 Initialization
1205 ***********************************************************************/
1207 /* Initialize various things for memory allocation. */
1209 static void
1210 r_alloc_init (void)
1212 if (r_alloc_initialized)
1213 return;
1214 r_alloc_initialized = 1;
1216 page_size = PAGE;
1217 #ifndef SYSTEM_MALLOC
1218 real_morecore = __morecore;
1219 __morecore = r_alloc_sbrk;
1221 first_heap = last_heap = &heap_base;
1222 first_heap->next = first_heap->prev = NIL_HEAP;
1223 first_heap->start = first_heap->bloc_start
1224 = virtual_break_value = break_value = (*real_morecore) (0);
1225 if (break_value == NIL)
1226 abort ();
1228 extra_bytes = ROUNDUP (50000);
1229 #endif
1231 #ifdef DOUG_LEA_MALLOC
1232 BLOCK_INPUT;
1233 mallopt (M_TOP_PAD, 64 * 4096);
1234 UNBLOCK_INPUT;
1235 #else
1236 #ifndef SYSTEM_MALLOC
1237 /* Give GNU malloc's morecore some hysteresis
1238 so that we move all the relocatable blocks much less often. */
1239 __malloc_extra_blocks = 64;
1240 #endif
1241 #endif
1243 #ifndef SYSTEM_MALLOC
1244 first_heap->end = (POINTER) ROUNDUP (first_heap->start);
1246 /* The extra call to real_morecore guarantees that the end of the
1247 address space is a multiple of page_size, even if page_size is
1248 not really the page size of the system running the binary in
1249 which page_size is stored. This allows a binary to be built on a
1250 system with one page size and run on a system with a smaller page
1251 size. */
1252 (*real_morecore) ((char *) first_heap->end - (char *) first_heap->start);
1254 /* Clear the rest of the last page; this memory is in our address space
1255 even though it is after the sbrk value. */
1256 /* Doubly true, with the additional call that explicitly adds the
1257 rest of that page to the address space. */
1258 memset (first_heap->start, 0,
1259 (char *) first_heap->end - (char *) first_heap->start);
1260 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
1261 #endif
1263 use_relocatable_buffers = 1;
1266 /* arch-tag: 6a524a15-faff-44c8-95d4-a5da6f55110f
1267 (do not change this comment) */