]> code.delx.au - gnu-emacs/blob - src/ralloc.c
Ibuffer change marks
[gnu-emacs] / src / ralloc.c
1 /* Block-relocating memory allocator.
2 Copyright (C) 1993, 1995, 2000-2016 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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 (at
9 your option) any later version.
10
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.
15
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/>. */
18
19 /* NOTES:
20
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. */
24
25 #include <config.h>
26
27 #include <stddef.h>
28
29 #ifdef emacs
30 # include "lisp.h"
31 # include "blockinput.h"
32 # include <unistd.h>
33 #endif
34
35 #include "getpagesize.h"
36
37 /* A flag to indicate whether we have initialized ralloc yet. For
38 Emacs's sake, please do not make this local to malloc_init; on some
39 machines, the dumping procedure makes all static variables
40 read-only. On these machines, the word static is #defined to be
41 the empty string, meaning that r_alloc_initialized becomes an
42 automatic variable, and loses its value each time Emacs is started
43 up. */
44
45 static int r_alloc_initialized = 0;
46
47 static void r_alloc_init (void);
48
49 \f
50 /* Declarations for working with the malloc, ralloc, and system breaks. */
51
52 /* Function to set the real break value. */
53 void *(*real_morecore) (ptrdiff_t);
54
55 /* The break value, as seen by malloc. */
56 static void *virtual_break_value;
57
58 /* The address of the end of the last data in use by ralloc,
59 including relocatable blocs as well as malloc data. */
60 static void *break_value;
61
62 /* This is the size of a page. We round memory requests to this boundary. */
63 static int page_size;
64
65 /* Whenever we get memory from the system, get this many extra bytes. This
66 must be a multiple of page_size. */
67 static int extra_bytes;
68
69 /* Macros for rounding. Note that rounding to any value is possible
70 by changing the definition of PAGE. */
71 #define PAGE (getpagesize ())
72 #define PAGE_ROUNDUP(size) (((size_t) (size) + page_size - 1) \
73 & ~((size_t) (page_size - 1)))
74
75 #define MEM_ALIGN sizeof (double)
76 #define MEM_ROUNDUP(addr) (((size_t) (addr) + MEM_ALIGN - 1) \
77 & ~(MEM_ALIGN - 1))
78
79 /* The hook `malloc' uses for the function which gets more space
80 from the system. */
81
82 #ifdef HAVE_MALLOC_H
83 # include <malloc.h>
84 #else
85 extern void *(*__morecore) (ptrdiff_t);
86 #endif
87
88
89 \f
90 /***********************************************************************
91 Implementation using sbrk
92 ***********************************************************************/
93
94 /* Data structures of heaps and blocs. */
95
96 /* The relocatable objects, or blocs, and the malloc data
97 both reside within one or more heaps.
98 Each heap contains malloc data, running from `start' to `bloc_start',
99 and relocatable objects, running from `bloc_start' to `free'.
100
101 Relocatable objects may relocate within the same heap
102 or may move into another heap; the heaps themselves may grow
103 but they never move.
104
105 We try to make just one heap and make it larger as necessary.
106 But sometimes we can't do that, because we can't get contiguous
107 space to add onto the heap. When that happens, we start a new heap. */
108
109 typedef struct heap
110 {
111 struct heap *next;
112 struct heap *prev;
113 /* Start of memory range of this heap. */
114 void *start;
115 /* End of memory range of this heap. */
116 void *end;
117 /* Start of relocatable data in this heap. */
118 void *bloc_start;
119 /* Start of unused space in this heap. */
120 void *free;
121 /* First bloc in this heap. */
122 struct bp *first_bloc;
123 /* Last bloc in this heap. */
124 struct bp *last_bloc;
125 } *heap_ptr;
126
127 #define NIL_HEAP ((heap_ptr) 0)
128
129 /* This is the first heap object.
130 If we need additional heap objects, each one resides at the beginning of
131 the space it covers. */
132 static struct heap heap_base;
133
134 /* Head and tail of the list of heaps. */
135 static heap_ptr first_heap, last_heap;
136
137 /* These structures are allocated in the malloc arena.
138 The linked list is kept in order of increasing '.data' members.
139 The data blocks abut each other; if b->next is non-nil, then
140 b->data + b->size == b->next->data.
141
142 An element with variable==NULL denotes a freed block, which has not yet
143 been collected. They may only appear while r_alloc_freeze_level > 0,
144 and will be freed when the arena is thawed. Currently, these blocs are
145 not reusable, while the arena is frozen. Very inefficient. */
146
147 typedef struct bp
148 {
149 struct bp *next;
150 struct bp *prev;
151 void **variable;
152 void *data;
153 size_t size;
154 void *new_data; /* temporarily used for relocation */
155 struct heap *heap; /* Heap this bloc is in. */
156 } *bloc_ptr;
157
158 #define NIL_BLOC ((bloc_ptr) 0)
159 #define BLOC_PTR_SIZE (sizeof (struct bp))
160
161 /* Head and tail of the list of relocatable blocs. */
162 static bloc_ptr first_bloc, last_bloc;
163
164 static int use_relocatable_buffers;
165
166 /* If >0, no relocation whatsoever takes place. */
167 static int r_alloc_freeze_level;
168
169 \f
170 /* Functions to get and return memory from the system. */
171
172 /* Find the heap that ADDRESS falls within. */
173
174 static heap_ptr
175 find_heap (void *address)
176 {
177 heap_ptr heap;
178
179 for (heap = last_heap; heap; heap = heap->prev)
180 {
181 if (heap->start <= address && address <= heap->end)
182 return heap;
183 }
184
185 return NIL_HEAP;
186 }
187
188 /* Find SIZE bytes of space in a heap.
189 Try to get them at ADDRESS (which must fall within some heap's range)
190 if we can get that many within one heap.
191
192 If enough space is not presently available in our reserve, this means
193 getting more page-aligned space from the system. If the returned space
194 is not contiguous to the last heap, allocate a new heap, and append it
195 to the heap list.
196
197 obtain does not try to keep track of whether space is in use or not
198 in use. It just returns the address of SIZE bytes that fall within a
199 single heap. If you call obtain twice in a row with the same arguments,
200 you typically get the same value. It's the caller's responsibility to
201 keep track of what space is in use.
202
203 Return the address of the space if all went well, or zero if we couldn't
204 allocate the memory. */
205
206 static void *
207 obtain (void *address, size_t size)
208 {
209 heap_ptr heap;
210 size_t already_available;
211
212 /* Find the heap that ADDRESS falls within. */
213 for (heap = last_heap; heap; heap = heap->prev)
214 {
215 if (heap->start <= address && address <= heap->end)
216 break;
217 }
218
219 if (! heap)
220 emacs_abort ();
221
222 /* If we can't fit SIZE bytes in that heap,
223 try successive later heaps. */
224 while (heap && (char *) address + size > (char *) heap->end)
225 {
226 heap = heap->next;
227 if (heap == NIL_HEAP)
228 break;
229 address = heap->bloc_start;
230 }
231
232 /* If we can't fit them within any existing heap,
233 get more space. */
234 if (heap == NIL_HEAP)
235 {
236 void *new = real_morecore (0);
237 size_t get;
238
239 already_available = (char *) last_heap->end - (char *) address;
240
241 if (new != last_heap->end)
242 {
243 /* Someone else called sbrk. Make a new heap. */
244
245 heap_ptr new_heap = (heap_ptr) MEM_ROUNDUP (new);
246 void *bloc_start = (void *) MEM_ROUNDUP ((void *) (new_heap + 1));
247
248 if (real_morecore ((char *) bloc_start - (char *) new) != new)
249 return 0;
250
251 new_heap->start = new;
252 new_heap->end = bloc_start;
253 new_heap->bloc_start = bloc_start;
254 new_heap->free = bloc_start;
255 new_heap->next = NIL_HEAP;
256 new_heap->prev = last_heap;
257 new_heap->first_bloc = NIL_BLOC;
258 new_heap->last_bloc = NIL_BLOC;
259 last_heap->next = new_heap;
260 last_heap = new_heap;
261
262 address = bloc_start;
263 already_available = 0;
264 }
265
266 /* Add space to the last heap (which we may have just created).
267 Get some extra, so we can come here less often. */
268
269 get = size + extra_bytes - already_available;
270 get = (char *) PAGE_ROUNDUP ((char *) last_heap->end + get)
271 - (char *) last_heap->end;
272
273 if (real_morecore (get) != last_heap->end)
274 return 0;
275
276 last_heap->end = (char *) last_heap->end + get;
277 }
278
279 return address;
280 }
281
282 /* Return unused heap space to the system
283 if there is a lot of unused space now.
284 This can make the last heap smaller;
285 it can also eliminate the last heap entirely. */
286
287 static void
288 relinquish (void)
289 {
290 register heap_ptr h;
291 ptrdiff_t excess = 0;
292
293 /* Add the amount of space beyond break_value
294 in all heaps which have extend beyond break_value at all. */
295
296 for (h = last_heap; h && break_value < h->end; h = h->prev)
297 {
298 excess += (char *) h->end - (char *) ((break_value < h->bloc_start)
299 ? h->bloc_start : break_value);
300 }
301
302 if (excess > extra_bytes * 2 && real_morecore (0) == last_heap->end)
303 {
304 /* Keep extra_bytes worth of empty space.
305 And don't free anything unless we can free at least extra_bytes. */
306 excess -= extra_bytes;
307
308 if ((char *) last_heap->end - (char *) last_heap->bloc_start <= excess)
309 {
310 heap_ptr lh_prev;
311
312 /* This heap should have no blocs in it. If it does, we
313 cannot return it to the system. */
314 if (last_heap->first_bloc != NIL_BLOC
315 || last_heap->last_bloc != NIL_BLOC)
316 return;
317
318 /* Return the last heap, with its header, to the system. */
319 excess = (char *) last_heap->end - (char *) last_heap->start;
320 lh_prev = last_heap->prev;
321 /* If the system doesn't want that much memory back, leave
322 last_heap unaltered to reflect that. This can occur if
323 break_value is still within the original data segment. */
324 if (real_morecore (- excess) != 0)
325 {
326 last_heap = lh_prev;
327 last_heap->next = NIL_HEAP;
328 }
329 }
330 else
331 {
332 excess = ((char *) last_heap->end
333 - (char *) PAGE_ROUNDUP ((char *) last_heap->end - excess));
334 /* If the system doesn't want that much memory back, leave
335 the end of the last heap unchanged to reflect that. This
336 can occur if break_value is still within the original
337 data segment. */
338 if (real_morecore (- excess) != 0)
339 last_heap->end = (char *) last_heap->end - excess;
340 }
341 }
342 }
343 \f
344 /* The meat - allocating, freeing, and relocating blocs. */
345
346 /* Find the bloc referenced by the address in PTR. Returns a pointer
347 to that block. */
348
349 static bloc_ptr
350 find_bloc (void **ptr)
351 {
352 bloc_ptr p = first_bloc;
353
354 while (p != NIL_BLOC)
355 {
356 /* Consistency check. Don't return inconsistent blocs.
357 Don't abort here, as callers might be expecting this, but
358 callers that always expect a bloc to be returned should abort
359 if one isn't to avoid a memory corruption bug that is
360 difficult to track down. */
361 if (p->variable == ptr && p->data == *ptr)
362 return p;
363
364 p = p->next;
365 }
366
367 return p;
368 }
369
370 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
371 Returns a pointer to the new bloc, or zero if we couldn't allocate
372 memory for the new block. */
373
374 static bloc_ptr
375 get_bloc (size_t size)
376 {
377 bloc_ptr new_bloc;
378 heap_ptr heap;
379
380 if (! (new_bloc = malloc (BLOC_PTR_SIZE))
381 || ! (new_bloc->data = obtain (break_value, size)))
382 {
383 free (new_bloc);
384
385 return 0;
386 }
387
388 break_value = (char *) new_bloc->data + size;
389
390 new_bloc->size = size;
391 new_bloc->next = NIL_BLOC;
392 new_bloc->variable = NULL;
393 new_bloc->new_data = 0;
394
395 /* Record in the heap that this space is in use. */
396 heap = find_heap (new_bloc->data);
397 heap->free = break_value;
398
399 /* Maintain the correspondence between heaps and blocs. */
400 new_bloc->heap = heap;
401 heap->last_bloc = new_bloc;
402 if (heap->first_bloc == NIL_BLOC)
403 heap->first_bloc = new_bloc;
404
405 /* Put this bloc on the doubly-linked list of blocs. */
406 if (first_bloc)
407 {
408 new_bloc->prev = last_bloc;
409 last_bloc->next = new_bloc;
410 last_bloc = new_bloc;
411 }
412 else
413 {
414 first_bloc = last_bloc = new_bloc;
415 new_bloc->prev = NIL_BLOC;
416 }
417
418 return new_bloc;
419 }
420 \f
421 /* Calculate new locations of blocs in the list beginning with BLOC,
422 relocating it to start at ADDRESS, in heap HEAP. If enough space is
423 not presently available in our reserve, call obtain for
424 more space.
425
426 Store the new location of each bloc in its new_data field.
427 Do not touch the contents of blocs or break_value. */
428
429 static int
430 relocate_blocs (bloc_ptr bloc, heap_ptr heap, void *address)
431 {
432 bloc_ptr b = bloc;
433
434 /* No need to ever call this if arena is frozen, bug somewhere! */
435 if (r_alloc_freeze_level)
436 emacs_abort ();
437
438 while (b)
439 {
440 /* If bloc B won't fit within HEAP,
441 move to the next heap and try again. */
442 while (heap && (char *) address + b->size > (char *) heap->end)
443 {
444 heap = heap->next;
445 if (heap == NIL_HEAP)
446 break;
447 address = heap->bloc_start;
448 }
449
450 /* If BLOC won't fit in any heap,
451 get enough new space to hold BLOC and all following blocs. */
452 if (heap == NIL_HEAP)
453 {
454 bloc_ptr tb = b;
455 size_t s = 0;
456
457 /* Add up the size of all the following blocs. */
458 while (tb != NIL_BLOC)
459 {
460 if (tb->variable)
461 s += tb->size;
462
463 tb = tb->next;
464 }
465
466 /* Get that space. */
467 address = obtain (address, s);
468 if (address == 0)
469 return 0;
470
471 heap = last_heap;
472 }
473
474 /* Record the new address of this bloc
475 and update where the next bloc can start. */
476 b->new_data = address;
477 if (b->variable)
478 address = (char *) address + b->size;
479 b = b->next;
480 }
481
482 return 1;
483 }
484 \f
485 /* Update the records of which heaps contain which blocs, starting
486 with heap HEAP and bloc BLOC. */
487
488 static void
489 update_heap_bloc_correspondence (bloc_ptr bloc, heap_ptr heap)
490 {
491 register bloc_ptr b;
492
493 /* Initialize HEAP's status to reflect blocs before BLOC. */
494 if (bloc != NIL_BLOC && bloc->prev != NIL_BLOC && bloc->prev->heap == heap)
495 {
496 /* The previous bloc is in HEAP. */
497 heap->last_bloc = bloc->prev;
498 heap->free = (char *) bloc->prev->data + bloc->prev->size;
499 }
500 else
501 {
502 /* HEAP contains no blocs before BLOC. */
503 heap->first_bloc = NIL_BLOC;
504 heap->last_bloc = NIL_BLOC;
505 heap->free = heap->bloc_start;
506 }
507
508 /* Advance through blocs one by one. */
509 for (b = bloc; b != NIL_BLOC; b = b->next)
510 {
511 /* Advance through heaps, marking them empty,
512 till we get to the one that B is in. */
513 while (heap)
514 {
515 if (heap->bloc_start <= b->data && b->data <= heap->end)
516 break;
517 heap = heap->next;
518 /* We know HEAP is not null now,
519 because there has to be space for bloc B. */
520 heap->first_bloc = NIL_BLOC;
521 heap->last_bloc = NIL_BLOC;
522 heap->free = heap->bloc_start;
523 }
524
525 /* Update HEAP's status for bloc B. */
526 heap->free = (char *) b->data + b->size;
527 heap->last_bloc = b;
528 if (heap->first_bloc == NIL_BLOC)
529 heap->first_bloc = b;
530
531 /* Record that B is in HEAP. */
532 b->heap = heap;
533 }
534
535 /* If there are any remaining heaps and no blocs left,
536 mark those heaps as empty. */
537 heap = heap->next;
538 while (heap)
539 {
540 heap->first_bloc = NIL_BLOC;
541 heap->last_bloc = NIL_BLOC;
542 heap->free = heap->bloc_start;
543 heap = heap->next;
544 }
545 }
546 \f
547 /* Resize BLOC to SIZE bytes. This relocates the blocs
548 that come after BLOC in memory. */
549
550 static int
551 resize_bloc (bloc_ptr bloc, size_t size)
552 {
553 bloc_ptr b;
554 heap_ptr heap;
555 void *address;
556 size_t old_size;
557
558 /* No need to ever call this if arena is frozen, bug somewhere! */
559 if (r_alloc_freeze_level)
560 emacs_abort ();
561
562 if (bloc == NIL_BLOC || size == bloc->size)
563 return 1;
564
565 for (heap = first_heap; heap != NIL_HEAP; heap = heap->next)
566 {
567 if (heap->bloc_start <= bloc->data && bloc->data <= heap->end)
568 break;
569 }
570
571 if (heap == NIL_HEAP)
572 emacs_abort ();
573
574 old_size = bloc->size;
575 bloc->size = size;
576
577 /* Note that bloc could be moved into the previous heap. */
578 address = (bloc->prev ? (char *) bloc->prev->data + bloc->prev->size
579 : (char *) first_heap->bloc_start);
580 while (heap)
581 {
582 if (heap->bloc_start <= address && address <= heap->end)
583 break;
584 heap = heap->prev;
585 }
586
587 if (! relocate_blocs (bloc, heap, address))
588 {
589 bloc->size = old_size;
590 return 0;
591 }
592
593 if (size > old_size)
594 {
595 for (b = last_bloc; b != bloc; b = b->prev)
596 {
597 if (!b->variable)
598 {
599 b->size = 0;
600 b->data = b->new_data;
601 }
602 else
603 {
604 if (b->new_data != b->data)
605 memmove (b->new_data, b->data, b->size);
606 *b->variable = b->data = b->new_data;
607 }
608 }
609 if (!bloc->variable)
610 {
611 bloc->size = 0;
612 bloc->data = bloc->new_data;
613 }
614 else
615 {
616 if (bloc->new_data != bloc->data)
617 memmove (bloc->new_data, bloc->data, old_size);
618 memset ((char *) bloc->new_data + old_size, 0, size - old_size);
619 *bloc->variable = bloc->data = bloc->new_data;
620 }
621 }
622 else
623 {
624 for (b = bloc; b != NIL_BLOC; b = b->next)
625 {
626 if (!b->variable)
627 {
628 b->size = 0;
629 b->data = b->new_data;
630 }
631 else
632 {
633 if (b->new_data != b->data)
634 memmove (b->new_data, b->data, b->size);
635 *b->variable = b->data = b->new_data;
636 }
637 }
638 }
639
640 update_heap_bloc_correspondence (bloc, heap);
641
642 break_value = (last_bloc ? (char *) last_bloc->data + last_bloc->size
643 : (char *) first_heap->bloc_start);
644 return 1;
645 }
646 \f
647 /* Free BLOC from the chain of blocs, relocating any blocs above it.
648 This may return space to the system. */
649
650 static void
651 free_bloc (bloc_ptr bloc)
652 {
653 heap_ptr heap = bloc->heap;
654 heap_ptr h;
655
656 if (r_alloc_freeze_level)
657 {
658 bloc->variable = NULL;
659 return;
660 }
661
662 resize_bloc (bloc, 0);
663
664 if (bloc == first_bloc && bloc == last_bloc)
665 {
666 first_bloc = last_bloc = NIL_BLOC;
667 }
668 else if (bloc == last_bloc)
669 {
670 last_bloc = bloc->prev;
671 last_bloc->next = NIL_BLOC;
672 }
673 else if (bloc == first_bloc)
674 {
675 first_bloc = bloc->next;
676 first_bloc->prev = NIL_BLOC;
677 }
678 else
679 {
680 bloc->next->prev = bloc->prev;
681 bloc->prev->next = bloc->next;
682 }
683
684 /* Sometimes, 'heap' obtained from bloc->heap above is not really a
685 'heap' structure. It can even be beyond the current break point,
686 which will cause crashes when we dereference it below (see
687 bug#12242). Evidently, the reason is bloc allocations done while
688 use_relocatable_buffers was non-positive, because additional
689 memory we get then is not recorded in the heaps we manage. If
690 bloc->heap records such a "heap", we cannot (and don't need to)
691 update its records. So we validate the 'heap' value by making
692 sure it is one of the heaps we manage via the heaps linked list,
693 and don't touch a 'heap' that isn't found there. This avoids
694 accessing memory we know nothing about. */
695 for (h = first_heap; h != NIL_HEAP; h = h->next)
696 if (heap == h)
697 break;
698
699 if (h)
700 {
701 /* Update the records of which blocs are in HEAP. */
702 if (heap->first_bloc == bloc)
703 {
704 if (bloc->next != 0 && bloc->next->heap == heap)
705 heap->first_bloc = bloc->next;
706 else
707 heap->first_bloc = heap->last_bloc = NIL_BLOC;
708 }
709 if (heap->last_bloc == bloc)
710 {
711 if (bloc->prev != 0 && bloc->prev->heap == heap)
712 heap->last_bloc = bloc->prev;
713 else
714 heap->first_bloc = heap->last_bloc = NIL_BLOC;
715 }
716 }
717
718 relinquish ();
719 free (bloc);
720 }
721 \f
722 /* Interface routines. */
723
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.
728
729 We provide hysteresis, never relocating by less than extra_bytes.
730
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. */
734
735 static void *
736 r_alloc_sbrk (ptrdiff_t size)
737 {
738 bloc_ptr b;
739 void *address;
740
741 if (! r_alloc_initialized)
742 r_alloc_init ();
743
744 if (use_relocatable_buffers <= 0)
745 return real_morecore (size);
746
747 if (size == 0)
748 return virtual_break_value;
749
750 if (size > 0)
751 {
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 void *new_bloc_start;
756 heap_ptr h = first_heap;
757 size_t get = PAGE_ROUNDUP (size);
758
759 address = (void *) PAGE_ROUNDUP (virtual_break_value);
760
761 /* Search the list upward for a heap which is large enough. */
762 while ((char *) h->end < (char *) MEM_ROUNDUP ((char *) address + get))
763 {
764 h = h->next;
765 if (h == NIL_HEAP)
766 break;
767 address = (void *) PAGE_ROUNDUP (h->start);
768 }
769
770 /* If not found, obtain more space. */
771 if (h == NIL_HEAP)
772 {
773 get += extra_bytes + page_size;
774
775 if (! obtain (address, get))
776 return 0;
777
778 if (first_heap == last_heap)
779 address = (void *) PAGE_ROUNDUP (virtual_break_value);
780 else
781 address = (void *) PAGE_ROUNDUP (last_heap->start);
782 h = last_heap;
783 }
784
785 new_bloc_start = (void *) MEM_ROUNDUP ((char *) address + get);
786
787 if (first_heap->bloc_start < new_bloc_start)
788 {
789 /* This is no clean solution - no idea how to do it better. */
790 if (r_alloc_freeze_level)
791 return NULL;
792
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. */
796
797 /* Move all blocs upward. */
798 if (! relocate_blocs (first_bloc, h, new_bloc_start))
799 return 0;
800
801 /* Note that (char *) (h + 1) <= (char *) 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)
805 {
806 if (b->new_data != b->data)
807 memmove (b->new_data, b->data, b->size);
808 *b->variable = b->data = b->new_data;
809 }
810
811 h->bloc_start = new_bloc_start;
812
813 update_heap_bloc_correspondence (first_bloc, h);
814 }
815 if (h != first_heap)
816 {
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;
827
828 if (first_heap->next)
829 first_heap->next->prev = first_heap;
830 else
831 last_heap = first_heap;
832 }
833
834 memset (address, 0, size);
835 }
836 else /* size < 0 */
837 {
838 size_t excess = ((char *) first_heap->bloc_start
839 - ((char *) virtual_break_value + size));
840
841 address = virtual_break_value;
842
843 if (r_alloc_freeze_level == 0 && excess > 2 * extra_bytes)
844 {
845 excess -= extra_bytes;
846 first_heap->bloc_start
847 = (void *) MEM_ROUNDUP ((char *) first_heap->bloc_start - excess);
848
849 relocate_blocs (first_bloc, first_heap, first_heap->bloc_start);
850
851 for (b = first_bloc; b != NIL_BLOC; b = b->next)
852 {
853 if (b->new_data != b->data)
854 memmove (b->new_data, b->data, b->size);
855 *b->variable = b->data = b->new_data;
856 }
857 }
858
859 if ((char *) virtual_break_value + size < (char *) first_heap->start)
860 {
861 /* We found an additional space below the first heap */
862 first_heap->start = (void *) ((char *) virtual_break_value + size);
863 }
864 }
865
866 virtual_break_value = (void *) ((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 ();
872
873 return address;
874 }
875
876
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.
880
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.
884
885 If we can't allocate the necessary memory, set *PTR to zero, and
886 return zero. */
887
888 void *
889 r_alloc (void **ptr, size_t size)
890 {
891 bloc_ptr new_bloc;
892
893 if (! r_alloc_initialized)
894 r_alloc_init ();
895
896 new_bloc = get_bloc (MEM_ROUNDUP (size));
897 if (new_bloc)
898 {
899 new_bloc->variable = ptr;
900 *ptr = new_bloc->data;
901 }
902 else
903 *ptr = 0;
904
905 return *ptr;
906 }
907
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. */
910
911 void
912 r_alloc_free (void **ptr)
913 {
914 bloc_ptr dead_bloc;
915
916 if (! r_alloc_initialized)
917 r_alloc_init ();
918
919 dead_bloc = find_bloc (ptr);
920 if (dead_bloc == NIL_BLOC)
921 emacs_abort (); /* Double free? PTR not originally used to allocate? */
922
923 free_bloc (dead_bloc);
924 *ptr = 0;
925
926 #ifdef emacs
927 refill_memory_reserve ();
928 #endif
929 }
930
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.
935
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.
939
940 Change *PTR to reflect the new bloc, and return this value.
941
942 If more memory cannot be allocated, then leave *PTR unchanged, and
943 return zero. */
944
945 void *
946 r_re_alloc (void **ptr, size_t size)
947 {
948 bloc_ptr bloc;
949
950 if (! r_alloc_initialized)
951 r_alloc_init ();
952
953 if (!*ptr)
954 return r_alloc (ptr, size);
955 if (!size)
956 {
957 r_alloc_free (ptr);
958 return r_alloc (ptr, 0);
959 }
960
961 bloc = find_bloc (ptr);
962 if (bloc == NIL_BLOC)
963 emacs_abort (); /* Already freed? PTR not originally used to allocate? */
964
965 if (size < bloc->size)
966 {
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)
971 {
972 resize_bloc (bloc, MEM_ROUNDUP (size));
973 /* Never mind if this fails, just do nothing... */
974 /* It *should* be infallible! */
975 }
976 }
977 else if (size > bloc->size)
978 {
979 if (r_alloc_freeze_level)
980 {
981 bloc_ptr new_bloc;
982 new_bloc = get_bloc (MEM_ROUNDUP (size));
983 if (new_bloc)
984 {
985 new_bloc->variable = ptr;
986 *ptr = new_bloc->data;
987 bloc->variable = NULL;
988 }
989 else
990 return NULL;
991 }
992 else
993 {
994 if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
995 return NULL;
996 }
997 }
998 return *ptr;
999 }
1000
1001
1002 #if defined (emacs) && defined (DOUG_LEA_MALLOC)
1003
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)
1008 {
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)
1012 {
1013 real_morecore = __morecore;
1014 __morecore = r_alloc_sbrk;
1015 }
1016 }
1017
1018 #endif /* emacs && DOUG_LEA_MALLOC */
1019
1020 #ifdef DEBUG
1021
1022 #include <assert.h>
1023
1024 void
1025 r_alloc_check (void)
1026 {
1027 int found = 0;
1028 heap_ptr h, ph = 0;
1029 bloc_ptr b, pb = 0;
1030
1031 if (!r_alloc_initialized)
1032 return;
1033
1034 assert (first_heap);
1035 assert (last_heap->end <= (void *) sbrk (0));
1036 assert ((void *) first_heap < first_heap->start);
1037 assert (first_heap->start <= virtual_break_value);
1038 assert (virtual_break_value <= first_heap->end);
1039
1040 for (h = first_heap; h; h = h->next)
1041 {
1042 assert (h->prev == ph);
1043 assert ((void *) PAGE_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 ((void *) MEM_ROUNDUP (h->start) == h->start);
1048 #endif
1049 assert ((void *) MEM_ROUNDUP (h->bloc_start) == h->bloc_start);
1050 assert (h->start <= h->bloc_start && h->bloc_start <= h->end);
1051
1052 if (ph)
1053 {
1054 assert (ph->end < h->start);
1055 assert (h->start <= (void *) h && (void *) (h + 1) <= h->bloc_start);
1056 }
1057
1058 if (h->bloc_start <= break_value && break_value <= h->end)
1059 found = 1;
1060
1061 ph = h;
1062 }
1063
1064 assert (found);
1065 assert (last_heap == ph);
1066
1067 for (b = first_bloc; b; b = b->next)
1068 {
1069 assert (b->prev == pb);
1070 assert ((void *) MEM_ROUNDUP (b->data) == b->data);
1071 assert ((size_t) MEM_ROUNDUP (b->size) == b->size);
1072
1073 ph = 0;
1074 for (h = first_heap; h; h = h->next)
1075 {
1076 if (h->bloc_start <= b->data && b->data + b->size <= h->end)
1077 break;
1078 ph = h;
1079 }
1080
1081 assert (h);
1082
1083 if (pb && pb->data + pb->size != b->data)
1084 {
1085 assert (ph && b->data == h->bloc_start);
1086 while (ph)
1087 {
1088 if (ph->bloc_start <= pb->data
1089 && pb->data + pb->size <= ph->end)
1090 {
1091 assert (pb->data + pb->size + b->size > ph->end);
1092 break;
1093 }
1094 else
1095 {
1096 assert (ph->bloc_start + b->size > ph->end);
1097 }
1098 ph = ph->prev;
1099 }
1100 }
1101 pb = b;
1102 }
1103
1104 assert (last_bloc == pb);
1105
1106 if (last_bloc)
1107 assert (last_bloc->data + last_bloc->size == break_value);
1108 else
1109 assert (first_heap->bloc_start == break_value);
1110 }
1111
1112 #endif /* DEBUG */
1113
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 (void **old, void **new)
1121 {
1122 bloc_ptr bloc = first_bloc;
1123
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)
1128 {
1129 if (bloc->data == *new)
1130 break;
1131
1132 bloc = bloc->next;
1133 }
1134
1135 if (bloc == NIL_BLOC || bloc->variable != old)
1136 emacs_abort (); /* Already freed? OLD not originally used to allocate? */
1137
1138 /* Update variable to point to the new location. */
1139 bloc->variable = new;
1140 }
1141
1142 void
1143 r_alloc_inhibit_buffer_relocation (int inhibit)
1144 {
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++;
1151 }
1152
1153 \f
1154 /***********************************************************************
1155 Initialization
1156 ***********************************************************************/
1157
1158 /* Initialize various things for memory allocation. */
1159
1160 static void
1161 r_alloc_init (void)
1162 {
1163 if (r_alloc_initialized)
1164 return;
1165 r_alloc_initialized = 1;
1166
1167 page_size = PAGE;
1168 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
1169 real_morecore = __morecore;
1170 __morecore = r_alloc_sbrk;
1171
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 == NULL)
1177 emacs_abort ();
1178
1179 extra_bytes = PAGE_ROUNDUP (50000);
1180 #endif
1181
1182 #ifdef DOUG_LEA_MALLOC
1183 block_input ();
1184 mallopt (M_TOP_PAD, 64 * 4096);
1185 unblock_input ();
1186 #else
1187 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
1188 /* Give GNU malloc's morecore some hysteresis so that we move all
1189 the relocatable blocks much less often. The number used to be
1190 64, but alloc.c would override that with 32 in code that was
1191 removed when SYNC_INPUT became the only input handling mode.
1192 That code was conditioned on !DOUG_LEA_MALLOC, so the call to
1193 mallopt above is left unchanged. (Actually, I think there's no
1194 system nowadays that uses DOUG_LEA_MALLOC and also uses
1195 REL_ALLOC.) */
1196 __malloc_extra_blocks = 32;
1197 #endif
1198 #endif
1199
1200 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
1201 first_heap->end = (void *) PAGE_ROUNDUP (first_heap->start);
1202
1203 /* The extra call to real_morecore guarantees that the end of the
1204 address space is a multiple of page_size, even if page_size is
1205 not really the page size of the system running the binary in
1206 which page_size is stored. This allows a binary to be built on a
1207 system with one page size and run on a system with a smaller page
1208 size. */
1209 real_morecore ((char *) first_heap->end - (char *) first_heap->start);
1210
1211 /* Clear the rest of the last page; this memory is in our address space
1212 even though it is after the sbrk value. */
1213 /* Doubly true, with the additional call that explicitly adds the
1214 rest of that page to the address space. */
1215 memset (first_heap->start, 0,
1216 (char *) first_heap->end - (char *) first_heap->start);
1217 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
1218 #endif
1219
1220 use_relocatable_buffers = 1;
1221 }