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