Expand PMF_FN_* macros.
[netbsd-mini2440.git] / gnu / lib / libmalloc / ralloc.c
blobebe8181dfaa2a797c712ef3eaff303bb11835ae0
1 /* Block-relocating memory allocator.
2 Copyright (C) 1993 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, USA. */
22 /* NOTES:
24 Only relocate the blocs neccessary for SIZE in r_alloc_sbrk,
25 rather than all of them. This means allowing for a possible
26 hole between the first bloc and the end of malloc storage. */
28 #ifdef emacs
30 #include "config.h"
31 #include "lisp.h" /* Needed for VALBITS. */
33 #undef NULL
35 /* The important properties of this type are that 1) it's a pointer, and
36 2) arithmetic on it should work as if the size of the object pointed
37 to has a size of 1. */
38 #if 0 /* Arithmetic on void* is a GCC extension. */
39 #ifdef __STDC__
40 typedef void *POINTER;
41 #else
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
47 typedef char *POINTER;
49 #endif
50 #endif /* 0 */
52 /* Unconditionally use char * for this. */
53 typedef char *POINTER;
55 typedef unsigned long SIZE;
57 /* Declared in dispnew.c, this version doesn't screw up if regions
58 overlap. */
59 extern void safe_bcopy ();
61 #include "getpagesize.h"
63 #else /* Not emacs. */
65 #include <stddef.h>
67 typedef size_t SIZE;
68 typedef void *POINTER;
70 #include <unistd.h>
71 #include <stdlib.h>
72 #include <malloc.h>
73 #include <string.h>
75 #define safe_bcopy(x, y, z) memmove (y, x, z)
77 #endif /* emacs. */
79 #define NIL ((POINTER) 0)
81 /* A flag to indicate whether we have initialized ralloc yet. For
82 Emacs's sake, please do not make this local to malloc_init; on some
83 machines, the dumping procedure makes all static variables
84 read-only. On these machines, the word static is #defined to be
85 the empty string, meaning that r_alloc_initialized becomes an
86 automatic variable, and loses its value each time Emacs is started up. */
87 static int r_alloc_initialized = 0;
89 static void r_alloc_init ();
91 /* Declarations for working with the malloc, ralloc, and system breaks. */
93 /* Function to set the real break value. */
94 static POINTER (*real_morecore) ();
96 /* The break value, as seen by malloc (). */
97 static POINTER virtual_break_value;
99 /* The break value, viewed by the relocatable blocs. */
100 static POINTER break_value;
102 /* The REAL (i.e., page aligned) break value of the process. */
103 static POINTER page_break_value;
105 /* This is the size of a page. We round memory requests to this boundary. */
106 static int page_size;
108 /* Whenever we get memory from the system, get this many extra bytes. This
109 must be a multiple of page_size. */
110 static int extra_bytes;
112 /* Macros for rounding. Note that rounding to any value is possible
113 by changing the definition of PAGE. */
114 #define PAGE (getpagesize ())
115 #define ALIGNED(addr) (((unsigned long) (addr) & (page_size - 1)) == 0)
116 #define ROUNDUP(size) (((unsigned long) (size) + page_size - 1) & ~(page_size - 1))
117 #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
119 /* Functions to get and return memory from the system. */
121 /* Obtain SIZE bytes of space. If enough space is not presently available
122 in our process reserve, (i.e., (page_break_value - break_value)),
123 this means getting more page-aligned space from the system.
125 Return non-zero if all went well, or zero if we couldn't allocate
126 the memory. */
127 static int
128 obtain (size)
129 SIZE size;
131 SIZE already_available = page_break_value - break_value;
133 if (already_available < size)
135 SIZE get = ROUNDUP (size - already_available);
136 /* Get some extra, so we can come here less often. */
137 get += extra_bytes;
139 if ((*real_morecore) (get) == 0)
140 return 0;
142 page_break_value += get;
145 break_value += size;
147 return 1;
150 /* Obtain SIZE bytes of space and return a pointer to the new area.
151 If we could not allocate the space, return zero. */
153 static POINTER
154 get_more_space (size)
155 SIZE size;
157 POINTER ptr = break_value;
158 if (obtain (size))
159 return ptr;
160 else
161 return 0;
164 /* Note that SIZE bytes of space have been relinquished by the process.
165 If SIZE is more than a page, return the space to the system. */
167 static void
168 relinquish (size)
169 SIZE size;
171 POINTER new_page_break;
172 int excess;
174 break_value -= size;
175 new_page_break = (POINTER) ROUNDUP (break_value);
176 excess = (char *) page_break_value - (char *) new_page_break;
178 if (excess > extra_bytes * 2)
180 /* Keep extra_bytes worth of empty space.
181 And don't free anything unless we can free at least extra_bytes. */
182 if ((*real_morecore) (extra_bytes - excess) == 0)
183 abort ();
185 page_break_value += extra_bytes - excess;
188 /* Zero the space from the end of the "official" break to the actual
189 break, so that bugs show up faster. */
190 bzero (break_value, ((char *) page_break_value - (char *) break_value));
193 /* The meat - allocating, freeing, and relocating blocs. */
195 /* These structures are allocated in the malloc arena.
196 The linked list is kept in order of increasing '.data' members.
197 The data blocks abut each other; if b->next is non-nil, then
198 b->data + b->size == b->next->data. */
199 typedef struct bp
201 struct bp *next;
202 struct bp *prev;
203 POINTER *variable;
204 POINTER data;
205 SIZE size;
206 } *bloc_ptr;
208 #define NIL_BLOC ((bloc_ptr) 0)
209 #define BLOC_PTR_SIZE (sizeof (struct bp))
211 /* Head and tail of the list of relocatable blocs. */
212 static bloc_ptr first_bloc, last_bloc;
214 /* Find the bloc referenced by the address in PTR. Returns a pointer
215 to that block. */
217 static bloc_ptr
218 find_bloc (ptr)
219 POINTER *ptr;
221 register bloc_ptr p = first_bloc;
223 while (p != NIL_BLOC)
225 if (p->variable == ptr && p->data == *ptr)
226 return p;
228 p = p->next;
231 return p;
234 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
235 Returns a pointer to the new bloc, or zero if we couldn't allocate
236 memory for the new block. */
238 static bloc_ptr
239 get_bloc (size)
240 SIZE size;
242 register bloc_ptr new_bloc;
244 if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
245 || ! (new_bloc->data = get_more_space (size)))
247 if (new_bloc)
248 free (new_bloc);
250 return 0;
253 new_bloc->size = size;
254 new_bloc->next = NIL_BLOC;
255 new_bloc->variable = (POINTER *) NIL;
257 if (first_bloc)
259 new_bloc->prev = last_bloc;
260 last_bloc->next = new_bloc;
261 last_bloc = new_bloc;
263 else
265 first_bloc = last_bloc = new_bloc;
266 new_bloc->prev = NIL_BLOC;
269 return new_bloc;
272 /* Relocate all blocs from BLOC on upward in the list to the zone
273 indicated by ADDRESS. Direction of relocation is determined by
274 the position of ADDRESS relative to BLOC->data.
276 If BLOC is NIL_BLOC, nothing is done.
278 Note that ordering of blocs is not affected by this function. */
280 static void
281 relocate_some_blocs (bloc, address)
282 bloc_ptr bloc;
283 POINTER address;
285 if (bloc != NIL_BLOC)
287 register SIZE offset = address - bloc->data;
288 register SIZE data_size = 0;
289 register bloc_ptr b;
291 for (b = bloc; b != NIL_BLOC; b = b->next)
293 data_size += b->size;
294 b->data += offset;
295 *b->variable = b->data;
298 safe_bcopy (address - offset, address, data_size);
303 /* Free BLOC from the chain of blocs, relocating any blocs above it
304 and returning BLOC->size bytes to the free area. */
306 static void
307 free_bloc (bloc)
308 bloc_ptr bloc;
310 if (bloc == first_bloc && bloc == last_bloc)
312 first_bloc = last_bloc = NIL_BLOC;
314 else if (bloc == last_bloc)
316 last_bloc = bloc->prev;
317 last_bloc->next = NIL_BLOC;
319 else if (bloc == first_bloc)
321 first_bloc = bloc->next;
322 first_bloc->prev = NIL_BLOC;
324 else
326 bloc->next->prev = bloc->prev;
327 bloc->prev->next = bloc->next;
330 relocate_some_blocs (bloc->next, bloc->data);
331 relinquish (bloc->size);
332 free (bloc);
335 /* Interface routines. */
337 static int use_relocatable_buffers;
339 /* Obtain SIZE bytes of storage from the free pool, or the system, as
340 necessary. If relocatable blocs are in use, this means relocating
341 them. This function gets plugged into the GNU malloc's __morecore
342 hook.
344 We provide hysteresis, never relocating by less than extra_bytes.
346 If we're out of memory, we should return zero, to imitate the other
347 __morecore hook values - in particular, __default_morecore in the
348 GNU malloc package. */
350 POINTER
351 r_alloc_sbrk (size)
352 long size;
354 /* This is the first address not currently available for the heap. */
355 POINTER top;
356 /* Amount of empty space below that. */
357 /* It is not correct to use SIZE here, because that is usually unsigned.
358 ptrdiff_t would be okay, but is not always available.
359 `long' will work in all cases, in practice. */
360 long already_available;
361 POINTER ptr;
363 if (! use_relocatable_buffers)
364 return (*real_morecore) (size);
366 top = first_bloc ? first_bloc->data : page_break_value;
367 already_available = (char *) top - (char *) virtual_break_value;
369 /* Do we not have enough gap already? */
370 if (size > 0 && already_available < size)
372 /* Get what we need, plus some extra so we can come here less often. */
373 SIZE get = size - already_available + extra_bytes;
375 if (! obtain (get))
376 return 0;
378 if (first_bloc)
379 relocate_some_blocs (first_bloc, first_bloc->data + get);
381 /* Zero out the space we just allocated, to help catch bugs
382 quickly. */
383 bzero (virtual_break_value, get);
385 /* Can we keep extra_bytes of gap while freeing at least extra_bytes? */
386 else if (size < 0 && already_available - size > 2 * extra_bytes)
388 /* Ok, do so. This is how many to free. */
389 SIZE give_back = already_available - size - extra_bytes;
391 if (first_bloc)
392 relocate_some_blocs (first_bloc, first_bloc->data - give_back);
393 relinquish (give_back);
396 ptr = virtual_break_value;
397 virtual_break_value += size;
399 return ptr;
402 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
403 the data is returned in *PTR. PTR is thus the address of some variable
404 which will use the data area.
406 If we can't allocate the necessary memory, set *PTR to zero, and
407 return zero. */
409 POINTER
410 r_alloc (ptr, size)
411 POINTER *ptr;
412 SIZE size;
414 register bloc_ptr new_bloc;
416 if (! r_alloc_initialized)
417 r_alloc_init ();
419 new_bloc = get_bloc (size);
420 if (new_bloc)
422 new_bloc->variable = ptr;
423 *ptr = new_bloc->data;
425 else
426 *ptr = 0;
428 return *ptr;
431 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
432 Store 0 in *PTR to show there's no block allocated. */
434 void
435 r_alloc_free (ptr)
436 register POINTER *ptr;
438 register bloc_ptr dead_bloc;
440 dead_bloc = find_bloc (ptr);
441 if (dead_bloc == NIL_BLOC)
442 abort ();
444 free_bloc (dead_bloc);
445 *ptr = 0;
448 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
449 Do this by shifting all blocks above this one up in memory, unless
450 SIZE is less than or equal to the current bloc size, in which case
451 do nothing.
453 Change *PTR to reflect the new bloc, and return this value.
455 If more memory cannot be allocated, then leave *PTR unchanged, and
456 return zero. */
458 POINTER
459 r_re_alloc (ptr, size)
460 POINTER *ptr;
461 SIZE size;
463 register bloc_ptr bloc;
465 bloc = find_bloc (ptr);
466 if (bloc == NIL_BLOC)
467 abort ();
469 if (size <= bloc->size)
470 /* Wouldn't it be useful to actually resize the bloc here? */
471 return *ptr;
473 if (! obtain (size - bloc->size))
474 return 0;
476 relocate_some_blocs (bloc->next, bloc->data + size);
478 /* Zero out the new space in the bloc, to help catch bugs faster. */
479 bzero (bloc->data + bloc->size, size - bloc->size);
481 /* Indicate that this block has a new size. */
482 bloc->size = size;
484 return *ptr;
487 /* The hook `malloc' uses for the function which gets more space
488 from the system. */
489 extern POINTER (*__morecore) ();
491 /* Intialize various things for memory allocation. */
493 static void
494 r_alloc_init ()
496 if (r_alloc_initialized)
497 return;
499 r_alloc_initialized = 1;
500 real_morecore = __morecore;
501 __morecore = r_alloc_sbrk;
503 virtual_break_value = break_value = (*real_morecore) (0);
504 if (break_value == NIL)
505 abort ();
507 page_size = PAGE;
508 extra_bytes = ROUNDUP (50000);
510 page_break_value = (POINTER) ROUNDUP (break_value);
511 /* Clear the rest of the last page; this memory is in our address space
512 even though it is after the sbrk value. */
513 bzero (break_value, (page_break_value - break_value));
514 use_relocatable_buffers = 1;