* fileio.c: Fix bugs with large file offsets.
[emacs.git] / src / ralloc.c
blob64a47416202357811ed3160c56abecd9df2f4fd6
1 /* Block-relocating memory allocator.
2 Copyright (C) 1993, 1995, 2000-2011 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 typedef POINTER_TYPE *POINTER;
35 typedef size_t SIZE;
37 #ifdef DOUG_LEA_MALLOC
38 #define M_TOP_PAD -2
39 extern int mallopt (int, int);
40 #else /* not DOUG_LEA_MALLOC */
41 #ifndef SYSTEM_MALLOC
42 extern size_t __malloc_extra_blocks;
43 #endif /* SYSTEM_MALLOC */
44 #endif /* not DOUG_LEA_MALLOC */
46 #else /* not emacs */
48 #include <stddef.h>
50 typedef size_t SIZE;
51 typedef void *POINTER;
53 #include <unistd.h>
54 #include <malloc.h>
56 #endif /* not emacs */
59 #include "getpagesize.h"
61 #define NIL ((POINTER) 0)
63 /* A flag to indicate whether we have initialized ralloc yet. For
64 Emacs's sake, please do not make this local to malloc_init; on some
65 machines, the dumping procedure makes all static variables
66 read-only. On these machines, the word static is #defined to be
67 the empty string, meaning that r_alloc_initialized becomes an
68 automatic variable, and loses its value each time Emacs is started
69 up. */
71 static int r_alloc_initialized = 0;
73 static void r_alloc_init (void);
76 /* Declarations for working with the malloc, ralloc, and system breaks. */
78 /* Function to set the real break value. */
79 POINTER (*real_morecore) (long int);
81 /* The break value, as seen by malloc. */
82 static POINTER virtual_break_value;
84 /* The address of the end of the last data in use by ralloc,
85 including relocatable blocs as well as malloc data. */
86 static POINTER break_value;
88 /* This is the size of a page. We round memory requests to this boundary. */
89 static int page_size;
91 /* Whenever we get memory from the system, get this many extra bytes. This
92 must be a multiple of page_size. */
93 static int extra_bytes;
95 /* Macros for rounding. Note that rounding to any value is possible
96 by changing the definition of PAGE. */
97 #define PAGE (getpagesize ())
98 #define ALIGNED(addr) (((unsigned long int) (addr) & (page_size - 1)) == 0)
99 #define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \
100 & ~(page_size - 1))
101 #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
103 #define MEM_ALIGN sizeof(double)
104 #define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \
105 & ~(MEM_ALIGN - 1))
107 /* The hook `malloc' uses for the function which gets more space
108 from the system. */
110 #ifndef SYSTEM_MALLOC
111 extern POINTER (*__morecore) (long int);
112 #endif
116 /***********************************************************************
117 Implementation using sbrk
118 ***********************************************************************/
120 /* Data structures of heaps and blocs. */
122 /* The relocatable objects, or blocs, and the malloc data
123 both reside within one or more heaps.
124 Each heap contains malloc data, running from `start' to `bloc_start',
125 and relocatable objects, running from `bloc_start' to `free'.
127 Relocatable objects may relocate within the same heap
128 or may move into another heap; the heaps themselves may grow
129 but they never move.
131 We try to make just one heap and make it larger as necessary.
132 But sometimes we can't do that, because we can't get contiguous
133 space to add onto the heap. When that happens, we start a new heap. */
135 typedef struct heap
137 struct heap *next;
138 struct heap *prev;
139 /* Start of memory range of this heap. */
140 POINTER start;
141 /* End of memory range of this heap. */
142 POINTER end;
143 /* Start of relocatable data in this heap. */
144 POINTER bloc_start;
145 /* Start of unused space in this heap. */
146 POINTER free;
147 /* First bloc in this heap. */
148 struct bp *first_bloc;
149 /* Last bloc in this heap. */
150 struct bp *last_bloc;
151 } *heap_ptr;
153 #define NIL_HEAP ((heap_ptr) 0)
154 #define HEAP_PTR_SIZE (sizeof (struct heap))
156 /* This is the first heap object.
157 If we need additional heap objects, each one resides at the beginning of
158 the space it covers. */
159 static struct heap heap_base;
161 /* Head and tail of the list of heaps. */
162 static heap_ptr first_heap, last_heap;
164 /* These structures are allocated in the malloc arena.
165 The linked list is kept in order of increasing '.data' members.
166 The data blocks abut each other; if b->next is non-nil, then
167 b->data + b->size == b->next->data.
169 An element with variable==NIL denotes a freed block, which has not yet
170 been collected. They may only appear while r_alloc_freeze_level > 0,
171 and will be freed when the arena is thawed. Currently, these blocs are
172 not reusable, while the arena is frozen. Very inefficient. */
174 typedef struct bp
176 struct bp *next;
177 struct bp *prev;
178 POINTER *variable;
179 POINTER data;
180 SIZE size;
181 POINTER new_data; /* temporarily used for relocation */
182 struct heap *heap; /* Heap this bloc is in. */
183 } *bloc_ptr;
185 #define NIL_BLOC ((bloc_ptr) 0)
186 #define BLOC_PTR_SIZE (sizeof (struct bp))
188 /* Head and tail of the list of relocatable blocs. */
189 static bloc_ptr first_bloc, last_bloc;
191 static int use_relocatable_buffers;
193 /* If >0, no relocation whatsoever takes place. */
194 static int r_alloc_freeze_level;
197 /* Functions to get and return memory from the system. */
199 /* Find the heap that ADDRESS falls within. */
201 static heap_ptr
202 find_heap (POINTER address)
204 heap_ptr heap;
206 for (heap = last_heap; heap; heap = heap->prev)
208 if (heap->start <= address && address <= heap->end)
209 return heap;
212 return NIL_HEAP;
215 /* Find SIZE bytes of space in a heap.
216 Try to get them at ADDRESS (which must fall within some heap's range)
217 if we can get that many within one heap.
219 If enough space is not presently available in our reserve, this means
220 getting more page-aligned space from the system. If the returned space
221 is not contiguous to the last heap, allocate a new heap, and append it
223 obtain does not try to keep track of whether space is in use
224 or not in use. It just returns the address of SIZE bytes that
225 fall within a single heap. If you call obtain twice in a row
226 with the same arguments, you typically get the same value.
227 to the heap list. It's the caller's responsibility to keep
228 track of what space is in use.
230 Return the address of the space if all went well, or zero if we couldn't
231 allocate the memory. */
233 static POINTER
234 obtain (POINTER address, SIZE size)
236 heap_ptr heap;
237 SIZE already_available;
239 /* Find the heap that ADDRESS falls within. */
240 for (heap = last_heap; heap; heap = heap->prev)
242 if (heap->start <= address && address <= heap->end)
243 break;
246 if (! heap)
247 abort ();
249 /* If we can't fit SIZE bytes in that heap,
250 try successive later heaps. */
251 while (heap && (char *) address + size > (char *) heap->end)
253 heap = heap->next;
254 if (heap == NIL_HEAP)
255 break;
256 address = heap->bloc_start;
259 /* If we can't fit them within any existing heap,
260 get more space. */
261 if (heap == NIL_HEAP)
263 POINTER new = (*real_morecore)(0);
264 SIZE get;
266 already_available = (char *)last_heap->end - (char *)address;
268 if (new != last_heap->end)
270 /* Someone else called sbrk. Make a new heap. */
272 heap_ptr new_heap = (heap_ptr) MEM_ROUNDUP (new);
273 POINTER bloc_start = (POINTER) MEM_ROUNDUP ((POINTER)(new_heap + 1));
275 if ((*real_morecore) ((char *) bloc_start - (char *) new) != new)
276 return 0;
278 new_heap->start = new;
279 new_heap->end = bloc_start;
280 new_heap->bloc_start = bloc_start;
281 new_heap->free = bloc_start;
282 new_heap->next = NIL_HEAP;
283 new_heap->prev = last_heap;
284 new_heap->first_bloc = NIL_BLOC;
285 new_heap->last_bloc = NIL_BLOC;
286 last_heap->next = new_heap;
287 last_heap = new_heap;
289 address = bloc_start;
290 already_available = 0;
293 /* Add space to the last heap (which we may have just created).
294 Get some extra, so we can come here less often. */
296 get = size + extra_bytes - already_available;
297 get = (char *) ROUNDUP ((char *)last_heap->end + get)
298 - (char *) last_heap->end;
300 if ((*real_morecore) (get) != last_heap->end)
301 return 0;
303 last_heap->end = (char *) last_heap->end + get;
306 return address;
309 /* Return unused heap space to the system
310 if there is a lot of unused space now.
311 This can make the last heap smaller;
312 it can also eliminate the last heap entirely. */
314 static void
315 relinquish (void)
317 register heap_ptr h;
318 long excess = 0;
320 /* Add the amount of space beyond break_value
321 in all heaps which have extend beyond break_value at all. */
323 for (h = last_heap; h && break_value < h->end; h = h->prev)
325 excess += (char *) h->end - (char *) ((break_value < h->bloc_start)
326 ? h->bloc_start : break_value);
329 if (excess > extra_bytes * 2 && (*real_morecore) (0) == last_heap->end)
331 /* Keep extra_bytes worth of empty space.
332 And don't free anything unless we can free at least extra_bytes. */
333 excess -= extra_bytes;
335 if ((char *)last_heap->end - (char *)last_heap->bloc_start <= excess)
337 /* This heap should have no blocs in it. */
338 if (last_heap->first_bloc != NIL_BLOC
339 || last_heap->last_bloc != NIL_BLOC)
340 abort ();
342 /* Return the last heap, with its header, to the system. */
343 excess = (char *)last_heap->end - (char *)last_heap->start;
344 last_heap = last_heap->prev;
345 last_heap->next = NIL_HEAP;
347 else
349 excess = (char *) last_heap->end
350 - (char *) ROUNDUP ((char *)last_heap->end - excess);
351 last_heap->end = (char *) last_heap->end - excess;
354 if ((*real_morecore) (- excess) == 0)
356 /* If the system didn't want that much memory back, adjust
357 the end of the last heap to reflect that. This can occur
358 if break_value is still within the original data segment. */
359 last_heap->end = (char *) last_heap->end + excess;
360 /* Make sure that the result of the adjustment is accurate.
361 It should be, for the else clause above; the other case,
362 which returns the entire last heap to the system, seems
363 unlikely to trigger this mode of failure. */
364 if (last_heap->end != (*real_morecore) (0))
365 abort ();
370 /* Return the total size in use by relocating allocator,
371 above where malloc gets space. */
373 long
374 r_alloc_size_in_use (void)
376 return (char *) break_value - (char *) virtual_break_value;
379 /* The meat - allocating, freeing, and relocating blocs. */
381 /* Find the bloc referenced by the address in PTR. Returns a pointer
382 to that block. */
384 static bloc_ptr
385 find_bloc (POINTER *ptr)
387 register bloc_ptr p = first_bloc;
389 while (p != NIL_BLOC)
391 /* Consistency check. Don't return inconsistent blocs.
392 Don't abort here, as callers might be expecting this, but
393 callers that always expect a bloc to be returned should abort
394 if one isn't to avoid a memory corruption bug that is
395 difficult to track down. */
396 if (p->variable == ptr && p->data == *ptr)
397 return p;
399 p = p->next;
402 return p;
405 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
406 Returns a pointer to the new bloc, or zero if we couldn't allocate
407 memory for the new block. */
409 static bloc_ptr
410 get_bloc (SIZE size)
412 register bloc_ptr new_bloc;
413 register heap_ptr heap;
415 if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
416 || ! (new_bloc->data = obtain (break_value, size)))
418 free (new_bloc);
420 return 0;
423 break_value = (char *) new_bloc->data + size;
425 new_bloc->size = size;
426 new_bloc->next = NIL_BLOC;
427 new_bloc->variable = (POINTER *) NIL;
428 new_bloc->new_data = 0;
430 /* Record in the heap that this space is in use. */
431 heap = find_heap (new_bloc->data);
432 heap->free = break_value;
434 /* Maintain the correspondence between heaps and blocs. */
435 new_bloc->heap = heap;
436 heap->last_bloc = new_bloc;
437 if (heap->first_bloc == NIL_BLOC)
438 heap->first_bloc = new_bloc;
440 /* Put this bloc on the doubly-linked list of blocs. */
441 if (first_bloc)
443 new_bloc->prev = last_bloc;
444 last_bloc->next = new_bloc;
445 last_bloc = new_bloc;
447 else
449 first_bloc = last_bloc = new_bloc;
450 new_bloc->prev = NIL_BLOC;
453 return new_bloc;
456 /* Calculate new locations of blocs in the list beginning with BLOC,
457 relocating it to start at ADDRESS, in heap HEAP. If enough space is
458 not presently available in our reserve, call obtain for
459 more space.
461 Store the new location of each bloc in its new_data field.
462 Do not touch the contents of blocs or break_value. */
464 static int
465 relocate_blocs (bloc_ptr bloc, heap_ptr heap, POINTER address)
467 register bloc_ptr b = bloc;
469 /* No need to ever call this if arena is frozen, bug somewhere! */
470 if (r_alloc_freeze_level)
471 abort();
473 while (b)
475 /* If bloc B won't fit within HEAP,
476 move to the next heap and try again. */
477 while (heap && (char *) address + b->size > (char *) heap->end)
479 heap = heap->next;
480 if (heap == NIL_HEAP)
481 break;
482 address = heap->bloc_start;
485 /* If BLOC won't fit in any heap,
486 get enough new space to hold BLOC and all following blocs. */
487 if (heap == NIL_HEAP)
489 register bloc_ptr tb = b;
490 register SIZE s = 0;
492 /* Add up the size of all the following blocs. */
493 while (tb != NIL_BLOC)
495 if (tb->variable)
496 s += tb->size;
498 tb = tb->next;
501 /* Get that space. */
502 address = obtain (address, s);
503 if (address == 0)
504 return 0;
506 heap = last_heap;
509 /* Record the new address of this bloc
510 and update where the next bloc can start. */
511 b->new_data = address;
512 if (b->variable)
513 address = (char *) address + b->size;
514 b = b->next;
517 return 1;
520 /* Update the records of which heaps contain which blocs, starting
521 with heap HEAP and bloc BLOC. */
523 static void
524 update_heap_bloc_correspondence (bloc_ptr bloc, heap_ptr heap)
526 register bloc_ptr b;
528 /* Initialize HEAP's status to reflect blocs before BLOC. */
529 if (bloc != NIL_BLOC && bloc->prev != NIL_BLOC && bloc->prev->heap == heap)
531 /* The previous bloc is in HEAP. */
532 heap->last_bloc = bloc->prev;
533 heap->free = (char *) bloc->prev->data + bloc->prev->size;
535 else
537 /* HEAP contains no blocs before BLOC. */
538 heap->first_bloc = NIL_BLOC;
539 heap->last_bloc = NIL_BLOC;
540 heap->free = heap->bloc_start;
543 /* Advance through blocs one by one. */
544 for (b = bloc; b != NIL_BLOC; b = b->next)
546 /* Advance through heaps, marking them empty,
547 till we get to the one that B is in. */
548 while (heap)
550 if (heap->bloc_start <= b->data && b->data <= heap->end)
551 break;
552 heap = heap->next;
553 /* We know HEAP is not null now,
554 because there has to be space for bloc B. */
555 heap->first_bloc = NIL_BLOC;
556 heap->last_bloc = NIL_BLOC;
557 heap->free = heap->bloc_start;
560 /* Update HEAP's status for bloc B. */
561 heap->free = (char *) b->data + b->size;
562 heap->last_bloc = b;
563 if (heap->first_bloc == NIL_BLOC)
564 heap->first_bloc = b;
566 /* Record that B is in HEAP. */
567 b->heap = heap;
570 /* If there are any remaining heaps and no blocs left,
571 mark those heaps as empty. */
572 heap = heap->next;
573 while (heap)
575 heap->first_bloc = NIL_BLOC;
576 heap->last_bloc = NIL_BLOC;
577 heap->free = heap->bloc_start;
578 heap = heap->next;
582 /* Resize BLOC to SIZE bytes. This relocates the blocs
583 that come after BLOC in memory. */
585 static int
586 resize_bloc (bloc_ptr bloc, SIZE size)
588 register bloc_ptr b;
589 heap_ptr heap;
590 POINTER address;
591 SIZE old_size;
593 /* No need to ever call this if arena is frozen, bug somewhere! */
594 if (r_alloc_freeze_level)
595 abort();
597 if (bloc == NIL_BLOC || size == bloc->size)
598 return 1;
600 for (heap = first_heap; heap != NIL_HEAP; heap = heap->next)
602 if (heap->bloc_start <= bloc->data && bloc->data <= heap->end)
603 break;
606 if (heap == NIL_HEAP)
607 abort ();
609 old_size = bloc->size;
610 bloc->size = size;
612 /* Note that bloc could be moved into the previous heap. */
613 address = (bloc->prev ? (char *) bloc->prev->data + bloc->prev->size
614 : (char *) first_heap->bloc_start);
615 while (heap)
617 if (heap->bloc_start <= address && address <= heap->end)
618 break;
619 heap = heap->prev;
622 if (! relocate_blocs (bloc, heap, address))
624 bloc->size = old_size;
625 return 0;
628 if (size > old_size)
630 for (b = last_bloc; b != bloc; b = b->prev)
632 if (!b->variable)
634 b->size = 0;
635 b->data = b->new_data;
637 else
639 memmove (b->new_data, b->data, b->size);
640 *b->variable = b->data = b->new_data;
643 if (!bloc->variable)
645 bloc->size = 0;
646 bloc->data = bloc->new_data;
648 else
650 memmove (bloc->new_data, bloc->data, old_size);
651 memset ((char *) bloc->new_data + old_size, 0, size - old_size);
652 *bloc->variable = bloc->data = bloc->new_data;
655 else
657 for (b = bloc; b != NIL_BLOC; b = b->next)
659 if (!b->variable)
661 b->size = 0;
662 b->data = b->new_data;
664 else
666 memmove (b->new_data, b->data, b->size);
667 *b->variable = b->data = b->new_data;
672 update_heap_bloc_correspondence (bloc, heap);
674 break_value = (last_bloc ? (char *) last_bloc->data + last_bloc->size
675 : (char *) first_heap->bloc_start);
676 return 1;
679 /* Free BLOC from the chain of blocs, relocating any blocs above it.
680 This may return space to the system. */
682 static void
683 free_bloc (bloc_ptr bloc)
685 heap_ptr heap = bloc->heap;
687 if (r_alloc_freeze_level)
689 bloc->variable = (POINTER *) NIL;
690 return;
693 resize_bloc (bloc, 0);
695 if (bloc == first_bloc && bloc == last_bloc)
697 first_bloc = last_bloc = NIL_BLOC;
699 else if (bloc == last_bloc)
701 last_bloc = bloc->prev;
702 last_bloc->next = NIL_BLOC;
704 else if (bloc == first_bloc)
706 first_bloc = bloc->next;
707 first_bloc->prev = NIL_BLOC;
709 else
711 bloc->next->prev = bloc->prev;
712 bloc->prev->next = bloc->next;
715 /* Update the records of which blocs are in HEAP. */
716 if (heap->first_bloc == bloc)
718 if (bloc->next != 0 && bloc->next->heap == heap)
719 heap->first_bloc = bloc->next;
720 else
721 heap->first_bloc = heap->last_bloc = NIL_BLOC;
723 if (heap->last_bloc == bloc)
725 if (bloc->prev != 0 && bloc->prev->heap == heap)
726 heap->last_bloc = bloc->prev;
727 else
728 heap->first_bloc = heap->last_bloc = NIL_BLOC;
731 relinquish ();
732 free (bloc);
735 /* Interface routines. */
737 /* Obtain SIZE bytes of storage from the free pool, or the system, as
738 necessary. If relocatable blocs are in use, this means relocating
739 them. This function gets plugged into the GNU malloc's __morecore
740 hook.
742 We provide hysteresis, never relocating by less than extra_bytes.
744 If we're out of memory, we should return zero, to imitate the other
745 __morecore hook values - in particular, __default_morecore in the
746 GNU malloc package. */
748 POINTER
749 r_alloc_sbrk (long int size)
751 register bloc_ptr b;
752 POINTER address;
754 if (! r_alloc_initialized)
755 r_alloc_init ();
757 if (! use_relocatable_buffers)
758 return (*real_morecore) (size);
760 if (size == 0)
761 return virtual_break_value;
763 if (size > 0)
765 /* Allocate a page-aligned space. GNU malloc would reclaim an
766 extra space if we passed an unaligned one. But we could
767 not always find a space which is contiguous to the previous. */
768 POINTER new_bloc_start;
769 heap_ptr h = first_heap;
770 SIZE get = ROUNDUP (size);
772 address = (POINTER) ROUNDUP (virtual_break_value);
774 /* Search the list upward for a heap which is large enough. */
775 while ((char *) h->end < (char *) MEM_ROUNDUP ((char *)address + get))
777 h = h->next;
778 if (h == NIL_HEAP)
779 break;
780 address = (POINTER) ROUNDUP (h->start);
783 /* If not found, obtain more space. */
784 if (h == NIL_HEAP)
786 get += extra_bytes + page_size;
788 if (! obtain (address, get))
789 return 0;
791 if (first_heap == last_heap)
792 address = (POINTER) ROUNDUP (virtual_break_value);
793 else
794 address = (POINTER) ROUNDUP (last_heap->start);
795 h = last_heap;
798 new_bloc_start = (POINTER) MEM_ROUNDUP ((char *)address + get);
800 if (first_heap->bloc_start < new_bloc_start)
802 /* This is no clean solution - no idea how to do it better. */
803 if (r_alloc_freeze_level)
804 return NIL;
806 /* There is a bug here: if the above obtain call succeeded, but the
807 relocate_blocs call below does not succeed, we need to free
808 the memory that we got with obtain. */
810 /* Move all blocs upward. */
811 if (! relocate_blocs (first_bloc, h, new_bloc_start))
812 return 0;
814 /* Note that (POINTER)(h+1) <= new_bloc_start since
815 get >= page_size, so the following does not destroy the heap
816 header. */
817 for (b = last_bloc; b != NIL_BLOC; b = b->prev)
819 memmove (b->new_data, b->data, b->size);
820 *b->variable = b->data = b->new_data;
823 h->bloc_start = new_bloc_start;
825 update_heap_bloc_correspondence (first_bloc, h);
827 if (h != first_heap)
829 /* Give up managing heaps below the one the new
830 virtual_break_value points to. */
831 first_heap->prev = NIL_HEAP;
832 first_heap->next = h->next;
833 first_heap->start = h->start;
834 first_heap->end = h->end;
835 first_heap->free = h->free;
836 first_heap->first_bloc = h->first_bloc;
837 first_heap->last_bloc = h->last_bloc;
838 first_heap->bloc_start = h->bloc_start;
840 if (first_heap->next)
841 first_heap->next->prev = first_heap;
842 else
843 last_heap = first_heap;
846 memset (address, 0, size);
848 else /* size < 0 */
850 SIZE excess = (char *)first_heap->bloc_start
851 - ((char *)virtual_break_value + size);
853 address = virtual_break_value;
855 if (r_alloc_freeze_level == 0 && excess > 2 * extra_bytes)
857 excess -= extra_bytes;
858 first_heap->bloc_start
859 = (POINTER) MEM_ROUNDUP ((char *)first_heap->bloc_start - excess);
861 relocate_blocs (first_bloc, first_heap, first_heap->bloc_start);
863 for (b = first_bloc; b != NIL_BLOC; b = b->next)
865 memmove (b->new_data, b->data, b->size);
866 *b->variable = b->data = b->new_data;
870 if ((char *)virtual_break_value + size < (char *)first_heap->start)
872 /* We found an additional space below the first heap */
873 first_heap->start = (POINTER) ((char *)virtual_break_value + size);
877 virtual_break_value = (POINTER) ((char *)address + size);
878 break_value = (last_bloc
879 ? (char *) last_bloc->data + last_bloc->size
880 : (char *) first_heap->bloc_start);
881 if (size < 0)
882 relinquish ();
884 return address;
888 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
889 the data is returned in *PTR. PTR is thus the address of some variable
890 which will use the data area.
892 The allocation of 0 bytes is valid.
893 In case r_alloc_freeze_level is set, a best fit of unused blocs could be
894 done before allocating a new area. Not yet done.
896 If we can't allocate the necessary memory, set *PTR to zero, and
897 return zero. */
899 POINTER
900 r_alloc (POINTER *ptr, SIZE size)
902 register bloc_ptr new_bloc;
904 if (! r_alloc_initialized)
905 r_alloc_init ();
907 new_bloc = get_bloc (MEM_ROUNDUP (size));
908 if (new_bloc)
910 new_bloc->variable = ptr;
911 *ptr = new_bloc->data;
913 else
914 *ptr = 0;
916 return *ptr;
919 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
920 Store 0 in *PTR to show there's no block allocated. */
922 void
923 r_alloc_free (register POINTER *ptr)
925 register bloc_ptr dead_bloc;
927 if (! r_alloc_initialized)
928 r_alloc_init ();
930 dead_bloc = find_bloc (ptr);
931 if (dead_bloc == NIL_BLOC)
932 abort (); /* Double free? PTR not originally used to allocate? */
934 free_bloc (dead_bloc);
935 *ptr = 0;
937 #ifdef emacs
938 refill_memory_reserve ();
939 #endif
942 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
943 Do this by shifting all blocks above this one up in memory, unless
944 SIZE is less than or equal to the current bloc size, in which case
945 do nothing.
947 In case r_alloc_freeze_level is set, a new bloc is allocated, and the
948 memory copied to it. Not very efficient. We could traverse the
949 bloc_list for a best fit of free blocs first.
951 Change *PTR to reflect the new bloc, and return this value.
953 If more memory cannot be allocated, then leave *PTR unchanged, and
954 return zero. */
956 POINTER
957 r_re_alloc (POINTER *ptr, SIZE size)
959 register bloc_ptr bloc;
961 if (! r_alloc_initialized)
962 r_alloc_init ();
964 if (!*ptr)
965 return r_alloc (ptr, size);
966 if (!size)
968 r_alloc_free (ptr);
969 return r_alloc (ptr, 0);
972 bloc = find_bloc (ptr);
973 if (bloc == NIL_BLOC)
974 abort (); /* Already freed? PTR not originally used to allocate? */
976 if (size < bloc->size)
978 /* Wouldn't it be useful to actually resize the bloc here? */
979 /* I think so too, but not if it's too expensive... */
980 if ((bloc->size - MEM_ROUNDUP (size) >= page_size)
981 && r_alloc_freeze_level == 0)
983 resize_bloc (bloc, MEM_ROUNDUP (size));
984 /* Never mind if this fails, just do nothing... */
985 /* It *should* be infallible! */
988 else if (size > bloc->size)
990 if (r_alloc_freeze_level)
992 bloc_ptr new_bloc;
993 new_bloc = get_bloc (MEM_ROUNDUP (size));
994 if (new_bloc)
996 new_bloc->variable = ptr;
997 *ptr = new_bloc->data;
998 bloc->variable = (POINTER *) NIL;
1000 else
1001 return NIL;
1003 else
1005 if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
1006 return NIL;
1009 return *ptr;
1012 /* Disable relocations, after making room for at least SIZE bytes
1013 of non-relocatable heap if possible. The relocatable blocs are
1014 guaranteed to hold still until thawed, even if this means that
1015 malloc must return a null pointer. */
1017 void
1018 r_alloc_freeze (long int size)
1020 if (! r_alloc_initialized)
1021 r_alloc_init ();
1023 /* If already frozen, we can't make any more room, so don't try. */
1024 if (r_alloc_freeze_level > 0)
1025 size = 0;
1026 /* If we can't get the amount requested, half is better than nothing. */
1027 while (size > 0 && r_alloc_sbrk (size) == 0)
1028 size /= 2;
1029 ++r_alloc_freeze_level;
1030 if (size > 0)
1031 r_alloc_sbrk (-size);
1034 void
1035 r_alloc_thaw (void)
1038 if (! r_alloc_initialized)
1039 r_alloc_init ();
1041 if (--r_alloc_freeze_level < 0)
1042 abort ();
1044 /* This frees all unused blocs. It is not too inefficient, as the resize
1045 and memcpy is done only once. Afterwards, all unreferenced blocs are
1046 already shrunk to zero size. */
1047 if (!r_alloc_freeze_level)
1049 bloc_ptr *b = &first_bloc;
1050 while (*b)
1051 if (!(*b)->variable)
1052 free_bloc (*b);
1053 else
1054 b = &(*b)->next;
1059 #if defined (emacs) && defined (DOUG_LEA_MALLOC)
1061 /* Reinitialize the morecore hook variables after restarting a dumped
1062 Emacs. This is needed when using Doug Lea's malloc from GNU libc. */
1063 void
1064 r_alloc_reinit (void)
1066 /* Only do this if the hook has been reset, so that we don't get an
1067 infinite loop, in case Emacs was linked statically. */
1068 if (__morecore != r_alloc_sbrk)
1070 real_morecore = __morecore;
1071 __morecore = r_alloc_sbrk;
1075 #endif /* emacs && DOUG_LEA_MALLOC */
1077 #ifdef DEBUG
1079 #include <assert.h>
1081 void
1082 r_alloc_check (void)
1084 int found = 0;
1085 heap_ptr h, ph = 0;
1086 bloc_ptr b, pb = 0;
1088 if (!r_alloc_initialized)
1089 return;
1091 assert (first_heap);
1092 assert (last_heap->end <= (POINTER) sbrk (0));
1093 assert ((POINTER) first_heap < first_heap->start);
1094 assert (first_heap->start <= virtual_break_value);
1095 assert (virtual_break_value <= first_heap->end);
1097 for (h = first_heap; h; h = h->next)
1099 assert (h->prev == ph);
1100 assert ((POINTER) ROUNDUP (h->end) == h->end);
1101 #if 0 /* ??? The code in ralloc.c does not really try to ensure
1102 the heap start has any sort of alignment.
1103 Perhaps it should. */
1104 assert ((POINTER) MEM_ROUNDUP (h->start) == h->start);
1105 #endif
1106 assert ((POINTER) MEM_ROUNDUP (h->bloc_start) == h->bloc_start);
1107 assert (h->start <= h->bloc_start && h->bloc_start <= h->end);
1109 if (ph)
1111 assert (ph->end < h->start);
1112 assert (h->start <= (POINTER)h && (POINTER)(h+1) <= h->bloc_start);
1115 if (h->bloc_start <= break_value && break_value <= h->end)
1116 found = 1;
1118 ph = h;
1121 assert (found);
1122 assert (last_heap == ph);
1124 for (b = first_bloc; b; b = b->next)
1126 assert (b->prev == pb);
1127 assert ((POINTER) MEM_ROUNDUP (b->data) == b->data);
1128 assert ((SIZE) MEM_ROUNDUP (b->size) == b->size);
1130 ph = 0;
1131 for (h = first_heap; h; h = h->next)
1133 if (h->bloc_start <= b->data && b->data + b->size <= h->end)
1134 break;
1135 ph = h;
1138 assert (h);
1140 if (pb && pb->data + pb->size != b->data)
1142 assert (ph && b->data == h->bloc_start);
1143 while (ph)
1145 if (ph->bloc_start <= pb->data
1146 && pb->data + pb->size <= ph->end)
1148 assert (pb->data + pb->size + b->size > ph->end);
1149 break;
1151 else
1153 assert (ph->bloc_start + b->size > ph->end);
1155 ph = ph->prev;
1158 pb = b;
1161 assert (last_bloc == pb);
1163 if (last_bloc)
1164 assert (last_bloc->data + last_bloc->size == break_value);
1165 else
1166 assert (first_heap->bloc_start == break_value);
1169 #endif /* DEBUG */
1171 /* Update the internal record of which variable points to some data to NEW.
1172 Used by buffer-swap-text in Emacs to restore consistency after it
1173 swaps the buffer text between two buffer objects. The OLD pointer
1174 is checked to ensure that memory corruption does not occur due to
1175 misuse. */
1176 void
1177 r_alloc_reset_variable (POINTER *old, POINTER *new)
1179 bloc_ptr bloc = first_bloc;
1181 /* Find the bloc that corresponds to the data pointed to by pointer.
1182 find_bloc cannot be used, as it has internal consistency checks
1183 which fail when the variable needs reseting. */
1184 while (bloc != NIL_BLOC)
1186 if (bloc->data == *new)
1187 break;
1189 bloc = bloc->next;
1192 if (bloc == NIL_BLOC || bloc->variable != old)
1193 abort (); /* Already freed? OLD not originally used to allocate? */
1195 /* Update variable to point to the new location. */
1196 bloc->variable = new;
1200 /***********************************************************************
1201 Initialization
1202 ***********************************************************************/
1204 /* Initialize various things for memory allocation. */
1206 static void
1207 r_alloc_init (void)
1209 if (r_alloc_initialized)
1210 return;
1211 r_alloc_initialized = 1;
1213 page_size = PAGE;
1214 #ifndef SYSTEM_MALLOC
1215 real_morecore = __morecore;
1216 __morecore = r_alloc_sbrk;
1218 first_heap = last_heap = &heap_base;
1219 first_heap->next = first_heap->prev = NIL_HEAP;
1220 first_heap->start = first_heap->bloc_start
1221 = virtual_break_value = break_value = (*real_morecore) (0);
1222 if (break_value == NIL)
1223 abort ();
1225 extra_bytes = ROUNDUP (50000);
1226 #endif
1228 #ifdef DOUG_LEA_MALLOC
1229 BLOCK_INPUT;
1230 mallopt (M_TOP_PAD, 64 * 4096);
1231 UNBLOCK_INPUT;
1232 #else
1233 #ifndef SYSTEM_MALLOC
1234 /* Give GNU malloc's morecore some hysteresis
1235 so that we move all the relocatable blocks much less often. */
1236 __malloc_extra_blocks = 64;
1237 #endif
1238 #endif
1240 #ifndef SYSTEM_MALLOC
1241 first_heap->end = (POINTER) ROUNDUP (first_heap->start);
1243 /* The extra call to real_morecore guarantees that the end of the
1244 address space is a multiple of page_size, even if page_size is
1245 not really the page size of the system running the binary in
1246 which page_size is stored. This allows a binary to be built on a
1247 system with one page size and run on a system with a smaller page
1248 size. */
1249 (*real_morecore) ((char *) first_heap->end - (char *) first_heap->start);
1251 /* Clear the rest of the last page; this memory is in our address space
1252 even though it is after the sbrk value. */
1253 /* Doubly true, with the additional call that explicitly adds the
1254 rest of that page to the address space. */
1255 memset (first_heap->start, 0,
1256 (char *) first_heap->end - (char *) first_heap->start);
1257 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
1258 #endif
1260 use_relocatable_buffers = 1;