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