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