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