Fix bug #9221 with memory leak in bidi display.
[bpt/emacs.git] / src / ralloc.c
CommitLineData
177c0ea7 1/* Block-relocating memory allocator.
73b0cd50 2 Copyright (C) 1993, 1995, 2000-2011 Free Software Foundation, Inc.
dcfdbac7
JB
3
4This file is part of GNU Emacs.
5
9ec0b715 6GNU Emacs is free software: you can redistribute it and/or modify
dcfdbac7 7it under the terms of the GNU General Public License as published by
9ec0b715
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
dcfdbac7
JB
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9ec0b715 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
dcfdbac7
JB
18
19/* NOTES:
20
eb8c3be9 21 Only relocate the blocs necessary for SIZE in r_alloc_sbrk,
dcfdbac7 22 rather than all of them. This means allowing for a possible
abe9ff32 23 hole between the first bloc and the end of malloc storage. */
dcfdbac7 24
2c46d29f 25#ifdef emacs
aef4d570 26
18160b98 27#include <config.h>
d7306fe6 28#include <setjmp.h>
956ace37 29#include "lisp.h" /* Needed for VALBITS. */
a4766fd5 30#include "blockinput.h"
0a58f946 31
642a1733 32#include <unistd.h>
a8c0e5ea 33
0a58f946
GM
34typedef POINTER_TYPE *POINTER;
35typedef size_t SIZE;
f275fd9a 36
b0119c68 37#ifdef DOUG_LEA_MALLOC
177c0ea7 38#define M_TOP_PAD -2
971de7fb 39extern int mallopt (int, int);
0a58f946 40#else /* not DOUG_LEA_MALLOC */
a2c23c92 41#ifndef SYSTEM_MALLOC
b1685c5f 42extern size_t __malloc_extra_blocks;
a2c23c92 43#endif /* SYSTEM_MALLOC */
0a58f946 44#endif /* not DOUG_LEA_MALLOC */
49081834 45
d5179acc 46#else /* not emacs */
aef4d570 47
2c46d29f 48#include <stddef.h>
aef4d570 49
2c46d29f
RS
50typedef size_t SIZE;
51typedef void *POINTER;
aef4d570 52
aef4d570
RM
53#include <unistd.h>
54#include <malloc.h>
aef4d570 55
d5179acc 56#endif /* not emacs */
2c46d29f 57
0a58f946 58
d5179acc 59#include "getpagesize.h"
dcfdbac7
JB
60
61#define NIL ((POINTER) 0)
62
2c46d29f
RS
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
0a58f946
GM
68 automatic variable, and loses its value each time Emacs is started
69 up. */
70
2c46d29f
RS
71static int r_alloc_initialized = 0;
72
971de7fb 73static void r_alloc_init (void);
0a58f946 74
dcfdbac7 75\f
956ace37
JB
76/* Declarations for working with the malloc, ralloc, and system breaks. */
77
abe9ff32 78/* Function to set the real break value. */
361358ea 79POINTER (*real_morecore) (long int);
dcfdbac7 80
abe9ff32 81/* The break value, as seen by malloc. */
dcfdbac7
JB
82static POINTER virtual_break_value;
83
abe9ff32
RS
84/* The address of the end of the last data in use by ralloc,
85 including relocatable blocs as well as malloc data. */
dcfdbac7
JB
86static POINTER break_value;
87
7516b7d5
RS
88/* This is the size of a page. We round memory requests to this boundary. */
89static int page_size;
90
177c0ea7 91/* Whenever we get memory from the system, get this many extra bytes. This
ad3bb3d2 92 must be a multiple of page_size. */
7516b7d5
RS
93static int extra_bytes;
94
dcfdbac7 95/* Macros for rounding. Note that rounding to any value is possible
abe9ff32 96 by changing the definition of PAGE. */
dcfdbac7 97#define PAGE (getpagesize ())
f7a009a5
RM
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))
7516b7d5 101#define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
e429caa2
KH
102
103#define MEM_ALIGN sizeof(double)
104#define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \
105 & ~(MEM_ALIGN - 1))
0a58f946 106
aeac019e
GM
107/* The hook `malloc' uses for the function which gets more space
108 from the system. */
109
110#ifndef SYSTEM_MALLOC
361358ea 111extern POINTER (*__morecore) (long int);
aeac019e
GM
112#endif
113
114
e429caa2 115\f
0a58f946
GM
116/***********************************************************************
117 Implementation using sbrk
118 ***********************************************************************/
119
abe9ff32
RS
120/* Data structures of heaps and blocs. */
121
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'.
126
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.
130
131 We try to make just one heap and make it larger as necessary.
8e6208c5 132 But sometimes we can't do that, because we can't get contiguous
abe9ff32 133 space to add onto the heap. When that happens, we start a new heap. */
177c0ea7 134
e429caa2
KH
135typedef struct heap
136{
137 struct heap *next;
138 struct heap *prev;
abe9ff32 139 /* Start of memory range of this heap. */
e429caa2 140 POINTER start;
abe9ff32 141 /* End of memory range of this heap. */
e429caa2 142 POINTER end;
abe9ff32
RS
143 /* Start of relocatable data in this heap. */
144 POINTER bloc_start;
145 /* Start of unused space in this heap. */
146 POINTER free;
47f13333
RS
147 /* First bloc in this heap. */
148 struct bp *first_bloc;
149 /* Last bloc in this heap. */
150 struct bp *last_bloc;
e429caa2
KH
151} *heap_ptr;
152
153#define NIL_HEAP ((heap_ptr) 0)
154#define HEAP_PTR_SIZE (sizeof (struct heap))
155
abe9ff32
RS
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. */
159static struct heap heap_base;
160
161/* Head and tail of the list of heaps. */
e429caa2
KH
162static heap_ptr first_heap, last_heap;
163
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
177c0ea7 167 b->data + b->size == b->next->data.
49f82b3d
RS
168
169 An element with variable==NIL denotes a freed block, which has not yet
f96f2c5b
JB
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. */
49f82b3d 173
e429caa2
KH
174typedef struct bp
175{
176 struct bp *next;
177 struct bp *prev;
178 POINTER *variable;
179 POINTER data;
180 SIZE size;
8e6208c5 181 POINTER new_data; /* temporarily used for relocation */
49f82b3d 182 struct heap *heap; /* Heap this bloc is in. */
e429caa2
KH
183} *bloc_ptr;
184
185#define NIL_BLOC ((bloc_ptr) 0)
186#define BLOC_PTR_SIZE (sizeof (struct bp))
187
abe9ff32 188/* Head and tail of the list of relocatable blocs. */
e429caa2
KH
189static bloc_ptr first_bloc, last_bloc;
190
49f82b3d
RS
191static int use_relocatable_buffers;
192
193/* If >0, no relocation whatsoever takes place. */
194static int r_alloc_freeze_level;
195
dcfdbac7 196\f
956ace37
JB
197/* Functions to get and return memory from the system. */
198
abe9ff32
RS
199/* Find the heap that ADDRESS falls within. */
200
201static heap_ptr
971de7fb 202find_heap (POINTER address)
abe9ff32
RS
203{
204 heap_ptr heap;
205
206 for (heap = last_heap; heap; heap = heap->prev)
207 {
208 if (heap->start <= address && address <= heap->end)
209 return heap;
210 }
211
212 return NIL_HEAP;
213}
214
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.
218
e429caa2 219 If enough space is not presently available in our reserve, this means
8e6208c5
KH
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
abe9ff32
RS
222
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.
dcfdbac7 229
e429caa2
KH
230 Return the address of the space if all went well, or zero if we couldn't
231 allocate the memory. */
abe9ff32 232
e429caa2 233static POINTER
971de7fb 234obtain (POINTER address, SIZE size)
dcfdbac7 235{
e429caa2
KH
236 heap_ptr heap;
237 SIZE already_available;
dcfdbac7 238
abe9ff32 239 /* Find the heap that ADDRESS falls within. */
e429caa2 240 for (heap = last_heap; heap; heap = heap->prev)
dcfdbac7 241 {
e429caa2
KH
242 if (heap->start <= address && address <= heap->end)
243 break;
244 }
dcfdbac7 245
e429caa2 246 if (! heap)
abe9ff32 247 abort ();
dcfdbac7 248
abe9ff32
RS
249 /* If we can't fit SIZE bytes in that heap,
250 try successive later heaps. */
91a211b5 251 while (heap && (char *) address + size > (char *) heap->end)
e429caa2
KH
252 {
253 heap = heap->next;
254 if (heap == NIL_HEAP)
255 break;
256 address = heap->bloc_start;
dcfdbac7
JB
257 }
258
abe9ff32
RS
259 /* If we can't fit them within any existing heap,
260 get more space. */
e429caa2
KH
261 if (heap == NIL_HEAP)
262 {
263 POINTER new = (*real_morecore)(0);
264 SIZE get;
98b7fe02 265
e429caa2 266 already_available = (char *)last_heap->end - (char *)address;
dcfdbac7 267
e429caa2
KH
268 if (new != last_heap->end)
269 {
abe9ff32
RS
270 /* Someone else called sbrk. Make a new heap. */
271
272 heap_ptr new_heap = (heap_ptr) MEM_ROUNDUP (new);
273 POINTER bloc_start = (POINTER) MEM_ROUNDUP ((POINTER)(new_heap + 1));
e429caa2 274
91a211b5 275 if ((*real_morecore) ((char *) bloc_start - (char *) new) != new)
e429caa2
KH
276 return 0;
277
278 new_heap->start = new;
279 new_heap->end = bloc_start;
280 new_heap->bloc_start = bloc_start;
abe9ff32 281 new_heap->free = bloc_start;
e429caa2
KH
282 new_heap->next = NIL_HEAP;
283 new_heap->prev = last_heap;
47f13333
RS
284 new_heap->first_bloc = NIL_BLOC;
285 new_heap->last_bloc = NIL_BLOC;
e429caa2
KH
286 last_heap->next = new_heap;
287 last_heap = new_heap;
288
289 address = bloc_start;
290 already_available = 0;
291 }
dcfdbac7 292
abe9ff32
RS
293 /* Add space to the last heap (which we may have just created).
294 Get some extra, so we can come here less often. */
295
e429caa2 296 get = size + extra_bytes - already_available;
abe9ff32 297 get = (char *) ROUNDUP ((char *)last_heap->end + get)
e429caa2 298 - (char *) last_heap->end;
dcfdbac7 299
e429caa2
KH
300 if ((*real_morecore) (get) != last_heap->end)
301 return 0;
302
91a211b5 303 last_heap->end = (char *) last_heap->end + get;
e429caa2
KH
304 }
305
306 return address;
307}
dcfdbac7 308
abe9ff32
RS
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. */
313
dcfdbac7 314static void
971de7fb 315relinquish (void)
dcfdbac7 316{
e429caa2 317 register heap_ptr h;
8d31e373 318 long excess = 0;
e429caa2 319
abe9ff32
RS
320 /* Add the amount of space beyond break_value
321 in all heaps which have extend beyond break_value at all. */
322
e429caa2
KH
323 for (h = last_heap; h && break_value < h->end; h = h->prev)
324 {
325 excess += (char *) h->end - (char *) ((break_value < h->bloc_start)
326 ? h->bloc_start : break_value);
327 }
328
329 if (excess > extra_bytes * 2 && (*real_morecore) (0) == last_heap->end)
dcfdbac7 330 {
7516b7d5
RS
331 /* Keep extra_bytes worth of empty space.
332 And don't free anything unless we can free at least extra_bytes. */
e429caa2 333 excess -= extra_bytes;
dcfdbac7 334
e429caa2
KH
335 if ((char *)last_heap->end - (char *)last_heap->bloc_start <= excess)
336 {
47f13333
RS
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 ();
341
abe9ff32 342 /* Return the last heap, with its header, to the system. */
e429caa2
KH
343 excess = (char *)last_heap->end - (char *)last_heap->start;
344 last_heap = last_heap->prev;
345 last_heap->next = NIL_HEAP;
346 }
347 else
348 {
349 excess = (char *) last_heap->end
abe9ff32 350 - (char *) ROUNDUP ((char *)last_heap->end - excess);
91a211b5 351 last_heap->end = (char *) last_heap->end - excess;
e429caa2 352 }
dcfdbac7 353
e429caa2 354 if ((*real_morecore) (- excess) == 0)
21532667
KH
355 {
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. */
91a211b5 359 last_heap->end = (char *) last_heap->end + excess;
21532667
KH
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 ();
366 }
e429caa2 367 }
dcfdbac7 368}
719b242f
RS
369
370/* Return the total size in use by relocating allocator,
371 above where malloc gets space. */
372
373long
971de7fb 374r_alloc_size_in_use (void)
719b242f 375{
91a211b5 376 return (char *) break_value - (char *) virtual_break_value;
719b242f 377}
dcfdbac7 378\f
956ace37
JB
379/* The meat - allocating, freeing, and relocating blocs. */
380
956ace37 381/* Find the bloc referenced by the address in PTR. Returns a pointer
abe9ff32 382 to that block. */
dcfdbac7
JB
383
384static bloc_ptr
971de7fb 385find_bloc (POINTER *ptr)
dcfdbac7
JB
386{
387 register bloc_ptr p = first_bloc;
388
389 while (p != NIL_BLOC)
390 {
747d9d14
JR
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. */
dcfdbac7
JB
396 if (p->variable == ptr && p->data == *ptr)
397 return p;
398
399 p = p->next;
400 }
401
402 return p;
403}
404
405/* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
98b7fe02
JB
406 Returns a pointer to the new bloc, or zero if we couldn't allocate
407 memory for the new block. */
dcfdbac7
JB
408
409static bloc_ptr
971de7fb 410get_bloc (SIZE size)
dcfdbac7 411{
98b7fe02 412 register bloc_ptr new_bloc;
abe9ff32 413 register heap_ptr heap;
98b7fe02
JB
414
415 if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
e429caa2 416 || ! (new_bloc->data = obtain (break_value, size)))
98b7fe02 417 {
c2cd06e6 418 free (new_bloc);
98b7fe02
JB
419
420 return 0;
421 }
dcfdbac7 422
91a211b5 423 break_value = (char *) new_bloc->data + size;
e429caa2 424
dcfdbac7
JB
425 new_bloc->size = size;
426 new_bloc->next = NIL_BLOC;
8c7f1e35 427 new_bloc->variable = (POINTER *) NIL;
e429caa2 428 new_bloc->new_data = 0;
dcfdbac7 429
abe9ff32
RS
430 /* Record in the heap that this space is in use. */
431 heap = find_heap (new_bloc->data);
432 heap->free = break_value;
433
47f13333
RS
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;
439
abe9ff32 440 /* Put this bloc on the doubly-linked list of blocs. */
dcfdbac7
JB
441 if (first_bloc)
442 {
443 new_bloc->prev = last_bloc;
444 last_bloc->next = new_bloc;
445 last_bloc = new_bloc;
446 }
447 else
448 {
449 first_bloc = last_bloc = new_bloc;
450 new_bloc->prev = NIL_BLOC;
451 }
452
453 return new_bloc;
454}
47f13333 455\f
abe9ff32
RS
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
177c0ea7
JB
459 more space.
460
abe9ff32
RS
461 Store the new location of each bloc in its new_data field.
462 Do not touch the contents of blocs or break_value. */
dcfdbac7 463
e429caa2 464static int
971de7fb 465relocate_blocs (bloc_ptr bloc, heap_ptr heap, POINTER address)
e429caa2
KH
466{
467 register bloc_ptr b = bloc;
ad3bb3d2 468
49f82b3d 469 /* No need to ever call this if arena is frozen, bug somewhere! */
177c0ea7 470 if (r_alloc_freeze_level)
49f82b3d
RS
471 abort();
472
e429caa2
KH
473 while (b)
474 {
abe9ff32
RS
475 /* If bloc B won't fit within HEAP,
476 move to the next heap and try again. */
91a211b5 477 while (heap && (char *) address + b->size > (char *) heap->end)
e429caa2
KH
478 {
479 heap = heap->next;
480 if (heap == NIL_HEAP)
481 break;
482 address = heap->bloc_start;
483 }
dcfdbac7 484
abe9ff32
RS
485 /* If BLOC won't fit in any heap,
486 get enough new space to hold BLOC and all following blocs. */
e429caa2
KH
487 if (heap == NIL_HEAP)
488 {
489 register bloc_ptr tb = b;
490 register SIZE s = 0;
491
abe9ff32 492 /* Add up the size of all the following blocs. */
e429caa2
KH
493 while (tb != NIL_BLOC)
494 {
177c0ea7 495 if (tb->variable)
49f82b3d
RS
496 s += tb->size;
497
e429caa2
KH
498 tb = tb->next;
499 }
500
abe9ff32
RS
501 /* Get that space. */
502 address = obtain (address, s);
503 if (address == 0)
e429caa2
KH
504 return 0;
505
506 heap = last_heap;
507 }
508
abe9ff32
RS
509 /* Record the new address of this bloc
510 and update where the next bloc can start. */
e429caa2 511 b->new_data = address;
177c0ea7 512 if (b->variable)
91a211b5 513 address = (char *) address + b->size;
e429caa2
KH
514 b = b->next;
515 }
516
517 return 1;
518}
47f13333
RS
519\f
520/* Update the records of which heaps contain which blocs, starting
521 with heap HEAP and bloc BLOC. */
522
523static void
971de7fb 524update_heap_bloc_correspondence (bloc_ptr bloc, heap_ptr heap)
abe9ff32
RS
525{
526 register bloc_ptr b;
527
47f13333
RS
528 /* Initialize HEAP's status to reflect blocs before BLOC. */
529 if (bloc != NIL_BLOC && bloc->prev != NIL_BLOC && bloc->prev->heap == heap)
530 {
531 /* The previous bloc is in HEAP. */
532 heap->last_bloc = bloc->prev;
91a211b5 533 heap->free = (char *) bloc->prev->data + bloc->prev->size;
47f13333
RS
534 }
535 else
536 {
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;
541 }
542
abe9ff32
RS
543 /* Advance through blocs one by one. */
544 for (b = bloc; b != NIL_BLOC; b = b->next)
545 {
47f13333
RS
546 /* Advance through heaps, marking them empty,
547 till we get to the one that B is in. */
abe9ff32
RS
548 while (heap)
549 {
550 if (heap->bloc_start <= b->data && b->data <= heap->end)
551 break;
552 heap = heap->next;
47f13333
RS
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;
abe9ff32
RS
557 heap->free = heap->bloc_start;
558 }
47f13333
RS
559
560 /* Update HEAP's status for bloc B. */
91a211b5 561 heap->free = (char *) b->data + b->size;
47f13333
RS
562 heap->last_bloc = b;
563 if (heap->first_bloc == NIL_BLOC)
564 heap->first_bloc = b;
565
566 /* Record that B is in HEAP. */
567 b->heap = heap;
abe9ff32
RS
568 }
569
570 /* If there are any remaining heaps and no blocs left,
47f13333 571 mark those heaps as empty. */
abe9ff32
RS
572 heap = heap->next;
573 while (heap)
574 {
47f13333
RS
575 heap->first_bloc = NIL_BLOC;
576 heap->last_bloc = NIL_BLOC;
abe9ff32
RS
577 heap->free = heap->bloc_start;
578 heap = heap->next;
579 }
580}
47f13333 581\f
abe9ff32
RS
582/* Resize BLOC to SIZE bytes. This relocates the blocs
583 that come after BLOC in memory. */
584
e429caa2 585static int
971de7fb 586resize_bloc (bloc_ptr bloc, SIZE size)
dcfdbac7 587{
e429caa2
KH
588 register bloc_ptr b;
589 heap_ptr heap;
590 POINTER address;
591 SIZE old_size;
592
49f82b3d 593 /* No need to ever call this if arena is frozen, bug somewhere! */
177c0ea7 594 if (r_alloc_freeze_level)
49f82b3d
RS
595 abort();
596
e429caa2
KH
597 if (bloc == NIL_BLOC || size == bloc->size)
598 return 1;
599
600 for (heap = first_heap; heap != NIL_HEAP; heap = heap->next)
601 {
602 if (heap->bloc_start <= bloc->data && bloc->data <= heap->end)
603 break;
604 }
605
606 if (heap == NIL_HEAP)
abe9ff32 607 abort ();
e429caa2
KH
608
609 old_size = bloc->size;
610 bloc->size = size;
611
abe9ff32 612 /* Note that bloc could be moved into the previous heap. */
91a211b5
GM
613 address = (bloc->prev ? (char *) bloc->prev->data + bloc->prev->size
614 : (char *) first_heap->bloc_start);
e429caa2
KH
615 while (heap)
616 {
617 if (heap->bloc_start <= address && address <= heap->end)
618 break;
619 heap = heap->prev;
620 }
621
622 if (! relocate_blocs (bloc, heap, address))
623 {
624 bloc->size = old_size;
625 return 0;
626 }
627
628 if (size > old_size)
629 {
630 for (b = last_bloc; b != bloc; b = b->prev)
631 {
49f82b3d
RS
632 if (!b->variable)
633 {
634 b->size = 0;
635 b->data = b->new_data;
177c0ea7
JB
636 }
637 else
49f82b3d 638 {
72af86bd 639 memmove (b->new_data, b->data, b->size);
49f82b3d
RS
640 *b->variable = b->data = b->new_data;
641 }
642 }
643 if (!bloc->variable)
644 {
645 bloc->size = 0;
646 bloc->data = bloc->new_data;
647 }
648 else
649 {
72af86bd 650 memmove (bloc->new_data, bloc->data, old_size);
3ce2f8ac 651 memset ((char *) bloc->new_data + old_size, 0, size - old_size);
49f82b3d 652 *bloc->variable = bloc->data = bloc->new_data;
e429caa2 653 }
e429caa2
KH
654 }
655 else
dcfdbac7 656 {
ad3bb3d2
JB
657 for (b = bloc; b != NIL_BLOC; b = b->next)
658 {
49f82b3d
RS
659 if (!b->variable)
660 {
661 b->size = 0;
662 b->data = b->new_data;
177c0ea7
JB
663 }
664 else
49f82b3d 665 {
72af86bd 666 memmove (b->new_data, b->data, b->size);
49f82b3d
RS
667 *b->variable = b->data = b->new_data;
668 }
ad3bb3d2 669 }
ad3bb3d2 670 }
dcfdbac7 671
47f13333 672 update_heap_bloc_correspondence (bloc, heap);
abe9ff32 673
91a211b5
GM
674 break_value = (last_bloc ? (char *) last_bloc->data + last_bloc->size
675 : (char *) first_heap->bloc_start);
e429caa2
KH
676 return 1;
677}
47f13333 678\f
abe9ff32
RS
679/* Free BLOC from the chain of blocs, relocating any blocs above it.
680 This may return space to the system. */
dcfdbac7
JB
681
682static void
971de7fb 683free_bloc (bloc_ptr bloc)
dcfdbac7 684{
47f13333
RS
685 heap_ptr heap = bloc->heap;
686
49f82b3d
RS
687 if (r_alloc_freeze_level)
688 {
689 bloc->variable = (POINTER *) NIL;
690 return;
691 }
177c0ea7 692
e429caa2
KH
693 resize_bloc (bloc, 0);
694
dcfdbac7
JB
695 if (bloc == first_bloc && bloc == last_bloc)
696 {
697 first_bloc = last_bloc = NIL_BLOC;
698 }
699 else if (bloc == last_bloc)
700 {
701 last_bloc = bloc->prev;
702 last_bloc->next = NIL_BLOC;
703 }
704 else if (bloc == first_bloc)
705 {
706 first_bloc = bloc->next;
707 first_bloc->prev = NIL_BLOC;
dcfdbac7
JB
708 }
709 else
710 {
711 bloc->next->prev = bloc->prev;
712 bloc->prev->next = bloc->next;
dcfdbac7
JB
713 }
714
47f13333
RS
715 /* Update the records of which blocs are in HEAP. */
716 if (heap->first_bloc == bloc)
717 {
d5179acc 718 if (bloc->next != 0 && bloc->next->heap == heap)
47f13333
RS
719 heap->first_bloc = bloc->next;
720 else
721 heap->first_bloc = heap->last_bloc = NIL_BLOC;
722 }
723 if (heap->last_bloc == bloc)
724 {
d5179acc 725 if (bloc->prev != 0 && bloc->prev->heap == heap)
47f13333
RS
726 heap->last_bloc = bloc->prev;
727 else
728 heap->first_bloc = heap->last_bloc = NIL_BLOC;
729 }
730
e429caa2 731 relinquish ();
dcfdbac7
JB
732 free (bloc);
733}
734\f
956ace37
JB
735/* Interface routines. */
736
98b7fe02 737/* Obtain SIZE bytes of storage from the free pool, or the system, as
2c46d29f 738 necessary. If relocatable blocs are in use, this means relocating
98b7fe02
JB
739 them. This function gets plugged into the GNU malloc's __morecore
740 hook.
741
7516b7d5
RS
742 We provide hysteresis, never relocating by less than extra_bytes.
743
98b7fe02
JB
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. */
dcfdbac7 747
177c0ea7 748POINTER
971de7fb 749r_alloc_sbrk (long int size)
dcfdbac7 750{
e429caa2
KH
751 register bloc_ptr b;
752 POINTER address;
dcfdbac7 753
44d3dec0
RS
754 if (! r_alloc_initialized)
755 r_alloc_init ();
756
dcfdbac7 757 if (! use_relocatable_buffers)
bbc60227 758 return (*real_morecore) (size);
dcfdbac7 759
e429caa2
KH
760 if (size == 0)
761 return virtual_break_value;
7516b7d5 762
e429caa2 763 if (size > 0)
dcfdbac7 764 {
abe9ff32
RS
765 /* Allocate a page-aligned space. GNU malloc would reclaim an
766 extra space if we passed an unaligned one. But we could
8e6208c5 767 not always find a space which is contiguous to the previous. */
e429caa2
KH
768 POINTER new_bloc_start;
769 heap_ptr h = first_heap;
abe9ff32 770 SIZE get = ROUNDUP (size);
7516b7d5 771
abe9ff32 772 address = (POINTER) ROUNDUP (virtual_break_value);
e429caa2 773
abe9ff32
RS
774 /* Search the list upward for a heap which is large enough. */
775 while ((char *) h->end < (char *) MEM_ROUNDUP ((char *)address + get))
e429caa2
KH
776 {
777 h = h->next;
778 if (h == NIL_HEAP)
779 break;
abe9ff32 780 address = (POINTER) ROUNDUP (h->start);
e429caa2
KH
781 }
782
abe9ff32 783 /* If not found, obtain more space. */
e429caa2
KH
784 if (h == NIL_HEAP)
785 {
786 get += extra_bytes + page_size;
787
49f82b3d 788 if (! obtain (address, get))
e429caa2 789 return 0;
98b7fe02 790
e429caa2 791 if (first_heap == last_heap)
abe9ff32 792 address = (POINTER) ROUNDUP (virtual_break_value);
e429caa2 793 else
abe9ff32 794 address = (POINTER) ROUNDUP (last_heap->start);
e429caa2
KH
795 h = last_heap;
796 }
797
abe9ff32 798 new_bloc_start = (POINTER) MEM_ROUNDUP ((char *)address + get);
e429caa2
KH
799
800 if (first_heap->bloc_start < new_bloc_start)
801 {
49f82b3d 802 /* This is no clean solution - no idea how to do it better. */
177c0ea7 803 if (r_alloc_freeze_level)
49f82b3d
RS
804 return NIL;
805
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. */
809
abe9ff32 810 /* Move all blocs upward. */
49f82b3d 811 if (! relocate_blocs (first_bloc, h, new_bloc_start))
e429caa2
KH
812 return 0;
813
814 /* Note that (POINTER)(h+1) <= new_bloc_start since
815 get >= page_size, so the following does not destroy the heap
abe9ff32 816 header. */
e429caa2
KH
817 for (b = last_bloc; b != NIL_BLOC; b = b->prev)
818 {
72af86bd 819 memmove (b->new_data, b->data, b->size);
e429caa2
KH
820 *b->variable = b->data = b->new_data;
821 }
822
823 h->bloc_start = new_bloc_start;
abe9ff32 824
47f13333 825 update_heap_bloc_correspondence (first_bloc, h);
e429caa2 826 }
e429caa2
KH
827 if (h != first_heap)
828 {
829 /* Give up managing heaps below the one the new
abe9ff32 830 virtual_break_value points to. */
e429caa2
KH
831 first_heap->prev = NIL_HEAP;
832 first_heap->next = h->next;
833 first_heap->start = h->start;
834 first_heap->end = h->end;
abe9ff32 835 first_heap->free = h->free;
47f13333
RS
836 first_heap->first_bloc = h->first_bloc;
837 first_heap->last_bloc = h->last_bloc;
e429caa2
KH
838 first_heap->bloc_start = h->bloc_start;
839
840 if (first_heap->next)
841 first_heap->next->prev = first_heap;
842 else
843 last_heap = first_heap;
844 }
845
72af86bd 846 memset (address, 0, size);
dcfdbac7 847 }
e429caa2 848 else /* size < 0 */
dcfdbac7 849 {
e429caa2
KH
850 SIZE excess = (char *)first_heap->bloc_start
851 - ((char *)virtual_break_value + size);
852
853 address = virtual_break_value;
854
855 if (r_alloc_freeze_level == 0 && excess > 2 * extra_bytes)
856 {
857 excess -= extra_bytes;
858 first_heap->bloc_start
47f13333 859 = (POINTER) MEM_ROUNDUP ((char *)first_heap->bloc_start - excess);
e429caa2 860
abe9ff32 861 relocate_blocs (first_bloc, first_heap, first_heap->bloc_start);
7516b7d5 862
e429caa2
KH
863 for (b = first_bloc; b != NIL_BLOC; b = b->next)
864 {
72af86bd 865 memmove (b->new_data, b->data, b->size);
e429caa2
KH
866 *b->variable = b->data = b->new_data;
867 }
868 }
869
870 if ((char *)virtual_break_value + size < (char *)first_heap->start)
871 {
872 /* We found an additional space below the first heap */
873 first_heap->start = (POINTER) ((char *)virtual_break_value + size);
874 }
dcfdbac7
JB
875 }
876
e429caa2 877 virtual_break_value = (POINTER) ((char *)address + size);
47f13333 878 break_value = (last_bloc
91a211b5
GM
879 ? (char *) last_bloc->data + last_bloc->size
880 : (char *) first_heap->bloc_start);
e429caa2 881 if (size < 0)
abe9ff32 882 relinquish ();
7516b7d5 883
e429caa2 884 return address;
dcfdbac7
JB
885}
886
0a58f946 887
dcfdbac7
JB
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
98b7fe02
JB
890 which will use the data area.
891
49f82b3d 892 The allocation of 0 bytes is valid.
f96f2c5b
JB
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.
49f82b3d 895
98b7fe02
JB
896 If we can't allocate the necessary memory, set *PTR to zero, and
897 return zero. */
dcfdbac7
JB
898
899POINTER
971de7fb 900r_alloc (POINTER *ptr, SIZE size)
dcfdbac7
JB
901{
902 register bloc_ptr new_bloc;
903
2c46d29f
RS
904 if (! r_alloc_initialized)
905 r_alloc_init ();
906
abe9ff32 907 new_bloc = get_bloc (MEM_ROUNDUP (size));
98b7fe02
JB
908 if (new_bloc)
909 {
910 new_bloc->variable = ptr;
911 *ptr = new_bloc->data;
912 }
913 else
914 *ptr = 0;
dcfdbac7
JB
915
916 return *ptr;
917}
918
2c46d29f
RS
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. */
dcfdbac7
JB
921
922void
971de7fb 923r_alloc_free (register POINTER *ptr)
dcfdbac7
JB
924{
925 register bloc_ptr dead_bloc;
926
44d3dec0
RS
927 if (! r_alloc_initialized)
928 r_alloc_init ();
929
dcfdbac7
JB
930 dead_bloc = find_bloc (ptr);
931 if (dead_bloc == NIL_BLOC)
747d9d14 932 abort (); /* Double free? PTR not originally used to allocate? */
dcfdbac7
JB
933
934 free_bloc (dead_bloc);
2c46d29f 935 *ptr = 0;
719b242f 936
d5179acc 937#ifdef emacs
719b242f 938 refill_memory_reserve ();
d5179acc 939#endif
dcfdbac7
JB
940}
941
16a5c729 942/* Given a pointer at address PTR to relocatable data, resize it to SIZE.
98b7fe02
JB
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.
dcfdbac7 946
f96f2c5b 947 In case r_alloc_freeze_level is set, a new bloc is allocated, and the
8e6208c5 948 memory copied to it. Not very efficient. We could traverse the
49f82b3d
RS
949 bloc_list for a best fit of free blocs first.
950
98b7fe02
JB
951 Change *PTR to reflect the new bloc, and return this value.
952
953 If more memory cannot be allocated, then leave *PTR unchanged, and
954 return zero. */
dcfdbac7
JB
955
956POINTER
971de7fb 957r_re_alloc (POINTER *ptr, SIZE size)
dcfdbac7 958{
16a5c729 959 register bloc_ptr bloc;
dcfdbac7 960
44d3dec0
RS
961 if (! r_alloc_initialized)
962 r_alloc_init ();
963
49f82b3d
RS
964 if (!*ptr)
965 return r_alloc (ptr, size);
177c0ea7 966 if (!size)
49f82b3d
RS
967 {
968 r_alloc_free (ptr);
969 return r_alloc (ptr, 0);
970 }
971
16a5c729
JB
972 bloc = find_bloc (ptr);
973 if (bloc == NIL_BLOC)
747d9d14 974 abort (); /* Already freed? PTR not originally used to allocate? */
dcfdbac7 975
177c0ea7 976 if (size < bloc->size)
49f82b3d
RS
977 {
978 /* Wouldn't it be useful to actually resize the bloc here? */
979 /* I think so too, but not if it's too expensive... */
177c0ea7
JB
980 if ((bloc->size - MEM_ROUNDUP (size) >= page_size)
981 && r_alloc_freeze_level == 0)
49f82b3d
RS
982 {
983 resize_bloc (bloc, MEM_ROUNDUP (size));
984 /* Never mind if this fails, just do nothing... */
985 /* It *should* be infallible! */
986 }
987 }
988 else if (size > bloc->size)
989 {
990 if (r_alloc_freeze_level)
991 {
992 bloc_ptr new_bloc;
993 new_bloc = get_bloc (MEM_ROUNDUP (size));
994 if (new_bloc)
995 {
996 new_bloc->variable = ptr;
997 *ptr = new_bloc->data;
998 bloc->variable = (POINTER *) NIL;
999 }
1000 else
1001 return NIL;
1002 }
177c0ea7 1003 else
49f82b3d
RS
1004 {
1005 if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
1006 return NIL;
1007 }
1008 }
dcfdbac7
JB
1009 return *ptr;
1010}
81bd58e8
KH
1011
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. */
abe9ff32 1016
81bd58e8 1017void
971de7fb 1018r_alloc_freeze (long int size)
81bd58e8 1019{
44d3dec0
RS
1020 if (! r_alloc_initialized)
1021 r_alloc_init ();
1022
81bd58e8
KH
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);
1032}
1033
1034void
971de7fb 1035r_alloc_thaw (void)
81bd58e8 1036{
49f82b3d 1037
177c0ea7 1038 if (! r_alloc_initialized)
49f82b3d
RS
1039 r_alloc_init ();
1040
81bd58e8
KH
1041 if (--r_alloc_freeze_level < 0)
1042 abort ();
49f82b3d 1043
177c0ea7 1044 /* This frees all unused blocs. It is not too inefficient, as the resize
72af86bd 1045 and memcpy is done only once. Afterwards, all unreferenced blocs are
49f82b3d 1046 already shrunk to zero size. */
177c0ea7 1047 if (!r_alloc_freeze_level)
49f82b3d
RS
1048 {
1049 bloc_ptr *b = &first_bloc;
177c0ea7
JB
1050 while (*b)
1051 if (!(*b)->variable)
1052 free_bloc (*b);
1053 else
49f82b3d
RS
1054 b = &(*b)->next;
1055 }
81bd58e8 1056}
49f82b3d 1057
dec41418
RS
1058
1059#if defined (emacs) && defined (DOUG_LEA_MALLOC)
1060
1061/* Reinitialize the morecore hook variables after restarting a dumped
1062 Emacs. This is needed when using Doug Lea's malloc from GNU libc. */
1063void
971de7fb 1064r_alloc_reinit (void)
dec41418
RS
1065{
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)
1069 {
1070 real_morecore = __morecore;
1071 __morecore = r_alloc_sbrk;
1072 }
1073}
0a58f946
GM
1074
1075#endif /* emacs && DOUG_LEA_MALLOC */
dec41418 1076
e429caa2 1077#ifdef DEBUG
0a58f946 1078
e429caa2
KH
1079#include <assert.h>
1080
44d3dec0 1081void
268c2c36 1082r_alloc_check (void)
e429caa2 1083{
6d16dd06
RS
1084 int found = 0;
1085 heap_ptr h, ph = 0;
1086 bloc_ptr b, pb = 0;
1087
1088 if (!r_alloc_initialized)
1089 return;
1090
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);
1096
1097 for (h = first_heap; h; h = h->next)
1098 {
1099 assert (h->prev == ph);
1100 assert ((POINTER) ROUNDUP (h->end) == h->end);
40f3f04b
RS
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. */
6d16dd06 1104 assert ((POINTER) MEM_ROUNDUP (h->start) == h->start);
40f3f04b 1105#endif
6d16dd06
RS
1106 assert ((POINTER) MEM_ROUNDUP (h->bloc_start) == h->bloc_start);
1107 assert (h->start <= h->bloc_start && h->bloc_start <= h->end);
1108
1109 if (ph)
1110 {
1111 assert (ph->end < h->start);
1112 assert (h->start <= (POINTER)h && (POINTER)(h+1) <= h->bloc_start);
1113 }
1114
1115 if (h->bloc_start <= break_value && break_value <= h->end)
1116 found = 1;
1117
1118 ph = h;
1119 }
1120
1121 assert (found);
1122 assert (last_heap == ph);
1123
1124 for (b = first_bloc; b; b = b->next)
1125 {
1126 assert (b->prev == pb);
1127 assert ((POINTER) MEM_ROUNDUP (b->data) == b->data);
1128 assert ((SIZE) MEM_ROUNDUP (b->size) == b->size);
1129
1130 ph = 0;
1131 for (h = first_heap; h; h = h->next)
1132 {
1133 if (h->bloc_start <= b->data && b->data + b->size <= h->end)
1134 break;
1135 ph = h;
1136 }
1137
1138 assert (h);
1139
1140 if (pb && pb->data + pb->size != b->data)
1141 {
1142 assert (ph && b->data == h->bloc_start);
1143 while (ph)
1144 {
1145 if (ph->bloc_start <= pb->data
1146 && pb->data + pb->size <= ph->end)
1147 {
1148 assert (pb->data + pb->size + b->size > ph->end);
1149 break;
1150 }
1151 else
1152 {
1153 assert (ph->bloc_start + b->size > ph->end);
1154 }
1155 ph = ph->prev;
1156 }
1157 }
1158 pb = b;
1159 }
1160
1161 assert (last_bloc == pb);
1162
1163 if (last_bloc)
1164 assert (last_bloc->data + last_bloc->size == break_value);
1165 else
1166 assert (first_heap->bloc_start == break_value);
e429caa2 1167}
0a58f946 1168
e429caa2 1169#endif /* DEBUG */
0a58f946 1170
baae5c2d
JR
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. */
1176void
971de7fb 1177r_alloc_reset_variable (POINTER *old, POINTER *new)
baae5c2d
JR
1178{
1179 bloc_ptr bloc = first_bloc;
1180
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)
1185 {
1186 if (bloc->data == *new)
1187 break;
1188
1189 bloc = bloc->next;
1190 }
1191
1192 if (bloc == NIL_BLOC || bloc->variable != old)
747d9d14 1193 abort (); /* Already freed? OLD not originally used to allocate? */
baae5c2d
JR
1194
1195 /* Update variable to point to the new location. */
1196 bloc->variable = new;
1197}
0a58f946
GM
1198
1199\f
1200/***********************************************************************
1201 Initialization
1202 ***********************************************************************/
1203
0a58f946
GM
1204/* Initialize various things for memory allocation. */
1205
1206static void
971de7fb 1207r_alloc_init (void)
0a58f946
GM
1208{
1209 if (r_alloc_initialized)
1210 return;
0a58f946 1211 r_alloc_initialized = 1;
177c0ea7 1212
a2c23c92
DL
1213 page_size = PAGE;
1214#ifndef SYSTEM_MALLOC
0a58f946
GM
1215 real_morecore = __morecore;
1216 __morecore = r_alloc_sbrk;
1217
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 ();
1224
0a58f946 1225 extra_bytes = ROUNDUP (50000);
a2c23c92 1226#endif
0a58f946
GM
1227
1228#ifdef DOUG_LEA_MALLOC
1673df2e
JD
1229 BLOCK_INPUT;
1230 mallopt (M_TOP_PAD, 64 * 4096);
1231 UNBLOCK_INPUT;
0a58f946 1232#else
a2c23c92 1233#ifndef SYSTEM_MALLOC
0a58f946
GM
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
a2c23c92 1238#endif
0a58f946 1239
5ad25b24 1240#ifndef SYSTEM_MALLOC
0a58f946
GM
1241 first_heap->end = (POINTER) ROUNDUP (first_heap->start);
1242
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. */
91a211b5 1249 (*real_morecore) ((char *) first_heap->end - (char *) first_heap->start);
0a58f946
GM
1250
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. */
72af86bd
AS
1255 memset (first_heap->start, 0,
1256 (char *) first_heap->end - (char *) first_heap->start);
0a58f946 1257 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
a2c23c92 1258#endif
177c0ea7 1259
0a58f946
GM
1260 use_relocatable_buffers = 1;
1261}