Fix up "missing braces" warning.
[emacs.git] / src / ralloc.c
blob0a2b156e393621362c4fb02db11663d152e9e36a
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) ();
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) ();
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 /* 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. */
527 static void
528 reorder_bloc (bloc_ptr bloc, bloc_ptr before)
530 bloc_ptr prev, next;
532 /* Splice BLOC out from where it is. */
533 prev = bloc->prev;
534 next = bloc->next;
536 if (prev)
537 prev->next = next;
538 if (next)
539 next->prev = prev;
541 /* Splice it in before BEFORE. */
542 prev = before->prev;
544 if (prev)
545 prev->next = bloc;
546 bloc->prev = prev;
548 before->prev = bloc;
549 bloc->next = before;
552 /* Update the records of which heaps contain which blocs, starting
553 with heap HEAP and bloc BLOC. */
555 static void
556 update_heap_bloc_correspondence (bloc_ptr bloc, heap_ptr heap)
558 register bloc_ptr b;
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;
567 else
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. */
580 while (heap)
582 if (heap->bloc_start <= b->data && b->data <= heap->end)
583 break;
584 heap = heap->next;
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;
594 heap->last_bloc = b;
595 if (heap->first_bloc == NIL_BLOC)
596 heap->first_bloc = b;
598 /* Record that B is in HEAP. */
599 b->heap = heap;
602 /* If there are any remaining heaps and no blocs left,
603 mark those heaps as empty. */
604 heap = heap->next;
605 while (heap)
607 heap->first_bloc = NIL_BLOC;
608 heap->last_bloc = NIL_BLOC;
609 heap->free = heap->bloc_start;
610 heap = heap->next;
614 /* Resize BLOC to SIZE bytes. This relocates the blocs
615 that come after BLOC in memory. */
617 static int
618 resize_bloc (bloc_ptr bloc, SIZE size)
620 register bloc_ptr b;
621 heap_ptr heap;
622 POINTER address;
623 SIZE old_size;
625 /* No need to ever call this if arena is frozen, bug somewhere! */
626 if (r_alloc_freeze_level)
627 abort();
629 if (bloc == NIL_BLOC || size == bloc->size)
630 return 1;
632 for (heap = first_heap; heap != NIL_HEAP; heap = heap->next)
634 if (heap->bloc_start <= bloc->data && bloc->data <= heap->end)
635 break;
638 if (heap == NIL_HEAP)
639 abort ();
641 old_size = bloc->size;
642 bloc->size = 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);
647 while (heap)
649 if (heap->bloc_start <= address && address <= heap->end)
650 break;
651 heap = heap->prev;
654 if (! relocate_blocs (bloc, heap, address))
656 bloc->size = old_size;
657 return 0;
660 if (size > old_size)
662 for (b = last_bloc; b != bloc; b = b->prev)
664 if (!b->variable)
666 b->size = 0;
667 b->data = b->new_data;
669 else
671 memmove (b->new_data, b->data, b->size);
672 *b->variable = b->data = b->new_data;
675 if (!bloc->variable)
677 bloc->size = 0;
678 bloc->data = bloc->new_data;
680 else
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;
687 else
689 for (b = bloc; b != NIL_BLOC; b = b->next)
691 if (!b->variable)
693 b->size = 0;
694 b->data = b->new_data;
696 else
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);
708 return 1;
711 /* Free BLOC from the chain of blocs, relocating any blocs above it.
712 This may return space to the system. */
714 static void
715 free_bloc (bloc_ptr bloc)
717 heap_ptr heap = bloc->heap;
719 if (r_alloc_freeze_level)
721 bloc->variable = (POINTER *) NIL;
722 return;
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;
741 else
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;
752 else
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;
759 else
760 heap->first_bloc = heap->last_bloc = NIL_BLOC;
763 relinquish ();
764 free (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
772 hook.
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. */
780 POINTER
781 r_alloc_sbrk (long int size)
783 register bloc_ptr b;
784 POINTER address;
786 if (! r_alloc_initialized)
787 r_alloc_init ();
789 if (! use_relocatable_buffers)
790 return (*real_morecore) (size);
792 if (size == 0)
793 return virtual_break_value;
795 if (size > 0)
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))
809 h = h->next;
810 if (h == NIL_HEAP)
811 break;
812 address = (POINTER) ROUNDUP (h->start);
815 /* If not found, obtain more space. */
816 if (h == NIL_HEAP)
818 get += extra_bytes + page_size;
820 if (! obtain (address, get))
821 return 0;
823 if (first_heap == last_heap)
824 address = (POINTER) ROUNDUP (virtual_break_value);
825 else
826 address = (POINTER) ROUNDUP (last_heap->start);
827 h = last_heap;
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)
836 return NIL;
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))
844 return 0;
846 /* Note that (POINTER)(h+1) <= new_bloc_start since
847 get >= page_size, so the following does not destroy the heap
848 header. */
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);
859 if (h != first_heap)
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;
874 else
875 last_heap = first_heap;
878 memset (address, 0, size);
880 else /* size < 0 */
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);
913 if (size < 0)
914 relinquish ();
916 return address;
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
929 return zero. */
931 POINTER
932 r_alloc (POINTER *ptr, SIZE size)
934 register bloc_ptr new_bloc;
936 if (! r_alloc_initialized)
937 r_alloc_init ();
939 new_bloc = get_bloc (MEM_ROUNDUP (size));
940 if (new_bloc)
942 new_bloc->variable = ptr;
943 *ptr = new_bloc->data;
945 else
946 *ptr = 0;
948 return *ptr;
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. */
954 void
955 r_alloc_free (register POINTER *ptr)
957 register bloc_ptr dead_bloc;
959 if (! r_alloc_initialized)
960 r_alloc_init ();
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);
967 *ptr = 0;
969 #ifdef emacs
970 refill_memory_reserve ();
971 #endif
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
977 do nothing.
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
986 return zero. */
988 POINTER
989 r_re_alloc (POINTER *ptr, SIZE size)
991 register bloc_ptr bloc;
993 if (! r_alloc_initialized)
994 r_alloc_init ();
996 if (!*ptr)
997 return r_alloc (ptr, size);
998 if (!size)
1000 r_alloc_free (ptr);
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)
1024 bloc_ptr new_bloc;
1025 new_bloc = get_bloc (MEM_ROUNDUP (size));
1026 if (new_bloc)
1028 new_bloc->variable = ptr;
1029 *ptr = new_bloc->data;
1030 bloc->variable = (POINTER *) NIL;
1032 else
1033 return NIL;
1035 else
1037 if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
1038 return NIL;
1041 return *ptr;
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. */
1049 void
1050 r_alloc_freeze (long int size)
1052 if (! r_alloc_initialized)
1053 r_alloc_init ();
1055 /* If already frozen, we can't make any more room, so don't try. */
1056 if (r_alloc_freeze_level > 0)
1057 size = 0;
1058 /* If we can't get the amount requested, half is better than nothing. */
1059 while (size > 0 && r_alloc_sbrk (size) == 0)
1060 size /= 2;
1061 ++r_alloc_freeze_level;
1062 if (size > 0)
1063 r_alloc_sbrk (-size);
1066 void
1067 r_alloc_thaw (void)
1070 if (! r_alloc_initialized)
1071 r_alloc_init ();
1073 if (--r_alloc_freeze_level < 0)
1074 abort ();
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;
1082 while (*b)
1083 if (!(*b)->variable)
1084 free_bloc (*b);
1085 else
1086 b = &(*b)->next;
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. */
1095 void
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 */
1109 #ifdef DEBUG
1111 #include <assert.h>
1113 void
1114 r_alloc_check ()
1116 int found = 0;
1117 heap_ptr h, ph = 0;
1118 bloc_ptr b, pb = 0;
1120 if (!r_alloc_initialized)
1121 return;
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);
1137 #endif
1138 assert ((POINTER) MEM_ROUNDUP (h->bloc_start) == h->bloc_start);
1139 assert (h->start <= h->bloc_start && h->bloc_start <= h->end);
1141 if (ph)
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)
1148 found = 1;
1150 ph = h;
1153 assert (found);
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);
1162 ph = 0;
1163 for (h = first_heap; h; h = h->next)
1165 if (h->bloc_start <= b->data && b->data + b->size <= h->end)
1166 break;
1167 ph = h;
1170 assert (h);
1172 if (pb && pb->data + pb->size != b->data)
1174 assert (ph && b->data == h->bloc_start);
1175 while (ph)
1177 if (ph->bloc_start <= pb->data
1178 && pb->data + pb->size <= ph->end)
1180 assert (pb->data + pb->size + b->size > ph->end);
1181 break;
1183 else
1185 assert (ph->bloc_start + b->size > ph->end);
1187 ph = ph->prev;
1190 pb = b;
1193 assert (last_bloc == pb);
1195 if (last_bloc)
1196 assert (last_bloc->data + last_bloc->size == break_value);
1197 else
1198 assert (first_heap->bloc_start == break_value);
1201 #endif /* DEBUG */
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
1207 misuse. */
1208 void
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)
1219 break;
1221 bloc = bloc->next;
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 /***********************************************************************
1233 Initialization
1234 ***********************************************************************/
1236 /* Initialize various things for memory allocation. */
1238 static void
1239 r_alloc_init (void)
1241 if (r_alloc_initialized)
1242 return;
1243 r_alloc_initialized = 1;
1245 page_size = PAGE;
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)
1255 abort ();
1257 extra_bytes = ROUNDUP (50000);
1258 #endif
1260 #ifdef DOUG_LEA_MALLOC
1261 BLOCK_INPUT;
1262 mallopt (M_TOP_PAD, 64 * 4096);
1263 UNBLOCK_INPUT;
1264 #else
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;
1269 #endif
1270 #endif
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
1280 size. */
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;
1290 #endif
1292 use_relocatable_buffers = 1;
1295 /* arch-tag: 6a524a15-faff-44c8-95d4-a5da6f55110f
1296 (do not change this comment) */