1 /* Block-relocating memory allocator.
2 Copyright (C) 1992 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 Only relocate the blocs neccessary for SIZE in r_alloc_sbrk,
23 rather than all of them. This means allowing for a possible
24 hole between the first bloc and the end of malloc storage. */
27 #include "lisp.h" /* Needed for VALBITS. */
29 #include "mem_limits.h"
30 #include "getpagesize.h"
32 #define NIL ((POINTER) 0)
35 /* Declarations for working with the malloc, ralloc, and system breaks. */
37 /* System call to set the break value. */
38 extern POINTER
sbrk ();
40 /* The break value, as seen by malloc (). */
41 static POINTER virtual_break_value
;
43 /* The break value, viewed by the relocatable blocs. */
44 static POINTER break_value
;
46 /* The REAL (i.e., page aligned) break value of the process. */
47 static POINTER page_break_value
;
49 /* Macros for rounding. Note that rounding to any value is possible
50 by changing the definition of PAGE. */
51 #define PAGE (getpagesize ())
52 #define ALIGNED(addr) (((unsigned int) (addr) & (PAGE - 1)) == 0)
53 #define ROUNDUP(size) (((unsigned int) (size) + PAGE) & ~(PAGE - 1))
54 #define ROUND_TO_PAGE(addr) (addr & (~(PAGE - 1)))
55 #define EXCEEDS_ELISP_PTR(ptr) ((unsigned int) (ptr) >> VALBITS)
57 /* Managing "almost out of memory" warnings. */
59 /* Level of warnings issued. */
62 /* Function to call to issue a warning;
63 0 means don't issue them. */
64 static void (*warnfunction
) ();
67 check_memory_limits (address
)
70 SIZE data_size
= address
- data_space_start
;
75 if (data_size
> (lim_data
/ 4) * 3)
78 (*warnfunction
) ("Warning: past 75% of memory limit");
83 if (data_size
> (lim_data
/ 20) * 17)
86 (*warnfunction
) ("Warning: past 85% of memory limit");
91 if (data_size
> (lim_data
/ 20) * 19)
94 (*warnfunction
) ("Warning: past 95% of memory limit");
99 (*warnfunction
) ("Warning: past acceptable memory limits");
103 if (EXCEEDS_ELISP_PTR (address
))
107 /* Functions to get and return memory from the system. */
109 /* Obtain SIZE bytes of space. If enough space is not presently available
110 in our process reserve, (i.e., (page_break_value - break_value)),
111 this means getting more page-aligned space from the system. */
117 SIZE already_available
= page_break_value
- break_value
;
119 if (already_available
< size
)
121 SIZE get
= ROUNDUP (size
- already_available
);
124 check_memory_limits (page_break_value
);
126 if (((int) sbrk (get
)) < 0)
129 page_break_value
+= get
;
135 /* Obtain SIZE bytes of space and return a pointer to the new area. */
138 get_more_space (size
)
141 POINTER ptr
= break_value
;
146 /* Note that SIZE bytes of space have been relinquished by the process.
147 If SIZE is more than a page, return the space to the system. */
153 POINTER new_page_break
;
156 new_page_break
= (POINTER
) ROUNDUP (break_value
);
158 if (new_page_break
!= page_break_value
)
160 if (((int) (sbrk ((char *) new_page_break
161 - (char *) page_break_value
))) < 0)
164 page_break_value
= new_page_break
;
167 /* Zero the space from the end of the "official" break to the actual
168 break, so that bugs show up faster. */
169 bzero (break_value
, ((char *) page_break_value
- (char *) break_value
));
172 /* The meat - allocating, freeing, and relocating blocs. */
174 /* These structures are allocated in the malloc arena.
175 The linked list is kept in order of increasing '.data' members.
176 The data blocks abut each other; if b->next is non-nil, then
177 b->data + b->size == b->next->data. */
187 #define NIL_BLOC ((bloc_ptr) 0)
188 #define BLOC_PTR_SIZE (sizeof (struct bp))
190 /* Head and tail of the list of relocatable blocs. */
191 static bloc_ptr first_bloc
, last_bloc
;
193 /* Declared in dispnew.c, this version doesn't screw up if regions
195 extern void safe_bcopy ();
197 /* Find the bloc referenced by the address in PTR. Returns a pointer
204 register bloc_ptr p
= first_bloc
;
206 while (p
!= NIL_BLOC
)
208 if (p
->variable
== ptr
&& p
->data
== *ptr
)
217 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
218 Returns a pointer to the new bloc. */
224 register bloc_ptr new_bloc
= (bloc_ptr
) malloc (BLOC_PTR_SIZE
);
226 new_bloc
->data
= get_more_space (size
);
227 new_bloc
->size
= size
;
228 new_bloc
->next
= NIL_BLOC
;
229 new_bloc
->variable
= NIL
;
233 new_bloc
->prev
= last_bloc
;
234 last_bloc
->next
= new_bloc
;
235 last_bloc
= new_bloc
;
239 first_bloc
= last_bloc
= new_bloc
;
240 new_bloc
->prev
= NIL_BLOC
;
246 /* Relocate all blocs from BLOC on upward in the list to the zone
247 indicated by ADDRESS. Direction of relocation is determined by
248 the position of ADDRESS relative to BLOC->data.
250 Note that ordering of blocs is not affected by this function. */
253 relocate_some_blocs (bloc
, address
)
258 POINTER data_zone
= bloc
->data
;
259 register SIZE data_zone_size
= 0;
260 register SIZE offset
= bloc
->data
- address
;
261 POINTER new_data_zone
= data_zone
- offset
;
263 for (b
= bloc
; b
!= NIL_BLOC
; b
= b
->next
)
265 data_zone_size
+= b
->size
;
267 *b
->variable
= b
->data
;
270 safe_bcopy (data_zone
, new_data_zone
, data_zone_size
);
273 /* Free BLOC from the chain of blocs, relocating any blocs above it
274 and returning BLOC->size bytes to the free area. */
280 if (bloc
== first_bloc
&& bloc
== last_bloc
)
282 first_bloc
= last_bloc
= NIL_BLOC
;
284 else if (bloc
== last_bloc
)
286 last_bloc
= bloc
->prev
;
287 last_bloc
->next
= NIL_BLOC
;
289 else if (bloc
== first_bloc
)
291 first_bloc
= bloc
->next
;
292 first_bloc
->prev
= NIL_BLOC
;
293 relocate_some_blocs (bloc
->next
, bloc
->data
);
297 bloc
->next
->prev
= bloc
->prev
;
298 bloc
->prev
->next
= bloc
->next
;
299 relocate_some_blocs (bloc
->next
, bloc
->data
);
302 relinquish (bloc
->size
);
306 /* Interface routines. */
308 static int use_relocatable_buffers
;
310 /* Obtain SIZE bytes of storage from the free pool, or the system,
311 as neccessary. If relocatable blocs are in use, this means
320 if (! use_relocatable_buffers
)
328 relocate_some_blocs (first_bloc
, first_bloc
->data
+ size
);
330 /* Zero out the space we just allocated, to help catch bugs
332 bzero (virtual_break_value
, size
);
338 relocate_some_blocs (first_bloc
, first_bloc
->data
+ size
);
342 ptr
= virtual_break_value
;
343 virtual_break_value
+= size
;
347 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
348 the data is returned in *PTR. PTR is thus the address of some variable
349 which will use the data area. */
356 register bloc_ptr new_bloc
;
358 new_bloc
= get_bloc (size
);
359 new_bloc
->variable
= ptr
;
360 *ptr
= new_bloc
->data
;
365 /* Free a bloc of relocatable storage whose data is pointed to by PTR. */
369 register POINTER
*ptr
;
371 register bloc_ptr dead_bloc
;
373 dead_bloc
= find_bloc (ptr
);
374 if (dead_bloc
== NIL_BLOC
)
377 free_bloc (dead_bloc
);
380 /* Given a pointer at address PTR to relocatable data, resize it
381 to SIZE. This is done by obtaining a new block and freeing the
382 old, unless SIZE is less than or equal to the current bloc size,
383 in which case nothing happens and the current value is returned.
385 The contents of PTR is changed to reflect the new bloc, and this
386 value is returned. */
389 r_re_alloc (ptr
, size
)
393 register bloc_ptr old_bloc
, new_bloc
;
395 old_bloc
= find_bloc (ptr
);
396 if (old_bloc
== NIL_BLOC
)
399 if (size
<= old_bloc
->size
)
400 /* Wouldn't it be useful to actually resize the bloc here? */
403 new_bloc
= get_bloc (size
);
404 new_bloc
->variable
= ptr
;
405 safe_bcopy (old_bloc
->data
, new_bloc
->data
, old_bloc
->size
);
406 *ptr
= new_bloc
->data
;
408 free_bloc (old_bloc
);
413 /* The hook `malloc' uses for the function which gets more space
415 extern POINTER (*__morecore
) ();
417 /* A flag to indicate whether we have initialized ralloc yet. For
418 Emacs's sake, please do not make this local to malloc_init; on some
419 machines, the dumping procedure makes all static variables
420 read-only. On these machines, the word static is #defined to be
421 the empty string, meaning that malloc_initialized becomes an
422 automatic variable, and loses its value each time Emacs is started
424 static int malloc_initialized
= 0;
426 /* Intialize various things for memory allocation. */
429 malloc_init (start
, warn_func
)
431 void (*warn_func
) ();
434 data_space_start
= start
;
436 if (malloc_initialized
)
439 malloc_initialized
= 1;
440 __morecore
= r_alloc_sbrk
;
441 virtual_break_value
= break_value
= sbrk (0);
442 page_break_value
= (POINTER
) ROUNDUP (break_value
);
443 bzero (break_value
, (page_break_value
- break_value
));
444 use_relocatable_buffers
= 1;
448 warnfunction
= warn_func
;