185
|
1 /* Block-relocating memory allocator.
|
0
|
2 Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with GNU Emacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
255
|
19 Boston, MA 02111-1307, USA.
|
|
20
|
|
21 Synched Up with: FSF 20.2 (non-mmap portion only)
|
|
22 */
|
0
|
23
|
|
24 /* NOTES:
|
|
25
|
|
26 Only relocate the blocs necessary for SIZE in r_alloc_sbrk,
|
|
27 rather than all of them. This means allowing for a possible
|
|
28 hole between the first bloc and the end of malloc storage. */
|
|
29
|
|
30 #ifdef emacs
|
|
31
|
|
32 #include <config.h>
|
|
33 #include "lisp.h" /* Needed for VALBITS. */
|
|
34
|
|
35 #undef NULL
|
|
36
|
|
37 /* The important properties of this type are that 1) it's a pointer, and
|
|
38 2) arithmetic on it should work as if the size of the object pointed
|
|
39 to has a size of 1. */
|
|
40 #if 0 /* Arithmetic on void* is a GCC extension. */
|
|
41 #ifdef __STDC__
|
|
42 typedef void *POINTER;
|
|
43 #else
|
|
44 typedef unsigned char *POINTER;
|
|
45 #endif
|
|
46 #endif /* 0 */
|
|
47
|
|
48 /* Unconditionally use unsigned char * for this. */
|
|
49 typedef unsigned char *POINTER;
|
|
50
|
|
51 typedef unsigned long SIZE;
|
|
52
|
255
|
53 #ifdef DOUG_LEA_MALLOC
|
|
54 #define M_TOP_PAD -2
|
|
55 extern int mallopt ();
|
|
56 #endif
|
|
57
|
0
|
58 #include "getpagesize.h"
|
|
59
|
|
60 #include <string.h>
|
|
61
|
|
62 #else /* Not emacs. */
|
|
63
|
|
64 #include <stddef.h>
|
|
65
|
|
66 typedef size_t SIZE;
|
|
67 typedef void *POINTER;
|
|
68
|
|
69 #include <unistd.h>
|
|
70 #include <malloc.h>
|
|
71 #include <string.h>
|
|
72
|
|
73 #endif /* emacs. */
|
|
74
|
|
75 #define safe_bcopy(x, y, z) memmove (y, x, z)
|
|
76
|
|
77 #define NIL ((POINTER) 0)
|
|
78
|
|
79
|
255
|
80 #if !defined(HAVE_MMAP) || defined(DOUG_LEA_MALLOC)
|
0
|
81
|
|
82 /* A flag to indicate whether we have initialized ralloc yet. For
|
|
83 Emacs's sake, please do not make this local to malloc_init; on some
|
|
84 machines, the dumping procedure makes all static variables
|
|
85 read-only. On these machines, the word static is #defined to be
|
|
86 the empty string, meaning that r_alloc_initialized becomes an
|
|
87 automatic variable, and loses its value each time Emacs is started up. */
|
|
88 static int r_alloc_initialized = 0;
|
|
89
|
|
90
|
|
91 /* Declarations for working with the malloc, ralloc, and system breaks. */
|
|
92
|
|
93 /* Function to set the real break value. */
|
|
94 static POINTER (*real_morecore) ();
|
|
95
|
|
96 /* The break value, as seen by malloc (). */
|
|
97 static POINTER virtual_break_value;
|
|
98
|
|
99 /* The break value, viewed by the relocatable blocs. */
|
|
100 static POINTER break_value;
|
|
101
|
|
102 /* The REAL (i.e., page aligned) break value of the process. */
|
|
103 static POINTER page_break_value;
|
|
104
|
|
105 /* This is the size of a page. We round memory requests to this boundary. */
|
|
106 static int page_size;
|
|
107
|
185
|
108 /* Whenever we get memory from the system, get this many extra bytes. This
|
0
|
109 must be a multiple of page_size. */
|
|
110 static int extra_bytes;
|
|
111
|
|
112 /* Macros for rounding. Note that rounding to any value is possible
|
|
113 by changing the definition of PAGE. */
|
|
114 #define PAGE (getpagesize ())
|
|
115 #define ALIGNED(addr) (((unsigned long int) (addr) & (page_size - 1)) == 0)
|
|
116 #define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \
|
|
117 & ~(page_size - 1))
|
|
118 #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
|
255
|
119
|
|
120 #define MEM_ALIGN sizeof(double)
|
|
121 #define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \
|
|
122 & ~(MEM_ALIGN - 1))
|
0
|
123
|
255
|
124 /* Data structures of heaps and blocs. */
|
|
125
|
|
126 /* The relocatable objects, or blocs, and the malloc data
|
|
127 both reside within one or more heaps.
|
|
128 Each heap contains malloc data, running from `start' to `bloc_start',
|
|
129 and relocatable objects, running from `bloc_start' to `free'.
|
|
130
|
|
131 Relocatable objects may relocate within the same heap
|
|
132 or may move into another heap; the heaps themselves may grow
|
|
133 but they never move.
|
|
134
|
|
135 We try to make just one heap and make it larger as necessary.
|
|
136 But sometimes we can't do that, because we can't get contiguous
|
|
137 space to add onto the heap. When that happens, we start a new heap. */
|
|
138
|
|
139 typedef struct heap
|
|
140 {
|
|
141 struct heap *next;
|
|
142 struct heap *prev;
|
|
143 /* Start of memory range of this heap. */
|
|
144 POINTER start;
|
|
145 /* End of memory range of this heap. */
|
|
146 POINTER end;
|
|
147 /* Start of relocatable data in this heap. */
|
|
148 POINTER bloc_start;
|
|
149 /* Start of unused space in this heap. */
|
|
150 POINTER free;
|
|
151 /* First bloc in this heap. */
|
|
152 struct bp *first_bloc;
|
|
153 /* Last bloc in this heap. */
|
|
154 struct bp *last_bloc;
|
|
155 } *heap_ptr;
|
|
156
|
|
157 #define NIL_HEAP ((heap_ptr) 0)
|
|
158 #define HEAP_PTR_SIZE (sizeof (struct heap))
|
|
159
|
|
160 /* This is the first heap object.
|
|
161 If we need additional heap objects, each one resides at the beginning of
|
|
162 the space it covers. */
|
|
163 static struct heap heap_base;
|
|
164
|
|
165 /* Head and tail of the list of heaps. */
|
|
166 static heap_ptr first_heap, last_heap;
|
|
167
|
|
168 /* These structures are allocated in the malloc arena.
|
|
169 The linked list is kept in order of increasing '.data' members.
|
|
170 The data blocks abut each other; if b->next is non-nil, then
|
|
171 b->data + b->size == b->next->data.
|
|
172
|
|
173 An element with variable==NIL denotes a freed block, which has not yet
|
|
174 been collected. They may only appear while r_alloc_freeze > 0, and will be
|
|
175 freed when the arena is thawed. Currently, these blocs are not reusable,
|
|
176 while the arena is frozen. Very inefficient. */
|
|
177
|
|
178 typedef struct bp
|
|
179 {
|
|
180 struct bp *next;
|
|
181 struct bp *prev;
|
|
182 POINTER *variable;
|
|
183 POINTER data;
|
|
184 SIZE size;
|
|
185 POINTER new_data; /* temporarily used for relocation */
|
|
186 struct heap *heap; /* Heap this bloc is in. */
|
|
187 } *bloc_ptr;
|
|
188
|
|
189 #define NIL_BLOC ((bloc_ptr) 0)
|
|
190 #define BLOC_PTR_SIZE (sizeof (struct bp))
|
|
191
|
|
192 /* Head and tail of the list of relocatable blocs. */
|
|
193 static bloc_ptr first_bloc, last_bloc;
|
|
194
|
|
195 static int use_relocatable_buffers;
|
|
196
|
|
197 /* If >0, no relocation whatsoever takes place. */
|
|
198 static int r_alloc_freeze_level;
|
0
|
199
|
|
200 /* Obtain SIZE bytes of space. If enough space is not presently available
|
|
201 in our process reserve, (i.e., (page_break_value - break_value)),
|
|
202 this means getting more page-aligned space from the system.
|
|
203
|
|
204 Return non-zero if all went well, or zero if we couldn't allocate
|
|
205 the memory. */
|
255
|
206
|
|
207 /* Functions to get and return memory from the system. */
|
|
208
|
|
209 /* Find the heap that ADDRESS falls within. */
|
|
210
|
|
211 static heap_ptr
|
|
212 find_heap (address)
|
|
213 POINTER address;
|
0
|
214 {
|
255
|
215 heap_ptr heap;
|
|
216
|
|
217 for (heap = last_heap; heap; heap = heap->prev)
|
|
218 {
|
|
219 if (heap->start <= address && address <= heap->end)
|
|
220 return heap;
|
|
221 }
|
|
222
|
|
223 return NIL_HEAP;
|
|
224 }
|
|
225
|
|
226 /* Find SIZE bytes of space in a heap.
|
|
227 Try to get them at ADDRESS (which must fall within some heap's range)
|
|
228 if we can get that many within one heap.
|
|
229
|
|
230 If enough space is not presently available in our reserve, this means
|
|
231 getting more page-aligned space from the system. If the returned space
|
|
232 is not contiguous to the last heap, allocate a new heap, and append it
|
|
233
|
|
234 obtain does not try to keep track of whether space is in use
|
|
235 or not in use. It just returns the address of SIZE bytes that
|
|
236 fall within a single heap. If you call obtain twice in a row
|
|
237 with the same arguments, you typically get the same value.
|
|
238 to the heap list. It's the caller's responsibility to keep
|
|
239 track of what space is in use.
|
|
240
|
|
241 Return the address of the space if all went well, or zero if we couldn't
|
|
242 allocate the memory. */
|
|
243
|
|
244 static POINTER
|
|
245 obtain (POINTER address, SIZE size)
|
|
246 {
|
|
247 heap_ptr heap;
|
|
248 SIZE already_available;
|
|
249
|
|
250 /* Find the heap that ADDRESS falls within. */
|
|
251 for (heap = last_heap; heap; heap = heap->prev)
|
|
252 {
|
|
253 if (heap->start <= address && address <= heap->end)
|
|
254 break;
|
|
255 }
|
0
|
256
|
255
|
257 if (! heap)
|
|
258 abort ();
|
|
259
|
|
260 /* If we can't fit SIZE bytes in that heap,
|
|
261 try successive later heaps. */
|
|
262 while (heap && address + size > heap->end)
|
|
263 {
|
|
264 heap = heap->next;
|
|
265 if (heap == NIL_HEAP)
|
|
266 break;
|
|
267 address = heap->bloc_start;
|
|
268 }
|
|
269
|
|
270 /* If we can't fit them within any existing heap,
|
|
271 get more space. */
|
|
272 if (heap == NIL_HEAP)
|
0
|
273 {
|
255
|
274 POINTER new = (*real_morecore)(0);
|
|
275 SIZE get;
|
|
276
|
|
277 already_available = (char *)last_heap->end - (char *)address;
|
|
278
|
|
279 if (new != last_heap->end)
|
|
280 {
|
|
281 /* Someone else called sbrk. Make a new heap. */
|
|
282
|
|
283 heap_ptr new_heap = (heap_ptr) MEM_ROUNDUP (new);
|
|
284 POINTER bloc_start = (POINTER) MEM_ROUNDUP ((POINTER)(new_heap + 1));
|
|
285
|
|
286 if ((*real_morecore) (bloc_start - new) != new)
|
|
287 return 0;
|
0
|
288
|
255
|
289 new_heap->start = new;
|
|
290 new_heap->end = bloc_start;
|
|
291 new_heap->bloc_start = bloc_start;
|
|
292 new_heap->free = bloc_start;
|
|
293 new_heap->next = NIL_HEAP;
|
|
294 new_heap->prev = last_heap;
|
|
295 new_heap->first_bloc = NIL_BLOC;
|
|
296 new_heap->last_bloc = NIL_BLOC;
|
|
297 last_heap->next = new_heap;
|
|
298 last_heap = new_heap;
|
|
299
|
|
300 address = bloc_start;
|
|
301 already_available = 0;
|
|
302 }
|
|
303
|
|
304 /* Add space to the last heap (which we may have just created).
|
|
305 Get some extra, so we can come here less often. */
|
|
306
|
|
307 get = size + extra_bytes - already_available;
|
|
308 get = (char *) ROUNDUP ((char *)last_heap->end + get)
|
|
309 - (char *) last_heap->end;
|
|
310
|
|
311 if ((*real_morecore) (get) != last_heap->end)
|
0
|
312 return 0;
|
|
313
|
255
|
314 last_heap->end += get;
|
0
|
315 }
|
|
316
|
255
|
317 return address;
|
0
|
318 }
|
|
319
|
255
|
320 #if 0
|
0
|
321 /* Obtain SIZE bytes of space and return a pointer to the new area.
|
|
322 If we could not allocate the space, return zero. */
|
|
323
|
|
324 static POINTER
|
|
325 get_more_space (SIZE size)
|
|
326 {
|
|
327 POINTER ptr = break_value;
|
|
328 if (obtain (size))
|
|
329 return ptr;
|
|
330 else
|
|
331 return 0;
|
|
332 }
|
255
|
333 #endif
|
0
|
334
|
|
335 /* Note that SIZE bytes of space have been relinquished by the process.
|
|
336 If SIZE is more than a page, return the space to the system. */
|
|
337
|
|
338 static void
|
255
|
339 relinquish ()
|
0
|
340 {
|
255
|
341 register heap_ptr h;
|
|
342 int excess = 0;
|
|
343
|
|
344 /* Add the amount of space beyond break_value
|
|
345 in all heaps which have extend beyond break_value at all. */
|
0
|
346
|
255
|
347 for (h = last_heap; h && break_value < h->end; h = h->prev)
|
|
348 {
|
|
349 excess += (char *) h->end - (char *) ((break_value < h->bloc_start)
|
|
350 ? h->bloc_start : break_value);
|
|
351 }
|
185
|
352
|
255
|
353 if (excess > extra_bytes * 2 && (*real_morecore) (0) == last_heap->end)
|
0
|
354 {
|
|
355 /* Keep extra_bytes worth of empty space.
|
|
356 And don't free anything unless we can free at least extra_bytes. */
|
255
|
357 excess -= extra_bytes;
|
|
358
|
|
359 if ((char *)last_heap->end - (char *)last_heap->bloc_start <= excess)
|
|
360 {
|
|
361 /* This heap should have no blocs in it. */
|
|
362 if (last_heap->first_bloc != NIL_BLOC
|
|
363 || last_heap->last_bloc != NIL_BLOC)
|
|
364 abort ();
|
|
365
|
|
366 /* Return the last heap, with its header, to the system. */
|
|
367 excess = (char *)last_heap->end - (char *)last_heap->start;
|
|
368 last_heap = last_heap->prev;
|
|
369 last_heap->next = NIL_HEAP;
|
|
370 }
|
|
371 else
|
|
372 {
|
|
373 excess = (char *) last_heap->end
|
|
374 - (char *) ROUNDUP ((char *)last_heap->end - excess);
|
|
375 last_heap->end -= excess;
|
|
376 }
|
|
377
|
|
378 if ((*real_morecore) (- excess) == 0)
|
0
|
379 abort ();
|
|
380 }
|
255
|
381 }
|
0
|
382
|
255
|
383 /* Return the total size in use by relocating allocator,
|
|
384 above where malloc gets space. */
|
|
385
|
|
386 long
|
|
387 r_alloc_size_in_use ()
|
|
388 {
|
|
389 return break_value - virtual_break_value;
|
0
|
390 }
|
|
391
|
|
392 /* The meat - allocating, freeing, and relocating blocs. */
|
|
393
|
|
394
|
|
395 /* Find the bloc referenced by the address in PTR. Returns a pointer
|
|
396 to that block. */
|
|
397
|
|
398 static bloc_ptr
|
|
399 find_bloc (POINTER *ptr)
|
|
400 {
|
255
|
401 register bloc_ptr p = first_bloc;
|
0
|
402
|
|
403 while (p != NIL_BLOC)
|
|
404 {
|
|
405 if (p->variable == ptr && p->data == *ptr)
|
|
406 return p;
|
|
407
|
|
408 p = p->next;
|
|
409 }
|
|
410
|
|
411 return p;
|
|
412 }
|
|
413
|
|
414 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
|
|
415 Returns a pointer to the new bloc, or zero if we couldn't allocate
|
|
416 memory for the new block. */
|
|
417
|
|
418 static bloc_ptr
|
|
419 get_bloc (SIZE size)
|
|
420 {
|
255
|
421 register bloc_ptr new_bloc;
|
|
422 register heap_ptr heap;
|
0
|
423
|
|
424 if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
|
255
|
425 || ! (new_bloc->data = obtain (break_value, size)))
|
0
|
426 {
|
|
427 if (new_bloc)
|
|
428 free (new_bloc);
|
|
429
|
|
430 return 0;
|
|
431 }
|
|
432
|
255
|
433 break_value = new_bloc->data + size;
|
|
434
|
0
|
435 new_bloc->size = size;
|
|
436 new_bloc->next = NIL_BLOC;
|
|
437 new_bloc->variable = (POINTER *) NIL;
|
255
|
438 new_bloc->new_data = 0;
|
0
|
439
|
255
|
440 /* Record in the heap that this space is in use. */
|
|
441 heap = find_heap (new_bloc->data);
|
|
442 heap->free = break_value;
|
|
443
|
|
444 /* Maintain the correspondence between heaps and blocs. */
|
|
445 new_bloc->heap = heap;
|
|
446 heap->last_bloc = new_bloc;
|
|
447 if (heap->first_bloc == NIL_BLOC)
|
|
448 heap->first_bloc = new_bloc;
|
|
449
|
|
450 /* Put this bloc on the doubly-linked list of blocs. */
|
0
|
451 if (first_bloc)
|
|
452 {
|
|
453 new_bloc->prev = last_bloc;
|
|
454 last_bloc->next = new_bloc;
|
|
455 last_bloc = new_bloc;
|
|
456 }
|
|
457 else
|
|
458 {
|
|
459 first_bloc = last_bloc = new_bloc;
|
|
460 new_bloc->prev = NIL_BLOC;
|
|
461 }
|
|
462
|
|
463 return new_bloc;
|
|
464 }
|
|
465
|
255
|
466 /* Calculate new locations of blocs in the list beginning with BLOC,
|
|
467 relocating it to start at ADDRESS, in heap HEAP. If enough space is
|
|
468 not presently available in our reserve, call obtain for
|
|
469 more space.
|
|
470
|
|
471 Store the new location of each bloc in its new_data field.
|
|
472 Do not touch the contents of blocs or break_value. */
|
|
473
|
|
474 static int
|
|
475 relocate_blocs (bloc, heap, address)
|
|
476 bloc_ptr bloc;
|
|
477 heap_ptr heap;
|
|
478 POINTER address;
|
|
479 {
|
|
480 register bloc_ptr b = bloc;
|
|
481
|
|
482 /* No need to ever call this if arena is frozen, bug somewhere! */
|
|
483 if (r_alloc_freeze_level)
|
|
484 abort();
|
|
485
|
|
486 while (b)
|
|
487 {
|
|
488 /* If bloc B won't fit within HEAP,
|
|
489 move to the next heap and try again. */
|
|
490 while (heap && address + b->size > heap->end)
|
|
491 {
|
|
492 heap = heap->next;
|
|
493 if (heap == NIL_HEAP)
|
|
494 break;
|
|
495 address = heap->bloc_start;
|
|
496 }
|
|
497
|
|
498 /* If BLOC won't fit in any heap,
|
|
499 get enough new space to hold BLOC and all following blocs. */
|
|
500 if (heap == NIL_HEAP)
|
|
501 {
|
|
502 register bloc_ptr tb = b;
|
|
503 register SIZE s = 0;
|
|
504
|
|
505 /* Add up the size of all the following blocs. */
|
|
506 while (tb != NIL_BLOC)
|
|
507 {
|
|
508 if (tb->variable)
|
|
509 s += tb->size;
|
|
510
|
|
511 tb = tb->next;
|
|
512 }
|
0
|
513
|
255
|
514 /* Get that space. */
|
|
515 address = obtain (address, s);
|
|
516 if (address == 0)
|
|
517 return 0;
|
|
518
|
|
519 heap = last_heap;
|
|
520 }
|
|
521
|
|
522 /* Record the new address of this bloc
|
|
523 and update where the next bloc can start. */
|
|
524 b->new_data = address;
|
|
525 if (b->variable)
|
|
526 address += b->size;
|
|
527 b = b->next;
|
|
528 }
|
|
529
|
|
530 return 1;
|
|
531 }
|
|
532
|
|
533 /* Reorder the bloc BLOC to go before bloc BEFORE in the doubly linked list.
|
|
534 This is necessary if we put the memory of space of BLOC
|
|
535 before that of BEFORE. */
|
0
|
536
|
255
|
537 static void
|
|
538 reorder_bloc (bloc, before)
|
|
539 bloc_ptr bloc, before;
|
|
540 {
|
|
541 bloc_ptr prev, next;
|
|
542
|
|
543 /* Splice BLOC out from where it is. */
|
|
544 prev = bloc->prev;
|
|
545 next = bloc->next;
|
|
546
|
|
547 if (prev)
|
|
548 prev->next = next;
|
|
549 if (next)
|
|
550 next->prev = prev;
|
|
551
|
|
552 /* Splice it in before BEFORE. */
|
|
553 prev = before->prev;
|
|
554
|
|
555 if (prev)
|
|
556 prev->next = bloc;
|
|
557 bloc->prev = prev;
|
|
558
|
|
559 before->prev = bloc;
|
|
560 bloc->next = before;
|
|
561 }
|
|
562
|
|
563 /* Update the records of which heaps contain which blocs, starting
|
|
564 with heap HEAP and bloc BLOC. */
|
0
|
565
|
|
566 static void
|
255
|
567 update_heap_bloc_correspondence (bloc, heap)
|
|
568 bloc_ptr bloc;
|
|
569 heap_ptr heap;
|
0
|
570 {
|
255
|
571 register bloc_ptr b;
|
|
572
|
|
573 /* Initialize HEAP's status to reflect blocs before BLOC. */
|
|
574 if (bloc != NIL_BLOC && bloc->prev != NIL_BLOC && bloc->prev->heap == heap)
|
|
575 {
|
|
576 /* The previous bloc is in HEAP. */
|
|
577 heap->last_bloc = bloc->prev;
|
|
578 heap->free = bloc->prev->data + bloc->prev->size;
|
|
579 }
|
|
580 else
|
|
581 {
|
|
582 /* HEAP contains no blocs before BLOC. */
|
|
583 heap->first_bloc = NIL_BLOC;
|
|
584 heap->last_bloc = NIL_BLOC;
|
|
585 heap->free = heap->bloc_start;
|
|
586 }
|
|
587
|
|
588 /* Advance through blocs one by one. */
|
|
589 for (b = bloc; b != NIL_BLOC; b = b->next)
|
|
590 {
|
|
591 /* Advance through heaps, marking them empty,
|
|
592 till we get to the one that B is in. */
|
|
593 while (heap)
|
|
594 {
|
|
595 if (heap->bloc_start <= b->data && b->data <= heap->end)
|
|
596 break;
|
|
597 heap = heap->next;
|
|
598 /* We know HEAP is not null now,
|
|
599 because there has to be space for bloc B. */
|
|
600 heap->first_bloc = NIL_BLOC;
|
|
601 heap->last_bloc = NIL_BLOC;
|
|
602 heap->free = heap->bloc_start;
|
|
603 }
|
|
604
|
|
605 /* Update HEAP's status for bloc B. */
|
|
606 heap->free = b->data + b->size;
|
|
607 heap->last_bloc = b;
|
|
608 if (heap->first_bloc == NIL_BLOC)
|
|
609 heap->first_bloc = b;
|
|
610
|
|
611 /* Record that B is in HEAP. */
|
|
612 b->heap = heap;
|
|
613 }
|
|
614
|
|
615 /* If there are any remaining heaps and no blocs left,
|
|
616 mark those heaps as empty. */
|
|
617 heap = heap->next;
|
|
618 while (heap)
|
0
|
619 {
|
255
|
620 heap->first_bloc = NIL_BLOC;
|
|
621 heap->last_bloc = NIL_BLOC;
|
|
622 heap->free = heap->bloc_start;
|
|
623 heap = heap->next;
|
|
624 }
|
|
625 }
|
|
626
|
|
627 /* Resize BLOC to SIZE bytes. This relocates the blocs
|
|
628 that come after BLOC in memory. */
|
|
629
|
|
630 static int
|
|
631 resize_bloc (bloc, size)
|
|
632 bloc_ptr bloc;
|
|
633 SIZE size;
|
|
634 {
|
|
635 register bloc_ptr b;
|
|
636 heap_ptr heap;
|
|
637 POINTER address;
|
|
638 SIZE old_size;
|
|
639
|
|
640 /* No need to ever call this if arena is frozen, bug somewhere! */
|
|
641 if (r_alloc_freeze_level)
|
|
642 abort();
|
|
643
|
|
644 if (bloc == NIL_BLOC || size == bloc->size)
|
|
645 return 1;
|
|
646
|
|
647 for (heap = first_heap; heap != NIL_HEAP; heap = heap->next)
|
|
648 {
|
|
649 if (heap->bloc_start <= bloc->data && bloc->data <= heap->end)
|
|
650 break;
|
|
651 }
|
|
652
|
|
653 if (heap == NIL_HEAP)
|
|
654 abort ();
|
|
655
|
|
656 old_size = bloc->size;
|
|
657 bloc->size = size;
|
185
|
658
|
255
|
659 /* Note that bloc could be moved into the previous heap. */
|
|
660 address = (bloc->prev ? bloc->prev->data + bloc->prev->size
|
|
661 : first_heap->bloc_start);
|
|
662 while (heap)
|
|
663 {
|
|
664 if (heap->bloc_start <= address && address <= heap->end)
|
|
665 break;
|
|
666 heap = heap->prev;
|
|
667 }
|
|
668
|
|
669 if (! relocate_blocs (bloc, heap, address))
|
|
670 {
|
|
671 bloc->size = old_size;
|
|
672 return 0;
|
|
673 }
|
|
674
|
|
675 if (size > old_size)
|
|
676 {
|
|
677 for (b = last_bloc; b != bloc; b = b->prev)
|
|
678 {
|
|
679 if (!b->variable)
|
|
680 {
|
|
681 b->size = 0;
|
|
682 b->data = b->new_data;
|
|
683 }
|
|
684 else
|
|
685 {
|
|
686 safe_bcopy (b->data, b->new_data, b->size);
|
|
687 *b->variable = b->data = b->new_data;
|
|
688 }
|
|
689 }
|
|
690 if (!bloc->variable)
|
|
691 {
|
|
692 bloc->size = 0;
|
|
693 bloc->data = bloc->new_data;
|
|
694 }
|
|
695 else
|
|
696 {
|
|
697 safe_bcopy (bloc->data, bloc->new_data, old_size);
|
|
698 memset (bloc->new_data + old_size, 0, size - old_size);
|
|
699 *bloc->variable = bloc->data = bloc->new_data;
|
|
700 }
|
|
701 }
|
|
702 else
|
|
703 {
|
0
|
704 for (b = bloc; b != NIL_BLOC; b = b->next)
|
|
705 {
|
255
|
706 if (!b->variable)
|
|
707 {
|
|
708 b->size = 0;
|
|
709 b->data = b->new_data;
|
|
710 }
|
|
711 else
|
|
712 {
|
|
713 safe_bcopy (b->data, b->new_data, b->size);
|
|
714 *b->variable = b->data = b->new_data;
|
|
715 }
|
0
|
716 }
|
255
|
717 }
|
0
|
718
|
255
|
719 update_heap_bloc_correspondence (bloc, heap);
|
|
720
|
|
721 break_value = (last_bloc ? last_bloc->data + last_bloc->size
|
|
722 : first_heap->bloc_start);
|
|
723 return 1;
|
0
|
724 }
|
255
|
725
|
0
|
726 /* Free BLOC from the chain of blocs, relocating any blocs above it
|
|
727 and returning BLOC->size bytes to the free area. */
|
|
728
|
|
729 static void
|
|
730 free_bloc (bloc_ptr bloc)
|
|
731 {
|
255
|
732 heap_ptr heap = bloc->heap;
|
|
733
|
|
734 if (r_alloc_freeze_level)
|
|
735 {
|
|
736 bloc->variable = (POINTER *) NIL;
|
|
737 return;
|
|
738 }
|
|
739
|
|
740 resize_bloc (bloc, 0);
|
|
741
|
0
|
742 if (bloc == first_bloc && bloc == last_bloc)
|
|
743 {
|
|
744 first_bloc = last_bloc = NIL_BLOC;
|
|
745 }
|
|
746 else if (bloc == last_bloc)
|
|
747 {
|
|
748 last_bloc = bloc->prev;
|
|
749 last_bloc->next = NIL_BLOC;
|
|
750 }
|
|
751 else if (bloc == first_bloc)
|
|
752 {
|
|
753 first_bloc = bloc->next;
|
|
754 first_bloc->prev = NIL_BLOC;
|
|
755 }
|
|
756 else
|
|
757 {
|
|
758 bloc->next->prev = bloc->prev;
|
|
759 bloc->prev->next = bloc->next;
|
|
760 }
|
|
761
|
255
|
762 /* Update the records of which blocs are in HEAP. */
|
|
763 if (heap->first_bloc == bloc)
|
|
764 {
|
|
765 if (bloc->next != 0 && bloc->next->heap == heap)
|
|
766 heap->first_bloc = bloc->next;
|
|
767 else
|
|
768 heap->first_bloc = heap->last_bloc = NIL_BLOC;
|
|
769 }
|
|
770 if (heap->last_bloc == bloc)
|
|
771 {
|
|
772 if (bloc->prev != 0 && bloc->prev->heap == heap)
|
|
773 heap->last_bloc = bloc->prev;
|
|
774 else
|
|
775 heap->first_bloc = heap->last_bloc = NIL_BLOC;
|
|
776 }
|
|
777
|
|
778 relinquish ();
|
0
|
779 free (bloc);
|
|
780 }
|
|
781
|
|
782 /* Interface routines. */
|
|
783
|
|
784 /* Obtain SIZE bytes of storage from the free pool, or the system, as
|
|
785 necessary. If relocatable blocs are in use, this means relocating
|
|
786 them. This function gets plugged into the GNU malloc's __morecore
|
|
787 hook.
|
|
788
|
|
789 We provide hysteresis, never relocating by less than extra_bytes.
|
|
790
|
|
791 If we're out of memory, we should return zero, to imitate the other
|
|
792 __morecore hook values - in particular, __default_morecore in the
|
|
793 GNU malloc package. */
|
|
794
|
185
|
795 POINTER
|
0
|
796 r_alloc_sbrk (long size)
|
|
797 {
|
255
|
798 register bloc_ptr b;
|
|
799 POINTER address;
|
|
800
|
|
801 if (! r_alloc_initialized)
|
|
802 init_ralloc ();
|
0
|
803
|
|
804 if (! use_relocatable_buffers)
|
|
805 return (*real_morecore) (size);
|
|
806
|
255
|
807 if (size == 0)
|
|
808 return virtual_break_value;
|
0
|
809
|
255
|
810 if (size > 0)
|
0
|
811 {
|
255
|
812 /* Allocate a page-aligned space. GNU malloc would reclaim an
|
|
813 extra space if we passed an unaligned one. But we could
|
|
814 not always find a space which is contiguous to the previous. */
|
|
815 POINTER new_bloc_start;
|
|
816 heap_ptr h = first_heap;
|
|
817 SIZE get = ROUNDUP (size);
|
|
818
|
|
819 address = (POINTER) ROUNDUP (virtual_break_value);
|
|
820
|
|
821 /* Search the list upward for a heap which is large enough. */
|
|
822 while ((char *) h->end < (char *) MEM_ROUNDUP ((char *)address + get))
|
|
823 {
|
|
824 h = h->next;
|
|
825 if (h == NIL_HEAP)
|
|
826 break;
|
|
827 address = (POINTER) ROUNDUP (h->start);
|
|
828 }
|
|
829
|
|
830 /* If not found, obtain more space. */
|
|
831 if (h == NIL_HEAP)
|
|
832 {
|
|
833 get += extra_bytes + page_size;
|
0
|
834
|
255
|
835 if (! obtain (address, get))
|
|
836 return 0;
|
|
837
|
|
838 if (first_heap == last_heap)
|
|
839 address = (POINTER) ROUNDUP (virtual_break_value);
|
|
840 else
|
|
841 address = (POINTER) ROUNDUP (last_heap->start);
|
|
842 h = last_heap;
|
|
843 }
|
|
844
|
|
845 new_bloc_start = (POINTER) MEM_ROUNDUP ((char *)address + get);
|
|
846
|
|
847 if (first_heap->bloc_start < new_bloc_start)
|
|
848 {
|
|
849 /* This is no clean solution - no idea how to do it better. */
|
|
850 if (r_alloc_freeze_level)
|
|
851 return NIL;
|
|
852
|
|
853 /* There is a bug here: if the above obtain call succeeded, but the
|
|
854 relocate_blocs call below does not succeed, we need to free
|
|
855 the memory that we got with obtain. */
|
|
856
|
|
857 /* Move all blocs upward. */
|
|
858 if (! relocate_blocs (first_bloc, h, new_bloc_start))
|
|
859 return 0;
|
0
|
860
|
255
|
861 /* Note that (POINTER)(h+1) <= new_bloc_start since
|
|
862 get >= page_size, so the following does not destroy the heap
|
|
863 header. */
|
|
864 for (b = last_bloc; b != NIL_BLOC; b = b->prev)
|
|
865 {
|
|
866 safe_bcopy (b->data, b->new_data, b->size);
|
|
867 *b->variable = b->data = b->new_data;
|
|
868 }
|
|
869
|
|
870 h->bloc_start = new_bloc_start;
|
0
|
871
|
255
|
872 update_heap_bloc_correspondence (first_bloc, h);
|
|
873 }
|
|
874 if (h != first_heap)
|
|
875 {
|
|
876 /* Give up managing heaps below the one the new
|
|
877 virtual_break_value points to. */
|
|
878 first_heap->prev = NIL_HEAP;
|
|
879 first_heap->next = h->next;
|
|
880 first_heap->start = h->start;
|
|
881 first_heap->end = h->end;
|
|
882 first_heap->free = h->free;
|
|
883 first_heap->first_bloc = h->first_bloc;
|
|
884 first_heap->last_bloc = h->last_bloc;
|
|
885 first_heap->bloc_start = h->bloc_start;
|
|
886
|
|
887 if (first_heap->next)
|
|
888 first_heap->next->prev = first_heap;
|
|
889 else
|
|
890 last_heap = first_heap;
|
|
891 }
|
|
892
|
|
893 memset (address, 0, size);
|
0
|
894 }
|
255
|
895 else /* size < 0 */
|
0
|
896 {
|
255
|
897 SIZE excess = (char *)first_heap->bloc_start
|
|
898 - ((char *)virtual_break_value + size);
|
|
899
|
|
900 address = virtual_break_value;
|
|
901
|
|
902 if (r_alloc_freeze_level == 0 && excess > 2 * extra_bytes)
|
|
903 {
|
|
904 excess -= extra_bytes;
|
|
905 first_heap->bloc_start
|
|
906 = (POINTER) MEM_ROUNDUP ((char *)first_heap->bloc_start - excess);
|
|
907
|
|
908 relocate_blocs (first_bloc, first_heap, first_heap->bloc_start);
|
0
|
909
|
255
|
910 for (b = first_bloc; b != NIL_BLOC; b = b->next)
|
|
911 {
|
|
912 safe_bcopy (b->data, b->new_data, b->size);
|
|
913 *b->variable = b->data = b->new_data;
|
|
914 }
|
|
915 }
|
|
916
|
|
917 if ((char *)virtual_break_value + size < (char *)first_heap->start)
|
|
918 {
|
|
919 /* We found an additional space below the first heap */
|
|
920 first_heap->start = (POINTER) ((char *)virtual_break_value + size);
|
|
921 }
|
0
|
922 }
|
|
923
|
255
|
924 virtual_break_value = (POINTER) ((char *)address + size);
|
|
925 break_value = (last_bloc
|
|
926 ? last_bloc->data + last_bloc->size
|
|
927 : first_heap->bloc_start);
|
|
928 if (size < 0)
|
|
929 relinquish ();
|
0
|
930
|
255
|
931 return address;
|
0
|
932 }
|
|
933
|
|
934 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
|
|
935 the data is returned in *PTR. PTR is thus the address of some variable
|
|
936 which will use the data area.
|
|
937
|
255
|
938 The allocation of 0 bytes is valid.
|
|
939 In case r_alloc_freeze is set, a best fit of unused blocs could be done
|
|
940 before allocating a new area. Not yet done.
|
|
941
|
0
|
942 If we can't allocate the necessary memory, set *PTR to zero, and
|
|
943 return zero. */
|
|
944
|
|
945 POINTER
|
|
946 r_alloc (POINTER *ptr, SIZE size)
|
|
947 {
|
|
948 bloc_ptr new_bloc;
|
|
949
|
|
950 if (! r_alloc_initialized)
|
|
951 init_ralloc ();
|
|
952
|
|
953 new_bloc = get_bloc (size);
|
|
954 if (new_bloc)
|
|
955 {
|
|
956 new_bloc->variable = ptr;
|
|
957 *ptr = new_bloc->data;
|
|
958 }
|
|
959 else
|
|
960 *ptr = 0;
|
|
961
|
|
962 return *ptr;
|
|
963 }
|
|
964
|
|
965 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
|
|
966 Store 0 in *PTR to show there's no block allocated. */
|
|
967
|
|
968 void
|
|
969 r_alloc_free (POINTER *ptr)
|
|
970 {
|
255
|
971 register bloc_ptr dead_bloc;
|
|
972
|
|
973 if (! r_alloc_initialized)
|
|
974 init_ralloc ();
|
0
|
975
|
|
976 dead_bloc = find_bloc (ptr);
|
|
977 if (dead_bloc == NIL_BLOC)
|
|
978 abort ();
|
|
979
|
|
980 free_bloc (dead_bloc);
|
|
981 *ptr = 0;
|
255
|
982
|
|
983 #ifdef emacs
|
|
984 refill_memory_reserve ();
|
|
985 #endif
|
0
|
986 }
|
|
987
|
|
988 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
|
|
989 Do this by shifting all blocks above this one up in memory, unless
|
|
990 SIZE is less than or equal to the current bloc size, in which case
|
|
991 do nothing.
|
|
992
|
255
|
993 In case r_alloc_freeze is set, a new bloc is allocated, and the
|
|
994 memory copied to it. Not very efficient. We could traverse the
|
|
995 bloc_list for a best fit of free blocs first.
|
|
996
|
0
|
997 Change *PTR to reflect the new bloc, and return this value.
|
|
998
|
|
999 If more memory cannot be allocated, then leave *PTR unchanged, and
|
|
1000 return zero. */
|
|
1001
|
|
1002 POINTER
|
|
1003 r_re_alloc (POINTER *ptr, SIZE size)
|
|
1004 {
|
255
|
1005 register bloc_ptr bloc;
|
|
1006
|
|
1007 if (! r_alloc_initialized)
|
|
1008 init_ralloc ();
|
|
1009
|
|
1010 if (!*ptr)
|
|
1011 return r_alloc (ptr, size);
|
|
1012 if (!size)
|
|
1013 {
|
|
1014 r_alloc_free (ptr);
|
|
1015 return r_alloc (ptr, 0);
|
|
1016 }
|
0
|
1017
|
|
1018 bloc = find_bloc (ptr);
|
|
1019 if (bloc == NIL_BLOC)
|
|
1020 abort ();
|
|
1021
|
255
|
1022 if (size < bloc->size)
|
|
1023 {
|
|
1024 /* Wouldn't it be useful to actually resize the bloc here? */
|
|
1025 /* I think so too, but not if it's too expensive... */
|
|
1026 if ((bloc->size - MEM_ROUNDUP (size) >= page_size)
|
|
1027 && r_alloc_freeze_level == 0)
|
|
1028 {
|
|
1029 resize_bloc (bloc, MEM_ROUNDUP (size));
|
|
1030 /* Never mind if this fails, just do nothing... */
|
|
1031 /* It *should* be infallible! */
|
|
1032 }
|
|
1033 }
|
|
1034 else if (size > bloc->size)
|
|
1035 {
|
|
1036 if (r_alloc_freeze_level)
|
|
1037 {
|
|
1038 bloc_ptr new_bloc;
|
|
1039 new_bloc = get_bloc (MEM_ROUNDUP (size));
|
|
1040 if (new_bloc)
|
|
1041 {
|
|
1042 new_bloc->variable = ptr;
|
|
1043 *ptr = new_bloc->data;
|
|
1044 bloc->variable = (POINTER *) NIL;
|
|
1045 }
|
|
1046 else
|
|
1047 return NIL;
|
|
1048 }
|
|
1049 else
|
|
1050 {
|
|
1051 if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
|
|
1052 return NIL;
|
|
1053 }
|
|
1054 }
|
0
|
1055 return *ptr;
|
|
1056 }
|
255
|
1057
|
|
1058 /* Disable relocations, after making room for at least SIZE bytes
|
|
1059 of non-relocatable heap if possible. The relocatable blocs are
|
|
1060 guaranteed to hold still until thawed, even if this means that
|
|
1061 malloc must return a null pointer. */
|
|
1062
|
|
1063 void
|
|
1064 r_alloc_freeze (size)
|
|
1065 long size;
|
|
1066 {
|
|
1067 if (! r_alloc_initialized)
|
|
1068 init_ralloc ();
|
|
1069
|
|
1070 /* If already frozen, we can't make any more room, so don't try. */
|
|
1071 if (r_alloc_freeze_level > 0)
|
|
1072 size = 0;
|
|
1073 /* If we can't get the amount requested, half is better than nothing. */
|
|
1074 while (size > 0 && r_alloc_sbrk (size) == 0)
|
|
1075 size /= 2;
|
|
1076 ++r_alloc_freeze_level;
|
|
1077 if (size > 0)
|
|
1078 r_alloc_sbrk (-size);
|
|
1079 }
|
|
1080
|
|
1081 void
|
|
1082 r_alloc_thaw ()
|
|
1083 {
|
|
1084
|
|
1085 if (! r_alloc_initialized)
|
|
1086 init_ralloc ();
|
|
1087
|
|
1088 if (--r_alloc_freeze_level < 0)
|
|
1089 abort ();
|
|
1090
|
|
1091 /* This frees all unused blocs. It is not too inefficient, as the resize
|
|
1092 and bcopy is done only once. Afterwards, all unreferenced blocs are
|
|
1093 already shrunk to zero size. */
|
|
1094 if (!r_alloc_freeze_level)
|
|
1095 {
|
|
1096 bloc_ptr *b = &first_bloc;
|
|
1097 while (*b)
|
|
1098 if (!(*b)->variable)
|
|
1099 free_bloc (*b);
|
|
1100 else
|
|
1101 b = &(*b)->next;
|
|
1102 }
|
|
1103 }
|
|
1104
|
0
|
1105
|
|
1106 /* The hook `malloc' uses for the function which gets more space
|
|
1107 from the system. */
|
|
1108 extern POINTER (*__morecore) ();
|
|
1109
|
|
1110 /* Initialize various things for memory allocation. */
|
|
1111
|
|
1112 void
|
|
1113 init_ralloc (void)
|
|
1114 {
|
|
1115 if (r_alloc_initialized)
|
|
1116 return;
|
|
1117
|
|
1118 r_alloc_initialized = 1;
|
|
1119 real_morecore = __morecore;
|
|
1120 __morecore = r_alloc_sbrk;
|
|
1121
|
255
|
1122 first_heap = last_heap = &heap_base;
|
|
1123 first_heap->next = first_heap->prev = NIL_HEAP;
|
|
1124 first_heap->start = first_heap->bloc_start
|
|
1125 = virtual_break_value = break_value = (*real_morecore) (0);
|
0
|
1126 if (break_value == NIL)
|
|
1127 abort ();
|
|
1128
|
|
1129 page_size = PAGE;
|
|
1130 extra_bytes = ROUNDUP (50000);
|
|
1131
|
255
|
1132 #ifdef DOUG_LEA_MALLOC
|
|
1133 mallopt (M_TOP_PAD, 64 * 4096);
|
|
1134 #else
|
|
1135 #if 0 /* Hasn't been synched yet */
|
|
1136 /* Give GNU malloc's morecore some hysteresis
|
|
1137 so that we move all the relocatable blocks much less often. */
|
|
1138 __malloc_extra_blocks = 64;
|
|
1139 #endif
|
|
1140 #endif
|
0
|
1141
|
255
|
1142 first_heap->end = (POINTER) ROUNDUP (first_heap->start);
|
|
1143
|
|
1144 /* The extra call to real_morecore guarantees that the end of the
|
0
|
1145 address space is a multiple of page_size, even if page_size is
|
|
1146 not really the page size of the system running the binary in
|
|
1147 which page_size is stored. This allows a binary to be built on a
|
|
1148 system with one page size and run on a system with a smaller page
|
255
|
1149 size. */
|
|
1150 (*real_morecore) (first_heap->end - first_heap->start);
|
0
|
1151
|
|
1152 /* Clear the rest of the last page; this memory is in our address space
|
|
1153 even though it is after the sbrk value. */
|
|
1154 /* Doubly true, with the additional call that explicitly adds the
|
|
1155 rest of that page to the address space. */
|
255
|
1156 memset (first_heap->start, 0, first_heap->end - first_heap->start);
|
|
1157 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
|
0
|
1158 use_relocatable_buffers = 1;
|
|
1159 }
|
255
|
1160
|
|
1161 #if defined (emacs) && defined (DOUG_LEA_MALLOC)
|
|
1162
|
|
1163 /* Reinitialize the morecore hook variables after restarting a dumped
|
|
1164 Emacs. This is needed when using Doug Lea's malloc from GNU libc. */
|
|
1165 void
|
|
1166 r_alloc_reinit ()
|
|
1167 {
|
|
1168 /* Only do this if the hook has been reset, so that we don't get an
|
|
1169 infinite loop, in case Emacs was linked statically. */
|
|
1170 if (__morecore != r_alloc_sbrk)
|
|
1171 {
|
|
1172 real_morecore = __morecore;
|
|
1173 __morecore = r_alloc_sbrk;
|
|
1174 }
|
|
1175 }
|
|
1176 #if 0
|
|
1177 #ifdef DEBUG
|
|
1178
|
|
1179 void
|
|
1180 r_alloc_check ()
|
|
1181 {
|
|
1182 int found = 0;
|
|
1183 heap_ptr h, ph = 0;
|
|
1184 bloc_ptr b, pb = 0;
|
|
1185
|
|
1186 if (!r_alloc_initialized)
|
|
1187 return;
|
|
1188
|
|
1189 assert (first_heap);
|
|
1190 assert (last_heap->end <= (POINTER) sbrk (0));
|
|
1191 assert ((POINTER) first_heap < first_heap->start);
|
|
1192 assert (first_heap->start <= virtual_break_value);
|
|
1193 assert (virtual_break_value <= first_heap->end);
|
|
1194
|
|
1195 for (h = first_heap; h; h = h->next)
|
|
1196 {
|
|
1197 assert (h->prev == ph);
|
|
1198 assert ((POINTER) ROUNDUP (h->end) == h->end);
|
|
1199 #if 0 /* ??? The code in ralloc.c does not really try to ensure
|
|
1200 the heap start has any sort of alignment.
|
|
1201 Perhaps it should. */
|
|
1202 assert ((POINTER) MEM_ROUNDUP (h->start) == h->start);
|
|
1203 #endif
|
|
1204 assert ((POINTER) MEM_ROUNDUP (h->bloc_start) == h->bloc_start);
|
|
1205 assert (h->start <= h->bloc_start && h->bloc_start <= h->end);
|
|
1206
|
|
1207 if (ph)
|
|
1208 {
|
|
1209 assert (ph->end < h->start);
|
|
1210 assert (h->start <= (POINTER)h && (POINTER)(h+1) <= h->bloc_start);
|
|
1211 }
|
|
1212
|
|
1213 if (h->bloc_start <= break_value && break_value <= h->end)
|
|
1214 found = 1;
|
|
1215
|
|
1216 ph = h;
|
|
1217 }
|
|
1218
|
|
1219 assert (found);
|
|
1220 assert (last_heap == ph);
|
|
1221
|
|
1222 for (b = first_bloc; b; b = b->next)
|
|
1223 {
|
|
1224 assert (b->prev == pb);
|
|
1225 assert ((POINTER) MEM_ROUNDUP (b->data) == b->data);
|
|
1226 assert ((SIZE) MEM_ROUNDUP (b->size) == b->size);
|
|
1227
|
|
1228 ph = 0;
|
|
1229 for (h = first_heap; h; h = h->next)
|
|
1230 {
|
|
1231 if (h->bloc_start <= b->data && b->data + b->size <= h->end)
|
|
1232 break;
|
|
1233 ph = h;
|
|
1234 }
|
|
1235
|
|
1236 assert (h);
|
|
1237
|
|
1238 if (pb && pb->data + pb->size != b->data)
|
|
1239 {
|
|
1240 assert (ph && b->data == h->bloc_start);
|
|
1241 while (ph)
|
|
1242 {
|
|
1243 if (ph->bloc_start <= pb->data
|
|
1244 && pb->data + pb->size <= ph->end)
|
|
1245 {
|
|
1246 assert (pb->data + pb->size + b->size > ph->end);
|
|
1247 break;
|
|
1248 }
|
|
1249 else
|
|
1250 {
|
|
1251 assert (ph->bloc_start + b->size > ph->end);
|
|
1252 }
|
|
1253 ph = ph->prev;
|
|
1254 }
|
|
1255 }
|
|
1256 pb = b;
|
|
1257 }
|
|
1258
|
|
1259 assert (last_bloc == pb);
|
|
1260
|
|
1261 if (last_bloc)
|
|
1262 assert (last_bloc->data + last_bloc->size == break_value);
|
|
1263 else
|
|
1264 assert (first_heap->bloc_start == break_value);
|
|
1265 }
|
|
1266 #endif /* DEBUG */
|
|
1267 #endif /* 0 */
|
|
1268
|
|
1269 #endif
|
|
1270
|
0
|
1271 #else /* HAVE_MMAP */
|
|
1272
|
185
|
1273 /*
|
0
|
1274 A relocating allocator built using the mmap(2) facility available
|
|
1275 in some OSes. Based on another version written by Paul Flinders,
|
|
1276 from which code (and comments) are snarfed.
|
|
1277
|
|
1278 The OS should support mmap() with MAP_ANONYMOUS attribute, or have
|
|
1279 /dev/zero. It should support private memory mapping.
|
|
1280
|
|
1281 Paul Flinders wrote a version which works well for systems that
|
|
1282 allow callers to specify (virtual) addresses to mmap().
|
|
1283 Unfortunately, such a scheme doesn't work for certain systems like
|
|
1284 HP-UX that have a system-wide virtual->real address map, and
|
|
1285 consequently impose restrictions on the virtual address values
|
185
|
1286 permitted.
|
0
|
1287
|
|
1288 NB: The mapping scheme in HP-UX is motivated by the inverted page
|
|
1289 table design in some HP processors.
|
|
1290
|
|
1291 This alternate implementation allows for the addresses to be
|
|
1292 optionally chosen by the system. Fortunately, buffer allocation
|
|
1293 doesn't insist upon contiguous memory which Flinders' scheme
|
|
1294 provides, and this one doesn't.
|
|
1295
|
|
1296 We don't really provide for hysteresis here, but add some metering
|
|
1297 to monitor how poorly the allocator actually works. See the
|
|
1298 documentation for `mmap-hysteresis'.
|
|
1299
|
|
1300 This implementation actually cycles through the blocks allocated
|
|
1301 via mmap() and only sends it to free() if it wasn't one of them.
|
|
1302 Unfortunately, this is O(n) in the number of mmapped blocks. (Not
|
|
1303 really, as we have a hash table which tries to reduce the cost.)
|
|
1304 Also, this dereferences the pointer passed, so it would cause a
|
|
1305 segfault if garbage was passed to it. */
|
|
1306
|
|
1307 #include <fcntl.h>
|
|
1308 #include <sys/mman.h>
|
|
1309 #include <stdio.h>
|
|
1310
|
|
1311 typedef void *VM_ADDR; /* VM addresses */
|
|
1312 static CONST VM_ADDR VM_FAILURE_ADDR = (VM_ADDR) -1; /* mmap returns this when it fails. */
|
|
1313
|
|
1314 /* Configuration for relocating allocator. */
|
|
1315
|
|
1316 /* #define MMAP_GENERATE_ADDRESSES */
|
|
1317 /* Define this if you want Emacs to manage the address table.
|
|
1318 It is not recommended unless you have major problems with the
|
|
1319 default scheme, which allows the OS to pick addresses. */
|
|
1320
|
|
1321 /* USELESS_LOWER_ADDRESS_BITS defines the number of bits which can be
|
|
1322 discarded while computing the hash, as they're always zero. The
|
|
1323 default is appropriate for a page size of 4096 bytes. */
|
|
1324
|
|
1325 #define USELESS_LOWER_ADDRESS_BITS 12
|
|
1326
|
|
1327
|
|
1328 /* Size of hash table for inverted VM_ADDR->MMAP_HANDLE lookup */
|
|
1329
|
|
1330 #define MHASH_PRIME 89
|
|
1331
|
|
1332
|
|
1333 /* Whether we want to enable metering of some ralloc performance.
|
|
1334 This incurs a constant penalty for each mmap operation. */
|
|
1335
|
|
1336 #define MMAP_METERING
|
|
1337
|
|
1338
|
|
1339 /* Rename the following to protect against a some smartness elsewhere.
|
|
1340 We need access to the allocator used for non-mmap allocation
|
|
1341 elsewhere, in case we get passed a handle that we didn't allocate
|
|
1342 ourselves. Currently, this default allocator is also used to
|
|
1343 maintain local structures for relocatable blocks. */
|
|
1344
|
|
1345 #define UNDERLYING_MALLOC malloc
|
|
1346 #define UNDERLYING_FREE free
|
|
1347 #define UNDERLYING_REALLOC realloc
|
|
1348
|
|
1349 /* MAP_ADDRCHOICE_FLAG is set to MAP_FIXED if MMAP_GENERATE_ADDRESSES
|
|
1350 is defined, and MAP_VARIABLE otherwise. Some losing systems don't
|
|
1351 define the _FIXED/_VARIABLE flags, in which case it is set to 0 */
|
|
1352
|
|
1353 #ifdef MMAP_GENERATE_ADDRESSES
|
|
1354 # ifdef MAP_FIXED
|
|
1355 # define MAP_ADDRCHOICE_FLAG MAP_FIXED
|
|
1356 # endif
|
|
1357 #else /* !MMAP_GENERATE_ADDRESSES */
|
|
1358 # ifdef MAP_VARIABLE
|
|
1359 # define MAP_ADDRCHOICE_FLAG MAP_VARIABLE
|
|
1360 # endif
|
|
1361 #endif /* MMAP_GENERATE_ADDRESSES */
|
|
1362
|
|
1363 /* Default case. */
|
|
1364 #ifndef MAP_ADDRCHOICE_FLAG
|
|
1365 # define MAP_ADDRCHOICE_FLAG 0
|
|
1366 #endif /* MAP_ADDRCHOICE_FLAG */
|
|
1367
|
|
1368 #ifdef MAP_ANONYMOUS
|
|
1369 # define MAP_FLAGS (MAP_PRIVATE | MAP_ADDRCHOICE_FLAG | MAP_ANONYMOUS)
|
|
1370 #else
|
|
1371 # define MAP_FLAGS (MAP_PRIVATE | MAP_ADDRCHOICE_FLAG)
|
|
1372 #endif /* MAP_ANONYMOUS */
|
|
1373
|
|
1374
|
|
1375 /* (ptf): A flag to indicate whether we have initialized ralloc yet. For
|
|
1376 Emacs's sake, please do not make this local to malloc_init; on some
|
|
1377 machines, the dumping procedure makes all static variables
|
|
1378 read-only. On these machines, the word static is #defined to be
|
|
1379 the empty string, meaning that r_alloc_initialized becomes an
|
|
1380 automatic variable, and loses its value each time Emacs is started up.
|
|
1381
|
|
1382 If we're using mmap this flag has three possible values
|
|
1383 0 - initial value
|
|
1384 1 - Normal value when running temacs. In this case buffers
|
|
1385 are allocated using malloc so that any data that they
|
|
1386 contain becomes part of the undumped executable.
|
|
1387 2 - Normal value when running emacs */
|
|
1388 static int r_alloc_initialized = 0;
|
|
1389
|
|
1390 /* (ptf): Macros for rounding. Note that rounding to any value is possible
|
|
1391 by changing the definition of PAGE. */
|
|
1392 #define PAGE (getpagesize ())
|
|
1393 #define PAGES_FOR(size) (((unsigned long int) (size) + page_size - 1)/page_size)
|
|
1394 #define ROUNDUP(size) ((unsigned long int)PAGES_FOR(size)*page_size)
|
|
1395
|
|
1396
|
|
1397 /* DEV_ZERO_FD is -1 normally, but for systems without MAP_ANONYMOUS
|
|
1398 points to a file descriptor opened on /dev/zero */
|
|
1399
|
|
1400 static int DEV_ZERO_FD = -1;
|
|
1401
|
|
1402
|
|
1403 /* We actually need a datastructure that can be usefully structured
|
|
1404 based on the VM address, and allows an ~O(1) lookup on an arbitrary
|
|
1405 address, ie a hash-table. Maybe the XEmacs hash table can be
|
|
1406 coaxed enough. At the moment, we use lookup on a hash-table to
|
|
1407 decide whether to do an O(n) search on the malloced block list.
|
|
1408 Addresses are hashed to a bucket modulo MHASH_PRIME */
|
|
1409
|
|
1410
|
|
1411 /* We settle for a standard doubly-linked-list. The dynarr type isn't
|
|
1412 very amenable to deletion of items in the middle, so we conjure up
|
|
1413 yet another stupid datastructure. The structure is maintained as a
|
|
1414 ring, and the singleton ring has the sole element as it's left and
|
|
1415 right neighbours. */
|
|
1416
|
|
1417 static void init_MHASH_table (void); /* Forward reference */
|
|
1418
|
|
1419 typedef struct alloc_dll
|
|
1420 {
|
|
1421 size_t size; /* #bytes currently in use */
|
|
1422 size_t space_for; /* #bytes we really have */
|
|
1423 POINTER* aliased_address; /* Address of aliased variable, to tweak if relocating */
|
|
1424 VM_ADDR vm_addr; /* VM address returned by mmap */
|
|
1425 struct alloc_dll *left; /* Left link in circular doubly linked list */
|
|
1426 struct alloc_dll *right;
|
|
1427 } *MMAP_HANDLE;
|
|
1428
|
|
1429 static MMAP_HANDLE mmap_start = 0; /* Head of linked list */
|
|
1430 static size_t page_size = 0; /* Size of VM pages */
|
|
1431 static int mmap_hysteresis; /* Should be size_t, really. */
|
|
1432
|
|
1433 /* Get a new handle for a fresh block. */
|
|
1434 static MMAP_HANDLE
|
|
1435 new_mmap_handle (size_t nsiz)
|
|
1436 {
|
163
|
1437 MMAP_HANDLE h = (MMAP_HANDLE) UNDERLYING_MALLOC( sizeof (struct alloc_dll));
|
0
|
1438 if ( h == 0) return 0;
|
|
1439 h->size = nsiz;
|
|
1440 if (mmap_start == 0)
|
|
1441 {
|
|
1442 init_MHASH_table ();
|
|
1443 mmap_start = h; mmap_start->left = h; mmap_start->right = h;
|
|
1444 }
|
|
1445 {
|
|
1446 MMAP_HANDLE prev = mmap_start->left;
|
|
1447 MMAP_HANDLE nex = mmap_start;
|
|
1448
|
|
1449 /* Four pointers need fixing. */
|
|
1450 h->right = nex;
|
|
1451 h->left = prev;
|
|
1452 prev->right = h;
|
|
1453 nex->left = h;
|
|
1454 }
|
|
1455 return h;
|
|
1456 }
|
|
1457
|
|
1458 /* Find a handle given the aliased address using linear search. */
|
|
1459 static MMAP_HANDLE
|
|
1460 find_mmap_handle_lsearch (POINTER *alias)
|
|
1461 {
|
|
1462 MMAP_HANDLE h = mmap_start;
|
|
1463 if (h == 0) return 0;
|
|
1464 do {
|
|
1465 if (h->aliased_address == alias && *alias == h->vm_addr)
|
|
1466 return h;
|
|
1467 h = h->right;
|
|
1468 } while( h != mmap_start );
|
|
1469 return 0; /* Bogus alias passed. */
|
|
1470 }
|
|
1471
|
|
1472 /* Free a handle. */
|
|
1473 static void
|
|
1474 free_mmap_handle (MMAP_HANDLE h)
|
|
1475 {
|
|
1476 MMAP_HANDLE prev = h->left;
|
|
1477 MMAP_HANDLE nex = h->right;
|
|
1478 if (prev == h || nex == h) /* In fact, this should be && */
|
|
1479 { /* We're the singleton dll */
|
|
1480 UNDERLYING_FREE( h ); /* Free the sole item */
|
|
1481 mmap_start = 0; return;
|
|
1482 }
|
185
|
1483 else if (h == mmap_start)
|
0
|
1484 {
|
|
1485 mmap_start = nex; /* Make sure mmap_start isn't bogus. */
|
|
1486 }
|
|
1487 prev->right = nex;
|
|
1488 nex->left = prev;
|
|
1489 UNDERLYING_FREE( h );
|
|
1490 }
|
|
1491
|
|
1492 /* A simple hash table to speed up the inverted lookup of
|
|
1493 VM_ADDR->MMAP_HANDLE. We maintain the number of hits for a
|
|
1494 particular bucket. We invalidate a hash table entry during block
|
|
1495 deletion if the hash has cached the deleted block's address. */
|
|
1496
|
|
1497 /* Simple hash check. */
|
|
1498 struct {
|
|
1499 int n_hits; /* How many addresses map to this? */
|
|
1500 MMAP_HANDLE handle; /* What is the current handle? */
|
|
1501 VM_ADDR addr; /* What is it's VM address? */
|
|
1502 } MHASH_HITS[ MHASH_PRIME ];
|
|
1503
|
|
1504 static void
|
|
1505 init_MHASH_table (void)
|
|
1506 {
|
|
1507 int i = 0;
|
185
|
1508 for (; i < MHASH_PRIME; i++)
|
0
|
1509 {
|
|
1510 MHASH_HITS[i].n_hits = 0;
|
|
1511 MHASH_HITS[i].addr = 0;
|
|
1512 MHASH_HITS[i].handle = 0;
|
|
1513 }
|
|
1514 }
|
|
1515
|
|
1516 /* Compute the hash value for an address. */
|
|
1517 static int
|
|
1518 MHASH (VM_ADDR addr)
|
|
1519 {
|
116
|
1520 #if (LONGBITS == 64)
|
|
1521 unsigned long int addr_shift = (unsigned long int)(addr) >> USELESS_LOWER_ADDRESS_BITS;
|
|
1522 #else
|
0
|
1523 unsigned int addr_shift = (unsigned int)(addr) >> USELESS_LOWER_ADDRESS_BITS;
|
116
|
1524 #endif
|
185
|
1525 int hval = addr_shift % MHASH_PRIME; /* We could have addresses which are -ve
|
0
|
1526 when converted to signed ints */
|
|
1527 return ((hval >= 0) ? hval : MHASH_PRIME + hval);
|
|
1528 }
|
|
1529
|
|
1530 /* Add a VM address with it's corresponding handle to the table. */
|
|
1531 static void
|
|
1532 MHASH_ADD (VM_ADDR addr, MMAP_HANDLE h)
|
|
1533 {
|
|
1534 int kVal = MHASH( addr );
|
|
1535 if (MHASH_HITS[kVal].n_hits++ == 0)
|
|
1536 { /* Only overwrite the table if there were no hits so far. */
|
|
1537 MHASH_HITS[kVal].addr = addr;
|
|
1538 MHASH_HITS[kVal].handle = h;
|
|
1539 }
|
|
1540 }
|
|
1541
|
|
1542 /* Delete a VM address entry from the hash table. */
|
|
1543 static void
|
|
1544 MHASH_DEL (VM_ADDR addr)
|
|
1545 {
|
|
1546 int kVal = MHASH( addr );
|
|
1547 MHASH_HITS[kVal].n_hits--;
|
|
1548 if (addr == MHASH_HITS[kVal].addr)
|
|
1549 {
|
|
1550 MHASH_HITS[kVal].addr = 0; /* Invalidate cache. */
|
|
1551 MHASH_HITS[kVal].handle = 0;
|
|
1552 }
|
|
1553 }
|
|
1554
|
|
1555 /* End of hash buckets */
|
|
1556
|
|
1557 /* Metering malloc performance. */
|
|
1558 #ifdef MMAP_METERING
|
|
1559 /* If we're metering, we introduce some extra symbols to aid the noble
|
|
1560 cause of bloating XEmacs core size. */
|
|
1561
|
|
1562 Lisp_Object Qmm_times_mapped;
|
|
1563 Lisp_Object Qmm_pages_mapped;
|
|
1564 Lisp_Object Qmm_times_unmapped;
|
|
1565 Lisp_Object Qmm_times_remapped;
|
|
1566 Lisp_Object Qmm_didnt_copy;
|
|
1567 Lisp_Object Qmm_pages_copied;
|
|
1568 Lisp_Object Qmm_average_bumpval;
|
|
1569 Lisp_Object Qmm_wastage;
|
|
1570 Lisp_Object Qmm_live_pages;
|
|
1571 Lisp_Object Qmm_addr_looked_up;
|
|
1572 Lisp_Object Qmm_hash_worked;
|
|
1573 Lisp_Object Qmm_addrlist_size;
|
|
1574
|
|
1575 #define M_Map 0 /* How many times allocated? */
|
|
1576 #define M_Pages_Map 1 /* How many pages allocated? */
|
|
1577 #define M_Unmap 2 /* How many times freed? */
|
|
1578 #define M_Remap 3 /* How many times increased in size? */
|
|
1579 #define M_Didnt_Copy 4 /* How many times didn't need to copy? */
|
|
1580 #define M_Copy_Pages 5 /* Total # pages copied */
|
|
1581 #define M_Average_Bumpval 6 /* Average bump value */
|
|
1582 #define M_Wastage 7 /* Remaining (unused space) */
|
|
1583 #define M_Live_Pages 8 /* #live pages */
|
|
1584 #define M_Address_Lookup 9 /* How many times did we need to check if an addr is in the block? */
|
|
1585 #define M_Hash_Worked 10 /* How many times did the simple hash check work? */
|
|
1586 #define M_Addrlist_Size 11 /* What is the size of the XEmacs memory map? */
|
|
1587
|
|
1588 #define N_Meterables 12 /* Total number of meterables */
|
|
1589 #define MEMMETER(x) {x;}
|
|
1590 #define MVAL(x) (meter[x])
|
|
1591 #define MLVAL(x) (make_int (meter[x]))
|
|
1592 static int meter[N_Meterables];
|
|
1593
|
20
|
1594 DEFUN ("mmap-allocator-status", Fmmap_allocator_status, 0, 0, 0, /*
|
0
|
1595 Return some information about mmap-based allocator.
|
|
1596
|
185
|
1597 mmap-addrlist-size: number of entries in address picking list.
|
|
1598 mmap-times-mapped: number of times r_alloc was called.
|
|
1599 mmap-pages-mapped: number of pages mapped by r_alloc calls only.
|
|
1600 mmap-times-unmapped: number of times r_free was called.
|
0
|
1601 mmap-times-remapped: number of times r_re_alloc was called.
|
|
1602 mmap-didnt-copy: number of times re-alloc didn\'t have to move the block.
|
|
1603 mmap-pages-copied: total number of pages copied.
|
|
1604 mmap-average-bumpval: average increase in size demanded to re-alloc.
|
|
1605 mmap-wastage: total number of bytes allocated, but not currently in use.
|
|
1606 mmap-live-pages: total number of pages live.
|
20
|
1607 */
|
|
1608 ())
|
0
|
1609 {
|
|
1610 Lisp_Object result;
|
|
1611
|
|
1612 result = Fcons (Fcons (Qmm_addrlist_size, MLVAL (M_Addrlist_Size)), Qnil);
|
|
1613 result = Fcons (Fcons (Qmm_hash_worked, MLVAL (M_Hash_Worked)), result);
|
|
1614 result = Fcons (Fcons (Qmm_addr_looked_up, MLVAL (M_Address_Lookup)), result);
|
|
1615 result = Fcons (Fcons (Qmm_live_pages, MLVAL (M_Live_Pages)), result);
|
|
1616 result = Fcons (Fcons (Qmm_wastage, MLVAL (M_Wastage)), result);
|
|
1617 result = Fcons (Fcons (Qmm_average_bumpval, MLVAL (M_Average_Bumpval)),
|
|
1618 result);
|
|
1619 result = Fcons (Fcons (Qmm_pages_copied, MLVAL (M_Copy_Pages)), result);
|
|
1620 result = Fcons (Fcons (Qmm_didnt_copy, MLVAL (M_Didnt_Copy)), result);
|
|
1621 result = Fcons (Fcons (Qmm_times_remapped, MLVAL (M_Remap)), result);
|
|
1622 result = Fcons (Fcons (Qmm_times_unmapped, MLVAL (M_Unmap)), result);
|
|
1623 result = Fcons (Fcons (Qmm_pages_mapped, MLVAL (M_Pages_Map)), result);
|
|
1624 result = Fcons (Fcons (Qmm_times_mapped, MLVAL (M_Map)), result);
|
|
1625
|
|
1626 return result;
|
|
1627 }
|
|
1628
|
|
1629 #else /* !MMAP_METERING */
|
|
1630
|
185
|
1631 #define MEMMETER(x)
|
0
|
1632 #define MVAL(x)
|
|
1633
|
|
1634 #endif /* MMAP_METERING */
|
|
1635
|
|
1636 static MMAP_HANDLE
|
|
1637 find_mmap_handle (POINTER *alias)
|
|
1638 {
|
|
1639 int kval = MHASH( *alias );
|
|
1640 MEMMETER( MVAL(M_Address_Lookup)++ )
|
|
1641 switch( MHASH_HITS[kval].n_hits)
|
|
1642 {
|
|
1643 case 0:
|
|
1644 MEMMETER( MVAL( M_Hash_Worked )++ )
|
|
1645 return 0;
|
|
1646
|
|
1647 case 1:
|
185
|
1648 if (*alias == MHASH_HITS[kval].addr)
|
|
1649 {
|
0
|
1650 MEMMETER( MVAL( M_Hash_Worked) ++ );
|
|
1651 return MHASH_HITS[kval].handle;
|
|
1652 }
|
|
1653 /* FALL THROUGH */
|
|
1654 default:
|
|
1655 return find_mmap_handle_lsearch( alias );
|
|
1656 } /* switch */
|
|
1657 }
|
|
1658
|
185
|
1659 /*
|
0
|
1660 Some kernels don't like being asked to pick addresses for mapping
|
|
1661 themselves---IRIX is known to become extremely slow if mmap is
|
|
1662 passed a ZERO as the first argument. In such cases, we use an
|
|
1663 address map which is managed local to the XEmacs process. The
|
|
1664 address map maintains an ordered linked list of (address, size,
|
|
1665 occupancy) triples ordered by the absolute address. Initially, a
|
|
1666 large address area is marked as being empty. The address picking
|
|
1667 scheme takes bites off the first block which is still empty and
|
|
1668 large enough. If mmap with the specified address fails, it is
|
|
1669 marked unavailable and not attempted thereafter. The scheme will
|
|
1670 keep fragmenting the large empty block until it finds an address
|
|
1671 which can be successfully mmapped, or until there are no free
|
|
1672 blocks of the given size left.
|
|
1673
|
|
1674 Note that this scheme, given it's first-fit strategy, is prone to
|
2
|
1675 fragmentation of the first part of memory earmarked for this
|
0
|
1676 purpose. [ACP Vol I]. We can't use the workaround of using a
|
|
1677 randomized first fit because we don't want to presume too much
|
|
1678 about the memory map. Instead, we try to coalesce empty or
|
|
1679 unavailable blocks at any available opportunity. */
|
|
1680
|
74
|
1681 /* Initialization procedure for address picking scheme */
|
|
1682 static void Addr_Block_initialize(void);
|
|
1683
|
|
1684 /* Get a suitable VM_ADDR via mmap */
|
|
1685 static VM_ADDR New_Addr_Block( SIZE sz );
|
|
1686
|
|
1687 /* Free a VM_ADDR allocated via New_Addr_Block */
|
|
1688 static void Free_Addr_Block( VM_ADDR addr, SIZE sz );
|
0
|
1689
|
|
1690 #ifdef MMAP_GENERATE_ADDRESSES
|
|
1691 /* Implementation of the three calls for address picking when XEmacs is incharge */
|
|
1692
|
|
1693 /* The enum denotes the status of the following block. */
|
|
1694 typedef enum { empty = 0, occupied, unavailable } addr_status;
|
|
1695
|
|
1696 typedef struct addr_chain
|
|
1697 {
|
|
1698 POINTER addr;
|
|
1699 SIZE sz;
|
|
1700 addr_status flag;
|
|
1701 struct addr_chain *next;
|
|
1702 } ADDRESS_BLOCK, *ADDRESS_CHAIN;
|
|
1703 /* NB: empty and unavailable blocks are concatenated. */
|
|
1704
|
|
1705 static ADDRESS_CHAIN addr_chain = 0;
|
|
1706 /* Start off the address block chain with a humongous address block
|
|
1707 which is empty to start with. Note that addr_chain is invariant
|
|
1708 WRT the addition/deletion of address blocks because of the assert
|
185
|
1709 in Coalesce() and the strict ordering of blocks by their address
|
0
|
1710 */
|
|
1711 static void Addr_Block_initialize()
|
|
1712 {
|
|
1713 MEMMETER( MVAL( M_Addrlist_Size )++)
|
|
1714 addr_chain = (ADDRESS_CHAIN) UNDERLYING_MALLOC( sizeof( ADDRESS_BLOCK ));
|
|
1715 addr_chain->next = 0; /* Last block in chain */
|
|
1716 addr_chain->sz = 0x0c000000; /* Size */
|
|
1717 addr_chain->addr = (POINTER) (0x04000000 | DATA_SEG_BITS);
|
|
1718 addr_chain->flag = empty;
|
|
1719 }
|
|
1720
|
|
1721 /* Coalesce address blocks if they are contiguous. Only empty and
|
|
1722 unavailable slots are coalesced. */
|
|
1723 static void Coalesce_Addr_Blocks()
|
|
1724 {
|
|
1725 ADDRESS_CHAIN p;
|
|
1726 for (p = addr_chain; p; p = p->next)
|
|
1727 {
|
|
1728 while (p->next && p->flag == p->next->flag)
|
|
1729 {
|
|
1730 ADDRESS_CHAIN np;
|
|
1731 np = p->next;
|
|
1732
|
|
1733 if (p->flag == occupied) break; /* No cigar */
|
|
1734
|
|
1735 /* Check if the addresses are contiguous. */
|
185
|
1736 if (p->addr + p->sz != np->addr) break;
|
|
1737
|
0
|
1738 MEMMETER( MVAL( M_Addrlist_Size )--)
|
|
1739 /* We can coalesce these two. */
|
|
1740 p->sz += np->sz;
|
|
1741 p->next = np->next;
|
|
1742 assert( np != addr_chain ); /* We're not freeing the head of the list. */
|
|
1743 UNDERLYING_FREE( np );
|
|
1744 }
|
|
1745 } /* for all p */
|
|
1746 }
|
|
1747
|
|
1748 /* Get an empty address block of specified size. */
|
|
1749 static VM_ADDR New_Addr_Block( SIZE sz )
|
|
1750 {
|
|
1751 ADDRESS_CHAIN p = addr_chain;
|
|
1752 VM_ADDR new_addr = VM_FAILURE_ADDR;
|
|
1753 for (; p; p = p->next)
|
|
1754 {
|
|
1755 if (p->flag == empty && p->sz > sz)
|
|
1756 {
|
|
1757 /* Create a new entry following p which is empty. */
|
|
1758 ADDRESS_CHAIN remainder = (ADDRESS_CHAIN) UNDERLYING_MALLOC( sizeof( ADDRESS_BLOCK ) );
|
|
1759 remainder->next = p->next;
|
|
1760 remainder->flag = empty;
|
|
1761 remainder->addr = p->addr + sz;
|
|
1762 remainder->sz = p->sz - sz;
|
|
1763
|
|
1764 MEMMETER( MVAL( M_Addrlist_Size )++)
|
185
|
1765
|
0
|
1766 /* Now make p become an occupied block with the appropriate size */
|
|
1767 p->next = remainder;
|
|
1768 p->sz = sz;
|
|
1769 new_addr = mmap( (VM_ADDR) p->addr, p->sz, PROT_READ|PROT_WRITE,
|
|
1770 MAP_FLAGS, DEV_ZERO_FD, 0 );
|
|
1771 if (new_addr == VM_FAILURE_ADDR)
|
|
1772 {
|
|
1773 p->flag = unavailable;
|
|
1774 continue;
|
|
1775 }
|
|
1776 p->flag = occupied;
|
|
1777 break;
|
|
1778 }
|
|
1779 }
|
|
1780 Coalesce_Addr_Blocks();
|
|
1781 return new_addr;
|
|
1782 }
|
|
1783
|
|
1784 /* Free an address block. We mark the block as being empty, and attempt to
|
|
1785 do any coalescing that may have resulted from this. */
|
|
1786 static void Free_Addr_Block( VM_ADDR addr, SIZE sz )
|
|
1787 {
|
|
1788 ADDRESS_CHAIN p = addr_chain;
|
|
1789 for (; p; p = p->next )
|
|
1790 {
|
|
1791 if (p->addr == addr)
|
|
1792 {
|
|
1793 if (p->sz != sz) abort(); /* ACK! Shouldn't happen at all. */
|
|
1794 munmap( (VM_ADDR) p->addr, p->sz );
|
|
1795 p->flag = empty;
|
|
1796 break;
|
|
1797 }
|
|
1798 }
|
185
|
1799 if (!p) abort(); /* Can't happen... we've got a block to free which is not in
|
0
|
1800 the address list. */
|
|
1801 Coalesce_Addr_Blocks();
|
|
1802 }
|
|
1803 #else /* !MMAP_GENERATE_ADDRESSES */
|
|
1804 /* This is an alternate (simpler) implementation in cases where the
|
|
1805 address is picked by the kernel. */
|
|
1806
|
74
|
1807 static void Addr_Block_initialize(void)
|
|
1808 {
|
|
1809 /* Nothing. */
|
|
1810 }
|
0
|
1811
|
|
1812 static VM_ADDR New_Addr_Block( SIZE sz )
|
|
1813 {
|
185
|
1814 return mmap (0, sz, PROT_READ|PROT_WRITE, MAP_FLAGS,
|
0
|
1815 DEV_ZERO_FD, 0 );
|
|
1816 }
|
|
1817
|
|
1818 static void Free_Addr_Block( VM_ADDR addr, SIZE sz )
|
|
1819 {
|
185
|
1820 munmap ((caddr_t) addr, sz );
|
0
|
1821 }
|
|
1822
|
|
1823 #endif /* MMAP_GENERATE_ADDRESSES */
|
|
1824
|
|
1825
|
|
1826 /* IMPLEMENTATION OF EXPORTED RELOCATOR INTERFACE */
|
|
1827
|
|
1828 /*
|
|
1829 r_alloc( POINTER, SIZE ): Allocate a relocatable area with the start
|
185
|
1830 address aliased to the first parameter.
|
0
|
1831 */
|
|
1832
|
|
1833 POINTER r_alloc (POINTER *ptr, SIZE size);
|
|
1834 POINTER
|
|
1835 r_alloc (POINTER *ptr, SIZE size)
|
|
1836 {
|
|
1837 MMAP_HANDLE mh;
|
185
|
1838
|
0
|
1839 switch(r_alloc_initialized)
|
|
1840 {
|
|
1841 case 0:
|
|
1842 abort();
|
|
1843 case 1:
|
163
|
1844 *ptr = (POINTER) UNDERLYING_MALLOC(size);
|
0
|
1845 break;
|
|
1846 default:
|
|
1847 mh = new_mmap_handle( size );
|
|
1848 if (mh)
|
|
1849 {
|
|
1850 SIZE hysteresis = (mmap_hysteresis > 0 ? mmap_hysteresis : 0);
|
|
1851 SIZE mmapped_size = ROUNDUP( size + hysteresis );
|
|
1852 MEMMETER( MVAL(M_Map)++ )
|
|
1853 MEMMETER( MVAL(M_Pages_Map) += (mmapped_size/page_size) )
|
|
1854 MEMMETER( MVAL(M_Wastage) += mmapped_size - size )
|
|
1855 MEMMETER( MVAL(M_Live_Pages) += (mmapped_size/page_size) )
|
|
1856 mh->vm_addr = New_Addr_Block( mmapped_size );
|
|
1857 if (mh->vm_addr == VM_FAILURE_ADDR) {
|
|
1858 free_mmap_handle( mh ); /* Free the loser */
|
|
1859 *ptr = 0;
|
|
1860 return 0; /* ralloc failed due to mmap() failure. */
|
|
1861 }
|
|
1862 MHASH_ADD( mh->vm_addr, mh );
|
|
1863 mh->space_for = mmapped_size;
|
|
1864 mh->aliased_address = ptr;
|
185
|
1865 *ptr = (POINTER) mh->vm_addr;
|
0
|
1866 }
|
|
1867 else
|
|
1868 *ptr = 0; /* Malloc of block failed */
|
|
1869 break;
|
|
1870 }
|
|
1871 return *ptr;
|
|
1872 }
|
|
1873
|
|
1874 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
|
|
1875 Store 0 in *PTR to show there's no block allocated. */
|
|
1876
|
|
1877 void r_alloc_free (POINTER *ptr);
|
|
1878 void
|
|
1879 r_alloc_free (POINTER *ptr)
|
|
1880 {
|
|
1881 switch( r_alloc_initialized) {
|
|
1882 case 0:
|
|
1883 abort();
|
|
1884
|
|
1885 case 1:
|
|
1886 UNDERLYING_FREE( *ptr ); /* Certain this is from the heap. */
|
|
1887 break;
|
|
1888
|
|
1889 default:
|
|
1890 {
|
|
1891 MMAP_HANDLE dead_handle = find_mmap_handle( ptr );
|
|
1892 /* Check if we've got it. */
|
|
1893 if (dead_handle == 0) /* Didn't find it in the list of mmap handles */
|
|
1894 {
|
|
1895 UNDERLYING_FREE( *ptr );
|
|
1896 }
|
|
1897 else
|
|
1898 {
|
|
1899 MEMMETER( MVAL( M_Wastage ) -= (dead_handle->space_for - dead_handle->size) )
|
|
1900 MEMMETER( MVAL( M_Live_Pages ) -= (dead_handle->space_for / page_size ))
|
|
1901 MEMMETER(MVAL(M_Unmap)++)
|
|
1902 MHASH_DEL( dead_handle->vm_addr );
|
|
1903 Free_Addr_Block( dead_handle->vm_addr, dead_handle->space_for );
|
|
1904 free_mmap_handle (dead_handle);
|
|
1905 }
|
|
1906 }
|
|
1907 break;
|
|
1908 } /* r_alloc_initialized */
|
|
1909 *ptr = 0; /* Zap the pointer's contents. */
|
|
1910 }
|
|
1911
|
|
1912 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
|
|
1913
|
|
1914 Change *PTR to reflect the new bloc, and return this value.
|
|
1915
|
|
1916 If more memory cannot be allocated, then leave *PTR unchanged, and
|
|
1917 return zero. */
|
|
1918
|
|
1919 POINTER r_re_alloc (POINTER *ptr, SIZE sz);
|
|
1920 POINTER
|
|
1921 r_re_alloc (POINTER *ptr, SIZE sz)
|
|
1922 {
|
|
1923 if (r_alloc_initialized == 0)
|
|
1924 {
|
|
1925 abort ();
|
|
1926 return 0; /* suppress compiler warning */
|
|
1927 }
|
|
1928 else if (r_alloc_initialized == 1)
|
|
1929 {
|
163
|
1930 POINTER tmp = (POINTER) realloc(*ptr, sz);
|
0
|
1931 if (tmp)
|
|
1932 *ptr = tmp;
|
|
1933 return tmp;
|
|
1934 }
|
|
1935 else
|
|
1936 {
|
|
1937 SIZE hysteresis = (mmap_hysteresis > 0 ? mmap_hysteresis : 0);
|
|
1938 SIZE actual_sz = ROUNDUP( sz + hysteresis );
|
|
1939 MMAP_HANDLE h = find_mmap_handle( ptr );
|
|
1940 VM_ADDR new_vm_addr;
|
|
1941
|
|
1942 if ( h == 0 ) /* Was allocated using malloc. */
|
|
1943 {
|
163
|
1944 POINTER tmp = (POINTER) UNDERLYING_REALLOC(*ptr, sz);
|
0
|
1945 if (tmp)
|
|
1946 *ptr = tmp;
|
|
1947 return tmp;
|
|
1948 }
|
|
1949
|
|
1950 MEMMETER(
|
|
1951 MVAL(M_Average_Bumpval) =
|
|
1952 (((double) MVAL(M_Remap) * MVAL(M_Average_Bumpval)) + (sz - h->size))
|
|
1953 / (double) (MVAL(M_Remap) + 1))
|
|
1954 MEMMETER(MVAL(M_Remap)++)
|
|
1955 if (h->space_for > sz) /* We've got some more room */
|
|
1956 { /* Also, if a shrinkage was asked for. */
|
|
1957 MEMMETER( MVAL(M_Didnt_Copy)++ )
|
|
1958 MEMMETER( MVAL(M_Wastage) -= (sz - h->size))
|
185
|
1959 /* We're pretty dumb at handling shrinkage. We should check for
|
0
|
1960 a larger gap than the standard hysteresis allowable, and if so,
|
|
1961 shrink the number of pages. Right now, we simply reset the size
|
|
1962 component and return. */
|
|
1963 h->size = sz;
|
|
1964 return *ptr;
|
|
1965 }
|
185
|
1966
|
0
|
1967 new_vm_addr = New_Addr_Block( actual_sz );
|
185
|
1968 if (new_vm_addr == VM_FAILURE_ADDR)
|
0
|
1969 {/* Failed to realloc. */
|
|
1970 /* *ptr = 0; */
|
|
1971 return 0;
|
|
1972 }
|
|
1973
|
|
1974 MHASH_ADD( new_vm_addr, h );
|
|
1975 /* We got a block OK: now we should move the old contents to the
|
|
1976 new address. We use the old size of this block. */
|
|
1977 memmove(new_vm_addr, h->vm_addr, h->size);
|
|
1978 MHASH_DEL( h->vm_addr );
|
|
1979 Free_Addr_Block( h->vm_addr, h->space_for ); /* Unmap old area. */
|
|
1980
|
|
1981 MEMMETER( MVAL( M_Copy_Pages ) += (h->space_for/page_size) )
|
|
1982 MEMMETER( MVAL( M_Live_Pages ) -= (h->space_for / page_size))
|
|
1983 MEMMETER( MVAL( M_Live_Pages ) += (actual_sz / page_size))
|
|
1984 MEMMETER( MVAL( M_Wastage ) -= (h->space_for - h->size))
|
|
1985 MEMMETER( MVAL( M_Wastage ) += (actual_sz - sz) )
|
|
1986
|
|
1987 /* Update block datastructure. */
|
|
1988 h->space_for = actual_sz; /* New total space */
|
|
1989 h->size = sz; /* New (requested) size */
|
|
1990 h->vm_addr = new_vm_addr; /* New VM start address */
|
|
1991 h->aliased_address = ptr; /* Change alias to reflect block relocation. */
|
185
|
1992 *ptr = (POINTER) h->vm_addr;
|
0
|
1993 return *ptr;
|
|
1994 }
|
|
1995 }
|
|
1996
|
|
1997
|
|
1998 /* Initialize various things for memory allocation.
|
|
1999 */
|
|
2000 void
|
|
2001 init_ralloc (void)
|
|
2002 {
|
|
2003 int i = 0;
|
|
2004 if (r_alloc_initialized > 1)
|
|
2005 return; /* used to return 1 */
|
|
2006
|
|
2007 if (++r_alloc_initialized == 1)
|
|
2008 return; /* used to return 1 */
|
|
2009
|
|
2010 Addr_Block_initialize(); /* Initialize the address picker, if required. */
|
|
2011 page_size = PAGE;
|
|
2012 assert( page_size > 0 ); /* getpagesize() bogosity check. */
|
|
2013
|
|
2014 #ifndef MAP_ANONYMOUS
|
|
2015 DEV_ZERO_FD = open( "/dev/zero", O_RDWR );
|
|
2016 if (DEV_ZERO_FD < 0)
|
|
2017 /* Failed. Perhaps we should abort here? */
|
|
2018 return; /* used to return 0 */
|
|
2019 #endif
|
|
2020
|
|
2021 #ifdef MMAP_METERING
|
|
2022 for(i = 0; i < N_Meterables; i++ )
|
|
2023 {
|
|
2024 meter[i] = 0;
|
|
2025 }
|
|
2026 #endif /* MMAP_METERING */
|
|
2027 }
|
|
2028
|
|
2029 void
|
|
2030 syms_of_ralloc (void)
|
|
2031 {
|
|
2032 #ifdef MMAP_METERING
|
84
|
2033 defsymbol (&Qmm_times_mapped, "mmap-times-mapped");
|
|
2034 defsymbol (&Qmm_pages_mapped, "mmap-pages-mapped");
|
|
2035 defsymbol (&Qmm_times_unmapped, "mmap-times-unmapped");
|
|
2036 defsymbol (&Qmm_times_remapped, "mmap-times-remapped");
|
|
2037 defsymbol (&Qmm_didnt_copy, "mmap-didnt-copy");
|
|
2038 defsymbol (&Qmm_pages_copied, "mmap-pages-copied");
|
|
2039 defsymbol (&Qmm_average_bumpval, "mmap-average-bumpval");
|
|
2040 defsymbol (&Qmm_wastage, "mmap-wastage");
|
|
2041 defsymbol (&Qmm_live_pages, "mmap-live-pages");
|
|
2042 defsymbol (&Qmm_addr_looked_up, "mmap-had-to-look-up-address");
|
|
2043 defsymbol (&Qmm_hash_worked, "mmap-hash-table-worked");
|
|
2044 defsymbol (&Qmm_addrlist_size, "mmap-addrlist-size");
|
20
|
2045 DEFSUBR (Fmmap_allocator_status);
|
0
|
2046 #endif /* MMAP_METERING */
|
|
2047 }
|
|
2048
|
|
2049 void
|
|
2050 vars_of_ralloc (void)
|
|
2051 {
|
|
2052 DEFVAR_INT ("mmap-hysteresis", &mmap_hysteresis /*
|
|
2053 Extra room left at the end of an allocated arena,
|
185
|
2054 so that a re-alloc requesting extra space smaller than this
|
0
|
2055 does not actually cause a new arena to be allocated.
|
|
2056
|
185
|
2057 A negative value is considered equal to zero. This is the
|
|
2058 minimum amount of space guaranteed to be left at the end of
|
0
|
2059 the arena. Because allocation happens in multiples of the OS
|
|
2060 page size, it is possible for more space to be left unused.
|
|
2061 */ );
|
|
2062 mmap_hysteresis = 0;
|
|
2063 }
|
|
2064
|
|
2065 #endif /* HAVE_MMAP */
|