Mercurial > emacs
annotate src/ralloc.c @ 1398:70d0cd4c5bff
mem_limits.h is now called mem-limits.h.
author | Roland McGrath <roland@gnu.org> |
---|---|
date | Mon, 12 Oct 1992 18:55:05 +0000 |
parents | 92df75f4167f |
children | 9689db083f88 |
rev | line source |
---|---|
118 | 1 /* Block-relocating memory allocator. |
577 | 2 Copyright (C) 1992 Free Software Foundation, Inc. |
118 | 3 |
4 This file is part of GNU Emacs. | |
5 | |
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) | |
9 any later version. | |
10 | |
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. | |
15 | |
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. */ | |
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 | |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
26 #ifdef emacs |
118 | 27 #include "config.h" |
577 | 28 #include "lisp.h" /* Needed for VALBITS. */ |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
29 |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
30 /* Declared in dispnew.c, this version doesn't screw up if regions |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
31 overlap. */ |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
32 extern void safe_bcopy (); |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
33 #endif |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
34 |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
35 #ifndef emacs |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
36 #include <stddef.h> |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
37 typedef size_t SIZE; |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
38 typedef void *POINTER; |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
39 #define EXCEEDS_LISP_PTR(x) 0 |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
40 |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
41 #define safe_bcopy(x, y, z) memmove (y, x, z) |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
42 #endif |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
43 |
118 | 44 #undef NULL |
1398
70d0cd4c5bff
mem_limits.h is now called mem-limits.h.
Roland McGrath <roland@gnu.org>
parents:
1390
diff
changeset
|
45 #include "mem-limits.h" |
621 | 46 #include "getpagesize.h" |
118 | 47 |
48 #define NIL ((POINTER) 0) | |
49 | |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
50 /* A flag to indicate whether we have initialized ralloc yet. For |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
51 Emacs's sake, please do not make this local to malloc_init; on some |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
52 machines, the dumping procedure makes all static variables |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
53 read-only. On these machines, the word static is #defined to be |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
54 the empty string, meaning that r_alloc_initialized becomes an |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
55 automatic variable, and loses its value each time Emacs is started up. */ |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
56 static int r_alloc_initialized = 0; |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
57 |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
58 static void r_alloc_init (); |
118 | 59 |
577 | 60 /* Declarations for working with the malloc, ralloc, and system breaks. */ |
61 | |
118 | 62 /* System call to set the break value. */ |
63 extern POINTER sbrk (); | |
64 | |
65 /* The break value, as seen by malloc (). */ | |
66 static POINTER virtual_break_value; | |
67 | |
68 /* The break value, viewed by the relocatable blocs. */ | |
69 static POINTER break_value; | |
70 | |
71 /* The REAL (i.e., page aligned) break value of the process. */ | |
72 static POINTER page_break_value; | |
73 | |
74 /* Macros for rounding. Note that rounding to any value is possible | |
75 by changing the definition of PAGE. */ | |
76 #define PAGE (getpagesize ()) | |
77 #define ALIGNED(addr) (((unsigned int) (addr) & (PAGE - 1)) == 0) | |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
78 #define ROUNDUP(size) (((unsigned int) (size) + PAGE - 1) & ~(PAGE - 1)) |
118 | 79 #define ROUND_TO_PAGE(addr) (addr & (~(PAGE - 1))) |
80 | |
577 | 81 /* Managing "almost out of memory" warnings. */ |
82 | |
118 | 83 /* Level of warnings issued. */ |
84 static int warnlevel; | |
85 | |
86 /* Function to call to issue a warning; | |
87 0 means don't issue them. */ | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
88 static void (*warn_function) (); |
118 | 89 |
90 static void | |
91 check_memory_limits (address) | |
92 POINTER address; | |
93 { | |
94 SIZE data_size = address - data_space_start; | |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
95 int five_percent = lim_data / 20; |
118 | 96 |
97 switch (warnlevel) | |
98 { | |
99 case 0: | |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
100 if (data_size > five_percent * 15) |
118 | 101 { |
102 warnlevel++; | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
103 (*warn_function) ("Warning: past 75% of memory limit"); |
118 | 104 } |
105 break; | |
106 | |
107 case 1: | |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
108 if (data_size > five_percent * 17) |
118 | 109 { |
110 warnlevel++; | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
111 (*warn_function) ("Warning: past 85% of memory limit"); |
118 | 112 } |
113 break; | |
114 | |
115 case 2: | |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
116 if (data_size > five_percent * 19) |
118 | 117 { |
118 warnlevel++; | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
119 (*warn_function) ("Warning: past 95% of memory limit"); |
118 | 120 } |
121 break; | |
122 | |
123 default: | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
124 (*warn_function) ("Warning: past acceptable memory limits"); |
118 | 125 break; |
126 } | |
127 | |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
128 /* If we go down below 70% full, issue another 75% warning |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
129 when we go up again. */ |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
130 if (data_size < five_percent * 14) |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
131 warnlevel = 0; |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
132 /* If we go down below 80% full, issue another 85% warning |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
133 when we go up again. */ |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
134 else if (warnlevel > 1 && data_size < five_percent * 16) |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
135 warnlevel = 1; |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
136 /* If we go down below 90% full, issue another 95% warning |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
137 when we go up again. */ |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
138 else if (warnlevel > 2 && data_size < five_percent * 18) |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
139 warnlevel = 2; |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
140 |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
141 if (EXCEEDS_LISP_PTR (address)) |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
142 memory_full (); |
118 | 143 } |
144 | |
577 | 145 /* Functions to get and return memory from the system. */ |
146 | |
118 | 147 /* Obtain SIZE bytes of space. If enough space is not presently available |
148 in our process reserve, (i.e., (page_break_value - break_value)), | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
149 this means getting more page-aligned space from the system. |
118 | 150 |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
151 Return non-zero if all went well, or zero if we couldn't allocate |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
152 the memory. */ |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
153 static int |
118 | 154 obtain (size) |
155 SIZE size; | |
156 { | |
157 SIZE already_available = page_break_value - break_value; | |
158 | |
159 if (already_available < size) | |
160 { | |
577 | 161 SIZE get = ROUNDUP (size - already_available); |
118 | 162 |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
163 if (warn_function) |
118 | 164 check_memory_limits (page_break_value); |
165 | |
166 if (((int) sbrk (get)) < 0) | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
167 return 0; |
118 | 168 |
169 page_break_value += get; | |
170 } | |
171 | |
172 break_value += size; | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
173 |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
174 return 1; |
118 | 175 } |
176 | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
177 /* Obtain SIZE bytes of space and return a pointer to the new area. |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
178 If we could not allocate the space, return zero. */ |
118 | 179 |
180 static POINTER | |
181 get_more_space (size) | |
182 SIZE size; | |
183 { | |
184 POINTER ptr = break_value; | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
185 if (obtain (size)) |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
186 return ptr; |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
187 else |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
188 return 0; |
118 | 189 } |
190 | |
191 /* Note that SIZE bytes of space have been relinquished by the process. | |
577 | 192 If SIZE is more than a page, return the space to the system. */ |
118 | 193 |
194 static void | |
195 relinquish (size) | |
196 SIZE size; | |
197 { | |
577 | 198 POINTER new_page_break; |
118 | 199 |
577 | 200 break_value -= size; |
201 new_page_break = (POINTER) ROUNDUP (break_value); | |
202 | |
203 if (new_page_break != page_break_value) | |
118 | 204 { |
577 | 205 if (((int) (sbrk ((char *) new_page_break |
206 - (char *) page_break_value))) < 0) | |
118 | 207 abort (); |
208 | |
577 | 209 page_break_value = new_page_break; |
118 | 210 } |
211 | |
577 | 212 /* Zero the space from the end of the "official" break to the actual |
213 break, so that bugs show up faster. */ | |
214 bzero (break_value, ((char *) page_break_value - (char *) break_value)); | |
118 | 215 } |
216 | |
577 | 217 /* The meat - allocating, freeing, and relocating blocs. */ |
218 | |
219 /* These structures are allocated in the malloc arena. | |
220 The linked list is kept in order of increasing '.data' members. | |
221 The data blocks abut each other; if b->next is non-nil, then | |
222 b->data + b->size == b->next->data. */ | |
118 | 223 typedef struct bp |
224 { | |
225 struct bp *next; | |
226 struct bp *prev; | |
227 POINTER *variable; | |
228 POINTER data; | |
229 SIZE size; | |
230 } *bloc_ptr; | |
231 | |
232 #define NIL_BLOC ((bloc_ptr) 0) | |
233 #define BLOC_PTR_SIZE (sizeof (struct bp)) | |
234 | |
235 /* Head and tail of the list of relocatable blocs. */ | |
236 static bloc_ptr first_bloc, last_bloc; | |
237 | |
577 | 238 /* Find the bloc referenced by the address in PTR. Returns a pointer |
118 | 239 to that block. */ |
240 | |
241 static bloc_ptr | |
242 find_bloc (ptr) | |
243 POINTER *ptr; | |
244 { | |
245 register bloc_ptr p = first_bloc; | |
246 | |
247 while (p != NIL_BLOC) | |
248 { | |
249 if (p->variable == ptr && p->data == *ptr) | |
250 return p; | |
251 | |
252 p = p->next; | |
253 } | |
254 | |
255 return p; | |
256 } | |
257 | |
258 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs. | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
259 Returns a pointer to the new bloc, or zero if we couldn't allocate |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
260 memory for the new block. */ |
118 | 261 |
262 static bloc_ptr | |
263 get_bloc (size) | |
264 SIZE size; | |
265 { | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
266 register bloc_ptr new_bloc; |
118 | 267 |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
268 if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE)) |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
269 || ! (new_bloc->data = get_more_space (size))) |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
270 { |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
271 if (new_bloc) |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
272 free (new_bloc); |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
273 |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
274 return 0; |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
275 } |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
276 |
118 | 277 new_bloc->size = size; |
278 new_bloc->next = NIL_BLOC; | |
1013
6bf2c4766d4c
* ralloc.c (get_bloc): When initializing new_bloc->variable, cast
Jim Blandy <jimb@redhat.com>
parents:
734
diff
changeset
|
279 new_bloc->variable = (POINTER *) NIL; |
118 | 280 |
281 if (first_bloc) | |
282 { | |
283 new_bloc->prev = last_bloc; | |
284 last_bloc->next = new_bloc; | |
285 last_bloc = new_bloc; | |
286 } | |
287 else | |
288 { | |
289 first_bloc = last_bloc = new_bloc; | |
290 new_bloc->prev = NIL_BLOC; | |
291 } | |
292 | |
293 return new_bloc; | |
294 } | |
295 | |
296 /* Relocate all blocs from BLOC on upward in the list to the zone | |
297 indicated by ADDRESS. Direction of relocation is determined by | |
298 the position of ADDRESS relative to BLOC->data. | |
299 | |
300 Note that ordering of blocs is not affected by this function. */ | |
301 | |
302 static void | |
303 relocate_some_blocs (bloc, address) | |
304 bloc_ptr bloc; | |
305 POINTER address; | |
306 { | |
307 register bloc_ptr b; | |
308 POINTER data_zone = bloc->data; | |
309 register SIZE data_zone_size = 0; | |
310 register SIZE offset = bloc->data - address; | |
311 POINTER new_data_zone = data_zone - offset; | |
312 | |
313 for (b = bloc; b != NIL_BLOC; b = b->next) | |
314 { | |
315 data_zone_size += b->size; | |
316 b->data -= offset; | |
317 *b->variable = b->data; | |
318 } | |
319 | |
320 safe_bcopy (data_zone, new_data_zone, data_zone_size); | |
321 } | |
322 | |
323 /* Free BLOC from the chain of blocs, relocating any blocs above it | |
324 and returning BLOC->size bytes to the free area. */ | |
325 | |
326 static void | |
327 free_bloc (bloc) | |
328 bloc_ptr bloc; | |
329 { | |
330 if (bloc == first_bloc && bloc == last_bloc) | |
331 { | |
332 first_bloc = last_bloc = NIL_BLOC; | |
333 } | |
334 else if (bloc == last_bloc) | |
335 { | |
336 last_bloc = bloc->prev; | |
337 last_bloc->next = NIL_BLOC; | |
338 } | |
339 else if (bloc == first_bloc) | |
340 { | |
341 first_bloc = bloc->next; | |
342 first_bloc->prev = NIL_BLOC; | |
343 relocate_some_blocs (bloc->next, bloc->data); | |
344 } | |
345 else | |
346 { | |
347 bloc->next->prev = bloc->prev; | |
348 bloc->prev->next = bloc->next; | |
349 relocate_some_blocs (bloc->next, bloc->data); | |
350 } | |
351 | |
352 relinquish (bloc->size); | |
353 free (bloc); | |
354 } | |
355 | |
577 | 356 /* Interface routines. */ |
357 | |
118 | 358 static int use_relocatable_buffers; |
359 | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
360 /* Obtain SIZE bytes of storage from the free pool, or the system, as |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
361 necessary. If relocatable blocs are in use, this means relocating |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
362 them. This function gets plugged into the GNU malloc's __morecore |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
363 hook. |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
364 |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
365 If we're out of memory, we should return zero, to imitate the other |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
366 __morecore hook values - in particular, __default_morecore in the |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
367 GNU malloc package. */ |
118 | 368 |
369 POINTER | |
370 r_alloc_sbrk (size) | |
371 long size; | |
372 { | |
373 POINTER ptr; | |
374 | |
375 if (! use_relocatable_buffers) | |
376 return sbrk (size); | |
377 | |
378 if (size > 0) | |
379 { | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
380 if (! obtain (size)) |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
381 return 0; |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
382 |
118 | 383 if (first_bloc) |
384 { | |
385 relocate_some_blocs (first_bloc, first_bloc->data + size); | |
577 | 386 |
387 /* Zero out the space we just allocated, to help catch bugs | |
388 quickly. */ | |
118 | 389 bzero (virtual_break_value, size); |
390 } | |
391 } | |
392 else if (size < 0) | |
393 { | |
394 if (first_bloc) | |
395 relocate_some_blocs (first_bloc, first_bloc->data + size); | |
396 relinquish (- size); | |
397 } | |
398 | |
399 ptr = virtual_break_value; | |
400 virtual_break_value += size; | |
401 return ptr; | |
402 } | |
403 | |
404 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to | |
405 the data is returned in *PTR. PTR is thus the address of some variable | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
406 which will use the data area. |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
407 |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
408 If we can't allocate the necessary memory, set *PTR to zero, and |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
409 return zero. */ |
118 | 410 |
411 POINTER | |
412 r_alloc (ptr, size) | |
413 POINTER *ptr; | |
414 SIZE size; | |
415 { | |
416 register bloc_ptr new_bloc; | |
417 | |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
418 if (! r_alloc_initialized) |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
419 r_alloc_init (); |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
420 |
118 | 421 new_bloc = get_bloc (size); |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
422 if (new_bloc) |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
423 { |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
424 new_bloc->variable = ptr; |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
425 *ptr = new_bloc->data; |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
426 } |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
427 else |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
428 *ptr = 0; |
118 | 429 |
430 return *ptr; | |
431 } | |
432 | |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
433 /* Free a bloc of relocatable storage whose data is pointed to by PTR. |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
434 Store 0 in *PTR to show there's no block allocated. */ |
118 | 435 |
436 void | |
437 r_alloc_free (ptr) | |
438 register POINTER *ptr; | |
439 { | |
440 register bloc_ptr dead_bloc; | |
441 | |
442 dead_bloc = find_bloc (ptr); | |
443 if (dead_bloc == NIL_BLOC) | |
444 abort (); | |
445 | |
446 free_bloc (dead_bloc); | |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
447 *ptr = 0; |
118 | 448 } |
449 | |
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
450 /* Given a pointer at address PTR to relocatable data, resize it to SIZE. |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
451 Do this by shifting all blocks above this one up in memory, unless |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
452 SIZE is less than or equal to the current bloc size, in which case |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
453 do nothing. |
118 | 454 |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
455 Change *PTR to reflect the new bloc, and return this value. |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
456 |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
457 If more memory cannot be allocated, then leave *PTR unchanged, and |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
458 return zero. */ |
118 | 459 |
460 POINTER | |
461 r_re_alloc (ptr, size) | |
462 POINTER *ptr; | |
463 SIZE size; | |
464 { | |
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
465 register bloc_ptr bloc; |
118 | 466 |
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
467 bloc = find_bloc (ptr); |
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
468 if (bloc == NIL_BLOC) |
118 | 469 abort (); |
470 | |
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
471 if (size <= bloc->size) |
577 | 472 /* Wouldn't it be useful to actually resize the bloc here? */ |
118 | 473 return *ptr; |
474 | |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
475 if (! obtain (size - bloc->size)) |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
476 return 0; |
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
477 |
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
478 relocate_some_blocs (bloc->next, bloc->data + size); |
118 | 479 |
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
480 /* Zero out the new space in the bloc, to help catch bugs faster. */ |
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
481 bzero (bloc->data + bloc->size, size - bloc->size); |
1121 | 482 |
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
483 /* Indicate that this block has a new size. */ |
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
484 bloc->size = size; |
118 | 485 |
486 return *ptr; | |
487 } | |
488 | |
489 /* The hook `malloc' uses for the function which gets more space | |
490 from the system. */ | |
491 extern POINTER (*__morecore) (); | |
492 | |
493 /* Intialize various things for memory allocation. */ | |
494 | |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
495 static void |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
496 r_alloc_init () |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
497 { |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
498 if (r_alloc_initialized) |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
499 return; |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
500 |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
501 r_alloc_initialized = 1; |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
502 __morecore = r_alloc_sbrk; |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
503 |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
504 virtual_break_value = break_value = sbrk (0); |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
505 if (break_value == (POINTER)NULL) |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
506 abort (); |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
507 #if 0 /* The following is unreasonable because warn_func may be 0. */ |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
508 (*warn_func)("memory initialization got 0 from sbrk(0)."); |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
509 #endif |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
510 |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
511 page_break_value = (POINTER) ROUNDUP (break_value); |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
512 /* Clear the rest of the last page; this memory is in our address space |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
513 even though it is after the sbrk value. */ |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
514 bzero (break_value, (page_break_value - break_value)); |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
515 use_relocatable_buffers = 1; |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
516 |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
517 lim_data = 0; |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
518 warnlevel = 0; |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
519 |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
520 get_lim_data (); |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
521 } |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
522 |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
523 /* This is the name Emacs expects to call. */ |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
524 |
118 | 525 void |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
526 memory_warnings (start, warn_func) |
118 | 527 POINTER start; |
528 void (*warn_func) (); | |
529 { | |
530 if (start) | |
531 data_space_start = start; | |
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
532 else |
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
533 data_space_start = start_of_data (); |
1013
6bf2c4766d4c
* ralloc.c (get_bloc): When initializing new_bloc->variable, cast
Jim Blandy <jimb@redhat.com>
parents:
734
diff
changeset
|
534 |
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
535 warn_function = warn_func; |
118 | 536 } |