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