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