1 /* Block-relocating memory allocator.
2 Copyright (C) 1990 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 xterm.h */
29 #include "mem_limits.h"
30 #include "xterm.h" /* Needed for BLOCK_INPUT */
32 #define NIL ((POINTER) 0)
35 /* System call to set the break value. */
36 extern POINTER
sbrk ();
38 /* The break value, as seen by malloc (). */
39 static POINTER virtual_break_value
;
41 /* The break value, viewed by the relocatable blocs. */
42 static POINTER break_value
;
44 /* The REAL (i.e., page aligned) break value of the process. */
45 static POINTER page_break_value
;
47 /* Macros for rounding. Note that rounding to any value is possible
48 by changing the definition of PAGE. */
49 #define PAGE (getpagesize ())
50 #define ALIGNED(addr) (((unsigned int) (addr) & (PAGE - 1)) == 0)
51 #define ROUNDUP(size) (((unsigned int) (size) + PAGE) & ~(PAGE - 1))
52 #define ROUND_TO_PAGE(addr) (addr & (~(PAGE - 1)))
53 #define EXCEEDS_ELISP_PTR(ptr) ((unsigned int) (ptr) >> VALBITS)
55 /* Level of warnings issued. */
58 /* Function to call to issue a warning;
59 0 means don't issue them. */
60 static void (*warnfunction
) ();
63 check_memory_limits (address
)
66 SIZE data_size
= address
- data_space_start
;
71 if (data_size
> (lim_data
/ 4) * 3)
74 (*warnfunction
) ("Warning: past 75% of memory limit");
79 if (data_size
> (lim_data
/ 20) * 17)
82 (*warnfunction
) ("Warning: past 85% of memory limit");
87 if (data_size
> (lim_data
/ 20) * 19)
90 (*warnfunction
) ("Warning: past 95% of memory limit");
95 (*warnfunction
) ("Warning: past acceptable memory limits");
99 if (EXCEEDS_ELISP_PTR (address
))
103 /* Obtain SIZE bytes of space. If enough space is not presently available
104 in our process reserve, (i.e., (page_break_value - break_value)),
105 this means getting more page-aligned space from the system. */
111 SIZE already_available
= page_break_value
- break_value
;
113 if (already_available
< size
)
115 SIZE get
= ROUNDUP (size
);
118 check_memory_limits (page_break_value
);
120 if (((int) sbrk (get
)) < 0)
123 page_break_value
+= get
;
129 /* Obtain SIZE bytes of space and return a pointer to the new area. */
132 get_more_space (size
)
135 POINTER ptr
= break_value
;
140 /* Note that SIZE bytes of space have been relinquished by the process.
141 If SIZE is more than a page, return the space the system. */
147 SIZE page_part
= ROUND_TO_PAGE (size
);
151 if (((int) (sbrk (- page_part
))) < 0)
154 page_break_value
-= page_part
;
158 bzero (break_value
, (size
- page_part
));
170 #define NIL_BLOC ((bloc_ptr) 0)
171 #define BLOC_PTR_SIZE (sizeof (struct bp))
173 /* Head and tail of the list of relocatable blocs. */
174 static bloc_ptr first_bloc
, last_bloc
;
176 /* Declared in dispnew.c, this version dosen't fuck up if regions overlap. */
177 extern void safe_bcopy ();
179 /* Find the bloc reference by the address in PTR. Returns a pointer
186 register bloc_ptr p
= first_bloc
;
188 while (p
!= NIL_BLOC
)
190 if (p
->variable
== ptr
&& p
->data
== *ptr
)
199 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
200 Returns a pointer to the new bloc. */
206 register bloc_ptr new_bloc
= (bloc_ptr
) malloc (BLOC_PTR_SIZE
);
208 new_bloc
->data
= get_more_space (size
);
209 new_bloc
->size
= size
;
210 new_bloc
->next
= NIL_BLOC
;
211 new_bloc
->variable
= NIL
;
215 new_bloc
->prev
= last_bloc
;
216 last_bloc
->next
= new_bloc
;
217 last_bloc
= new_bloc
;
221 first_bloc
= last_bloc
= new_bloc
;
222 new_bloc
->prev
= NIL_BLOC
;
228 /* Relocate all blocs from BLOC on upward in the list to the zone
229 indicated by ADDRESS. Direction of relocation is determined by
230 the position of ADDRESS relative to BLOC->data.
232 Note that ordering of blocs is not affected by this function. */
235 relocate_some_blocs (bloc
, address
)
240 POINTER data_zone
= bloc
->data
;
241 register SIZE data_zone_size
= 0;
242 register SIZE offset
= bloc
->data
- address
;
243 POINTER new_data_zone
= data_zone
- offset
;
245 for (b
= bloc
; b
!= NIL_BLOC
; b
= b
->next
)
247 data_zone_size
+= b
->size
;
249 *b
->variable
= b
->data
;
252 safe_bcopy (data_zone
, new_data_zone
, data_zone_size
);
255 /* Free BLOC from the chain of blocs, relocating any blocs above it
256 and returning BLOC->size bytes to the free area. */
262 if (bloc
== first_bloc
&& bloc
== last_bloc
)
264 first_bloc
= last_bloc
= NIL_BLOC
;
266 else if (bloc
== last_bloc
)
268 last_bloc
= bloc
->prev
;
269 last_bloc
->next
= NIL_BLOC
;
271 else if (bloc
== first_bloc
)
273 first_bloc
= bloc
->next
;
274 first_bloc
->prev
= NIL_BLOC
;
275 relocate_some_blocs (bloc
->next
, bloc
->data
);
279 bloc
->next
->prev
= bloc
->prev
;
280 bloc
->prev
->next
= bloc
->next
;
281 relocate_some_blocs (bloc
->next
, bloc
->data
);
284 relinquish (bloc
->size
);
288 static int use_relocatable_buffers
;
290 /* Obtain SIZE bytes of storage from the free pool, or the system,
291 as neccessary. If relocatable blocs are in use, this means
300 if (! use_relocatable_buffers
)
308 relocate_some_blocs (first_bloc
, first_bloc
->data
+ size
);
309 bzero (virtual_break_value
, size
);
315 relocate_some_blocs (first_bloc
, first_bloc
->data
+ size
);
319 ptr
= virtual_break_value
;
320 virtual_break_value
+= size
;
324 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
325 the data is returned in *PTR. PTR is thus the address of some variable
326 which will use the data area. */
333 register bloc_ptr new_bloc
;
336 new_bloc
= get_bloc (size
);
337 new_bloc
->variable
= ptr
;
338 *ptr
= new_bloc
->data
;
344 /* Free a bloc of relocatable storage whose data is pointed to by PTR. */
348 register POINTER
*ptr
;
350 register bloc_ptr dead_bloc
;
353 dead_bloc
= find_bloc (ptr
);
354 if (dead_bloc
== NIL_BLOC
)
357 free_bloc (dead_bloc
);
361 /* Given a pointer at address PTR to relocatable data, resize it
362 to SIZE. This is done by obtaining a new block and freeing the
363 old, unless SIZE is less than or equal to the current bloc size,
364 in which case nothing happens and the current value is returned.
366 The contents of PTR is changed to reflect the new bloc, and this
367 value is returned. */
370 r_re_alloc (ptr
, size
)
374 register bloc_ptr old_bloc
, new_bloc
;
377 old_bloc
= find_bloc (ptr
);
378 if (old_bloc
== NIL_BLOC
)
381 if (size
<= old_bloc
->size
)
384 new_bloc
= get_bloc (size
);
385 new_bloc
->variable
= ptr
;
386 safe_bcopy (old_bloc
->data
, new_bloc
->data
, old_bloc
->size
);
387 *ptr
= new_bloc
->data
;
389 free_bloc (old_bloc
);
395 /* The hook `malloc' uses for the function which gets more space
397 extern POINTER (*__morecore
) ();
399 /* Intialize various things for memory allocation. */
402 malloc_init (start
, warn_func
)
404 void (*warn_func
) ();
406 static int malloc_initialized
= 0;
409 data_space_start
= start
;
411 if (malloc_initialized
)
414 malloc_initialized
= 1;
415 __morecore
= r_alloc_sbrk
;
416 virtual_break_value
= break_value
= sbrk (0);
417 page_break_value
= (POINTER
) ROUNDUP (break_value
);
418 bzero (break_value
, (page_break_value
- break_value
));
419 use_relocatable_buffers
= 1;
423 warnfunction
= warn_func
;