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