Commit | Line | Data |
---|---|---|
177c0ea7 | 1 | /* Block-relocating memory allocator. |
acaf905b | 2 | Copyright (C) 1993, 1995, 2000-2012 Free Software Foundation, Inc. |
dcfdbac7 JB |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
9ec0b715 | 6 | GNU Emacs is free software: you can redistribute it and/or modify |
dcfdbac7 | 7 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
8 | the Free Software Foundation, either version 3 of the License, or |
9 | (at your option) any later version. | |
dcfdbac7 JB |
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 | |
9ec0b715 | 17 | along 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 |
34 | typedef POINTER_TYPE *POINTER; |
35 | typedef size_t SIZE; | |
f275fd9a | 36 | |
b0119c68 | 37 | #ifdef DOUG_LEA_MALLOC |
177c0ea7 | 38 | #define M_TOP_PAD -2 |
971de7fb | 39 | extern int mallopt (int, int); |
0a58f946 | 40 | #else /* not DOUG_LEA_MALLOC */ |
a2c23c92 | 41 | #ifndef SYSTEM_MALLOC |
b1685c5f | 42 | extern 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 |
50 | typedef size_t SIZE; |
51 | typedef 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 |
71 | static int r_alloc_initialized = 0; |
72 | ||
971de7fb | 73 | static 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 | 79 | POINTER (*real_morecore) (long int); |
dcfdbac7 | 80 | |
abe9ff32 | 81 | /* The break value, as seen by malloc. */ |
dcfdbac7 JB |
82 | static 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 |
86 | static POINTER break_value; |
87 | ||
7516b7d5 RS |
88 | /* This is the size of a page. We round memory requests to this boundary. */ |
89 | static 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 |
93 | static 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 | 102 | |
5e617bc2 | 103 | #define MEM_ALIGN sizeof (double) |
e429caa2 KH |
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 | 111 | extern 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 |
135 | typedef 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. */ | |
159 | static struct heap heap_base; | |
160 | ||
161 | /* Head and tail of the list of heaps. */ | |
e429caa2 KH |
162 | static 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 |
174 | typedef 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 |
189 | static bloc_ptr first_bloc, last_bloc; |
190 | ||
49f82b3d RS |
191 | static int use_relocatable_buffers; |
192 | ||
193 | /* If >0, no relocation whatsoever takes place. */ | |
194 | static 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 | ||
201 | static heap_ptr | |
971de7fb | 202 | find_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 | |
0d26e0b6 | 222 | to the heap list. |
abe9ff32 | 223 | |
0d26e0b6 JB |
224 | obtain does not try to keep track of whether space is in use or not |
225 | in use. It just returns the address of SIZE bytes that fall within a | |
226 | single heap. If you call obtain twice in a row with the same arguments, | |
227 | you typically get the same value. It's the caller's responsibility to | |
228 | keep 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 | 233 | static POINTER |
971de7fb | 234 | obtain (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 | 314 | static void |
971de7fb | 315 | relinquish (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 | ||
373 | long | |
971de7fb | 374 | r_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 | |
384 | static bloc_ptr | |
971de7fb | 385 | find_bloc (POINTER *ptr) |
dcfdbac7 JB |
386 | { |
387 | register bloc_ptr p = first_bloc; | |
388 | ||
389 | while (p != NIL_BLOC) | |
390 | { | |
747d9d14 | 391 | /* Consistency check. Don't return inconsistent blocs. |
0d26e0b6 | 392 | Don't abort here, as callers might be expecting this, but |
747d9d14 JR |
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 | |
409 | static bloc_ptr | |
971de7fb | 410 | get_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 | 464 | static int |
971de7fb | 465 | relocate_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) |
5e617bc2 | 471 | abort (); |
49f82b3d | 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 | ||
523 | static void | |
971de7fb | 524 | update_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 | 585 | static int |
971de7fb | 586 | resize_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) |
5e617bc2 | 595 | abort (); |
49f82b3d | 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 | { |
78cef877 EZ |
639 | if (b->new_data != b->data) |
640 | memmove (b->new_data, b->data, b->size); | |
49f82b3d RS |
641 | *b->variable = b->data = b->new_data; |
642 | } | |
643 | } | |
644 | if (!bloc->variable) | |
645 | { | |
646 | bloc->size = 0; | |
647 | bloc->data = bloc->new_data; | |
648 | } | |
649 | else | |
650 | { | |
78cef877 EZ |
651 | if (bloc->new_data != bloc->data) |
652 | memmove (bloc->new_data, bloc->data, old_size); | |
3ce2f8ac | 653 | memset ((char *) bloc->new_data + old_size, 0, size - old_size); |
49f82b3d | 654 | *bloc->variable = bloc->data = bloc->new_data; |
e429caa2 | 655 | } |
e429caa2 KH |
656 | } |
657 | else | |
dcfdbac7 | 658 | { |
ad3bb3d2 JB |
659 | for (b = bloc; b != NIL_BLOC; b = b->next) |
660 | { | |
49f82b3d RS |
661 | if (!b->variable) |
662 | { | |
663 | b->size = 0; | |
664 | b->data = b->new_data; | |
177c0ea7 JB |
665 | } |
666 | else | |
49f82b3d | 667 | { |
78cef877 EZ |
668 | if (b->new_data != b->data) |
669 | memmove (b->new_data, b->data, b->size); | |
49f82b3d RS |
670 | *b->variable = b->data = b->new_data; |
671 | } | |
ad3bb3d2 | 672 | } |
ad3bb3d2 | 673 | } |
dcfdbac7 | 674 | |
47f13333 | 675 | update_heap_bloc_correspondence (bloc, heap); |
abe9ff32 | 676 | |
91a211b5 GM |
677 | break_value = (last_bloc ? (char *) last_bloc->data + last_bloc->size |
678 | : (char *) first_heap->bloc_start); | |
e429caa2 KH |
679 | return 1; |
680 | } | |
47f13333 | 681 | \f |
abe9ff32 RS |
682 | /* Free BLOC from the chain of blocs, relocating any blocs above it. |
683 | This may return space to the system. */ | |
dcfdbac7 JB |
684 | |
685 | static void | |
971de7fb | 686 | free_bloc (bloc_ptr bloc) |
dcfdbac7 | 687 | { |
47f13333 RS |
688 | heap_ptr heap = bloc->heap; |
689 | ||
49f82b3d RS |
690 | if (r_alloc_freeze_level) |
691 | { | |
692 | bloc->variable = (POINTER *) NIL; | |
693 | return; | |
694 | } | |
177c0ea7 | 695 | |
e429caa2 KH |
696 | resize_bloc (bloc, 0); |
697 | ||
dcfdbac7 JB |
698 | if (bloc == first_bloc && bloc == last_bloc) |
699 | { | |
700 | first_bloc = last_bloc = NIL_BLOC; | |
701 | } | |
702 | else if (bloc == last_bloc) | |
703 | { | |
704 | last_bloc = bloc->prev; | |
705 | last_bloc->next = NIL_BLOC; | |
706 | } | |
707 | else if (bloc == first_bloc) | |
708 | { | |
709 | first_bloc = bloc->next; | |
710 | first_bloc->prev = NIL_BLOC; | |
dcfdbac7 JB |
711 | } |
712 | else | |
713 | { | |
714 | bloc->next->prev = bloc->prev; | |
715 | bloc->prev->next = bloc->next; | |
dcfdbac7 JB |
716 | } |
717 | ||
47f13333 RS |
718 | /* Update the records of which blocs are in HEAP. */ |
719 | if (heap->first_bloc == bloc) | |
720 | { | |
d5179acc | 721 | if (bloc->next != 0 && bloc->next->heap == heap) |
47f13333 RS |
722 | heap->first_bloc = bloc->next; |
723 | else | |
724 | heap->first_bloc = heap->last_bloc = NIL_BLOC; | |
725 | } | |
726 | if (heap->last_bloc == bloc) | |
727 | { | |
d5179acc | 728 | if (bloc->prev != 0 && bloc->prev->heap == heap) |
47f13333 RS |
729 | heap->last_bloc = bloc->prev; |
730 | else | |
731 | heap->first_bloc = heap->last_bloc = NIL_BLOC; | |
732 | } | |
733 | ||
e429caa2 | 734 | relinquish (); |
dcfdbac7 JB |
735 | free (bloc); |
736 | } | |
737 | \f | |
956ace37 JB |
738 | /* Interface routines. */ |
739 | ||
98b7fe02 | 740 | /* Obtain SIZE bytes of storage from the free pool, or the system, as |
2c46d29f | 741 | necessary. If relocatable blocs are in use, this means relocating |
98b7fe02 JB |
742 | them. This function gets plugged into the GNU malloc's __morecore |
743 | hook. | |
744 | ||
7516b7d5 RS |
745 | We provide hysteresis, never relocating by less than extra_bytes. |
746 | ||
98b7fe02 JB |
747 | If we're out of memory, we should return zero, to imitate the other |
748 | __morecore hook values - in particular, __default_morecore in the | |
749 | GNU malloc package. */ | |
dcfdbac7 | 750 | |
177c0ea7 | 751 | POINTER |
971de7fb | 752 | r_alloc_sbrk (long int size) |
dcfdbac7 | 753 | { |
e429caa2 KH |
754 | register bloc_ptr b; |
755 | POINTER address; | |
dcfdbac7 | 756 | |
44d3dec0 RS |
757 | if (! r_alloc_initialized) |
758 | r_alloc_init (); | |
759 | ||
dcfdbac7 | 760 | if (! use_relocatable_buffers) |
bbc60227 | 761 | return (*real_morecore) (size); |
dcfdbac7 | 762 | |
e429caa2 KH |
763 | if (size == 0) |
764 | return virtual_break_value; | |
7516b7d5 | 765 | |
e429caa2 | 766 | if (size > 0) |
dcfdbac7 | 767 | { |
abe9ff32 RS |
768 | /* Allocate a page-aligned space. GNU malloc would reclaim an |
769 | extra space if we passed an unaligned one. But we could | |
8e6208c5 | 770 | not always find a space which is contiguous to the previous. */ |
e429caa2 KH |
771 | POINTER new_bloc_start; |
772 | heap_ptr h = first_heap; | |
abe9ff32 | 773 | SIZE get = ROUNDUP (size); |
7516b7d5 | 774 | |
abe9ff32 | 775 | address = (POINTER) ROUNDUP (virtual_break_value); |
e429caa2 | 776 | |
abe9ff32 RS |
777 | /* Search the list upward for a heap which is large enough. */ |
778 | while ((char *) h->end < (char *) MEM_ROUNDUP ((char *)address + get)) | |
e429caa2 KH |
779 | { |
780 | h = h->next; | |
781 | if (h == NIL_HEAP) | |
782 | break; | |
abe9ff32 | 783 | address = (POINTER) ROUNDUP (h->start); |
e429caa2 KH |
784 | } |
785 | ||
abe9ff32 | 786 | /* If not found, obtain more space. */ |
e429caa2 KH |
787 | if (h == NIL_HEAP) |
788 | { | |
789 | get += extra_bytes + page_size; | |
790 | ||
49f82b3d | 791 | if (! obtain (address, get)) |
e429caa2 | 792 | return 0; |
98b7fe02 | 793 | |
e429caa2 | 794 | if (first_heap == last_heap) |
abe9ff32 | 795 | address = (POINTER) ROUNDUP (virtual_break_value); |
e429caa2 | 796 | else |
abe9ff32 | 797 | address = (POINTER) ROUNDUP (last_heap->start); |
e429caa2 KH |
798 | h = last_heap; |
799 | } | |
800 | ||
abe9ff32 | 801 | new_bloc_start = (POINTER) MEM_ROUNDUP ((char *)address + get); |
e429caa2 KH |
802 | |
803 | if (first_heap->bloc_start < new_bloc_start) | |
804 | { | |
49f82b3d | 805 | /* This is no clean solution - no idea how to do it better. */ |
177c0ea7 | 806 | if (r_alloc_freeze_level) |
49f82b3d RS |
807 | return NIL; |
808 | ||
809 | /* There is a bug here: if the above obtain call succeeded, but the | |
810 | relocate_blocs call below does not succeed, we need to free | |
811 | the memory that we got with obtain. */ | |
812 | ||
abe9ff32 | 813 | /* Move all blocs upward. */ |
49f82b3d | 814 | if (! relocate_blocs (first_bloc, h, new_bloc_start)) |
e429caa2 KH |
815 | return 0; |
816 | ||
817 | /* Note that (POINTER)(h+1) <= new_bloc_start since | |
818 | get >= page_size, so the following does not destroy the heap | |
abe9ff32 | 819 | header. */ |
e429caa2 KH |
820 | for (b = last_bloc; b != NIL_BLOC; b = b->prev) |
821 | { | |
78cef877 EZ |
822 | if (b->new_data != b->data) |
823 | memmove (b->new_data, b->data, b->size); | |
e429caa2 KH |
824 | *b->variable = b->data = b->new_data; |
825 | } | |
826 | ||
827 | h->bloc_start = new_bloc_start; | |
abe9ff32 | 828 | |
47f13333 | 829 | update_heap_bloc_correspondence (first_bloc, h); |
e429caa2 | 830 | } |
e429caa2 KH |
831 | if (h != first_heap) |
832 | { | |
833 | /* Give up managing heaps below the one the new | |
abe9ff32 | 834 | virtual_break_value points to. */ |
e429caa2 KH |
835 | first_heap->prev = NIL_HEAP; |
836 | first_heap->next = h->next; | |
837 | first_heap->start = h->start; | |
838 | first_heap->end = h->end; | |
abe9ff32 | 839 | first_heap->free = h->free; |
47f13333 RS |
840 | first_heap->first_bloc = h->first_bloc; |
841 | first_heap->last_bloc = h->last_bloc; | |
e429caa2 KH |
842 | first_heap->bloc_start = h->bloc_start; |
843 | ||
844 | if (first_heap->next) | |
845 | first_heap->next->prev = first_heap; | |
846 | else | |
847 | last_heap = first_heap; | |
848 | } | |
849 | ||
72af86bd | 850 | memset (address, 0, size); |
dcfdbac7 | 851 | } |
e429caa2 | 852 | else /* size < 0 */ |
dcfdbac7 | 853 | { |
e429caa2 KH |
854 | SIZE excess = (char *)first_heap->bloc_start |
855 | - ((char *)virtual_break_value + size); | |
856 | ||
857 | address = virtual_break_value; | |
858 | ||
859 | if (r_alloc_freeze_level == 0 && excess > 2 * extra_bytes) | |
860 | { | |
861 | excess -= extra_bytes; | |
862 | first_heap->bloc_start | |
47f13333 | 863 | = (POINTER) MEM_ROUNDUP ((char *)first_heap->bloc_start - excess); |
e429caa2 | 864 | |
abe9ff32 | 865 | relocate_blocs (first_bloc, first_heap, first_heap->bloc_start); |
7516b7d5 | 866 | |
e429caa2 KH |
867 | for (b = first_bloc; b != NIL_BLOC; b = b->next) |
868 | { | |
78cef877 EZ |
869 | if (b->new_data != b->data) |
870 | memmove (b->new_data, b->data, b->size); | |
e429caa2 KH |
871 | *b->variable = b->data = b->new_data; |
872 | } | |
873 | } | |
874 | ||
875 | if ((char *)virtual_break_value + size < (char *)first_heap->start) | |
876 | { | |
877 | /* We found an additional space below the first heap */ | |
878 | first_heap->start = (POINTER) ((char *)virtual_break_value + size); | |
879 | } | |
dcfdbac7 JB |
880 | } |
881 | ||
e429caa2 | 882 | virtual_break_value = (POINTER) ((char *)address + size); |
47f13333 | 883 | break_value = (last_bloc |
91a211b5 GM |
884 | ? (char *) last_bloc->data + last_bloc->size |
885 | : (char *) first_heap->bloc_start); | |
e429caa2 | 886 | if (size < 0) |
abe9ff32 | 887 | relinquish (); |
7516b7d5 | 888 | |
e429caa2 | 889 | return address; |
dcfdbac7 JB |
890 | } |
891 | ||
0a58f946 | 892 | |
dcfdbac7 JB |
893 | /* Allocate a relocatable bloc of storage of size SIZE. A pointer to |
894 | the data is returned in *PTR. PTR is thus the address of some variable | |
98b7fe02 JB |
895 | which will use the data area. |
896 | ||
49f82b3d | 897 | The allocation of 0 bytes is valid. |
f96f2c5b JB |
898 | In case r_alloc_freeze_level is set, a best fit of unused blocs could be |
899 | done before allocating a new area. Not yet done. | |
49f82b3d | 900 | |
98b7fe02 JB |
901 | If we can't allocate the necessary memory, set *PTR to zero, and |
902 | return zero. */ | |
dcfdbac7 JB |
903 | |
904 | POINTER | |
971de7fb | 905 | r_alloc (POINTER *ptr, SIZE size) |
dcfdbac7 JB |
906 | { |
907 | register bloc_ptr new_bloc; | |
908 | ||
2c46d29f RS |
909 | if (! r_alloc_initialized) |
910 | r_alloc_init (); | |
911 | ||
abe9ff32 | 912 | new_bloc = get_bloc (MEM_ROUNDUP (size)); |
98b7fe02 JB |
913 | if (new_bloc) |
914 | { | |
915 | new_bloc->variable = ptr; | |
916 | *ptr = new_bloc->data; | |
917 | } | |
918 | else | |
919 | *ptr = 0; | |
dcfdbac7 JB |
920 | |
921 | return *ptr; | |
922 | } | |
923 | ||
2c46d29f RS |
924 | /* Free a bloc of relocatable storage whose data is pointed to by PTR. |
925 | Store 0 in *PTR to show there's no block allocated. */ | |
dcfdbac7 JB |
926 | |
927 | void | |
971de7fb | 928 | r_alloc_free (register POINTER *ptr) |
dcfdbac7 JB |
929 | { |
930 | register bloc_ptr dead_bloc; | |
931 | ||
44d3dec0 RS |
932 | if (! r_alloc_initialized) |
933 | r_alloc_init (); | |
934 | ||
dcfdbac7 JB |
935 | dead_bloc = find_bloc (ptr); |
936 | if (dead_bloc == NIL_BLOC) | |
747d9d14 | 937 | abort (); /* Double free? PTR not originally used to allocate? */ |
dcfdbac7 JB |
938 | |
939 | free_bloc (dead_bloc); | |
2c46d29f | 940 | *ptr = 0; |
719b242f | 941 | |
d5179acc | 942 | #ifdef emacs |
719b242f | 943 | refill_memory_reserve (); |
d5179acc | 944 | #endif |
dcfdbac7 JB |
945 | } |
946 | ||
16a5c729 | 947 | /* Given a pointer at address PTR to relocatable data, resize it to SIZE. |
98b7fe02 JB |
948 | Do this by shifting all blocks above this one up in memory, unless |
949 | SIZE is less than or equal to the current bloc size, in which case | |
950 | do nothing. | |
dcfdbac7 | 951 | |
f96f2c5b | 952 | In case r_alloc_freeze_level is set, a new bloc is allocated, and the |
8e6208c5 | 953 | memory copied to it. Not very efficient. We could traverse the |
49f82b3d RS |
954 | bloc_list for a best fit of free blocs first. |
955 | ||
98b7fe02 JB |
956 | Change *PTR to reflect the new bloc, and return this value. |
957 | ||
958 | If more memory cannot be allocated, then leave *PTR unchanged, and | |
959 | return zero. */ | |
dcfdbac7 JB |
960 | |
961 | POINTER | |
971de7fb | 962 | r_re_alloc (POINTER *ptr, SIZE size) |
dcfdbac7 | 963 | { |
16a5c729 | 964 | register bloc_ptr bloc; |
dcfdbac7 | 965 | |
44d3dec0 RS |
966 | if (! r_alloc_initialized) |
967 | r_alloc_init (); | |
968 | ||
49f82b3d RS |
969 | if (!*ptr) |
970 | return r_alloc (ptr, size); | |
177c0ea7 | 971 | if (!size) |
49f82b3d RS |
972 | { |
973 | r_alloc_free (ptr); | |
974 | return r_alloc (ptr, 0); | |
975 | } | |
976 | ||
16a5c729 JB |
977 | bloc = find_bloc (ptr); |
978 | if (bloc == NIL_BLOC) | |
747d9d14 | 979 | abort (); /* Already freed? PTR not originally used to allocate? */ |
dcfdbac7 | 980 | |
177c0ea7 | 981 | if (size < bloc->size) |
49f82b3d RS |
982 | { |
983 | /* Wouldn't it be useful to actually resize the bloc here? */ | |
984 | /* I think so too, but not if it's too expensive... */ | |
177c0ea7 JB |
985 | if ((bloc->size - MEM_ROUNDUP (size) >= page_size) |
986 | && r_alloc_freeze_level == 0) | |
49f82b3d RS |
987 | { |
988 | resize_bloc (bloc, MEM_ROUNDUP (size)); | |
989 | /* Never mind if this fails, just do nothing... */ | |
990 | /* It *should* be infallible! */ | |
991 | } | |
992 | } | |
993 | else if (size > bloc->size) | |
994 | { | |
995 | if (r_alloc_freeze_level) | |
996 | { | |
997 | bloc_ptr new_bloc; | |
998 | new_bloc = get_bloc (MEM_ROUNDUP (size)); | |
999 | if (new_bloc) | |
1000 | { | |
1001 | new_bloc->variable = ptr; | |
1002 | *ptr = new_bloc->data; | |
1003 | bloc->variable = (POINTER *) NIL; | |
1004 | } | |
1005 | else | |
1006 | return NIL; | |
1007 | } | |
177c0ea7 | 1008 | else |
49f82b3d RS |
1009 | { |
1010 | if (! resize_bloc (bloc, MEM_ROUNDUP (size))) | |
1011 | return NIL; | |
1012 | } | |
1013 | } | |
dcfdbac7 JB |
1014 | return *ptr; |
1015 | } | |
81bd58e8 KH |
1016 | |
1017 | /* Disable relocations, after making room for at least SIZE bytes | |
1018 | of non-relocatable heap if possible. The relocatable blocs are | |
1019 | guaranteed to hold still until thawed, even if this means that | |
1020 | malloc must return a null pointer. */ | |
abe9ff32 | 1021 | |
81bd58e8 | 1022 | void |
971de7fb | 1023 | r_alloc_freeze (long int size) |
81bd58e8 | 1024 | { |
44d3dec0 RS |
1025 | if (! r_alloc_initialized) |
1026 | r_alloc_init (); | |
1027 | ||
81bd58e8 KH |
1028 | /* If already frozen, we can't make any more room, so don't try. */ |
1029 | if (r_alloc_freeze_level > 0) | |
1030 | size = 0; | |
1031 | /* If we can't get the amount requested, half is better than nothing. */ | |
1032 | while (size > 0 && r_alloc_sbrk (size) == 0) | |
1033 | size /= 2; | |
1034 | ++r_alloc_freeze_level; | |
1035 | if (size > 0) | |
1036 | r_alloc_sbrk (-size); | |
1037 | } | |
1038 | ||
1039 | void | |
971de7fb | 1040 | r_alloc_thaw (void) |
81bd58e8 | 1041 | { |
49f82b3d | 1042 | |
177c0ea7 | 1043 | if (! r_alloc_initialized) |
49f82b3d RS |
1044 | r_alloc_init (); |
1045 | ||
81bd58e8 KH |
1046 | if (--r_alloc_freeze_level < 0) |
1047 | abort (); | |
49f82b3d | 1048 | |
177c0ea7 | 1049 | /* This frees all unused blocs. It is not too inefficient, as the resize |
72af86bd | 1050 | and memcpy is done only once. Afterwards, all unreferenced blocs are |
49f82b3d | 1051 | already shrunk to zero size. */ |
177c0ea7 | 1052 | if (!r_alloc_freeze_level) |
49f82b3d RS |
1053 | { |
1054 | bloc_ptr *b = &first_bloc; | |
177c0ea7 JB |
1055 | while (*b) |
1056 | if (!(*b)->variable) | |
1057 | free_bloc (*b); | |
1058 | else | |
49f82b3d RS |
1059 | b = &(*b)->next; |
1060 | } | |
81bd58e8 | 1061 | } |
49f82b3d | 1062 | |
dec41418 RS |
1063 | |
1064 | #if defined (emacs) && defined (DOUG_LEA_MALLOC) | |
1065 | ||
1066 | /* Reinitialize the morecore hook variables after restarting a dumped | |
1067 | Emacs. This is needed when using Doug Lea's malloc from GNU libc. */ | |
1068 | void | |
971de7fb | 1069 | r_alloc_reinit (void) |
dec41418 RS |
1070 | { |
1071 | /* Only do this if the hook has been reset, so that we don't get an | |
1072 | infinite loop, in case Emacs was linked statically. */ | |
1073 | if (__morecore != r_alloc_sbrk) | |
1074 | { | |
1075 | real_morecore = __morecore; | |
1076 | __morecore = r_alloc_sbrk; | |
1077 | } | |
1078 | } | |
0a58f946 GM |
1079 | |
1080 | #endif /* emacs && DOUG_LEA_MALLOC */ | |
dec41418 | 1081 | |
e429caa2 | 1082 | #ifdef DEBUG |
0a58f946 | 1083 | |
e429caa2 KH |
1084 | #include <assert.h> |
1085 | ||
44d3dec0 | 1086 | void |
268c2c36 | 1087 | r_alloc_check (void) |
e429caa2 | 1088 | { |
6d16dd06 RS |
1089 | int found = 0; |
1090 | heap_ptr h, ph = 0; | |
1091 | bloc_ptr b, pb = 0; | |
1092 | ||
1093 | if (!r_alloc_initialized) | |
1094 | return; | |
1095 | ||
1096 | assert (first_heap); | |
1097 | assert (last_heap->end <= (POINTER) sbrk (0)); | |
1098 | assert ((POINTER) first_heap < first_heap->start); | |
1099 | assert (first_heap->start <= virtual_break_value); | |
1100 | assert (virtual_break_value <= first_heap->end); | |
1101 | ||
1102 | for (h = first_heap; h; h = h->next) | |
1103 | { | |
1104 | assert (h->prev == ph); | |
1105 | assert ((POINTER) ROUNDUP (h->end) == h->end); | |
40f3f04b RS |
1106 | #if 0 /* ??? The code in ralloc.c does not really try to ensure |
1107 | the heap start has any sort of alignment. | |
1108 | Perhaps it should. */ | |
6d16dd06 | 1109 | assert ((POINTER) MEM_ROUNDUP (h->start) == h->start); |
40f3f04b | 1110 | #endif |
6d16dd06 RS |
1111 | assert ((POINTER) MEM_ROUNDUP (h->bloc_start) == h->bloc_start); |
1112 | assert (h->start <= h->bloc_start && h->bloc_start <= h->end); | |
1113 | ||
1114 | if (ph) | |
1115 | { | |
1116 | assert (ph->end < h->start); | |
1117 | assert (h->start <= (POINTER)h && (POINTER)(h+1) <= h->bloc_start); | |
1118 | } | |
1119 | ||
1120 | if (h->bloc_start <= break_value && break_value <= h->end) | |
1121 | found = 1; | |
1122 | ||
1123 | ph = h; | |
1124 | } | |
1125 | ||
1126 | assert (found); | |
1127 | assert (last_heap == ph); | |
1128 | ||
1129 | for (b = first_bloc; b; b = b->next) | |
1130 | { | |
1131 | assert (b->prev == pb); | |
1132 | assert ((POINTER) MEM_ROUNDUP (b->data) == b->data); | |
1133 | assert ((SIZE) MEM_ROUNDUP (b->size) == b->size); | |
1134 | ||
1135 | ph = 0; | |
1136 | for (h = first_heap; h; h = h->next) | |
1137 | { | |
1138 | if (h->bloc_start <= b->data && b->data + b->size <= h->end) | |
1139 | break; | |
1140 | ph = h; | |
1141 | } | |
1142 | ||
1143 | assert (h); | |
1144 | ||
1145 | if (pb && pb->data + pb->size != b->data) | |
1146 | { | |
1147 | assert (ph && b->data == h->bloc_start); | |
1148 | while (ph) | |
1149 | { | |
1150 | if (ph->bloc_start <= pb->data | |
1151 | && pb->data + pb->size <= ph->end) | |
1152 | { | |
1153 | assert (pb->data + pb->size + b->size > ph->end); | |
1154 | break; | |
1155 | } | |
1156 | else | |
1157 | { | |
1158 | assert (ph->bloc_start + b->size > ph->end); | |
1159 | } | |
1160 | ph = ph->prev; | |
1161 | } | |
1162 | } | |
1163 | pb = b; | |
1164 | } | |
1165 | ||
1166 | assert (last_bloc == pb); | |
1167 | ||
1168 | if (last_bloc) | |
1169 | assert (last_bloc->data + last_bloc->size == break_value); | |
1170 | else | |
1171 | assert (first_heap->bloc_start == break_value); | |
e429caa2 | 1172 | } |
0a58f946 | 1173 | |
e429caa2 | 1174 | #endif /* DEBUG */ |
0a58f946 | 1175 | |
baae5c2d JR |
1176 | /* Update the internal record of which variable points to some data to NEW. |
1177 | Used by buffer-swap-text in Emacs to restore consistency after it | |
1178 | swaps the buffer text between two buffer objects. The OLD pointer | |
1179 | is checked to ensure that memory corruption does not occur due to | |
1180 | misuse. */ | |
1181 | void | |
971de7fb | 1182 | r_alloc_reset_variable (POINTER *old, POINTER *new) |
baae5c2d JR |
1183 | { |
1184 | bloc_ptr bloc = first_bloc; | |
1185 | ||
1186 | /* Find the bloc that corresponds to the data pointed to by pointer. | |
1187 | find_bloc cannot be used, as it has internal consistency checks | |
0d26e0b6 | 1188 | which fail when the variable needs resetting. */ |
baae5c2d JR |
1189 | while (bloc != NIL_BLOC) |
1190 | { | |
1191 | if (bloc->data == *new) | |
1192 | break; | |
1193 | ||
1194 | bloc = bloc->next; | |
1195 | } | |
1196 | ||
1197 | if (bloc == NIL_BLOC || bloc->variable != old) | |
747d9d14 | 1198 | abort (); /* Already freed? OLD not originally used to allocate? */ |
baae5c2d JR |
1199 | |
1200 | /* Update variable to point to the new location. */ | |
1201 | bloc->variable = new; | |
1202 | } | |
0a58f946 GM |
1203 | |
1204 | \f | |
1205 | /*********************************************************************** | |
1206 | Initialization | |
1207 | ***********************************************************************/ | |
1208 | ||
0a58f946 GM |
1209 | /* Initialize various things for memory allocation. */ |
1210 | ||
1211 | static void | |
971de7fb | 1212 | r_alloc_init (void) |
0a58f946 GM |
1213 | { |
1214 | if (r_alloc_initialized) | |
1215 | return; | |
0a58f946 | 1216 | r_alloc_initialized = 1; |
177c0ea7 | 1217 | |
a2c23c92 DL |
1218 | page_size = PAGE; |
1219 | #ifndef SYSTEM_MALLOC | |
0a58f946 GM |
1220 | real_morecore = __morecore; |
1221 | __morecore = r_alloc_sbrk; | |
1222 | ||
1223 | first_heap = last_heap = &heap_base; | |
1224 | first_heap->next = first_heap->prev = NIL_HEAP; | |
1225 | first_heap->start = first_heap->bloc_start | |
1226 | = virtual_break_value = break_value = (*real_morecore) (0); | |
1227 | if (break_value == NIL) | |
1228 | abort (); | |
1229 | ||
0a58f946 | 1230 | extra_bytes = ROUNDUP (50000); |
a2c23c92 | 1231 | #endif |
0a58f946 GM |
1232 | |
1233 | #ifdef DOUG_LEA_MALLOC | |
1673df2e JD |
1234 | BLOCK_INPUT; |
1235 | mallopt (M_TOP_PAD, 64 * 4096); | |
1236 | UNBLOCK_INPUT; | |
0a58f946 | 1237 | #else |
a2c23c92 | 1238 | #ifndef SYSTEM_MALLOC |
0a58f946 GM |
1239 | /* Give GNU malloc's morecore some hysteresis |
1240 | so that we move all the relocatable blocks much less often. */ | |
1241 | __malloc_extra_blocks = 64; | |
1242 | #endif | |
a2c23c92 | 1243 | #endif |
0a58f946 | 1244 | |
5ad25b24 | 1245 | #ifndef SYSTEM_MALLOC |
0a58f946 GM |
1246 | first_heap->end = (POINTER) ROUNDUP (first_heap->start); |
1247 | ||
1248 | /* The extra call to real_morecore guarantees that the end of the | |
1249 | address space is a multiple of page_size, even if page_size is | |
1250 | not really the page size of the system running the binary in | |
1251 | which page_size is stored. This allows a binary to be built on a | |
1252 | system with one page size and run on a system with a smaller page | |
1253 | size. */ | |
91a211b5 | 1254 | (*real_morecore) ((char *) first_heap->end - (char *) first_heap->start); |
0a58f946 GM |
1255 | |
1256 | /* Clear the rest of the last page; this memory is in our address space | |
1257 | even though it is after the sbrk value. */ | |
1258 | /* Doubly true, with the additional call that explicitly adds the | |
1259 | rest of that page to the address space. */ | |
72af86bd AS |
1260 | memset (first_heap->start, 0, |
1261 | (char *) first_heap->end - (char *) first_heap->start); | |
0a58f946 | 1262 | virtual_break_value = break_value = first_heap->bloc_start = first_heap->end; |
a2c23c92 | 1263 | #endif |
177c0ea7 | 1264 | |
0a58f946 GM |
1265 | use_relocatable_buffers = 1; |
1266 | } |