1 |
39 |
lampret |
/* ---------- To make a malloc.h, start cutting here ------------ */
|
2 |
|
|
|
3 |
|
|
/*
|
4 |
|
|
A version of malloc/free/realloc written by Doug Lea and released to the
|
5 |
|
|
public domain. Send questions/comments/complaints/performance data
|
6 |
|
|
to dl@cs.oswego.edu
|
7 |
|
|
|
8 |
|
|
* VERSION 2.6.4 Thu Nov 28 07:54:55 1996 Doug Lea (dl at gee)
|
9 |
|
|
|
10 |
|
|
Note: There may be an updated version of this malloc obtainable at
|
11 |
|
|
ftp://g.oswego.edu/pub/misc/malloc.c
|
12 |
|
|
Check before installing!
|
13 |
|
|
|
14 |
|
|
* Why use this malloc?
|
15 |
|
|
|
16 |
|
|
This is not the fastest, most space-conserving, most portable, or
|
17 |
|
|
most tunable malloc ever written. However it is among the fastest
|
18 |
|
|
while also being among the most space-conserving, portable and tunable.
|
19 |
|
|
Consistent balance across these factors results in a good general-purpose
|
20 |
|
|
allocator. For a high-level description, see
|
21 |
|
|
http://g.oswego.edu/dl/html/malloc.html
|
22 |
|
|
|
23 |
|
|
* Synopsis of public routines
|
24 |
|
|
|
25 |
|
|
(Much fuller descriptions are contained in the program documentation below.)
|
26 |
|
|
|
27 |
|
|
malloc(size_t n);
|
28 |
|
|
Return a pointer to a newly allocated chunk of at least n bytes, or null
|
29 |
|
|
if no space is available.
|
30 |
|
|
free(Void_t* p);
|
31 |
|
|
Release the chunk of memory pointed to by p, or no effect if p is null.
|
32 |
|
|
realloc(Void_t* p, size_t n);
|
33 |
|
|
Return a pointer to a chunk of size n that contains the same data
|
34 |
|
|
as does chunk p up to the minimum of (n, p's size) bytes, or null
|
35 |
|
|
if no space is available. The returned pointer may or may not be
|
36 |
|
|
the same as p. If p is null, equivalent to malloc. Unless the
|
37 |
|
|
#define REALLOC_ZERO_BYTES_FREES below is set, realloc with a
|
38 |
|
|
size argument of zero (re)allocates a minimum-sized chunk.
|
39 |
|
|
memalign(size_t alignment, size_t n);
|
40 |
|
|
Return a pointer to a newly allocated chunk of n bytes, aligned
|
41 |
|
|
in accord with the alignment argument, which must be a power of
|
42 |
|
|
two.
|
43 |
|
|
valloc(size_t n);
|
44 |
|
|
Equivalent to memalign(pagesize, n), where pagesize is the page
|
45 |
|
|
size of the system (or as near to this as can be figured out from
|
46 |
|
|
all the includes/defines below.)
|
47 |
|
|
pvalloc(size_t n);
|
48 |
|
|
Equivalent to valloc(minimum-page-that-holds(n)), that is,
|
49 |
|
|
round up n to nearest pagesize.
|
50 |
|
|
calloc(size_t unit, size_t quantity);
|
51 |
|
|
Returns a pointer to quantity * unit bytes, with all locations
|
52 |
|
|
set to zero.
|
53 |
|
|
cfree(Void_t* p);
|
54 |
|
|
Equivalent to free(p).
|
55 |
|
|
malloc_trim(size_t pad);
|
56 |
|
|
Release all but pad bytes of freed top-most memory back
|
57 |
|
|
to the system. Return 1 if successful, else 0.
|
58 |
|
|
malloc_usable_size(Void_t* p);
|
59 |
|
|
Report the number usable allocated bytes associated with allocated
|
60 |
|
|
chunk p. This may or may not report more bytes than were requested,
|
61 |
|
|
due to alignment and minimum size constraints.
|
62 |
|
|
malloc_stats();
|
63 |
|
|
Prints brief summary statistics on stderr.
|
64 |
|
|
mallinfo()
|
65 |
|
|
Returns (by copy) a struct containing various summary statistics.
|
66 |
|
|
mallopt(int parameter_number, int parameter_value)
|
67 |
|
|
Changes one of the tunable parameters described below. Returns
|
68 |
|
|
1 if successful in changing the parameter, else 0.
|
69 |
|
|
|
70 |
|
|
* Vital statistics:
|
71 |
|
|
|
72 |
|
|
Alignment: 8-byte
|
73 |
|
|
8 byte alignment is currently hardwired into the design. This
|
74 |
|
|
seems to suffice for all current machines and C compilers.
|
75 |
|
|
|
76 |
|
|
Assumed pointer representation: 4 or 8 bytes
|
77 |
|
|
Code for 8-byte pointers is untested by me but has worked
|
78 |
|
|
reliably by Wolfram Gloger, who contributed most of the
|
79 |
|
|
changes supporting this.
|
80 |
|
|
|
81 |
|
|
Assumed size_t representation: 4 or 8 bytes
|
82 |
|
|
Note that size_t is allowed to be 4 bytes even if pointers are 8.
|
83 |
|
|
|
84 |
|
|
Minimum overhead per allocated chunk: 4 or 8 bytes
|
85 |
|
|
Each malloced chunk has a hidden overhead of 4 bytes holding size
|
86 |
|
|
and status information.
|
87 |
|
|
|
88 |
|
|
Minimum allocated size: 4-byte ptrs: 16 bytes (including 4 overhead)
|
89 |
|
|
8-byte ptrs: 24/32 bytes (including, 4/8 overhead)
|
90 |
|
|
|
91 |
|
|
When a chunk is freed, 12 (for 4byte ptrs) or 20 (for 8 byte
|
92 |
|
|
ptrs but 4 byte size) or 24 (for 8/8) additional bytes are
|
93 |
|
|
needed; 4 (8) for a trailing size field
|
94 |
|
|
and 8 (16) bytes for free list pointers. Thus, the minimum
|
95 |
|
|
allocatable size is 16/24/32 bytes.
|
96 |
|
|
|
97 |
|
|
Even a request for zero bytes (i.e., malloc(0)) returns a
|
98 |
|
|
pointer to something of the minimum allocatable size.
|
99 |
|
|
|
100 |
|
|
Maximum allocated size: 4-byte size_t: 2^31 - 8 bytes
|
101 |
|
|
8-byte size_t: 2^63 - 16 bytes
|
102 |
|
|
|
103 |
|
|
It is assumed that (possibly signed) size_t bit values suffice to
|
104 |
|
|
represent chunk sizes. `Possibly signed' is due to the fact
|
105 |
|
|
that `size_t' may be defined on a system as either a signed or
|
106 |
|
|
an unsigned type. To be conservative, values that would appear
|
107 |
|
|
as negative numbers are avoided.
|
108 |
|
|
Requests for sizes with a negative sign bit will return a
|
109 |
|
|
minimum-sized chunk.
|
110 |
|
|
|
111 |
|
|
Maximum overhead wastage per allocated chunk: normally 15 bytes
|
112 |
|
|
|
113 |
|
|
Alignnment demands, plus the minimum allocatable size restriction
|
114 |
|
|
make the normal worst-case wastage 15 bytes (i.e., up to 15
|
115 |
|
|
more bytes will be allocated than were requested in malloc), with
|
116 |
|
|
two exceptions:
|
117 |
|
|
1. Because requests for zero bytes allocate non-zero space,
|
118 |
|
|
the worst case wastage for a request of zero bytes is 24 bytes.
|
119 |
|
|
2. For requests >= mmap_threshold that are serviced via
|
120 |
|
|
mmap(), the worst case wastage is 8 bytes plus the remainder
|
121 |
|
|
from a system page (the minimal mmap unit); typically 4096 bytes.
|
122 |
|
|
|
123 |
|
|
* Limitations
|
124 |
|
|
|
125 |
|
|
Here are some features that are NOT currently supported
|
126 |
|
|
|
127 |
|
|
* No user-definable hooks for callbacks and the like.
|
128 |
|
|
* No automated mechanism for fully checking that all accesses
|
129 |
|
|
to malloced memory stay within their bounds.
|
130 |
|
|
* No support for compaction.
|
131 |
|
|
|
132 |
|
|
* Synopsis of compile-time options:
|
133 |
|
|
|
134 |
|
|
People have reported using previous versions of this malloc on all
|
135 |
|
|
versions of Unix, sometimes by tweaking some of the defines
|
136 |
|
|
below. It has been tested most extensively on Solaris and
|
137 |
|
|
Linux. It is also reported to work on WIN32 platforms.
|
138 |
|
|
People have also reported adapting this malloc for use in
|
139 |
|
|
stand-alone embedded systems.
|
140 |
|
|
|
141 |
|
|
The implementation is in straight, hand-tuned ANSI C. Among other
|
142 |
|
|
consequences, it uses a lot of macros. Because of this, to be at
|
143 |
|
|
all usable, this code should be compiled using an optimizing compiler
|
144 |
|
|
(for example gcc -O2) that can simplify expressions and control
|
145 |
|
|
paths.
|
146 |
|
|
|
147 |
|
|
__STD_C (default: derived from C compiler defines)
|
148 |
|
|
Nonzero if using ANSI-standard C compiler, a C++ compiler, or
|
149 |
|
|
a C compiler sufficiently close to ANSI to get away with it.
|
150 |
|
|
DEBUG (default: NOT defined)
|
151 |
|
|
Define to enable debugging. Adds fairly extensive assertion-based
|
152 |
|
|
checking to help track down memory errors, but noticeably slows down
|
153 |
|
|
execution.
|
154 |
|
|
SEPARATE_OBJECTS (default: NOT defined)
|
155 |
|
|
Define this to compile into separate .o files. You must then
|
156 |
|
|
compile malloc.c several times, defining a DEFINE_* macro each
|
157 |
|
|
time. The list of DEFINE_* macros appears below.
|
158 |
|
|
MALLOC_LOCK (default: NOT defined)
|
159 |
|
|
MALLOC_UNLOCK (default: NOT defined)
|
160 |
|
|
Define these to C expressions which are run to lock and unlock
|
161 |
|
|
the malloc data structures. Calls may be nested; that is,
|
162 |
|
|
MALLOC_LOCK may be called more than once before the corresponding
|
163 |
|
|
MALLOC_UNLOCK calls. MALLOC_LOCK must avoid waiting for a lock
|
164 |
|
|
that it already holds.
|
165 |
|
|
REALLOC_ZERO_BYTES_FREES (default: NOT defined)
|
166 |
|
|
Define this if you think that realloc(p, 0) should be equivalent
|
167 |
|
|
to free(p). Otherwise, since malloc returns a unique pointer for
|
168 |
|
|
malloc(0), so does realloc(p, 0).
|
169 |
|
|
HAVE_MEMCPY (default: defined)
|
170 |
|
|
Define if you are not otherwise using ANSI STD C, but still
|
171 |
|
|
have memcpy and memset in your C library and want to use them.
|
172 |
|
|
Otherwise, simple internal versions are supplied.
|
173 |
|
|
USE_MEMCPY (default: 1 if HAVE_MEMCPY is defined, 0 otherwise)
|
174 |
|
|
Define as 1 if you want the C library versions of memset and
|
175 |
|
|
memcpy called in realloc and calloc (otherwise macro versions are used).
|
176 |
|
|
At least on some platforms, the simple macro versions usually
|
177 |
|
|
outperform libc versions.
|
178 |
|
|
HAVE_MMAP (default: defined as 1)
|
179 |
|
|
Define to non-zero to optionally make malloc() use mmap() to
|
180 |
|
|
allocate very large blocks.
|
181 |
|
|
HAVE_MREMAP (default: defined as 0 unless Linux libc set)
|
182 |
|
|
Define to non-zero to optionally make realloc() use mremap() to
|
183 |
|
|
reallocate very large blocks.
|
184 |
|
|
malloc_getpagesize (default: derived from system #includes)
|
185 |
|
|
Either a constant or routine call returning the system page size.
|
186 |
|
|
HAVE_USR_INCLUDE_MALLOC_H (default: NOT defined)
|
187 |
|
|
Optionally define if you are on a system with a /usr/include/malloc.h
|
188 |
|
|
that declares struct mallinfo. It is not at all necessary to
|
189 |
|
|
define this even if you do, but will ensure consistency.
|
190 |
|
|
INTERNAL_SIZE_T (default: size_t)
|
191 |
|
|
Define to a 32-bit type (probably `unsigned int') if you are on a
|
192 |
|
|
64-bit machine, yet do not want or need to allow malloc requests of
|
193 |
|
|
greater than 2^31 to be handled. This saves space, especially for
|
194 |
|
|
very small chunks.
|
195 |
|
|
INTERNAL_LINUX_C_LIB (default: NOT defined)
|
196 |
|
|
Defined only when compiled as part of Linux libc.
|
197 |
|
|
Also note that there is some odd internal name-mangling via defines
|
198 |
|
|
(for example, internally, `malloc' is named `mALLOc') needed
|
199 |
|
|
when compiling in this case. These look funny but don't otherwise
|
200 |
|
|
affect anything.
|
201 |
|
|
INTERNAL_NEWLIB (default: NOT defined)
|
202 |
|
|
Defined only when compiled as part of the Cygnus newlib
|
203 |
|
|
distribution.
|
204 |
|
|
WIN32 (default: undefined)
|
205 |
|
|
Define this on MS win (95, nt) platforms to compile in sbrk emulation.
|
206 |
|
|
LACKS_UNISTD_H (default: undefined)
|
207 |
|
|
Define this if your system does not have a <unistd.h>.
|
208 |
|
|
MORECORE (default: sbrk)
|
209 |
|
|
The name of the routine to call to obtain more memory from the system.
|
210 |
|
|
MORECORE_FAILURE (default: -1)
|
211 |
|
|
The value returned upon failure of MORECORE.
|
212 |
|
|
MORECORE_CLEARS (default 1)
|
213 |
|
|
True (1) if the routine mapped to MORECORE zeroes out memory (which
|
214 |
|
|
holds for sbrk).
|
215 |
|
|
DEFAULT_TRIM_THRESHOLD
|
216 |
|
|
DEFAULT_TOP_PAD
|
217 |
|
|
DEFAULT_MMAP_THRESHOLD
|
218 |
|
|
DEFAULT_MMAP_MAX
|
219 |
|
|
Default values of tunable parameters (described in detail below)
|
220 |
|
|
controlling interaction with host system routines (sbrk, mmap, etc).
|
221 |
|
|
These values may also be changed dynamically via mallopt(). The
|
222 |
|
|
preset defaults are those that give best performance for typical
|
223 |
|
|
programs/systems.
|
224 |
|
|
|
225 |
|
|
|
226 |
|
|
*/
|
227 |
|
|
|
228 |
|
|
|
229 |
|
|
|
230 |
|
|
|
231 |
|
|
/* Preliminaries */
|
232 |
|
|
|
233 |
|
|
#ifndef __STD_C
|
234 |
|
|
#ifdef __STDC__
|
235 |
|
|
#define __STD_C 1
|
236 |
|
|
#else
|
237 |
|
|
#if __cplusplus
|
238 |
|
|
#define __STD_C 1
|
239 |
|
|
#else
|
240 |
|
|
#define __STD_C 0
|
241 |
|
|
#endif /*__cplusplus*/
|
242 |
|
|
#endif /*__STDC__*/
|
243 |
|
|
#endif /*__STD_C*/
|
244 |
|
|
|
245 |
|
|
#ifndef Void_t
|
246 |
|
|
#if __STD_C
|
247 |
|
|
#define Void_t void
|
248 |
|
|
#else
|
249 |
|
|
#define Void_t char
|
250 |
|
|
#endif
|
251 |
|
|
#endif /*Void_t*/
|
252 |
|
|
|
253 |
|
|
#if __STD_C
|
254 |
|
|
#include <stddef.h> /* for size_t */
|
255 |
|
|
#else
|
256 |
|
|
#include <sys/types.h>
|
257 |
|
|
#endif
|
258 |
|
|
|
259 |
|
|
#ifdef __cplusplus
|
260 |
|
|
extern "C" {
|
261 |
|
|
#endif
|
262 |
|
|
|
263 |
|
|
#include <stdio.h> /* needed for malloc_stats */
|
264 |
|
|
|
265 |
|
|
|
266 |
|
|
/*
|
267 |
|
|
Compile-time options
|
268 |
|
|
*/
|
269 |
|
|
|
270 |
|
|
|
271 |
|
|
/*
|
272 |
|
|
|
273 |
|
|
Special defines for Cygnus newlib distribution.
|
274 |
|
|
|
275 |
|
|
*/
|
276 |
|
|
|
277 |
|
|
#ifdef INTERNAL_NEWLIB
|
278 |
|
|
|
279 |
|
|
#include <sys/config.h>
|
280 |
|
|
|
281 |
|
|
/*
|
282 |
|
|
In newlib, all the publically visible routines take a reentrancy
|
283 |
|
|
pointer. We don't currently do anything much with it, but we do
|
284 |
|
|
pass it to the lock routine.
|
285 |
|
|
*/
|
286 |
|
|
|
287 |
|
|
#include <reent.h>
|
288 |
|
|
|
289 |
|
|
#define POINTER_UINT unsigned _POINTER_INT
|
290 |
|
|
#define SEPARATE_OBJECTS
|
291 |
|
|
#define HAVE_MMAP 0
|
292 |
|
|
#define MORECORE(size) _sbrk_r(reent_ptr, (size))
|
293 |
|
|
#define MORECORE_CLEARS 0
|
294 |
|
|
#define MALLOC_LOCK __malloc_lock(reent_ptr)
|
295 |
|
|
#define MALLOC_UNLOCK __malloc_unlock(reent_ptr)
|
296 |
|
|
|
297 |
|
|
#ifndef _WIN32
|
298 |
|
|
#ifdef SMALL_MEMORY
|
299 |
|
|
#define malloc_getpagesize (128)
|
300 |
|
|
#else
|
301 |
|
|
#define malloc_getpagesize (4096)
|
302 |
|
|
#endif
|
303 |
|
|
#endif
|
304 |
|
|
|
305 |
|
|
#if __STD_C
|
306 |
|
|
extern void __malloc_lock(struct _reent *);
|
307 |
|
|
extern void __malloc_unlock(struct _reent *);
|
308 |
|
|
#else
|
309 |
|
|
extern void __malloc_lock();
|
310 |
|
|
extern void __malloc_unlock();
|
311 |
|
|
#endif
|
312 |
|
|
|
313 |
|
|
#if __STD_C
|
314 |
|
|
#define RARG struct _reent *reent_ptr,
|
315 |
|
|
#define RONEARG struct _reent *reent_ptr
|
316 |
|
|
#else
|
317 |
|
|
#define RARG reent_ptr
|
318 |
|
|
#define RONEARG reent_ptr
|
319 |
|
|
#define RDECL struct _reent *reent_ptr;
|
320 |
|
|
#endif
|
321 |
|
|
|
322 |
|
|
#define RCALL reent_ptr,
|
323 |
|
|
#define RONECALL reent_ptr
|
324 |
|
|
|
325 |
|
|
#else /* ! INTERNAL_NEWLIB */
|
326 |
|
|
|
327 |
|
|
#define POINTER_UINT unsigned long
|
328 |
|
|
#define RARG
|
329 |
|
|
#define RONEARG
|
330 |
|
|
#define RDECL
|
331 |
|
|
#define RCALL
|
332 |
|
|
#define RONECALL
|
333 |
|
|
|
334 |
|
|
#endif /* ! INTERNAL_NEWLIB */
|
335 |
|
|
|
336 |
|
|
/*
|
337 |
|
|
Debugging:
|
338 |
|
|
|
339 |
|
|
Because freed chunks may be overwritten with link fields, this
|
340 |
|
|
malloc will often die when freed memory is overwritten by user
|
341 |
|
|
programs. This can be very effective (albeit in an annoying way)
|
342 |
|
|
in helping track down dangling pointers.
|
343 |
|
|
|
344 |
|
|
If you compile with -DDEBUG, a number of assertion checks are
|
345 |
|
|
enabled that will catch more memory errors. You probably won't be
|
346 |
|
|
able to make much sense of the actual assertion errors, but they
|
347 |
|
|
should help you locate incorrectly overwritten memory. The
|
348 |
|
|
checking is fairly extensive, and will slow down execution
|
349 |
|
|
noticeably. Calling malloc_stats or mallinfo with DEBUG set will
|
350 |
|
|
attempt to check every non-mmapped allocated and free chunk in the
|
351 |
|
|
course of computing the summmaries. (By nature, mmapped regions
|
352 |
|
|
cannot be checked very much automatically.)
|
353 |
|
|
|
354 |
|
|
Setting DEBUG may also be helpful if you are trying to modify
|
355 |
|
|
this code. The assertions in the check routines spell out in more
|
356 |
|
|
detail the assumptions and invariants underlying the algorithms.
|
357 |
|
|
|
358 |
|
|
*/
|
359 |
|
|
|
360 |
|
|
#if DEBUG
|
361 |
|
|
#include <assert.h>
|
362 |
|
|
#else
|
363 |
|
|
#define assert(x) ((void)0)
|
364 |
|
|
#endif
|
365 |
|
|
|
366 |
|
|
|
367 |
|
|
/*
|
368 |
|
|
SEPARATE_OBJECTS should be defined if you want each function to go
|
369 |
|
|
into a separate .o file. You must then compile malloc.c once per
|
370 |
|
|
function, defining the appropriate DEFINE_ macro. See below for the
|
371 |
|
|
list of macros.
|
372 |
|
|
*/
|
373 |
|
|
|
374 |
|
|
#ifndef SEPARATE_OBJECTS
|
375 |
|
|
#define DEFINE_MALLOC
|
376 |
|
|
#define DEFINE_FREE
|
377 |
|
|
#define DEFINE_REALLOC
|
378 |
|
|
#define DEFINE_CALLOC
|
379 |
|
|
#define DEFINE_CFREE
|
380 |
|
|
#define DEFINE_MEMALIGN
|
381 |
|
|
#define DEFINE_VALLOC
|
382 |
|
|
#define DEFINE_PVALLOC
|
383 |
|
|
#define DEFINE_MALLINFO
|
384 |
|
|
#define DEFINE_MALLOC_STATS
|
385 |
|
|
#define DEFINE_MALLOC_USABLE_SIZE
|
386 |
|
|
#define DEFINE_MALLOPT
|
387 |
|
|
|
388 |
|
|
#define STATIC static
|
389 |
|
|
#else
|
390 |
|
|
#define STATIC
|
391 |
|
|
#endif
|
392 |
|
|
|
393 |
|
|
/*
|
394 |
|
|
Define MALLOC_LOCK and MALLOC_UNLOCK to C expressions to run to
|
395 |
|
|
lock and unlock the malloc data structures. MALLOC_LOCK may be
|
396 |
|
|
called recursively.
|
397 |
|
|
*/
|
398 |
|
|
|
399 |
|
|
#ifndef MALLOC_LOCK
|
400 |
|
|
#define MALLOC_LOCK
|
401 |
|
|
#endif
|
402 |
|
|
|
403 |
|
|
#ifndef MALLOC_UNLOCK
|
404 |
|
|
#define MALLOC_UNLOCK
|
405 |
|
|
#endif
|
406 |
|
|
|
407 |
|
|
/*
|
408 |
|
|
INTERNAL_SIZE_T is the word-size used for internal bookkeeping
|
409 |
|
|
of chunk sizes. On a 64-bit machine, you can reduce malloc
|
410 |
|
|
overhead by defining INTERNAL_SIZE_T to be a 32 bit `unsigned int'
|
411 |
|
|
at the expense of not being able to handle requests greater than
|
412 |
|
|
2^31. This limitation is hardly ever a concern; you are encouraged
|
413 |
|
|
to set this. However, the default version is the same as size_t.
|
414 |
|
|
*/
|
415 |
|
|
|
416 |
|
|
#ifndef INTERNAL_SIZE_T
|
417 |
|
|
#define INTERNAL_SIZE_T size_t
|
418 |
|
|
#endif
|
419 |
|
|
|
420 |
|
|
/*
|
421 |
|
|
REALLOC_ZERO_BYTES_FREES should be set if a call to
|
422 |
|
|
realloc with zero bytes should be the same as a call to free.
|
423 |
|
|
Some people think it should. Otherwise, since this malloc
|
424 |
|
|
returns a unique pointer for malloc(0), so does realloc(p, 0).
|
425 |
|
|
*/
|
426 |
|
|
|
427 |
|
|
|
428 |
|
|
/* #define REALLOC_ZERO_BYTES_FREES */
|
429 |
|
|
|
430 |
|
|
|
431 |
|
|
/*
|
432 |
|
|
WIN32 causes an emulation of sbrk to be compiled in
|
433 |
|
|
mmap-based options are not currently supported in WIN32.
|
434 |
|
|
*/
|
435 |
|
|
|
436 |
|
|
/* #define WIN32 */
|
437 |
|
|
#ifdef WIN32
|
438 |
|
|
#define MORECORE wsbrk
|
439 |
|
|
#define HAVE_MMAP 0
|
440 |
|
|
#endif
|
441 |
|
|
|
442 |
|
|
|
443 |
|
|
/*
|
444 |
|
|
HAVE_MEMCPY should be defined if you are not otherwise using
|
445 |
|
|
ANSI STD C, but still have memcpy and memset in your C library
|
446 |
|
|
and want to use them in calloc and realloc. Otherwise simple
|
447 |
|
|
macro versions are defined here.
|
448 |
|
|
|
449 |
|
|
USE_MEMCPY should be defined as 1 if you actually want to
|
450 |
|
|
have memset and memcpy called. People report that the macro
|
451 |
|
|
versions are often enough faster than libc versions on many
|
452 |
|
|
systems that it is better to use them.
|
453 |
|
|
|
454 |
|
|
*/
|
455 |
|
|
|
456 |
|
|
#define HAVE_MEMCPY
|
457 |
|
|
|
458 |
|
|
#ifndef USE_MEMCPY
|
459 |
|
|
#ifdef HAVE_MEMCPY
|
460 |
|
|
#define USE_MEMCPY 1
|
461 |
|
|
#else
|
462 |
|
|
#define USE_MEMCPY 0
|
463 |
|
|
#endif
|
464 |
|
|
#endif
|
465 |
|
|
|
466 |
|
|
#if (__STD_C || defined(HAVE_MEMCPY))
|
467 |
|
|
|
468 |
|
|
#if __STD_C
|
469 |
|
|
void* memset(void*, int, size_t);
|
470 |
|
|
void* memcpy(void*, const void*, size_t);
|
471 |
|
|
#else
|
472 |
|
|
Void_t* memset();
|
473 |
|
|
Void_t* memcpy();
|
474 |
|
|
#endif
|
475 |
|
|
#endif
|
476 |
|
|
|
477 |
|
|
#if USE_MEMCPY
|
478 |
|
|
|
479 |
|
|
/* The following macros are only invoked with (2n+1)-multiples of
|
480 |
|
|
INTERNAL_SIZE_T units, with a positive integer n. This is exploited
|
481 |
|
|
for fast inline execution when n is small. */
|
482 |
|
|
|
483 |
|
|
#define MALLOC_ZERO(charp, nbytes) \
|
484 |
|
|
do { \
|
485 |
|
|
INTERNAL_SIZE_T mzsz = (nbytes); \
|
486 |
|
|
if(mzsz <= 9*sizeof(mzsz)) { \
|
487 |
|
|
INTERNAL_SIZE_T* mz = (INTERNAL_SIZE_T*) (charp); \
|
488 |
|
|
if(mzsz >= 5*sizeof(mzsz)) { *mz++ = 0; \
|
489 |
|
|
*mz++ = 0; \
|
490 |
|
|
if(mzsz >= 7*sizeof(mzsz)) { *mz++ = 0; \
|
491 |
|
|
*mz++ = 0; \
|
492 |
|
|
if(mzsz >= 9*sizeof(mzsz)) { *mz++ = 0; \
|
493 |
|
|
*mz++ = 0; }}} \
|
494 |
|
|
*mz++ = 0; \
|
495 |
|
|
*mz++ = 0; \
|
496 |
|
|
*mz = 0; \
|
497 |
|
|
} else memset((charp), 0, mzsz); \
|
498 |
|
|
} while(0)
|
499 |
|
|
|
500 |
|
|
#define MALLOC_COPY(dest,src,nbytes) \
|
501 |
|
|
do { \
|
502 |
|
|
INTERNAL_SIZE_T mcsz = (nbytes); \
|
503 |
|
|
if(mcsz <= 9*sizeof(mcsz)) { \
|
504 |
|
|
INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) (src); \
|
505 |
|
|
INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) (dest); \
|
506 |
|
|
if(mcsz >= 5*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
|
507 |
|
|
*mcdst++ = *mcsrc++; \
|
508 |
|
|
if(mcsz >= 7*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
|
509 |
|
|
*mcdst++ = *mcsrc++; \
|
510 |
|
|
if(mcsz >= 9*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
|
511 |
|
|
*mcdst++ = *mcsrc++; }}} \
|
512 |
|
|
*mcdst++ = *mcsrc++; \
|
513 |
|
|
*mcdst++ = *mcsrc++; \
|
514 |
|
|
*mcdst = *mcsrc ; \
|
515 |
|
|
} else memcpy(dest, src, mcsz); \
|
516 |
|
|
} while(0)
|
517 |
|
|
|
518 |
|
|
#else /* !USE_MEMCPY */
|
519 |
|
|
|
520 |
|
|
/* Use Duff's device for good zeroing/copying performance. */
|
521 |
|
|
|
522 |
|
|
#define MALLOC_ZERO(charp, nbytes) \
|
523 |
|
|
do { \
|
524 |
|
|
INTERNAL_SIZE_T* mzp = (INTERNAL_SIZE_T*)(charp); \
|
525 |
|
|
long mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T), mcn; \
|
526 |
|
|
if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; } \
|
527 |
|
|
switch (mctmp) { \
|
528 |
|
|
case 0: for(;;) { *mzp++ = 0; \
|
529 |
|
|
case 7: *mzp++ = 0; \
|
530 |
|
|
case 6: *mzp++ = 0; \
|
531 |
|
|
case 5: *mzp++ = 0; \
|
532 |
|
|
case 4: *mzp++ = 0; \
|
533 |
|
|
case 3: *mzp++ = 0; \
|
534 |
|
|
case 2: *mzp++ = 0; \
|
535 |
|
|
case 1: *mzp++ = 0; if(mcn <= 0) break; mcn--; } \
|
536 |
|
|
} \
|
537 |
|
|
} while(0)
|
538 |
|
|
|
539 |
|
|
#define MALLOC_COPY(dest,src,nbytes) \
|
540 |
|
|
do { \
|
541 |
|
|
INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) src; \
|
542 |
|
|
INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) dest; \
|
543 |
|
|
long mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T), mcn; \
|
544 |
|
|
if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; } \
|
545 |
|
|
switch (mctmp) { \
|
546 |
|
|
case 0: for(;;) { *mcdst++ = *mcsrc++; \
|
547 |
|
|
case 7: *mcdst++ = *mcsrc++; \
|
548 |
|
|
case 6: *mcdst++ = *mcsrc++; \
|
549 |
|
|
case 5: *mcdst++ = *mcsrc++; \
|
550 |
|
|
case 4: *mcdst++ = *mcsrc++; \
|
551 |
|
|
case 3: *mcdst++ = *mcsrc++; \
|
552 |
|
|
case 2: *mcdst++ = *mcsrc++; \
|
553 |
|
|
case 1: *mcdst++ = *mcsrc++; if(mcn <= 0) break; mcn--; } \
|
554 |
|
|
} \
|
555 |
|
|
} while(0)
|
556 |
|
|
|
557 |
|
|
#endif
|
558 |
|
|
|
559 |
|
|
|
560 |
|
|
/*
|
561 |
|
|
Define HAVE_MMAP to optionally make malloc() use mmap() to
|
562 |
|
|
allocate very large blocks. These will be returned to the
|
563 |
|
|
operating system immediately after a free().
|
564 |
|
|
*/
|
565 |
|
|
|
566 |
|
|
#ifndef HAVE_MMAP
|
567 |
|
|
#define HAVE_MMAP 1
|
568 |
|
|
#endif
|
569 |
|
|
|
570 |
|
|
/*
|
571 |
|
|
Define HAVE_MREMAP to make realloc() use mremap() to re-allocate
|
572 |
|
|
large blocks. This is currently only possible on Linux with
|
573 |
|
|
kernel versions newer than 1.3.77.
|
574 |
|
|
*/
|
575 |
|
|
|
576 |
|
|
#ifndef HAVE_MREMAP
|
577 |
|
|
#ifdef INTERNAL_LINUX_C_LIB
|
578 |
|
|
#define HAVE_MREMAP 1
|
579 |
|
|
#else
|
580 |
|
|
#define HAVE_MREMAP 0
|
581 |
|
|
#endif
|
582 |
|
|
#endif
|
583 |
|
|
|
584 |
|
|
#if HAVE_MMAP
|
585 |
|
|
|
586 |
|
|
#include <unistd.h>
|
587 |
|
|
#include <fcntl.h>
|
588 |
|
|
#include <sys/mman.h>
|
589 |
|
|
|
590 |
|
|
#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
|
591 |
|
|
#define MAP_ANONYMOUS MAP_ANON
|
592 |
|
|
#endif
|
593 |
|
|
|
594 |
|
|
#endif /* HAVE_MMAP */
|
595 |
|
|
|
596 |
|
|
/*
|
597 |
|
|
Access to system page size. To the extent possible, this malloc
|
598 |
|
|
manages memory from the system in page-size units.
|
599 |
|
|
|
600 |
|
|
The following mechanics for getpagesize were adapted from
|
601 |
|
|
bsd/gnu getpagesize.h
|
602 |
|
|
*/
|
603 |
|
|
|
604 |
|
|
#ifndef LACKS_UNISTD_H
|
605 |
|
|
# include <unistd.h>
|
606 |
|
|
#endif
|
607 |
|
|
|
608 |
|
|
#ifndef malloc_getpagesize
|
609 |
|
|
# ifdef _SC_PAGESIZE /* some SVR4 systems omit an underscore */
|
610 |
|
|
# ifndef _SC_PAGE_SIZE
|
611 |
|
|
# define _SC_PAGE_SIZE _SC_PAGESIZE
|
612 |
|
|
# endif
|
613 |
|
|
# endif
|
614 |
|
|
# ifdef _SC_PAGE_SIZE
|
615 |
|
|
# define malloc_getpagesize sysconf(_SC_PAGE_SIZE)
|
616 |
|
|
# else
|
617 |
|
|
# if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
|
618 |
|
|
extern size_t getpagesize();
|
619 |
|
|
# define malloc_getpagesize getpagesize()
|
620 |
|
|
# else
|
621 |
|
|
# include <sys/param.h>
|
622 |
|
|
# ifdef EXEC_PAGESIZE
|
623 |
|
|
# define malloc_getpagesize EXEC_PAGESIZE
|
624 |
|
|
# else
|
625 |
|
|
# ifdef NBPG
|
626 |
|
|
# ifndef CLSIZE
|
627 |
|
|
# define malloc_getpagesize NBPG
|
628 |
|
|
# else
|
629 |
|
|
# define malloc_getpagesize (NBPG * CLSIZE)
|
630 |
|
|
# endif
|
631 |
|
|
# else
|
632 |
|
|
# ifdef NBPC
|
633 |
|
|
# define malloc_getpagesize NBPC
|
634 |
|
|
# else
|
635 |
|
|
# ifdef PAGESIZE
|
636 |
|
|
# define malloc_getpagesize PAGESIZE
|
637 |
|
|
# else
|
638 |
|
|
# define malloc_getpagesize (4096) /* just guess */
|
639 |
|
|
# endif
|
640 |
|
|
# endif
|
641 |
|
|
# endif
|
642 |
|
|
# endif
|
643 |
|
|
# endif
|
644 |
|
|
# endif
|
645 |
|
|
#endif
|
646 |
|
|
|
647 |
|
|
|
648 |
|
|
|
649 |
|
|
/*
|
650 |
|
|
|
651 |
|
|
This version of malloc supports the standard SVID/XPG mallinfo
|
652 |
|
|
routine that returns a struct containing the same kind of
|
653 |
|
|
information you can get from malloc_stats. It should work on
|
654 |
|
|
any SVID/XPG compliant system that has a /usr/include/malloc.h
|
655 |
|
|
defining struct mallinfo. (If you'd like to install such a thing
|
656 |
|
|
yourself, cut out the preliminary declarations as described above
|
657 |
|
|
and below and save them in a malloc.h file. But there's no
|
658 |
|
|
compelling reason to bother to do this.)
|
659 |
|
|
|
660 |
|
|
The main declaration needed is the mallinfo struct that is returned
|
661 |
|
|
(by-copy) by mallinfo(). The SVID/XPG malloinfo struct contains a
|
662 |
|
|
bunch of fields, most of which are not even meaningful in this
|
663 |
|
|
version of malloc. Some of these fields are are instead filled by
|
664 |
|
|
mallinfo() with other numbers that might possibly be of interest.
|
665 |
|
|
|
666 |
|
|
HAVE_USR_INCLUDE_MALLOC_H should be set if you have a
|
667 |
|
|
/usr/include/malloc.h file that includes a declaration of struct
|
668 |
|
|
mallinfo. If so, it is included; else an SVID2/XPG2 compliant
|
669 |
|
|
version is declared below. These must be precisely the same for
|
670 |
|
|
mallinfo() to work.
|
671 |
|
|
|
672 |
|
|
*/
|
673 |
|
|
|
674 |
|
|
/* #define HAVE_USR_INCLUDE_MALLOC_H */
|
675 |
|
|
|
676 |
|
|
#if HAVE_USR_INCLUDE_MALLOC_H
|
677 |
|
|
#include "/usr/include/malloc.h"
|
678 |
|
|
#else
|
679 |
|
|
|
680 |
|
|
/* SVID2/XPG mallinfo structure */
|
681 |
|
|
|
682 |
|
|
struct mallinfo {
|
683 |
|
|
int arena; /* total space allocated from system */
|
684 |
|
|
int ordblks; /* number of non-inuse chunks */
|
685 |
|
|
int smblks; /* unused -- always zero */
|
686 |
|
|
int hblks; /* number of mmapped regions */
|
687 |
|
|
int hblkhd; /* total space in mmapped regions */
|
688 |
|
|
int usmblks; /* unused -- always zero */
|
689 |
|
|
int fsmblks; /* unused -- always zero */
|
690 |
|
|
int uordblks; /* total allocated space */
|
691 |
|
|
int fordblks; /* total non-inuse space */
|
692 |
|
|
int keepcost; /* top-most, releasable (via malloc_trim) space */
|
693 |
|
|
};
|
694 |
|
|
|
695 |
|
|
/* SVID2/XPG mallopt options */
|
696 |
|
|
|
697 |
|
|
#define M_MXFAST 1 /* UNUSED in this malloc */
|
698 |
|
|
#define M_NLBLKS 2 /* UNUSED in this malloc */
|
699 |
|
|
#define M_GRAIN 3 /* UNUSED in this malloc */
|
700 |
|
|
#define M_KEEP 4 /* UNUSED in this malloc */
|
701 |
|
|
|
702 |
|
|
#endif
|
703 |
|
|
|
704 |
|
|
/* mallopt options that actually do something */
|
705 |
|
|
|
706 |
|
|
#define M_TRIM_THRESHOLD -1
|
707 |
|
|
#define M_TOP_PAD -2
|
708 |
|
|
#define M_MMAP_THRESHOLD -3
|
709 |
|
|
#define M_MMAP_MAX -4
|
710 |
|
|
|
711 |
|
|
|
712 |
|
|
|
713 |
|
|
#ifndef DEFAULT_TRIM_THRESHOLD
|
714 |
|
|
#define DEFAULT_TRIM_THRESHOLD (128L * 1024L)
|
715 |
|
|
#endif
|
716 |
|
|
|
717 |
|
|
/*
|
718 |
|
|
M_TRIM_THRESHOLD is the maximum amount of unused top-most memory
|
719 |
|
|
to keep before releasing via malloc_trim in free().
|
720 |
|
|
|
721 |
|
|
Automatic trimming is mainly useful in long-lived programs.
|
722 |
|
|
Because trimming via sbrk can be slow on some systems, and can
|
723 |
|
|
sometimes be wasteful (in cases where programs immediately
|
724 |
|
|
afterward allocate more large chunks) the value should be high
|
725 |
|
|
enough so that your overall system performance would improve by
|
726 |
|
|
releasing.
|
727 |
|
|
|
728 |
|
|
The trim threshold and the mmap control parameters (see below)
|
729 |
|
|
can be traded off with one another. Trimming and mmapping are
|
730 |
|
|
two different ways of releasing unused memory back to the
|
731 |
|
|
system. Between these two, it is often possible to keep
|
732 |
|
|
system-level demands of a long-lived program down to a bare
|
733 |
|
|
minimum. For example, in one test suite of sessions measuring
|
734 |
|
|
the XF86 X server on Linux, using a trim threshold of 128K and a
|
735 |
|
|
mmap threshold of 192K led to near-minimal long term resource
|
736 |
|
|
consumption.
|
737 |
|
|
|
738 |
|
|
If you are using this malloc in a long-lived program, it should
|
739 |
|
|
pay to experiment with these values. As a rough guide, you
|
740 |
|
|
might set to a value close to the average size of a process
|
741 |
|
|
(program) running on your system. Releasing this much memory
|
742 |
|
|
would allow such a process to run in memory. Generally, it's
|
743 |
|
|
worth it to tune for trimming rather tham memory mapping when a
|
744 |
|
|
program undergoes phases where several large chunks are
|
745 |
|
|
allocated and released in ways that can reuse each other's
|
746 |
|
|
storage, perhaps mixed with phases where there are no such
|
747 |
|
|
chunks at all. And in well-behaved long-lived programs,
|
748 |
|
|
controlling release of large blocks via trimming versus mapping
|
749 |
|
|
is usually faster.
|
750 |
|
|
|
751 |
|
|
However, in most programs, these parameters serve mainly as
|
752 |
|
|
protection against the system-level effects of carrying around
|
753 |
|
|
massive amounts of unneeded memory. Since frequent calls to
|
754 |
|
|
sbrk, mmap, and munmap otherwise degrade performance, the default
|
755 |
|
|
parameters are set to relatively high values that serve only as
|
756 |
|
|
safeguards.
|
757 |
|
|
|
758 |
|
|
The default trim value is high enough to cause trimming only in
|
759 |
|
|
fairly extreme (by current memory consumption standards) cases.
|
760 |
|
|
It must be greater than page size to have any useful effect. To
|
761 |
|
|
disable trimming completely, you can set to (unsigned long)(-1);
|
762 |
|
|
|
763 |
|
|
|
764 |
|
|
*/
|
765 |
|
|
|
766 |
|
|
|
767 |
|
|
#ifndef DEFAULT_TOP_PAD
|
768 |
|
|
#define DEFAULT_TOP_PAD (0)
|
769 |
|
|
#endif
|
770 |
|
|
|
771 |
|
|
/*
|
772 |
|
|
M_TOP_PAD is the amount of extra `padding' space to allocate or
|
773 |
|
|
retain whenever sbrk is called. It is used in two ways internally:
|
774 |
|
|
|
775 |
|
|
* When sbrk is called to extend the top of the arena to satisfy
|
776 |
|
|
a new malloc request, this much padding is added to the sbrk
|
777 |
|
|
request.
|
778 |
|
|
|
779 |
|
|
* When malloc_trim is called automatically from free(),
|
780 |
|
|
it is used as the `pad' argument.
|
781 |
|
|
|
782 |
|
|
In both cases, the actual amount of padding is rounded
|
783 |
|
|
so that the end of the arena is always a system page boundary.
|
784 |
|
|
|
785 |
|
|
The main reason for using padding is to avoid calling sbrk so
|
786 |
|
|
often. Having even a small pad greatly reduces the likelihood
|
787 |
|
|
that nearly every malloc request during program start-up (or
|
788 |
|
|
after trimming) will invoke sbrk, which needlessly wastes
|
789 |
|
|
time.
|
790 |
|
|
|
791 |
|
|
Automatic rounding-up to page-size units is normally sufficient
|
792 |
|
|
to avoid measurable overhead, so the default is 0. However, in
|
793 |
|
|
systems where sbrk is relatively slow, it can pay to increase
|
794 |
|
|
this value, at the expense of carrying around more memory than
|
795 |
|
|
the program needs.
|
796 |
|
|
|
797 |
|
|
*/
|
798 |
|
|
|
799 |
|
|
|
800 |
|
|
#ifndef DEFAULT_MMAP_THRESHOLD
|
801 |
|
|
#define DEFAULT_MMAP_THRESHOLD (128 * 1024)
|
802 |
|
|
#endif
|
803 |
|
|
|
804 |
|
|
/*
|
805 |
|
|
|
806 |
|
|
M_MMAP_THRESHOLD is the request size threshold for using mmap()
|
807 |
|
|
to service a request. Requests of at least this size that cannot
|
808 |
|
|
be allocated using already-existing space will be serviced via mmap.
|
809 |
|
|
(If enough normal freed space already exists it is used instead.)
|
810 |
|
|
|
811 |
|
|
Using mmap segregates relatively large chunks of memory so that
|
812 |
|
|
they can be individually obtained and released from the host
|
813 |
|
|
system. A request serviced through mmap is never reused by any
|
814 |
|
|
other request (at least not directly; the system may just so
|
815 |
|
|
happen to remap successive requests to the same locations).
|
816 |
|
|
|
817 |
|
|
Segregating space in this way has the benefit that mmapped space
|
818 |
|
|
can ALWAYS be individually released back to the system, which
|
819 |
|
|
helps keep the system level memory demands of a long-lived
|
820 |
|
|
program low. Mapped memory can never become `locked' between
|
821 |
|
|
other chunks, as can happen with normally allocated chunks, which
|
822 |
|
|
menas that even trimming via malloc_trim would not release them.
|
823 |
|
|
|
824 |
|
|
However, it has the disadvantages that:
|
825 |
|
|
|
826 |
|
|
1. The space cannot be reclaimed, consolidated, and then
|
827 |
|
|
used to service later requests, as happens with normal chunks.
|
828 |
|
|
2. It can lead to more wastage because of mmap page alignment
|
829 |
|
|
requirements
|
830 |
|
|
3. It causes malloc performance to be more dependent on host
|
831 |
|
|
system memory management support routines which may vary in
|
832 |
|
|
implementation quality and may impose arbitrary
|
833 |
|
|
limitations. Generally, servicing a request via normal
|
834 |
|
|
malloc steps is faster than going through a system's mmap.
|
835 |
|
|
|
836 |
|
|
All together, these considerations should lead you to use mmap
|
837 |
|
|
only for relatively large requests.
|
838 |
|
|
|
839 |
|
|
|
840 |
|
|
*/
|
841 |
|
|
|
842 |
|
|
|
843 |
|
|
|
844 |
|
|
#ifndef DEFAULT_MMAP_MAX
|
845 |
|
|
#if HAVE_MMAP
|
846 |
|
|
#define DEFAULT_MMAP_MAX (64)
|
847 |
|
|
#else
|
848 |
|
|
#define DEFAULT_MMAP_MAX (0)
|
849 |
|
|
#endif
|
850 |
|
|
#endif
|
851 |
|
|
|
852 |
|
|
/*
|
853 |
|
|
M_MMAP_MAX is the maximum number of requests to simultaneously
|
854 |
|
|
service using mmap. This parameter exists because:
|
855 |
|
|
|
856 |
|
|
1. Some systems have a limited number of internal tables for
|
857 |
|
|
use by mmap.
|
858 |
|
|
2. In most systems, overreliance on mmap can degrade overall
|
859 |
|
|
performance.
|
860 |
|
|
3. If a program allocates many large regions, it is probably
|
861 |
|
|
better off using normal sbrk-based allocation routines that
|
862 |
|
|
can reclaim and reallocate normal heap memory. Using a
|
863 |
|
|
small value allows transition into this mode after the
|
864 |
|
|
first few allocations.
|
865 |
|
|
|
866 |
|
|
Setting to 0 disables all use of mmap. If HAVE_MMAP is not set,
|
867 |
|
|
the default value is 0, and attempts to set it to non-zero values
|
868 |
|
|
in mallopt will fail.
|
869 |
|
|
*/
|
870 |
|
|
|
871 |
|
|
|
872 |
|
|
|
873 |
|
|
|
874 |
|
|
/*
|
875 |
|
|
|
876 |
|
|
Special defines for linux libc
|
877 |
|
|
|
878 |
|
|
Except when compiled using these special defines for Linux libc
|
879 |
|
|
using weak aliases, this malloc is NOT designed to work in
|
880 |
|
|
multithreaded applications. No semaphores or other concurrency
|
881 |
|
|
control are provided to ensure that multiple malloc or free calls
|
882 |
|
|
don't run at the same time, which could be disasterous. A single
|
883 |
|
|
semaphore could be used across malloc, realloc, and free (which is
|
884 |
|
|
essentially the effect of the linux weak alias approach). It would
|
885 |
|
|
be hard to obtain finer granularity.
|
886 |
|
|
|
887 |
|
|
*/
|
888 |
|
|
|
889 |
|
|
|
890 |
|
|
#ifdef INTERNAL_LINUX_C_LIB
|
891 |
|
|
|
892 |
|
|
#if __STD_C
|
893 |
|
|
|
894 |
|
|
Void_t * __default_morecore_init (ptrdiff_t);
|
895 |
|
|
Void_t *(*__morecore)(ptrdiff_t) = __default_morecore_init;
|
896 |
|
|
|
897 |
|
|
#else
|
898 |
|
|
|
899 |
|
|
Void_t * __default_morecore_init ();
|
900 |
|
|
Void_t *(*__morecore)() = __default_morecore_init;
|
901 |
|
|
|
902 |
|
|
#endif
|
903 |
|
|
|
904 |
|
|
#define MORECORE (*__morecore)
|
905 |
|
|
#define MORECORE_FAILURE 0
|
906 |
|
|
#define MORECORE_CLEARS 1
|
907 |
|
|
|
908 |
|
|
#else /* INTERNAL_LINUX_C_LIB */
|
909 |
|
|
|
910 |
|
|
#ifndef INTERNAL_NEWLIB
|
911 |
|
|
#if __STD_C
|
912 |
|
|
extern Void_t* sbrk(ptrdiff_t);
|
913 |
|
|
#else
|
914 |
|
|
extern Void_t* sbrk();
|
915 |
|
|
#endif
|
916 |
|
|
#endif
|
917 |
|
|
|
918 |
|
|
#ifndef MORECORE
|
919 |
|
|
#define MORECORE sbrk
|
920 |
|
|
#endif
|
921 |
|
|
|
922 |
|
|
#ifndef MORECORE_FAILURE
|
923 |
|
|
#define MORECORE_FAILURE -1
|
924 |
|
|
#endif
|
925 |
|
|
|
926 |
|
|
#ifndef MORECORE_CLEARS
|
927 |
|
|
#define MORECORE_CLEARS 1
|
928 |
|
|
#endif
|
929 |
|
|
|
930 |
|
|
#endif /* INTERNAL_LINUX_C_LIB */
|
931 |
|
|
|
932 |
|
|
#if defined(INTERNAL_LINUX_C_LIB) && defined(__ELF__)
|
933 |
|
|
|
934 |
|
|
#define cALLOc __libc_calloc
|
935 |
|
|
#define fREe __libc_free
|
936 |
|
|
#define mALLOc __libc_malloc
|
937 |
|
|
#define mEMALIGn __libc_memalign
|
938 |
|
|
#define rEALLOc __libc_realloc
|
939 |
|
|
#define vALLOc __libc_valloc
|
940 |
|
|
#define pvALLOc __libc_pvalloc
|
941 |
|
|
#define mALLINFo __libc_mallinfo
|
942 |
|
|
#define mALLOPt __libc_mallopt
|
943 |
|
|
|
944 |
|
|
#pragma weak calloc = __libc_calloc
|
945 |
|
|
#pragma weak free = __libc_free
|
946 |
|
|
#pragma weak cfree = __libc_free
|
947 |
|
|
#pragma weak malloc = __libc_malloc
|
948 |
|
|
#pragma weak memalign = __libc_memalign
|
949 |
|
|
#pragma weak realloc = __libc_realloc
|
950 |
|
|
#pragma weak valloc = __libc_valloc
|
951 |
|
|
#pragma weak pvalloc = __libc_pvalloc
|
952 |
|
|
#pragma weak mallinfo = __libc_mallinfo
|
953 |
|
|
#pragma weak mallopt = __libc_mallopt
|
954 |
|
|
|
955 |
|
|
#else
|
956 |
|
|
|
957 |
|
|
#ifdef INTERNAL_NEWLIB
|
958 |
|
|
|
959 |
|
|
#define cALLOc _calloc_r
|
960 |
|
|
#define fREe _free_r
|
961 |
|
|
#define mALLOc _malloc_r
|
962 |
|
|
#define mEMALIGn _memalign_r
|
963 |
|
|
#define rEALLOc _realloc_r
|
964 |
|
|
#define vALLOc _valloc_r
|
965 |
|
|
#define pvALLOc _pvalloc_r
|
966 |
|
|
#define mALLINFo _mallinfo_r
|
967 |
|
|
#define mALLOPt _mallopt_r
|
968 |
|
|
|
969 |
|
|
#define malloc_stats _malloc_stats_r
|
970 |
|
|
#define malloc_trim _malloc_trim_r
|
971 |
|
|
#define malloc_usable_size _malloc_usable_size_r
|
972 |
|
|
|
973 |
|
|
#define malloc_update_mallinfo __malloc_update_mallinfo
|
974 |
|
|
|
975 |
|
|
#define malloc_av_ __malloc_av_
|
976 |
|
|
#define malloc_current_mallinfo __malloc_current_mallinfo
|
977 |
|
|
#define malloc_max_sbrked_mem __malloc_max_sbrked_mem
|
978 |
|
|
#define malloc_max_total_mem __malloc_max_total_mem
|
979 |
|
|
#define malloc_sbrk_base __malloc_sbrk_base
|
980 |
|
|
#define malloc_top_pad __malloc_top_pad
|
981 |
|
|
#define malloc_trim_threshold __malloc_trim_threshold
|
982 |
|
|
|
983 |
|
|
#else /* ! INTERNAL_NEWLIB */
|
984 |
|
|
|
985 |
|
|
#define cALLOc calloc
|
986 |
|
|
#define fREe free
|
987 |
|
|
#define mALLOc malloc
|
988 |
|
|
#define mEMALIGn memalign
|
989 |
|
|
#define rEALLOc realloc
|
990 |
|
|
#define vALLOc valloc
|
991 |
|
|
#define pvALLOc pvalloc
|
992 |
|
|
#define mALLINFo mallinfo
|
993 |
|
|
#define mALLOPt mallopt
|
994 |
|
|
|
995 |
|
|
#endif /* ! INTERNAL_NEWLIB */
|
996 |
|
|
#endif
|
997 |
|
|
|
998 |
|
|
/* Public routines */
|
999 |
|
|
|
1000 |
|
|
#if __STD_C
|
1001 |
|
|
|
1002 |
|
|
Void_t* mALLOc(RARG size_t);
|
1003 |
|
|
void fREe(RARG Void_t*);
|
1004 |
|
|
Void_t* rEALLOc(RARG Void_t*, size_t);
|
1005 |
|
|
Void_t* mEMALIGn(RARG size_t, size_t);
|
1006 |
|
|
Void_t* vALLOc(RARG size_t);
|
1007 |
|
|
Void_t* pvALLOc(RARG size_t);
|
1008 |
|
|
Void_t* cALLOc(RARG size_t, size_t);
|
1009 |
|
|
void cfree(Void_t*);
|
1010 |
|
|
int malloc_trim(RARG size_t);
|
1011 |
|
|
size_t malloc_usable_size(RARG Void_t*);
|
1012 |
|
|
void malloc_stats(RONEARG);
|
1013 |
|
|
int mALLOPt(RARG int, int);
|
1014 |
|
|
struct mallinfo mALLINFo(RONEARG);
|
1015 |
|
|
#else
|
1016 |
|
|
Void_t* mALLOc();
|
1017 |
|
|
void fREe();
|
1018 |
|
|
Void_t* rEALLOc();
|
1019 |
|
|
Void_t* mEMALIGn();
|
1020 |
|
|
Void_t* vALLOc();
|
1021 |
|
|
Void_t* pvALLOc();
|
1022 |
|
|
Void_t* cALLOc();
|
1023 |
|
|
void cfree();
|
1024 |
|
|
int malloc_trim();
|
1025 |
|
|
size_t malloc_usable_size();
|
1026 |
|
|
void malloc_stats();
|
1027 |
|
|
int mALLOPt();
|
1028 |
|
|
struct mallinfo mALLINFo();
|
1029 |
|
|
#endif
|
1030 |
|
|
|
1031 |
|
|
|
1032 |
|
|
#ifdef __cplusplus
|
1033 |
|
|
}; /* end of extern "C" */
|
1034 |
|
|
#endif
|
1035 |
|
|
|
1036 |
|
|
/* ---------- To make a malloc.h, end cutting here ------------ */
|
1037 |
|
|
|
1038 |
|
|
|
1039 |
|
|
/*
|
1040 |
|
|
Emulation of sbrk for WIN32
|
1041 |
|
|
All code within the ifdef WIN32 is untested by me.
|
1042 |
|
|
*/
|
1043 |
|
|
|
1044 |
|
|
|
1045 |
|
|
#ifdef WIN32
|
1046 |
|
|
|
1047 |
|
|
#define AlignPage(add) (((add) + (malloc_getpagesize-1)) &
|
1048 |
|
|
~(malloc_getpagesize-1))
|
1049 |
|
|
|
1050 |
|
|
/* resrve 64MB to insure large contiguous space */
|
1051 |
|
|
#define RESERVED_SIZE (1024*1024*64)
|
1052 |
|
|
#define NEXT_SIZE (2048*1024)
|
1053 |
|
|
#define TOP_MEMORY ((unsigned long)2*1024*1024*1024)
|
1054 |
|
|
|
1055 |
|
|
struct GmListElement;
|
1056 |
|
|
typedef struct GmListElement GmListElement;
|
1057 |
|
|
|
1058 |
|
|
struct GmListElement
|
1059 |
|
|
{
|
1060 |
|
|
GmListElement* next;
|
1061 |
|
|
void* base;
|
1062 |
|
|
};
|
1063 |
|
|
|
1064 |
|
|
static GmListElement* head = 0;
|
1065 |
|
|
static unsigned int gNextAddress = 0;
|
1066 |
|
|
static unsigned int gAddressBase = 0;
|
1067 |
|
|
static unsigned int gAllocatedSize = 0;
|
1068 |
|
|
|
1069 |
|
|
static
|
1070 |
|
|
GmListElement* makeGmListElement (void* bas)
|
1071 |
|
|
{
|
1072 |
|
|
GmListElement* this;
|
1073 |
|
|
this = (GmListElement*)(void*)LocalAlloc (0, sizeof (GmListElement));
|
1074 |
|
|
ASSERT (this);
|
1075 |
|
|
if (this)
|
1076 |
|
|
{
|
1077 |
|
|
this->base = bas;
|
1078 |
|
|
this->next = head;
|
1079 |
|
|
head = this;
|
1080 |
|
|
}
|
1081 |
|
|
return this;
|
1082 |
|
|
}
|
1083 |
|
|
|
1084 |
|
|
void gcleanup ()
|
1085 |
|
|
{
|
1086 |
|
|
BOOL rval;
|
1087 |
|
|
ASSERT ( (head == NULL) || (head->base == (void*)gAddressBase));
|
1088 |
|
|
if (gAddressBase && (gNextAddress - gAddressBase))
|
1089 |
|
|
{
|
1090 |
|
|
rval = VirtualFree ((void*)gAddressBase,
|
1091 |
|
|
gNextAddress - gAddressBase,
|
1092 |
|
|
MEM_DECOMMIT);
|
1093 |
|
|
ASSERT (rval);
|
1094 |
|
|
}
|
1095 |
|
|
while (head)
|
1096 |
|
|
{
|
1097 |
|
|
GmListElement* next = head->next;
|
1098 |
|
|
rval = VirtualFree (head->base, 0, MEM_RELEASE);
|
1099 |
|
|
ASSERT (rval);
|
1100 |
|
|
LocalFree (head);
|
1101 |
|
|
head = next;
|
1102 |
|
|
}
|
1103 |
|
|
}
|
1104 |
|
|
|
1105 |
|
|
static
|
1106 |
|
|
void* findRegion (void* start_address, unsigned long size)
|
1107 |
|
|
{
|
1108 |
|
|
MEMORY_BASIC_INFORMATION info;
|
1109 |
|
|
while ((unsigned long)start_address < TOP_MEMORY)
|
1110 |
|
|
{
|
1111 |
|
|
VirtualQuery (start_address, &info, sizeof (info));
|
1112 |
|
|
if (info.State != MEM_FREE)
|
1113 |
|
|
start_address = (char*)info.BaseAddress + info.RegionSize;
|
1114 |
|
|
else if (info.RegionSize >= size)
|
1115 |
|
|
return start_address;
|
1116 |
|
|
else
|
1117 |
|
|
start_address = (char*)info.BaseAddress + info.RegionSize;
|
1118 |
|
|
}
|
1119 |
|
|
return NULL;
|
1120 |
|
|
|
1121 |
|
|
}
|
1122 |
|
|
|
1123 |
|
|
|
1124 |
|
|
void* wsbrk (long size)
|
1125 |
|
|
{
|
1126 |
|
|
void* tmp;
|
1127 |
|
|
if (size > 0)
|
1128 |
|
|
{
|
1129 |
|
|
if (gAddressBase == 0)
|
1130 |
|
|
{
|
1131 |
|
|
gAllocatedSize = max (RESERVED_SIZE, AlignPage (size));
|
1132 |
|
|
gNextAddress = gAddressBase =
|
1133 |
|
|
(unsigned int)VirtualAlloc (NULL, gAllocatedSize,
|
1134 |
|
|
MEM_RESERVE, PAGE_NOACCESS);
|
1135 |
|
|
} else if (AlignPage (gNextAddress + size) > (gAddressBase +
|
1136 |
|
|
gAllocatedSize))
|
1137 |
|
|
{
|
1138 |
|
|
long new_size = max (NEXT_SIZE, AlignPage (size));
|
1139 |
|
|
void* new_address = (void*)(gAddressBase+gAllocatedSize);
|
1140 |
|
|
do
|
1141 |
|
|
{
|
1142 |
|
|
new_address = findRegion (new_address, new_size);
|
1143 |
|
|
|
1144 |
|
|
if (new_address == 0)
|
1145 |
|
|
return (void*)-1;
|
1146 |
|
|
|
1147 |
|
|
gAddressBase = gNextAddress =
|
1148 |
|
|
(unsigned int)VirtualAlloc (new_address, new_size,
|
1149 |
|
|
MEM_RESERVE, PAGE_NOACCESS);
|
1150 |
|
|
// repeat in case of race condition
|
1151 |
|
|
// The region that we found has been snagged
|
1152 |
|
|
// by another thread
|
1153 |
|
|
}
|
1154 |
|
|
while (gAddressBase == 0);
|
1155 |
|
|
|
1156 |
|
|
ASSERT (new_address == (void*)gAddressBase);
|
1157 |
|
|
|
1158 |
|
|
gAllocatedSize = new_size;
|
1159 |
|
|
|
1160 |
|
|
if (!makeGmListElement ((void*)gAddressBase))
|
1161 |
|
|
return (void*)-1;
|
1162 |
|
|
}
|
1163 |
|
|
if ((size + gNextAddress) > AlignPage (gNextAddress))
|
1164 |
|
|
{
|
1165 |
|
|
void* res;
|
1166 |
|
|
res = VirtualAlloc ((void*)AlignPage (gNextAddress),
|
1167 |
|
|
(size + gNextAddress -
|
1168 |
|
|
AlignPage (gNextAddress)),
|
1169 |
|
|
MEM_COMMIT, PAGE_READWRITE);
|
1170 |
|
|
if (res == 0)
|
1171 |
|
|
return (void*)-1;
|
1172 |
|
|
}
|
1173 |
|
|
tmp = (void*)gNextAddress;
|
1174 |
|
|
gNextAddress = (unsigned int)tmp + size;
|
1175 |
|
|
return tmp;
|
1176 |
|
|
}
|
1177 |
|
|
else if (size < 0)
|
1178 |
|
|
{
|
1179 |
|
|
unsigned int alignedGoal = AlignPage (gNextAddress + size);
|
1180 |
|
|
/* Trim by releasing the virtual memory */
|
1181 |
|
|
if (alignedGoal >= gAddressBase)
|
1182 |
|
|
{
|
1183 |
|
|
VirtualFree ((void*)alignedGoal, gNextAddress - alignedGoal,
|
1184 |
|
|
MEM_DECOMMIT);
|
1185 |
|
|
gNextAddress = gNextAddress + size;
|
1186 |
|
|
return (void*)gNextAddress;
|
1187 |
|
|
}
|
1188 |
|
|
else
|
1189 |
|
|
{
|
1190 |
|
|
VirtualFree ((void*)gAddressBase, gNextAddress - gAddressBase,
|
1191 |
|
|
MEM_DECOMMIT);
|
1192 |
|
|
gNextAddress = gAddressBase;
|
1193 |
|
|
return (void*)-1;
|
1194 |
|
|
}
|
1195 |
|
|
}
|
1196 |
|
|
else
|
1197 |
|
|
{
|
1198 |
|
|
return (void*)gNextAddress;
|
1199 |
|
|
}
|
1200 |
|
|
}
|
1201 |
|
|
|
1202 |
|
|
#endif
|
1203 |
|
|
|
1204 |
|
|
|
1205 |
|
|
|
1206 |
|
|
/*
|
1207 |
|
|
Type declarations
|
1208 |
|
|
*/
|
1209 |
|
|
|
1210 |
|
|
|
1211 |
|
|
struct malloc_chunk
|
1212 |
|
|
{
|
1213 |
|
|
INTERNAL_SIZE_T prev_size; /* Size of previous chunk (if free). */
|
1214 |
|
|
INTERNAL_SIZE_T size; /* Size in bytes, including overhead. */
|
1215 |
|
|
struct malloc_chunk* fd; /* double links -- used only if free. */
|
1216 |
|
|
struct malloc_chunk* bk;
|
1217 |
|
|
};
|
1218 |
|
|
|
1219 |
|
|
typedef struct malloc_chunk* mchunkptr;
|
1220 |
|
|
|
1221 |
|
|
/*
|
1222 |
|
|
|
1223 |
|
|
malloc_chunk details:
|
1224 |
|
|
|
1225 |
|
|
(The following includes lightly edited explanations by Colin Plumb.)
|
1226 |
|
|
|
1227 |
|
|
Chunks of memory are maintained using a `boundary tag' method as
|
1228 |
|
|
described in e.g., Knuth or Standish. (See the paper by Paul
|
1229 |
|
|
Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a
|
1230 |
|
|
survey of such techniques.) Sizes of free chunks are stored both
|
1231 |
|
|
in the front of each chunk and at the end. This makes
|
1232 |
|
|
consolidating fragmented chunks into bigger chunks very fast. The
|
1233 |
|
|
size fields also hold bits representing whether chunks are free or
|
1234 |
|
|
in use.
|
1235 |
|
|
|
1236 |
|
|
An allocated chunk looks like this:
|
1237 |
|
|
|
1238 |
|
|
|
1239 |
|
|
chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
1240 |
|
|
| Size of previous chunk, if allocated | |
|
1241 |
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
1242 |
|
|
| Size of chunk, in bytes |P|
|
1243 |
|
|
mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
1244 |
|
|
| User data starts here... .
|
1245 |
|
|
. .
|
1246 |
|
|
. (malloc_usable_space() bytes) .
|
1247 |
|
|
. |
|
1248 |
|
|
nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
1249 |
|
|
| Size of chunk |
|
1250 |
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
1251 |
|
|
|
1252 |
|
|
|
1253 |
|
|
Where "chunk" is the front of the chunk for the purpose of most of
|
1254 |
|
|
the malloc code, but "mem" is the pointer that is returned to the
|
1255 |
|
|
user. "Nextchunk" is the beginning of the next contiguous chunk.
|
1256 |
|
|
|
1257 |
|
|
Chunks always begin on even word boundries, so the mem portion
|
1258 |
|
|
(which is returned to the user) is also on an even word boundary, and
|
1259 |
|
|
thus double-word aligned.
|
1260 |
|
|
|
1261 |
|
|
Free chunks are stored in circular doubly-linked lists, and look like this:
|
1262 |
|
|
|
1263 |
|
|
chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
1264 |
|
|
| Size of previous chunk |
|
1265 |
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
1266 |
|
|
`head:' | Size of chunk, in bytes |P|
|
1267 |
|
|
mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
1268 |
|
|
| Forward pointer to next chunk in list |
|
1269 |
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
1270 |
|
|
| Back pointer to previous chunk in list |
|
1271 |
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
1272 |
|
|
| Unused space (may be 0 bytes long) .
|
1273 |
|
|
. .
|
1274 |
|
|
. |
|
1275 |
|
|
nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
1276 |
|
|
`foot:' | Size of chunk, in bytes |
|
1277 |
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
1278 |
|
|
|
1279 |
|
|
The P (PREV_INUSE) bit, stored in the unused low-order bit of the
|
1280 |
|
|
chunk size (which is always a multiple of two words), is an in-use
|
1281 |
|
|
bit for the *previous* chunk. If that bit is *clear*, then the
|
1282 |
|
|
word before the current chunk size contains the previous chunk
|
1283 |
|
|
size, and can be used to find the front of the previous chunk.
|
1284 |
|
|
(The very first chunk allocated always has this bit set,
|
1285 |
|
|
preventing access to non-existent (or non-owned) memory.)
|
1286 |
|
|
|
1287 |
|
|
Note that the `foot' of the current chunk is actually represented
|
1288 |
|
|
as the prev_size of the NEXT chunk. (This makes it easier to
|
1289 |
|
|
deal with alignments etc).
|
1290 |
|
|
|
1291 |
|
|
The two exceptions to all this are
|
1292 |
|
|
|
1293 |
|
|
1. The special chunk `top', which doesn't bother using the
|
1294 |
|
|
trailing size field since there is no
|
1295 |
|
|
next contiguous chunk that would have to index off it. (After
|
1296 |
|
|
initialization, `top' is forced to always exist. If it would
|
1297 |
|
|
become less than MINSIZE bytes long, it is replenished via
|
1298 |
|
|
malloc_extend_top.)
|
1299 |
|
|
|
1300 |
|
|
2. Chunks allocated via mmap, which have the second-lowest-order
|
1301 |
|
|
bit (IS_MMAPPED) set in their size fields. Because they are
|
1302 |
|
|
never merged or traversed from any other chunk, they have no
|
1303 |
|
|
foot size or inuse information.
|
1304 |
|
|
|
1305 |
|
|
Available chunks are kept in any of several places (all declared below):
|
1306 |
|
|
|
1307 |
|
|
* `av': An array of chunks serving as bin headers for consolidated
|
1308 |
|
|
chunks. Each bin is doubly linked. The bins are approximately
|
1309 |
|
|
proportionally (log) spaced. There are a lot of these bins
|
1310 |
|
|
(128). This may look excessive, but works very well in
|
1311 |
|
|
practice. All procedures maintain the invariant that no
|
1312 |
|
|
consolidated chunk physically borders another one. Chunks in
|
1313 |
|
|
bins are kept in size order, with ties going to the
|
1314 |
|
|
approximately least recently used chunk.
|
1315 |
|
|
|
1316 |
|
|
The chunks in each bin are maintained in decreasing sorted order by
|
1317 |
|
|
size. This is irrelevant for the small bins, which all contain
|
1318 |
|
|
the same-sized chunks, but facilitates best-fit allocation for
|
1319 |
|
|
larger chunks. (These lists are just sequential. Keeping them in
|
1320 |
|
|
order almost never requires enough traversal to warrant using
|
1321 |
|
|
fancier ordered data structures.) Chunks of the same size are
|
1322 |
|
|
linked with the most recently freed at the front, and allocations
|
1323 |
|
|
are taken from the back. This results in LRU or FIFO allocation
|
1324 |
|
|
order, which tends to give each chunk an equal opportunity to be
|
1325 |
|
|
consolidated with adjacent freed chunks, resulting in larger free
|
1326 |
|
|
chunks and less fragmentation.
|
1327 |
|
|
|
1328 |
|
|
* `top': The top-most available chunk (i.e., the one bordering the
|
1329 |
|
|
end of available memory) is treated specially. It is never
|
1330 |
|
|
included in any bin, is used only if no other chunk is
|
1331 |
|
|
available, and is released back to the system if it is very
|
1332 |
|
|
large (see M_TRIM_THRESHOLD).
|
1333 |
|
|
|
1334 |
|
|
* `last_remainder': A bin holding only the remainder of the
|
1335 |
|
|
most recently split (non-top) chunk. This bin is checked
|
1336 |
|
|
before other non-fitting chunks, so as to provide better
|
1337 |
|
|
locality for runs of sequentially allocated chunks.
|
1338 |
|
|
|
1339 |
|
|
* Implicitly, through the host system's memory mapping tables.
|
1340 |
|
|
If supported, requests greater than a threshold are usually
|
1341 |
|
|
serviced via calls to mmap, and then later released via munmap.
|
1342 |
|
|
|
1343 |
|
|
*/
|
1344 |
|
|
|
1345 |
|
|
|
1346 |
|
|
|
1347 |
|
|
|
1348 |
|
|
|
1349 |
|
|
|
1350 |
|
|
/* sizes, alignments */
|
1351 |
|
|
|
1352 |
|
|
#define SIZE_SZ (sizeof(INTERNAL_SIZE_T))
|
1353 |
|
|
#ifndef MALLOC_ALIGNMENT
|
1354 |
|
|
#define MALLOC_ALIGNMENT (SIZE_SZ + SIZE_SZ)
|
1355 |
|
|
#endif
|
1356 |
|
|
#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
|
1357 |
|
|
#define MINSIZE (sizeof(struct malloc_chunk))
|
1358 |
|
|
|
1359 |
|
|
/* conversion from malloc headers to user pointers, and back */
|
1360 |
|
|
|
1361 |
|
|
#define chunk2mem(p) ((Void_t*)((char*)(p) + 2*SIZE_SZ))
|
1362 |
|
|
#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ))
|
1363 |
|
|
|
1364 |
|
|
/* pad request bytes into a usable size */
|
1365 |
|
|
|
1366 |
|
|
#define request2size(req) \
|
1367 |
|
|
(((long)((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) < \
|
1368 |
|
|
(long)(MINSIZE + MALLOC_ALIGN_MASK)) ? MINSIZE : \
|
1369 |
|
|
(((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) & ~(MALLOC_ALIGN_MASK)))
|
1370 |
|
|
|
1371 |
|
|
/* Check if m has acceptable alignment */
|
1372 |
|
|
|
1373 |
|
|
#define aligned_OK(m) (((unsigned long)((m)) & (MALLOC_ALIGN_MASK)) == 0)
|
1374 |
|
|
|
1375 |
|
|
|
1376 |
|
|
|
1377 |
|
|
|
1378 |
|
|
/*
|
1379 |
|
|
Physical chunk operations
|
1380 |
|
|
*/
|
1381 |
|
|
|
1382 |
|
|
|
1383 |
|
|
/* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */
|
1384 |
|
|
|
1385 |
|
|
#define PREV_INUSE 0x1
|
1386 |
|
|
|
1387 |
|
|
/* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */
|
1388 |
|
|
|
1389 |
|
|
#define IS_MMAPPED 0x2
|
1390 |
|
|
|
1391 |
|
|
/* Bits to mask off when extracting size */
|
1392 |
|
|
|
1393 |
|
|
#define SIZE_BITS (PREV_INUSE|IS_MMAPPED)
|
1394 |
|
|
|
1395 |
|
|
|
1396 |
|
|
/* Ptr to next physical malloc_chunk. */
|
1397 |
|
|
|
1398 |
|
|
#define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->size & ~PREV_INUSE) ))
|
1399 |
|
|
|
1400 |
|
|
/* Ptr to previous physical malloc_chunk */
|
1401 |
|
|
|
1402 |
|
|
#define prev_chunk(p)\
|
1403 |
|
|
((mchunkptr)( ((char*)(p)) - ((p)->prev_size) ))
|
1404 |
|
|
|
1405 |
|
|
|
1406 |
|
|
/* Treat space at ptr + offset as a chunk */
|
1407 |
|
|
|
1408 |
|
|
#define chunk_at_offset(p, s) ((mchunkptr)(((char*)(p)) + (s)))
|
1409 |
|
|
|
1410 |
|
|
|
1411 |
|
|
|
1412 |
|
|
|
1413 |
|
|
/*
|
1414 |
|
|
Dealing with use bits
|
1415 |
|
|
*/
|
1416 |
|
|
|
1417 |
|
|
/* extract p's inuse bit */
|
1418 |
|
|
|
1419 |
|
|
#define inuse(p)\
|
1420 |
|
|
((((mchunkptr)(((char*)(p))+((p)->size & ~PREV_INUSE)))->size) & PREV_INUSE)
|
1421 |
|
|
|
1422 |
|
|
/* extract inuse bit of previous chunk */
|
1423 |
|
|
|
1424 |
|
|
#define prev_inuse(p) ((p)->size & PREV_INUSE)
|
1425 |
|
|
|
1426 |
|
|
/* check for mmap()'ed chunk */
|
1427 |
|
|
|
1428 |
|
|
#define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED)
|
1429 |
|
|
|
1430 |
|
|
/* set/clear chunk as in use without otherwise disturbing */
|
1431 |
|
|
|
1432 |
|
|
#define set_inuse(p)\
|
1433 |
|
|
((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size |= PREV_INUSE
|
1434 |
|
|
|
1435 |
|
|
#define clear_inuse(p)\
|
1436 |
|
|
((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size &= ~(PREV_INUSE)
|
1437 |
|
|
|
1438 |
|
|
/* check/set/clear inuse bits in known places */
|
1439 |
|
|
|
1440 |
|
|
#define inuse_bit_at_offset(p, s)\
|
1441 |
|
|
(((mchunkptr)(((char*)(p)) + (s)))->size & PREV_INUSE)
|
1442 |
|
|
|
1443 |
|
|
#define set_inuse_bit_at_offset(p, s)\
|
1444 |
|
|
(((mchunkptr)(((char*)(p)) + (s)))->size |= PREV_INUSE)
|
1445 |
|
|
|
1446 |
|
|
#define clear_inuse_bit_at_offset(p, s)\
|
1447 |
|
|
(((mchunkptr)(((char*)(p)) + (s)))->size &= ~(PREV_INUSE))
|
1448 |
|
|
|
1449 |
|
|
|
1450 |
|
|
|
1451 |
|
|
|
1452 |
|
|
/*
|
1453 |
|
|
Dealing with size fields
|
1454 |
|
|
*/
|
1455 |
|
|
|
1456 |
|
|
/* Get size, ignoring use bits */
|
1457 |
|
|
|
1458 |
|
|
#define chunksize(p) ((p)->size & ~(SIZE_BITS))
|
1459 |
|
|
|
1460 |
|
|
/* Set size at head, without disturbing its use bit */
|
1461 |
|
|
|
1462 |
|
|
#define set_head_size(p, s) ((p)->size = (((p)->size & PREV_INUSE) | (s)))
|
1463 |
|
|
|
1464 |
|
|
/* Set size/use ignoring previous bits in header */
|
1465 |
|
|
|
1466 |
|
|
#define set_head(p, s) ((p)->size = (s))
|
1467 |
|
|
|
1468 |
|
|
/* Set size at footer (only when chunk is not in use) */
|
1469 |
|
|
|
1470 |
|
|
#define set_foot(p, s) (((mchunkptr)((char*)(p) + (s)))->prev_size = (s))
|
1471 |
|
|
|
1472 |
|
|
|
1473 |
|
|
|
1474 |
|
|
|
1475 |
|
|
|
1476 |
|
|
/*
|
1477 |
|
|
Bins
|
1478 |
|
|
|
1479 |
|
|
The bins, `av_' are an array of pairs of pointers serving as the
|
1480 |
|
|
heads of (initially empty) doubly-linked lists of chunks, laid out
|
1481 |
|
|
in a way so that each pair can be treated as if it were in a
|
1482 |
|
|
malloc_chunk. (This way, the fd/bk offsets for linking bin heads
|
1483 |
|
|
and chunks are the same).
|
1484 |
|
|
|
1485 |
|
|
Bins for sizes < 512 bytes contain chunks of all the same size, spaced
|
1486 |
|
|
8 bytes apart. Larger bins are approximately logarithmically
|
1487 |
|
|
spaced. (See the table below.) The `av_' array is never mentioned
|
1488 |
|
|
directly in the code, but instead via bin access macros.
|
1489 |
|
|
|
1490 |
|
|
Bin layout:
|
1491 |
|
|
|
1492 |
|
|
64 bins of size 8
|
1493 |
|
|
32 bins of size 64
|
1494 |
|
|
16 bins of size 512
|
1495 |
|
|
8 bins of size 4096
|
1496 |
|
|
4 bins of size 32768
|
1497 |
|
|
2 bins of size 262144
|
1498 |
|
|
1 bin of size what's left
|
1499 |
|
|
|
1500 |
|
|
There is actually a little bit of slop in the numbers in bin_index
|
1501 |
|
|
for the sake of speed. This makes no difference elsewhere.
|
1502 |
|
|
|
1503 |
|
|
The special chunks `top' and `last_remainder' get their own bins,
|
1504 |
|
|
(this is implemented via yet more trickery with the av_ array),
|
1505 |
|
|
although `top' is never properly linked to its bin since it is
|
1506 |
|
|
always handled specially.
|
1507 |
|
|
|
1508 |
|
|
*/
|
1509 |
|
|
|
1510 |
|
|
#ifdef SEPARATE_OBJECTS
|
1511 |
|
|
#define av_ malloc_av_
|
1512 |
|
|
#endif
|
1513 |
|
|
|
1514 |
|
|
#define NAV 128 /* number of bins */
|
1515 |
|
|
|
1516 |
|
|
typedef struct malloc_chunk* mbinptr;
|
1517 |
|
|
|
1518 |
|
|
/* access macros */
|
1519 |
|
|
|
1520 |
|
|
#define bin_at(i) ((mbinptr)((char*)&(av_[2*(i) + 2]) - 2*SIZE_SZ))
|
1521 |
|
|
#define next_bin(b) ((mbinptr)((char*)(b) + 2 * sizeof(mbinptr)))
|
1522 |
|
|
#define prev_bin(b) ((mbinptr)((char*)(b) - 2 * sizeof(mbinptr)))
|
1523 |
|
|
|
1524 |
|
|
/*
|
1525 |
|
|
The first 2 bins are never indexed. The corresponding av_ cells are instead
|
1526 |
|
|
used for bookkeeping. This is not to save space, but to simplify
|
1527 |
|
|
indexing, maintain locality, and avoid some initialization tests.
|
1528 |
|
|
*/
|
1529 |
|
|
|
1530 |
|
|
#define top (bin_at(0)->fd) /* The topmost chunk */
|
1531 |
|
|
#define last_remainder (bin_at(1)) /* remainder from last split */
|
1532 |
|
|
|
1533 |
|
|
|
1534 |
|
|
/*
|
1535 |
|
|
Because top initially points to its own bin with initial
|
1536 |
|
|
zero size, thus forcing extension on the first malloc request,
|
1537 |
|
|
we avoid having any special code in malloc to check whether
|
1538 |
|
|
it even exists yet. But we still need to in malloc_extend_top.
|
1539 |
|
|
*/
|
1540 |
|
|
|
1541 |
|
|
#define initial_top ((mchunkptr)(bin_at(0)))
|
1542 |
|
|
|
1543 |
|
|
/* Helper macro to initialize bins */
|
1544 |
|
|
|
1545 |
|
|
#define IAV(i) bin_at(i), bin_at(i)
|
1546 |
|
|
|
1547 |
|
|
#ifdef DEFINE_MALLOC
|
1548 |
|
|
STATIC mbinptr av_[NAV * 2 + 2] = {
|
1549 |
|
|
0, 0,
|
1550 |
|
|
IAV(0), IAV(1), IAV(2), IAV(3), IAV(4), IAV(5), IAV(6), IAV(7),
|
1551 |
|
|
IAV(8), IAV(9), IAV(10), IAV(11), IAV(12), IAV(13), IAV(14), IAV(15),
|
1552 |
|
|
IAV(16), IAV(17), IAV(18), IAV(19), IAV(20), IAV(21), IAV(22), IAV(23),
|
1553 |
|
|
IAV(24), IAV(25), IAV(26), IAV(27), IAV(28), IAV(29), IAV(30), IAV(31),
|
1554 |
|
|
IAV(32), IAV(33), IAV(34), IAV(35), IAV(36), IAV(37), IAV(38), IAV(39),
|
1555 |
|
|
IAV(40), IAV(41), IAV(42), IAV(43), IAV(44), IAV(45), IAV(46), IAV(47),
|
1556 |
|
|
IAV(48), IAV(49), IAV(50), IAV(51), IAV(52), IAV(53), IAV(54), IAV(55),
|
1557 |
|
|
IAV(56), IAV(57), IAV(58), IAV(59), IAV(60), IAV(61), IAV(62), IAV(63),
|
1558 |
|
|
IAV(64), IAV(65), IAV(66), IAV(67), IAV(68), IAV(69), IAV(70), IAV(71),
|
1559 |
|
|
IAV(72), IAV(73), IAV(74), IAV(75), IAV(76), IAV(77), IAV(78), IAV(79),
|
1560 |
|
|
IAV(80), IAV(81), IAV(82), IAV(83), IAV(84), IAV(85), IAV(86), IAV(87),
|
1561 |
|
|
IAV(88), IAV(89), IAV(90), IAV(91), IAV(92), IAV(93), IAV(94), IAV(95),
|
1562 |
|
|
IAV(96), IAV(97), IAV(98), IAV(99), IAV(100), IAV(101), IAV(102), IAV(103),
|
1563 |
|
|
IAV(104), IAV(105), IAV(106), IAV(107), IAV(108), IAV(109), IAV(110), IAV(111),
|
1564 |
|
|
IAV(112), IAV(113), IAV(114), IAV(115), IAV(116), IAV(117), IAV(118), IAV(119),
|
1565 |
|
|
IAV(120), IAV(121), IAV(122), IAV(123), IAV(124), IAV(125), IAV(126), IAV(127)
|
1566 |
|
|
};
|
1567 |
|
|
#else
|
1568 |
|
|
extern mbinptr av_[NAV * 2 + 2];
|
1569 |
|
|
#endif
|
1570 |
|
|
|
1571 |
|
|
|
1572 |
|
|
|
1573 |
|
|
/* field-extraction macros */
|
1574 |
|
|
|
1575 |
|
|
#define first(b) ((b)->fd)
|
1576 |
|
|
#define last(b) ((b)->bk)
|
1577 |
|
|
|
1578 |
|
|
/*
|
1579 |
|
|
Indexing into bins
|
1580 |
|
|
*/
|
1581 |
|
|
|
1582 |
|
|
#define bin_index(sz) \
|
1583 |
|
|
(((((unsigned long)(sz)) >> 9) == 0) ? (((unsigned long)(sz)) >> 3): \
|
1584 |
|
|
((((unsigned long)(sz)) >> 9) <= 4) ? 56 + (((unsigned long)(sz)) >> 6): \
|
1585 |
|
|
((((unsigned long)(sz)) >> 9) <= 20) ? 91 + (((unsigned long)(sz)) >> 9): \
|
1586 |
|
|
((((unsigned long)(sz)) >> 9) <= 84) ? 110 + (((unsigned long)(sz)) >> 12): \
|
1587 |
|
|
((((unsigned long)(sz)) >> 9) <= 340) ? 119 + (((unsigned long)(sz)) >> 15): \
|
1588 |
|
|
((((unsigned long)(sz)) >> 9) <= 1364) ? 124 + (((unsigned long)(sz)) >> 18): \
|
1589 |
|
|
126)
|
1590 |
|
|
/*
|
1591 |
|
|
bins for chunks < 512 are all spaced 8 bytes apart, and hold
|
1592 |
|
|
identically sized chunks. This is exploited in malloc.
|
1593 |
|
|
*/
|
1594 |
|
|
|
1595 |
|
|
#define MAX_SMALLBIN 63
|
1596 |
|
|
#define MAX_SMALLBIN_SIZE 512
|
1597 |
|
|
#define SMALLBIN_WIDTH 8
|
1598 |
|
|
|
1599 |
|
|
#define smallbin_index(sz) (((unsigned long)(sz)) >> 3)
|
1600 |
|
|
|
1601 |
|
|
/*
|
1602 |
|
|
Requests are `small' if both the corresponding and the next bin are small
|
1603 |
|
|
*/
|
1604 |
|
|
|
1605 |
|
|
#define is_small_request(nb) (nb < MAX_SMALLBIN_SIZE - SMALLBIN_WIDTH)
|
1606 |
|
|
|
1607 |
|
|
|
1608 |
|
|
|
1609 |
|
|
/*
|
1610 |
|
|
To help compensate for the large number of bins, a one-level index
|
1611 |
|
|
structure is used for bin-by-bin searching. `binblocks' is a
|
1612 |
|
|
one-word bitvector recording whether groups of BINBLOCKWIDTH bins
|
1613 |
|
|
have any (possibly) non-empty bins, so they can be skipped over
|
1614 |
|
|
all at once during during traversals. The bits are NOT always
|
1615 |
|
|
cleared as soon as all bins in a block are empty, but instead only
|
1616 |
|
|
when all are noticed to be empty during traversal in malloc.
|
1617 |
|
|
*/
|
1618 |
|
|
|
1619 |
|
|
#define BINBLOCKWIDTH 4 /* bins per block */
|
1620 |
|
|
|
1621 |
|
|
#define binblocks (bin_at(0)->size) /* bitvector of nonempty blocks */
|
1622 |
|
|
|
1623 |
|
|
/* bin<->block macros */
|
1624 |
|
|
|
1625 |
|
|
#define idx2binblock(ix) ((unsigned long)1 << (ix / BINBLOCKWIDTH))
|
1626 |
|
|
#define mark_binblock(ii) (binblocks |= idx2binblock(ii))
|
1627 |
|
|
#define clear_binblock(ii) (binblocks &= ~(idx2binblock(ii)))
|
1628 |
|
|
|
1629 |
|
|
|
1630 |
|
|
|
1631 |
|
|
|
1632 |
|
|
|
1633 |
|
|
/* Other static bookkeeping data */
|
1634 |
|
|
|
1635 |
|
|
#ifdef SEPARATE_OBJECTS
|
1636 |
|
|
#define trim_threshold malloc_trim_threshold
|
1637 |
|
|
#define top_pad malloc_top_pad
|
1638 |
|
|
#define n_mmaps_max malloc_n_mmaps_max
|
1639 |
|
|
#define mmap_threshold malloc_mmap_threshold
|
1640 |
|
|
#define sbrk_base malloc_sbrk_base
|
1641 |
|
|
#define max_sbrked_mem malloc_max_sbrked_mem
|
1642 |
|
|
#define max_total_mem malloc_max_total_mem
|
1643 |
|
|
#define current_mallinfo malloc_current_mallinfo
|
1644 |
|
|
#define n_mmaps malloc_n_mmaps
|
1645 |
|
|
#define max_n_mmaps malloc_max_n_mmaps
|
1646 |
|
|
#define mmapped_mem malloc_mmapped_mem
|
1647 |
|
|
#define max_mmapped_mem malloc_max_mmapped_mem
|
1648 |
|
|
#endif
|
1649 |
|
|
|
1650 |
|
|
/* variables holding tunable values */
|
1651 |
|
|
|
1652 |
|
|
#ifdef DEFINE_MALLOC
|
1653 |
|
|
|
1654 |
|
|
STATIC unsigned long trim_threshold = DEFAULT_TRIM_THRESHOLD;
|
1655 |
|
|
STATIC unsigned long top_pad = DEFAULT_TOP_PAD;
|
1656 |
|
|
#if HAVE_MMAP
|
1657 |
|
|
STATIC unsigned int n_mmaps_max = DEFAULT_MMAP_MAX;
|
1658 |
|
|
STATIC unsigned long mmap_threshold = DEFAULT_MMAP_THRESHOLD;
|
1659 |
|
|
#endif
|
1660 |
|
|
|
1661 |
|
|
/* The first value returned from sbrk */
|
1662 |
|
|
STATIC char* sbrk_base = (char*)(-1);
|
1663 |
|
|
|
1664 |
|
|
/* The maximum memory obtained from system via sbrk */
|
1665 |
|
|
STATIC unsigned long max_sbrked_mem = 0;
|
1666 |
|
|
|
1667 |
|
|
/* The maximum via either sbrk or mmap */
|
1668 |
|
|
STATIC unsigned long max_total_mem = 0;
|
1669 |
|
|
|
1670 |
|
|
/* internal working copy of mallinfo */
|
1671 |
|
|
STATIC struct mallinfo current_mallinfo = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
1672 |
|
|
|
1673 |
|
|
#if HAVE_MMAP
|
1674 |
|
|
|
1675 |
|
|
/* Tracking mmaps */
|
1676 |
|
|
|
1677 |
|
|
STATIC unsigned int n_mmaps = 0;
|
1678 |
|
|
STATIC unsigned int max_n_mmaps = 0;
|
1679 |
|
|
STATIC unsigned long mmapped_mem = 0;
|
1680 |
|
|
STATIC unsigned long max_mmapped_mem = 0;
|
1681 |
|
|
|
1682 |
|
|
#endif
|
1683 |
|
|
|
1684 |
|
|
#else /* ! DEFINE_MALLOC */
|
1685 |
|
|
|
1686 |
|
|
extern unsigned long trim_threshold;
|
1687 |
|
|
extern unsigned long top_pad;
|
1688 |
|
|
#if HAVE_MMAP
|
1689 |
|
|
extern unsigned int n_mmaps_max;
|
1690 |
|
|
extern unsigned long mmap_threshold;
|
1691 |
|
|
#endif
|
1692 |
|
|
extern char* sbrk_base;
|
1693 |
|
|
extern unsigned long max_sbrked_mem;
|
1694 |
|
|
extern unsigned long max_total_mem;
|
1695 |
|
|
extern struct mallinfo current_mallinfo;
|
1696 |
|
|
#if HAVE_MMAP
|
1697 |
|
|
extern unsigned int n_mmaps;
|
1698 |
|
|
extern unsigned int max_n_mmaps;
|
1699 |
|
|
extern unsigned long mmapped_mem;
|
1700 |
|
|
extern unsigned long max_mmapped_mem;
|
1701 |
|
|
#endif
|
1702 |
|
|
|
1703 |
|
|
#endif /* ! DEFINE_MALLOC */
|
1704 |
|
|
|
1705 |
|
|
/* The total memory obtained from system via sbrk */
|
1706 |
|
|
#define sbrked_mem (current_mallinfo.arena)
|
1707 |
|
|
|
1708 |
|
|
|
1709 |
|
|
|
1710 |
|
|
/*
|
1711 |
|
|
Debugging support
|
1712 |
|
|
*/
|
1713 |
|
|
|
1714 |
|
|
#if DEBUG
|
1715 |
|
|
|
1716 |
|
|
|
1717 |
|
|
/*
|
1718 |
|
|
These routines make a number of assertions about the states
|
1719 |
|
|
of data structures that should be true at all times. If any
|
1720 |
|
|
are not true, it's very likely that a user program has somehow
|
1721 |
|
|
trashed memory. (It's also possible that there is a coding error
|
1722 |
|
|
in malloc. In which case, please report it!)
|
1723 |
|
|
*/
|
1724 |
|
|
|
1725 |
|
|
#if __STD_C
|
1726 |
|
|
static void do_check_chunk(mchunkptr p)
|
1727 |
|
|
#else
|
1728 |
|
|
static void do_check_chunk(p) mchunkptr p;
|
1729 |
|
|
#endif
|
1730 |
|
|
{
|
1731 |
|
|
INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
|
1732 |
|
|
|
1733 |
|
|
/* No checkable chunk is mmapped */
|
1734 |
|
|
assert(!chunk_is_mmapped(p));
|
1735 |
|
|
|
1736 |
|
|
/* Check for legal address ... */
|
1737 |
|
|
assert((char*)p >= sbrk_base);
|
1738 |
|
|
if (p != top)
|
1739 |
|
|
assert((char*)p + sz <= (char*)top);
|
1740 |
|
|
else
|
1741 |
|
|
assert((char*)p + sz <= sbrk_base + sbrked_mem);
|
1742 |
|
|
|
1743 |
|
|
}
|
1744 |
|
|
|
1745 |
|
|
|
1746 |
|
|
#if __STD_C
|
1747 |
|
|
static void do_check_free_chunk(mchunkptr p)
|
1748 |
|
|
#else
|
1749 |
|
|
static void do_check_free_chunk(p) mchunkptr p;
|
1750 |
|
|
#endif
|
1751 |
|
|
{
|
1752 |
|
|
INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
|
1753 |
|
|
mchunkptr next = chunk_at_offset(p, sz);
|
1754 |
|
|
|
1755 |
|
|
do_check_chunk(p);
|
1756 |
|
|
|
1757 |
|
|
/* Check whether it claims to be free ... */
|
1758 |
|
|
assert(!inuse(p));
|
1759 |
|
|
|
1760 |
|
|
/* Unless a special marker, must have OK fields */
|
1761 |
|
|
if ((long)sz >= (long)MINSIZE)
|
1762 |
|
|
{
|
1763 |
|
|
assert((sz & MALLOC_ALIGN_MASK) == 0);
|
1764 |
|
|
assert(aligned_OK(chunk2mem(p)));
|
1765 |
|
|
/* ... matching footer field */
|
1766 |
|
|
assert(next->prev_size == sz);
|
1767 |
|
|
/* ... and is fully consolidated */
|
1768 |
|
|
assert(prev_inuse(p));
|
1769 |
|
|
assert (next == top || inuse(next));
|
1770 |
|
|
|
1771 |
|
|
/* ... and has minimally sane links */
|
1772 |
|
|
assert(p->fd->bk == p);
|
1773 |
|
|
assert(p->bk->fd == p);
|
1774 |
|
|
}
|
1775 |
|
|
else /* markers are always of size SIZE_SZ */
|
1776 |
|
|
assert(sz == SIZE_SZ);
|
1777 |
|
|
}
|
1778 |
|
|
|
1779 |
|
|
#if __STD_C
|
1780 |
|
|
static void do_check_inuse_chunk(mchunkptr p)
|
1781 |
|
|
#else
|
1782 |
|
|
static void do_check_inuse_chunk(p) mchunkptr p;
|
1783 |
|
|
#endif
|
1784 |
|
|
{
|
1785 |
|
|
mchunkptr next = next_chunk(p);
|
1786 |
|
|
do_check_chunk(p);
|
1787 |
|
|
|
1788 |
|
|
/* Check whether it claims to be in use ... */
|
1789 |
|
|
assert(inuse(p));
|
1790 |
|
|
|
1791 |
|
|
/* ... and is surrounded by OK chunks.
|
1792 |
|
|
Since more things can be checked with free chunks than inuse ones,
|
1793 |
|
|
if an inuse chunk borders them and debug is on, it's worth doing them.
|
1794 |
|
|
*/
|
1795 |
|
|
if (!prev_inuse(p))
|
1796 |
|
|
{
|
1797 |
|
|
mchunkptr prv = prev_chunk(p);
|
1798 |
|
|
assert(next_chunk(prv) == p);
|
1799 |
|
|
do_check_free_chunk(prv);
|
1800 |
|
|
}
|
1801 |
|
|
if (next == top)
|
1802 |
|
|
{
|
1803 |
|
|
assert(prev_inuse(next));
|
1804 |
|
|
assert(chunksize(next) >= MINSIZE);
|
1805 |
|
|
}
|
1806 |
|
|
else if (!inuse(next))
|
1807 |
|
|
do_check_free_chunk(next);
|
1808 |
|
|
|
1809 |
|
|
}
|
1810 |
|
|
|
1811 |
|
|
#if __STD_C
|
1812 |
|
|
static void do_check_malloced_chunk(mchunkptr p, INTERNAL_SIZE_T s)
|
1813 |
|
|
#else
|
1814 |
|
|
static void do_check_malloced_chunk(p, s) mchunkptr p; INTERNAL_SIZE_T s;
|
1815 |
|
|
#endif
|
1816 |
|
|
{
|
1817 |
|
|
INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
|
1818 |
|
|
long room = sz - s;
|
1819 |
|
|
|
1820 |
|
|
do_check_inuse_chunk(p);
|
1821 |
|
|
|
1822 |
|
|
/* Legal size ... */
|
1823 |
|
|
assert((long)sz >= (long)MINSIZE);
|
1824 |
|
|
assert((sz & MALLOC_ALIGN_MASK) == 0);
|
1825 |
|
|
assert(room >= 0);
|
1826 |
|
|
assert(room < (long)MINSIZE);
|
1827 |
|
|
|
1828 |
|
|
/* ... and alignment */
|
1829 |
|
|
assert(aligned_OK(chunk2mem(p)));
|
1830 |
|
|
|
1831 |
|
|
|
1832 |
|
|
/* ... and was allocated at front of an available chunk */
|
1833 |
|
|
assert(prev_inuse(p));
|
1834 |
|
|
|
1835 |
|
|
}
|
1836 |
|
|
|
1837 |
|
|
|
1838 |
|
|
#define check_free_chunk(P) do_check_free_chunk(P)
|
1839 |
|
|
#define check_inuse_chunk(P) do_check_inuse_chunk(P)
|
1840 |
|
|
#define check_chunk(P) do_check_chunk(P)
|
1841 |
|
|
#define check_malloced_chunk(P,N) do_check_malloced_chunk(P,N)
|
1842 |
|
|
#else
|
1843 |
|
|
#define check_free_chunk(P)
|
1844 |
|
|
#define check_inuse_chunk(P)
|
1845 |
|
|
#define check_chunk(P)
|
1846 |
|
|
#define check_malloced_chunk(P,N)
|
1847 |
|
|
#endif
|
1848 |
|
|
|
1849 |
|
|
|
1850 |
|
|
|
1851 |
|
|
/*
|
1852 |
|
|
Macro-based internal utilities
|
1853 |
|
|
*/
|
1854 |
|
|
|
1855 |
|
|
|
1856 |
|
|
/*
|
1857 |
|
|
Linking chunks in bin lists.
|
1858 |
|
|
Call these only with variables, not arbitrary expressions, as arguments.
|
1859 |
|
|
*/
|
1860 |
|
|
|
1861 |
|
|
/*
|
1862 |
|
|
Place chunk p of size s in its bin, in size order,
|
1863 |
|
|
putting it ahead of others of same size.
|
1864 |
|
|
*/
|
1865 |
|
|
|
1866 |
|
|
|
1867 |
|
|
#define frontlink(P, S, IDX, BK, FD) \
|
1868 |
|
|
{ \
|
1869 |
|
|
if (S < MAX_SMALLBIN_SIZE) \
|
1870 |
|
|
{ \
|
1871 |
|
|
IDX = smallbin_index(S); \
|
1872 |
|
|
mark_binblock(IDX); \
|
1873 |
|
|
BK = bin_at(IDX); \
|
1874 |
|
|
FD = BK->fd; \
|
1875 |
|
|
P->bk = BK; \
|
1876 |
|
|
P->fd = FD; \
|
1877 |
|
|
FD->bk = BK->fd = P; \
|
1878 |
|
|
} \
|
1879 |
|
|
else \
|
1880 |
|
|
{ \
|
1881 |
|
|
IDX = bin_index(S); \
|
1882 |
|
|
BK = bin_at(IDX); \
|
1883 |
|
|
FD = BK->fd; \
|
1884 |
|
|
if (FD == BK) mark_binblock(IDX); \
|
1885 |
|
|
else \
|
1886 |
|
|
{ \
|
1887 |
|
|
while (FD != BK && S < chunksize(FD)) FD = FD->fd; \
|
1888 |
|
|
BK = FD->bk; \
|
1889 |
|
|
} \
|
1890 |
|
|
P->bk = BK; \
|
1891 |
|
|
P->fd = FD; \
|
1892 |
|
|
FD->bk = BK->fd = P; \
|
1893 |
|
|
} \
|
1894 |
|
|
}
|
1895 |
|
|
|
1896 |
|
|
|
1897 |
|
|
/* take a chunk off a list */
|
1898 |
|
|
|
1899 |
|
|
#define unlink(P, BK, FD) \
|
1900 |
|
|
{ \
|
1901 |
|
|
BK = P->bk; \
|
1902 |
|
|
FD = P->fd; \
|
1903 |
|
|
FD->bk = BK; \
|
1904 |
|
|
BK->fd = FD; \
|
1905 |
|
|
} \
|
1906 |
|
|
|
1907 |
|
|
/* Place p as the last remainder */
|
1908 |
|
|
|
1909 |
|
|
#define link_last_remainder(P) \
|
1910 |
|
|
{ \
|
1911 |
|
|
last_remainder->fd = last_remainder->bk = P; \
|
1912 |
|
|
P->fd = P->bk = last_remainder; \
|
1913 |
|
|
}
|
1914 |
|
|
|
1915 |
|
|
/* Clear the last_remainder bin */
|
1916 |
|
|
|
1917 |
|
|
#define clear_last_remainder \
|
1918 |
|
|
(last_remainder->fd = last_remainder->bk = last_remainder)
|
1919 |
|
|
|
1920 |
|
|
|
1921 |
|
|
|
1922 |
|
|
|
1923 |
|
|
|
1924 |
|
|
|
1925 |
|
|
/* Routines dealing with mmap(). */
|
1926 |
|
|
|
1927 |
|
|
#if HAVE_MMAP
|
1928 |
|
|
|
1929 |
|
|
#ifdef DEFINE_MALLOC
|
1930 |
|
|
|
1931 |
|
|
#if __STD_C
|
1932 |
|
|
static mchunkptr mmap_chunk(size_t size)
|
1933 |
|
|
#else
|
1934 |
|
|
static mchunkptr mmap_chunk(size) size_t size;
|
1935 |
|
|
#endif
|
1936 |
|
|
{
|
1937 |
|
|
size_t page_mask = malloc_getpagesize - 1;
|
1938 |
|
|
mchunkptr p;
|
1939 |
|
|
|
1940 |
|
|
#ifndef MAP_ANONYMOUS
|
1941 |
|
|
static int fd = -1;
|
1942 |
|
|
#endif
|
1943 |
|
|
|
1944 |
|
|
if(n_mmaps >= n_mmaps_max) return 0; /* too many regions */
|
1945 |
|
|
|
1946 |
|
|
/* For mmapped chunks, the overhead is one SIZE_SZ unit larger, because
|
1947 |
|
|
* there is no following chunk whose prev_size field could be used.
|
1948 |
|
|
*/
|
1949 |
|
|
size = (size + SIZE_SZ + page_mask) & ~page_mask;
|
1950 |
|
|
|
1951 |
|
|
#ifdef MAP_ANONYMOUS
|
1952 |
|
|
p = (mchunkptr)mmap(0, size, PROT_READ|PROT_WRITE,
|
1953 |
|
|
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
|
1954 |
|
|
#else /* !MAP_ANONYMOUS */
|
1955 |
|
|
if (fd < 0)
|
1956 |
|
|
{
|
1957 |
|
|
fd = open("/dev/zero", O_RDWR);
|
1958 |
|
|
if(fd < 0) return 0;
|
1959 |
|
|
}
|
1960 |
|
|
p = (mchunkptr)mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
|
1961 |
|
|
#endif
|
1962 |
|
|
|
1963 |
|
|
if(p == (mchunkptr)-1) return 0;
|
1964 |
|
|
|
1965 |
|
|
n_mmaps++;
|
1966 |
|
|
if (n_mmaps > max_n_mmaps) max_n_mmaps = n_mmaps;
|
1967 |
|
|
|
1968 |
|
|
/* We demand that eight bytes into a page must be 8-byte aligned. */
|
1969 |
|
|
assert(aligned_OK(chunk2mem(p)));
|
1970 |
|
|
|
1971 |
|
|
/* The offset to the start of the mmapped region is stored
|
1972 |
|
|
* in the prev_size field of the chunk; normally it is zero,
|
1973 |
|
|
* but that can be changed in memalign().
|
1974 |
|
|
*/
|
1975 |
|
|
p->prev_size = 0;
|
1976 |
|
|
set_head(p, size|IS_MMAPPED);
|
1977 |
|
|
|
1978 |
|
|
mmapped_mem += size;
|
1979 |
|
|
if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem)
|
1980 |
|
|
max_mmapped_mem = mmapped_mem;
|
1981 |
|
|
if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem)
|
1982 |
|
|
max_total_mem = mmapped_mem + sbrked_mem;
|
1983 |
|
|
return p;
|
1984 |
|
|
}
|
1985 |
|
|
|
1986 |
|
|
#endif /* DEFINE_MALLOC */
|
1987 |
|
|
|
1988 |
|
|
#ifdef SEPARATE_OBJECTS
|
1989 |
|
|
#define munmap_chunk malloc_munmap_chunk
|
1990 |
|
|
#endif
|
1991 |
|
|
|
1992 |
|
|
#ifdef DEFINE_FREE
|
1993 |
|
|
|
1994 |
|
|
#if __STD_C
|
1995 |
|
|
STATIC void munmap_chunk(mchunkptr p)
|
1996 |
|
|
#else
|
1997 |
|
|
STATIC void munmap_chunk(p) mchunkptr p;
|
1998 |
|
|
#endif
|
1999 |
|
|
{
|
2000 |
|
|
INTERNAL_SIZE_T size = chunksize(p);
|
2001 |
|
|
int ret;
|
2002 |
|
|
|
2003 |
|
|
assert (chunk_is_mmapped(p));
|
2004 |
|
|
assert(! ((char*)p >= sbrk_base && (char*)p < sbrk_base + sbrked_mem));
|
2005 |
|
|
assert((n_mmaps > 0));
|
2006 |
|
|
assert(((p->prev_size + size) & (malloc_getpagesize-1)) == 0);
|
2007 |
|
|
|
2008 |
|
|
n_mmaps--;
|
2009 |
|
|
mmapped_mem -= (size + p->prev_size);
|
2010 |
|
|
|
2011 |
|
|
ret = munmap((char *)p - p->prev_size, size + p->prev_size);
|
2012 |
|
|
|
2013 |
|
|
/* munmap returns non-zero on failure */
|
2014 |
|
|
assert(ret == 0);
|
2015 |
|
|
}
|
2016 |
|
|
|
2017 |
|
|
#else /* ! DEFINE_FREE */
|
2018 |
|
|
|
2019 |
|
|
#if __STD_C
|
2020 |
|
|
extern void munmap_chunk(mchunkptr);
|
2021 |
|
|
#else
|
2022 |
|
|
extern void munmap_chunk();
|
2023 |
|
|
#endif
|
2024 |
|
|
|
2025 |
|
|
#endif /* ! DEFINE_FREE */
|
2026 |
|
|
|
2027 |
|
|
#if HAVE_MREMAP
|
2028 |
|
|
|
2029 |
|
|
#ifdef DEFINE_REALLOC
|
2030 |
|
|
|
2031 |
|
|
#if __STD_C
|
2032 |
|
|
static mchunkptr mremap_chunk(mchunkptr p, size_t new_size)
|
2033 |
|
|
#else
|
2034 |
|
|
static mchunkptr mremap_chunk(p, new_size) mchunkptr p; size_t new_size;
|
2035 |
|
|
#endif
|
2036 |
|
|
{
|
2037 |
|
|
size_t page_mask = malloc_getpagesize - 1;
|
2038 |
|
|
INTERNAL_SIZE_T offset = p->prev_size;
|
2039 |
|
|
INTERNAL_SIZE_T size = chunksize(p);
|
2040 |
|
|
char *cp;
|
2041 |
|
|
|
2042 |
|
|
assert (chunk_is_mmapped(p));
|
2043 |
|
|
assert(! ((char*)p >= sbrk_base && (char*)p < sbrk_base + sbrked_mem));
|
2044 |
|
|
assert((n_mmaps > 0));
|
2045 |
|
|
assert(((size + offset) & (malloc_getpagesize-1)) == 0);
|
2046 |
|
|
|
2047 |
|
|
/* Note the extra SIZE_SZ overhead as in mmap_chunk(). */
|
2048 |
|
|
new_size = (new_size + offset + SIZE_SZ + page_mask) & ~page_mask;
|
2049 |
|
|
|
2050 |
|
|
cp = (char *)mremap((char *)p - offset, size + offset, new_size, 1);
|
2051 |
|
|
|
2052 |
|
|
if (cp == (char *)-1) return 0;
|
2053 |
|
|
|
2054 |
|
|
p = (mchunkptr)(cp + offset);
|
2055 |
|
|
|
2056 |
|
|
assert(aligned_OK(chunk2mem(p)));
|
2057 |
|
|
|
2058 |
|
|
assert((p->prev_size == offset));
|
2059 |
|
|
set_head(p, (new_size - offset)|IS_MMAPPED);
|
2060 |
|
|
|
2061 |
|
|
mmapped_mem -= size + offset;
|
2062 |
|
|
mmapped_mem += new_size;
|
2063 |
|
|
if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem)
|
2064 |
|
|
max_mmapped_mem = mmapped_mem;
|
2065 |
|
|
if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem)
|
2066 |
|
|
max_total_mem = mmapped_mem + sbrked_mem;
|
2067 |
|
|
return p;
|
2068 |
|
|
}
|
2069 |
|
|
|
2070 |
|
|
#endif /* DEFINE_REALLOC */
|
2071 |
|
|
|
2072 |
|
|
#endif /* HAVE_MREMAP */
|
2073 |
|
|
|
2074 |
|
|
#endif /* HAVE_MMAP */
|
2075 |
|
|
|
2076 |
|
|
|
2077 |
|
|
|
2078 |
|
|
|
2079 |
|
|
#ifdef DEFINE_MALLOC
|
2080 |
|
|
|
2081 |
|
|
/*
|
2082 |
|
|
Extend the top-most chunk by obtaining memory from system.
|
2083 |
|
|
Main interface to sbrk (but see also malloc_trim).
|
2084 |
|
|
*/
|
2085 |
|
|
|
2086 |
|
|
#if __STD_C
|
2087 |
|
|
static void malloc_extend_top(RARG INTERNAL_SIZE_T nb)
|
2088 |
|
|
#else
|
2089 |
|
|
static void malloc_extend_top(RARG nb) RDECL INTERNAL_SIZE_T nb;
|
2090 |
|
|
#endif
|
2091 |
|
|
{
|
2092 |
|
|
char* brk; /* return value from sbrk */
|
2093 |
|
|
INTERNAL_SIZE_T front_misalign; /* unusable bytes at front of sbrked space */
|
2094 |
|
|
INTERNAL_SIZE_T correction; /* bytes for 2nd sbrk call */
|
2095 |
|
|
char* new_brk; /* return of 2nd sbrk call */
|
2096 |
|
|
INTERNAL_SIZE_T top_size; /* new size of top chunk */
|
2097 |
|
|
|
2098 |
|
|
mchunkptr old_top = top; /* Record state of old top */
|
2099 |
|
|
INTERNAL_SIZE_T old_top_size = chunksize(old_top);
|
2100 |
|
|
char* old_end = (char*)(chunk_at_offset(old_top, old_top_size));
|
2101 |
|
|
|
2102 |
|
|
/* Pad request with top_pad plus minimal overhead */
|
2103 |
|
|
|
2104 |
|
|
INTERNAL_SIZE_T sbrk_size = nb + top_pad + MINSIZE;
|
2105 |
|
|
unsigned long pagesz = malloc_getpagesize;
|
2106 |
|
|
|
2107 |
|
|
/* If not the first time through, round to preserve page boundary */
|
2108 |
|
|
/* Otherwise, we need to correct to a page size below anyway. */
|
2109 |
|
|
/* (We also correct below if an intervening foreign sbrk call.) */
|
2110 |
|
|
|
2111 |
|
|
if (sbrk_base != (char*)(-1))
|
2112 |
|
|
sbrk_size = (sbrk_size + (pagesz - 1)) & ~(pagesz - 1);
|
2113 |
|
|
|
2114 |
|
|
brk = (char*)(MORECORE (sbrk_size));
|
2115 |
|
|
|
2116 |
|
|
/* Fail if sbrk failed or if a foreign sbrk call killed our space */
|
2117 |
|
|
if (brk == (char*)(MORECORE_FAILURE) ||
|
2118 |
|
|
(brk < old_end && old_top != initial_top))
|
2119 |
|
|
return;
|
2120 |
|
|
|
2121 |
|
|
sbrked_mem += sbrk_size;
|
2122 |
|
|
|
2123 |
|
|
if (brk == old_end) /* can just add bytes to current top */
|
2124 |
|
|
{
|
2125 |
|
|
top_size = sbrk_size + old_top_size;
|
2126 |
|
|
set_head(top, top_size | PREV_INUSE);
|
2127 |
|
|
}
|
2128 |
|
|
else
|
2129 |
|
|
{
|
2130 |
|
|
if (sbrk_base == (char*)(-1)) /* First time through. Record base */
|
2131 |
|
|
sbrk_base = brk;
|
2132 |
|
|
else /* Someone else called sbrk(). Count those bytes as sbrked_mem. */
|
2133 |
|
|
sbrked_mem += brk - (char*)old_end;
|
2134 |
|
|
|
2135 |
|
|
/* Guarantee alignment of first new chunk made from this space */
|
2136 |
|
|
front_misalign = (POINTER_UINT)chunk2mem(brk) & MALLOC_ALIGN_MASK;
|
2137 |
|
|
if (front_misalign > 0)
|
2138 |
|
|
{
|
2139 |
|
|
correction = (MALLOC_ALIGNMENT) - front_misalign;
|
2140 |
|
|
brk += correction;
|
2141 |
|
|
}
|
2142 |
|
|
else
|
2143 |
|
|
correction = 0;
|
2144 |
|
|
|
2145 |
|
|
/* Guarantee the next brk will be at a page boundary */
|
2146 |
|
|
correction += pagesz - ((POINTER_UINT)(brk + sbrk_size) & (pagesz - 1));
|
2147 |
|
|
|
2148 |
|
|
/* Allocate correction */
|
2149 |
|
|
new_brk = (char*)(MORECORE (correction));
|
2150 |
|
|
if (new_brk == (char*)(MORECORE_FAILURE)) return;
|
2151 |
|
|
|
2152 |
|
|
sbrked_mem += correction;
|
2153 |
|
|
|
2154 |
|
|
top = (mchunkptr)brk;
|
2155 |
|
|
top_size = new_brk - brk + correction;
|
2156 |
|
|
set_head(top, top_size | PREV_INUSE);
|
2157 |
|
|
|
2158 |
|
|
if (old_top != initial_top)
|
2159 |
|
|
{
|
2160 |
|
|
|
2161 |
|
|
/* There must have been an intervening foreign sbrk call. */
|
2162 |
|
|
/* A double fencepost is necessary to prevent consolidation */
|
2163 |
|
|
|
2164 |
|
|
/* If not enough space to do this, then user did something very wrong */
|
2165 |
|
|
if (old_top_size < MINSIZE)
|
2166 |
|
|
{
|
2167 |
|
|
set_head(top, PREV_INUSE); /* will force null return from malloc */
|
2168 |
|
|
return;
|
2169 |
|
|
}
|
2170 |
|
|
|
2171 |
|
|
/* Also keep size a multiple of MALLOC_ALIGNMENT */
|
2172 |
|
|
old_top_size = (old_top_size - 3*SIZE_SZ) & ~MALLOC_ALIGN_MASK;
|
2173 |
|
|
set_head_size(old_top, old_top_size);
|
2174 |
|
|
chunk_at_offset(old_top, old_top_size )->size =
|
2175 |
|
|
SIZE_SZ|PREV_INUSE;
|
2176 |
|
|
chunk_at_offset(old_top, old_top_size + SIZE_SZ)->size =
|
2177 |
|
|
SIZE_SZ|PREV_INUSE;
|
2178 |
|
|
/* If possible, release the rest. */
|
2179 |
|
|
if (old_top_size >= MINSIZE)
|
2180 |
|
|
fREe(RCALL chunk2mem(old_top));
|
2181 |
|
|
}
|
2182 |
|
|
}
|
2183 |
|
|
|
2184 |
|
|
if ((unsigned long)sbrked_mem > (unsigned long)max_sbrked_mem)
|
2185 |
|
|
max_sbrked_mem = sbrked_mem;
|
2186 |
|
|
#if HAVE_MMAP
|
2187 |
|
|
if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem)
|
2188 |
|
|
max_total_mem = mmapped_mem + sbrked_mem;
|
2189 |
|
|
#else
|
2190 |
|
|
if ((unsigned long)(sbrked_mem) > (unsigned long)max_total_mem)
|
2191 |
|
|
max_total_mem = sbrked_mem;
|
2192 |
|
|
#endif
|
2193 |
|
|
|
2194 |
|
|
/* We always land on a page boundary */
|
2195 |
|
|
assert(((unsigned long)((char*)top + top_size) & (pagesz - 1)) == 0);
|
2196 |
|
|
}
|
2197 |
|
|
|
2198 |
|
|
#endif /* DEFINE_MALLOC */
|
2199 |
|
|
|
2200 |
|
|
|
2201 |
|
|
/* Main public routines */
|
2202 |
|
|
|
2203 |
|
|
#ifdef DEFINE_MALLOC
|
2204 |
|
|
|
2205 |
|
|
/*
|
2206 |
|
|
Malloc Algorthim:
|
2207 |
|
|
|
2208 |
|
|
The requested size is first converted into a usable form, `nb'.
|
2209 |
|
|
This currently means to add 4 bytes overhead plus possibly more to
|
2210 |
|
|
obtain 8-byte alignment and/or to obtain a size of at least
|
2211 |
|
|
MINSIZE (currently 16 bytes), the smallest allocatable size.
|
2212 |
|
|
(All fits are considered `exact' if they are within MINSIZE bytes.)
|
2213 |
|
|
|
2214 |
|
|
From there, the first successful of the following steps is taken:
|
2215 |
|
|
|
2216 |
|
|
1. The bin corresponding to the request size is scanned, and if
|
2217 |
|
|
a chunk of exactly the right size is found, it is taken.
|
2218 |
|
|
|
2219 |
|
|
2. The most recently remaindered chunk is used if it is big
|
2220 |
|
|
enough. This is a form of (roving) first fit, used only in
|
2221 |
|
|
the absence of exact fits. Runs of consecutive requests use
|
2222 |
|
|
the remainder of the chunk used for the previous such request
|
2223 |
|
|
whenever possible. This limited use of a first-fit style
|
2224 |
|
|
allocation strategy tends to give contiguous chunks
|
2225 |
|
|
coextensive lifetimes, which improves locality and can reduce
|
2226 |
|
|
fragmentation in the long run.
|
2227 |
|
|
|
2228 |
|
|
3. Other bins are scanned in increasing size order, using a
|
2229 |
|
|
chunk big enough to fulfill the request, and splitting off
|
2230 |
|
|
any remainder. This search is strictly by best-fit; i.e.,
|
2231 |
|
|
the smallest (with ties going to approximately the least
|
2232 |
|
|
recently used) chunk that fits is selected.
|
2233 |
|
|
|
2234 |
|
|
4. If large enough, the chunk bordering the end of memory
|
2235 |
|
|
(`top') is split off. (This use of `top' is in accord with
|
2236 |
|
|
the best-fit search rule. In effect, `top' is treated as
|
2237 |
|
|
larger (and thus less well fitting) than any other available
|
2238 |
|
|
chunk since it can be extended to be as large as necessary
|
2239 |
|
|
(up to system limitations).
|
2240 |
|
|
|
2241 |
|
|
5. If the request size meets the mmap threshold and the
|
2242 |
|
|
system supports mmap, and there are few enough currently
|
2243 |
|
|
allocated mmapped regions, and a call to mmap succeeds,
|
2244 |
|
|
the request is allocated via direct memory mapping.
|
2245 |
|
|
|
2246 |
|
|
6. Otherwise, the top of memory is extended by
|
2247 |
|
|
obtaining more space from the system (normally using sbrk,
|
2248 |
|
|
but definable to anything else via the MORECORE macro).
|
2249 |
|
|
Memory is gathered from the system (in system page-sized
|
2250 |
|
|
units) in a way that allows chunks obtained across different
|
2251 |
|
|
sbrk calls to be consolidated, but does not require
|
2252 |
|
|
contiguous memory. Thus, it should be safe to intersperse
|
2253 |
|
|
mallocs with other sbrk calls.
|
2254 |
|
|
|
2255 |
|
|
|
2256 |
|
|
All allocations are made from the the `lowest' part of any found
|
2257 |
|
|
chunk. (The implementation invariant is that prev_inuse is
|
2258 |
|
|
always true of any allocated chunk; i.e., that each allocated
|
2259 |
|
|
chunk borders either a previously allocated and still in-use chunk,
|
2260 |
|
|
or the base of its memory arena.)
|
2261 |
|
|
|
2262 |
|
|
*/
|
2263 |
|
|
|
2264 |
|
|
#if __STD_C
|
2265 |
|
|
Void_t* mALLOc(RARG size_t bytes)
|
2266 |
|
|
#else
|
2267 |
|
|
Void_t* mALLOc(RARG bytes) RDECL size_t bytes;
|
2268 |
|
|
#endif
|
2269 |
|
|
{
|
2270 |
|
|
mchunkptr victim; /* inspected/selected chunk */
|
2271 |
|
|
INTERNAL_SIZE_T victim_size; /* its size */
|
2272 |
|
|
int idx; /* index for bin traversal */
|
2273 |
|
|
mbinptr bin; /* associated bin */
|
2274 |
|
|
mchunkptr remainder; /* remainder from a split */
|
2275 |
|
|
long remainder_size; /* its size */
|
2276 |
|
|
int remainder_index; /* its bin index */
|
2277 |
|
|
unsigned long block; /* block traverser bit */
|
2278 |
|
|
int startidx; /* first bin of a traversed block */
|
2279 |
|
|
mchunkptr fwd; /* misc temp for linking */
|
2280 |
|
|
mchunkptr bck; /* misc temp for linking */
|
2281 |
|
|
mbinptr q; /* misc temp */
|
2282 |
|
|
|
2283 |
|
|
INTERNAL_SIZE_T nb = request2size(bytes); /* padded request size; */
|
2284 |
|
|
|
2285 |
|
|
MALLOC_LOCK;
|
2286 |
|
|
|
2287 |
|
|
/* Check for exact match in a bin */
|
2288 |
|
|
|
2289 |
|
|
if (is_small_request(nb)) /* Faster version for small requests */
|
2290 |
|
|
{
|
2291 |
|
|
idx = smallbin_index(nb);
|
2292 |
|
|
|
2293 |
|
|
/* No traversal or size check necessary for small bins. */
|
2294 |
|
|
|
2295 |
|
|
q = bin_at(idx);
|
2296 |
|
|
victim = last(q);
|
2297 |
|
|
|
2298 |
|
|
/* Also scan the next one, since it would have a remainder < MINSIZE */
|
2299 |
|
|
if (victim == q)
|
2300 |
|
|
{
|
2301 |
|
|
q = next_bin(q);
|
2302 |
|
|
victim = last(q);
|
2303 |
|
|
}
|
2304 |
|
|
if (victim != q)
|
2305 |
|
|
{
|
2306 |
|
|
victim_size = chunksize(victim);
|
2307 |
|
|
unlink(victim, bck, fwd);
|
2308 |
|
|
set_inuse_bit_at_offset(victim, victim_size);
|
2309 |
|
|
check_malloced_chunk(victim, nb);
|
2310 |
|
|
MALLOC_UNLOCK;
|
2311 |
|
|
return chunk2mem(victim);
|
2312 |
|
|
}
|
2313 |
|
|
|
2314 |
|
|
idx += 2; /* Set for bin scan below. We've already scanned 2 bins. */
|
2315 |
|
|
|
2316 |
|
|
}
|
2317 |
|
|
else
|
2318 |
|
|
{
|
2319 |
|
|
idx = bin_index(nb);
|
2320 |
|
|
bin = bin_at(idx);
|
2321 |
|
|
|
2322 |
|
|
for (victim = last(bin); victim != bin; victim = victim->bk)
|
2323 |
|
|
{
|
2324 |
|
|
victim_size = chunksize(victim);
|
2325 |
|
|
remainder_size = victim_size - nb;
|
2326 |
|
|
|
2327 |
|
|
if (remainder_size >= (long)MINSIZE) /* too big */
|
2328 |
|
|
{
|
2329 |
|
|
--idx; /* adjust to rescan below after checking last remainder */
|
2330 |
|
|
break;
|
2331 |
|
|
}
|
2332 |
|
|
|
2333 |
|
|
else if (remainder_size >= 0) /* exact fit */
|
2334 |
|
|
{
|
2335 |
|
|
unlink(victim, bck, fwd);
|
2336 |
|
|
set_inuse_bit_at_offset(victim, victim_size);
|
2337 |
|
|
check_malloced_chunk(victim, nb);
|
2338 |
|
|
MALLOC_UNLOCK;
|
2339 |
|
|
return chunk2mem(victim);
|
2340 |
|
|
}
|
2341 |
|
|
}
|
2342 |
|
|
|
2343 |
|
|
++idx;
|
2344 |
|
|
|
2345 |
|
|
}
|
2346 |
|
|
|
2347 |
|
|
/* Try to use the last split-off remainder */
|
2348 |
|
|
|
2349 |
|
|
if ( (victim = last_remainder->fd) != last_remainder)
|
2350 |
|
|
{
|
2351 |
|
|
victim_size = chunksize(victim);
|
2352 |
|
|
remainder_size = victim_size - nb;
|
2353 |
|
|
|
2354 |
|
|
if (remainder_size >= (long)MINSIZE) /* re-split */
|
2355 |
|
|
{
|
2356 |
|
|
remainder = chunk_at_offset(victim, nb);
|
2357 |
|
|
set_head(victim, nb | PREV_INUSE);
|
2358 |
|
|
link_last_remainder(remainder);
|
2359 |
|
|
set_head(remainder, remainder_size | PREV_INUSE);
|
2360 |
|
|
set_foot(remainder, remainder_size);
|
2361 |
|
|
check_malloced_chunk(victim, nb);
|
2362 |
|
|
MALLOC_UNLOCK;
|
2363 |
|
|
return chunk2mem(victim);
|
2364 |
|
|
}
|
2365 |
|
|
|
2366 |
|
|
clear_last_remainder;
|
2367 |
|
|
|
2368 |
|
|
if (remainder_size >= 0) /* exhaust */
|
2369 |
|
|
{
|
2370 |
|
|
set_inuse_bit_at_offset(victim, victim_size);
|
2371 |
|
|
check_malloced_chunk(victim, nb);
|
2372 |
|
|
MALLOC_UNLOCK;
|
2373 |
|
|
return chunk2mem(victim);
|
2374 |
|
|
}
|
2375 |
|
|
|
2376 |
|
|
/* Else place in bin */
|
2377 |
|
|
|
2378 |
|
|
frontlink(victim, victim_size, remainder_index, bck, fwd);
|
2379 |
|
|
}
|
2380 |
|
|
|
2381 |
|
|
/*
|
2382 |
|
|
If there are any possibly nonempty big-enough blocks,
|
2383 |
|
|
search for best fitting chunk by scanning bins in blockwidth units.
|
2384 |
|
|
*/
|
2385 |
|
|
|
2386 |
|
|
if ( (block = idx2binblock(idx)) <= binblocks)
|
2387 |
|
|
{
|
2388 |
|
|
|
2389 |
|
|
/* Get to the first marked block */
|
2390 |
|
|
|
2391 |
|
|
if ( (block & binblocks) == 0)
|
2392 |
|
|
{
|
2393 |
|
|
/* force to an even block boundary */
|
2394 |
|
|
idx = (idx & ~(BINBLOCKWIDTH - 1)) + BINBLOCKWIDTH;
|
2395 |
|
|
block <<= 1;
|
2396 |
|
|
while ((block & binblocks) == 0)
|
2397 |
|
|
{
|
2398 |
|
|
idx += BINBLOCKWIDTH;
|
2399 |
|
|
block <<= 1;
|
2400 |
|
|
}
|
2401 |
|
|
}
|
2402 |
|
|
|
2403 |
|
|
/* For each possibly nonempty block ... */
|
2404 |
|
|
for (;;)
|
2405 |
|
|
{
|
2406 |
|
|
startidx = idx; /* (track incomplete blocks) */
|
2407 |
|
|
q = bin = bin_at(idx);
|
2408 |
|
|
|
2409 |
|
|
/* For each bin in this block ... */
|
2410 |
|
|
do
|
2411 |
|
|
{
|
2412 |
|
|
/* Find and use first big enough chunk ... */
|
2413 |
|
|
|
2414 |
|
|
for (victim = last(bin); victim != bin; victim = victim->bk)
|
2415 |
|
|
{
|
2416 |
|
|
victim_size = chunksize(victim);
|
2417 |
|
|
remainder_size = victim_size - nb;
|
2418 |
|
|
|
2419 |
|
|
if (remainder_size >= (long)MINSIZE) /* split */
|
2420 |
|
|
{
|
2421 |
|
|
remainder = chunk_at_offset(victim, nb);
|
2422 |
|
|
set_head(victim, nb | PREV_INUSE);
|
2423 |
|
|
unlink(victim, bck, fwd);
|
2424 |
|
|
link_last_remainder(remainder);
|
2425 |
|
|
set_head(remainder, remainder_size | PREV_INUSE);
|
2426 |
|
|
set_foot(remainder, remainder_size);
|
2427 |
|
|
check_malloced_chunk(victim, nb);
|
2428 |
|
|
MALLOC_UNLOCK;
|
2429 |
|
|
return chunk2mem(victim);
|
2430 |
|
|
}
|
2431 |
|
|
|
2432 |
|
|
else if (remainder_size >= 0) /* take */
|
2433 |
|
|
{
|
2434 |
|
|
set_inuse_bit_at_offset(victim, victim_size);
|
2435 |
|
|
unlink(victim, bck, fwd);
|
2436 |
|
|
check_malloced_chunk(victim, nb);
|
2437 |
|
|
MALLOC_UNLOCK;
|
2438 |
|
|
return chunk2mem(victim);
|
2439 |
|
|
}
|
2440 |
|
|
|
2441 |
|
|
}
|
2442 |
|
|
|
2443 |
|
|
bin = next_bin(bin);
|
2444 |
|
|
|
2445 |
|
|
} while ((++idx & (BINBLOCKWIDTH - 1)) != 0);
|
2446 |
|
|
|
2447 |
|
|
/* Clear out the block bit. */
|
2448 |
|
|
|
2449 |
|
|
do /* Possibly backtrack to try to clear a partial block */
|
2450 |
|
|
{
|
2451 |
|
|
if ((startidx & (BINBLOCKWIDTH - 1)) == 0)
|
2452 |
|
|
{
|
2453 |
|
|
binblocks &= ~block;
|
2454 |
|
|
break;
|
2455 |
|
|
}
|
2456 |
|
|
--startidx;
|
2457 |
|
|
q = prev_bin(q);
|
2458 |
|
|
} while (first(q) == q);
|
2459 |
|
|
|
2460 |
|
|
/* Get to the next possibly nonempty block */
|
2461 |
|
|
|
2462 |
|
|
if ( (block <<= 1) <= binblocks && (block != 0) )
|
2463 |
|
|
{
|
2464 |
|
|
while ((block & binblocks) == 0)
|
2465 |
|
|
{
|
2466 |
|
|
idx += BINBLOCKWIDTH;
|
2467 |
|
|
block <<= 1;
|
2468 |
|
|
}
|
2469 |
|
|
}
|
2470 |
|
|
else
|
2471 |
|
|
break;
|
2472 |
|
|
}
|
2473 |
|
|
}
|
2474 |
|
|
|
2475 |
|
|
|
2476 |
|
|
/* Try to use top chunk */
|
2477 |
|
|
|
2478 |
|
|
/* Require that there be a remainder, ensuring top always exists */
|
2479 |
|
|
remainder_size = chunksize(top) - nb;
|
2480 |
|
|
if (chunksize(top) < nb || remainder_size < (long)MINSIZE)
|
2481 |
|
|
{
|
2482 |
|
|
|
2483 |
|
|
#if HAVE_MMAP
|
2484 |
|
|
/* If big and would otherwise need to extend, try to use mmap instead */
|
2485 |
|
|
if ((unsigned long)nb >= (unsigned long)mmap_threshold &&
|
2486 |
|
|
(victim = mmap_chunk(nb)) != 0)
|
2487 |
|
|
{
|
2488 |
|
|
MALLOC_UNLOCK;
|
2489 |
|
|
return chunk2mem(victim);
|
2490 |
|
|
}
|
2491 |
|
|
#endif
|
2492 |
|
|
|
2493 |
|
|
/* Try to extend */
|
2494 |
|
|
malloc_extend_top(RCALL nb);
|
2495 |
|
|
remainder_size = chunksize(top) - nb;
|
2496 |
|
|
if (chunksize(top) < nb || remainder_size < (long)MINSIZE)
|
2497 |
|
|
{
|
2498 |
|
|
MALLOC_UNLOCK;
|
2499 |
|
|
return 0; /* propagate failure */
|
2500 |
|
|
}
|
2501 |
|
|
}
|
2502 |
|
|
|
2503 |
|
|
victim = top;
|
2504 |
|
|
set_head(victim, nb | PREV_INUSE);
|
2505 |
|
|
top = chunk_at_offset(victim, nb);
|
2506 |
|
|
set_head(top, remainder_size | PREV_INUSE);
|
2507 |
|
|
check_malloced_chunk(victim, nb);
|
2508 |
|
|
MALLOC_UNLOCK;
|
2509 |
|
|
return chunk2mem(victim);
|
2510 |
|
|
|
2511 |
|
|
}
|
2512 |
|
|
|
2513 |
|
|
#endif /* DEFINE_MALLOC */
|
2514 |
|
|
|
2515 |
|
|
#ifdef DEFINE_FREE
|
2516 |
|
|
|
2517 |
|
|
/*
|
2518 |
|
|
|
2519 |
|
|
free() algorithm :
|
2520 |
|
|
|
2521 |
|
|
cases:
|
2522 |
|
|
|
2523 |
|
|
1. free(0) has no effect.
|
2524 |
|
|
|
2525 |
|
|
2. If the chunk was allocated via mmap, it is release via munmap().
|
2526 |
|
|
|
2527 |
|
|
3. If a returned chunk borders the current high end of memory,
|
2528 |
|
|
it is consolidated into the top, and if the total unused
|
2529 |
|
|
topmost memory exceeds the trim threshold, malloc_trim is
|
2530 |
|
|
called.
|
2531 |
|
|
|
2532 |
|
|
4. Other chunks are consolidated as they arrive, and
|
2533 |
|
|
placed in corresponding bins. (This includes the case of
|
2534 |
|
|
consolidating with the current `last_remainder').
|
2535 |
|
|
|
2536 |
|
|
*/
|
2537 |
|
|
|
2538 |
|
|
|
2539 |
|
|
#if __STD_C
|
2540 |
|
|
void fREe(RARG Void_t* mem)
|
2541 |
|
|
#else
|
2542 |
|
|
void fREe(RARG mem) RDECL Void_t* mem;
|
2543 |
|
|
#endif
|
2544 |
|
|
{
|
2545 |
|
|
mchunkptr p; /* chunk corresponding to mem */
|
2546 |
|
|
INTERNAL_SIZE_T hd; /* its head field */
|
2547 |
|
|
INTERNAL_SIZE_T sz; /* its size */
|
2548 |
|
|
int idx; /* its bin index */
|
2549 |
|
|
mchunkptr next; /* next contiguous chunk */
|
2550 |
|
|
INTERNAL_SIZE_T nextsz; /* its size */
|
2551 |
|
|
INTERNAL_SIZE_T prevsz; /* size of previous contiguous chunk */
|
2552 |
|
|
mchunkptr bck; /* misc temp for linking */
|
2553 |
|
|
mchunkptr fwd; /* misc temp for linking */
|
2554 |
|
|
int islr; /* track whether merging with last_remainder */
|
2555 |
|
|
|
2556 |
|
|
if (mem == 0) /* free(0) has no effect */
|
2557 |
|
|
return;
|
2558 |
|
|
|
2559 |
|
|
MALLOC_LOCK;
|
2560 |
|
|
|
2561 |
|
|
p = mem2chunk(mem);
|
2562 |
|
|
hd = p->size;
|
2563 |
|
|
|
2564 |
|
|
#if HAVE_MMAP
|
2565 |
|
|
if (hd & IS_MMAPPED) /* release mmapped memory. */
|
2566 |
|
|
{
|
2567 |
|
|
munmap_chunk(p);
|
2568 |
|
|
MALLOC_UNLOCK;
|
2569 |
|
|
return;
|
2570 |
|
|
}
|
2571 |
|
|
#endif
|
2572 |
|
|
|
2573 |
|
|
check_inuse_chunk(p);
|
2574 |
|
|
|
2575 |
|
|
sz = hd & ~PREV_INUSE;
|
2576 |
|
|
next = chunk_at_offset(p, sz);
|
2577 |
|
|
nextsz = chunksize(next);
|
2578 |
|
|
|
2579 |
|
|
if (next == top) /* merge with top */
|
2580 |
|
|
{
|
2581 |
|
|
sz += nextsz;
|
2582 |
|
|
|
2583 |
|
|
if (!(hd & PREV_INUSE)) /* consolidate backward */
|
2584 |
|
|
{
|
2585 |
|
|
prevsz = p->prev_size;
|
2586 |
|
|
p = chunk_at_offset(p, -prevsz);
|
2587 |
|
|
sz += prevsz;
|
2588 |
|
|
unlink(p, bck, fwd);
|
2589 |
|
|
}
|
2590 |
|
|
|
2591 |
|
|
set_head(p, sz | PREV_INUSE);
|
2592 |
|
|
top = p;
|
2593 |
|
|
if ((unsigned long)(sz) >= (unsigned long)trim_threshold)
|
2594 |
|
|
malloc_trim(RCALL top_pad);
|
2595 |
|
|
MALLOC_UNLOCK;
|
2596 |
|
|
return;
|
2597 |
|
|
}
|
2598 |
|
|
|
2599 |
|
|
set_head(next, nextsz); /* clear inuse bit */
|
2600 |
|
|
|
2601 |
|
|
islr = 0;
|
2602 |
|
|
|
2603 |
|
|
if (!(hd & PREV_INUSE)) /* consolidate backward */
|
2604 |
|
|
{
|
2605 |
|
|
prevsz = p->prev_size;
|
2606 |
|
|
p = chunk_at_offset(p, -prevsz);
|
2607 |
|
|
sz += prevsz;
|
2608 |
|
|
|
2609 |
|
|
if (p->fd == last_remainder) /* keep as last_remainder */
|
2610 |
|
|
islr = 1;
|
2611 |
|
|
else
|
2612 |
|
|
unlink(p, bck, fwd);
|
2613 |
|
|
}
|
2614 |
|
|
|
2615 |
|
|
if (!(inuse_bit_at_offset(next, nextsz))) /* consolidate forward */
|
2616 |
|
|
{
|
2617 |
|
|
sz += nextsz;
|
2618 |
|
|
|
2619 |
|
|
if (!islr && next->fd == last_remainder) /* re-insert last_remainder */
|
2620 |
|
|
{
|
2621 |
|
|
islr = 1;
|
2622 |
|
|
link_last_remainder(p);
|
2623 |
|
|
}
|
2624 |
|
|
else
|
2625 |
|
|
unlink(next, bck, fwd);
|
2626 |
|
|
}
|
2627 |
|
|
|
2628 |
|
|
|
2629 |
|
|
set_head(p, sz | PREV_INUSE);
|
2630 |
|
|
set_foot(p, sz);
|
2631 |
|
|
if (!islr)
|
2632 |
|
|
frontlink(p, sz, idx, bck, fwd);
|
2633 |
|
|
|
2634 |
|
|
MALLOC_UNLOCK;
|
2635 |
|
|
}
|
2636 |
|
|
|
2637 |
|
|
#endif /* DEFINE_FREE */
|
2638 |
|
|
|
2639 |
|
|
#ifdef DEFINE_REALLOC
|
2640 |
|
|
|
2641 |
|
|
/*
|
2642 |
|
|
|
2643 |
|
|
Realloc algorithm:
|
2644 |
|
|
|
2645 |
|
|
Chunks that were obtained via mmap cannot be extended or shrunk
|
2646 |
|
|
unless HAVE_MREMAP is defined, in which case mremap is used.
|
2647 |
|
|
Otherwise, if their reallocation is for additional space, they are
|
2648 |
|
|
copied. If for less, they are just left alone.
|
2649 |
|
|
|
2650 |
|
|
Otherwise, if the reallocation is for additional space, and the
|
2651 |
|
|
chunk can be extended, it is, else a malloc-copy-free sequence is
|
2652 |
|
|
taken. There are several different ways that a chunk could be
|
2653 |
|
|
extended. All are tried:
|
2654 |
|
|
|
2655 |
|
|
* Extending forward into following adjacent free chunk.
|
2656 |
|
|
* Shifting backwards, joining preceding adjacent space
|
2657 |
|
|
* Both shifting backwards and extending forward.
|
2658 |
|
|
* Extending into newly sbrked space
|
2659 |
|
|
|
2660 |
|
|
Unless the #define REALLOC_ZERO_BYTES_FREES is set, realloc with a
|
2661 |
|
|
size argument of zero (re)allocates a minimum-sized chunk.
|
2662 |
|
|
|
2663 |
|
|
If the reallocation is for less space, and the new request is for
|
2664 |
|
|
a `small' (<512 bytes) size, then the newly unused space is lopped
|
2665 |
|
|
off and freed.
|
2666 |
|
|
|
2667 |
|
|
The old unix realloc convention of allowing the last-free'd chunk
|
2668 |
|
|
to be used as an argument to realloc is no longer supported.
|
2669 |
|
|
I don't know of any programs still relying on this feature,
|
2670 |
|
|
and allowing it would also allow too many other incorrect
|
2671 |
|
|
usages of realloc to be sensible.
|
2672 |
|
|
|
2673 |
|
|
|
2674 |
|
|
*/
|
2675 |
|
|
|
2676 |
|
|
|
2677 |
|
|
#if __STD_C
|
2678 |
|
|
Void_t* rEALLOc(RARG Void_t* oldmem, size_t bytes)
|
2679 |
|
|
#else
|
2680 |
|
|
Void_t* rEALLOc(RARG oldmem, bytes) RDECL Void_t* oldmem; size_t bytes;
|
2681 |
|
|
#endif
|
2682 |
|
|
{
|
2683 |
|
|
INTERNAL_SIZE_T nb; /* padded request size */
|
2684 |
|
|
|
2685 |
|
|
mchunkptr oldp; /* chunk corresponding to oldmem */
|
2686 |
|
|
INTERNAL_SIZE_T oldsize; /* its size */
|
2687 |
|
|
|
2688 |
|
|
mchunkptr newp; /* chunk to return */
|
2689 |
|
|
INTERNAL_SIZE_T newsize; /* its size */
|
2690 |
|
|
Void_t* newmem; /* corresponding user mem */
|
2691 |
|
|
|
2692 |
|
|
mchunkptr next; /* next contiguous chunk after oldp */
|
2693 |
|
|
INTERNAL_SIZE_T nextsize; /* its size */
|
2694 |
|
|
|
2695 |
|
|
mchunkptr prev; /* previous contiguous chunk before oldp */
|
2696 |
|
|
INTERNAL_SIZE_T prevsize; /* its size */
|
2697 |
|
|
|
2698 |
|
|
mchunkptr remainder; /* holds split off extra space from newp */
|
2699 |
|
|
INTERNAL_SIZE_T remainder_size; /* its size */
|
2700 |
|
|
|
2701 |
|
|
mchunkptr bck; /* misc temp for linking */
|
2702 |
|
|
mchunkptr fwd; /* misc temp for linking */
|
2703 |
|
|
|
2704 |
|
|
#ifdef REALLOC_ZERO_BYTES_FREES
|
2705 |
|
|
if (bytes == 0) { fREe(RCALL oldmem); return 0; }
|
2706 |
|
|
#endif
|
2707 |
|
|
|
2708 |
|
|
|
2709 |
|
|
/* realloc of null is supposed to be same as malloc */
|
2710 |
|
|
if (oldmem == 0) return mALLOc(RCALL bytes);
|
2711 |
|
|
|
2712 |
|
|
MALLOC_LOCK;
|
2713 |
|
|
|
2714 |
|
|
newp = oldp = mem2chunk(oldmem);
|
2715 |
|
|
newsize = oldsize = chunksize(oldp);
|
2716 |
|
|
|
2717 |
|
|
|
2718 |
|
|
nb = request2size(bytes);
|
2719 |
|
|
|
2720 |
|
|
#if HAVE_MMAP
|
2721 |
|
|
if (chunk_is_mmapped(oldp))
|
2722 |
|
|
{
|
2723 |
|
|
#if HAVE_MREMAP
|
2724 |
|
|
newp = mremap_chunk(oldp, nb);
|
2725 |
|
|
if(newp)
|
2726 |
|
|
{
|
2727 |
|
|
MALLOC_UNLOCK;
|
2728 |
|
|
return chunk2mem(newp);
|
2729 |
|
|
}
|
2730 |
|
|
#endif
|
2731 |
|
|
/* Note the extra SIZE_SZ overhead. */
|
2732 |
|
|
if(oldsize - SIZE_SZ >= nb)
|
2733 |
|
|
{
|
2734 |
|
|
MALLOC_UNLOCK;
|
2735 |
|
|
return oldmem; /* do nothing */
|
2736 |
|
|
}
|
2737 |
|
|
/* Must alloc, copy, free. */
|
2738 |
|
|
newmem = mALLOc(RCALL bytes);
|
2739 |
|
|
if (newmem == 0)
|
2740 |
|
|
{
|
2741 |
|
|
MALLOC_UNLOCK;
|
2742 |
|
|
return 0; /* propagate failure */
|
2743 |
|
|
}
|
2744 |
|
|
MALLOC_COPY(newmem, oldmem, oldsize - 2*SIZE_SZ);
|
2745 |
|
|
munmap_chunk(oldp);
|
2746 |
|
|
MALLOC_UNLOCK;
|
2747 |
|
|
return newmem;
|
2748 |
|
|
}
|
2749 |
|
|
#endif
|
2750 |
|
|
|
2751 |
|
|
check_inuse_chunk(oldp);
|
2752 |
|
|
|
2753 |
|
|
if ((long)(oldsize) < (long)(nb))
|
2754 |
|
|
{
|
2755 |
|
|
|
2756 |
|
|
/* Try expanding forward */
|
2757 |
|
|
|
2758 |
|
|
next = chunk_at_offset(oldp, oldsize);
|
2759 |
|
|
if (next == top || !inuse(next))
|
2760 |
|
|
{
|
2761 |
|
|
nextsize = chunksize(next);
|
2762 |
|
|
|
2763 |
|
|
/* Forward into top only if a remainder */
|
2764 |
|
|
if (next == top)
|
2765 |
|
|
{
|
2766 |
|
|
if ((long)(nextsize + newsize) >= (long)(nb + MINSIZE))
|
2767 |
|
|
{
|
2768 |
|
|
newsize += nextsize;
|
2769 |
|
|
top = chunk_at_offset(oldp, nb);
|
2770 |
|
|
set_head(top, (newsize - nb) | PREV_INUSE);
|
2771 |
|
|
set_head_size(oldp, nb);
|
2772 |
|
|
MALLOC_UNLOCK;
|
2773 |
|
|
return chunk2mem(oldp);
|
2774 |
|
|
}
|
2775 |
|
|
}
|
2776 |
|
|
|
2777 |
|
|
/* Forward into next chunk */
|
2778 |
|
|
else if (((long)(nextsize + newsize) >= (long)(nb)))
|
2779 |
|
|
{
|
2780 |
|
|
unlink(next, bck, fwd);
|
2781 |
|
|
newsize += nextsize;
|
2782 |
|
|
goto split;
|
2783 |
|
|
}
|
2784 |
|
|
}
|
2785 |
|
|
else
|
2786 |
|
|
{
|
2787 |
|
|
next = 0;
|
2788 |
|
|
nextsize = 0;
|
2789 |
|
|
}
|
2790 |
|
|
|
2791 |
|
|
/* Try shifting backwards. */
|
2792 |
|
|
|
2793 |
|
|
if (!prev_inuse(oldp))
|
2794 |
|
|
{
|
2795 |
|
|
prev = prev_chunk(oldp);
|
2796 |
|
|
prevsize = chunksize(prev);
|
2797 |
|
|
|
2798 |
|
|
/* try forward + backward first to save a later consolidation */
|
2799 |
|
|
|
2800 |
|
|
if (next != 0)
|
2801 |
|
|
{
|
2802 |
|
|
/* into top */
|
2803 |
|
|
if (next == top)
|
2804 |
|
|
{
|
2805 |
|
|
if ((long)(nextsize + prevsize + newsize) >= (long)(nb + MINSIZE))
|
2806 |
|
|
{
|
2807 |
|
|
unlink(prev, bck, fwd);
|
2808 |
|
|
newp = prev;
|
2809 |
|
|
newsize += prevsize + nextsize;
|
2810 |
|
|
newmem = chunk2mem(newp);
|
2811 |
|
|
MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
|
2812 |
|
|
top = chunk_at_offset(newp, nb);
|
2813 |
|
|
set_head(top, (newsize - nb) | PREV_INUSE);
|
2814 |
|
|
set_head_size(newp, nb);
|
2815 |
|
|
MALLOC_UNLOCK;
|
2816 |
|
|
return newmem;
|
2817 |
|
|
}
|
2818 |
|
|
}
|
2819 |
|
|
|
2820 |
|
|
/* into next chunk */
|
2821 |
|
|
else if (((long)(nextsize + prevsize + newsize) >= (long)(nb)))
|
2822 |
|
|
{
|
2823 |
|
|
unlink(next, bck, fwd);
|
2824 |
|
|
unlink(prev, bck, fwd);
|
2825 |
|
|
newp = prev;
|
2826 |
|
|
newsize += nextsize + prevsize;
|
2827 |
|
|
newmem = chunk2mem(newp);
|
2828 |
|
|
MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
|
2829 |
|
|
goto split;
|
2830 |
|
|
}
|
2831 |
|
|
}
|
2832 |
|
|
|
2833 |
|
|
/* backward only */
|
2834 |
|
|
if (prev != 0 && (long)(prevsize + newsize) >= (long)nb)
|
2835 |
|
|
{
|
2836 |
|
|
unlink(prev, bck, fwd);
|
2837 |
|
|
newp = prev;
|
2838 |
|
|
newsize += prevsize;
|
2839 |
|
|
newmem = chunk2mem(newp);
|
2840 |
|
|
MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
|
2841 |
|
|
goto split;
|
2842 |
|
|
}
|
2843 |
|
|
}
|
2844 |
|
|
|
2845 |
|
|
/* Must allocate */
|
2846 |
|
|
|
2847 |
|
|
newmem = mALLOc (RCALL bytes);
|
2848 |
|
|
|
2849 |
|
|
if (newmem == 0) /* propagate failure */
|
2850 |
|
|
{
|
2851 |
|
|
MALLOC_UNLOCK;
|
2852 |
|
|
return 0;
|
2853 |
|
|
}
|
2854 |
|
|
|
2855 |
|
|
/* Avoid copy if newp is next chunk after oldp. */
|
2856 |
|
|
/* (This can only happen when new chunk is sbrk'ed.) */
|
2857 |
|
|
|
2858 |
|
|
if ( (newp = mem2chunk(newmem)) == next_chunk(oldp))
|
2859 |
|
|
{
|
2860 |
|
|
newsize += chunksize(newp);
|
2861 |
|
|
newp = oldp;
|
2862 |
|
|
goto split;
|
2863 |
|
|
}
|
2864 |
|
|
|
2865 |
|
|
/* Otherwise copy, free, and exit */
|
2866 |
|
|
MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
|
2867 |
|
|
fREe(RCALL oldmem);
|
2868 |
|
|
MALLOC_UNLOCK;
|
2869 |
|
|
return newmem;
|
2870 |
|
|
}
|
2871 |
|
|
|
2872 |
|
|
|
2873 |
|
|
split: /* split off extra room in old or expanded chunk */
|
2874 |
|
|
|
2875 |
|
|
if (newsize - nb >= MINSIZE) /* split off remainder */
|
2876 |
|
|
{
|
2877 |
|
|
remainder = chunk_at_offset(newp, nb);
|
2878 |
|
|
remainder_size = newsize - nb;
|
2879 |
|
|
set_head_size(newp, nb);
|
2880 |
|
|
set_head(remainder, remainder_size | PREV_INUSE);
|
2881 |
|
|
set_inuse_bit_at_offset(remainder, remainder_size);
|
2882 |
|
|
fREe(RCALL chunk2mem(remainder)); /* let free() deal with it */
|
2883 |
|
|
}
|
2884 |
|
|
else
|
2885 |
|
|
{
|
2886 |
|
|
set_head_size(newp, newsize);
|
2887 |
|
|
set_inuse_bit_at_offset(newp, newsize);
|
2888 |
|
|
}
|
2889 |
|
|
|
2890 |
|
|
check_inuse_chunk(newp);
|
2891 |
|
|
MALLOC_UNLOCK;
|
2892 |
|
|
return chunk2mem(newp);
|
2893 |
|
|
}
|
2894 |
|
|
|
2895 |
|
|
#endif /* DEFINE_REALLOC */
|
2896 |
|
|
|
2897 |
|
|
#ifdef DEFINE_MEMALIGN
|
2898 |
|
|
|
2899 |
|
|
/*
|
2900 |
|
|
|
2901 |
|
|
memalign algorithm:
|
2902 |
|
|
|
2903 |
|
|
memalign requests more than enough space from malloc, finds a spot
|
2904 |
|
|
within that chunk that meets the alignment request, and then
|
2905 |
|
|
possibly frees the leading and trailing space.
|
2906 |
|
|
|
2907 |
|
|
The alignment argument must be a power of two. This property is not
|
2908 |
|
|
checked by memalign, so misuse may result in random runtime errors.
|
2909 |
|
|
|
2910 |
|
|
8-byte alignment is guaranteed by normal malloc calls, so don't
|
2911 |
|
|
bother calling memalign with an argument of 8 or less.
|
2912 |
|
|
|
2913 |
|
|
Overreliance on memalign is a sure way to fragment space.
|
2914 |
|
|
|
2915 |
|
|
*/
|
2916 |
|
|
|
2917 |
|
|
|
2918 |
|
|
#if __STD_C
|
2919 |
|
|
Void_t* mEMALIGn(RARG size_t alignment, size_t bytes)
|
2920 |
|
|
#else
|
2921 |
|
|
Void_t* mEMALIGn(RARG alignment, bytes) RDECL size_t alignment; size_t bytes;
|
2922 |
|
|
#endif
|
2923 |
|
|
{
|
2924 |
|
|
INTERNAL_SIZE_T nb; /* padded request size */
|
2925 |
|
|
char* m; /* memory returned by malloc call */
|
2926 |
|
|
mchunkptr p; /* corresponding chunk */
|
2927 |
|
|
char* brk; /* alignment point within p */
|
2928 |
|
|
mchunkptr newp; /* chunk to return */
|
2929 |
|
|
INTERNAL_SIZE_T newsize; /* its size */
|
2930 |
|
|
INTERNAL_SIZE_T leadsize; /* leading space befor alignment point */
|
2931 |
|
|
mchunkptr remainder; /* spare room at end to split off */
|
2932 |
|
|
long remainder_size; /* its size */
|
2933 |
|
|
|
2934 |
|
|
/* If need less alignment than we give anyway, just relay to malloc */
|
2935 |
|
|
|
2936 |
|
|
if (alignment <= MALLOC_ALIGNMENT) return mALLOc(RCALL bytes);
|
2937 |
|
|
|
2938 |
|
|
/* Otherwise, ensure that it is at least a minimum chunk size */
|
2939 |
|
|
|
2940 |
|
|
if (alignment < MINSIZE) alignment = MINSIZE;
|
2941 |
|
|
|
2942 |
|
|
/* Call malloc with worst case padding to hit alignment. */
|
2943 |
|
|
|
2944 |
|
|
nb = request2size(bytes);
|
2945 |
|
|
m = (char*)(mALLOc(RCALL nb + alignment + MINSIZE));
|
2946 |
|
|
|
2947 |
|
|
if (m == 0) return 0; /* propagate failure */
|
2948 |
|
|
|
2949 |
|
|
MALLOC_LOCK;
|
2950 |
|
|
|
2951 |
|
|
p = mem2chunk(m);
|
2952 |
|
|
|
2953 |
|
|
if ((((unsigned long)(m)) % alignment) == 0) /* aligned */
|
2954 |
|
|
{
|
2955 |
|
|
#if HAVE_MMAP
|
2956 |
|
|
if(chunk_is_mmapped(p))
|
2957 |
|
|
{
|
2958 |
|
|
MALLOC_UNLOCK;
|
2959 |
|
|
return chunk2mem(p); /* nothing more to do */
|
2960 |
|
|
}
|
2961 |
|
|
#endif
|
2962 |
|
|
}
|
2963 |
|
|
else /* misaligned */
|
2964 |
|
|
{
|
2965 |
|
|
/*
|
2966 |
|
|
Find an aligned spot inside chunk.
|
2967 |
|
|
Since we need to give back leading space in a chunk of at
|
2968 |
|
|
least MINSIZE, if the first calculation places us at
|
2969 |
|
|
a spot with less than MINSIZE leader, we can move to the
|
2970 |
|
|
next aligned spot -- we've allocated enough total room so that
|
2971 |
|
|
this is always possible.
|
2972 |
|
|
*/
|
2973 |
|
|
|
2974 |
|
|
brk = (char*)mem2chunk(((unsigned long)(m + alignment - 1)) & -alignment);
|
2975 |
|
|
if ((long)(brk - (char*)(p)) < (long)MINSIZE) brk = brk + alignment;
|
2976 |
|
|
|
2977 |
|
|
newp = (mchunkptr)brk;
|
2978 |
|
|
leadsize = brk - (char*)(p);
|
2979 |
|
|
newsize = chunksize(p) - leadsize;
|
2980 |
|
|
|
2981 |
|
|
#if HAVE_MMAP
|
2982 |
|
|
if(chunk_is_mmapped(p))
|
2983 |
|
|
{
|
2984 |
|
|
newp->prev_size = p->prev_size + leadsize;
|
2985 |
|
|
set_head(newp, newsize|IS_MMAPPED);
|
2986 |
|
|
MALLOC_UNLOCK;
|
2987 |
|
|
return chunk2mem(newp);
|
2988 |
|
|
}
|
2989 |
|
|
#endif
|
2990 |
|
|
|
2991 |
|
|
/* give back leader, use the rest */
|
2992 |
|
|
|
2993 |
|
|
set_head(newp, newsize | PREV_INUSE);
|
2994 |
|
|
set_inuse_bit_at_offset(newp, newsize);
|
2995 |
|
|
set_head_size(p, leadsize);
|
2996 |
|
|
fREe(RCALL chunk2mem(p));
|
2997 |
|
|
p = newp;
|
2998 |
|
|
|
2999 |
|
|
assert (newsize >= nb && (((unsigned long)(chunk2mem(p))) % alignment) == 0);
|
3000 |
|
|
}
|
3001 |
|
|
|
3002 |
|
|
/* Also give back spare room at the end */
|
3003 |
|
|
|
3004 |
|
|
remainder_size = chunksize(p) - nb;
|
3005 |
|
|
|
3006 |
|
|
if (remainder_size >= (long)MINSIZE)
|
3007 |
|
|
{
|
3008 |
|
|
remainder = chunk_at_offset(p, nb);
|
3009 |
|
|
set_head(remainder, remainder_size | PREV_INUSE);
|
3010 |
|
|
set_head_size(p, nb);
|
3011 |
|
|
fREe(RCALL chunk2mem(remainder));
|
3012 |
|
|
}
|
3013 |
|
|
|
3014 |
|
|
check_inuse_chunk(p);
|
3015 |
|
|
MALLOC_UNLOCK;
|
3016 |
|
|
return chunk2mem(p);
|
3017 |
|
|
|
3018 |
|
|
}
|
3019 |
|
|
|
3020 |
|
|
#endif /* DEFINE_MEMALIGN */
|
3021 |
|
|
|
3022 |
|
|
#ifdef DEFINE_VALLOC
|
3023 |
|
|
|
3024 |
|
|
/*
|
3025 |
|
|
valloc just invokes memalign with alignment argument equal
|
3026 |
|
|
to the page size of the system (or as near to this as can
|
3027 |
|
|
be figured out from all the includes/defines above.)
|
3028 |
|
|
*/
|
3029 |
|
|
|
3030 |
|
|
#if __STD_C
|
3031 |
|
|
Void_t* vALLOc(RARG size_t bytes)
|
3032 |
|
|
#else
|
3033 |
|
|
Void_t* vALLOc(RARG bytes) RDECL size_t bytes;
|
3034 |
|
|
#endif
|
3035 |
|
|
{
|
3036 |
|
|
return mEMALIGn (RCALL malloc_getpagesize, bytes);
|
3037 |
|
|
}
|
3038 |
|
|
|
3039 |
|
|
#endif /* DEFINE_VALLOC */
|
3040 |
|
|
|
3041 |
|
|
#ifdef DEFINE_PVALLOC
|
3042 |
|
|
|
3043 |
|
|
/*
|
3044 |
|
|
pvalloc just invokes valloc for the nearest pagesize
|
3045 |
|
|
that will accommodate request
|
3046 |
|
|
*/
|
3047 |
|
|
|
3048 |
|
|
|
3049 |
|
|
#if __STD_C
|
3050 |
|
|
Void_t* pvALLOc(RARG size_t bytes)
|
3051 |
|
|
#else
|
3052 |
|
|
Void_t* pvALLOc(RARG bytes) RDECL size_t bytes;
|
3053 |
|
|
#endif
|
3054 |
|
|
{
|
3055 |
|
|
size_t pagesize = malloc_getpagesize;
|
3056 |
|
|
return mEMALIGn (RCALL pagesize, (bytes + pagesize - 1) & ~(pagesize - 1));
|
3057 |
|
|
}
|
3058 |
|
|
|
3059 |
|
|
#endif /* DEFINE_PVALLOC */
|
3060 |
|
|
|
3061 |
|
|
#ifdef DEFINE_CALLOC
|
3062 |
|
|
|
3063 |
|
|
/*
|
3064 |
|
|
|
3065 |
|
|
calloc calls malloc, then zeroes out the allocated chunk.
|
3066 |
|
|
|
3067 |
|
|
*/
|
3068 |
|
|
|
3069 |
|
|
#if __STD_C
|
3070 |
|
|
Void_t* cALLOc(RARG size_t n, size_t elem_size)
|
3071 |
|
|
#else
|
3072 |
|
|
Void_t* cALLOc(RARG n, elem_size) RDECL size_t n; size_t elem_size;
|
3073 |
|
|
#endif
|
3074 |
|
|
{
|
3075 |
|
|
mchunkptr p;
|
3076 |
|
|
INTERNAL_SIZE_T csz;
|
3077 |
|
|
|
3078 |
|
|
INTERNAL_SIZE_T sz = n * elem_size;
|
3079 |
|
|
|
3080 |
|
|
#if MORECORE_CLEARS
|
3081 |
|
|
mchunkptr oldtop;
|
3082 |
|
|
INTERNAL_SIZE_T oldtopsize;
|
3083 |
|
|
#endif
|
3084 |
|
|
Void_t* mem;
|
3085 |
|
|
|
3086 |
|
|
/* check if expand_top called, in which case don't need to clear */
|
3087 |
|
|
#if MORECORE_CLEARS
|
3088 |
|
|
MALLOC_LOCK;
|
3089 |
|
|
oldtop = top;
|
3090 |
|
|
oldtopsize = chunksize(top);
|
3091 |
|
|
#endif
|
3092 |
|
|
|
3093 |
|
|
mem = mALLOc (RCALL sz);
|
3094 |
|
|
|
3095 |
|
|
if (mem == 0)
|
3096 |
|
|
{
|
3097 |
|
|
#if MORECORE_CLEARS
|
3098 |
|
|
MALLOC_UNLOCK;
|
3099 |
|
|
#endif
|
3100 |
|
|
return 0;
|
3101 |
|
|
}
|
3102 |
|
|
else
|
3103 |
|
|
{
|
3104 |
|
|
p = mem2chunk(mem);
|
3105 |
|
|
|
3106 |
|
|
/* Two optional cases in which clearing not necessary */
|
3107 |
|
|
|
3108 |
|
|
|
3109 |
|
|
#if HAVE_MMAP
|
3110 |
|
|
if (chunk_is_mmapped(p))
|
3111 |
|
|
{
|
3112 |
|
|
#if MORECORE_CLEARS
|
3113 |
|
|
MALLOC_UNLOCK;
|
3114 |
|
|
#endif
|
3115 |
|
|
return mem;
|
3116 |
|
|
}
|
3117 |
|
|
#endif
|
3118 |
|
|
|
3119 |
|
|
csz = chunksize(p);
|
3120 |
|
|
|
3121 |
|
|
#if MORECORE_CLEARS
|
3122 |
|
|
if (p == oldtop && csz > oldtopsize)
|
3123 |
|
|
{
|
3124 |
|
|
/* clear only the bytes from non-freshly-sbrked memory */
|
3125 |
|
|
csz = oldtopsize;
|
3126 |
|
|
}
|
3127 |
|
|
MALLOC_UNLOCK;
|
3128 |
|
|
#endif
|
3129 |
|
|
|
3130 |
|
|
MALLOC_ZERO(mem, csz - SIZE_SZ);
|
3131 |
|
|
return mem;
|
3132 |
|
|
}
|
3133 |
|
|
}
|
3134 |
|
|
|
3135 |
|
|
#endif /* DEFINE_CALLOC */
|
3136 |
|
|
|
3137 |
|
|
#ifdef DEFINE_CFREE
|
3138 |
|
|
|
3139 |
|
|
/*
|
3140 |
|
|
|
3141 |
|
|
cfree just calls free. It is needed/defined on some systems
|
3142 |
|
|
that pair it with calloc, presumably for odd historical reasons.
|
3143 |
|
|
|
3144 |
|
|
*/
|
3145 |
|
|
|
3146 |
|
|
#if !defined(INTERNAL_LINUX_C_LIB) || !defined(__ELF__)
|
3147 |
|
|
#if !defined(INTERNAL_NEWLIB) || !defined(_REENT_ONLY)
|
3148 |
|
|
#if __STD_C
|
3149 |
|
|
void cfree(Void_t *mem)
|
3150 |
|
|
#else
|
3151 |
|
|
void cfree(mem) Void_t *mem;
|
3152 |
|
|
#endif
|
3153 |
|
|
{
|
3154 |
|
|
#ifdef INTERNAL_NEWLIB
|
3155 |
|
|
fREe(_REENT, mem);
|
3156 |
|
|
#else
|
3157 |
|
|
fREe(mem);
|
3158 |
|
|
#endif
|
3159 |
|
|
}
|
3160 |
|
|
#endif
|
3161 |
|
|
#endif
|
3162 |
|
|
|
3163 |
|
|
#endif /* DEFINE_CFREE */
|
3164 |
|
|
|
3165 |
|
|
#ifdef DEFINE_FREE
|
3166 |
|
|
|
3167 |
|
|
/*
|
3168 |
|
|
|
3169 |
|
|
Malloc_trim gives memory back to the system (via negative
|
3170 |
|
|
arguments to sbrk) if there is unused memory at the `high' end of
|
3171 |
|
|
the malloc pool. You can call this after freeing large blocks of
|
3172 |
|
|
memory to potentially reduce the system-level memory requirements
|
3173 |
|
|
of a program. However, it cannot guarantee to reduce memory. Under
|
3174 |
|
|
some allocation patterns, some large free blocks of memory will be
|
3175 |
|
|
locked between two used chunks, so they cannot be given back to
|
3176 |
|
|
the system.
|
3177 |
|
|
|
3178 |
|
|
The `pad' argument to malloc_trim represents the amount of free
|
3179 |
|
|
trailing space to leave untrimmed. If this argument is zero,
|
3180 |
|
|
only the minimum amount of memory to maintain internal data
|
3181 |
|
|
structures will be left (one page or less). Non-zero arguments
|
3182 |
|
|
can be supplied to maintain enough trailing space to service
|
3183 |
|
|
future expected allocations without having to re-obtain memory
|
3184 |
|
|
from the system.
|
3185 |
|
|
|
3186 |
|
|
Malloc_trim returns 1 if it actually released any memory, else 0.
|
3187 |
|
|
|
3188 |
|
|
*/
|
3189 |
|
|
|
3190 |
|
|
#if __STD_C
|
3191 |
|
|
int malloc_trim(RARG size_t pad)
|
3192 |
|
|
#else
|
3193 |
|
|
int malloc_trim(RARG pad) RDECL size_t pad;
|
3194 |
|
|
#endif
|
3195 |
|
|
{
|
3196 |
|
|
long top_size; /* Amount of top-most memory */
|
3197 |
|
|
long extra; /* Amount to release */
|
3198 |
|
|
char* current_brk; /* address returned by pre-check sbrk call */
|
3199 |
|
|
char* new_brk; /* address returned by negative sbrk call */
|
3200 |
|
|
|
3201 |
|
|
unsigned long pagesz = malloc_getpagesize;
|
3202 |
|
|
|
3203 |
|
|
MALLOC_LOCK;
|
3204 |
|
|
|
3205 |
|
|
top_size = chunksize(top);
|
3206 |
|
|
extra = ((top_size - pad - MINSIZE + (pagesz-1)) / pagesz - 1) * pagesz;
|
3207 |
|
|
|
3208 |
|
|
if (extra < (long)pagesz) /* Not enough memory to release */
|
3209 |
|
|
{
|
3210 |
|
|
MALLOC_UNLOCK;
|
3211 |
|
|
return 0;
|
3212 |
|
|
}
|
3213 |
|
|
|
3214 |
|
|
else
|
3215 |
|
|
{
|
3216 |
|
|
/* Test to make sure no one else called sbrk */
|
3217 |
|
|
current_brk = (char*)(MORECORE (0));
|
3218 |
|
|
if (current_brk != (char*)(top) + top_size)
|
3219 |
|
|
{
|
3220 |
|
|
MALLOC_UNLOCK;
|
3221 |
|
|
return 0; /* Apparently we don't own memory; must fail */
|
3222 |
|
|
}
|
3223 |
|
|
|
3224 |
|
|
else
|
3225 |
|
|
{
|
3226 |
|
|
new_brk = (char*)(MORECORE (-extra));
|
3227 |
|
|
|
3228 |
|
|
if (new_brk == (char*)(MORECORE_FAILURE)) /* sbrk failed? */
|
3229 |
|
|
{
|
3230 |
|
|
/* Try to figure out what we have */
|
3231 |
|
|
current_brk = (char*)(MORECORE (0));
|
3232 |
|
|
top_size = current_brk - (char*)top;
|
3233 |
|
|
if (top_size >= (long)MINSIZE) /* if not, we are very very dead! */
|
3234 |
|
|
{
|
3235 |
|
|
sbrked_mem = current_brk - sbrk_base;
|
3236 |
|
|
set_head(top, top_size | PREV_INUSE);
|
3237 |
|
|
}
|
3238 |
|
|
check_chunk(top);
|
3239 |
|
|
MALLOC_UNLOCK;
|
3240 |
|
|
return 0;
|
3241 |
|
|
}
|
3242 |
|
|
|
3243 |
|
|
else
|
3244 |
|
|
{
|
3245 |
|
|
/* Success. Adjust top accordingly. */
|
3246 |
|
|
set_head(top, (top_size - extra) | PREV_INUSE);
|
3247 |
|
|
sbrked_mem -= extra;
|
3248 |
|
|
check_chunk(top);
|
3249 |
|
|
MALLOC_UNLOCK;
|
3250 |
|
|
return 1;
|
3251 |
|
|
}
|
3252 |
|
|
}
|
3253 |
|
|
}
|
3254 |
|
|
}
|
3255 |
|
|
|
3256 |
|
|
#endif /* DEFINE_FREE */
|
3257 |
|
|
|
3258 |
|
|
#ifdef DEFINE_MALLOC_USABLE_SIZE
|
3259 |
|
|
|
3260 |
|
|
/*
|
3261 |
|
|
malloc_usable_size:
|
3262 |
|
|
|
3263 |
|
|
This routine tells you how many bytes you can actually use in an
|
3264 |
|
|
allocated chunk, which may be more than you requested (although
|
3265 |
|
|
often not). You can use this many bytes without worrying about
|
3266 |
|
|
overwriting other allocated objects. Not a particularly great
|
3267 |
|
|
programming practice, but still sometimes useful.
|
3268 |
|
|
|
3269 |
|
|
*/
|
3270 |
|
|
|
3271 |
|
|
#if __STD_C
|
3272 |
|
|
size_t malloc_usable_size(RARG Void_t* mem)
|
3273 |
|
|
#else
|
3274 |
|
|
size_t malloc_usable_size(RARG mem) RDECL Void_t* mem;
|
3275 |
|
|
#endif
|
3276 |
|
|
{
|
3277 |
|
|
mchunkptr p;
|
3278 |
|
|
if (mem == 0)
|
3279 |
|
|
return 0;
|
3280 |
|
|
else
|
3281 |
|
|
{
|
3282 |
|
|
p = mem2chunk(mem);
|
3283 |
|
|
if(!chunk_is_mmapped(p))
|
3284 |
|
|
{
|
3285 |
|
|
if (!inuse(p)) return 0;
|
3286 |
|
|
#if DEBUG
|
3287 |
|
|
MALLOC_LOCK;
|
3288 |
|
|
check_inuse_chunk(p);
|
3289 |
|
|
MALLOC_UNLOCK;
|
3290 |
|
|
#endif
|
3291 |
|
|
return chunksize(p) - SIZE_SZ;
|
3292 |
|
|
}
|
3293 |
|
|
return chunksize(p) - 2*SIZE_SZ;
|
3294 |
|
|
}
|
3295 |
|
|
}
|
3296 |
|
|
|
3297 |
|
|
#endif /* DEFINE_MALLOC_USABLE_SIZE */
|
3298 |
|
|
|
3299 |
|
|
#ifdef DEFINE_MALLINFO
|
3300 |
|
|
|
3301 |
|
|
/* Utility to update current_mallinfo for malloc_stats and mallinfo() */
|
3302 |
|
|
|
3303 |
|
|
STATIC void malloc_update_mallinfo()
|
3304 |
|
|
{
|
3305 |
|
|
int i;
|
3306 |
|
|
mbinptr b;
|
3307 |
|
|
mchunkptr p;
|
3308 |
|
|
#if DEBUG
|
3309 |
|
|
mchunkptr q;
|
3310 |
|
|
#endif
|
3311 |
|
|
|
3312 |
|
|
INTERNAL_SIZE_T avail = chunksize(top);
|
3313 |
|
|
int navail = ((long)(avail) >= (long)MINSIZE)? 1 : 0;
|
3314 |
|
|
|
3315 |
|
|
for (i = 1; i < NAV; ++i)
|
3316 |
|
|
{
|
3317 |
|
|
b = bin_at(i);
|
3318 |
|
|
for (p = last(b); p != b; p = p->bk)
|
3319 |
|
|
{
|
3320 |
|
|
#if DEBUG
|
3321 |
|
|
check_free_chunk(p);
|
3322 |
|
|
for (q = next_chunk(p);
|
3323 |
|
|
q < top && inuse(q) && (long)(chunksize(q)) >= (long)MINSIZE;
|
3324 |
|
|
q = next_chunk(q))
|
3325 |
|
|
check_inuse_chunk(q);
|
3326 |
|
|
#endif
|
3327 |
|
|
avail += chunksize(p);
|
3328 |
|
|
navail++;
|
3329 |
|
|
}
|
3330 |
|
|
}
|
3331 |
|
|
|
3332 |
|
|
current_mallinfo.ordblks = navail;
|
3333 |
|
|
current_mallinfo.uordblks = sbrked_mem - avail;
|
3334 |
|
|
current_mallinfo.fordblks = avail;
|
3335 |
|
|
#if HAVE_MMAP
|
3336 |
|
|
current_mallinfo.hblks = n_mmaps;
|
3337 |
|
|
current_mallinfo.hblkhd = mmapped_mem;
|
3338 |
|
|
#endif
|
3339 |
|
|
current_mallinfo.keepcost = chunksize(top);
|
3340 |
|
|
|
3341 |
|
|
}
|
3342 |
|
|
|
3343 |
|
|
#else /* ! DEFINE_MALLINFO */
|
3344 |
|
|
|
3345 |
|
|
#if __STD_C
|
3346 |
|
|
extern void malloc_update_mallinfo(void);
|
3347 |
|
|
#else
|
3348 |
|
|
extern void malloc_update_mallinfo();
|
3349 |
|
|
#endif
|
3350 |
|
|
|
3351 |
|
|
#endif /* ! DEFINE_MALLINFO */
|
3352 |
|
|
|
3353 |
|
|
#ifdef DEFINE_MALLOC_STATS
|
3354 |
|
|
|
3355 |
|
|
/*
|
3356 |
|
|
|
3357 |
|
|
malloc_stats:
|
3358 |
|
|
|
3359 |
|
|
Prints on stderr the amount of space obtain from the system (both
|
3360 |
|
|
via sbrk and mmap), the maximum amount (which may be more than
|
3361 |
|
|
current if malloc_trim and/or munmap got called), the maximum
|
3362 |
|
|
number of simultaneous mmap regions used, and the current number
|
3363 |
|
|
of bytes allocated via malloc (or realloc, etc) but not yet
|
3364 |
|
|
freed. (Note that this is the number of bytes allocated, not the
|
3365 |
|
|
number requested. It will be larger than the number requested
|
3366 |
|
|
because of alignment and bookkeeping overhead.)
|
3367 |
|
|
|
3368 |
|
|
*/
|
3369 |
|
|
|
3370 |
|
|
#if __STD_C
|
3371 |
|
|
void malloc_stats(RONEARG)
|
3372 |
|
|
#else
|
3373 |
|
|
void malloc_stats(RONEARG) RDECL
|
3374 |
|
|
#endif
|
3375 |
|
|
{
|
3376 |
|
|
unsigned long local_max_total_mem;
|
3377 |
|
|
int local_sbrked_mem;
|
3378 |
|
|
struct mallinfo local_mallinfo;
|
3379 |
|
|
#if HAVE_MMAP
|
3380 |
|
|
unsigned long local_mmapped_mem, local_max_n_mmaps;
|
3381 |
|
|
#endif
|
3382 |
|
|
FILE *fp;
|
3383 |
|
|
|
3384 |
|
|
MALLOC_LOCK;
|
3385 |
|
|
malloc_update_mallinfo();
|
3386 |
|
|
local_max_total_mem = max_total_mem;
|
3387 |
|
|
local_sbrked_mem = sbrked_mem;
|
3388 |
|
|
local_mallinfo = current_mallinfo;
|
3389 |
|
|
#if HAVE_MMAP
|
3390 |
|
|
local_mmapped_mem = mmapped_mem;
|
3391 |
|
|
local_max_n_mmaps = max_n_mmaps;
|
3392 |
|
|
#endif
|
3393 |
|
|
MALLOC_UNLOCK;
|
3394 |
|
|
|
3395 |
|
|
#ifdef INTERNAL_NEWLIB
|
3396 |
|
|
fp = _stderr_r(reent_ptr);
|
3397 |
|
|
#define fprintf fiprintf
|
3398 |
|
|
#else
|
3399 |
|
|
fp = stderr;
|
3400 |
|
|
#endif
|
3401 |
|
|
|
3402 |
|
|
fprintf(fp, "max system bytes = %10u\n",
|
3403 |
|
|
(unsigned int)(local_max_total_mem));
|
3404 |
|
|
#if HAVE_MMAP
|
3405 |
|
|
fprintf(fp, "system bytes = %10u\n",
|
3406 |
|
|
(unsigned int)(local_sbrked_mem + local_mmapped_mem));
|
3407 |
|
|
fprintf(fp, "in use bytes = %10u\n",
|
3408 |
|
|
(unsigned int)(local_mallinfo.uordblks + local_mmapped_mem));
|
3409 |
|
|
#else
|
3410 |
|
|
fprintf(fp, "system bytes = %10u\n",
|
3411 |
|
|
(unsigned int)local_sbrked_mem);
|
3412 |
|
|
fprintf(fp, "in use bytes = %10u\n",
|
3413 |
|
|
(unsigned int)local_mallinfo.uordblks);
|
3414 |
|
|
#endif
|
3415 |
|
|
#if HAVE_MMAP
|
3416 |
|
|
fprintf(fp, "max mmap regions = %10u\n",
|
3417 |
|
|
(unsigned int)local_max_n_mmaps);
|
3418 |
|
|
#endif
|
3419 |
|
|
}
|
3420 |
|
|
|
3421 |
|
|
#endif /* DEFINE_MALLOC_STATS */
|
3422 |
|
|
|
3423 |
|
|
#ifdef DEFINE_MALLINFO
|
3424 |
|
|
|
3425 |
|
|
/*
|
3426 |
|
|
mallinfo returns a copy of updated current mallinfo.
|
3427 |
|
|
*/
|
3428 |
|
|
|
3429 |
|
|
#if __STD_C
|
3430 |
|
|
struct mallinfo mALLINFo(RONEARG)
|
3431 |
|
|
#else
|
3432 |
|
|
struct mallinfo mALLINFo(RONEARG) RDECL
|
3433 |
|
|
#endif
|
3434 |
|
|
{
|
3435 |
|
|
struct mallinfo ret;
|
3436 |
|
|
|
3437 |
|
|
MALLOC_LOCK;
|
3438 |
|
|
malloc_update_mallinfo();
|
3439 |
|
|
ret = current_mallinfo;
|
3440 |
|
|
MALLOC_UNLOCK;
|
3441 |
|
|
return ret;
|
3442 |
|
|
}
|
3443 |
|
|
|
3444 |
|
|
#endif /* DEFINE_MALLINFO */
|
3445 |
|
|
|
3446 |
|
|
#ifdef DEFINE_MALLOPT
|
3447 |
|
|
|
3448 |
|
|
/*
|
3449 |
|
|
mallopt:
|
3450 |
|
|
|
3451 |
|
|
mallopt is the general SVID/XPG interface to tunable parameters.
|
3452 |
|
|
The format is to provide a (parameter-number, parameter-value) pair.
|
3453 |
|
|
mallopt then sets the corresponding parameter to the argument
|
3454 |
|
|
value if it can (i.e., so long as the value is meaningful),
|
3455 |
|
|
and returns 1 if successful else 0.
|
3456 |
|
|
|
3457 |
|
|
See descriptions of tunable parameters above.
|
3458 |
|
|
|
3459 |
|
|
*/
|
3460 |
|
|
|
3461 |
|
|
#if __STD_C
|
3462 |
|
|
int mALLOPt(RARG int param_number, int value)
|
3463 |
|
|
#else
|
3464 |
|
|
int mALLOPt(RARG param_number, value) RDECL int param_number; int value;
|
3465 |
|
|
#endif
|
3466 |
|
|
{
|
3467 |
|
|
MALLOC_LOCK;
|
3468 |
|
|
switch(param_number)
|
3469 |
|
|
{
|
3470 |
|
|
case M_TRIM_THRESHOLD:
|
3471 |
|
|
trim_threshold = value; MALLOC_UNLOCK; return 1;
|
3472 |
|
|
case M_TOP_PAD:
|
3473 |
|
|
top_pad = value; MALLOC_UNLOCK; return 1;
|
3474 |
|
|
case M_MMAP_THRESHOLD:
|
3475 |
|
|
#if HAVE_MMAP
|
3476 |
|
|
mmap_threshold = value;
|
3477 |
|
|
#endif
|
3478 |
|
|
MALLOC_UNLOCK;
|
3479 |
|
|
return 1;
|
3480 |
|
|
case M_MMAP_MAX:
|
3481 |
|
|
#if HAVE_MMAP
|
3482 |
|
|
n_mmaps_max = value; MALLOC_UNLOCK; return 1;
|
3483 |
|
|
#else
|
3484 |
|
|
MALLOC_UNLOCK; return value == 0;
|
3485 |
|
|
#endif
|
3486 |
|
|
|
3487 |
|
|
default:
|
3488 |
|
|
MALLOC_UNLOCK;
|
3489 |
|
|
return 0;
|
3490 |
|
|
}
|
3491 |
|
|
}
|
3492 |
|
|
|
3493 |
|
|
#endif /* DEFINE_MALLOPT */
|
3494 |
|
|
|
3495 |
|
|
/*
|
3496 |
|
|
|
3497 |
|
|
History:
|
3498 |
|
|
|
3499 |
|
|
V2.6.3 Sun May 19 08:17:58 1996 Doug Lea (dl at gee)
|
3500 |
|
|
* Added pvalloc, as recommended by H.J. Liu
|
3501 |
|
|
* Added 64bit pointer support mainly from Wolfram Gloger
|
3502 |
|
|
* Added anonymously donated WIN32 sbrk emulation
|
3503 |
|
|
* Malloc, calloc, getpagesize: add optimizations from Raymond Nijssen
|
3504 |
|
|
* malloc_extend_top: fix mask error that caused wastage after
|
3505 |
|
|
foreign sbrks
|
3506 |
|
|
* Add linux mremap support code from HJ Liu
|
3507 |
|
|
|
3508 |
|
|
V2.6.2 Tue Dec 5 06:52:55 1995 Doug Lea (dl at gee)
|
3509 |
|
|
* Integrated most documentation with the code.
|
3510 |
|
|
* Add support for mmap, with help from
|
3511 |
|
|
Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
|
3512 |
|
|
* Use last_remainder in more cases.
|
3513 |
|
|
* Pack bins using idea from colin@nyx10.cs.du.edu
|
3514 |
|
|
* Use ordered bins instead of best-fit threshhold
|
3515 |
|
|
* Eliminate block-local decls to simplify tracing and debugging.
|
3516 |
|
|
* Support another case of realloc via move into top
|
3517 |
|
|
* Fix error occuring when initial sbrk_base not word-aligned.
|
3518 |
|
|
* Rely on page size for units instead of SBRK_UNIT to
|
3519 |
|
|
avoid surprises about sbrk alignment conventions.
|
3520 |
|
|
* Add mallinfo, mallopt. Thanks to Raymond Nijssen
|
3521 |
|
|
(raymond@es.ele.tue.nl) for the suggestion.
|
3522 |
|
|
* Add `pad' argument to malloc_trim and top_pad mallopt parameter.
|
3523 |
|
|
* More precautions for cases where other routines call sbrk,
|
3524 |
|
|
courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
|
3525 |
|
|
* Added macros etc., allowing use in linux libc from
|
3526 |
|
|
H.J. Lu (hjl@gnu.ai.mit.edu)
|
3527 |
|
|
* Inverted this history list
|
3528 |
|
|
|
3529 |
|
|
V2.6.1 Sat Dec 2 14:10:57 1995 Doug Lea (dl at gee)
|
3530 |
|
|
* Re-tuned and fixed to behave more nicely with V2.6.0 changes.
|
3531 |
|
|
* Removed all preallocation code since under current scheme
|
3532 |
|
|
the work required to undo bad preallocations exceeds
|
3533 |
|
|
the work saved in good cases for most test programs.
|
3534 |
|
|
* No longer use return list or unconsolidated bins since
|
3535 |
|
|
no scheme using them consistently outperforms those that don't
|
3536 |
|
|
given above changes.
|
3537 |
|
|
* Use best fit for very large chunks to prevent some worst-cases.
|
3538 |
|
|
* Added some support for debugging
|
3539 |
|
|
|
3540 |
|
|
V2.6.0 Sat Nov 4 07:05:23 1995 Doug Lea (dl at gee)
|
3541 |
|
|
* Removed footers when chunks are in use. Thanks to
|
3542 |
|
|
Paul Wilson (wilson@cs.texas.edu) for the suggestion.
|
3543 |
|
|
|
3544 |
|
|
V2.5.4 Wed Nov 1 07:54:51 1995 Doug Lea (dl at gee)
|
3545 |
|
|
* Added malloc_trim, with help from Wolfram Gloger
|
3546 |
|
|
(wmglo@Dent.MED.Uni-Muenchen.DE).
|
3547 |
|
|
|
3548 |
|
|
V2.5.3 Tue Apr 26 10:16:01 1994 Doug Lea (dl at g)
|
3549 |
|
|
|
3550 |
|
|
V2.5.2 Tue Apr 5 16:20:40 1994 Doug Lea (dl at g)
|
3551 |
|
|
* realloc: try to expand in both directions
|
3552 |
|
|
* malloc: swap order of clean-bin strategy;
|
3553 |
|
|
* realloc: only conditionally expand backwards
|
3554 |
|
|
* Try not to scavenge used bins
|
3555 |
|
|
* Use bin counts as a guide to preallocation
|
3556 |
|
|
* Occasionally bin return list chunks in first scan
|
3557 |
|
|
* Add a few optimizations from colin@nyx10.cs.du.edu
|
3558 |
|
|
|
3559 |
|
|
V2.5.1 Sat Aug 14 15:40:43 1993 Doug Lea (dl at g)
|
3560 |
|
|
* faster bin computation & slightly different binning
|
3561 |
|
|
* merged all consolidations to one part of malloc proper
|
3562 |
|
|
(eliminating old malloc_find_space & malloc_clean_bin)
|
3563 |
|
|
* Scan 2 returns chunks (not just 1)
|
3564 |
|
|
* Propagate failure in realloc if malloc returns 0
|
3565 |
|
|
* Add stuff to allow compilation on non-ANSI compilers
|
3566 |
|
|
from kpv@research.att.com
|
3567 |
|
|
|
3568 |
|
|
V2.5 Sat Aug 7 07:41:59 1993 Doug Lea (dl at g.oswego.edu)
|
3569 |
|
|
* removed potential for odd address access in prev_chunk
|
3570 |
|
|
* removed dependency on getpagesize.h
|
3571 |
|
|
* misc cosmetics and a bit more internal documentation
|
3572 |
|
|
* anticosmetics: mangled names in macros to evade debugger strangeness
|
3573 |
|
|
* tested on sparc, hp-700, dec-mips, rs6000
|
3574 |
|
|
with gcc & native cc (hp, dec only) allowing
|
3575 |
|
|
Detlefs & Zorn comparison study (in SIGPLAN Notices.)
|
3576 |
|
|
|
3577 |
|
|
Trial version Fri Aug 28 13:14:29 1992 Doug Lea (dl at g.oswego.edu)
|
3578 |
|
|
* Based loosely on libg++-1.2X malloc. (It retains some of the overall
|
3579 |
|
|
structure of old version, but most details differ.)
|
3580 |
|
|
|
3581 |
|
|
*/
|