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