Mercurial > emacs
annotate src/ralloc.c @ 1401:9689db083f88
(sbrk): Removed decl.
(real_morecore): New static variable.
(warnlevel, warn_function, check_memory_limits): Removed.
(obtain): Don't call check_memory_limits.
(obtain, relinquish, r_alloc_sbrk): Use (*real_morecore) in place of sbrk;
it returns 0 for errors, not -1.
(r_alloc_init): Set real_morecore to old value of __morecore.
Don't initialize lim_data or warnlevel, and don't call get_lim_data.
(memory_warnings): Function removed.
| author | Roland McGrath <roland@gnu.org> |
|---|---|
| date | Mon, 12 Oct 1992 21:07:25 +0000 |
| parents | 70d0cd4c5bff |
| children | f0ea279194f8 |
| 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 | |
| 1401 | 62 /* Function to set the real break value. */ |
| 63 static POINTER (*real_morecore) (); | |
| 118 | 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 /* Functions to get and return memory from the system. */ |
| 82 | |
| 118 | 83 /* Obtain SIZE bytes of space. If enough space is not presently available |
| 84 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
|
85 this means getting more page-aligned space from the system. |
| 118 | 86 |
|
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
87 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
|
88 the memory. */ |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
89 static int |
| 118 | 90 obtain (size) |
| 91 SIZE size; | |
| 92 { | |
| 93 SIZE already_available = page_break_value - break_value; | |
| 94 | |
| 95 if (already_available < size) | |
| 96 { | |
| 577 | 97 SIZE get = ROUNDUP (size - already_available); |
| 118 | 98 |
| 1401 | 99 if ((*real_morecore) (get) == 0) |
|
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
100 return 0; |
| 118 | 101 |
| 102 page_break_value += get; | |
| 103 } | |
| 104 | |
| 105 break_value += size; | |
|
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
106 |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
107 return 1; |
| 118 | 108 } |
| 109 | |
|
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
110 /* 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
|
111 If we could not allocate the space, return zero. */ |
| 118 | 112 |
| 113 static POINTER | |
| 114 get_more_space (size) | |
| 115 SIZE size; | |
| 116 { | |
| 117 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
|
118 if (obtain (size)) |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
119 return ptr; |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
120 else |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
121 return 0; |
| 118 | 122 } |
| 123 | |
| 124 /* Note that SIZE bytes of space have been relinquished by the process. | |
| 577 | 125 If SIZE is more than a page, return the space to the system. */ |
| 118 | 126 |
| 127 static void | |
| 128 relinquish (size) | |
| 129 SIZE size; | |
| 130 { | |
| 577 | 131 POINTER new_page_break; |
| 118 | 132 |
| 577 | 133 break_value -= size; |
| 134 new_page_break = (POINTER) ROUNDUP (break_value); | |
| 135 | |
| 136 if (new_page_break != page_break_value) | |
| 118 | 137 { |
| 1401 | 138 if ((*real_morecore) ((char *) new_page_break |
| 139 - (char *) page_break_value) == 0) | |
| 118 | 140 abort (); |
| 141 | |
| 577 | 142 page_break_value = new_page_break; |
| 118 | 143 } |
| 144 | |
| 577 | 145 /* Zero the space from the end of the "official" break to the actual |
| 146 break, so that bugs show up faster. */ | |
| 147 bzero (break_value, ((char *) page_break_value - (char *) break_value)); | |
| 118 | 148 } |
| 149 | |
| 577 | 150 /* The meat - allocating, freeing, and relocating blocs. */ |
| 151 | |
| 152 /* These structures are allocated in the malloc arena. | |
| 153 The linked list is kept in order of increasing '.data' members. | |
| 154 The data blocks abut each other; if b->next is non-nil, then | |
| 155 b->data + b->size == b->next->data. */ | |
| 118 | 156 typedef struct bp |
| 157 { | |
| 158 struct bp *next; | |
| 159 struct bp *prev; | |
| 160 POINTER *variable; | |
| 161 POINTER data; | |
| 162 SIZE size; | |
| 163 } *bloc_ptr; | |
| 164 | |
| 165 #define NIL_BLOC ((bloc_ptr) 0) | |
| 166 #define BLOC_PTR_SIZE (sizeof (struct bp)) | |
| 167 | |
| 168 /* Head and tail of the list of relocatable blocs. */ | |
| 169 static bloc_ptr first_bloc, last_bloc; | |
| 170 | |
| 577 | 171 /* Find the bloc referenced by the address in PTR. Returns a pointer |
| 118 | 172 to that block. */ |
| 173 | |
| 174 static bloc_ptr | |
| 175 find_bloc (ptr) | |
| 176 POINTER *ptr; | |
| 177 { | |
| 178 register bloc_ptr p = first_bloc; | |
| 179 | |
| 180 while (p != NIL_BLOC) | |
| 181 { | |
| 182 if (p->variable == ptr && p->data == *ptr) | |
| 183 return p; | |
| 184 | |
| 185 p = p->next; | |
| 186 } | |
| 187 | |
| 188 return p; | |
| 189 } | |
| 190 | |
| 191 /* 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
|
192 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
|
193 memory for the new block. */ |
| 118 | 194 |
| 195 static bloc_ptr | |
| 196 get_bloc (size) | |
| 197 SIZE size; | |
| 198 { | |
|
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
199 register bloc_ptr new_bloc; |
| 118 | 200 |
|
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
201 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
|
202 || ! (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
|
203 { |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
204 if (new_bloc) |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
205 free (new_bloc); |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
206 |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
207 return 0; |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
208 } |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
209 |
| 118 | 210 new_bloc->size = size; |
| 211 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
|
212 new_bloc->variable = (POINTER *) NIL; |
| 118 | 213 |
| 214 if (first_bloc) | |
| 215 { | |
| 216 new_bloc->prev = last_bloc; | |
| 217 last_bloc->next = new_bloc; | |
| 218 last_bloc = new_bloc; | |
| 219 } | |
| 220 else | |
| 221 { | |
| 222 first_bloc = last_bloc = new_bloc; | |
| 223 new_bloc->prev = NIL_BLOC; | |
| 224 } | |
| 225 | |
| 226 return new_bloc; | |
| 227 } | |
| 228 | |
| 229 /* Relocate all blocs from BLOC on upward in the list to the zone | |
| 230 indicated by ADDRESS. Direction of relocation is determined by | |
| 231 the position of ADDRESS relative to BLOC->data. | |
| 232 | |
| 233 Note that ordering of blocs is not affected by this function. */ | |
| 234 | |
| 235 static void | |
| 236 relocate_some_blocs (bloc, address) | |
| 237 bloc_ptr bloc; | |
| 238 POINTER address; | |
| 239 { | |
| 240 register bloc_ptr b; | |
| 241 POINTER data_zone = bloc->data; | |
| 242 register SIZE data_zone_size = 0; | |
| 243 register SIZE offset = bloc->data - address; | |
| 244 POINTER new_data_zone = data_zone - offset; | |
| 245 | |
| 246 for (b = bloc; b != NIL_BLOC; b = b->next) | |
| 247 { | |
| 248 data_zone_size += b->size; | |
| 249 b->data -= offset; | |
| 250 *b->variable = b->data; | |
| 251 } | |
| 252 | |
| 253 safe_bcopy (data_zone, new_data_zone, data_zone_size); | |
| 254 } | |
| 255 | |
| 256 /* Free BLOC from the chain of blocs, relocating any blocs above it | |
| 257 and returning BLOC->size bytes to the free area. */ | |
| 258 | |
| 259 static void | |
| 260 free_bloc (bloc) | |
| 261 bloc_ptr bloc; | |
| 262 { | |
| 263 if (bloc == first_bloc && bloc == last_bloc) | |
| 264 { | |
| 265 first_bloc = last_bloc = NIL_BLOC; | |
| 266 } | |
| 267 else if (bloc == last_bloc) | |
| 268 { | |
| 269 last_bloc = bloc->prev; | |
| 270 last_bloc->next = NIL_BLOC; | |
| 271 } | |
| 272 else if (bloc == first_bloc) | |
| 273 { | |
| 274 first_bloc = bloc->next; | |
| 275 first_bloc->prev = NIL_BLOC; | |
| 276 relocate_some_blocs (bloc->next, bloc->data); | |
| 277 } | |
| 278 else | |
| 279 { | |
| 280 bloc->next->prev = bloc->prev; | |
| 281 bloc->prev->next = bloc->next; | |
| 282 relocate_some_blocs (bloc->next, bloc->data); | |
| 283 } | |
| 284 | |
| 285 relinquish (bloc->size); | |
| 286 free (bloc); | |
| 287 } | |
| 288 | |
| 577 | 289 /* Interface routines. */ |
| 290 | |
| 118 | 291 static int use_relocatable_buffers; |
| 292 | |
|
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
293 /* 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
|
294 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
|
295 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
|
296 hook. |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
297 |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
298 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
|
299 __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
|
300 GNU malloc package. */ |
| 118 | 301 |
| 302 POINTER | |
| 303 r_alloc_sbrk (size) | |
| 304 long size; | |
| 305 { | |
| 306 POINTER ptr; | |
| 307 | |
| 308 if (! use_relocatable_buffers) | |
| 1401 | 309 return (*real_morecore) (size); |
| 118 | 310 |
| 311 if (size > 0) | |
| 312 { | |
|
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
313 if (! obtain (size)) |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
314 return 0; |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
315 |
| 118 | 316 if (first_bloc) |
| 317 { | |
| 318 relocate_some_blocs (first_bloc, first_bloc->data + size); | |
| 577 | 319 |
| 320 /* Zero out the space we just allocated, to help catch bugs | |
| 321 quickly. */ | |
| 118 | 322 bzero (virtual_break_value, size); |
| 323 } | |
| 324 } | |
| 325 else if (size < 0) | |
| 326 { | |
| 327 if (first_bloc) | |
| 328 relocate_some_blocs (first_bloc, first_bloc->data + size); | |
| 329 relinquish (- size); | |
| 330 } | |
| 331 | |
| 332 ptr = virtual_break_value; | |
| 333 virtual_break_value += size; | |
| 334 return ptr; | |
| 335 } | |
| 336 | |
| 337 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to | |
| 338 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
|
339 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
|
340 |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
341 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
|
342 return zero. */ |
| 118 | 343 |
| 344 POINTER | |
| 345 r_alloc (ptr, size) | |
| 346 POINTER *ptr; | |
| 347 SIZE size; | |
| 348 { | |
| 349 register bloc_ptr new_bloc; | |
| 350 | |
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
351 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
|
352 r_alloc_init (); |
|
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
353 |
| 118 | 354 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
|
355 if (new_bloc) |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
356 { |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
357 new_bloc->variable = ptr; |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
358 *ptr = new_bloc->data; |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
359 } |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
360 else |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
361 *ptr = 0; |
| 118 | 362 |
| 363 return *ptr; | |
| 364 } | |
| 365 | |
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
366 /* 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
|
367 Store 0 in *PTR to show there's no block allocated. */ |
| 118 | 368 |
| 369 void | |
| 370 r_alloc_free (ptr) | |
| 371 register POINTER *ptr; | |
| 372 { | |
| 373 register bloc_ptr dead_bloc; | |
| 374 | |
| 375 dead_bloc = find_bloc (ptr); | |
| 376 if (dead_bloc == NIL_BLOC) | |
| 377 abort (); | |
| 378 | |
| 379 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
|
380 *ptr = 0; |
| 118 | 381 } |
| 382 | |
|
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
383 /* 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
|
384 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
|
385 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
|
386 do nothing. |
| 118 | 387 |
|
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
388 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
|
389 |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
390 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
|
391 return zero. */ |
| 118 | 392 |
| 393 POINTER | |
| 394 r_re_alloc (ptr, size) | |
| 395 POINTER *ptr; | |
| 396 SIZE size; | |
| 397 { | |
|
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
398 register bloc_ptr bloc; |
| 118 | 399 |
|
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
400 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
|
401 if (bloc == NIL_BLOC) |
| 118 | 402 abort (); |
| 403 | |
|
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
404 if (size <= bloc->size) |
| 577 | 405 /* Wouldn't it be useful to actually resize the bloc here? */ |
| 118 | 406 return *ptr; |
| 407 | |
|
1249
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
408 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
|
409 return 0; |
|
761b9b4fd3ed
* ralloc.c: Since the users of the relocating allocation code
Jim Blandy <jimb@redhat.com>
parents:
1121
diff
changeset
|
410 |
|
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
411 relocate_some_blocs (bloc->next, bloc->data + size); |
| 118 | 412 |
|
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
413 /* 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
|
414 bzero (bloc->data + bloc->size, size - bloc->size); |
| 1121 | 415 |
|
1087
6c410cc87574
* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the
Jim Blandy <jimb@redhat.com>
parents:
1013
diff
changeset
|
416 /* 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
|
417 bloc->size = size; |
| 118 | 418 |
| 419 return *ptr; | |
| 420 } | |
| 421 | |
| 422 /* The hook `malloc' uses for the function which gets more space | |
| 423 from the system. */ | |
| 424 extern POINTER (*__morecore) (); | |
| 425 | |
| 426 /* Intialize various things for memory allocation. */ | |
| 427 | |
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
428 static void |
|
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
429 r_alloc_init () |
|
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
430 { |
|
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
431 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
|
432 return; |
|
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
433 |
|
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
434 r_alloc_initialized = 1; |
| 1401 | 435 real_morecore = __morecore; |
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
436 __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
|
437 |
| 1401 | 438 virtual_break_value = break_value = (*real_morecore) (0); |
| 439 if (break_value == NULL) | |
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
440 abort (); |
|
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
441 |
|
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
parents:
1249
diff
changeset
|
442 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
|
443 /* 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
|
444 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
|
445 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
|
446 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
|
447 } |
