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