lisp/gnus/ChangeLog: Cosmetic fix
[emacs.git] / src / ralloc.c
blob0d8bcaf4ffbaab5bea19d36c1f594297f39295d0
1 /* Block-relocating memory allocator.
2 Copyright (C) 1993, 1995, 2000-2012 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19 /* NOTES:
21 Only relocate the blocs necessary for SIZE in r_alloc_sbrk,
22 rather than all of them. This means allowing for a possible
23 hole between the first bloc and the end of malloc storage. */
25 #ifdef emacs
27 #include <config.h>
28 #include <setjmp.h>
29 #include "lisp.h" /* Needed for VALBITS. */
30 #include "blockinput.h"
32 #include <unistd.h>
34 #ifdef DOUG_LEA_MALLOC
35 #define M_TOP_PAD -2
36 extern int mallopt (int, int);
37 #else /* not DOUG_LEA_MALLOC */
38 #ifndef SYSTEM_MALLOC
39 extern size_t __malloc_extra_blocks;
40 #endif /* SYSTEM_MALLOC */
41 #endif /* not DOUG_LEA_MALLOC */
43 #else /* not emacs */
45 #include <stddef.h>
47 #include <unistd.h>
48 #include <malloc.h>
50 #endif /* not emacs */
53 #include "getpagesize.h"
55 typedef size_t SIZE;
56 typedef void *POINTER;
57 #define NIL ((POINTER) 0)
59 /* A flag to indicate whether we have initialized ralloc yet. For
60 Emacs's sake, please do not make this local to malloc_init; on some
61 machines, the dumping procedure makes all static variables
62 read-only. On these machines, the word static is #defined to be
63 the empty string, meaning that r_alloc_initialized becomes an
64 automatic variable, and loses its value each time Emacs is started
65 up. */
67 static int r_alloc_initialized = 0;
69 static void r_alloc_init (void);
72 /* Declarations for working with the malloc, ralloc, and system breaks. */
74 /* Function to set the real break value. */
75 POINTER (*real_morecore) (long int);
77 /* The break value, as seen by malloc. */
78 static POINTER virtual_break_value;
80 /* The address of the end of the last data in use by ralloc,
81 including relocatable blocs as well as malloc data. */
82 static POINTER break_value;
84 /* This is the size of a page. We round memory requests to this boundary. */
85 static int page_size;
87 /* Whenever we get memory from the system, get this many extra bytes. This
88 must be a multiple of page_size. */
89 static int extra_bytes;
91 /* Macros for rounding. Note that rounding to any value is possible
92 by changing the definition of PAGE. */
93 #define PAGE (getpagesize ())
94 #define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \
95 & ~(page_size - 1))
97 #define MEM_ALIGN sizeof (double)
98 #define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \
99 & ~(MEM_ALIGN - 1))
101 /* The hook `malloc' uses for the function which gets more space
102 from the system. */
104 #ifndef SYSTEM_MALLOC
105 extern POINTER (*__morecore) (long int);
106 #endif
110 /***********************************************************************
111 Implementation using sbrk
112 ***********************************************************************/
114 /* Data structures of heaps and blocs. */
116 /* The relocatable objects, or blocs, and the malloc data
117 both reside within one or more heaps.
118 Each heap contains malloc data, running from `start' to `bloc_start',
119 and relocatable objects, running from `bloc_start' to `free'.
121 Relocatable objects may relocate within the same heap
122 or may move into another heap; the heaps themselves may grow
123 but they never move.
125 We try to make just one heap and make it larger as necessary.
126 But sometimes we can't do that, because we can't get contiguous
127 space to add onto the heap. When that happens, we start a new heap. */
129 typedef struct heap
131 struct heap *next;
132 struct heap *prev;
133 /* Start of memory range of this heap. */
134 POINTER start;
135 /* End of memory range of this heap. */
136 POINTER end;
137 /* Start of relocatable data in this heap. */
138 POINTER bloc_start;
139 /* Start of unused space in this heap. */
140 POINTER free;
141 /* First bloc in this heap. */
142 struct bp *first_bloc;
143 /* Last bloc in this heap. */
144 struct bp *last_bloc;
145 } *heap_ptr;
147 #define NIL_HEAP ((heap_ptr) 0)
149 /* This is the first heap object.
150 If we need additional heap objects, each one resides at the beginning of
151 the space it covers. */
152 static struct heap heap_base;
154 /* Head and tail of the list of heaps. */
155 static heap_ptr first_heap, last_heap;
157 /* These structures are allocated in the malloc arena.
158 The linked list is kept in order of increasing '.data' members.
159 The data blocks abut each other; if b->next is non-nil, then
160 b->data + b->size == b->next->data.
162 An element with variable==NIL denotes a freed block, which has not yet
163 been collected. They may only appear while r_alloc_freeze_level > 0,
164 and will be freed when the arena is thawed. Currently, these blocs are
165 not reusable, while the arena is frozen. Very inefficient. */
167 typedef struct bp
169 struct bp *next;
170 struct bp *prev;
171 POINTER *variable;
172 POINTER data;
173 SIZE size;
174 POINTER new_data; /* temporarily used for relocation */
175 struct heap *heap; /* Heap this bloc is in. */
176 } *bloc_ptr;
178 #define NIL_BLOC ((bloc_ptr) 0)
179 #define BLOC_PTR_SIZE (sizeof (struct bp))
181 /* Head and tail of the list of relocatable blocs. */
182 static bloc_ptr first_bloc, last_bloc;
184 static int use_relocatable_buffers;
186 /* If >0, no relocation whatsoever takes place. */
187 static int r_alloc_freeze_level;
190 /* Functions to get and return memory from the system. */
192 /* Find the heap that ADDRESS falls within. */
194 static heap_ptr
195 find_heap (POINTER address)
197 heap_ptr heap;
199 for (heap = last_heap; heap; heap = heap->prev)
201 if (heap->start <= address && address <= heap->end)
202 return heap;
205 return NIL_HEAP;
208 /* Find SIZE bytes of space in a heap.
209 Try to get them at ADDRESS (which must fall within some heap's range)
210 if we can get that many within one heap.
212 If enough space is not presently available in our reserve, this means
213 getting more page-aligned space from the system. If the returned space
214 is not contiguous to the last heap, allocate a new heap, and append it
215 to the heap list.
217 obtain does not try to keep track of whether space is in use or not
218 in use. It just returns the address of SIZE bytes that fall within a
219 single heap. If you call obtain twice in a row with the same arguments,
220 you typically get the same value. It's the caller's responsibility to
221 keep track of what space is in use.
223 Return the address of the space if all went well, or zero if we couldn't
224 allocate the memory. */
226 static POINTER
227 obtain (POINTER address, SIZE size)
229 heap_ptr heap;
230 SIZE already_available;
232 /* Find the heap that ADDRESS falls within. */
233 for (heap = last_heap; heap; heap = heap->prev)
235 if (heap->start <= address && address <= heap->end)
236 break;
239 if (! heap)
240 abort ();
242 /* If we can't fit SIZE bytes in that heap,
243 try successive later heaps. */
244 while (heap && (char *) address + size > (char *) heap->end)
246 heap = heap->next;
247 if (heap == NIL_HEAP)
248 break;
249 address = heap->bloc_start;
252 /* If we can't fit them within any existing heap,
253 get more space. */
254 if (heap == NIL_HEAP)
256 POINTER new = (*real_morecore)(0);
257 SIZE get;
259 already_available = (char *)last_heap->end - (char *)address;
261 if (new != last_heap->end)
263 /* Someone else called sbrk. Make a new heap. */
265 heap_ptr new_heap = (heap_ptr) MEM_ROUNDUP (new);
266 POINTER bloc_start = (POINTER) MEM_ROUNDUP ((POINTER)(new_heap + 1));
268 if ((*real_morecore) ((char *) bloc_start - (char *) new) != new)
269 return 0;
271 new_heap->start = new;
272 new_heap->end = bloc_start;
273 new_heap->bloc_start = bloc_start;
274 new_heap->free = bloc_start;
275 new_heap->next = NIL_HEAP;
276 new_heap->prev = last_heap;
277 new_heap->first_bloc = NIL_BLOC;
278 new_heap->last_bloc = NIL_BLOC;
279 last_heap->next = new_heap;
280 last_heap = new_heap;
282 address = bloc_start;
283 already_available = 0;
286 /* Add space to the last heap (which we may have just created).
287 Get some extra, so we can come here less often. */
289 get = size + extra_bytes - already_available;
290 get = (char *) ROUNDUP ((char *)last_heap->end + get)
291 - (char *) last_heap->end;
293 if ((*real_morecore) (get) != last_heap->end)
294 return 0;
296 last_heap->end = (char *) last_heap->end + get;
299 return address;
302 /* Return unused heap space to the system
303 if there is a lot of unused space now.
304 This can make the last heap smaller;
305 it can also eliminate the last heap entirely. */
307 static void
308 relinquish (void)
310 register heap_ptr h;
311 long excess = 0;
313 /* Add the amount of space beyond break_value
314 in all heaps which have extend beyond break_value at all. */
316 for (h = last_heap; h && break_value < h->end; h = h->prev)
318 excess += (char *) h->end - (char *) ((break_value < h->bloc_start)
319 ? h->bloc_start : break_value);
322 if (excess > extra_bytes * 2 && (*real_morecore) (0) == last_heap->end)
324 /* Keep extra_bytes worth of empty space.
325 And don't free anything unless we can free at least extra_bytes. */
326 excess -= extra_bytes;
328 if ((char *)last_heap->end - (char *)last_heap->bloc_start <= excess)
330 /* This heap should have no blocs in it. */
331 if (last_heap->first_bloc != NIL_BLOC
332 || last_heap->last_bloc != NIL_BLOC)
333 abort ();
335 /* Return the last heap, with its header, to the system. */
336 excess = (char *)last_heap->end - (char *)last_heap->start;
337 last_heap = last_heap->prev;
338 last_heap->next = NIL_HEAP;
340 else
342 excess = (char *) last_heap->end
343 - (char *) ROUNDUP ((char *)last_heap->end - excess);
344 last_heap->end = (char *) last_heap->end - excess;
347 if ((*real_morecore) (- excess) == 0)
349 /* If the system didn't want that much memory back, adjust
350 the end of the last heap to reflect that. This can occur
351 if break_value is still within the original data segment. */
352 last_heap->end = (char *) last_heap->end + excess;
353 /* Make sure that the result of the adjustment is accurate.
354 It should be, for the else clause above; the other case,
355 which returns the entire last heap to the system, seems
356 unlikely to trigger this mode of failure. */
357 if (last_heap->end != (*real_morecore) (0))
358 abort ();
363 /* The meat - allocating, freeing, and relocating blocs. */
365 /* Find the bloc referenced by the address in PTR. Returns a pointer
366 to that block. */
368 static bloc_ptr
369 find_bloc (POINTER *ptr)
371 register bloc_ptr p = first_bloc;
373 while (p != NIL_BLOC)
375 /* Consistency check. Don't return inconsistent blocs.
376 Don't abort here, as callers might be expecting this, but
377 callers that always expect a bloc to be returned should abort
378 if one isn't to avoid a memory corruption bug that is
379 difficult to track down. */
380 if (p->variable == ptr && p->data == *ptr)
381 return p;
383 p = p->next;
386 return p;
389 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
390 Returns a pointer to the new bloc, or zero if we couldn't allocate
391 memory for the new block. */
393 static bloc_ptr
394 get_bloc (SIZE size)
396 register bloc_ptr new_bloc;
397 register heap_ptr heap;
399 if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
400 || ! (new_bloc->data = obtain (break_value, size)))
402 free (new_bloc);
404 return 0;
407 break_value = (char *) new_bloc->data + size;
409 new_bloc->size = size;
410 new_bloc->next = NIL_BLOC;
411 new_bloc->variable = (POINTER *) NIL;
412 new_bloc->new_data = 0;
414 /* Record in the heap that this space is in use. */
415 heap = find_heap (new_bloc->data);
416 heap->free = break_value;
418 /* Maintain the correspondence between heaps and blocs. */
419 new_bloc->heap = heap;
420 heap->last_bloc = new_bloc;
421 if (heap->first_bloc == NIL_BLOC)
422 heap->first_bloc = new_bloc;
424 /* Put this bloc on the doubly-linked list of blocs. */
425 if (first_bloc)
427 new_bloc->prev = last_bloc;
428 last_bloc->next = new_bloc;
429 last_bloc = new_bloc;
431 else
433 first_bloc = last_bloc = new_bloc;
434 new_bloc->prev = NIL_BLOC;
437 return new_bloc;
440 /* Calculate new locations of blocs in the list beginning with BLOC,
441 relocating it to start at ADDRESS, in heap HEAP. If enough space is
442 not presently available in our reserve, call obtain for
443 more space.
445 Store the new location of each bloc in its new_data field.
446 Do not touch the contents of blocs or break_value. */
448 static int
449 relocate_blocs (bloc_ptr bloc, heap_ptr heap, POINTER address)
451 register bloc_ptr b = bloc;
453 /* No need to ever call this if arena is frozen, bug somewhere! */
454 if (r_alloc_freeze_level)
455 abort ();
457 while (b)
459 /* If bloc B won't fit within HEAP,
460 move to the next heap and try again. */
461 while (heap && (char *) address + b->size > (char *) heap->end)
463 heap = heap->next;
464 if (heap == NIL_HEAP)
465 break;
466 address = heap->bloc_start;
469 /* If BLOC won't fit in any heap,
470 get enough new space to hold BLOC and all following blocs. */
471 if (heap == NIL_HEAP)
473 register bloc_ptr tb = b;
474 register SIZE s = 0;
476 /* Add up the size of all the following blocs. */
477 while (tb != NIL_BLOC)
479 if (tb->variable)
480 s += tb->size;
482 tb = tb->next;
485 /* Get that space. */
486 address = obtain (address, s);
487 if (address == 0)
488 return 0;
490 heap = last_heap;
493 /* Record the new address of this bloc
494 and update where the next bloc can start. */
495 b->new_data = address;
496 if (b->variable)
497 address = (char *) address + b->size;
498 b = b->next;
501 return 1;
504 /* Update the records of which heaps contain which blocs, starting
505 with heap HEAP and bloc BLOC. */
507 static void
508 update_heap_bloc_correspondence (bloc_ptr bloc, heap_ptr heap)
510 register bloc_ptr b;
512 /* Initialize HEAP's status to reflect blocs before BLOC. */
513 if (bloc != NIL_BLOC && bloc->prev != NIL_BLOC && bloc->prev->heap == heap)
515 /* The previous bloc is in HEAP. */
516 heap->last_bloc = bloc->prev;
517 heap->free = (char *) bloc->prev->data + bloc->prev->size;
519 else
521 /* HEAP contains no blocs before BLOC. */
522 heap->first_bloc = NIL_BLOC;
523 heap->last_bloc = NIL_BLOC;
524 heap->free = heap->bloc_start;
527 /* Advance through blocs one by one. */
528 for (b = bloc; b != NIL_BLOC; b = b->next)
530 /* Advance through heaps, marking them empty,
531 till we get to the one that B is in. */
532 while (heap)
534 if (heap->bloc_start <= b->data && b->data <= heap->end)
535 break;
536 heap = heap->next;
537 /* We know HEAP is not null now,
538 because there has to be space for bloc B. */
539 heap->first_bloc = NIL_BLOC;
540 heap->last_bloc = NIL_BLOC;
541 heap->free = heap->bloc_start;
544 /* Update HEAP's status for bloc B. */
545 heap->free = (char *) b->data + b->size;
546 heap->last_bloc = b;
547 if (heap->first_bloc == NIL_BLOC)
548 heap->first_bloc = b;
550 /* Record that B is in HEAP. */
551 b->heap = heap;
554 /* If there are any remaining heaps and no blocs left,
555 mark those heaps as empty. */
556 heap = heap->next;
557 while (heap)
559 heap->first_bloc = NIL_BLOC;
560 heap->last_bloc = NIL_BLOC;
561 heap->free = heap->bloc_start;
562 heap = heap->next;
566 /* Resize BLOC to SIZE bytes. This relocates the blocs
567 that come after BLOC in memory. */
569 static int
570 resize_bloc (bloc_ptr bloc, SIZE size)
572 register bloc_ptr b;
573 heap_ptr heap;
574 POINTER address;
575 SIZE old_size;
577 /* No need to ever call this if arena is frozen, bug somewhere! */
578 if (r_alloc_freeze_level)
579 abort ();
581 if (bloc == NIL_BLOC || size == bloc->size)
582 return 1;
584 for (heap = first_heap; heap != NIL_HEAP; heap = heap->next)
586 if (heap->bloc_start <= bloc->data && bloc->data <= heap->end)
587 break;
590 if (heap == NIL_HEAP)
591 abort ();
593 old_size = bloc->size;
594 bloc->size = size;
596 /* Note that bloc could be moved into the previous heap. */
597 address = (bloc->prev ? (char *) bloc->prev->data + bloc->prev->size
598 : (char *) first_heap->bloc_start);
599 while (heap)
601 if (heap->bloc_start <= address && address <= heap->end)
602 break;
603 heap = heap->prev;
606 if (! relocate_blocs (bloc, heap, address))
608 bloc->size = old_size;
609 return 0;
612 if (size > old_size)
614 for (b = last_bloc; b != bloc; b = b->prev)
616 if (!b->variable)
618 b->size = 0;
619 b->data = b->new_data;
621 else
623 if (b->new_data != b->data)
624 memmove (b->new_data, b->data, b->size);
625 *b->variable = b->data = b->new_data;
628 if (!bloc->variable)
630 bloc->size = 0;
631 bloc->data = bloc->new_data;
633 else
635 if (bloc->new_data != bloc->data)
636 memmove (bloc->new_data, bloc->data, old_size);
637 memset ((char *) bloc->new_data + old_size, 0, size - old_size);
638 *bloc->variable = bloc->data = bloc->new_data;
641 else
643 for (b = bloc; b != NIL_BLOC; b = b->next)
645 if (!b->variable)
647 b->size = 0;
648 b->data = b->new_data;
650 else
652 if (b->new_data != b->data)
653 memmove (b->new_data, b->data, b->size);
654 *b->variable = b->data = b->new_data;
659 update_heap_bloc_correspondence (bloc, heap);
661 break_value = (last_bloc ? (char *) last_bloc->data + last_bloc->size
662 : (char *) first_heap->bloc_start);
663 return 1;
666 /* Free BLOC from the chain of blocs, relocating any blocs above it.
667 This may return space to the system. */
669 static void
670 free_bloc (bloc_ptr bloc)
672 heap_ptr heap = bloc->heap;
674 if (r_alloc_freeze_level)
676 bloc->variable = (POINTER *) NIL;
677 return;
680 resize_bloc (bloc, 0);
682 if (bloc == first_bloc && bloc == last_bloc)
684 first_bloc = last_bloc = NIL_BLOC;
686 else if (bloc == last_bloc)
688 last_bloc = bloc->prev;
689 last_bloc->next = NIL_BLOC;
691 else if (bloc == first_bloc)
693 first_bloc = bloc->next;
694 first_bloc->prev = NIL_BLOC;
696 else
698 bloc->next->prev = bloc->prev;
699 bloc->prev->next = bloc->next;
702 /* Update the records of which blocs are in HEAP. */
703 if (heap->first_bloc == bloc)
705 if (bloc->next != 0 && bloc->next->heap == heap)
706 heap->first_bloc = bloc->next;
707 else
708 heap->first_bloc = heap->last_bloc = NIL_BLOC;
710 if (heap->last_bloc == bloc)
712 if (bloc->prev != 0 && bloc->prev->heap == heap)
713 heap->last_bloc = bloc->prev;
714 else
715 heap->first_bloc = heap->last_bloc = NIL_BLOC;
718 relinquish ();
719 free (bloc);
722 /* Interface routines. */
724 /* Obtain SIZE bytes of storage from the free pool, or the system, as
725 necessary. If relocatable blocs are in use, this means relocating
726 them. This function gets plugged into the GNU malloc's __morecore
727 hook.
729 We provide hysteresis, never relocating by less than extra_bytes.
731 If we're out of memory, we should return zero, to imitate the other
732 __morecore hook values - in particular, __default_morecore in the
733 GNU malloc package. */
735 static POINTER
736 r_alloc_sbrk (long int size)
738 register bloc_ptr b;
739 POINTER address;
741 if (! r_alloc_initialized)
742 r_alloc_init ();
744 if (use_relocatable_buffers <= 0)
745 return (*real_morecore) (size);
747 if (size == 0)
748 return virtual_break_value;
750 if (size > 0)
752 /* Allocate a page-aligned space. GNU malloc would reclaim an
753 extra space if we passed an unaligned one. But we could
754 not always find a space which is contiguous to the previous. */
755 POINTER new_bloc_start;
756 heap_ptr h = first_heap;
757 SIZE get = ROUNDUP (size);
759 address = (POINTER) ROUNDUP (virtual_break_value);
761 /* Search the list upward for a heap which is large enough. */
762 while ((char *) h->end < (char *) MEM_ROUNDUP ((char *)address + get))
764 h = h->next;
765 if (h == NIL_HEAP)
766 break;
767 address = (POINTER) ROUNDUP (h->start);
770 /* If not found, obtain more space. */
771 if (h == NIL_HEAP)
773 get += extra_bytes + page_size;
775 if (! obtain (address, get))
776 return 0;
778 if (first_heap == last_heap)
779 address = (POINTER) ROUNDUP (virtual_break_value);
780 else
781 address = (POINTER) ROUNDUP (last_heap->start);
782 h = last_heap;
785 new_bloc_start = (POINTER) MEM_ROUNDUP ((char *)address + get);
787 if (first_heap->bloc_start < new_bloc_start)
789 /* This is no clean solution - no idea how to do it better. */
790 if (r_alloc_freeze_level)
791 return NIL;
793 /* There is a bug here: if the above obtain call succeeded, but the
794 relocate_blocs call below does not succeed, we need to free
795 the memory that we got with obtain. */
797 /* Move all blocs upward. */
798 if (! relocate_blocs (first_bloc, h, new_bloc_start))
799 return 0;
801 /* Note that (POINTER)(h+1) <= new_bloc_start since
802 get >= page_size, so the following does not destroy the heap
803 header. */
804 for (b = last_bloc; b != NIL_BLOC; b = b->prev)
806 if (b->new_data != b->data)
807 memmove (b->new_data, b->data, b->size);
808 *b->variable = b->data = b->new_data;
811 h->bloc_start = new_bloc_start;
813 update_heap_bloc_correspondence (first_bloc, h);
815 if (h != first_heap)
817 /* Give up managing heaps below the one the new
818 virtual_break_value points to. */
819 first_heap->prev = NIL_HEAP;
820 first_heap->next = h->next;
821 first_heap->start = h->start;
822 first_heap->end = h->end;
823 first_heap->free = h->free;
824 first_heap->first_bloc = h->first_bloc;
825 first_heap->last_bloc = h->last_bloc;
826 first_heap->bloc_start = h->bloc_start;
828 if (first_heap->next)
829 first_heap->next->prev = first_heap;
830 else
831 last_heap = first_heap;
834 memset (address, 0, size);
836 else /* size < 0 */
838 SIZE excess = (char *)first_heap->bloc_start
839 - ((char *)virtual_break_value + size);
841 address = virtual_break_value;
843 if (r_alloc_freeze_level == 0 && excess > 2 * extra_bytes)
845 excess -= extra_bytes;
846 first_heap->bloc_start
847 = (POINTER) MEM_ROUNDUP ((char *)first_heap->bloc_start - excess);
849 relocate_blocs (first_bloc, first_heap, first_heap->bloc_start);
851 for (b = first_bloc; b != NIL_BLOC; b = b->next)
853 if (b->new_data != b->data)
854 memmove (b->new_data, b->data, b->size);
855 *b->variable = b->data = b->new_data;
859 if ((char *)virtual_break_value + size < (char *)first_heap->start)
861 /* We found an additional space below the first heap */
862 first_heap->start = (POINTER) ((char *)virtual_break_value + size);
866 virtual_break_value = (POINTER) ((char *)address + size);
867 break_value = (last_bloc
868 ? (char *) last_bloc->data + last_bloc->size
869 : (char *) first_heap->bloc_start);
870 if (size < 0)
871 relinquish ();
873 return address;
877 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
878 the data is returned in *PTR. PTR is thus the address of some variable
879 which will use the data area.
881 The allocation of 0 bytes is valid.
882 In case r_alloc_freeze_level is set, a best fit of unused blocs could be
883 done before allocating a new area. Not yet done.
885 If we can't allocate the necessary memory, set *PTR to zero, and
886 return zero. */
888 POINTER
889 r_alloc (POINTER *ptr, SIZE size)
891 register bloc_ptr new_bloc;
893 if (! r_alloc_initialized)
894 r_alloc_init ();
896 new_bloc = get_bloc (MEM_ROUNDUP (size));
897 if (new_bloc)
899 new_bloc->variable = ptr;
900 *ptr = new_bloc->data;
902 else
903 *ptr = 0;
905 return *ptr;
908 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
909 Store 0 in *PTR to show there's no block allocated. */
911 void
912 r_alloc_free (register POINTER *ptr)
914 register bloc_ptr dead_bloc;
916 if (! r_alloc_initialized)
917 r_alloc_init ();
919 dead_bloc = find_bloc (ptr);
920 if (dead_bloc == NIL_BLOC)
921 abort (); /* Double free? PTR not originally used to allocate? */
923 free_bloc (dead_bloc);
924 *ptr = 0;
926 #ifdef emacs
927 refill_memory_reserve ();
928 #endif
931 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
932 Do this by shifting all blocks above this one up in memory, unless
933 SIZE is less than or equal to the current bloc size, in which case
934 do nothing.
936 In case r_alloc_freeze_level is set, a new bloc is allocated, and the
937 memory copied to it. Not very efficient. We could traverse the
938 bloc_list for a best fit of free blocs first.
940 Change *PTR to reflect the new bloc, and return this value.
942 If more memory cannot be allocated, then leave *PTR unchanged, and
943 return zero. */
945 POINTER
946 r_re_alloc (POINTER *ptr, SIZE size)
948 register bloc_ptr bloc;
950 if (! r_alloc_initialized)
951 r_alloc_init ();
953 if (!*ptr)
954 return r_alloc (ptr, size);
955 if (!size)
957 r_alloc_free (ptr);
958 return r_alloc (ptr, 0);
961 bloc = find_bloc (ptr);
962 if (bloc == NIL_BLOC)
963 abort (); /* Already freed? PTR not originally used to allocate? */
965 if (size < bloc->size)
967 /* Wouldn't it be useful to actually resize the bloc here? */
968 /* I think so too, but not if it's too expensive... */
969 if ((bloc->size - MEM_ROUNDUP (size) >= page_size)
970 && r_alloc_freeze_level == 0)
972 resize_bloc (bloc, MEM_ROUNDUP (size));
973 /* Never mind if this fails, just do nothing... */
974 /* It *should* be infallible! */
977 else if (size > bloc->size)
979 if (r_alloc_freeze_level)
981 bloc_ptr new_bloc;
982 new_bloc = get_bloc (MEM_ROUNDUP (size));
983 if (new_bloc)
985 new_bloc->variable = ptr;
986 *ptr = new_bloc->data;
987 bloc->variable = (POINTER *) NIL;
989 else
990 return NIL;
992 else
994 if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
995 return NIL;
998 return *ptr;
1002 #if defined (emacs) && defined (DOUG_LEA_MALLOC)
1004 /* Reinitialize the morecore hook variables after restarting a dumped
1005 Emacs. This is needed when using Doug Lea's malloc from GNU libc. */
1006 void
1007 r_alloc_reinit (void)
1009 /* Only do this if the hook has been reset, so that we don't get an
1010 infinite loop, in case Emacs was linked statically. */
1011 if (__morecore != r_alloc_sbrk)
1013 real_morecore = __morecore;
1014 __morecore = r_alloc_sbrk;
1018 #endif /* emacs && DOUG_LEA_MALLOC */
1020 #ifdef DEBUG
1022 #include <assert.h>
1024 void
1025 r_alloc_check (void)
1027 int found = 0;
1028 heap_ptr h, ph = 0;
1029 bloc_ptr b, pb = 0;
1031 if (!r_alloc_initialized)
1032 return;
1034 assert (first_heap);
1035 assert (last_heap->end <= (POINTER) sbrk (0));
1036 assert ((POINTER) first_heap < first_heap->start);
1037 assert (first_heap->start <= virtual_break_value);
1038 assert (virtual_break_value <= first_heap->end);
1040 for (h = first_heap; h; h = h->next)
1042 assert (h->prev == ph);
1043 assert ((POINTER) ROUNDUP (h->end) == h->end);
1044 #if 0 /* ??? The code in ralloc.c does not really try to ensure
1045 the heap start has any sort of alignment.
1046 Perhaps it should. */
1047 assert ((POINTER) MEM_ROUNDUP (h->start) == h->start);
1048 #endif
1049 assert ((POINTER) MEM_ROUNDUP (h->bloc_start) == h->bloc_start);
1050 assert (h->start <= h->bloc_start && h->bloc_start <= h->end);
1052 if (ph)
1054 assert (ph->end < h->start);
1055 assert (h->start <= (POINTER)h && (POINTER)(h+1) <= h->bloc_start);
1058 if (h->bloc_start <= break_value && break_value <= h->end)
1059 found = 1;
1061 ph = h;
1064 assert (found);
1065 assert (last_heap == ph);
1067 for (b = first_bloc; b; b = b->next)
1069 assert (b->prev == pb);
1070 assert ((POINTER) MEM_ROUNDUP (b->data) == b->data);
1071 assert ((SIZE) MEM_ROUNDUP (b->size) == b->size);
1073 ph = 0;
1074 for (h = first_heap; h; h = h->next)
1076 if (h->bloc_start <= b->data && b->data + b->size <= h->end)
1077 break;
1078 ph = h;
1081 assert (h);
1083 if (pb && pb->data + pb->size != b->data)
1085 assert (ph && b->data == h->bloc_start);
1086 while (ph)
1088 if (ph->bloc_start <= pb->data
1089 && pb->data + pb->size <= ph->end)
1091 assert (pb->data + pb->size + b->size > ph->end);
1092 break;
1094 else
1096 assert (ph->bloc_start + b->size > ph->end);
1098 ph = ph->prev;
1101 pb = b;
1104 assert (last_bloc == pb);
1106 if (last_bloc)
1107 assert (last_bloc->data + last_bloc->size == break_value);
1108 else
1109 assert (first_heap->bloc_start == break_value);
1112 #endif /* DEBUG */
1114 /* Update the internal record of which variable points to some data to NEW.
1115 Used by buffer-swap-text in Emacs to restore consistency after it
1116 swaps the buffer text between two buffer objects. The OLD pointer
1117 is checked to ensure that memory corruption does not occur due to
1118 misuse. */
1119 void
1120 r_alloc_reset_variable (POINTER *old, POINTER *new)
1122 bloc_ptr bloc = first_bloc;
1124 /* Find the bloc that corresponds to the data pointed to by pointer.
1125 find_bloc cannot be used, as it has internal consistency checks
1126 which fail when the variable needs resetting. */
1127 while (bloc != NIL_BLOC)
1129 if (bloc->data == *new)
1130 break;
1132 bloc = bloc->next;
1135 if (bloc == NIL_BLOC || bloc->variable != old)
1136 abort (); /* Already freed? OLD not originally used to allocate? */
1138 /* Update variable to point to the new location. */
1139 bloc->variable = new;
1142 void
1143 r_alloc_inhibit_buffer_relocation (int inhibit)
1145 if (use_relocatable_buffers > 1)
1146 use_relocatable_buffers = 1;
1147 if (inhibit)
1148 use_relocatable_buffers--;
1149 else if (use_relocatable_buffers < 1)
1150 use_relocatable_buffers++;
1154 /***********************************************************************
1155 Initialization
1156 ***********************************************************************/
1158 /* Initialize various things for memory allocation. */
1160 static void
1161 r_alloc_init (void)
1163 if (r_alloc_initialized)
1164 return;
1165 r_alloc_initialized = 1;
1167 page_size = PAGE;
1168 #ifndef SYSTEM_MALLOC
1169 real_morecore = __morecore;
1170 __morecore = r_alloc_sbrk;
1172 first_heap = last_heap = &heap_base;
1173 first_heap->next = first_heap->prev = NIL_HEAP;
1174 first_heap->start = first_heap->bloc_start
1175 = virtual_break_value = break_value = (*real_morecore) (0);
1176 if (break_value == NIL)
1177 abort ();
1179 extra_bytes = ROUNDUP (50000);
1180 #endif
1182 #ifdef DOUG_LEA_MALLOC
1183 BLOCK_INPUT;
1184 mallopt (M_TOP_PAD, 64 * 4096);
1185 UNBLOCK_INPUT;
1186 #else
1187 #ifndef SYSTEM_MALLOC
1188 /* Give GNU malloc's morecore some hysteresis
1189 so that we move all the relocatable blocks much less often. */
1190 __malloc_extra_blocks = 64;
1191 #endif
1192 #endif
1194 #ifndef SYSTEM_MALLOC
1195 first_heap->end = (POINTER) ROUNDUP (first_heap->start);
1197 /* The extra call to real_morecore guarantees that the end of the
1198 address space is a multiple of page_size, even if page_size is
1199 not really the page size of the system running the binary in
1200 which page_size is stored. This allows a binary to be built on a
1201 system with one page size and run on a system with a smaller page
1202 size. */
1203 (*real_morecore) ((char *) first_heap->end - (char *) first_heap->start);
1205 /* Clear the rest of the last page; this memory is in our address space
1206 even though it is after the sbrk value. */
1207 /* Doubly true, with the additional call that explicitly adds the
1208 rest of that page to the address space. */
1209 memset (first_heap->start, 0,
1210 (char *) first_heap->end - (char *) first_heap->start);
1211 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
1212 #endif
1214 use_relocatable_buffers = 1;