49600
|
1 /* Block-relocating memory allocator.
|
64770
|
2 Copyright (C) 1993, 1995, 2000, 2002, 2003, 2004,
|
68651
|
3 2005, 2006 Free Software Foundation, Inc.
|
118
|
4
|
|
5 This file is part of GNU Emacs.
|
|
6
|
|
7 GNU Emacs is free software; you can redistribute it and/or modify
|
|
8 it under the terms of the GNU General Public License as published by
|
10432
|
9 the Free Software Foundation; either version 2, or (at your option)
|
118
|
10 any later version.
|
|
11
|
|
12 GNU Emacs is distributed in the hope that it will be useful,
|
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 GNU General Public License for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with GNU Emacs; see the file COPYING. If not, write to
|
64084
|
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
20 Boston, MA 02110-1301, USA. */
|
118
|
21
|
|
22 /* NOTES:
|
|
23
|
3591
|
24 Only relocate the blocs necessary for SIZE in r_alloc_sbrk,
|
118
|
25 rather than all of them. This means allowing for a possible
|
9596
|
26 hole between the first bloc and the end of malloc storage. */
|
118
|
27
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
28 #ifdef emacs
|
1403
|
29
|
4696
|
30 #include <config.h>
|
577
|
31 #include "lisp.h" /* Needed for VALBITS. */
|
61226
|
32 #include "blockinput.h"
|
31414
|
33
|
29917
|
34 #ifdef HAVE_UNISTD_H
|
|
35 #include <unistd.h>
|
|
36 #endif
|
1403
|
37
|
31414
|
38 typedef POINTER_TYPE *POINTER;
|
|
39 typedef size_t SIZE;
|
1451
|
40
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
41 /* 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>
diff
changeset
|
42 overlap. */
|
31414
|
43
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
44 extern void safe_bcopy ();
|
1403
|
45
|
17845
|
46 #ifdef DOUG_LEA_MALLOC
|
49600
|
47 #define M_TOP_PAD -2
|
17845
|
48 extern int mallopt ();
|
31414
|
49 #else /* not DOUG_LEA_MALLOC */
|
31498
|
50 #ifndef SYSTEM_MALLOC
|
31891
|
51 extern size_t __malloc_extra_blocks;
|
31498
|
52 #endif /* SYSTEM_MALLOC */
|
31414
|
53 #endif /* not DOUG_LEA_MALLOC */
|
10785
|
54
|
10747
|
55 #else /* not emacs */
|
1403
|
56
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
57 #include <stddef.h>
|
1403
|
58
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
59 typedef size_t SIZE;
|
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
60 typedef void *POINTER;
|
1403
|
61
|
|
62 #include <unistd.h>
|
|
63 #include <malloc.h>
|
|
64
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
65 #define safe_bcopy(x, y, z) memmove (y, x, z)
|
10747
|
66 #define bzero(x, len) memset (x, 0, len)
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
67
|
10747
|
68 #endif /* not emacs */
|
|
69
|
31414
|
70
|
10747
|
71 #include "getpagesize.h"
|
118
|
72
|
|
73 #define NIL ((POINTER) 0)
|
|
74
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
75 /* 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>
diff
changeset
|
76 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>
diff
changeset
|
77 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>
diff
changeset
|
78 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>
diff
changeset
|
79 the empty string, meaning that r_alloc_initialized becomes an
|
31414
|
80 automatic variable, and loses its value each time Emacs is started
|
|
81 up. */
|
|
82
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
83 static int r_alloc_initialized = 0;
|
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
84
|
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
85 static void r_alloc_init ();
|
31414
|
86
|
118
|
87
|
577
|
88 /* Declarations for working with the malloc, ralloc, and system breaks. */
|
|
89
|
9596
|
90 /* Function to set the real break value. */
|
30061
|
91 POINTER (*real_morecore) ();
|
118
|
92
|
9596
|
93 /* The break value, as seen by malloc. */
|
118
|
94 static POINTER virtual_break_value;
|
|
95
|
9596
|
96 /* The address of the end of the last data in use by ralloc,
|
|
97 including relocatable blocs as well as malloc data. */
|
118
|
98 static POINTER break_value;
|
|
99
|
1473
6359d8850fa3
(relinquish): Adjust page_break_value by amount of memory actually given back.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
100 /* This is the size of a page. We round memory requests to this boundary. */
|
6359d8850fa3
(relinquish): Adjust page_break_value by amount of memory actually given back.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
101 static int page_size;
|
6359d8850fa3
(relinquish): Adjust page_break_value by amount of memory actually given back.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
102
|
49600
|
103 /* Whenever we get memory from the system, get this many extra bytes. This
|
1595
|
104 must be a multiple of page_size. */
|
1473
6359d8850fa3
(relinquish): Adjust page_break_value by amount of memory actually given back.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
105 static int extra_bytes;
|
6359d8850fa3
(relinquish): Adjust page_break_value by amount of memory actually given back.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
106
|
118
|
107 /* Macros for rounding. Note that rounding to any value is possible
|
9596
|
108 by changing the definition of PAGE. */
|
118
|
109 #define PAGE (getpagesize ())
|
4230
df4d091e603e
(ALIGNED, ROUNDUP): Use `unsigned long int' instead of `unsigned int' for
Roland McGrath <roland@gnu.org>
diff
changeset
|
110 #define ALIGNED(addr) (((unsigned long int) (addr) & (page_size - 1)) == 0)
|
df4d091e603e
(ALIGNED, ROUNDUP): Use `unsigned long int' instead of `unsigned int' for
Roland McGrath <roland@gnu.org>
diff
changeset
|
111 #define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \
|
df4d091e603e
(ALIGNED, ROUNDUP): Use `unsigned long int' instead of `unsigned int' for
Roland McGrath <roland@gnu.org>
diff
changeset
|
112 & ~(page_size - 1))
|
1473
6359d8850fa3
(relinquish): Adjust page_break_value by amount of memory actually given back.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
113 #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
|
118
|
114
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
115 #define MEM_ALIGN sizeof(double)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
116 #define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
117 & ~(MEM_ALIGN - 1))
|
31414
|
118
|
36187
|
119 /* The hook `malloc' uses for the function which gets more space
|
|
120 from the system. */
|
|
121
|
|
122 #ifndef SYSTEM_MALLOC
|
|
123 extern POINTER (*__morecore) ();
|
|
124 #endif
|
|
125
|
|
126
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
127
|
31414
|
128 /***********************************************************************
|
|
129 Implementation using sbrk
|
|
130 ***********************************************************************/
|
|
131
|
9596
|
132 /* Data structures of heaps and blocs. */
|
|
133
|
|
134 /* The relocatable objects, or blocs, and the malloc data
|
|
135 both reside within one or more heaps.
|
|
136 Each heap contains malloc data, running from `start' to `bloc_start',
|
|
137 and relocatable objects, running from `bloc_start' to `free'.
|
|
138
|
|
139 Relocatable objects may relocate within the same heap
|
|
140 or may move into another heap; the heaps themselves may grow
|
|
141 but they never move.
|
|
142
|
|
143 We try to make just one heap and make it larger as necessary.
|
14036
|
144 But sometimes we can't do that, because we can't get contiguous
|
9596
|
145 space to add onto the heap. When that happens, we start a new heap. */
|
49600
|
146
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
147 typedef struct heap
|
118
|
148 {
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
149 struct heap *next;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
150 struct heap *prev;
|
9596
|
151 /* Start of memory range of this heap. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
152 POINTER start;
|
9596
|
153 /* End of memory range of this heap. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
154 POINTER end;
|
9596
|
155 /* Start of relocatable data in this heap. */
|
|
156 POINTER bloc_start;
|
|
157 /* Start of unused space in this heap. */
|
|
158 POINTER free;
|
9666
|
159 /* First bloc in this heap. */
|
|
160 struct bp *first_bloc;
|
|
161 /* Last bloc in this heap. */
|
|
162 struct bp *last_bloc;
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
163 } *heap_ptr;
|
118
|
164
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
165 #define NIL_HEAP ((heap_ptr) 0)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
166 #define HEAP_PTR_SIZE (sizeof (struct heap))
|
118
|
167
|
9596
|
168 /* This is the first heap object.
|
|
169 If we need additional heap objects, each one resides at the beginning of
|
|
170 the space it covers. */
|
|
171 static struct heap heap_base;
|
|
172
|
|
173 /* Head and tail of the list of heaps. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
174 static heap_ptr first_heap, last_heap;
|
577
|
175
|
|
176 /* These structures are allocated in the malloc arena.
|
|
177 The linked list is kept in order of increasing '.data' members.
|
|
178 The data blocks abut each other; if b->next is non-nil, then
|
49600
|
179 b->data + b->size == b->next->data.
|
11146
|
180
|
|
181 An element with variable==NIL denotes a freed block, which has not yet
|
|
182 been collected. They may only appear while r_alloc_freeze > 0, and will be
|
|
183 freed when the arena is thawed. Currently, these blocs are not reusable,
|
14036
|
184 while the arena is frozen. Very inefficient. */
|
11146
|
185
|
118
|
186 typedef struct bp
|
|
187 {
|
|
188 struct bp *next;
|
|
189 struct bp *prev;
|
|
190 POINTER *variable;
|
|
191 POINTER data;
|
|
192 SIZE size;
|
14036
|
193 POINTER new_data; /* temporarily used for relocation */
|
11146
|
194 struct heap *heap; /* Heap this bloc is in. */
|
118
|
195 } *bloc_ptr;
|
|
196
|
|
197 #define NIL_BLOC ((bloc_ptr) 0)
|
|
198 #define BLOC_PTR_SIZE (sizeof (struct bp))
|
|
199
|
9596
|
200 /* Head and tail of the list of relocatable blocs. */
|
118
|
201 static bloc_ptr first_bloc, last_bloc;
|
|
202
|
11146
|
203 static int use_relocatable_buffers;
|
|
204
|
|
205 /* If >0, no relocation whatsoever takes place. */
|
|
206 static int r_alloc_freeze_level;
|
|
207
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
208
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
209 /* Functions to get and return memory from the system. */
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
210
|
9596
|
211 /* Find the heap that ADDRESS falls within. */
|
|
212
|
|
213 static heap_ptr
|
|
214 find_heap (address)
|
|
215 POINTER address;
|
|
216 {
|
|
217 heap_ptr heap;
|
|
218
|
|
219 for (heap = last_heap; heap; heap = heap->prev)
|
|
220 {
|
|
221 if (heap->start <= address && address <= heap->end)
|
|
222 return heap;
|
|
223 }
|
|
224
|
|
225 return NIL_HEAP;
|
|
226 }
|
|
227
|
|
228 /* Find SIZE bytes of space in a heap.
|
|
229 Try to get them at ADDRESS (which must fall within some heap's range)
|
|
230 if we can get that many within one heap.
|
|
231
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
232 If enough space is not presently available in our reserve, this means
|
14036
|
233 getting more page-aligned space from the system. If the returned space
|
|
234 is not contiguous to the last heap, allocate a new heap, and append it
|
9596
|
235
|
|
236 obtain does not try to keep track of whether space is in use
|
|
237 or not in use. It just returns the address of SIZE bytes that
|
|
238 fall within a single heap. If you call obtain twice in a row
|
|
239 with the same arguments, you typically get the same value.
|
|
240 to the heap list. It's the caller's responsibility to keep
|
|
241 track of what space is in use.
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
242
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
243 Return the address of the space if all went well, or zero if we couldn't
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
244 allocate the memory. */
|
9596
|
245
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
246 static POINTER
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
247 obtain (address, size)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
248 POINTER address;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
249 SIZE size;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
250 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
251 heap_ptr heap;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
252 SIZE already_available;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
253
|
9596
|
254 /* Find the heap that ADDRESS falls within. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
255 for (heap = last_heap; heap; heap = heap->prev)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
256 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
257 if (heap->start <= address && address <= heap->end)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
258 break;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
259 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
260
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
261 if (! heap)
|
9596
|
262 abort ();
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
263
|
9596
|
264 /* If we can't fit SIZE bytes in that heap,
|
|
265 try successive later heaps. */
|
31473
|
266 while (heap && (char *) address + size > (char *) heap->end)
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
267 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
268 heap = heap->next;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
269 if (heap == NIL_HEAP)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
270 break;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
271 address = heap->bloc_start;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
272 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
273
|
9596
|
274 /* If we can't fit them within any existing heap,
|
|
275 get more space. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
276 if (heap == NIL_HEAP)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
277 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
278 POINTER new = (*real_morecore)(0);
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
279 SIZE get;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
280
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
281 already_available = (char *)last_heap->end - (char *)address;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
282
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
283 if (new != last_heap->end)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
284 {
|
9596
|
285 /* Someone else called sbrk. Make a new heap. */
|
|
286
|
|
287 heap_ptr new_heap = (heap_ptr) MEM_ROUNDUP (new);
|
|
288 POINTER bloc_start = (POINTER) MEM_ROUNDUP ((POINTER)(new_heap + 1));
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
289
|
31473
|
290 if ((*real_morecore) ((char *) bloc_start - (char *) new) != new)
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
291 return 0;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
292
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
293 new_heap->start = new;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
294 new_heap->end = bloc_start;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
295 new_heap->bloc_start = bloc_start;
|
9596
|
296 new_heap->free = bloc_start;
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
297 new_heap->next = NIL_HEAP;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
298 new_heap->prev = last_heap;
|
9666
|
299 new_heap->first_bloc = NIL_BLOC;
|
|
300 new_heap->last_bloc = NIL_BLOC;
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
301 last_heap->next = new_heap;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
302 last_heap = new_heap;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
303
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
304 address = bloc_start;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
305 already_available = 0;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
306 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
307
|
9596
|
308 /* Add space to the last heap (which we may have just created).
|
|
309 Get some extra, so we can come here less often. */
|
|
310
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
311 get = size + extra_bytes - already_available;
|
9596
|
312 get = (char *) ROUNDUP ((char *)last_heap->end + get)
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
313 - (char *) last_heap->end;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
314
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
315 if ((*real_morecore) (get) != last_heap->end)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
316 return 0;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
317
|
31473
|
318 last_heap->end = (char *) last_heap->end + get;
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
319 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
320
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
321 return address;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
322 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
323
|
9596
|
324 /* Return unused heap space to the system
|
|
325 if there is a lot of unused space now.
|
|
326 This can make the last heap smaller;
|
|
327 it can also eliminate the last heap entirely. */
|
|
328
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
329 static void
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
330 relinquish ()
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
331 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
332 register heap_ptr h;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
333 int excess = 0;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
334
|
9596
|
335 /* Add the amount of space beyond break_value
|
|
336 in all heaps which have extend beyond break_value at all. */
|
|
337
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
338 for (h = last_heap; h && break_value < h->end; h = h->prev)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
339 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
340 excess += (char *) h->end - (char *) ((break_value < h->bloc_start)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
341 ? h->bloc_start : break_value);
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
342 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
343
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
344 if (excess > extra_bytes * 2 && (*real_morecore) (0) == last_heap->end)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
345 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
346 /* Keep extra_bytes worth of empty space.
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
347 And don't free anything unless we can free at least extra_bytes. */
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
348 excess -= extra_bytes;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
349
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
350 if ((char *)last_heap->end - (char *)last_heap->bloc_start <= excess)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
351 {
|
9666
|
352 /* This heap should have no blocs in it. */
|
|
353 if (last_heap->first_bloc != NIL_BLOC
|
|
354 || last_heap->last_bloc != NIL_BLOC)
|
|
355 abort ();
|
|
356
|
9596
|
357 /* Return the last heap, with its header, to the system. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
358 excess = (char *)last_heap->end - (char *)last_heap->start;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
359 last_heap = last_heap->prev;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
360 last_heap->next = NIL_HEAP;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
361 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
362 else
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
363 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
364 excess = (char *) last_heap->end
|
9596
|
365 - (char *) ROUNDUP ((char *)last_heap->end - excess);
|
31473
|
366 last_heap->end = (char *) last_heap->end - excess;
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
367 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
368
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
369 if ((*real_morecore) (- excess) == 0)
|
19999
|
370 {
|
|
371 /* If the system didn't want that much memory back, adjust
|
|
372 the end of the last heap to reflect that. This can occur
|
|
373 if break_value is still within the original data segment. */
|
31473
|
374 last_heap->end = (char *) last_heap->end + excess;
|
19999
|
375 /* Make sure that the result of the adjustment is accurate.
|
|
376 It should be, for the else clause above; the other case,
|
|
377 which returns the entire last heap to the system, seems
|
|
378 unlikely to trigger this mode of failure. */
|
|
379 if (last_heap->end != (*real_morecore) (0))
|
|
380 abort ();
|
|
381 }
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
382 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
383 }
|
10682
|
384
|
|
385 /* Return the total size in use by relocating allocator,
|
|
386 above where malloc gets space. */
|
|
387
|
|
388 long
|
|
389 r_alloc_size_in_use ()
|
|
390 {
|
31473
|
391 return (char *) break_value - (char *) virtual_break_value;
|
10682
|
392 }
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
393
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
394 /* The meat - allocating, freeing, and relocating blocs. */
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
395
|
577
|
396 /* Find the bloc referenced by the address in PTR. Returns a pointer
|
9596
|
397 to that block. */
|
118
|
398
|
|
399 static bloc_ptr
|
|
400 find_bloc (ptr)
|
|
401 POINTER *ptr;
|
|
402 {
|
|
403 register bloc_ptr p = first_bloc;
|
|
404
|
|
405 while (p != NIL_BLOC)
|
|
406 {
|
|
407 if (p->variable == ptr && p->data == *ptr)
|
|
408 return p;
|
|
409
|
|
410 p = p->next;
|
|
411 }
|
|
412
|
|
413 return p;
|
|
414 }
|
|
415
|
|
416 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
|
1249
|
417 Returns a pointer to the new bloc, or zero if we couldn't allocate
|
|
418 memory for the new block. */
|
118
|
419
|
|
420 static bloc_ptr
|
|
421 get_bloc (size)
|
|
422 SIZE size;
|
|
423 {
|
1249
|
424 register bloc_ptr new_bloc;
|
9596
|
425 register heap_ptr heap;
|
118
|
426
|
1249
|
427 if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
428 || ! (new_bloc->data = obtain (break_value, size)))
|
1249
|
429 {
|
|
430 if (new_bloc)
|
|
431 free (new_bloc);
|
|
432
|
|
433 return 0;
|
|
434 }
|
|
435
|
31473
|
436 break_value = (char *) new_bloc->data + size;
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
437
|
118
|
438 new_bloc->size = size;
|
|
439 new_bloc->next = NIL_BLOC;
|
1013
|
440 new_bloc->variable = (POINTER *) NIL;
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
441 new_bloc->new_data = 0;
|
118
|
442
|
9596
|
443 /* Record in the heap that this space is in use. */
|
|
444 heap = find_heap (new_bloc->data);
|
|
445 heap->free = break_value;
|
|
446
|
9666
|
447 /* Maintain the correspondence between heaps and blocs. */
|
|
448 new_bloc->heap = heap;
|
|
449 heap->last_bloc = new_bloc;
|
|
450 if (heap->first_bloc == NIL_BLOC)
|
|
451 heap->first_bloc = new_bloc;
|
|
452
|
9596
|
453 /* Put this bloc on the doubly-linked list of blocs. */
|
118
|
454 if (first_bloc)
|
|
455 {
|
|
456 new_bloc->prev = last_bloc;
|
|
457 last_bloc->next = new_bloc;
|
|
458 last_bloc = new_bloc;
|
|
459 }
|
|
460 else
|
|
461 {
|
|
462 first_bloc = last_bloc = new_bloc;
|
|
463 new_bloc->prev = NIL_BLOC;
|
|
464 }
|
|
465
|
|
466 return new_bloc;
|
|
467 }
|
9666
|
468
|
9596
|
469 /* Calculate new locations of blocs in the list beginning with BLOC,
|
|
470 relocating it to start at ADDRESS, in heap HEAP. If enough space is
|
|
471 not presently available in our reserve, call obtain for
|
49600
|
472 more space.
|
|
473
|
9596
|
474 Store the new location of each bloc in its new_data field.
|
|
475 Do not touch the contents of blocs or break_value. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
476
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
477 static int
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
478 relocate_blocs (bloc, heap, address)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
479 bloc_ptr bloc;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
480 heap_ptr heap;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
481 POINTER address;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
482 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
483 register bloc_ptr b = bloc;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
484
|
11146
|
485 /* No need to ever call this if arena is frozen, bug somewhere! */
|
49600
|
486 if (r_alloc_freeze_level)
|
11146
|
487 abort();
|
|
488
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
489 while (b)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
490 {
|
9596
|
491 /* If bloc B won't fit within HEAP,
|
|
492 move to the next heap and try again. */
|
31473
|
493 while (heap && (char *) address + b->size > (char *) heap->end)
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
494 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
495 heap = heap->next;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
496 if (heap == NIL_HEAP)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
497 break;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
498 address = heap->bloc_start;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
499 }
|
118
|
500
|
9596
|
501 /* If BLOC won't fit in any heap,
|
|
502 get enough new space to hold BLOC and all following blocs. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
503 if (heap == NIL_HEAP)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
504 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
505 register bloc_ptr tb = b;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
506 register SIZE s = 0;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
507
|
9596
|
508 /* Add up the size of all the following blocs. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
509 while (tb != NIL_BLOC)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
510 {
|
49600
|
511 if (tb->variable)
|
11146
|
512 s += tb->size;
|
|
513
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
514 tb = tb->next;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
515 }
|
1595
|
516
|
9596
|
517 /* Get that space. */
|
|
518 address = obtain (address, s);
|
|
519 if (address == 0)
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
520 return 0;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
521
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
522 heap = last_heap;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
523 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
524
|
9596
|
525 /* Record the new address of this bloc
|
|
526 and update where the next bloc can start. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
527 b->new_data = address;
|
49600
|
528 if (b->variable)
|
31473
|
529 address = (char *) address + b->size;
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
530 b = b->next;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
531 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
532
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
533 return 1;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
534 }
|
118
|
535
|
9666
|
536 /* Reorder the bloc BLOC to go before bloc BEFORE in the doubly linked list.
|
|
537 This is necessary if we put the memory of space of BLOC
|
|
538 before that of BEFORE. */
|
|
539
|
|
540 static void
|
|
541 reorder_bloc (bloc, before)
|
|
542 bloc_ptr bloc, before;
|
|
543 {
|
|
544 bloc_ptr prev, next;
|
|
545
|
|
546 /* Splice BLOC out from where it is. */
|
|
547 prev = bloc->prev;
|
|
548 next = bloc->next;
|
9596
|
549
|
9666
|
550 if (prev)
|
|
551 prev->next = next;
|
|
552 if (next)
|
|
553 next->prev = prev;
|
|
554
|
|
555 /* Splice it in before BEFORE. */
|
|
556 prev = before->prev;
|
|
557
|
|
558 if (prev)
|
|
559 prev->next = bloc;
|
|
560 bloc->prev = prev;
|
|
561
|
|
562 before->prev = bloc;
|
|
563 bloc->next = before;
|
|
564 }
|
|
565
|
|
566 /* Update the records of which heaps contain which blocs, starting
|
|
567 with heap HEAP and bloc BLOC. */
|
|
568
|
|
569 static void
|
|
570 update_heap_bloc_correspondence (bloc, heap)
|
9596
|
571 bloc_ptr bloc;
|
|
572 heap_ptr heap;
|
|
573 {
|
|
574 register bloc_ptr b;
|
|
575
|
9666
|
576 /* Initialize HEAP's status to reflect blocs before BLOC. */
|
|
577 if (bloc != NIL_BLOC && bloc->prev != NIL_BLOC && bloc->prev->heap == heap)
|
|
578 {
|
|
579 /* The previous bloc is in HEAP. */
|
|
580 heap->last_bloc = bloc->prev;
|
31473
|
581 heap->free = (char *) bloc->prev->data + bloc->prev->size;
|
9666
|
582 }
|
|
583 else
|
|
584 {
|
|
585 /* HEAP contains no blocs before BLOC. */
|
|
586 heap->first_bloc = NIL_BLOC;
|
|
587 heap->last_bloc = NIL_BLOC;
|
|
588 heap->free = heap->bloc_start;
|
|
589 }
|
|
590
|
9596
|
591 /* Advance through blocs one by one. */
|
|
592 for (b = bloc; b != NIL_BLOC; b = b->next)
|
|
593 {
|
9666
|
594 /* Advance through heaps, marking them empty,
|
|
595 till we get to the one that B is in. */
|
9596
|
596 while (heap)
|
|
597 {
|
|
598 if (heap->bloc_start <= b->data && b->data <= heap->end)
|
|
599 break;
|
|
600 heap = heap->next;
|
9666
|
601 /* We know HEAP is not null now,
|
|
602 because there has to be space for bloc B. */
|
|
603 heap->first_bloc = NIL_BLOC;
|
|
604 heap->last_bloc = NIL_BLOC;
|
9596
|
605 heap->free = heap->bloc_start;
|
|
606 }
|
9666
|
607
|
|
608 /* Update HEAP's status for bloc B. */
|
31473
|
609 heap->free = (char *) b->data + b->size;
|
9666
|
610 heap->last_bloc = b;
|
|
611 if (heap->first_bloc == NIL_BLOC)
|
|
612 heap->first_bloc = b;
|
|
613
|
|
614 /* Record that B is in HEAP. */
|
|
615 b->heap = heap;
|
9596
|
616 }
|
|
617
|
|
618 /* If there are any remaining heaps and no blocs left,
|
9666
|
619 mark those heaps as empty. */
|
9596
|
620 heap = heap->next;
|
|
621 while (heap)
|
|
622 {
|
9666
|
623 heap->first_bloc = NIL_BLOC;
|
|
624 heap->last_bloc = NIL_BLOC;
|
9596
|
625 heap->free = heap->bloc_start;
|
|
626 heap = heap->next;
|
|
627 }
|
|
628 }
|
9666
|
629
|
9596
|
630 /* Resize BLOC to SIZE bytes. This relocates the blocs
|
|
631 that come after BLOC in memory. */
|
|
632
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
633 static int
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
634 resize_bloc (bloc, size)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
635 bloc_ptr bloc;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
636 SIZE size;
|
118
|
637 {
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
638 register bloc_ptr b;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
639 heap_ptr heap;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
640 POINTER address;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
641 SIZE old_size;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
642
|
11146
|
643 /* No need to ever call this if arena is frozen, bug somewhere! */
|
49600
|
644 if (r_alloc_freeze_level)
|
11146
|
645 abort();
|
|
646
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
647 if (bloc == NIL_BLOC || size == bloc->size)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
648 return 1;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
649
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
650 for (heap = first_heap; heap != NIL_HEAP; heap = heap->next)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
651 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
652 if (heap->bloc_start <= bloc->data && bloc->data <= heap->end)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
653 break;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
654 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
655
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
656 if (heap == NIL_HEAP)
|
9596
|
657 abort ();
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
658
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
659 old_size = bloc->size;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
660 bloc->size = size;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
661
|
9596
|
662 /* Note that bloc could be moved into the previous heap. */
|
31473
|
663 address = (bloc->prev ? (char *) bloc->prev->data + bloc->prev->size
|
|
664 : (char *) first_heap->bloc_start);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
665 while (heap)
|
118
|
666 {
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
667 if (heap->bloc_start <= address && address <= heap->end)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
668 break;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
669 heap = heap->prev;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
670 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
671
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
672 if (! relocate_blocs (bloc, heap, address))
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
673 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
674 bloc->size = old_size;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
675 return 0;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
676 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
677
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
678 if (size > old_size)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
679 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
680 for (b = last_bloc; b != bloc; b = b->prev)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
681 {
|
11146
|
682 if (!b->variable)
|
|
683 {
|
|
684 b->size = 0;
|
|
685 b->data = b->new_data;
|
49600
|
686 }
|
|
687 else
|
11146
|
688 {
|
|
689 safe_bcopy (b->data, b->new_data, b->size);
|
|
690 *b->variable = b->data = b->new_data;
|
|
691 }
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
692 }
|
11146
|
693 if (!bloc->variable)
|
|
694 {
|
|
695 bloc->size = 0;
|
|
696 bloc->data = bloc->new_data;
|
|
697 }
|
|
698 else
|
|
699 {
|
|
700 safe_bcopy (bloc->data, bloc->new_data, old_size);
|
31473
|
701 bzero ((char *) bloc->new_data + old_size, size - old_size);
|
11146
|
702 *bloc->variable = bloc->data = bloc->new_data;
|
|
703 }
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
704 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
705 else
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
706 {
|
1595
|
707 for (b = bloc; b != NIL_BLOC; b = b->next)
|
|
708 {
|
11146
|
709 if (!b->variable)
|
|
710 {
|
|
711 b->size = 0;
|
|
712 b->data = b->new_data;
|
49600
|
713 }
|
|
714 else
|
11146
|
715 {
|
|
716 safe_bcopy (b->data, b->new_data, b->size);
|
|
717 *b->variable = b->data = b->new_data;
|
|
718 }
|
1595
|
719 }
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
720 }
|
1595
|
721
|
9666
|
722 update_heap_bloc_correspondence (bloc, heap);
|
9596
|
723
|
31473
|
724 break_value = (last_bloc ? (char *) last_bloc->data + last_bloc->size
|
|
725 : (char *) first_heap->bloc_start);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
726 return 1;
|
1595
|
727 }
|
9666
|
728
|
9596
|
729 /* Free BLOC from the chain of blocs, relocating any blocs above it.
|
|
730 This may return space to the system. */
|
118
|
731
|
|
732 static void
|
|
733 free_bloc (bloc)
|
|
734 bloc_ptr bloc;
|
|
735 {
|
9666
|
736 heap_ptr heap = bloc->heap;
|
|
737
|
11146
|
738 if (r_alloc_freeze_level)
|
|
739 {
|
|
740 bloc->variable = (POINTER *) NIL;
|
|
741 return;
|
|
742 }
|
49600
|
743
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
744 resize_bloc (bloc, 0);
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
745
|
118
|
746 if (bloc == first_bloc && bloc == last_bloc)
|
|
747 {
|
|
748 first_bloc = last_bloc = NIL_BLOC;
|
|
749 }
|
|
750 else if (bloc == last_bloc)
|
|
751 {
|
|
752 last_bloc = bloc->prev;
|
|
753 last_bloc->next = NIL_BLOC;
|
|
754 }
|
|
755 else if (bloc == first_bloc)
|
|
756 {
|
|
757 first_bloc = bloc->next;
|
|
758 first_bloc->prev = NIL_BLOC;
|
|
759 }
|
|
760 else
|
|
761 {
|
|
762 bloc->next->prev = bloc->prev;
|
|
763 bloc->prev->next = bloc->next;
|
|
764 }
|
|
765
|
9666
|
766 /* Update the records of which blocs are in HEAP. */
|
|
767 if (heap->first_bloc == bloc)
|
|
768 {
|
10747
|
769 if (bloc->next != 0 && bloc->next->heap == heap)
|
9666
|
770 heap->first_bloc = bloc->next;
|
|
771 else
|
|
772 heap->first_bloc = heap->last_bloc = NIL_BLOC;
|
|
773 }
|
|
774 if (heap->last_bloc == bloc)
|
|
775 {
|
10747
|
776 if (bloc->prev != 0 && bloc->prev->heap == heap)
|
9666
|
777 heap->last_bloc = bloc->prev;
|
|
778 else
|
|
779 heap->first_bloc = heap->last_bloc = NIL_BLOC;
|
|
780 }
|
|
781
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
782 relinquish ();
|
118
|
783 free (bloc);
|
|
784 }
|
|
785
|
577
|
786 /* Interface routines. */
|
|
787
|
1249
|
788 /* 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>
diff
changeset
|
789 necessary. If relocatable blocs are in use, this means relocating
|
1249
|
790 them. This function gets plugged into the GNU malloc's __morecore
|
|
791 hook.
|
|
792
|
1473
6359d8850fa3
(relinquish): Adjust page_break_value by amount of memory actually given back.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
793 We provide hysteresis, never relocating by less than extra_bytes.
|
6359d8850fa3
(relinquish): Adjust page_break_value by amount of memory actually given back.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
794
|
1249
|
795 If we're out of memory, we should return zero, to imitate the other
|
|
796 __morecore hook values - in particular, __default_morecore in the
|
|
797 GNU malloc package. */
|
118
|
798
|
49600
|
799 POINTER
|
118
|
800 r_alloc_sbrk (size)
|
|
801 long size;
|
|
802 {
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
803 register bloc_ptr b;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
804 POINTER address;
|
118
|
805
|
10767
|
806 if (! r_alloc_initialized)
|
|
807 r_alloc_init ();
|
|
808
|
118
|
809 if (! use_relocatable_buffers)
|
1401
|
810 return (*real_morecore) (size);
|
118
|
811
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
812 if (size == 0)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
813 return virtual_break_value;
|
1473
6359d8850fa3
(relinquish): Adjust page_break_value by amount of memory actually given back.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
814
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
815 if (size > 0)
|
118
|
816 {
|
9596
|
817 /* Allocate a page-aligned space. GNU malloc would reclaim an
|
|
818 extra space if we passed an unaligned one. But we could
|
14036
|
819 not always find a space which is contiguous to the previous. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
820 POINTER new_bloc_start;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
821 heap_ptr h = first_heap;
|
9596
|
822 SIZE get = ROUNDUP (size);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
823
|
9596
|
824 address = (POINTER) ROUNDUP (virtual_break_value);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
825
|
9596
|
826 /* Search the list upward for a heap which is large enough. */
|
|
827 while ((char *) h->end < (char *) MEM_ROUNDUP ((char *)address + get))
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
828 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
829 h = h->next;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
830 if (h == NIL_HEAP)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
831 break;
|
9596
|
832 address = (POINTER) ROUNDUP (h->start);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
833 }
|
1473
6359d8850fa3
(relinquish): Adjust page_break_value by amount of memory actually given back.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
834
|
9596
|
835 /* If not found, obtain more space. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
836 if (h == NIL_HEAP)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
837 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
838 get += extra_bytes + page_size;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
839
|
11146
|
840 if (! obtain (address, get))
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
841 return 0;
|
1249
|
842
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
843 if (first_heap == last_heap)
|
9596
|
844 address = (POINTER) ROUNDUP (virtual_break_value);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
845 else
|
9596
|
846 address = (POINTER) ROUNDUP (last_heap->start);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
847 h = last_heap;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
848 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
849
|
9596
|
850 new_bloc_start = (POINTER) MEM_ROUNDUP ((char *)address + get);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
851
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
852 if (first_heap->bloc_start < new_bloc_start)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
853 {
|
11146
|
854 /* This is no clean solution - no idea how to do it better. */
|
49600
|
855 if (r_alloc_freeze_level)
|
11146
|
856 return NIL;
|
|
857
|
|
858 /* There is a bug here: if the above obtain call succeeded, but the
|
|
859 relocate_blocs call below does not succeed, we need to free
|
|
860 the memory that we got with obtain. */
|
|
861
|
9596
|
862 /* Move all blocs upward. */
|
11146
|
863 if (! relocate_blocs (first_bloc, h, new_bloc_start))
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
864 return 0;
|
577
|
865
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
866 /* Note that (POINTER)(h+1) <= new_bloc_start since
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
867 get >= page_size, so the following does not destroy the heap
|
9596
|
868 header. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
869 for (b = last_bloc; b != NIL_BLOC; b = b->prev)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
870 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
871 safe_bcopy (b->data, b->new_data, b->size);
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
872 *b->variable = b->data = b->new_data;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
873 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
874
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
875 h->bloc_start = new_bloc_start;
|
9596
|
876
|
9666
|
877 update_heap_bloc_correspondence (first_bloc, h);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
878 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
879 if (h != first_heap)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
880 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
881 /* Give up managing heaps below the one the new
|
9596
|
882 virtual_break_value points to. */
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
883 first_heap->prev = NIL_HEAP;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
884 first_heap->next = h->next;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
885 first_heap->start = h->start;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
886 first_heap->end = h->end;
|
9596
|
887 first_heap->free = h->free;
|
9666
|
888 first_heap->first_bloc = h->first_bloc;
|
|
889 first_heap->last_bloc = h->last_bloc;
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
890 first_heap->bloc_start = h->bloc_start;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
891
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
892 if (first_heap->next)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
893 first_heap->next->prev = first_heap;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
894 else
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
895 last_heap = first_heap;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
896 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
897
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
898 bzero (address, size);
|
118
|
899 }
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
900 else /* size < 0 */
|
118
|
901 {
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
902 SIZE excess = (char *)first_heap->bloc_start
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
903 - ((char *)virtual_break_value + size);
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
904
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
905 address = virtual_break_value;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
906
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
907 if (r_alloc_freeze_level == 0 && excess > 2 * extra_bytes)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
908 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
909 excess -= extra_bytes;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
910 first_heap->bloc_start
|
9666
|
911 = (POINTER) MEM_ROUNDUP ((char *)first_heap->bloc_start - excess);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
912
|
9596
|
913 relocate_blocs (first_bloc, first_heap, first_heap->bloc_start);
|
1473
6359d8850fa3
(relinquish): Adjust page_break_value by amount of memory actually given back.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
914
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
915 for (b = first_bloc; b != NIL_BLOC; b = b->next)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
916 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
917 safe_bcopy (b->data, b->new_data, b->size);
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
918 *b->variable = b->data = b->new_data;
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
919 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
920 }
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
921
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
922 if ((char *)virtual_break_value + size < (char *)first_heap->start)
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
923 {
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
924 /* We found an additional space below the first heap */
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
925 first_heap->start = (POINTER) ((char *)virtual_break_value + size);
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
926 }
|
118
|
927 }
|
|
928
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
929 virtual_break_value = (POINTER) ((char *)address + size);
|
9666
|
930 break_value = (last_bloc
|
31473
|
931 ? (char *) last_bloc->data + last_bloc->size
|
|
932 : (char *) first_heap->bloc_start);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
933 if (size < 0)
|
9596
|
934 relinquish ();
|
1473
6359d8850fa3
(relinquish): Adjust page_break_value by amount of memory actually given back.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
935
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
936 return address;
|
118
|
937 }
|
|
938
|
31414
|
939
|
118
|
940 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
|
|
941 the data is returned in *PTR. PTR is thus the address of some variable
|
1249
|
942 which will use the data area.
|
|
943
|
11146
|
944 The allocation of 0 bytes is valid.
|
|
945 In case r_alloc_freeze is set, a best fit of unused blocs could be done
|
|
946 before allocating a new area. Not yet done.
|
|
947
|
1249
|
948 If we can't allocate the necessary memory, set *PTR to zero, and
|
|
949 return zero. */
|
118
|
950
|
|
951 POINTER
|
|
952 r_alloc (ptr, size)
|
|
953 POINTER *ptr;
|
|
954 SIZE size;
|
|
955 {
|
|
956 register bloc_ptr new_bloc;
|
|
957
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
958 if (! r_alloc_initialized)
|
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
959 r_alloc_init ();
|
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
960
|
9596
|
961 new_bloc = get_bloc (MEM_ROUNDUP (size));
|
1249
|
962 if (new_bloc)
|
|
963 {
|
|
964 new_bloc->variable = ptr;
|
|
965 *ptr = new_bloc->data;
|
|
966 }
|
|
967 else
|
|
968 *ptr = 0;
|
118
|
969
|
|
970 return *ptr;
|
|
971 }
|
|
972
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
973 /* 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>
diff
changeset
|
974 Store 0 in *PTR to show there's no block allocated. */
|
118
|
975
|
|
976 void
|
|
977 r_alloc_free (ptr)
|
|
978 register POINTER *ptr;
|
|
979 {
|
|
980 register bloc_ptr dead_bloc;
|
|
981
|
10767
|
982 if (! r_alloc_initialized)
|
|
983 r_alloc_init ();
|
|
984
|
118
|
985 dead_bloc = find_bloc (ptr);
|
|
986 if (dead_bloc == NIL_BLOC)
|
|
987 abort ();
|
|
988
|
|
989 free_bloc (dead_bloc);
|
1390
92df75f4167f
(check_memory_limits): Reduce warnlevel when usage drops far enough.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
990 *ptr = 0;
|
10682
|
991
|
10747
|
992 #ifdef emacs
|
10682
|
993 refill_memory_reserve ();
|
10747
|
994 #endif
|
118
|
995 }
|
|
996
|
1087
|
997 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
|
1249
|
998 Do this by shifting all blocks above this one up in memory, unless
|
|
999 SIZE is less than or equal to the current bloc size, in which case
|
|
1000 do nothing.
|
118
|
1001
|
11146
|
1002 In case r_alloc_freeze is set, a new bloc is allocated, and the
|
14036
|
1003 memory copied to it. Not very efficient. We could traverse the
|
11146
|
1004 bloc_list for a best fit of free blocs first.
|
|
1005
|
1249
|
1006 Change *PTR to reflect the new bloc, and return this value.
|
|
1007
|
|
1008 If more memory cannot be allocated, then leave *PTR unchanged, and
|
|
1009 return zero. */
|
118
|
1010
|
|
1011 POINTER
|
|
1012 r_re_alloc (ptr, size)
|
|
1013 POINTER *ptr;
|
|
1014 SIZE size;
|
|
1015 {
|
1087
|
1016 register bloc_ptr bloc;
|
118
|
1017
|
10767
|
1018 if (! r_alloc_initialized)
|
|
1019 r_alloc_init ();
|
|
1020
|
11146
|
1021 if (!*ptr)
|
|
1022 return r_alloc (ptr, size);
|
49600
|
1023 if (!size)
|
11146
|
1024 {
|
|
1025 r_alloc_free (ptr);
|
|
1026 return r_alloc (ptr, 0);
|
|
1027 }
|
|
1028
|
1087
|
1029 bloc = find_bloc (ptr);
|
|
1030 if (bloc == NIL_BLOC)
|
118
|
1031 abort ();
|
|
1032
|
49600
|
1033 if (size < bloc->size)
|
11146
|
1034 {
|
|
1035 /* Wouldn't it be useful to actually resize the bloc here? */
|
|
1036 /* I think so too, but not if it's too expensive... */
|
49600
|
1037 if ((bloc->size - MEM_ROUNDUP (size) >= page_size)
|
|
1038 && r_alloc_freeze_level == 0)
|
11146
|
1039 {
|
|
1040 resize_bloc (bloc, MEM_ROUNDUP (size));
|
|
1041 /* Never mind if this fails, just do nothing... */
|
|
1042 /* It *should* be infallible! */
|
|
1043 }
|
|
1044 }
|
|
1045 else if (size > bloc->size)
|
|
1046 {
|
|
1047 if (r_alloc_freeze_level)
|
|
1048 {
|
|
1049 bloc_ptr new_bloc;
|
|
1050 new_bloc = get_bloc (MEM_ROUNDUP (size));
|
|
1051 if (new_bloc)
|
|
1052 {
|
|
1053 new_bloc->variable = ptr;
|
|
1054 *ptr = new_bloc->data;
|
|
1055 bloc->variable = (POINTER *) NIL;
|
|
1056 }
|
|
1057 else
|
|
1058 return NIL;
|
|
1059 }
|
49600
|
1060 else
|
11146
|
1061 {
|
|
1062 if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
|
|
1063 return NIL;
|
|
1064 }
|
|
1065 }
|
118
|
1066 return *ptr;
|
|
1067 }
|
8951
|
1068
|
|
1069 /* Disable relocations, after making room for at least SIZE bytes
|
|
1070 of non-relocatable heap if possible. The relocatable blocs are
|
|
1071 guaranteed to hold still until thawed, even if this means that
|
|
1072 malloc must return a null pointer. */
|
9596
|
1073
|
8951
|
1074 void
|
|
1075 r_alloc_freeze (size)
|
|
1076 long size;
|
|
1077 {
|
10767
|
1078 if (! r_alloc_initialized)
|
|
1079 r_alloc_init ();
|
|
1080
|
8951
|
1081 /* If already frozen, we can't make any more room, so don't try. */
|
|
1082 if (r_alloc_freeze_level > 0)
|
|
1083 size = 0;
|
|
1084 /* If we can't get the amount requested, half is better than nothing. */
|
|
1085 while (size > 0 && r_alloc_sbrk (size) == 0)
|
|
1086 size /= 2;
|
|
1087 ++r_alloc_freeze_level;
|
|
1088 if (size > 0)
|
|
1089 r_alloc_sbrk (-size);
|
|
1090 }
|
|
1091
|
|
1092 void
|
|
1093 r_alloc_thaw ()
|
|
1094 {
|
11146
|
1095
|
49600
|
1096 if (! r_alloc_initialized)
|
11146
|
1097 r_alloc_init ();
|
|
1098
|
8951
|
1099 if (--r_alloc_freeze_level < 0)
|
|
1100 abort ();
|
11146
|
1101
|
49600
|
1102 /* This frees all unused blocs. It is not too inefficient, as the resize
|
|
1103 and bcopy is done only once. Afterwards, all unreferenced blocs are
|
11146
|
1104 already shrunk to zero size. */
|
49600
|
1105 if (!r_alloc_freeze_level)
|
11146
|
1106 {
|
|
1107 bloc_ptr *b = &first_bloc;
|
49600
|
1108 while (*b)
|
|
1109 if (!(*b)->variable)
|
|
1110 free_bloc (*b);
|
|
1111 else
|
11146
|
1112 b = &(*b)->next;
|
|
1113 }
|
8951
|
1114 }
|
11146
|
1115
|
18757
|
1116
|
|
1117 #if defined (emacs) && defined (DOUG_LEA_MALLOC)
|
|
1118
|
|
1119 /* Reinitialize the morecore hook variables after restarting a dumped
|
|
1120 Emacs. This is needed when using Doug Lea's malloc from GNU libc. */
|
|
1121 void
|
|
1122 r_alloc_reinit ()
|
|
1123 {
|
|
1124 /* Only do this if the hook has been reset, so that we don't get an
|
|
1125 infinite loop, in case Emacs was linked statically. */
|
|
1126 if (__morecore != r_alloc_sbrk)
|
|
1127 {
|
|
1128 real_morecore = __morecore;
|
|
1129 __morecore = r_alloc_sbrk;
|
|
1130 }
|
|
1131 }
|
31414
|
1132
|
|
1133 #endif /* emacs && DOUG_LEA_MALLOC */
|
18757
|
1134
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1135 #ifdef DEBUG
|
31414
|
1136
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1137 #include <assert.h>
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1138
|
10767
|
1139 void
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1140 r_alloc_check ()
|
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1141 {
|
10766
|
1142 int found = 0;
|
|
1143 heap_ptr h, ph = 0;
|
|
1144 bloc_ptr b, pb = 0;
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1145
|
10766
|
1146 if (!r_alloc_initialized)
|
|
1147 return;
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1148
|
10766
|
1149 assert (first_heap);
|
|
1150 assert (last_heap->end <= (POINTER) sbrk (0));
|
|
1151 assert ((POINTER) first_heap < first_heap->start);
|
|
1152 assert (first_heap->start <= virtual_break_value);
|
|
1153 assert (virtual_break_value <= first_heap->end);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1154
|
10766
|
1155 for (h = first_heap; h; h = h->next)
|
|
1156 {
|
|
1157 assert (h->prev == ph);
|
|
1158 assert ((POINTER) ROUNDUP (h->end) == h->end);
|
14953
|
1159 #if 0 /* ??? The code in ralloc.c does not really try to ensure
|
|
1160 the heap start has any sort of alignment.
|
|
1161 Perhaps it should. */
|
10766
|
1162 assert ((POINTER) MEM_ROUNDUP (h->start) == h->start);
|
14953
|
1163 #endif
|
10766
|
1164 assert ((POINTER) MEM_ROUNDUP (h->bloc_start) == h->bloc_start);
|
|
1165 assert (h->start <= h->bloc_start && h->bloc_start <= h->end);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1166
|
10766
|
1167 if (ph)
|
|
1168 {
|
|
1169 assert (ph->end < h->start);
|
|
1170 assert (h->start <= (POINTER)h && (POINTER)(h+1) <= h->bloc_start);
|
|
1171 }
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1172
|
10766
|
1173 if (h->bloc_start <= break_value && break_value <= h->end)
|
|
1174 found = 1;
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1175
|
10766
|
1176 ph = h;
|
|
1177 }
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1178
|
10766
|
1179 assert (found);
|
|
1180 assert (last_heap == ph);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1181
|
10766
|
1182 for (b = first_bloc; b; b = b->next)
|
|
1183 {
|
|
1184 assert (b->prev == pb);
|
|
1185 assert ((POINTER) MEM_ROUNDUP (b->data) == b->data);
|
|
1186 assert ((SIZE) MEM_ROUNDUP (b->size) == b->size);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1187
|
10766
|
1188 ph = 0;
|
|
1189 for (h = first_heap; h; h = h->next)
|
|
1190 {
|
|
1191 if (h->bloc_start <= b->data && b->data + b->size <= h->end)
|
|
1192 break;
|
|
1193 ph = h;
|
|
1194 }
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1195
|
10766
|
1196 assert (h);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1197
|
10766
|
1198 if (pb && pb->data + pb->size != b->data)
|
|
1199 {
|
|
1200 assert (ph && b->data == h->bloc_start);
|
|
1201 while (ph)
|
|
1202 {
|
|
1203 if (ph->bloc_start <= pb->data
|
|
1204 && pb->data + pb->size <= ph->end)
|
|
1205 {
|
|
1206 assert (pb->data + pb->size + b->size > ph->end);
|
|
1207 break;
|
|
1208 }
|
|
1209 else
|
|
1210 {
|
|
1211 assert (ph->bloc_start + b->size > ph->end);
|
|
1212 }
|
|
1213 ph = ph->prev;
|
|
1214 }
|
|
1215 }
|
|
1216 pb = b;
|
|
1217 }
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1218
|
10766
|
1219 assert (last_bloc == pb);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1220
|
10766
|
1221 if (last_bloc)
|
|
1222 assert (last_bloc->data + last_bloc->size == break_value);
|
|
1223 else
|
|
1224 assert (first_heap->bloc_start == break_value);
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1225 }
|
31414
|
1226
|
9459
a1569f00a6a6
Install Hiroshi Nakano's rewrite to allow multiple heaps, for implementations
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1227 #endif /* DEBUG */
|
31414
|
1228
|
|
1229
|
|
1230
|
|
1231 /***********************************************************************
|
|
1232 Initialization
|
|
1233 ***********************************************************************/
|
|
1234
|
|
1235 /* Initialize various things for memory allocation. */
|
|
1236
|
|
1237 static void
|
|
1238 r_alloc_init ()
|
|
1239 {
|
|
1240 if (r_alloc_initialized)
|
|
1241 return;
|
|
1242 r_alloc_initialized = 1;
|
49600
|
1243
|
31498
|
1244 page_size = PAGE;
|
|
1245 #ifndef SYSTEM_MALLOC
|
31414
|
1246 real_morecore = __morecore;
|
|
1247 __morecore = r_alloc_sbrk;
|
|
1248
|
|
1249 first_heap = last_heap = &heap_base;
|
|
1250 first_heap->next = first_heap->prev = NIL_HEAP;
|
|
1251 first_heap->start = first_heap->bloc_start
|
|
1252 = virtual_break_value = break_value = (*real_morecore) (0);
|
|
1253 if (break_value == NIL)
|
|
1254 abort ();
|
|
1255
|
|
1256 extra_bytes = ROUNDUP (50000);
|
31498
|
1257 #endif
|
31414
|
1258
|
|
1259 #ifdef DOUG_LEA_MALLOC
|
61225
|
1260 BLOCK_INPUT;
|
|
1261 mallopt (M_TOP_PAD, 64 * 4096);
|
|
1262 UNBLOCK_INPUT;
|
31414
|
1263 #else
|
31498
|
1264 #ifndef SYSTEM_MALLOC
|
31414
|
1265 /* Give GNU malloc's morecore some hysteresis
|
|
1266 so that we move all the relocatable blocks much less often. */
|
|
1267 __malloc_extra_blocks = 64;
|
|
1268 #endif
|
31498
|
1269 #endif
|
31414
|
1270
|
31509
|
1271 #ifndef SYSTEM_MALLOC
|
31414
|
1272 first_heap->end = (POINTER) ROUNDUP (first_heap->start);
|
|
1273
|
|
1274 /* The extra call to real_morecore guarantees that the end of the
|
|
1275 address space is a multiple of page_size, even if page_size is
|
|
1276 not really the page size of the system running the binary in
|
|
1277 which page_size is stored. This allows a binary to be built on a
|
|
1278 system with one page size and run on a system with a smaller page
|
|
1279 size. */
|
31473
|
1280 (*real_morecore) ((char *) first_heap->end - (char *) first_heap->start);
|
31414
|
1281
|
|
1282 /* Clear the rest of the last page; this memory is in our address space
|
|
1283 even though it is after the sbrk value. */
|
|
1284 /* Doubly true, with the additional call that explicitly adds the
|
|
1285 rest of that page to the address space. */
|
31473
|
1286 bzero (first_heap->start,
|
|
1287 (char *) first_heap->end - (char *) first_heap->start);
|
31414
|
1288 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
|
31498
|
1289 #endif
|
49600
|
1290
|
31414
|
1291 use_relocatable_buffers = 1;
|
|
1292 }
|
52401
|
1293
|
|
1294 /* arch-tag: 6a524a15-faff-44c8-95d4-a5da6f55110f
|
|
1295 (do not change this comment) */
|