* process.c (process_send_signal): Don't send SIGTSTP if the
[bpt/emacs.git] / src / ralloc.c
CommitLineData
dcfdbac7 1/* Block-relocating memory allocator.
956ace37 2 Copyright (C) 1992 Free Software Foundation, Inc.
dcfdbac7
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20/* NOTES:
21
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. */
25
26#include "config.h"
956ace37 27#include "lisp.h" /* Needed for VALBITS. */
dcfdbac7
JB
28#undef NULL
29#include "mem_limits.h"
4746118a 30#include "getpagesize.h"
dcfdbac7
JB
31
32#define NIL ((POINTER) 0)
33
34\f
956ace37
JB
35/* Declarations for working with the malloc, ralloc, and system breaks. */
36
dcfdbac7
JB
37/* System call to set the break value. */
38extern POINTER sbrk ();
39
40/* The break value, as seen by malloc (). */
41static POINTER virtual_break_value;
42
43/* The break value, viewed by the relocatable blocs. */
44static POINTER break_value;
45
46/* The REAL (i.e., page aligned) break value of the process. */
47static POINTER page_break_value;
48
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)))
dcfdbac7 55\f
956ace37
JB
56/* Managing "almost out of memory" warnings. */
57
dcfdbac7
JB
58/* Level of warnings issued. */
59static int warnlevel;
60
61/* Function to call to issue a warning;
62 0 means don't issue them. */
63static void (*warnfunction) ();
64
65static void
66check_memory_limits (address)
67 POINTER address;
68{
69 SIZE data_size = address - data_space_start;
70
71 switch (warnlevel)
72 {
73 case 0:
74 if (data_size > (lim_data / 4) * 3)
75 {
76 warnlevel++;
77 (*warnfunction) ("Warning: past 75% of memory limit");
78 }
79 break;
80
81 case 1:
82 if (data_size > (lim_data / 20) * 17)
83 {
84 warnlevel++;
85 (*warnfunction) ("Warning: past 85% of memory limit");
86 }
87 break;
88
89 case 2:
90 if (data_size > (lim_data / 20) * 19)
91 {
92 warnlevel++;
93 (*warnfunction) ("Warning: past 95% of memory limit");
94 }
95 break;
96
97 default:
98 (*warnfunction) ("Warning: past acceptable memory limits");
99 break;
100 }
101
102 if (EXCEEDS_ELISP_PTR (address))
265a9e55 103 memory_full ();
dcfdbac7
JB
104}
105\f
956ace37
JB
106/* Functions to get and return memory from the system. */
107
dcfdbac7
JB
108/* Obtain SIZE bytes of space. If enough space is not presently available
109 in our process reserve, (i.e., (page_break_value - break_value)),
110 this means getting more page-aligned space from the system. */
111
112static void
113obtain (size)
114 SIZE size;
115{
116 SIZE already_available = page_break_value - break_value;
117
118 if (already_available < size)
119 {
956ace37 120 SIZE get = ROUNDUP (size - already_available);
dcfdbac7
JB
121
122 if (warnfunction)
123 check_memory_limits (page_break_value);
124
125 if (((int) sbrk (get)) < 0)
126 abort ();
127
128 page_break_value += get;
129 }
130
131 break_value += size;
132}
133
134/* Obtain SIZE bytes of space and return a pointer to the new area. */
135
136static POINTER
137get_more_space (size)
138 SIZE size;
139{
140 POINTER ptr = break_value;
141 obtain (size);
142 return ptr;
143}
144
145/* Note that SIZE bytes of space have been relinquished by the process.
956ace37 146 If SIZE is more than a page, return the space to the system. */
dcfdbac7
JB
147
148static void
149relinquish (size)
150 SIZE size;
151{
956ace37 152 POINTER new_page_break;
dcfdbac7 153
956ace37
JB
154 break_value -= size;
155 new_page_break = (POINTER) ROUNDUP (break_value);
156
157 if (new_page_break != page_break_value)
dcfdbac7 158 {
956ace37
JB
159 if (((int) (sbrk ((char *) new_page_break
160 - (char *) page_break_value))) < 0)
dcfdbac7
JB
161 abort ();
162
956ace37 163 page_break_value = new_page_break;
dcfdbac7
JB
164 }
165
956ace37
JB
166 /* Zero the space from the end of the "official" break to the actual
167 break, so that bugs show up faster. */
168 bzero (break_value, ((char *) page_break_value - (char *) break_value));
dcfdbac7
JB
169}
170\f
956ace37
JB
171/* The meat - allocating, freeing, and relocating blocs. */
172
173/* These structures are allocated in the malloc arena.
174 The linked list is kept in order of increasing '.data' members.
175 The data blocks abut each other; if b->next is non-nil, then
176 b->data + b->size == b->next->data. */
dcfdbac7
JB
177typedef struct bp
178{
179 struct bp *next;
180 struct bp *prev;
181 POINTER *variable;
182 POINTER data;
183 SIZE size;
184} *bloc_ptr;
185
186#define NIL_BLOC ((bloc_ptr) 0)
187#define BLOC_PTR_SIZE (sizeof (struct bp))
188
189/* Head and tail of the list of relocatable blocs. */
190static bloc_ptr first_bloc, last_bloc;
191
956ace37
JB
192/* Declared in dispnew.c, this version doesn't screw up if regions
193 overlap. */
dcfdbac7
JB
194extern void safe_bcopy ();
195
956ace37 196/* Find the bloc referenced by the address in PTR. Returns a pointer
dcfdbac7
JB
197 to that block. */
198
199static bloc_ptr
200find_bloc (ptr)
201 POINTER *ptr;
202{
203 register bloc_ptr p = first_bloc;
204
205 while (p != NIL_BLOC)
206 {
207 if (p->variable == ptr && p->data == *ptr)
208 return p;
209
210 p = p->next;
211 }
212
213 return p;
214}
215
216/* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
217 Returns a pointer to the new bloc. */
218
219static bloc_ptr
220get_bloc (size)
221 SIZE size;
222{
223 register bloc_ptr new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE);
224
225 new_bloc->data = get_more_space (size);
226 new_bloc->size = size;
227 new_bloc->next = NIL_BLOC;
228 new_bloc->variable = NIL;
229
230 if (first_bloc)
231 {
232 new_bloc->prev = last_bloc;
233 last_bloc->next = new_bloc;
234 last_bloc = new_bloc;
235 }
236 else
237 {
238 first_bloc = last_bloc = new_bloc;
239 new_bloc->prev = NIL_BLOC;
240 }
241
242 return new_bloc;
243}
244
245/* Relocate all blocs from BLOC on upward in the list to the zone
246 indicated by ADDRESS. Direction of relocation is determined by
247 the position of ADDRESS relative to BLOC->data.
248
249 Note that ordering of blocs is not affected by this function. */
250
251static void
252relocate_some_blocs (bloc, address)
253 bloc_ptr bloc;
254 POINTER address;
255{
256 register bloc_ptr b;
257 POINTER data_zone = bloc->data;
258 register SIZE data_zone_size = 0;
259 register SIZE offset = bloc->data - address;
260 POINTER new_data_zone = data_zone - offset;
261
262 for (b = bloc; b != NIL_BLOC; b = b->next)
263 {
264 data_zone_size += b->size;
265 b->data -= offset;
266 *b->variable = b->data;
267 }
268
269 safe_bcopy (data_zone, new_data_zone, data_zone_size);
270}
271
272/* Free BLOC from the chain of blocs, relocating any blocs above it
273 and returning BLOC->size bytes to the free area. */
274
275static void
276free_bloc (bloc)
277 bloc_ptr bloc;
278{
279 if (bloc == first_bloc && bloc == last_bloc)
280 {
281 first_bloc = last_bloc = NIL_BLOC;
282 }
283 else if (bloc == last_bloc)
284 {
285 last_bloc = bloc->prev;
286 last_bloc->next = NIL_BLOC;
287 }
288 else if (bloc == first_bloc)
289 {
290 first_bloc = bloc->next;
291 first_bloc->prev = NIL_BLOC;
292 relocate_some_blocs (bloc->next, bloc->data);
293 }
294 else
295 {
296 bloc->next->prev = bloc->prev;
297 bloc->prev->next = bloc->next;
298 relocate_some_blocs (bloc->next, bloc->data);
299 }
300
301 relinquish (bloc->size);
302 free (bloc);
303}
304\f
956ace37
JB
305/* Interface routines. */
306
dcfdbac7
JB
307static int use_relocatable_buffers;
308
309/* Obtain SIZE bytes of storage from the free pool, or the system,
310 as neccessary. If relocatable blocs are in use, this means
311 relocating them. */
312
313POINTER
314r_alloc_sbrk (size)
315 long size;
316{
317 POINTER ptr;
318
319 if (! use_relocatable_buffers)
320 return sbrk (size);
321
322 if (size > 0)
323 {
324 obtain (size);
325 if (first_bloc)
326 {
327 relocate_some_blocs (first_bloc, first_bloc->data + size);
956ace37
JB
328
329 /* Zero out the space we just allocated, to help catch bugs
330 quickly. */
dcfdbac7
JB
331 bzero (virtual_break_value, size);
332 }
333 }
334 else if (size < 0)
335 {
336 if (first_bloc)
337 relocate_some_blocs (first_bloc, first_bloc->data + size);
338 relinquish (- size);
339 }
340
341 ptr = virtual_break_value;
342 virtual_break_value += size;
343 return ptr;
344}
345
346/* Allocate a relocatable bloc of storage of size SIZE. A pointer to
347 the data is returned in *PTR. PTR is thus the address of some variable
348 which will use the data area. */
349
350POINTER
351r_alloc (ptr, size)
352 POINTER *ptr;
353 SIZE size;
354{
355 register bloc_ptr new_bloc;
356
dcfdbac7
JB
357 new_bloc = get_bloc (size);
358 new_bloc->variable = ptr;
359 *ptr = new_bloc->data;
dcfdbac7
JB
360
361 return *ptr;
362}
363
364/* Free a bloc of relocatable storage whose data is pointed to by PTR. */
365
366void
367r_alloc_free (ptr)
368 register POINTER *ptr;
369{
370 register bloc_ptr dead_bloc;
371
dcfdbac7
JB
372 dead_bloc = find_bloc (ptr);
373 if (dead_bloc == NIL_BLOC)
374 abort ();
375
376 free_bloc (dead_bloc);
dcfdbac7
JB
377}
378
379/* Given a pointer at address PTR to relocatable data, resize it
380 to SIZE. This is done by obtaining a new block and freeing the
381 old, unless SIZE is less than or equal to the current bloc size,
382 in which case nothing happens and the current value is returned.
383
384 The contents of PTR is changed to reflect the new bloc, and this
385 value is returned. */
386
387POINTER
388r_re_alloc (ptr, size)
389 POINTER *ptr;
390 SIZE size;
391{
392 register bloc_ptr old_bloc, new_bloc;
393
dcfdbac7
JB
394 old_bloc = find_bloc (ptr);
395 if (old_bloc == NIL_BLOC)
396 abort ();
397
398 if (size <= old_bloc->size)
956ace37 399 /* Wouldn't it be useful to actually resize the bloc here? */
dcfdbac7
JB
400 return *ptr;
401
402 new_bloc = get_bloc (size);
403 new_bloc->variable = ptr;
404 safe_bcopy (old_bloc->data, new_bloc->data, old_bloc->size);
405 *ptr = new_bloc->data;
406
407 free_bloc (old_bloc);
dcfdbac7
JB
408
409 return *ptr;
410}
411\f
412/* The hook `malloc' uses for the function which gets more space
413 from the system. */
414extern POINTER (*__morecore) ();
415
956ace37
JB
416/* A flag to indicate whether we have initialized ralloc yet. For
417 Emacs's sake, please do not make this local to malloc_init; on some
418 machines, the dumping procedure makes all static variables
419 read-only. On these machines, the word static is #defined to be
420 the empty string, meaning that malloc_initialized becomes an
421 automatic variable, and loses its value each time Emacs is started
422 up. */
423static int malloc_initialized = 0;
424
dcfdbac7
JB
425/* Intialize various things for memory allocation. */
426
427void
428malloc_init (start, warn_func)
429 POINTER start;
430 void (*warn_func) ();
431{
dcfdbac7
JB
432 if (start)
433 data_space_start = start;
434
435 if (malloc_initialized)
436 return;
437
438 malloc_initialized = 1;
439 __morecore = r_alloc_sbrk;
440 virtual_break_value = break_value = sbrk (0);
441 page_break_value = (POINTER) ROUNDUP (break_value);
442 bzero (break_value, (page_break_value - break_value));
443 use_relocatable_buffers = 1;
444
445 lim_data = 0;
446 warnlevel = 0;
447 warnfunction = warn_func;
448
449 get_lim_data ();
450}