Subversion Repositories HelenOS-historic

Rev

Rev 1143 | Rev 1663 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
968 palkovsky 1
/*
2
  This is a version (aka dlmalloc) of malloc/free/realloc written by
3
  Doug Lea and released to the public domain, as explained at
4
  http://creativecommons.org/licenses/publicdomain.  Send questions,
5
  comments, complaints, performance data, etc to dl@cs.oswego.edu
6
 
7
* Version 2.8.3 Thu Sep 22 11:16:15 2005  Doug Lea  (dl at gee)
8
 
9
   Note: There may be an updated version of this malloc obtainable at
10
           ftp://gee.cs.oswego.edu/pub/misc/malloc.c
11
         Check before installing!
12
 
13
* Quickstart
14
 
15
  This library is all in one file to simplify the most common usage:
16
  ftp it, compile it (-O3), and link it into another program. All of
17
  the compile-time options default to reasonable values for use on
18
  most platforms.  You might later want to step through various
19
  compile-time and dynamic tuning options.
20
 
21
  For convenience, an include file for code using this malloc is at:
22
     ftp://gee.cs.oswego.edu/pub/misc/malloc-2.8.3.h
23
  You don't really need this .h file unless you call functions not
24
  defined in your system include files.  The .h file contains only the
25
  excerpts from this file needed for using this malloc on ANSI C/C++
26
  systems, so long as you haven't changed compile-time options about
27
  naming and tuning parameters.  If you do, then you can create your
28
  own malloc.h that does include all settings by cutting at the point
29
  indicated below. Note that you may already by default be using a C
30
  library containing a malloc that is based on some version of this
31
  malloc (for example in linux). You might still want to use the one
32
  in this file to customize settings or to avoid overheads associated
33
  with library versions.
34
 
35
* Vital statistics:
36
 
37
  Supported pointer/size_t representation:       4 or 8 bytes
38
       size_t MUST be an unsigned type of the same width as
39
       pointers. (If you are using an ancient system that declares
40
       size_t as a signed type, or need it to be a different width
41
       than pointers, you can use a previous release of this malloc
42
       (e.g. 2.7.2) supporting these.)
43
 
44
  Alignment:                                     8 bytes (default)
45
       This suffices for nearly all current machines and C compilers.
46
       However, you can define MALLOC_ALIGNMENT to be wider than this
47
       if necessary (up to 128bytes), at the expense of using more space.
48
 
49
  Minimum overhead per allocated chunk:   4 or  8 bytes (if 4byte sizes)
50
                                          8 or 16 bytes (if 8byte sizes)
51
       Each malloced chunk has a hidden word of overhead holding size
52
       and status information, and additional cross-check word
53
       if FOOTERS is defined.
54
 
55
  Minimum allocated size: 4-byte ptrs:  16 bytes    (including overhead)
56
                          8-byte ptrs:  32 bytes    (including overhead)
57
 
58
       Even a request for zero bytes (i.e., malloc(0)) returns a
59
       pointer to something of the minimum allocatable size.
60
       The maximum overhead wastage (i.e., number of extra bytes
61
       allocated than were requested in malloc) is less than or equal
62
       to the minimum size, except for requests >= mmap_threshold that
63
       are serviced via mmap(), where the worst case wastage is about
64
       32 bytes plus the remainder from a system page (the minimal
65
       mmap unit); typically 4096 or 8192 bytes.
66
 
67
  Security: static-safe; optionally more or less
68
       The "security" of malloc refers to the ability of malicious
69
       code to accentuate the effects of errors (for example, freeing
70
       space that is not currently malloc'ed or overwriting past the
71
       ends of chunks) in code that calls malloc.  This malloc
72
       guarantees not to modify any memory locations below the base of
73
       heap, i.e., static variables, even in the presence of usage
74
       errors.  The routines additionally detect most improper frees
75
       and reallocs.  All this holds as long as the static bookkeeping
76
       for malloc itself is not corrupted by some other means.  This
77
       is only one aspect of security -- these checks do not, and
78
       cannot, detect all possible programming errors.
79
 
80
       If FOOTERS is defined nonzero, then each allocated chunk
81
       carries an additional check word to verify that it was malloced
82
       from its space.  These check words are the same within each
83
       execution of a program using malloc, but differ across
84
       executions, so externally crafted fake chunks cannot be
85
       freed. This improves security by rejecting frees/reallocs that
86
       could corrupt heap memory, in addition to the checks preventing
87
       writes to statics that are always on.  This may further improve
88
       security at the expense of time and space overhead.  (Note that
89
       FOOTERS may also be worth using with MSPACES.)
90
 
91
       By default detected errors cause the program to abort (calling
92
       "abort()"). You can override this to instead proceed past
93
       errors by defining PROCEED_ON_ERROR.  In this case, a bad free
94
       has no effect, and a malloc that encounters a bad address
95
       caused by user overwrites will ignore the bad address by
96
       dropping pointers and indices to all known memory. This may
97
       be appropriate for programs that should continue if at all
98
       possible in the face of programming errors, although they may
99
       run out of memory because dropped memory is never reclaimed.
100
 
101
       If you don't like either of these options, you can define
102
       CORRUPTION_ERROR_ACTION and USAGE_ERROR_ACTION to do anything
103
       else. And if if you are sure that your program using malloc has
104
       no errors or vulnerabilities, you can define INSECURE to 1,
105
       which might (or might not) provide a small performance improvement.
106
 
107
  Thread-safety: NOT thread-safe unless USE_LOCKS defined
108
       When USE_LOCKS is defined, each public call to malloc, free,
109
       etc is surrounded with either a pthread mutex or a win32
110
       spinlock (depending on WIN32). This is not especially fast, and
111
       can be a major bottleneck.  It is designed only to provide
112
       minimal protection in concurrent environments, and to provide a
113
       basis for extensions.  If you are using malloc in a concurrent
114
       program, consider instead using ptmalloc, which is derived from
115
       a version of this malloc. (See http://www.malloc.de).
116
 
117
  System requirements: Any combination of MORECORE and/or MMAP/MUNMAP
118
       This malloc can use unix sbrk or any emulation (invoked using
119
       the CALL_MORECORE macro) and/or mmap/munmap or any emulation
120
       (invoked using CALL_MMAP/CALL_MUNMAP) to get and release system
121
       memory.  On most unix systems, it tends to work best if both
122
       MORECORE and MMAP are enabled.  On Win32, it uses emulations
123
       based on VirtualAlloc. It also uses common C library functions
124
       like memset.
125
 
126
  Compliance: I believe it is compliant with the Single Unix Specification
127
       (See http://www.unix.org). Also SVID/XPG, ANSI C, and probably
128
       others as well.
129
 
130
* Overview of algorithms
131
 
132
  This is not the fastest, most space-conserving, most portable, or
133
  most tunable malloc ever written. However it is among the fastest
134
  while also being among the most space-conserving, portable and
135
  tunable.  Consistent balance across these factors results in a good
136
  general-purpose allocator for malloc-intensive programs.
137
 
138
  In most ways, this malloc is a best-fit allocator. Generally, it
139
  chooses the best-fitting existing chunk for a request, with ties
140
  broken in approximately least-recently-used order. (This strategy
141
  normally maintains low fragmentation.) However, for requests less
142
  than 256bytes, it deviates from best-fit when there is not an
143
  exactly fitting available chunk by preferring to use space adjacent
144
  to that used for the previous small request, as well as by breaking
145
  ties in approximately most-recently-used order. (These enhance
146
  locality of series of small allocations.)  And for very large requests
147
  (>= 256Kb by default), it relies on system memory mapping
148
  facilities, if supported.  (This helps avoid carrying around and
149
  possibly fragmenting memory used only for large chunks.)
150
 
151
  All operations (except malloc_stats and mallinfo) have execution
152
  times that are bounded by a constant factor of the number of bits in
153
  a size_t, not counting any clearing in calloc or copying in realloc,
154
  or actions surrounding MORECORE and MMAP that have times
155
  proportional to the number of non-contiguous regions returned by
156
  system allocation routines, which is often just 1.
157
 
158
  The implementation is not very modular and seriously overuses
159
  macros. Perhaps someday all C compilers will do as good a job
160
  inlining modular code as can now be done by brute-force expansion,
161
  but now, enough of them seem not to.
162
 
163
  Some compilers issue a lot of warnings about code that is
164
  dead/unreachable only on some platforms, and also about intentional
165
  uses of negation on unsigned types. All known cases of each can be
166
  ignored.
167
 
168
  For a longer but out of date high-level description, see
169
     http://gee.cs.oswego.edu/dl/html/malloc.html
170
 
171
* MSPACES
172
  If MSPACES is defined, then in addition to malloc, free, etc.,
173
  this file also defines mspace_malloc, mspace_free, etc. These
174
  are versions of malloc routines that take an "mspace" argument
175
  obtained using create_mspace, to control all internal bookkeeping.
176
  If ONLY_MSPACES is defined, only these versions are compiled.
177
  So if you would like to use this allocator for only some allocations,
178
  and your system malloc for others, you can compile with
179
  ONLY_MSPACES and then do something like...
180
    static mspace mymspace = create_mspace(0,0); // for example
181
    #define mymalloc(bytes)  mspace_malloc(mymspace, bytes)
182
 
183
  (Note: If you only need one instance of an mspace, you can instead
184
  use "USE_DL_PREFIX" to relabel the global malloc.)
185
 
186
  You can similarly create thread-local allocators by storing
187
  mspaces as thread-locals. For example:
188
    static __thread mspace tlms = 0;
189
    void*  tlmalloc(size_t bytes) {
190
      if (tlms == 0) tlms = create_mspace(0, 0);
191
      return mspace_malloc(tlms, bytes);
192
    }
193
    void  tlfree(void* mem) { mspace_free(tlms, mem); }
194
 
195
  Unless FOOTERS is defined, each mspace is completely independent.
196
  You cannot allocate from one and free to another (although
197
  conformance is only weakly checked, so usage errors are not always
198
  caught). If FOOTERS is defined, then each chunk carries around a tag
199
  indicating its originating mspace, and frees are directed to their
200
  originating spaces.
201
 
202
 -------------------------  Compile-time options ---------------------------
203
 
204
Be careful in setting #define values for numerical constants of type
205
size_t. On some systems, literal values are not automatically extended
206
to size_t precision unless they are explicitly casted.
207
 
208
WIN32                    default: defined if _WIN32 defined
209
  Defining WIN32 sets up defaults for MS environment and compilers.
210
  Otherwise defaults are for unix.
211
 
212
MALLOC_ALIGNMENT         default: (size_t)8
213
  Controls the minimum alignment for malloc'ed chunks.  It must be a
214
  power of two and at least 8, even on machines for which smaller
215
  alignments would suffice. It may be defined as larger than this
216
  though. Note however that code and data structures are optimized for
217
  the case of 8-byte alignment.
218
 
219
MSPACES                  default: 0 (false)
220
  If true, compile in support for independent allocation spaces.
221
  This is only supported if HAVE_MMAP is true.
222
 
223
ONLY_MSPACES             default: 0 (false)
224
  If true, only compile in mspace versions, not regular versions.
225
 
226
USE_LOCKS                default: 0 (false)
227
  Causes each call to each public routine to be surrounded with
228
  pthread or WIN32 mutex lock/unlock. (If set true, this can be
229
  overridden on a per-mspace basis for mspace versions.)
230
 
231
FOOTERS                  default: 0
232
  If true, provide extra checking and dispatching by placing
233
  information in the footers of allocated chunks. This adds
234
  space and time overhead.
235
 
236
INSECURE                 default: 0
237
  If true, omit checks for usage errors and heap space overwrites.
238
 
239
USE_DL_PREFIX            default: NOT defined
240
  Causes compiler to prefix all public routines with the string 'dl'.
241
  This can be useful when you only want to use this malloc in one part
242
  of a program, using your regular system malloc elsewhere.
243
 
244
ABORT                    default: defined as abort()
245
  Defines how to abort on failed checks.  On most systems, a failed
246
  check cannot die with an "assert" or even print an informative
247
  message, because the underlying print routines in turn call malloc,
248
  which will fail again.  Generally, the best policy is to simply call
249
  abort(). It's not very useful to do more than this because many
250
  errors due to overwriting will show up as address faults (null, odd
251
  addresses etc) rather than malloc-triggered checks, so will also
252
  abort.  Also, most compilers know that abort() does not return, so
253
  can better optimize code conditionally calling it.
254
 
255
PROCEED_ON_ERROR           default: defined as 0 (false)
256
  Controls whether detected bad addresses cause them to bypassed
257
  rather than aborting. If set, detected bad arguments to free and
258
  realloc are ignored. And all bookkeeping information is zeroed out
259
  upon a detected overwrite of freed heap space, thus losing the
260
  ability to ever return it from malloc again, but enabling the
261
  application to proceed. If PROCEED_ON_ERROR is defined, the
262
  static variable malloc_corruption_error_count is compiled in
263
  and can be examined to see if errors have occurred. This option
264
  generates slower code than the default abort policy.
265
 
266
DEBUG                    default: NOT defined
267
  The DEBUG setting is mainly intended for people trying to modify
268
  this code or diagnose problems when porting to new platforms.
269
  However, it may also be able to better isolate user errors than just
270
  using runtime checks.  The assertions in the check routines spell
271
  out in more detail the assumptions and invariants underlying the
272
  algorithms.  The checking is fairly extensive, and will slow down
273
  execution noticeably. Calling malloc_stats or mallinfo with DEBUG
274
  set will attempt to check every non-mmapped allocated and free chunk
275
  in the course of computing the summaries.
276
 
277
ABORT_ON_ASSERT_FAILURE   default: defined as 1 (true)
278
  Debugging assertion failures can be nearly impossible if your
279
  version of the assert macro causes malloc to be called, which will
280
  lead to a cascade of further failures, blowing the runtime stack.
281
  ABORT_ON_ASSERT_FAILURE cause assertions failures to call abort(),
282
  which will usually make debugging easier.
283
 
284
MALLOC_FAILURE_ACTION     default: sets errno to ENOMEM, or no-op on win32
285
  The action to take before "return 0" when malloc fails to be able to
286
  return memory because there is none available.
287
 
288
HAVE_MORECORE             default: 1 (true) unless win32 or ONLY_MSPACES
289
  True if this system supports sbrk or an emulation of it.
290
 
291
MORECORE                  default: sbrk
292
  The name of the sbrk-style system routine to call to obtain more
293
  memory.  See below for guidance on writing custom MORECORE
294
  functions. The type of the argument to sbrk/MORECORE varies across
295
  systems.  It cannot be size_t, because it supports negative
296
  arguments, so it is normally the signed type of the same width as
297
  size_t (sometimes declared as "intptr_t").  It doesn't much matter
298
  though. Internally, we only call it with arguments less than half
299
  the max value of a size_t, which should work across all reasonable
300
  possibilities, although sometimes generating compiler warnings.  See
301
  near the end of this file for guidelines for creating a custom
302
  version of MORECORE.
303
 
304
MORECORE_CONTIGUOUS       default: 1 (true)
305
  If true, take advantage of fact that consecutive calls to MORECORE
306
  with positive arguments always return contiguous increasing
307
  addresses.  This is true of unix sbrk. It does not hurt too much to
308
  set it true anyway, since malloc copes with non-contiguities.
309
  Setting it false when definitely non-contiguous saves time
310
  and possibly wasted space it would take to discover this though.
311
 
312
MORECORE_CANNOT_TRIM      default: NOT defined
313
  True if MORECORE cannot release space back to the system when given
314
  negative arguments. This is generally necessary only if you are
315
  using a hand-crafted MORECORE function that cannot handle negative
316
  arguments.
317
 
318
HAVE_MMAP                 default: 1 (true)
319
  True if this system supports mmap or an emulation of it.  If so, and
320
  HAVE_MORECORE is not true, MMAP is used for all system
321
  allocation. If set and HAVE_MORECORE is true as well, MMAP is
322
  primarily used to directly allocate very large blocks. It is also
323
  used as a backup strategy in cases where MORECORE fails to provide
324
  space from system. Note: A single call to MUNMAP is assumed to be
325
  able to unmap memory that may have be allocated using multiple calls
326
  to MMAP, so long as they are adjacent.
327
 
328
HAVE_MREMAP               default: 1 on linux, else 0
329
  If true realloc() uses mremap() to re-allocate large blocks and
330
  extend or shrink allocation spaces.
331
 
332
MMAP_CLEARS               default: 1 on unix
333
  True if mmap clears memory so calloc doesn't need to. This is true
334
  for standard unix mmap using /dev/zero.
335
 
336
USE_BUILTIN_FFS            default: 0 (i.e., not used)
337
  Causes malloc to use the builtin ffs() function to compute indices.
338
  Some compilers may recognize and intrinsify ffs to be faster than the
339
  supplied C version. Also, the case of x86 using gcc is special-cased
340
  to an asm instruction, so is already as fast as it can be, and so
341
  this setting has no effect. (On most x86s, the asm version is only
342
  slightly faster than the C version.)
343
 
344
malloc_getpagesize         default: derive from system includes, or 4096.
345
  The system page size. To the extent possible, this malloc manages
346
  memory from the system in page-size units.  This may be (and
347
  usually is) a function rather than a constant. This is ignored
348
  if WIN32, where page size is determined using getSystemInfo during
349
  initialization.
350
 
351
USE_DEV_RANDOM             default: 0 (i.e., not used)
352
  Causes malloc to use /dev/random to initialize secure magic seed for
353
  stamping footers. Otherwise, the current time is used.
354
 
355
NO_MALLINFO                default: 0
356
  If defined, don't compile "mallinfo". This can be a simple way
357
  of dealing with mismatches between system declarations and
358
  those in this file.
359
 
360
MALLINFO_FIELD_TYPE        default: size_t
361
  The type of the fields in the mallinfo struct. This was originally
362
  defined as "int" in SVID etc, but is more usefully defined as
363
  size_t. The value is used only if  HAVE_USR_INCLUDE_MALLOC_H is not set
364
 
365
REALLOC_ZERO_BYTES_FREES    default: not defined
366
  This should be set if a call to realloc with zero bytes should
367
  be the same as a call to free. Some people think it should. Otherwise,
368
  since this malloc returns a unique pointer for malloc(0), so does
369
  realloc(p, 0).
370
 
371
LACKS_UNISTD_H, LACKS_FCNTL_H, LACKS_SYS_PARAM_H, LACKS_SYS_MMAN_H
372
LACKS_STRINGS_H, LACKS_STRING_H, LACKS_SYS_TYPES_H,  LACKS_ERRNO_H
373
LACKS_STDLIB_H                default: NOT defined unless on WIN32
374
  Define these if your system does not have these header files.
375
  You might need to manually insert some of the declarations they provide.
376
 
377
DEFAULT_GRANULARITY        default: page size if MORECORE_CONTIGUOUS,
378
                                system_info.dwAllocationGranularity in WIN32,
379
                                otherwise 64K.
380
      Also settable using mallopt(M_GRANULARITY, x)
381
  The unit for allocating and deallocating memory from the system.  On
382
  most systems with contiguous MORECORE, there is no reason to
383
  make this more than a page. However, systems with MMAP tend to
384
  either require or encourage larger granularities.  You can increase
385
  this value to prevent system allocation functions to be called so
386
  often, especially if they are slow.  The value must be at least one
387
  page and must be a power of two.  Setting to 0 causes initialization
388
  to either page size or win32 region size.  (Note: In previous
389
  versions of malloc, the equivalent of this option was called
390
  "TOP_PAD")
391
 
392
DEFAULT_TRIM_THRESHOLD    default: 2MB
393
      Also settable using mallopt(M_TRIM_THRESHOLD, x)
394
  The maximum amount of unused top-most memory to keep before
395
  releasing via malloc_trim in free().  Automatic trimming is mainly
396
  useful in long-lived programs using contiguous MORECORE.  Because
397
  trimming via sbrk can be slow on some systems, and can sometimes be
398
  wasteful (in cases where programs immediately afterward allocate
399
  more large chunks) the value should be high enough so that your
400
  overall system performance would improve by releasing this much
401
  memory.  As a rough guide, you might set to a value close to the
402
  average size of a process (program) running on your system.
403
  Releasing this much memory would allow such a process to run in
404
  memory.  Generally, it is worth tuning trim thresholds when a
405
  program undergoes phases where several large chunks are allocated
406
  and released in ways that can reuse each other's storage, perhaps
407
  mixed with phases where there are no such chunks at all. The trim
408
  value must be greater than page size to have any useful effect.  To
409
  disable trimming completely, you can set to MAX_SIZE_T. Note that the trick
410
  some people use of mallocing a huge space and then freeing it at
411
  program startup, in an attempt to reserve system memory, doesn't
412
  have the intended effect under automatic trimming, since that memory
413
  will immediately be returned to the system.
414
 
415
DEFAULT_MMAP_THRESHOLD       default: 256K
416
      Also settable using mallopt(M_MMAP_THRESHOLD, x)
417
  The request size threshold for using MMAP to directly service a
418
  request. Requests of at least this size that cannot be allocated
419
  using already-existing space will be serviced via mmap.  (If enough
420
  normal freed space already exists it is used instead.)  Using mmap
421
  segregates relatively large chunks of memory so that they can be
422
  individually obtained and released from the host system. A request
423
  serviced through mmap is never reused by any other request (at least
424
  not directly; the system may just so happen to remap successive
425
  requests to the same locations).  Segregating space in this way has
426
  the benefits that: Mmapped space can always be individually released
427
  back to the system, which helps keep the system level memory demands
428
  of a long-lived program low.  Also, mapped memory doesn't become
429
  `locked' between other chunks, as can happen with normally allocated
430
  chunks, which means that even trimming via malloc_trim would not
431
  release them.  However, it has the disadvantage that the space
432
  cannot be reclaimed, consolidated, and then used to service later
433
  requests, as happens with normal chunks.  The advantages of mmap
434
  nearly always outweigh disadvantages for "large" chunks, but the
435
  value of "large" may vary across systems.  The default is an
436
  empirically derived value that works well in most systems. You can
437
  disable mmap by setting to MAX_SIZE_T.
438
 
439
*/
440
 
1656 cejka 441
/** @addtogroup libcmalloc malloc
442
  * @brief Malloc originally written by Doug Lea and ported to HelenOS.
443
  * @ingroup libc
444
 * @{
445
 */
446
/** @file
447
 */
448
 
449
 
999 palkovsky 450
#include <sys/types.h>  /* For size_t */
451
 
452
/** Non-default helenos customizations */
453
#define LACKS_FCNTL_H
454
#define LACKS_SYS_MMAN_H
968 palkovsky 455
#define LACKS_SYS_PARAM_H
999 palkovsky 456
#undef HAVE_MMAP
457
#define HAVE_MMAP 0
968 palkovsky 458
#define LACKS_ERRNO_H
999 palkovsky 459
/* Set errno? */
460
#undef MALLOC_FAILURE_ACTION
968 palkovsky 461
#define MALLOC_FAILURE_ACTION
462
 
463
/* The maximum possible size_t value has all bits set */
464
#define MAX_SIZE_T           (~(size_t)0)
465
 
466
#define ONLY_MSPACES 0
467
#define MSPACES 0
468
#define MALLOC_ALIGNMENT ((size_t)8U)
469
#define FOOTERS 0
470
#define ABORT  abort()
471
#define ABORT_ON_ASSERT_FAILURE 1
472
#define PROCEED_ON_ERROR 0
1143 palkovsky 473
#define USE_LOCKS 1
968 palkovsky 474
#define INSECURE 0
999 palkovsky 475
#define HAVE_MMAP 0
476
 
968 palkovsky 477
#define MMAP_CLEARS 1
999 palkovsky 478
 
968 palkovsky 479
#define HAVE_MORECORE 1
999 palkovsky 480
#define MORECORE_CONTIGUOUS 1
968 palkovsky 481
#define MORECORE sbrk
482
#define DEFAULT_GRANULARITY (0)  /* 0 means to compute in init_mparams */
999 palkovsky 483
 
968 palkovsky 484
#ifndef DEFAULT_TRIM_THRESHOLD
485
#ifndef MORECORE_CANNOT_TRIM
486
#define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U)
487
#else   /* MORECORE_CANNOT_TRIM */
488
#define DEFAULT_TRIM_THRESHOLD MAX_SIZE_T
489
#endif  /* MORECORE_CANNOT_TRIM */
490
#endif  /* DEFAULT_TRIM_THRESHOLD */
491
#ifndef DEFAULT_MMAP_THRESHOLD
492
#if HAVE_MMAP
493
#define DEFAULT_MMAP_THRESHOLD ((size_t)256U * (size_t)1024U)
494
#else   /* HAVE_MMAP */
495
#define DEFAULT_MMAP_THRESHOLD MAX_SIZE_T
496
#endif  /* HAVE_MMAP */
497
#endif  /* DEFAULT_MMAP_THRESHOLD */
498
#ifndef USE_BUILTIN_FFS
499
#define USE_BUILTIN_FFS 0
500
#endif  /* USE_BUILTIN_FFS */
501
#ifndef USE_DEV_RANDOM
502
#define USE_DEV_RANDOM 0
503
#endif  /* USE_DEV_RANDOM */
504
#ifndef NO_MALLINFO
505
#define NO_MALLINFO 0
506
#endif  /* NO_MALLINFO */
507
#ifndef MALLINFO_FIELD_TYPE
508
#define MALLINFO_FIELD_TYPE size_t
509
#endif  /* MALLINFO_FIELD_TYPE */
510
 
511
/*
512
  mallopt tuning options.  SVID/XPG defines four standard parameter
513
  numbers for mallopt, normally defined in malloc.h.  None of these
514
  are used in this malloc, so setting them has no effect. But this
515
  malloc does support the following options.
516
*/
517
 
518
#define M_TRIM_THRESHOLD     (-1)
519
#define M_GRANULARITY        (-2)
520
#define M_MMAP_THRESHOLD     (-3)
521
 
522
/*
523
  ========================================================================
524
  To make a fully customizable malloc.h header file, cut everything
525
  above this line, put into file malloc.h, edit to suit, and #include it
526
  on the next line, as well as in programs that use this malloc.
527
  ========================================================================
528
*/
529
 
985 palkovsky 530
#include "malloc.h"
968 palkovsky 531
 
532
/*------------------------------ internal #includes ---------------------- */
533
 
534
#include <stdio.h>       /* for printing in malloc_stats */
999 palkovsky 535
#include <string.h>
968 palkovsky 536
 
537
#ifndef LACKS_ERRNO_H
538
#include <errno.h>       /* for MALLOC_FAILURE_ACTION */
539
#endif /* LACKS_ERRNO_H */
540
#if FOOTERS
541
#include <time.h>        /* for magic initialization */
542
#endif /* FOOTERS */
543
#ifndef LACKS_STDLIB_H
544
#include <stdlib.h>      /* for abort() */
545
#endif /* LACKS_STDLIB_H */
546
#ifdef DEBUG
547
#if ABORT_ON_ASSERT_FAILURE
1113 palkovsky 548
#define assert(x) {if(!(x)) {printf(#x);ABORT;}}
968 palkovsky 549
#else /* ABORT_ON_ASSERT_FAILURE */
550
#include <assert.h>
551
#endif /* ABORT_ON_ASSERT_FAILURE */
552
#else  /* DEBUG */
553
#define assert(x)
554
#endif /* DEBUG */
555
#if USE_BUILTIN_FFS
556
#ifndef LACKS_STRINGS_H
557
#include <strings.h>     /* for ffs */
558
#endif /* LACKS_STRINGS_H */
559
#endif /* USE_BUILTIN_FFS */
560
#if HAVE_MMAP
561
#ifndef LACKS_SYS_MMAN_H
562
#include <sys/mman.h>    /* for mmap */
563
#endif /* LACKS_SYS_MMAN_H */
564
#ifndef LACKS_FCNTL_H
565
#include <fcntl.h>
566
#endif /* LACKS_FCNTL_H */
567
#endif /* HAVE_MMAP */
568
#if HAVE_MORECORE
569
#ifndef LACKS_UNISTD_H
570
#include <unistd.h>     /* for sbrk */
571
#else /* LACKS_UNISTD_H */
572
#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
573
extern void*     sbrk(ptrdiff_t);
574
#endif /* FreeBSD etc */
575
#endif /* LACKS_UNISTD_H */
576
#endif /* HAVE_MMAP */
577
 
578
#ifndef WIN32
579
#ifndef malloc_getpagesize
580
#  ifdef _SC_PAGESIZE         /* some SVR4 systems omit an underscore */
581
#    ifndef _SC_PAGE_SIZE
582
#      define _SC_PAGE_SIZE _SC_PAGESIZE
583
#    endif
584
#  endif
585
#  ifdef _SC_PAGE_SIZE
586
#    define malloc_getpagesize sysconf(_SC_PAGE_SIZE)
587
#  else
588
#    if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
589
       extern size_t getpagesize();
590
#      define malloc_getpagesize getpagesize()
591
#    else
592
#      ifdef WIN32 /* use supplied emulation of getpagesize */
593
#        define malloc_getpagesize getpagesize()
594
#      else
595
#        ifndef LACKS_SYS_PARAM_H
596
#          include <sys/param.h>
597
#        endif
598
#        ifdef EXEC_PAGESIZE
599
#          define malloc_getpagesize EXEC_PAGESIZE
600
#        else
601
#          ifdef NBPG
602
#            ifndef CLSIZE
603
#              define malloc_getpagesize NBPG
604
#            else
605
#              define malloc_getpagesize (NBPG * CLSIZE)
606
#            endif
607
#          else
608
#            ifdef NBPC
609
#              define malloc_getpagesize NBPC
610
#            else
611
#              ifdef PAGESIZE
612
#                define malloc_getpagesize PAGESIZE
613
#              else /* just guess */
614
#                define malloc_getpagesize ((size_t)4096U)
615
#              endif
616
#            endif
617
#          endif
618
#        endif
619
#      endif
620
#    endif
621
#  endif
622
#endif
623
#endif
624
 
625
/* ------------------- size_t and alignment properties -------------------- */
626
 
627
/* The byte and bit size of a size_t */
628
#define SIZE_T_SIZE         (sizeof(size_t))
629
#define SIZE_T_BITSIZE      (sizeof(size_t) << 3)
630
 
631
/* Some constants coerced to size_t */
632
/* Annoying but necessary to avoid errors on some plaftorms */
633
#define SIZE_T_ZERO         ((size_t)0)
634
#define SIZE_T_ONE          ((size_t)1)
635
#define SIZE_T_TWO          ((size_t)2)
636
#define TWO_SIZE_T_SIZES    (SIZE_T_SIZE<<1)
637
#define FOUR_SIZE_T_SIZES   (SIZE_T_SIZE<<2)
638
#define SIX_SIZE_T_SIZES    (FOUR_SIZE_T_SIZES+TWO_SIZE_T_SIZES)
639
#define HALF_MAX_SIZE_T     (MAX_SIZE_T / 2U)
640
 
641
/* The bit mask value corresponding to MALLOC_ALIGNMENT */
642
#define CHUNK_ALIGN_MASK    (MALLOC_ALIGNMENT - SIZE_T_ONE)
643
 
644
/* True if address a has acceptable alignment */
645
#define is_aligned(A)       (((size_t)((A)) & (CHUNK_ALIGN_MASK)) == 0)
646
 
647
/* the number of bytes to offset an address to align it */
648
#define align_offset(A)\
649
 ((((size_t)(A) & CHUNK_ALIGN_MASK) == 0)? 0 :\
650
  ((MALLOC_ALIGNMENT - ((size_t)(A) & CHUNK_ALIGN_MASK)) & CHUNK_ALIGN_MASK))
651
 
652
/* -------------------------- MMAP preliminaries ------------------------- */
653
 
654
/*
655
   If HAVE_MORECORE or HAVE_MMAP are false, we just define calls and
656
   checks to fail so compiler optimizer can delete code rather than
657
   using so many "#if"s.
658
*/
659
 
660
 
661
/* MORECORE and MMAP must return MFAIL on failure */
662
#define MFAIL                ((void*)(MAX_SIZE_T))
663
#define CMFAIL               ((char*)(MFAIL)) /* defined for convenience */
664
 
665
#if !HAVE_MMAP
666
#define IS_MMAPPED_BIT       (SIZE_T_ZERO)
667
#define USE_MMAP_BIT         (SIZE_T_ZERO)
668
#define CALL_MMAP(s)         MFAIL
669
#define CALL_MUNMAP(a, s)    (-1)
670
#define DIRECT_MMAP(s)       MFAIL
671
 
672
#else /* HAVE_MMAP */
673
#define IS_MMAPPED_BIT       (SIZE_T_ONE)
674
#define USE_MMAP_BIT         (SIZE_T_ONE)
675
 
676
#ifndef WIN32
677
#define CALL_MUNMAP(a, s)    munmap((a), (s))
678
#define MMAP_PROT            (PROT_READ|PROT_WRITE)
679
#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
680
#define MAP_ANONYMOUS        MAP_ANON
681
#endif /* MAP_ANON */
682
#ifdef MAP_ANONYMOUS
683
#define MMAP_FLAGS           (MAP_PRIVATE|MAP_ANONYMOUS)
684
#define CALL_MMAP(s)         mmap(0, (s), MMAP_PROT, MMAP_FLAGS, -1, 0)
685
#else /* MAP_ANONYMOUS */
686
/*
687
   Nearly all versions of mmap support MAP_ANONYMOUS, so the following
688
   is unlikely to be needed, but is supplied just in case.
689
*/
690
#define MMAP_FLAGS           (MAP_PRIVATE)
691
static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */
692
#define CALL_MMAP(s) ((dev_zero_fd < 0) ? \
693
           (dev_zero_fd = open("/dev/zero", O_RDWR), \
694
            mmap(0, (s), MMAP_PROT, MMAP_FLAGS, dev_zero_fd, 0)) : \
695
            mmap(0, (s), MMAP_PROT, MMAP_FLAGS, dev_zero_fd, 0))
696
#endif /* MAP_ANONYMOUS */
697
 
698
#define DIRECT_MMAP(s)       CALL_MMAP(s)
699
#else /* WIN32 */
700
 
701
/* Win32 MMAP via VirtualAlloc */
702
static void* win32mmap(size_t size) {
703
  void* ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
704
  return (ptr != 0)? ptr: MFAIL;
705
}
706
 
707
/* For direct MMAP, use MEM_TOP_DOWN to minimize interference */
708
static void* win32direct_mmap(size_t size) {
709
  void* ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN,
710
                           PAGE_READWRITE);
711
  return (ptr != 0)? ptr: MFAIL;
712
}
713
 
714
/* This function supports releasing coalesed segments */
715
static int win32munmap(void* ptr, size_t size) {
716
  MEMORY_BASIC_INFORMATION minfo;
717
  char* cptr = ptr;
718
  while (size) {
719
    if (VirtualQuery(cptr, &minfo, sizeof(minfo)) == 0)
720
      return -1;
721
    if (minfo.BaseAddress != cptr || minfo.AllocationBase != cptr ||
722
        minfo.State != MEM_COMMIT || minfo.RegionSize > size)
723
      return -1;
724
    if (VirtualFree(cptr, 0, MEM_RELEASE) == 0)
725
      return -1;
726
    cptr += minfo.RegionSize;
727
    size -= minfo.RegionSize;
728
  }
729
  return 0;
730
}
731
 
732
#define CALL_MMAP(s)         win32mmap(s)
733
#define CALL_MUNMAP(a, s)    win32munmap((a), (s))
734
#define DIRECT_MMAP(s)       win32direct_mmap(s)
735
#endif /* WIN32 */
736
#endif /* HAVE_MMAP */
737
 
738
#if HAVE_MMAP && HAVE_MREMAP
739
#define CALL_MREMAP(addr, osz, nsz, mv) mremap((addr), (osz), (nsz), (mv))
740
#else  /* HAVE_MMAP && HAVE_MREMAP */
741
#define CALL_MREMAP(addr, osz, nsz, mv) MFAIL
742
#endif /* HAVE_MMAP && HAVE_MREMAP */
743
 
744
#if HAVE_MORECORE
745
#define CALL_MORECORE(S)     MORECORE(S)
746
#else  /* HAVE_MORECORE */
747
#define CALL_MORECORE(S)     MFAIL
748
#endif /* HAVE_MORECORE */
749
 
750
/* mstate bit set if continguous morecore disabled or failed */
751
#define USE_NONCONTIGUOUS_BIT (4U)
752
 
753
/* segment bit set in create_mspace_with_base */
754
#define EXTERN_BIT            (8U)
755
 
756
 
757
/* --------------------------- Lock preliminaries ------------------------ */
758
 
759
#if USE_LOCKS
760
 
761
/*
762
  When locks are defined, there are up to two global locks:
763
 
764
  * If HAVE_MORECORE, morecore_mutex protects sequences of calls to
765
    MORECORE.  In many cases sys_alloc requires two calls, that should
766
    not be interleaved with calls by other threads.  This does not
767
    protect against direct calls to MORECORE by other threads not
768
    using this lock, so there is still code to cope the best we can on
769
    interference.
770
 
771
  * magic_init_mutex ensures that mparams.magic and other
772
    unique mparams values are initialized only once.
773
*/
774
 
775
/* By default use posix locks */
1143 palkovsky 776
#include <futex.h>
777
#define MLOCK_T atomic_t
778
#define INITIAL_LOCK(l)      futex_initialize(l, 1)
779
/* futex_down cannot fail, but can return different
780
 * retvals for OK
781
 */
782
#define ACQUIRE_LOCK(l)      ({futex_down(l);0;})
783
#define RELEASE_LOCK(l)      futex_up(l)
968 palkovsky 784
 
785
#if HAVE_MORECORE
1143 palkovsky 786
static MLOCK_T morecore_mutex = FUTEX_INITIALIZER;
968 palkovsky 787
#endif /* HAVE_MORECORE */
788
 
1143 palkovsky 789
static MLOCK_T magic_init_mutex = FUTEX_INITIALIZER;
968 palkovsky 790
 
791
 
792
#define USE_LOCK_BIT               (2U)
793
#else  /* USE_LOCKS */
794
#define USE_LOCK_BIT               (0U)
795
#define INITIAL_LOCK(l)
796
#endif /* USE_LOCKS */
797
 
798
#if USE_LOCKS && HAVE_MORECORE
799
#define ACQUIRE_MORECORE_LOCK()    ACQUIRE_LOCK(&morecore_mutex);
800
#define RELEASE_MORECORE_LOCK()    RELEASE_LOCK(&morecore_mutex);
801
#else /* USE_LOCKS && HAVE_MORECORE */
802
#define ACQUIRE_MORECORE_LOCK()
803
#define RELEASE_MORECORE_LOCK()
804
#endif /* USE_LOCKS && HAVE_MORECORE */
805
 
806
#if USE_LOCKS
807
#define ACQUIRE_MAGIC_INIT_LOCK()  ACQUIRE_LOCK(&magic_init_mutex);
808
#define RELEASE_MAGIC_INIT_LOCK()  RELEASE_LOCK(&magic_init_mutex);
809
#else  /* USE_LOCKS */
810
#define ACQUIRE_MAGIC_INIT_LOCK()
811
#define RELEASE_MAGIC_INIT_LOCK()
812
#endif /* USE_LOCKS */
813
 
814
 
815
/* -----------------------  Chunk representations ------------------------ */
816
 
817
/*
818
  (The following includes lightly edited explanations by Colin Plumb.)
819
 
820
  The malloc_chunk declaration below is misleading (but accurate and
821
  necessary).  It declares a "view" into memory allowing access to
822
  necessary fields at known offsets from a given base.
823
 
824
  Chunks of memory are maintained using a `boundary tag' method as
825
  originally described by Knuth.  (See the paper by Paul Wilson
826
  ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a survey of such
827
  techniques.)  Sizes of free chunks are stored both in the front of
828
  each chunk and at the end.  This makes consolidating fragmented
829
  chunks into bigger chunks fast.  The head fields also hold bits
830
  representing whether chunks are free or in use.
831
 
832
  Here are some pictures to make it clearer.  They are "exploded" to
833
  show that the state of a chunk can be thought of as extending from
834
  the high 31 bits of the head field of its header through the
835
  prev_foot and PINUSE_BIT bit of the following chunk header.
836
 
837
  A chunk that's in use looks like:
838
 
839
   chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
840
           | Size of previous chunk (if P = 1)                             |
841
           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
842
         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |P|
843
         | Size of this chunk                                         1| +-+
844
   mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
845
         |                                                               |
846
         +-                                                             -+
847
         |                                                               |
848
         +-                                                             -+
849
         |                                                               :
850
         +-      size - sizeof(size_t) available payload bytes          -+
851
         :                                                               |
852
 chunk-> +-                                                             -+
853
         |                                                               |
854
         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
855
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |1|
856
       | Size of next chunk (may or may not be in use)               | +-+
857
 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
858
 
859
    And if it's free, it looks like this:
860
 
861
   chunk-> +-                                                             -+
862
           | User payload (must be in use, or we would have merged!)       |
863
           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
864
         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |P|
865
         | Size of this chunk                                         0| +-+
866
   mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
867
         | Next pointer                                                  |
868
         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
869
         | Prev pointer                                                  |
870
         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
871
         |                                                               :
872
         +-      size - sizeof(struct chunk) unused bytes               -+
873
         :                                                               |
874
 chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
875
         | Size of this chunk                                            |
876
         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
877
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |0|
878
       | Size of next chunk (must be in use, or we would have merged)| +-+
879
 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
880
       |                                                               :
881
       +- User payload                                                -+
882
       :                                                               |
883
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
884
                                                                     |0|
885
                                                                     +-+
886
  Note that since we always merge adjacent free chunks, the chunks
887
  adjacent to a free chunk must be in use.
888
 
889
  Given a pointer to a chunk (which can be derived trivially from the
890
  payload pointer) we can, in O(1) time, find out whether the adjacent
891
  chunks are free, and if so, unlink them from the lists that they
892
  are on and merge them with the current chunk.
893
 
894
  Chunks always begin on even word boundaries, so the mem portion
895
  (which is returned to the user) is also on an even word boundary, and
896
  thus at least double-word aligned.
897
 
898
  The P (PINUSE_BIT) bit, stored in the unused low-order bit of the
899
  chunk size (which is always a multiple of two words), is an in-use
900
  bit for the *previous* chunk.  If that bit is *clear*, then the
901
  word before the current chunk size contains the previous chunk
902
  size, and can be used to find the front of the previous chunk.
903
  The very first chunk allocated always has this bit set, preventing
904
  access to non-existent (or non-owned) memory. If pinuse is set for
905
  any given chunk, then you CANNOT determine the size of the
906
  previous chunk, and might even get a memory addressing fault when
907
  trying to do so.
908
 
909
  The C (CINUSE_BIT) bit, stored in the unused second-lowest bit of
910
  the chunk size redundantly records whether the current chunk is
911
  inuse. This redundancy enables usage checks within free and realloc,
912
  and reduces indirection when freeing and consolidating chunks.
913
 
914
  Each freshly allocated chunk must have both cinuse and pinuse set.
915
  That is, each allocated chunk borders either a previously allocated
916
  and still in-use chunk, or the base of its memory arena. This is
917
  ensured by making all allocations from the the `lowest' part of any
918
  found chunk.  Further, no free chunk physically borders another one,
919
  so each free chunk is known to be preceded and followed by either
920
  inuse chunks or the ends of memory.
921
 
922
  Note that the `foot' of the current chunk is actually represented
923
  as the prev_foot of the NEXT chunk. This makes it easier to
924
  deal with alignments etc but can be very confusing when trying
925
  to extend or adapt this code.
926
 
927
  The exceptions to all this are
928
 
929
     1. The special chunk `top' is the top-most available chunk (i.e.,
930
        the one bordering the end of available memory). It is treated
931
        specially.  Top is never included in any bin, is used only if
932
        no other chunk is available, and is released back to the
933
        system if it is very large (see M_TRIM_THRESHOLD).  In effect,
934
        the top chunk is treated as larger (and thus less well
935
        fitting) than any other available chunk.  The top chunk
936
        doesn't update its trailing size field since there is no next
937
        contiguous chunk that would have to index off it. However,
938
        space is still allocated for it (TOP_FOOT_SIZE) to enable
939
        separation or merging when space is extended.
940
 
941
     3. Chunks allocated via mmap, which have the lowest-order bit
942
        (IS_MMAPPED_BIT) set in their prev_foot fields, and do not set
943
        PINUSE_BIT in their head fields.  Because they are allocated
944
        one-by-one, each must carry its own prev_foot field, which is
945
        also used to hold the offset this chunk has within its mmapped
946
        region, which is needed to preserve alignment. Each mmapped
947
        chunk is trailed by the first two fields of a fake next-chunk
948
        for sake of usage checks.
949
 
950
*/
951
 
952
struct malloc_chunk {
953
  size_t               prev_foot;  /* Size of previous chunk (if free).  */
954
  size_t               head;       /* Size and inuse bits. */
955
  struct malloc_chunk* fd;         /* double links -- used only if free. */
956
  struct malloc_chunk* bk;
957
};
958
 
959
typedef struct malloc_chunk  mchunk;
960
typedef struct malloc_chunk* mchunkptr;
961
typedef struct malloc_chunk* sbinptr;  /* The type of bins of chunks */
962
typedef unsigned int bindex_t;         /* Described below */
963
typedef unsigned int binmap_t;         /* Described below */
964
typedef unsigned int flag_t;           /* The type of various bit flag sets */
965
 
966
/* ------------------- Chunks sizes and alignments ----------------------- */
967
 
968
#define MCHUNK_SIZE         (sizeof(mchunk))
969
 
970
#if FOOTERS
971
#define CHUNK_OVERHEAD      (TWO_SIZE_T_SIZES)
972
#else /* FOOTERS */
973
#define CHUNK_OVERHEAD      (SIZE_T_SIZE)
974
#endif /* FOOTERS */
975
 
976
/* MMapped chunks need a second word of overhead ... */
977
#define MMAP_CHUNK_OVERHEAD (TWO_SIZE_T_SIZES)
978
/* ... and additional padding for fake next-chunk at foot */
979
#define MMAP_FOOT_PAD       (FOUR_SIZE_T_SIZES)
980
 
981
/* The smallest size we can malloc is an aligned minimal chunk */
982
#define MIN_CHUNK_SIZE\
983
  ((MCHUNK_SIZE + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK)
984
 
985
/* conversion from malloc headers to user pointers, and back */
986
#define chunk2mem(p)        ((void*)((char*)(p)       + TWO_SIZE_T_SIZES))
987
#define mem2chunk(mem)      ((mchunkptr)((char*)(mem) - TWO_SIZE_T_SIZES))
988
/* chunk associated with aligned address A */
989
#define align_as_chunk(A)   (mchunkptr)((A) + align_offset(chunk2mem(A)))
990
 
991
/* Bounds on request (not chunk) sizes. */
992
#define MAX_REQUEST         ((-MIN_CHUNK_SIZE) << 2)
993
#define MIN_REQUEST         (MIN_CHUNK_SIZE - CHUNK_OVERHEAD - SIZE_T_ONE)
994
 
995
/* pad request bytes into a usable size */
996
#define pad_request(req) \
997
   (((req) + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK)
998
 
999
/* pad request, checking for minimum (but not maximum) */
1000
#define request2size(req) \
1001
  (((req) < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(req))
1002
 
1003
 
1004
/* ------------------ Operations on head and foot fields ----------------- */
1005
 
1006
/*
1007
  The head field of a chunk is or'ed with PINUSE_BIT when previous
1008
  adjacent chunk in use, and or'ed with CINUSE_BIT if this chunk is in
1009
  use. If the chunk was obtained with mmap, the prev_foot field has
1010
  IS_MMAPPED_BIT set, otherwise holding the offset of the base of the
1011
  mmapped region to the base of the chunk.
1012
*/
1013
 
1014
#define PINUSE_BIT          (SIZE_T_ONE)
1015
#define CINUSE_BIT          (SIZE_T_TWO)
1016
#define INUSE_BITS          (PINUSE_BIT|CINUSE_BIT)
1017
 
1018
/* Head value for fenceposts */
1019
#define FENCEPOST_HEAD      (INUSE_BITS|SIZE_T_SIZE)
1020
 
1021
/* extraction of fields from head words */
1022
#define cinuse(p)           ((p)->head & CINUSE_BIT)
1023
#define pinuse(p)           ((p)->head & PINUSE_BIT)
1024
#define chunksize(p)        ((p)->head & ~(INUSE_BITS))
1025
 
1026
#define clear_pinuse(p)     ((p)->head &= ~PINUSE_BIT)
1027
#define clear_cinuse(p)     ((p)->head &= ~CINUSE_BIT)
1028
 
1029
/* Treat space at ptr +/- offset as a chunk */
1030
#define chunk_plus_offset(p, s)  ((mchunkptr)(((char*)(p)) + (s)))
1031
#define chunk_minus_offset(p, s) ((mchunkptr)(((char*)(p)) - (s)))
1032
 
1033
/* Ptr to next or previous physical malloc_chunk. */
1034
#define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->head & ~INUSE_BITS)))
1035
#define prev_chunk(p) ((mchunkptr)( ((char*)(p)) - ((p)->prev_foot) ))
1036
 
1037
/* extract next chunk's pinuse bit */
1038
#define next_pinuse(p)  ((next_chunk(p)->head) & PINUSE_BIT)
1039
 
1040
/* Get/set size at footer */
1041
#define get_foot(p, s)  (((mchunkptr)((char*)(p) + (s)))->prev_foot)
1042
#define set_foot(p, s)  (((mchunkptr)((char*)(p) + (s)))->prev_foot = (s))
1043
 
1044
/* Set size, pinuse bit, and foot */
1045
#define set_size_and_pinuse_of_free_chunk(p, s)\
1046
  ((p)->head = (s|PINUSE_BIT), set_foot(p, s))
1047
 
1048
/* Set size, pinuse bit, foot, and clear next pinuse */
1049
#define set_free_with_pinuse(p, s, n)\
1050
  (clear_pinuse(n), set_size_and_pinuse_of_free_chunk(p, s))
1051
 
1052
#define is_mmapped(p)\
1053
  (!((p)->head & PINUSE_BIT) && ((p)->prev_foot & IS_MMAPPED_BIT))
1054
 
1055
/* Get the internal overhead associated with chunk p */
1056
#define overhead_for(p)\
1057
 (is_mmapped(p)? MMAP_CHUNK_OVERHEAD : CHUNK_OVERHEAD)
1058
 
1059
/* Return true if malloced space is not necessarily cleared */
1060
#if MMAP_CLEARS
1061
#define calloc_must_clear(p) (!is_mmapped(p))
1062
#else /* MMAP_CLEARS */
1063
#define calloc_must_clear(p) (1)
1064
#endif /* MMAP_CLEARS */
1065
 
1066
/* ---------------------- Overlaid data structures ----------------------- */
1067
 
1068
/*
1069
  When chunks are not in use, they are treated as nodes of either
1070
  lists or trees.
1071
 
1072
  "Small"  chunks are stored in circular doubly-linked lists, and look
1073
  like this:
1074
 
1075
    chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1076
            |             Size of previous chunk                            |
1077
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1078
    `head:' |             Size of chunk, in bytes                         |P|
1079
      mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1080
            |             Forward pointer to next chunk in list             |
1081
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1082
            |             Back pointer to previous chunk in list            |
1083
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1084
            |             Unused space (may be 0 bytes long)                .
1085
            .                                                               .
1086
            .                                                               |
1087
nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1088
    `foot:' |             Size of chunk, in bytes                           |
1089
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1090
 
1091
  Larger chunks are kept in a form of bitwise digital trees (aka
1092
  tries) keyed on chunksizes.  Because malloc_tree_chunks are only for
1093
  free chunks greater than 256 bytes, their size doesn't impose any
1094
  constraints on user chunk sizes.  Each node looks like:
1095
 
1096
    chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1097
            |             Size of previous chunk                            |
1098
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1099
    `head:' |             Size of chunk, in bytes                         |P|
1100
      mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1101
            |             Forward pointer to next chunk of same size        |
1102
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1103
            |             Back pointer to previous chunk of same size       |
1104
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1105
            |             Pointer to left child (child[0])                  |
1106
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1107
            |             Pointer to right child (child[1])                 |
1108
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1109
            |             Pointer to parent                                 |
1110
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1111
            |             bin index of this chunk                           |
1112
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1113
            |             Unused space                                      .
1114
            .                                                               |
1115
nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1116
    `foot:' |             Size of chunk, in bytes                           |
1117
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1118
 
1119
  Each tree holding treenodes is a tree of unique chunk sizes.  Chunks
1120
  of the same size are arranged in a circularly-linked list, with only
1121
  the oldest chunk (the next to be used, in our FIFO ordering)
1122
  actually in the tree.  (Tree members are distinguished by a non-null
1123
  parent pointer.)  If a chunk with the same size an an existing node
1124
  is inserted, it is linked off the existing node using pointers that
1125
  work in the same way as fd/bk pointers of small chunks.
1126
 
1127
  Each tree contains a power of 2 sized range of chunk sizes (the
1128
  smallest is 0x100 <= x < 0x180), which is is divided in half at each
1129
  tree level, with the chunks in the smaller half of the range (0x100
1130
  <= x < 0x140 for the top nose) in the left subtree and the larger
1131
  half (0x140 <= x < 0x180) in the right subtree.  This is, of course,
1132
  done by inspecting individual bits.
1133
 
1134
  Using these rules, each node's left subtree contains all smaller
1135
  sizes than its right subtree.  However, the node at the root of each
1136
  subtree has no particular ordering relationship to either.  (The
1137
  dividing line between the subtree sizes is based on trie relation.)
1138
  If we remove the last chunk of a given size from the interior of the
1139
  tree, we need to replace it with a leaf node.  The tree ordering
1140
  rules permit a node to be replaced by any leaf below it.
1141
 
1142
  The smallest chunk in a tree (a common operation in a best-fit
1143
  allocator) can be found by walking a path to the leftmost leaf in
1144
  the tree.  Unlike a usual binary tree, where we follow left child
1145
  pointers until we reach a null, here we follow the right child
1146
  pointer any time the left one is null, until we reach a leaf with
1147
  both child pointers null. The smallest chunk in the tree will be
1148
  somewhere along that path.
1149
 
1150
  The worst case number of steps to add, find, or remove a node is
1151
  bounded by the number of bits differentiating chunks within
1152
  bins. Under current bin calculations, this ranges from 6 up to 21
1153
  (for 32 bit sizes) or up to 53 (for 64 bit sizes). The typical case
1154
  is of course much better.
1155
*/
1156
 
1157
struct malloc_tree_chunk {
1158
  /* The first four fields must be compatible with malloc_chunk */
1159
  size_t                    prev_foot;
1160
  size_t                    head;
1161
  struct malloc_tree_chunk* fd;
1162
  struct malloc_tree_chunk* bk;
1163
 
1164
  struct malloc_tree_chunk* child[2];
1165
  struct malloc_tree_chunk* parent;
1166
  bindex_t                  index;
1167
};
1168
 
1169
typedef struct malloc_tree_chunk  tchunk;
1170
typedef struct malloc_tree_chunk* tchunkptr;
1171
typedef struct malloc_tree_chunk* tbinptr; /* The type of bins of trees */
1172
 
1173
/* A little helper macro for trees */
1174
#define leftmost_child(t) ((t)->child[0] != 0? (t)->child[0] : (t)->child[1])
1175
 
1176
/* ----------------------------- Segments -------------------------------- */
1177
 
1178
/*
1179
  Each malloc space may include non-contiguous segments, held in a
1180
  list headed by an embedded malloc_segment record representing the
1181
  top-most space. Segments also include flags holding properties of
1182
  the space. Large chunks that are directly allocated by mmap are not
1183
  included in this list. They are instead independently created and
1184
  destroyed without otherwise keeping track of them.
1185
 
1186
  Segment management mainly comes into play for spaces allocated by
1187
  MMAP.  Any call to MMAP might or might not return memory that is
1188
  adjacent to an existing segment.  MORECORE normally contiguously
1189
  extends the current space, so this space is almost always adjacent,
1190
  which is simpler and faster to deal with. (This is why MORECORE is
1191
  used preferentially to MMAP when both are available -- see
1192
  sys_alloc.)  When allocating using MMAP, we don't use any of the
1193
  hinting mechanisms (inconsistently) supported in various
1194
  implementations of unix mmap, or distinguish reserving from
1195
  committing memory. Instead, we just ask for space, and exploit
1196
  contiguity when we get it.  It is probably possible to do
1197
  better than this on some systems, but no general scheme seems
1198
  to be significantly better.
1199
 
1200
  Management entails a simpler variant of the consolidation scheme
1201
  used for chunks to reduce fragmentation -- new adjacent memory is
1202
  normally prepended or appended to an existing segment. However,
1203
  there are limitations compared to chunk consolidation that mostly
1204
  reflect the fact that segment processing is relatively infrequent
1205
  (occurring only when getting memory from system) and that we
1206
  don't expect to have huge numbers of segments:
1207
 
1208
  * Segments are not indexed, so traversal requires linear scans.  (It
1209
    would be possible to index these, but is not worth the extra
1210
    overhead and complexity for most programs on most platforms.)
1211
  * New segments are only appended to old ones when holding top-most
1212
    memory; if they cannot be prepended to others, they are held in
1213
    different segments.
1214
 
1215
  Except for the top-most segment of an mstate, each segment record
1216
  is kept at the tail of its segment. Segments are added by pushing
1217
  segment records onto the list headed by &mstate.seg for the
1218
  containing mstate.
1219
 
1220
  Segment flags control allocation/merge/deallocation policies:
1221
  * If EXTERN_BIT set, then we did not allocate this segment,
1222
    and so should not try to deallocate or merge with others.
1223
    (This currently holds only for the initial segment passed
1224
    into create_mspace_with_base.)
1225
  * If IS_MMAPPED_BIT set, the segment may be merged with
1226
    other surrounding mmapped segments and trimmed/de-allocated
1227
    using munmap.
1228
  * If neither bit is set, then the segment was obtained using
1229
    MORECORE so can be merged with surrounding MORECORE'd segments
1230
    and deallocated/trimmed using MORECORE with negative arguments.
1231
*/
1232
 
1233
struct malloc_segment {
1234
  char*        base;             /* base address */
1235
  size_t       size;             /* allocated size */
1236
  struct malloc_segment* next;   /* ptr to next segment */
1237
  flag_t       sflags;           /* mmap and extern flag */
1238
};
1239
 
1240
#define is_mmapped_segment(S)  ((S)->sflags & IS_MMAPPED_BIT)
1241
#define is_extern_segment(S)   ((S)->sflags & EXTERN_BIT)
1242
 
1243
typedef struct malloc_segment  msegment;
1244
typedef struct malloc_segment* msegmentptr;
1245
 
1246
/* ---------------------------- malloc_state ----------------------------- */
1247
 
1248
/*
1249
   A malloc_state holds all of the bookkeeping for a space.
1250
   The main fields are:
1251
 
1252
  Top
1253
    The topmost chunk of the currently active segment. Its size is
1254
    cached in topsize.  The actual size of topmost space is
1255
    topsize+TOP_FOOT_SIZE, which includes space reserved for adding
1256
    fenceposts and segment records if necessary when getting more
1257
    space from the system.  The size at which to autotrim top is
1258
    cached from mparams in trim_check, except that it is disabled if
1259
    an autotrim fails.
1260
 
1261
  Designated victim (dv)
1262
    This is the preferred chunk for servicing small requests that
1263
    don't have exact fits.  It is normally the chunk split off most
1264
    recently to service another small request.  Its size is cached in
1265
    dvsize. The link fields of this chunk are not maintained since it
1266
    is not kept in a bin.
1267
 
1268
  SmallBins
1269
    An array of bin headers for free chunks.  These bins hold chunks
1270
    with sizes less than MIN_LARGE_SIZE bytes. Each bin contains
1271
    chunks of all the same size, spaced 8 bytes apart.  To simplify
1272
    use in double-linked lists, each bin header acts as a malloc_chunk
1273
    pointing to the real first node, if it exists (else pointing to
1274
    itself).  This avoids special-casing for headers.  But to avoid
1275
    waste, we allocate only the fd/bk pointers of bins, and then use
1276
    repositioning tricks to treat these as the fields of a chunk.
1277
 
1278
  TreeBins
1279
    Treebins are pointers to the roots of trees holding a range of
1280
    sizes. There are 2 equally spaced treebins for each power of two
1281
    from TREE_SHIFT to TREE_SHIFT+16. The last bin holds anything
1282
    larger.
1283
 
1284
  Bin maps
1285
    There is one bit map for small bins ("smallmap") and one for
1286
    treebins ("treemap).  Each bin sets its bit when non-empty, and
1287
    clears the bit when empty.  Bit operations are then used to avoid
1288
    bin-by-bin searching -- nearly all "search" is done without ever
1289
    looking at bins that won't be selected.  The bit maps
1290
    conservatively use 32 bits per map word, even if on 64bit system.
1291
    For a good description of some of the bit-based techniques used
1292
    here, see Henry S. Warren Jr's book "Hacker's Delight" (and
1293
    supplement at http://hackersdelight.org/). Many of these are
1294
    intended to reduce the branchiness of paths through malloc etc, as
1295
    well as to reduce the number of memory locations read or written.
1296
 
1297
  Segments
1298
    A list of segments headed by an embedded malloc_segment record
1299
    representing the initial space.
1300
 
1301
  Address check support
1302
    The least_addr field is the least address ever obtained from
1303
    MORECORE or MMAP. Attempted frees and reallocs of any address less
1304
    than this are trapped (unless INSECURE is defined).
1305
 
1306
  Magic tag
1307
    A cross-check field that should always hold same value as mparams.magic.
1308
 
1309
  Flags
1310
    Bits recording whether to use MMAP, locks, or contiguous MORECORE
1311
 
1312
  Statistics
1313
    Each space keeps track of current and maximum system memory
1314
    obtained via MORECORE or MMAP.
1315
 
1316
  Locking
1317
    If USE_LOCKS is defined, the "mutex" lock is acquired and released
1318
    around every public call using this mspace.
1319
*/
1320
 
1321
/* Bin types, widths and sizes */
1322
#define NSMALLBINS        (32U)
1323
#define NTREEBINS         (32U)
1324
#define SMALLBIN_SHIFT    (3U)
1325
#define SMALLBIN_WIDTH    (SIZE_T_ONE << SMALLBIN_SHIFT)
1326
#define TREEBIN_SHIFT     (8U)
1327
#define MIN_LARGE_SIZE    (SIZE_T_ONE << TREEBIN_SHIFT)
1328
#define MAX_SMALL_SIZE    (MIN_LARGE_SIZE - SIZE_T_ONE)
1329
#define MAX_SMALL_REQUEST (MAX_SMALL_SIZE - CHUNK_ALIGN_MASK - CHUNK_OVERHEAD)
1330
 
1331
struct malloc_state {
1332
  binmap_t   smallmap;
1333
  binmap_t   treemap;
1334
  size_t     dvsize;
1335
  size_t     topsize;
1336
  char*      least_addr;
1337
  mchunkptr  dv;
1338
  mchunkptr  top;
1339
  size_t     trim_check;
1340
  size_t     magic;
1341
  mchunkptr  smallbins[(NSMALLBINS+1)*2];
1342
  tbinptr    treebins[NTREEBINS];
1343
  size_t     footprint;
1344
  size_t     max_footprint;
1345
  flag_t     mflags;
1346
#if USE_LOCKS
1347
  MLOCK_T    mutex;     /* locate lock among fields that rarely change */
1348
#endif /* USE_LOCKS */
1349
  msegment   seg;
1350
};
1351
 
1352
typedef struct malloc_state*    mstate;
1353
 
1354
/* ------------- Global malloc_state and malloc_params ------------------- */
1355
 
1356
/*
1357
  malloc_params holds global properties, including those that can be
1358
  dynamically set using mallopt. There is a single instance, mparams,
1359
  initialized in init_mparams.
1360
*/
1361
 
1362
struct malloc_params {
1363
  size_t magic;
1364
  size_t page_size;
1365
  size_t granularity;
1366
  size_t mmap_threshold;
1367
  size_t trim_threshold;
1368
  flag_t default_mflags;
1369
};
1370
 
1371
static struct malloc_params mparams;
1372
 
1373
/* The global malloc_state used for all non-"mspace" calls */
1374
static struct malloc_state _gm_;
1375
#define gm                 (&_gm_)
1376
#define is_global(M)       ((M) == &_gm_)
1377
#define is_initialized(M)  ((M)->top != 0)
1378
 
1379
/* -------------------------- system alloc setup ------------------------- */
1380
 
1381
/* Operations on mflags */
1382
 
1383
#define use_lock(M)           ((M)->mflags &   USE_LOCK_BIT)
1384
#define enable_lock(M)        ((M)->mflags |=  USE_LOCK_BIT)
1385
#define disable_lock(M)       ((M)->mflags &= ~USE_LOCK_BIT)
1386
 
1387
#define use_mmap(M)           ((M)->mflags &   USE_MMAP_BIT)
1388
#define enable_mmap(M)        ((M)->mflags |=  USE_MMAP_BIT)
1389
#define disable_mmap(M)       ((M)->mflags &= ~USE_MMAP_BIT)
1390
 
1391
#define use_noncontiguous(M)  ((M)->mflags &   USE_NONCONTIGUOUS_BIT)
1392
#define disable_contiguous(M) ((M)->mflags |=  USE_NONCONTIGUOUS_BIT)
1393
 
1394
#define set_lock(M,L)\
1395
 ((M)->mflags = (L)?\
1396
  ((M)->mflags | USE_LOCK_BIT) :\
1397
  ((M)->mflags & ~USE_LOCK_BIT))
1398
 
1399
/* page-align a size */
1400
#define page_align(S)\
1401
 (((S) + (mparams.page_size)) & ~(mparams.page_size - SIZE_T_ONE))
1402
 
1403
/* granularity-align a size */
1404
#define granularity_align(S)\
1405
  (((S) + (mparams.granularity)) & ~(mparams.granularity - SIZE_T_ONE))
1406
 
1407
#define is_page_aligned(S)\
1408
   (((size_t)(S) & (mparams.page_size - SIZE_T_ONE)) == 0)
1409
#define is_granularity_aligned(S)\
1410
   (((size_t)(S) & (mparams.granularity - SIZE_T_ONE)) == 0)
1411
 
1412
/*  True if segment S holds address A */
1413
#define segment_holds(S, A)\
1414
  ((char*)(A) >= S->base && (char*)(A) < S->base + S->size)
1415
 
1416
/* Return segment holding given address */
1417
static msegmentptr segment_holding(mstate m, char* addr) {
1418
  msegmentptr sp = &m->seg;
1419
  for (;;) {
1420
    if (addr >= sp->base && addr < sp->base + sp->size)
1421
      return sp;
1422
    if ((sp = sp->next) == 0)
1423
      return 0;
1424
  }
1425
}
1426
 
1427
/* Return true if segment contains a segment link */
1428
static int has_segment_link(mstate m, msegmentptr ss) {
1429
  msegmentptr sp = &m->seg;
1430
  for (;;) {
1431
    if ((char*)sp >= ss->base && (char*)sp < ss->base + ss->size)
1432
      return 1;
1433
    if ((sp = sp->next) == 0)
1434
      return 0;
1435
  }
1436
}
1437
 
1438
#ifndef MORECORE_CANNOT_TRIM
1439
#define should_trim(M,s)  ((s) > (M)->trim_check)
1440
#else  /* MORECORE_CANNOT_TRIM */
1441
#define should_trim(M,s)  (0)
1442
#endif /* MORECORE_CANNOT_TRIM */
1443
 
1444
/*
1445
  TOP_FOOT_SIZE is padding at the end of a segment, including space
1446
  that may be needed to place segment records and fenceposts when new
1447
  noncontiguous segments are added.
1448
*/
1449
#define TOP_FOOT_SIZE\
1450
  (align_offset(chunk2mem(0))+pad_request(sizeof(struct malloc_segment))+MIN_CHUNK_SIZE)
1451
 
1452
 
1453
/* -------------------------------  Hooks -------------------------------- */
1454
 
1455
/*
1456
  PREACTION should be defined to return 0 on success, and nonzero on
1457
  failure. If you are not using locking, you can redefine these to do
1458
  anything you like.
1459
*/
1460
 
1461
#if USE_LOCKS
1462
 
1463
/* Ensure locks are initialized */
1464
#define GLOBALLY_INITIALIZE() (mparams.page_size == 0 && init_mparams())
1465
 
1466
#define PREACTION(M)  ((GLOBALLY_INITIALIZE() || use_lock(M))? ACQUIRE_LOCK(&(M)->mutex) : 0)
1467
#define POSTACTION(M) { if (use_lock(M)) RELEASE_LOCK(&(M)->mutex); }
1468
#else /* USE_LOCKS */
1469
 
1470
#ifndef PREACTION
1471
#define PREACTION(M) (0)
1472
#endif  /* PREACTION */
1473
 
1474
#ifndef POSTACTION
1475
#define POSTACTION(M)
1476
#endif  /* POSTACTION */
1477
 
1478
#endif /* USE_LOCKS */
1479
 
1480
/*
1481
  CORRUPTION_ERROR_ACTION is triggered upon detected bad addresses.
1482
  USAGE_ERROR_ACTION is triggered on detected bad frees and
1483
  reallocs. The argument p is an address that might have triggered the
1484
  fault. It is ignored by the two predefined actions, but might be
1485
  useful in custom actions that try to help diagnose errors.
1486
*/
1487
 
1488
#if PROCEED_ON_ERROR
1489
 
1490
/* A count of the number of corruption errors causing resets */
1491
int malloc_corruption_error_count;
1492
 
1493
/* default corruption action */
1494
static void reset_on_error(mstate m);
1495
 
1496
#define CORRUPTION_ERROR_ACTION(m)  reset_on_error(m)
1497
#define USAGE_ERROR_ACTION(m, p)
1498
 
1499
#else /* PROCEED_ON_ERROR */
1500
 
1501
#ifndef CORRUPTION_ERROR_ACTION
1502
#define CORRUPTION_ERROR_ACTION(m) ABORT
1503
#endif /* CORRUPTION_ERROR_ACTION */
1504
 
1505
#ifndef USAGE_ERROR_ACTION
1506
#define USAGE_ERROR_ACTION(m,p) ABORT
1507
#endif /* USAGE_ERROR_ACTION */
1508
 
1509
#endif /* PROCEED_ON_ERROR */
1510
 
1511
/* -------------------------- Debugging setup ---------------------------- */
1512
 
1513
#if ! DEBUG
1514
 
1515
#define check_free_chunk(M,P)
1516
#define check_inuse_chunk(M,P)
1517
#define check_malloced_chunk(M,P,N)
1518
#define check_mmapped_chunk(M,P)
1519
#define check_malloc_state(M)
1520
#define check_top_chunk(M,P)
1521
 
1522
#else /* DEBUG */
1523
#define check_free_chunk(M,P)       do_check_free_chunk(M,P)
1524
#define check_inuse_chunk(M,P)      do_check_inuse_chunk(M,P)
1525
#define check_top_chunk(M,P)        do_check_top_chunk(M,P)
1526
#define check_malloced_chunk(M,P,N) do_check_malloced_chunk(M,P,N)
1527
#define check_mmapped_chunk(M,P)    do_check_mmapped_chunk(M,P)
1528
#define check_malloc_state(M)       do_check_malloc_state(M)
1529
 
1530
static void   do_check_any_chunk(mstate m, mchunkptr p);
1531
static void   do_check_top_chunk(mstate m, mchunkptr p);
1532
static void   do_check_mmapped_chunk(mstate m, mchunkptr p);
1533
static void   do_check_inuse_chunk(mstate m, mchunkptr p);
1534
static void   do_check_free_chunk(mstate m, mchunkptr p);
1535
static void   do_check_malloced_chunk(mstate m, void* mem, size_t s);
1536
static void   do_check_tree(mstate m, tchunkptr t);
1537
static void   do_check_treebin(mstate m, bindex_t i);
1538
static void   do_check_smallbin(mstate m, bindex_t i);
1539
static void   do_check_malloc_state(mstate m);
1540
static int    bin_find(mstate m, mchunkptr x);
1541
static size_t traverse_and_check(mstate m);
1542
#endif /* DEBUG */
1543
 
1544
/* ---------------------------- Indexing Bins ---------------------------- */
1545
 
1546
#define is_small(s)         (((s) >> SMALLBIN_SHIFT) < NSMALLBINS)
1547
#define small_index(s)      ((s)  >> SMALLBIN_SHIFT)
1548
#define small_index2size(i) ((i)  << SMALLBIN_SHIFT)
1549
#define MIN_SMALL_INDEX     (small_index(MIN_CHUNK_SIZE))
1550
 
1551
/* addressing by index. See above about smallbin repositioning */
1552
#define smallbin_at(M, i)   ((sbinptr)((char*)&((M)->smallbins[(i)<<1])))
1553
#define treebin_at(M,i)     (&((M)->treebins[i]))
1554
 
1555
/* assign tree index for size S to variable I */
1556
#if defined(__GNUC__) && defined(i386)
1557
#define compute_tree_index(S, I)\
1558
{\
1559
  size_t X = S >> TREEBIN_SHIFT;\
1560
  if (X == 0)\
1561
    I = 0;\
1562
  else if (X > 0xFFFF)\
1563
    I = NTREEBINS-1;\
1564
  else {\
1565
    unsigned int K;\
1566
    __asm__("bsrl %1,%0\n\t" : "=r" (K) : "rm"  (X));\
1567
    I =  (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\
1568
  }\
1569
}
1570
#else /* GNUC */
1571
#define compute_tree_index(S, I)\
1572
{\
1573
  size_t X = S >> TREEBIN_SHIFT;\
1574
  if (X == 0)\
1575
    I = 0;\
1576
  else if (X > 0xFFFF)\
1577
    I = NTREEBINS-1;\
1578
  else {\
1579
    unsigned int Y = (unsigned int)X;\
1580
    unsigned int N = ((Y - 0x100) >> 16) & 8;\
1581
    unsigned int K = (((Y <<= N) - 0x1000) >> 16) & 4;\
1582
    N += K;\
1583
    N += K = (((Y <<= K) - 0x4000) >> 16) & 2;\
1584
    K = 14 - N + ((Y <<= K) >> 15);\
1585
    I = (K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1));\
1586
  }\
1587
}
1588
#endif /* GNUC */
1589
 
1590
/* Bit representing maximum resolved size in a treebin at i */
1591
#define bit_for_tree_index(i) \
1592
   (i == NTREEBINS-1)? (SIZE_T_BITSIZE-1) : (((i) >> 1) + TREEBIN_SHIFT - 2)
1593
 
1594
/* Shift placing maximum resolved bit in a treebin at i as sign bit */
1595
#define leftshift_for_tree_index(i) \
1596
   ((i == NTREEBINS-1)? 0 : \
1597
    ((SIZE_T_BITSIZE-SIZE_T_ONE) - (((i) >> 1) + TREEBIN_SHIFT - 2)))
1598
 
1599
/* The size of the smallest chunk held in bin with index i */
1600
#define minsize_for_tree_index(i) \
1601
   ((SIZE_T_ONE << (((i) >> 1) + TREEBIN_SHIFT)) |  \
1602
   (((size_t)((i) & SIZE_T_ONE)) << (((i) >> 1) + TREEBIN_SHIFT - 1)))
1603
 
1604
 
1605
/* ------------------------ Operations on bin maps ----------------------- */
1606
 
1607
/* bit corresponding to given index */
1608
#define idx2bit(i)              ((binmap_t)(1) << (i))
1609
 
1610
/* Mark/Clear bits with given index */
1611
#define mark_smallmap(M,i)      ((M)->smallmap |=  idx2bit(i))
1612
#define clear_smallmap(M,i)     ((M)->smallmap &= ~idx2bit(i))
1613
#define smallmap_is_marked(M,i) ((M)->smallmap &   idx2bit(i))
1614
 
1615
#define mark_treemap(M,i)       ((M)->treemap  |=  idx2bit(i))
1616
#define clear_treemap(M,i)      ((M)->treemap  &= ~idx2bit(i))
1617
#define treemap_is_marked(M,i)  ((M)->treemap  &   idx2bit(i))
1618
 
1619
/* index corresponding to given bit */
1620
 
1621
#if defined(__GNUC__) && defined(i386)
1622
#define compute_bit2idx(X, I)\
1623
{\
1624
  unsigned int J;\
1625
  __asm__("bsfl %1,%0\n\t" : "=r" (J) : "rm" (X));\
1626
  I = (bindex_t)J;\
1627
}
1628
 
1629
#else /* GNUC */
1630
#if  USE_BUILTIN_FFS
1631
#define compute_bit2idx(X, I) I = ffs(X)-1
1632
 
1633
#else /* USE_BUILTIN_FFS */
1634
#define compute_bit2idx(X, I)\
1635
{\
1636
  unsigned int Y = X - 1;\
1637
  unsigned int K = Y >> (16-4) & 16;\
1638
  unsigned int N = K;        Y >>= K;\
1639
  N += K = Y >> (8-3) &  8;  Y >>= K;\
1640
  N += K = Y >> (4-2) &  4;  Y >>= K;\
1641
  N += K = Y >> (2-1) &  2;  Y >>= K;\
1642
  N += K = Y >> (1-0) &  1;  Y >>= K;\
1643
  I = (bindex_t)(N + Y);\
1644
}
1645
#endif /* USE_BUILTIN_FFS */
1646
#endif /* GNUC */
1647
 
1648
/* isolate the least set bit of a bitmap */
1649
#define least_bit(x)         ((x) & -(x))
1650
 
1651
/* mask with all bits to left of least bit of x on */
1652
#define left_bits(x)         ((x<<1) | -(x<<1))
1653
 
1654
/* mask with all bits to left of or equal to least bit of x on */
1655
#define same_or_left_bits(x) ((x) | -(x))
1656
 
1657
 
1658
/* ----------------------- Runtime Check Support ------------------------- */
1659
 
1660
/*
1661
  For security, the main invariant is that malloc/free/etc never
1662
  writes to a static address other than malloc_state, unless static
1663
  malloc_state itself has been corrupted, which cannot occur via
1664
  malloc (because of these checks). In essence this means that we
1665
  believe all pointers, sizes, maps etc held in malloc_state, but
1666
  check all of those linked or offsetted from other embedded data
1667
  structures.  These checks are interspersed with main code in a way
1668
  that tends to minimize their run-time cost.
1669
 
1670
  When FOOTERS is defined, in addition to range checking, we also
1671
  verify footer fields of inuse chunks, which can be used guarantee
1672
  that the mstate controlling malloc/free is intact.  This is a
1673
  streamlined version of the approach described by William Robertson
1674
  et al in "Run-time Detection of Heap-based Overflows" LISA'03
1675
  http://www.usenix.org/events/lisa03/tech/robertson.html The footer
1676
  of an inuse chunk holds the xor of its mstate and a random seed,
1677
  that is checked upon calls to free() and realloc().  This is
1678
  (probablistically) unguessable from outside the program, but can be
1679
  computed by any code successfully malloc'ing any chunk, so does not
1680
  itself provide protection against code that has already broken
1681
  security through some other means.  Unlike Robertson et al, we
1682
  always dynamically check addresses of all offset chunks (previous,
1683
  next, etc). This turns out to be cheaper than relying on hashes.
1684
*/
1685
 
1686
#if !INSECURE
1687
/* Check if address a is at least as high as any from MORECORE or MMAP */
1688
#define ok_address(M, a) ((char*)(a) >= (M)->least_addr)
1689
/* Check if address of next chunk n is higher than base chunk p */
1690
#define ok_next(p, n)    ((char*)(p) < (char*)(n))
1691
/* Check if p has its cinuse bit on */
1692
#define ok_cinuse(p)     cinuse(p)
1693
/* Check if p has its pinuse bit on */
1694
#define ok_pinuse(p)     pinuse(p)
1695
 
1696
#else /* !INSECURE */
1697
#define ok_address(M, a) (1)
1698
#define ok_next(b, n)    (1)
1699
#define ok_cinuse(p)     (1)
1700
#define ok_pinuse(p)     (1)
1701
#endif /* !INSECURE */
1702
 
1703
#if (FOOTERS && !INSECURE)
1704
/* Check if (alleged) mstate m has expected magic field */
1705
#define ok_magic(M)      ((M)->magic == mparams.magic)
1706
#else  /* (FOOTERS && !INSECURE) */
1707
#define ok_magic(M)      (1)
1708
#endif /* (FOOTERS && !INSECURE) */
1709
 
1710
 
1711
/* In gcc, use __builtin_expect to minimize impact of checks */
1712
#if !INSECURE
1713
#if defined(__GNUC__) && __GNUC__ >= 3
1714
#define RTCHECK(e)  __builtin_expect(e, 1)
1715
#else /* GNUC */
1716
#define RTCHECK(e)  (e)
1717
#endif /* GNUC */
1718
#else /* !INSECURE */
1719
#define RTCHECK(e)  (1)
1720
#endif /* !INSECURE */
1721
 
1722
/* macros to set up inuse chunks with or without footers */
1723
 
1724
#if !FOOTERS
1725
 
1726
#define mark_inuse_foot(M,p,s)
1727
 
1728
/* Set cinuse bit and pinuse bit of next chunk */
1729
#define set_inuse(M,p,s)\
1730
  ((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\
1731
  ((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT)
1732
 
1733
/* Set cinuse and pinuse of this chunk and pinuse of next chunk */
1734
#define set_inuse_and_pinuse(M,p,s)\
1735
  ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\
1736
  ((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT)
1737
 
1738
/* Set size, cinuse and pinuse bit of this chunk */
1739
#define set_size_and_pinuse_of_inuse_chunk(M, p, s)\
1740
  ((p)->head = (s|PINUSE_BIT|CINUSE_BIT))
1741
 
1742
#else /* FOOTERS */
1743
 
1744
/* Set foot of inuse chunk to be xor of mstate and seed */
1745
#define mark_inuse_foot(M,p,s)\
1746
  (((mchunkptr)((char*)(p) + (s)))->prev_foot = ((size_t)(M) ^ mparams.magic))
1747
 
1748
#define get_mstate_for(p)\
1749
  ((mstate)(((mchunkptr)((char*)(p) +\
1750
    (chunksize(p))))->prev_foot ^ mparams.magic))
1751
 
1752
#define set_inuse(M,p,s)\
1753
  ((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\
1754
  (((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT), \
1755
  mark_inuse_foot(M,p,s))
1756
 
1757
#define set_inuse_and_pinuse(M,p,s)\
1758
  ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\
1759
  (((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT),\
1760
 mark_inuse_foot(M,p,s))
1761
 
1762
#define set_size_and_pinuse_of_inuse_chunk(M, p, s)\
1763
  ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\
1764
  mark_inuse_foot(M, p, s))
1765
 
1766
#endif /* !FOOTERS */
1767
 
1768
/* ---------------------------- setting mparams -------------------------- */
1769
 
1770
/* Initialize mparams */
1771
static int init_mparams(void) {
1772
  if (mparams.page_size == 0) {
1773
    size_t s;
1774
 
1775
    mparams.mmap_threshold = DEFAULT_MMAP_THRESHOLD;
1776
    mparams.trim_threshold = DEFAULT_TRIM_THRESHOLD;
1777
#if MORECORE_CONTIGUOUS
1778
    mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT;
1779
#else  /* MORECORE_CONTIGUOUS */
1780
    mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT|USE_NONCONTIGUOUS_BIT;
1781
#endif /* MORECORE_CONTIGUOUS */
1782
 
1783
#if (FOOTERS && !INSECURE)
1784
    {
1785
#if USE_DEV_RANDOM
1786
      int fd;
1787
      unsigned char buf[sizeof(size_t)];
1788
      /* Try to use /dev/urandom, else fall back on using time */
1789
      if ((fd = open("/dev/urandom", O_RDONLY)) >= 0 &&
1790
          read(fd, buf, sizeof(buf)) == sizeof(buf)) {
1791
        s = *((size_t *) buf);
1792
        close(fd);
1793
      }
1794
      else
1795
#endif /* USE_DEV_RANDOM */
1796
        s = (size_t)(time(0) ^ (size_t)0x55555555U);
1797
 
1798
      s |= (size_t)8U;    /* ensure nonzero */
1799
      s &= ~(size_t)7U;   /* improve chances of fault for bad values */
1800
 
1801
    }
1802
#else /* (FOOTERS && !INSECURE) */
1803
    s = (size_t)0x58585858U;
1804
#endif /* (FOOTERS && !INSECURE) */
1805
    ACQUIRE_MAGIC_INIT_LOCK();
1806
    if (mparams.magic == 0) {
1807
      mparams.magic = s;
1808
      /* Set up lock for main malloc area */
1809
      INITIAL_LOCK(&gm->mutex);
1810
      gm->mflags = mparams.default_mflags;
1811
    }
1812
    RELEASE_MAGIC_INIT_LOCK();
1813
 
1814
#ifndef WIN32
1815
    mparams.page_size = malloc_getpagesize;
1816
    mparams.granularity = ((DEFAULT_GRANULARITY != 0)?
1817
                           DEFAULT_GRANULARITY : mparams.page_size);
1818
#else /* WIN32 */
1819
    {
1820
      SYSTEM_INFO system_info;
1821
      GetSystemInfo(&system_info);
1822
      mparams.page_size = system_info.dwPageSize;
1823
      mparams.granularity = system_info.dwAllocationGranularity;
1824
    }
1825
#endif /* WIN32 */
1826
 
1827
    /* Sanity-check configuration:
1828
       size_t must be unsigned and as wide as pointer type.
1829
       ints must be at least 4 bytes.
1830
       alignment must be at least 8.
1831
       Alignment, min chunk size, and page size must all be powers of 2.
1832
    */
1833
    if ((sizeof(size_t) != sizeof(char*)) ||
1834
        (MAX_SIZE_T < MIN_CHUNK_SIZE)  ||
1835
        (sizeof(int) < 4)  ||
1836
        (MALLOC_ALIGNMENT < (size_t)8U) ||
1837
        ((MALLOC_ALIGNMENT    & (MALLOC_ALIGNMENT-SIZE_T_ONE))    != 0) ||
1838
        ((MCHUNK_SIZE         & (MCHUNK_SIZE-SIZE_T_ONE))         != 0) ||
1839
        ((mparams.granularity & (mparams.granularity-SIZE_T_ONE)) != 0) ||
1840
        ((mparams.page_size   & (mparams.page_size-SIZE_T_ONE))   != 0))
1841
      ABORT;
1842
  }
1843
  return 0;
1844
}
1845
 
1846
/* support for mallopt */
1847
static int change_mparam(int param_number, int value) {
1848
  size_t val = (size_t)value;
1849
  init_mparams();
1850
  switch(param_number) {
1851
  case M_TRIM_THRESHOLD:
1852
    mparams.trim_threshold = val;
1853
    return 1;
1854
  case M_GRANULARITY:
1855
    if (val >= mparams.page_size && ((val & (val-1)) == 0)) {
1856
      mparams.granularity = val;
1857
      return 1;
1858
    }
1859
    else
1860
      return 0;
1861
  case M_MMAP_THRESHOLD:
1862
    mparams.mmap_threshold = val;
1863
    return 1;
1864
  default:
1865
    return 0;
1866
  }
1867
}
1868
 
1869
#if DEBUG
1870
/* ------------------------- Debugging Support --------------------------- */
1871
 
1872
/* Check properties of any chunk, whether free, inuse, mmapped etc  */
1873
static void do_check_any_chunk(mstate m, mchunkptr p) {
1874
  assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD));
1875
  assert(ok_address(m, p));
1876
}
1877
 
1878
/* Check properties of top chunk */
1879
static void do_check_top_chunk(mstate m, mchunkptr p) {
1880
  msegmentptr sp = segment_holding(m, (char*)p);
1881
  size_t  sz = chunksize(p);
1882
  assert(sp != 0);
1883
  assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD));
1884
  assert(ok_address(m, p));
1885
  assert(sz == m->topsize);
1886
  assert(sz > 0);
1887
  assert(sz == ((sp->base + sp->size) - (char*)p) - TOP_FOOT_SIZE);
1888
  assert(pinuse(p));
1889
  assert(!next_pinuse(p));
1890
}
1891
 
1892
/* Check properties of (inuse) mmapped chunks */
1893
static void do_check_mmapped_chunk(mstate m, mchunkptr p) {
1894
  size_t  sz = chunksize(p);
1895
  size_t len = (sz + (p->prev_foot & ~IS_MMAPPED_BIT) + MMAP_FOOT_PAD);
1896
  assert(is_mmapped(p));
1897
  assert(use_mmap(m));
1898
  assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD));
1899
  assert(ok_address(m, p));
1900
  assert(!is_small(sz));
1901
  assert((len & (mparams.page_size-SIZE_T_ONE)) == 0);
1902
  assert(chunk_plus_offset(p, sz)->head == FENCEPOST_HEAD);
1903
  assert(chunk_plus_offset(p, sz+SIZE_T_SIZE)->head == 0);
1904
}
1905
 
1906
/* Check properties of inuse chunks */
1907
static void do_check_inuse_chunk(mstate m, mchunkptr p) {
1908
  do_check_any_chunk(m, p);
1909
  assert(cinuse(p));
1910
  assert(next_pinuse(p));
1911
  /* If not pinuse and not mmapped, previous chunk has OK offset */
1912
  assert(is_mmapped(p) || pinuse(p) || next_chunk(prev_chunk(p)) == p);
1913
  if (is_mmapped(p))
1914
    do_check_mmapped_chunk(m, p);
1915
}
1916
 
1917
/* Check properties of free chunks */
1918
static void do_check_free_chunk(mstate m, mchunkptr p) {
1919
  size_t sz = p->head & ~(PINUSE_BIT|CINUSE_BIT);
1920
  mchunkptr next = chunk_plus_offset(p, sz);
1921
  do_check_any_chunk(m, p);
1922
  assert(!cinuse(p));
1923
  assert(!next_pinuse(p));
1924
  assert (!is_mmapped(p));
1925
  if (p != m->dv && p != m->top) {
1926
    if (sz >= MIN_CHUNK_SIZE) {
1927
      assert((sz & CHUNK_ALIGN_MASK) == 0);
1928
      assert(is_aligned(chunk2mem(p)));
1929
      assert(next->prev_foot == sz);
1930
      assert(pinuse(p));
1931
      assert (next == m->top || cinuse(next));
1932
      assert(p->fd->bk == p);
1933
      assert(p->bk->fd == p);
1934
    }
1935
    else  /* markers are always of size SIZE_T_SIZE */
1936
      assert(sz == SIZE_T_SIZE);
1937
  }
1938
}
1939
 
1940
/* Check properties of malloced chunks at the point they are malloced */
1941
static void do_check_malloced_chunk(mstate m, void* mem, size_t s) {
1942
  if (mem != 0) {
1943
    mchunkptr p = mem2chunk(mem);
1944
    size_t sz = p->head & ~(PINUSE_BIT|CINUSE_BIT);
1945
    do_check_inuse_chunk(m, p);
1946
    assert((sz & CHUNK_ALIGN_MASK) == 0);
1947
    assert(sz >= MIN_CHUNK_SIZE);
1948
    assert(sz >= s);
1949
    /* unless mmapped, size is less than MIN_CHUNK_SIZE more than request */
1950
    assert(is_mmapped(p) || sz < (s + MIN_CHUNK_SIZE));
1951
  }
1952
}
1953
 
1954
/* Check a tree and its subtrees.  */
1955
static void do_check_tree(mstate m, tchunkptr t) {
1956
  tchunkptr head = 0;
1957
  tchunkptr u = t;
1958
  bindex_t tindex = t->index;
1959
  size_t tsize = chunksize(t);
1960
  bindex_t idx;
1961
  compute_tree_index(tsize, idx);
1962
  assert(tindex == idx);
1963
  assert(tsize >= MIN_LARGE_SIZE);
1964
  assert(tsize >= minsize_for_tree_index(idx));
1965
  assert((idx == NTREEBINS-1) || (tsize < minsize_for_tree_index((idx+1))));
1966
 
1967
  do { /* traverse through chain of same-sized nodes */
1968
    do_check_any_chunk(m, ((mchunkptr)u));
1969
    assert(u->index == tindex);
1970
    assert(chunksize(u) == tsize);
1971
    assert(!cinuse(u));
1972
    assert(!next_pinuse(u));
1973
    assert(u->fd->bk == u);
1974
    assert(u->bk->fd == u);
1975
    if (u->parent == 0) {
1976
      assert(u->child[0] == 0);
1977
      assert(u->child[1] == 0);
1978
    }
1979
    else {
1980
      assert(head == 0); /* only one node on chain has parent */
1981
      head = u;
1982
      assert(u->parent != u);
1983
      assert (u->parent->child[0] == u ||
1984
              u->parent->child[1] == u ||
1985
              *((tbinptr*)(u->parent)) == u);
1986
      if (u->child[0] != 0) {
1987
        assert(u->child[0]->parent == u);
1988
        assert(u->child[0] != u);
1989
        do_check_tree(m, u->child[0]);
1990
      }
1991
      if (u->child[1] != 0) {
1992
        assert(u->child[1]->parent == u);
1993
        assert(u->child[1] != u);
1994
        do_check_tree(m, u->child[1]);
1995
      }
1996
      if (u->child[0] != 0 && u->child[1] != 0) {
1997
        assert(chunksize(u->child[0]) < chunksize(u->child[1]));
1998
      }
1999
    }
2000
    u = u->fd;
2001
  } while (u != t);
2002
  assert(head != 0);
2003
}
2004
 
2005
/*  Check all the chunks in a treebin.  */
2006
static void do_check_treebin(mstate m, bindex_t i) {
2007
  tbinptr* tb = treebin_at(m, i);
2008
  tchunkptr t = *tb;
2009
  int empty = (m->treemap & (1U << i)) == 0;
2010
  if (t == 0)
2011
    assert(empty);
2012
  if (!empty)
2013
    do_check_tree(m, t);
2014
}
2015
 
2016
/*  Check all the chunks in a smallbin.  */
2017
static void do_check_smallbin(mstate m, bindex_t i) {
2018
  sbinptr b = smallbin_at(m, i);
2019
  mchunkptr p = b->bk;
2020
  unsigned int empty = (m->smallmap & (1U << i)) == 0;
2021
  if (p == b)
2022
    assert(empty);
2023
  if (!empty) {
2024
    for (; p != b; p = p->bk) {
2025
      size_t size = chunksize(p);
2026
      mchunkptr q;
2027
      /* each chunk claims to be free */
2028
      do_check_free_chunk(m, p);
2029
      /* chunk belongs in bin */
2030
      assert(small_index(size) == i);
2031
      assert(p->bk == b || chunksize(p->bk) == chunksize(p));
2032
      /* chunk is followed by an inuse chunk */
2033
      q = next_chunk(p);
2034
      if (q->head != FENCEPOST_HEAD)
2035
        do_check_inuse_chunk(m, q);
2036
    }
2037
  }
2038
}
2039
 
2040
/* Find x in a bin. Used in other check functions. */
2041
static int bin_find(mstate m, mchunkptr x) {
2042
  size_t size = chunksize(x);
2043
  if (is_small(size)) {
2044
    bindex_t sidx = small_index(size);
2045
    sbinptr b = smallbin_at(m, sidx);
2046
    if (smallmap_is_marked(m, sidx)) {
2047
      mchunkptr p = b;
2048
      do {
2049
        if (p == x)
2050
          return 1;
2051
      } while ((p = p->fd) != b);
2052
    }
2053
  }
2054
  else {
2055
    bindex_t tidx;
2056
    compute_tree_index(size, tidx);
2057
    if (treemap_is_marked(m, tidx)) {
2058
      tchunkptr t = *treebin_at(m, tidx);
2059
      size_t sizebits = size << leftshift_for_tree_index(tidx);
2060
      while (t != 0 && chunksize(t) != size) {
2061
        t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1];
2062
        sizebits <<= 1;
2063
      }
2064
      if (t != 0) {
2065
        tchunkptr u = t;
2066
        do {
2067
          if (u == (tchunkptr)x)
2068
            return 1;
2069
        } while ((u = u->fd) != t);
2070
      }
2071
    }
2072
  }
2073
  return 0;
2074
}
2075
 
2076
/* Traverse each chunk and check it; return total */
2077
static size_t traverse_and_check(mstate m) {
2078
  size_t sum = 0;
2079
  if (is_initialized(m)) {
2080
    msegmentptr s = &m->seg;
2081
    sum += m->topsize + TOP_FOOT_SIZE;
2082
    while (s != 0) {
2083
      mchunkptr q = align_as_chunk(s->base);
2084
      mchunkptr lastq = 0;
2085
      assert(pinuse(q));
2086
      while (segment_holds(s, q) &&
2087
             q != m->top && q->head != FENCEPOST_HEAD) {
2088
        sum += chunksize(q);
2089
        if (cinuse(q)) {
2090
          assert(!bin_find(m, q));
2091
          do_check_inuse_chunk(m, q);
2092
        }
2093
        else {
2094
          assert(q == m->dv || bin_find(m, q));
2095
          assert(lastq == 0 || cinuse(lastq)); /* Not 2 consecutive free */
2096
          do_check_free_chunk(m, q);
2097
        }
2098
        lastq = q;
2099
        q = next_chunk(q);
2100
      }
2101
      s = s->next;
2102
    }
2103
  }
2104
  return sum;
2105
}
2106
 
2107
/* Check all properties of malloc_state. */
2108
static void do_check_malloc_state(mstate m) {
2109
  bindex_t i;
2110
  size_t total;
2111
  /* check bins */
2112
  for (i = 0; i < NSMALLBINS; ++i)
2113
    do_check_smallbin(m, i);
2114
  for (i = 0; i < NTREEBINS; ++i)
2115
    do_check_treebin(m, i);
2116
 
2117
  if (m->dvsize != 0) { /* check dv chunk */
2118
    do_check_any_chunk(m, m->dv);
2119
    assert(m->dvsize == chunksize(m->dv));
2120
    assert(m->dvsize >= MIN_CHUNK_SIZE);
2121
    assert(bin_find(m, m->dv) == 0);
2122
  }
2123
 
2124
  if (m->top != 0) {   /* check top chunk */
2125
    do_check_top_chunk(m, m->top);
2126
    assert(m->topsize == chunksize(m->top));
2127
    assert(m->topsize > 0);
2128
    assert(bin_find(m, m->top) == 0);
2129
  }
2130
 
2131
  total = traverse_and_check(m);
2132
  assert(total <= m->footprint);
2133
  assert(m->footprint <= m->max_footprint);
2134
}
2135
#endif /* DEBUG */
2136
 
2137
/* ----------------------------- statistics ------------------------------ */
2138
 
2139
#if !NO_MALLINFO
2140
static struct mallinfo internal_mallinfo(mstate m) {
2141
  struct mallinfo nm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2142
  if (!PREACTION(m)) {
2143
    check_malloc_state(m);
2144
    if (is_initialized(m)) {
2145
      size_t nfree = SIZE_T_ONE; /* top always free */
2146
      size_t mfree = m->topsize + TOP_FOOT_SIZE;
2147
      size_t sum = mfree;
2148
      msegmentptr s = &m->seg;
2149
      while (s != 0) {
2150
        mchunkptr q = align_as_chunk(s->base);
2151
        while (segment_holds(s, q) &&
2152
               q != m->top && q->head != FENCEPOST_HEAD) {
2153
          size_t sz = chunksize(q);
2154
          sum += sz;
2155
          if (!cinuse(q)) {
2156
            mfree += sz;
2157
            ++nfree;
2158
          }
2159
          q = next_chunk(q);
2160
        }
2161
        s = s->next;
2162
      }
2163
 
2164
      nm.arena    = sum;
2165
      nm.ordblks  = nfree;
2166
      nm.hblkhd   = m->footprint - sum;
2167
      nm.usmblks  = m->max_footprint;
2168
      nm.uordblks = m->footprint - mfree;
2169
      nm.fordblks = mfree;
2170
      nm.keepcost = m->topsize;
2171
    }
2172
 
2173
    POSTACTION(m);
2174
  }
2175
  return nm;
2176
}
2177
#endif /* !NO_MALLINFO */
2178
 
2179
static void internal_malloc_stats(mstate m) {
2180
  if (!PREACTION(m)) {
2181
    size_t maxfp = 0;
2182
    size_t fp = 0;
2183
    size_t used = 0;
2184
    check_malloc_state(m);
2185
    if (is_initialized(m)) {
2186
      msegmentptr s = &m->seg;
2187
      maxfp = m->max_footprint;
2188
      fp = m->footprint;
2189
      used = fp - (m->topsize + TOP_FOOT_SIZE);
2190
 
2191
      while (s != 0) {
2192
        mchunkptr q = align_as_chunk(s->base);
2193
        while (segment_holds(s, q) &&
2194
               q != m->top && q->head != FENCEPOST_HEAD) {
2195
          if (!cinuse(q))
2196
            used -= chunksize(q);
2197
          q = next_chunk(q);
2198
        }
2199
        s = s->next;
2200
      }
2201
    }
2202
 
2203
    fprintf(stderr, "max system bytes = %10lu\n", (unsigned long)(maxfp));
2204
    fprintf(stderr, "system bytes     = %10lu\n", (unsigned long)(fp));
2205
    fprintf(stderr, "in use bytes     = %10lu\n", (unsigned long)(used));
2206
 
2207
    POSTACTION(m);
2208
  }
2209
}
2210
 
2211
/* ----------------------- Operations on smallbins ----------------------- */
2212
 
2213
/*
2214
  Various forms of linking and unlinking are defined as macros.  Even
2215
  the ones for trees, which are very long but have very short typical
2216
  paths.  This is ugly but reduces reliance on inlining support of
2217
  compilers.
2218
*/
2219
 
2220
/* Link a free chunk into a smallbin  */
2221
#define insert_small_chunk(M, P, S) {\
2222
  bindex_t I  = small_index(S);\
2223
  mchunkptr B = smallbin_at(M, I);\
2224
  mchunkptr F = B;\
2225
  assert(S >= MIN_CHUNK_SIZE);\
2226
  if (!smallmap_is_marked(M, I))\
2227
    mark_smallmap(M, I);\
2228
  else if (RTCHECK(ok_address(M, B->fd)))\
2229
    F = B->fd;\
2230
  else {\
2231
    CORRUPTION_ERROR_ACTION(M);\
2232
  }\
2233
  B->fd = P;\
2234
  F->bk = P;\
2235
  P->fd = F;\
2236
  P->bk = B;\
2237
}
2238
 
2239
/* Unlink a chunk from a smallbin  */
2240
#define unlink_small_chunk(M, P, S) {\
2241
  mchunkptr F = P->fd;\
2242
  mchunkptr B = P->bk;\
2243
  bindex_t I = small_index(S);\
2244
  assert(P != B);\
2245
  assert(P != F);\
2246
  assert(chunksize(P) == small_index2size(I));\
2247
  if (F == B)\
2248
    clear_smallmap(M, I);\
2249
  else if (RTCHECK((F == smallbin_at(M,I) || ok_address(M, F)) &&\
2250
                   (B == smallbin_at(M,I) || ok_address(M, B)))) {\
2251
    F->bk = B;\
2252
    B->fd = F;\
2253
  }\
2254
  else {\
2255
    CORRUPTION_ERROR_ACTION(M);\
2256
  }\
2257
}
2258
 
2259
/* Unlink the first chunk from a smallbin */
2260
#define unlink_first_small_chunk(M, B, P, I) {\
2261
  mchunkptr F = P->fd;\
2262
  assert(P != B);\
2263
  assert(P != F);\
2264
  assert(chunksize(P) == small_index2size(I));\
2265
  if (B == F)\
2266
    clear_smallmap(M, I);\
2267
  else if (RTCHECK(ok_address(M, F))) {\
2268
    B->fd = F;\
2269
    F->bk = B;\
2270
  }\
2271
  else {\
2272
    CORRUPTION_ERROR_ACTION(M);\
2273
  }\
2274
}
2275
 
2276
/* Replace dv node, binning the old one */
2277
/* Used only when dvsize known to be small */
2278
#define replace_dv(M, P, S) {\
2279
  size_t DVS = M->dvsize;\
2280
  if (DVS != 0) {\
2281
    mchunkptr DV = M->dv;\
2282
    assert(is_small(DVS));\
2283
    insert_small_chunk(M, DV, DVS);\
2284
  }\
2285
  M->dvsize = S;\
2286
  M->dv = P;\
2287
}
2288
 
2289
/* ------------------------- Operations on trees ------------------------- */
2290
 
2291
/* Insert chunk into tree */
2292
#define insert_large_chunk(M, X, S) {\
2293
  tbinptr* H;\
2294
  bindex_t I;\
2295
  compute_tree_index(S, I);\
2296
  H = treebin_at(M, I);\
2297
  X->index = I;\
2298
  X->child[0] = X->child[1] = 0;\
2299
  if (!treemap_is_marked(M, I)) {\
2300
    mark_treemap(M, I);\
2301
    *H = X;\
2302
    X->parent = (tchunkptr)H;\
2303
    X->fd = X->bk = X;\
2304
  }\
2305
  else {\
2306
    tchunkptr T = *H;\
2307
    size_t K = S << leftshift_for_tree_index(I);\
2308
    for (;;) {\
2309
      if (chunksize(T) != S) {\
2310
        tchunkptr* C = &(T->child[(K >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]);\
2311
        K <<= 1;\
2312
        if (*C != 0)\
2313
          T = *C;\
2314
        else if (RTCHECK(ok_address(M, C))) {\
2315
          *C = X;\
2316
          X->parent = T;\
2317
          X->fd = X->bk = X;\
2318
          break;\
2319
        }\
2320
        else {\
2321
          CORRUPTION_ERROR_ACTION(M);\
2322
          break;\
2323
        }\
2324
      }\
2325
      else {\
2326
        tchunkptr F = T->fd;\
2327
        if (RTCHECK(ok_address(M, T) && ok_address(M, F))) {\
2328
          T->fd = F->bk = X;\
2329
          X->fd = F;\
2330
          X->bk = T;\
2331
          X->parent = 0;\
2332
          break;\
2333
        }\
2334
        else {\
2335
          CORRUPTION_ERROR_ACTION(M);\
2336
          break;\
2337
        }\
2338
      }\
2339
    }\
2340
  }\
2341
}
2342
 
2343
/*
2344
  Unlink steps:
2345
 
2346
  1. If x is a chained node, unlink it from its same-sized fd/bk links
2347
     and choose its bk node as its replacement.
2348
  2. If x was the last node of its size, but not a leaf node, it must
2349
     be replaced with a leaf node (not merely one with an open left or
2350
     right), to make sure that lefts and rights of descendents
2351
     correspond properly to bit masks.  We use the rightmost descendent
2352
     of x.  We could use any other leaf, but this is easy to locate and
2353
     tends to counteract removal of leftmosts elsewhere, and so keeps
2354
     paths shorter than minimally guaranteed.  This doesn't loop much
2355
     because on average a node in a tree is near the bottom.
2356
  3. If x is the base of a chain (i.e., has parent links) relink
2357
     x's parent and children to x's replacement (or null if none).
2358
*/
2359
 
2360
#define unlink_large_chunk(M, X) {\
2361
  tchunkptr XP = X->parent;\
2362
  tchunkptr R;\
2363
  if (X->bk != X) {\
2364
    tchunkptr F = X->fd;\
2365
    R = X->bk;\
2366
    if (RTCHECK(ok_address(M, F))) {\
2367
      F->bk = R;\
2368
      R->fd = F;\
2369
    }\
2370
    else {\
2371
      CORRUPTION_ERROR_ACTION(M);\
2372
    }\
2373
  }\
2374
  else {\
2375
    tchunkptr* RP;\
2376
    if (((R = *(RP = &(X->child[1]))) != 0) ||\
2377
        ((R = *(RP = &(X->child[0]))) != 0)) {\
2378
      tchunkptr* CP;\
2379
      while ((*(CP = &(R->child[1])) != 0) ||\
2380
             (*(CP = &(R->child[0])) != 0)) {\
2381
        R = *(RP = CP);\
2382
      }\
2383
      if (RTCHECK(ok_address(M, RP)))\
2384
        *RP = 0;\
2385
      else {\
2386
        CORRUPTION_ERROR_ACTION(M);\
2387
      }\
2388
    }\
2389
  }\
2390
  if (XP != 0) {\
2391
    tbinptr* H = treebin_at(M, X->index);\
2392
    if (X == *H) {\
2393
      if ((*H = R) == 0) \
2394
        clear_treemap(M, X->index);\
2395
    }\
2396
    else if (RTCHECK(ok_address(M, XP))) {\
2397
      if (XP->child[0] == X) \
2398
        XP->child[0] = R;\
2399
      else \
2400
        XP->child[1] = R;\
2401
    }\
2402
    else\
2403
      CORRUPTION_ERROR_ACTION(M);\
2404
    if (R != 0) {\
2405
      if (RTCHECK(ok_address(M, R))) {\
2406
        tchunkptr C0, C1;\
2407
        R->parent = XP;\
2408
        if ((C0 = X->child[0]) != 0) {\
2409
          if (RTCHECK(ok_address(M, C0))) {\
2410
            R->child[0] = C0;\
2411
            C0->parent = R;\
2412
          }\
2413
          else\
2414
            CORRUPTION_ERROR_ACTION(M);\
2415
        }\
2416
        if ((C1 = X->child[1]) != 0) {\
2417
          if (RTCHECK(ok_address(M, C1))) {\
2418
            R->child[1] = C1;\
2419
            C1->parent = R;\
2420
          }\
2421
          else\
2422
            CORRUPTION_ERROR_ACTION(M);\
2423
        }\
2424
      }\
2425
      else\
2426
        CORRUPTION_ERROR_ACTION(M);\
2427
    }\
2428
  }\
2429
}
2430
 
2431
/* Relays to large vs small bin operations */
2432
 
2433
#define insert_chunk(M, P, S)\
2434
  if (is_small(S)) insert_small_chunk(M, P, S)\
2435
  else { tchunkptr TP = (tchunkptr)(P); insert_large_chunk(M, TP, S); }
2436
 
2437
#define unlink_chunk(M, P, S)\
2438
  if (is_small(S)) unlink_small_chunk(M, P, S)\
2439
  else { tchunkptr TP = (tchunkptr)(P); unlink_large_chunk(M, TP); }
2440
 
2441
 
2442
/* Relays to internal calls to malloc/free from realloc, memalign etc */
2443
 
2444
#if ONLY_MSPACES
2445
#define internal_malloc(m, b) mspace_malloc(m, b)
2446
#define internal_free(m, mem) mspace_free(m,mem);
2447
#else /* ONLY_MSPACES */
2448
#if MSPACES
2449
#define internal_malloc(m, b)\
2450
   (m == gm)? dlmalloc(b) : mspace_malloc(m, b)
2451
#define internal_free(m, mem)\
2452
   if (m == gm) dlfree(mem); else mspace_free(m,mem);
2453
#else /* MSPACES */
2454
#define internal_malloc(m, b) dlmalloc(b)
2455
#define internal_free(m, mem) dlfree(mem)
2456
#endif /* MSPACES */
2457
#endif /* ONLY_MSPACES */
2458
 
2459
/* -----------------------  Direct-mmapping chunks ----------------------- */
2460
 
2461
/*
2462
  Directly mmapped chunks are set up with an offset to the start of
2463
  the mmapped region stored in the prev_foot field of the chunk. This
2464
  allows reconstruction of the required argument to MUNMAP when freed,
2465
  and also allows adjustment of the returned chunk to meet alignment
2466
  requirements (especially in memalign).  There is also enough space
2467
  allocated to hold a fake next chunk of size SIZE_T_SIZE to maintain
2468
  the PINUSE bit so frees can be checked.
2469
*/
2470
 
2471
/* Malloc using mmap */
2472
static void* mmap_alloc(mstate m, size_t nb) {
2473
  size_t mmsize = granularity_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK);
2474
  if (mmsize > nb) {     /* Check for wrap around 0 */
2475
    char* mm = (char*)(DIRECT_MMAP(mmsize));
2476
    if (mm != CMFAIL) {
2477
      size_t offset = align_offset(chunk2mem(mm));
2478
      size_t psize = mmsize - offset - MMAP_FOOT_PAD;
2479
      mchunkptr p = (mchunkptr)(mm + offset);
2480
      p->prev_foot = offset | IS_MMAPPED_BIT;
2481
      (p)->head = (psize|CINUSE_BIT);
2482
      mark_inuse_foot(m, p, psize);
2483
      chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD;
2484
      chunk_plus_offset(p, psize+SIZE_T_SIZE)->head = 0;
2485
 
2486
      if (mm < m->least_addr)
2487
        m->least_addr = mm;
2488
      if ((m->footprint += mmsize) > m->max_footprint)
2489
        m->max_footprint = m->footprint;
2490
      assert(is_aligned(chunk2mem(p)));
2491
      check_mmapped_chunk(m, p);
2492
      return chunk2mem(p);
2493
    }
2494
  }
2495
  return 0;
2496
}
2497
 
2498
/* Realloc using mmap */
2499
static mchunkptr mmap_resize(mstate m, mchunkptr oldp, size_t nb) {
2500
  size_t oldsize = chunksize(oldp);
2501
  if (is_small(nb)) /* Can't shrink mmap regions below small size */
2502
    return 0;
2503
  /* Keep old chunk if big enough but not too big */
2504
  if (oldsize >= nb + SIZE_T_SIZE &&
2505
      (oldsize - nb) <= (mparams.granularity << 1))
2506
    return oldp;
2507
  else {
2508
    size_t offset = oldp->prev_foot & ~IS_MMAPPED_BIT;
2509
    size_t oldmmsize = oldsize + offset + MMAP_FOOT_PAD;
2510
    size_t newmmsize = granularity_align(nb + SIX_SIZE_T_SIZES +
2511
                                         CHUNK_ALIGN_MASK);
2512
    char* cp = (char*)CALL_MREMAP((char*)oldp - offset,
2513
                                  oldmmsize, newmmsize, 1);
2514
    if (cp != CMFAIL) {
2515
      mchunkptr newp = (mchunkptr)(cp + offset);
2516
      size_t psize = newmmsize - offset - MMAP_FOOT_PAD;
2517
      newp->head = (psize|CINUSE_BIT);
2518
      mark_inuse_foot(m, newp, psize);
2519
      chunk_plus_offset(newp, psize)->head = FENCEPOST_HEAD;
2520
      chunk_plus_offset(newp, psize+SIZE_T_SIZE)->head = 0;
2521
 
2522
      if (cp < m->least_addr)
2523
        m->least_addr = cp;
2524
      if ((m->footprint += newmmsize - oldmmsize) > m->max_footprint)
2525
        m->max_footprint = m->footprint;
2526
      check_mmapped_chunk(m, newp);
2527
      return newp;
2528
    }
2529
  }
2530
  return 0;
2531
}
2532
 
2533
/* -------------------------- mspace management -------------------------- */
2534
 
2535
/* Initialize top chunk and its size */
2536
static void init_top(mstate m, mchunkptr p, size_t psize) {
2537
  /* Ensure alignment */
2538
  size_t offset = align_offset(chunk2mem(p));
2539
  p = (mchunkptr)((char*)p + offset);
2540
  psize -= offset;
2541
 
2542
  m->top = p;
2543
  m->topsize = psize;
2544
  p->head = psize | PINUSE_BIT;
2545
  /* set size of fake trailing chunk holding overhead space only once */
2546
  chunk_plus_offset(p, psize)->head = TOP_FOOT_SIZE;
2547
  m->trim_check = mparams.trim_threshold; /* reset on each update */
2548
}
2549
 
2550
/* Initialize bins for a new mstate that is otherwise zeroed out */
2551
static void init_bins(mstate m) {
2552
  /* Establish circular links for smallbins */
2553
  bindex_t i;
2554
  for (i = 0; i < NSMALLBINS; ++i) {
2555
    sbinptr bin = smallbin_at(m,i);
2556
    bin->fd = bin->bk = bin;
2557
  }
2558
}
2559
 
2560
#if PROCEED_ON_ERROR
2561
 
2562
/* default corruption action */
2563
static void reset_on_error(mstate m) {
2564
  int i;
2565
  ++malloc_corruption_error_count;
2566
  /* Reinitialize fields to forget about all memory */
2567
  m->smallbins = m->treebins = 0;
2568
  m->dvsize = m->topsize = 0;
2569
  m->seg.base = 0;
2570
  m->seg.size = 0;
2571
  m->seg.next = 0;
2572
  m->top = m->dv = 0;
2573
  for (i = 0; i < NTREEBINS; ++i)
2574
    *treebin_at(m, i) = 0;
2575
  init_bins(m);
2576
}
2577
#endif /* PROCEED_ON_ERROR */
2578
 
2579
/* Allocate chunk and prepend remainder with chunk in successor base. */
2580
static void* prepend_alloc(mstate m, char* newbase, char* oldbase,
2581
                           size_t nb) {
2582
  mchunkptr p = align_as_chunk(newbase);
2583
  mchunkptr oldfirst = align_as_chunk(oldbase);
2584
  size_t psize = (char*)oldfirst - (char*)p;
2585
  mchunkptr q = chunk_plus_offset(p, nb);
2586
  size_t qsize = psize - nb;
2587
  set_size_and_pinuse_of_inuse_chunk(m, p, nb);
2588
 
2589
  assert((char*)oldfirst > (char*)q);
2590
  assert(pinuse(oldfirst));
2591
  assert(qsize >= MIN_CHUNK_SIZE);
2592
 
2593
  /* consolidate remainder with first chunk of old base */
2594
  if (oldfirst == m->top) {
2595
    size_t tsize = m->topsize += qsize;
2596
    m->top = q;
2597
    q->head = tsize | PINUSE_BIT;
2598
    check_top_chunk(m, q);
2599
  }
2600
  else if (oldfirst == m->dv) {
2601
    size_t dsize = m->dvsize += qsize;
2602
    m->dv = q;
2603
    set_size_and_pinuse_of_free_chunk(q, dsize);
2604
  }
2605
  else {
2606
    if (!cinuse(oldfirst)) {
2607
      size_t nsize = chunksize(oldfirst);
2608
      unlink_chunk(m, oldfirst, nsize);
2609
      oldfirst = chunk_plus_offset(oldfirst, nsize);
2610
      qsize += nsize;
2611
    }
2612
    set_free_with_pinuse(q, qsize, oldfirst);
2613
    insert_chunk(m, q, qsize);
2614
    check_free_chunk(m, q);
2615
  }
2616
 
2617
  check_malloced_chunk(m, chunk2mem(p), nb);
2618
  return chunk2mem(p);
2619
}
2620
 
2621
 
2622
/* Add a segment to hold a new noncontiguous region */
2623
static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) {
2624
  /* Determine locations and sizes of segment, fenceposts, old top */
2625
  char* old_top = (char*)m->top;
2626
  msegmentptr oldsp = segment_holding(m, old_top);
2627
  char* old_end = oldsp->base + oldsp->size;
2628
  size_t ssize = pad_request(sizeof(struct malloc_segment));
2629
  char* rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK);
2630
  size_t offset = align_offset(chunk2mem(rawsp));
2631
  char* asp = rawsp + offset;
2632
  char* csp = (asp < (old_top + MIN_CHUNK_SIZE))? old_top : asp;
2633
  mchunkptr sp = (mchunkptr)csp;
2634
  msegmentptr ss = (msegmentptr)(chunk2mem(sp));
2635
  mchunkptr tnext = chunk_plus_offset(sp, ssize);
2636
  mchunkptr p = tnext;
2637
  int nfences = 0;
2638
 
2639
  /* reset top to new space */
2640
  init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE);
2641
 
2642
  /* Set up segment record */
2643
  assert(is_aligned(ss));
2644
  set_size_and_pinuse_of_inuse_chunk(m, sp, ssize);
2645
  *ss = m->seg; /* Push current record */
2646
  m->seg.base = tbase;
2647
  m->seg.size = tsize;
2648
  m->seg.sflags = mmapped;
2649
  m->seg.next = ss;
2650
 
2651
  /* Insert trailing fenceposts */
2652
  for (;;) {
2653
    mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE);
2654
    p->head = FENCEPOST_HEAD;
2655
    ++nfences;
2656
    if ((char*)(&(nextp->head)) < old_end)
2657
      p = nextp;
2658
    else
2659
      break;
2660
  }
2661
  assert(nfences >= 2);
2662
 
2663
  /* Insert the rest of old top into a bin as an ordinary free chunk */
2664
  if (csp != old_top) {
2665
    mchunkptr q = (mchunkptr)old_top;
2666
    size_t psize = csp - old_top;
2667
    mchunkptr tn = chunk_plus_offset(q, psize);
2668
    set_free_with_pinuse(q, psize, tn);
2669
    insert_chunk(m, q, psize);
2670
  }
2671
 
2672
  check_top_chunk(m, m->top);
2673
}
2674
 
2675
/* -------------------------- System allocation -------------------------- */
2676
 
2677
/* Get memory from system using MORECORE or MMAP */
2678
static void* sys_alloc(mstate m, size_t nb) {
2679
  char* tbase = CMFAIL;
2680
  size_t tsize = 0;
2681
  flag_t mmap_flag = 0;
2682
 
2683
  init_mparams();
2684
 
2685
  /* Directly map large chunks */
2686
  if (use_mmap(m) && nb >= mparams.mmap_threshold) {
2687
    void* mem = mmap_alloc(m, nb);
2688
    if (mem != 0)
2689
      return mem;
2690
  }
2691
 
2692
  /*
2693
    Try getting memory in any of three ways (in most-preferred to
2694
    least-preferred order):
2695
    1. A call to MORECORE that can normally contiguously extend memory.
2696
       (disabled if not MORECORE_CONTIGUOUS or not HAVE_MORECORE or
2697
       or main space is mmapped or a previous contiguous call failed)
2698
    2. A call to MMAP new space (disabled if not HAVE_MMAP).
2699
       Note that under the default settings, if MORECORE is unable to
2700
       fulfill a request, and HAVE_MMAP is true, then mmap is
2701
       used as a noncontiguous system allocator. This is a useful backup
2702
       strategy for systems with holes in address spaces -- in this case
2703
       sbrk cannot contiguously expand the heap, but mmap may be able to
2704
       find space.
2705
    3. A call to MORECORE that cannot usually contiguously extend memory.
2706
       (disabled if not HAVE_MORECORE)
2707
  */
2708
 
2709
  if (MORECORE_CONTIGUOUS && !use_noncontiguous(m)) {
2710
    char* br = CMFAIL;
2711
    msegmentptr ss = (m->top == 0)? 0 : segment_holding(m, (char*)m->top);
2712
    size_t asize = 0;
2713
    ACQUIRE_MORECORE_LOCK();
2714
 
2715
    if (ss == 0) {  /* First time through or recovery */
2716
      char* base = (char*)CALL_MORECORE(0);
2717
      if (base != CMFAIL) {
2718
        asize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE);
2719
        /* Adjust to end on a page boundary */
2720
        if (!is_page_aligned(base))
2721
          asize += (page_align((size_t)base) - (size_t)base);
2722
        /* Can't call MORECORE if size is negative when treated as signed */
2723
        if (asize < HALF_MAX_SIZE_T &&
2724
            (br = (char*)(CALL_MORECORE(asize))) == base) {
2725
          tbase = base;
2726
          tsize = asize;
2727
        }
2728
      }
2729
    }
2730
    else {
2731
      /* Subtract out existing available top space from MORECORE request. */
2732
      asize = granularity_align(nb - m->topsize + TOP_FOOT_SIZE + SIZE_T_ONE);
2733
      /* Use mem here only if it did continuously extend old space */
2734
      if (asize < HALF_MAX_SIZE_T &&
2735
          (br = (char*)(CALL_MORECORE(asize))) == ss->base+ss->size) {
2736
        tbase = br;
2737
        tsize = asize;
2738
      }
2739
    }
2740
 
2741
    if (tbase == CMFAIL) {    /* Cope with partial failure */
2742
      if (br != CMFAIL) {    /* Try to use/extend the space we did get */
2743
        if (asize < HALF_MAX_SIZE_T &&
2744
            asize < nb + TOP_FOOT_SIZE + SIZE_T_ONE) {
2745
          size_t esize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE - asize);
2746
          if (esize < HALF_MAX_SIZE_T) {
2747
            char* end = (char*)CALL_MORECORE(esize);
2748
            if (end != CMFAIL)
2749
              asize += esize;
2750
            else {            /* Can't use; try to release */
2751
              CALL_MORECORE(-asize);
2752
              br = CMFAIL;
2753
            }
2754
          }
2755
        }
2756
      }
2757
      if (br != CMFAIL) {    /* Use the space we did get */
2758
        tbase = br;
2759
        tsize = asize;
2760
      }
2761
      else
2762
        disable_contiguous(m); /* Don't try contiguous path in the future */
2763
    }
2764
 
2765
    RELEASE_MORECORE_LOCK();
2766
  }
2767
 
2768
  if (HAVE_MMAP && tbase == CMFAIL) {  /* Try MMAP */
2769
    size_t req = nb + TOP_FOOT_SIZE + SIZE_T_ONE;
2770
    size_t rsize = granularity_align(req);
2771
    if (rsize > nb) { /* Fail if wraps around zero */
2772
      char* mp = (char*)(CALL_MMAP(rsize));
2773
      if (mp != CMFAIL) {
2774
        tbase = mp;
2775
        tsize = rsize;
2776
        mmap_flag = IS_MMAPPED_BIT;
2777
      }
2778
    }
2779
  }
2780
 
2781
  if (HAVE_MORECORE && tbase == CMFAIL) { /* Try noncontiguous MORECORE */
2782
    size_t asize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE);
2783
    if (asize < HALF_MAX_SIZE_T) {
2784
      char* br = CMFAIL;
2785
      char* end = CMFAIL;
2786
      ACQUIRE_MORECORE_LOCK();
2787
      br = (char*)(CALL_MORECORE(asize));
2788
      end = (char*)(CALL_MORECORE(0));
2789
      RELEASE_MORECORE_LOCK();
2790
      if (br != CMFAIL && end != CMFAIL && br < end) {
2791
        size_t ssize = end - br;
2792
        if (ssize > nb + TOP_FOOT_SIZE) {
2793
          tbase = br;
2794
          tsize = ssize;
2795
        }
2796
      }
2797
    }
2798
  }
2799
 
2800
  if (tbase != CMFAIL) {
2801
 
2802
    if ((m->footprint += tsize) > m->max_footprint)
2803
      m->max_footprint = m->footprint;
2804
 
2805
    if (!is_initialized(m)) { /* first-time initialization */
2806
      m->seg.base = m->least_addr = tbase;
2807
      m->seg.size = tsize;
2808
      m->seg.sflags = mmap_flag;
2809
      m->magic = mparams.magic;
2810
      init_bins(m);
2811
      if (is_global(m))
2812
        init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE);
2813
      else {
2814
        /* Offset top by embedded malloc_state */
2815
        mchunkptr mn = next_chunk(mem2chunk(m));
2816
        init_top(m, mn, (size_t)((tbase + tsize) - (char*)mn) -TOP_FOOT_SIZE);
2817
      }
2818
    }
2819
 
2820
    else {
2821
      /* Try to merge with an existing segment */
2822
      msegmentptr sp = &m->seg;
2823
      while (sp != 0 && tbase != sp->base + sp->size)
2824
        sp = sp->next;
2825
      if (sp != 0 &&
2826
          !is_extern_segment(sp) &&
2827
          (sp->sflags & IS_MMAPPED_BIT) == mmap_flag &&
2828
          segment_holds(sp, m->top)) { /* append */
2829
        sp->size += tsize;
2830
        init_top(m, m->top, m->topsize + tsize);
2831
      }
2832
      else {
2833
        if (tbase < m->least_addr)
2834
          m->least_addr = tbase;
2835
        sp = &m->seg;
2836
        while (sp != 0 && sp->base != tbase + tsize)
2837
          sp = sp->next;
2838
        if (sp != 0 &&
2839
            !is_extern_segment(sp) &&
2840
            (sp->sflags & IS_MMAPPED_BIT) == mmap_flag) {
2841
          char* oldbase = sp->base;
2842
          sp->base = tbase;
2843
          sp->size += tsize;
2844
          return prepend_alloc(m, tbase, oldbase, nb);
2845
        }
2846
        else
2847
          add_segment(m, tbase, tsize, mmap_flag);
2848
      }
2849
    }
2850
 
2851
    if (nb < m->topsize) { /* Allocate from new or extended top space */
2852
      size_t rsize = m->topsize -= nb;
2853
      mchunkptr p = m->top;
2854
      mchunkptr r = m->top = chunk_plus_offset(p, nb);
2855
      r->head = rsize | PINUSE_BIT;
2856
      set_size_and_pinuse_of_inuse_chunk(m, p, nb);
2857
      check_top_chunk(m, m->top);
2858
      check_malloced_chunk(m, chunk2mem(p), nb);
2859
      return chunk2mem(p);
2860
    }
2861
  }
2862
 
2863
  MALLOC_FAILURE_ACTION;
2864
  return 0;
2865
}
2866
 
2867
/* -----------------------  system deallocation -------------------------- */
2868
 
2869
/* Unmap and unlink any mmapped segments that don't contain used chunks */
2870
static size_t release_unused_segments(mstate m) {
2871
  size_t released = 0;
2872
  msegmentptr pred = &m->seg;
2873
  msegmentptr sp = pred->next;
2874
  while (sp != 0) {
2875
    char* base = sp->base;
2876
    size_t size = sp->size;
2877
    msegmentptr next = sp->next;
2878
    if (is_mmapped_segment(sp) && !is_extern_segment(sp)) {
2879
      mchunkptr p = align_as_chunk(base);
2880
      size_t psize = chunksize(p);
2881
      /* Can unmap if first chunk holds entire segment and not pinned */
2882
      if (!cinuse(p) && (char*)p + psize >= base + size - TOP_FOOT_SIZE) {
2883
        tchunkptr tp = (tchunkptr)p;
2884
        assert(segment_holds(sp, (char*)sp));
2885
        if (p == m->dv) {
2886
          m->dv = 0;
2887
          m->dvsize = 0;
2888
        }
2889
        else {
2890
          unlink_large_chunk(m, tp);
2891
        }
2892
        if (CALL_MUNMAP(base, size) == 0) {
2893
          released += size;
2894
          m->footprint -= size;
2895
          /* unlink obsoleted record */
2896
          sp = pred;
2897
          sp->next = next;
2898
        }
2899
        else { /* back out if cannot unmap */
2900
          insert_large_chunk(m, tp, psize);
2901
        }
2902
      }
2903
    }
2904
    pred = sp;
2905
    sp = next;
2906
  }
2907
  return released;
2908
}
2909
 
2910
static int sys_trim(mstate m, size_t pad) {
2911
  size_t released = 0;
2912
  if (pad < MAX_REQUEST && is_initialized(m)) {
2913
    pad += TOP_FOOT_SIZE; /* ensure enough room for segment overhead */
2914
 
2915
    if (m->topsize > pad) {
2916
      /* Shrink top space in granularity-size units, keeping at least one */
2917
      size_t unit = mparams.granularity;
2918
      size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit -
2919
                      SIZE_T_ONE) * unit;
2920
      msegmentptr sp = segment_holding(m, (char*)m->top);
2921
 
2922
      if (!is_extern_segment(sp)) {
2923
        if (is_mmapped_segment(sp)) {
2924
          if (HAVE_MMAP &&
2925
              sp->size >= extra &&
2926
              !has_segment_link(m, sp)) { /* can't shrink if pinned */
2927
            size_t newsize = sp->size - extra;
2928
            /* Prefer mremap, fall back to munmap */
2929
            if ((CALL_MREMAP(sp->base, sp->size, newsize, 0) != MFAIL) ||
2930
                (CALL_MUNMAP(sp->base + newsize, extra) == 0)) {
2931
              released = extra;
2932
            }
2933
          }
2934
        }
2935
        else if (HAVE_MORECORE) {
2936
          if (extra >= HALF_MAX_SIZE_T) /* Avoid wrapping negative */
2937
            extra = (HALF_MAX_SIZE_T) + SIZE_T_ONE - unit;
2938
          ACQUIRE_MORECORE_LOCK();
2939
          {
2940
            /* Make sure end of memory is where we last set it. */
2941
            char* old_br = (char*)(CALL_MORECORE(0));
2942
            if (old_br == sp->base + sp->size) {
2943
              char* rel_br = (char*)(CALL_MORECORE(-extra));
2944
              char* new_br = (char*)(CALL_MORECORE(0));
2945
              if (rel_br != CMFAIL && new_br < old_br)
2946
                released = old_br - new_br;
2947
            }
2948
          }
2949
          RELEASE_MORECORE_LOCK();
2950
        }
2951
      }
2952
 
2953
      if (released != 0) {
2954
        sp->size -= released;
2955
        m->footprint -= released;
2956
        init_top(m, m->top, m->topsize - released);
2957
        check_top_chunk(m, m->top);
2958
      }
2959
    }
2960
 
2961
    /* Unmap any unused mmapped segments */
2962
    if (HAVE_MMAP)
2963
      released += release_unused_segments(m);
2964
 
2965
    /* On failure, disable autotrim to avoid repeated failed future calls */
2966
    if (released == 0)
2967
      m->trim_check = MAX_SIZE_T;
2968
  }
2969
 
2970
  return (released != 0)? 1 : 0;
2971
}
2972
 
2973
/* ---------------------------- malloc support --------------------------- */
2974
 
2975
/* allocate a large request from the best fitting chunk in a treebin */
2976
static void* tmalloc_large(mstate m, size_t nb) {
2977
  tchunkptr v = 0;
2978
  size_t rsize = -nb; /* Unsigned negation */
2979
  tchunkptr t;
2980
  bindex_t idx;
2981
  compute_tree_index(nb, idx);
2982
 
2983
  if ((t = *treebin_at(m, idx)) != 0) {
2984
    /* Traverse tree for this bin looking for node with size == nb */
2985
    size_t sizebits = nb << leftshift_for_tree_index(idx);
2986
    tchunkptr rst = 0;  /* The deepest untaken right subtree */
2987
    for (;;) {
2988
      tchunkptr rt;
2989
      size_t trem = chunksize(t) - nb;
2990
      if (trem < rsize) {
2991
        v = t;
2992
        if ((rsize = trem) == 0)
2993
          break;
2994
      }
2995
      rt = t->child[1];
2996
      t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1];
2997
      if (rt != 0 && rt != t)
2998
        rst = rt;
2999
      if (t == 0) {
3000
        t = rst; /* set t to least subtree holding sizes > nb */
3001
        break;
3002
      }
3003
      sizebits <<= 1;
3004
    }
3005
  }
3006
 
3007
  if (t == 0 && v == 0) { /* set t to root of next non-empty treebin */
3008
    binmap_t leftbits = left_bits(idx2bit(idx)) & m->treemap;
3009
    if (leftbits != 0) {
3010
      bindex_t i;
3011
      binmap_t leastbit = least_bit(leftbits);
3012
      compute_bit2idx(leastbit, i);
3013
      t = *treebin_at(m, i);
3014
    }
3015
  }
3016
 
3017
  while (t != 0) { /* find smallest of tree or subtree */
3018
    size_t trem = chunksize(t) - nb;
3019
    if (trem < rsize) {
3020
      rsize = trem;
3021
      v = t;
3022
    }
3023
    t = leftmost_child(t);
3024
  }
3025
 
3026
  /*  If dv is a better fit, return 0 so malloc will use it */
3027
  if (v != 0 && rsize < (size_t)(m->dvsize - nb)) {
3028
    if (RTCHECK(ok_address(m, v))) { /* split */
3029
      mchunkptr r = chunk_plus_offset(v, nb);
3030
      assert(chunksize(v) == rsize + nb);
3031
      if (RTCHECK(ok_next(v, r))) {
3032
        unlink_large_chunk(m, v);
3033
        if (rsize < MIN_CHUNK_SIZE)
3034
          set_inuse_and_pinuse(m, v, (rsize + nb));
3035
        else {
3036
          set_size_and_pinuse_of_inuse_chunk(m, v, nb);
3037
          set_size_and_pinuse_of_free_chunk(r, rsize);
3038
          insert_chunk(m, r, rsize);
3039
        }
3040
        return chunk2mem(v);
3041
      }
3042
    }
3043
    CORRUPTION_ERROR_ACTION(m);
3044
  }
3045
  return 0;
3046
}
3047
 
3048
/* allocate a small request from the best fitting chunk in a treebin */
3049
static void* tmalloc_small(mstate m, size_t nb) {
3050
  tchunkptr t, v;
3051
  size_t rsize;
3052
  bindex_t i;
3053
  binmap_t leastbit = least_bit(m->treemap);
3054
  compute_bit2idx(leastbit, i);
3055
 
3056
  v = t = *treebin_at(m, i);
3057
  rsize = chunksize(t) - nb;
3058
 
3059
  while ((t = leftmost_child(t)) != 0) {
3060
    size_t trem = chunksize(t) - nb;
3061
    if (trem < rsize) {
3062
      rsize = trem;
3063
      v = t;
3064
    }
3065
  }
3066
 
3067
  if (RTCHECK(ok_address(m, v))) {
3068
    mchunkptr r = chunk_plus_offset(v, nb);
3069
    assert(chunksize(v) == rsize + nb);
3070
    if (RTCHECK(ok_next(v, r))) {
3071
      unlink_large_chunk(m, v);
3072
      if (rsize < MIN_CHUNK_SIZE)
3073
        set_inuse_and_pinuse(m, v, (rsize + nb));
3074
      else {
3075
        set_size_and_pinuse_of_inuse_chunk(m, v, nb);
3076
        set_size_and_pinuse_of_free_chunk(r, rsize);
3077
        replace_dv(m, r, rsize);
3078
      }
3079
      return chunk2mem(v);
3080
    }
3081
  }
3082
 
3083
  CORRUPTION_ERROR_ACTION(m);
3084
  return 0;
3085
}
3086
 
3087
/* --------------------------- realloc support --------------------------- */
3088
 
3089
static void* internal_realloc(mstate m, void* oldmem, size_t bytes) {
3090
  if (bytes >= MAX_REQUEST) {
3091
    MALLOC_FAILURE_ACTION;
3092
    return 0;
3093
  }
3094
  if (!PREACTION(m)) {
3095
    mchunkptr oldp = mem2chunk(oldmem);
3096
    size_t oldsize = chunksize(oldp);
3097
    mchunkptr next = chunk_plus_offset(oldp, oldsize);
3098
    mchunkptr newp = 0;
3099
    void* extra = 0;
3100
 
3101
    /* Try to either shrink or extend into top. Else malloc-copy-free */
3102
 
3103
    if (RTCHECK(ok_address(m, oldp) && ok_cinuse(oldp) &&
3104
                ok_next(oldp, next) && ok_pinuse(next))) {
3105
      size_t nb = request2size(bytes);
3106
      if (is_mmapped(oldp))
3107
        newp = mmap_resize(m, oldp, nb);
3108
      else if (oldsize >= nb) { /* already big enough */
3109
        size_t rsize = oldsize - nb;
3110
        newp = oldp;
3111
        if (rsize >= MIN_CHUNK_SIZE) {
3112
          mchunkptr remainder = chunk_plus_offset(newp, nb);
3113
          set_inuse(m, newp, nb);
3114
          set_inuse(m, remainder, rsize);
3115
          extra = chunk2mem(remainder);
3116
        }
3117
      }
3118
      else if (next == m->top && oldsize + m->topsize > nb) {
3119
        /* Expand into top */
3120
        size_t newsize = oldsize + m->topsize;
3121
        size_t newtopsize = newsize - nb;
3122
        mchunkptr newtop = chunk_plus_offset(oldp, nb);
3123
        set_inuse(m, oldp, nb);
3124
        newtop->head = newtopsize |PINUSE_BIT;
3125
        m->top = newtop;
3126
        m->topsize = newtopsize;
3127
        newp = oldp;
3128
      }
3129
    }
3130
    else {
3131
      USAGE_ERROR_ACTION(m, oldmem);
3132
      POSTACTION(m);
3133
      return 0;
3134
    }
3135
 
3136
    POSTACTION(m);
3137
 
3138
    if (newp != 0) {
3139
      if (extra != 0) {
3140
        internal_free(m, extra);
3141
      }
3142
      check_inuse_chunk(m, newp);
3143
      return chunk2mem(newp);
3144
    }
3145
    else {
3146
      void* newmem = internal_malloc(m, bytes);
3147
      if (newmem != 0) {
3148
        size_t oc = oldsize - overhead_for(oldp);
3149
        memcpy(newmem, oldmem, (oc < bytes)? oc : bytes);
3150
        internal_free(m, oldmem);
3151
      }
3152
      return newmem;
3153
    }
3154
  }
3155
  return 0;
3156
}
3157
 
3158
/* --------------------------- memalign support -------------------------- */
3159
 
3160
static void* internal_memalign(mstate m, size_t alignment, size_t bytes) {
3161
  if (alignment <= MALLOC_ALIGNMENT)    /* Can just use malloc */
3162
    return internal_malloc(m, bytes);
3163
  if (alignment <  MIN_CHUNK_SIZE) /* must be at least a minimum chunk size */
3164
    alignment = MIN_CHUNK_SIZE;
3165
  if ((alignment & (alignment-SIZE_T_ONE)) != 0) {/* Ensure a power of 2 */
3166
    size_t a = MALLOC_ALIGNMENT << 1;
3167
    while (a < alignment) a <<= 1;
3168
    alignment = a;
3169
  }
3170
 
3171
  if (bytes >= MAX_REQUEST - alignment) {
3172
    if (m != 0)  { /* Test isn't needed but avoids compiler warning */
3173
      MALLOC_FAILURE_ACTION;
3174
    }
3175
  }
3176
  else {
3177
    size_t nb = request2size(bytes);
3178
    size_t req = nb + alignment + MIN_CHUNK_SIZE - CHUNK_OVERHEAD;
3179
    char* mem = (char*)internal_malloc(m, req);
3180
    if (mem != 0) {
3181
      void* leader = 0;
3182
      void* trailer = 0;
3183
      mchunkptr p = mem2chunk(mem);
3184
 
3185
      if (PREACTION(m)) return 0;
3186
      if ((((size_t)(mem)) % alignment) != 0) { /* misaligned */
3187
        /*
3188
          Find an aligned spot inside chunk.  Since we need to give
3189
          back leading space in a chunk of at least MIN_CHUNK_SIZE, if
3190
          the first calculation places us at a spot with less than
3191
          MIN_CHUNK_SIZE leader, we can move to the next aligned spot.
3192
          We've allocated enough total room so that this is always
3193
          possible.
3194
        */
3195
        char* br = (char*)mem2chunk((size_t)(((size_t)(mem +
3196
                                                       alignment -
3197
                                                       SIZE_T_ONE)) &
3198
                                             -alignment));
3199
        char* pos = ((size_t)(br - (char*)(p)) >= MIN_CHUNK_SIZE)?
3200
          br : br+alignment;
3201
        mchunkptr newp = (mchunkptr)pos;
3202
        size_t leadsize = pos - (char*)(p);
3203
        size_t newsize = chunksize(p) - leadsize;
3204
 
3205
        if (is_mmapped(p)) { /* For mmapped chunks, just adjust offset */
3206
          newp->prev_foot = p->prev_foot + leadsize;
3207
          newp->head = (newsize|CINUSE_BIT);
3208
        }
3209
        else { /* Otherwise, give back leader, use the rest */
3210
          set_inuse(m, newp, newsize);
3211
          set_inuse(m, p, leadsize);
3212
          leader = chunk2mem(p);
3213
        }
3214
        p = newp;
3215
      }
3216
 
3217
      /* Give back spare room at the end */
3218
      if (!is_mmapped(p)) {
3219
        size_t size = chunksize(p);
3220
        if (size > nb + MIN_CHUNK_SIZE) {
3221
          size_t remainder_size = size - nb;
3222
          mchunkptr remainder = chunk_plus_offset(p, nb);
3223
          set_inuse(m, p, nb);
3224
          set_inuse(m, remainder, remainder_size);
3225
          trailer = chunk2mem(remainder);
3226
        }
3227
      }
3228
 
3229
      assert (chunksize(p) >= nb);
3230
      assert((((size_t)(chunk2mem(p))) % alignment) == 0);
3231
      check_inuse_chunk(m, p);
3232
      POSTACTION(m);
3233
      if (leader != 0) {
3234
        internal_free(m, leader);
3235
      }
3236
      if (trailer != 0) {
3237
        internal_free(m, trailer);
3238
      }
3239
      return chunk2mem(p);
3240
    }
3241
  }
3242
  return 0;
3243
}
3244
 
3245
/* ------------------------ comalloc/coalloc support --------------------- */
3246
 
3247
static void** ialloc(mstate m,
3248
                     size_t n_elements,
3249
                     size_t* sizes,
3250
                     int opts,
3251
                     void* chunks[]) {
3252
  /*
3253
    This provides common support for independent_X routines, handling
3254
    all of the combinations that can result.
3255
 
3256
    The opts arg has:
3257
    bit 0 set if all elements are same size (using sizes[0])
3258
    bit 1 set if elements should be zeroed
3259
  */
3260
 
3261
  size_t    element_size;   /* chunksize of each element, if all same */
3262
  size_t    contents_size;  /* total size of elements */
3263
  size_t    array_size;     /* request size of pointer array */
3264
  void*     mem;            /* malloced aggregate space */
3265
  mchunkptr p;              /* corresponding chunk */
3266
  size_t    remainder_size; /* remaining bytes while splitting */
3267
  void**    marray;         /* either "chunks" or malloced ptr array */
3268
  mchunkptr array_chunk;    /* chunk for malloced ptr array */
3269
  flag_t    was_enabled;    /* to disable mmap */
3270
  size_t    size;
3271
  size_t    i;
3272
 
3273
  /* compute array length, if needed */
3274
  if (chunks != 0) {
3275
    if (n_elements == 0)
3276
      return chunks; /* nothing to do */
3277
    marray = chunks;
3278
    array_size = 0;
3279
  }
3280
  else {
3281
    /* if empty req, must still return chunk representing empty array */
3282
    if (n_elements == 0)
3283
      return (void**)internal_malloc(m, 0);
3284
    marray = 0;
3285
    array_size = request2size(n_elements * (sizeof(void*)));
3286
  }
3287
 
3288
  /* compute total element size */
3289
  if (opts & 0x1) { /* all-same-size */
3290
    element_size = request2size(*sizes);
3291
    contents_size = n_elements * element_size;
3292
  }
3293
  else { /* add up all the sizes */
3294
    element_size = 0;
3295
    contents_size = 0;
3296
    for (i = 0; i != n_elements; ++i)
3297
      contents_size += request2size(sizes[i]);
3298
  }
3299
 
3300
  size = contents_size + array_size;
3301
 
3302
  /*
3303
     Allocate the aggregate chunk.  First disable direct-mmapping so
3304
     malloc won't use it, since we would not be able to later
3305
     free/realloc space internal to a segregated mmap region.
3306
  */
3307
  was_enabled = use_mmap(m);
3308
  disable_mmap(m);
3309
  mem = internal_malloc(m, size - CHUNK_OVERHEAD);
3310
  if (was_enabled)
3311
    enable_mmap(m);
3312
  if (mem == 0)
3313
    return 0;
3314
 
3315
  if (PREACTION(m)) return 0;
3316
  p = mem2chunk(mem);
3317
  remainder_size = chunksize(p);
3318
 
3319
  assert(!is_mmapped(p));
3320
 
3321
  if (opts & 0x2) {       /* optionally clear the elements */
3322
    memset((size_t*)mem, 0, remainder_size - SIZE_T_SIZE - array_size);
3323
  }
3324
 
3325
  /* If not provided, allocate the pointer array as final part of chunk */
3326
  if (marray == 0) {
3327
    size_t  array_chunk_size;
3328
    array_chunk = chunk_plus_offset(p, contents_size);
3329
    array_chunk_size = remainder_size - contents_size;
3330
    marray = (void**) (chunk2mem(array_chunk));
3331
    set_size_and_pinuse_of_inuse_chunk(m, array_chunk, array_chunk_size);
3332
    remainder_size = contents_size;
3333
  }
3334
 
3335
  /* split out elements */
3336
  for (i = 0; ; ++i) {
3337
    marray[i] = chunk2mem(p);
3338
    if (i != n_elements-1) {
3339
      if (element_size != 0)
3340
        size = element_size;
3341
      else
3342
        size = request2size(sizes[i]);
3343
      remainder_size -= size;
3344
      set_size_and_pinuse_of_inuse_chunk(m, p, size);
3345
      p = chunk_plus_offset(p, size);
3346
    }
3347
    else { /* the final element absorbs any overallocation slop */
3348
      set_size_and_pinuse_of_inuse_chunk(m, p, remainder_size);
3349
      break;
3350
    }
3351
  }
3352
 
3353
#if DEBUG
3354
  if (marray != chunks) {
3355
    /* final element must have exactly exhausted chunk */
3356
    if (element_size != 0) {
3357
      assert(remainder_size == element_size);
3358
    }
3359
    else {
3360
      assert(remainder_size == request2size(sizes[i]));
3361
    }
3362
    check_inuse_chunk(m, mem2chunk(marray));
3363
  }
3364
  for (i = 0; i != n_elements; ++i)
3365
    check_inuse_chunk(m, mem2chunk(marray[i]));
3366
 
3367
#endif /* DEBUG */
3368
 
3369
  POSTACTION(m);
3370
  return marray;
3371
}
3372
 
3373
 
3374
/* -------------------------- public routines ---------------------------- */
3375
 
3376
#if !ONLY_MSPACES
3377
 
3378
void* dlmalloc(size_t bytes) {
3379
  /*
3380
     Basic algorithm:
3381
     If a small request (< 256 bytes minus per-chunk overhead):
3382
       1. If one exists, use a remainderless chunk in associated smallbin.
3383
          (Remainderless means that there are too few excess bytes to
3384
          represent as a chunk.)
3385
       2. If it is big enough, use the dv chunk, which is normally the
3386
          chunk adjacent to the one used for the most recent small request.
3387
       3. If one exists, split the smallest available chunk in a bin,
3388
          saving remainder in dv.
3389
       4. If it is big enough, use the top chunk.
3390
       5. If available, get memory from system and use it
3391
     Otherwise, for a large request:
3392
       1. Find the smallest available binned chunk that fits, and use it
3393
          if it is better fitting than dv chunk, splitting if necessary.
3394
       2. If better fitting than any binned chunk, use the dv chunk.
3395
       3. If it is big enough, use the top chunk.
3396
       4. If request size >= mmap threshold, try to directly mmap this chunk.
3397
       5. If available, get memory from system and use it
3398
 
3399
     The ugly goto's here ensure that postaction occurs along all paths.
3400
  */
3401
 
3402
  if (!PREACTION(gm)) {
3403
    void* mem;
3404
    size_t nb;
3405
    if (bytes <= MAX_SMALL_REQUEST) {
3406
      bindex_t idx;
3407
      binmap_t smallbits;
3408
      nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes);
3409
      idx = small_index(nb);
3410
      smallbits = gm->smallmap >> idx;
3411
 
3412
      if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */
3413
        mchunkptr b, p;
3414
        idx += ~smallbits & 1;       /* Uses next bin if idx empty */
3415
        b = smallbin_at(gm, idx);
3416
        p = b->fd;
3417
        assert(chunksize(p) == small_index2size(idx));
3418
        unlink_first_small_chunk(gm, b, p, idx);
3419
        set_inuse_and_pinuse(gm, p, small_index2size(idx));
3420
        mem = chunk2mem(p);
3421
        check_malloced_chunk(gm, mem, nb);
3422
        goto postaction;
3423
      }
3424
 
3425
      else if (nb > gm->dvsize) {
3426
        if (smallbits != 0) { /* Use chunk in next nonempty smallbin */
3427
          mchunkptr b, p, r;
3428
          size_t rsize;
3429
          bindex_t i;
3430
          binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx));
3431
          binmap_t leastbit = least_bit(leftbits);
3432
          compute_bit2idx(leastbit, i);
3433
          b = smallbin_at(gm, i);
3434
          p = b->fd;
3435
          assert(chunksize(p) == small_index2size(i));
3436
          unlink_first_small_chunk(gm, b, p, i);
3437
          rsize = small_index2size(i) - nb;
3438
          /* Fit here cannot be remainderless if 4byte sizes */
3439
          if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE)
3440
            set_inuse_and_pinuse(gm, p, small_index2size(i));
3441
          else {
3442
            set_size_and_pinuse_of_inuse_chunk(gm, p, nb);
3443
            r = chunk_plus_offset(p, nb);
3444
            set_size_and_pinuse_of_free_chunk(r, rsize);
3445
            replace_dv(gm, r, rsize);
3446
          }
3447
          mem = chunk2mem(p);
3448
          check_malloced_chunk(gm, mem, nb);
3449
          goto postaction;
3450
        }
3451
 
3452
        else if (gm->treemap != 0 && (mem = tmalloc_small(gm, nb)) != 0) {
3453
          check_malloced_chunk(gm, mem, nb);
3454
          goto postaction;
3455
        }
3456
      }
3457
    }
3458
    else if (bytes >= MAX_REQUEST)
3459
      nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */
3460
    else {
3461
      nb = pad_request(bytes);
3462
      if (gm->treemap != 0 && (mem = tmalloc_large(gm, nb)) != 0) {
3463
        check_malloced_chunk(gm, mem, nb);
3464
        goto postaction;
3465
      }
3466
    }
3467
 
3468
    if (nb <= gm->dvsize) {
3469
      size_t rsize = gm->dvsize - nb;
3470
      mchunkptr p = gm->dv;
3471
      if (rsize >= MIN_CHUNK_SIZE) { /* split dv */
3472
        mchunkptr r = gm->dv = chunk_plus_offset(p, nb);
3473
        gm->dvsize = rsize;
3474
        set_size_and_pinuse_of_free_chunk(r, rsize);
3475
        set_size_and_pinuse_of_inuse_chunk(gm, p, nb);
3476
      }
3477
      else { /* exhaust dv */
3478
        size_t dvs = gm->dvsize;
3479
        gm->dvsize = 0;
3480
        gm->dv = 0;
3481
        set_inuse_and_pinuse(gm, p, dvs);
3482
      }
3483
      mem = chunk2mem(p);
3484
      check_malloced_chunk(gm, mem, nb);
3485
      goto postaction;
3486
    }
3487
 
3488
    else if (nb < gm->topsize) { /* Split top */
3489
      size_t rsize = gm->topsize -= nb;
3490
      mchunkptr p = gm->top;
3491
      mchunkptr r = gm->top = chunk_plus_offset(p, nb);
3492
      r->head = rsize | PINUSE_BIT;
3493
      set_size_and_pinuse_of_inuse_chunk(gm, p, nb);
3494
      mem = chunk2mem(p);
3495
      check_top_chunk(gm, gm->top);
3496
      check_malloced_chunk(gm, mem, nb);
3497
      goto postaction;
3498
    }
3499
 
3500
    mem = sys_alloc(gm, nb);
3501
 
3502
  postaction:
3503
    POSTACTION(gm);
3504
    return mem;
3505
  }
3506
 
3507
  return 0;
3508
}
3509
 
3510
void dlfree(void* mem) {
3511
  /*
3512
     Consolidate freed chunks with preceeding or succeeding bordering
3513
     free chunks, if they exist, and then place in a bin.  Intermixed
3514
     with special cases for top, dv, mmapped chunks, and usage errors.
3515
  */
3516
 
3517
  if (mem != 0) {
3518
    mchunkptr p  = mem2chunk(mem);
3519
#if FOOTERS
3520
    mstate fm = get_mstate_for(p);
3521
    if (!ok_magic(fm)) {
3522
      USAGE_ERROR_ACTION(fm, p);
3523
      return;
3524
    }
3525
#else /* FOOTERS */
3526
#define fm gm
3527
#endif /* FOOTERS */
3528
    if (!PREACTION(fm)) {
3529
      check_inuse_chunk(fm, p);
3530
      if (RTCHECK(ok_address(fm, p) && ok_cinuse(p))) {
3531
        size_t psize = chunksize(p);
3532
        mchunkptr next = chunk_plus_offset(p, psize);
3533
        if (!pinuse(p)) {
3534
          size_t prevsize = p->prev_foot;
3535
          if ((prevsize & IS_MMAPPED_BIT) != 0) {
3536
            prevsize &= ~IS_MMAPPED_BIT;
3537
            psize += prevsize + MMAP_FOOT_PAD;
3538
            if (CALL_MUNMAP((char*)p - prevsize, psize) == 0)
3539
              fm->footprint -= psize;
3540
            goto postaction;
3541
          }
3542
          else {
3543
            mchunkptr prev = chunk_minus_offset(p, prevsize);
3544
            psize += prevsize;
3545
            p = prev;
3546
            if (RTCHECK(ok_address(fm, prev))) { /* consolidate backward */
3547
              if (p != fm->dv) {
3548
                unlink_chunk(fm, p, prevsize);
3549
              }
3550
              else if ((next->head & INUSE_BITS) == INUSE_BITS) {
3551
                fm->dvsize = psize;
3552
                set_free_with_pinuse(p, psize, next);
3553
                goto postaction;
3554
              }
3555
            }
3556
            else
3557
              goto erroraction;
3558
          }
3559
        }
3560
 
3561
        if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) {
3562
          if (!cinuse(next)) {  /* consolidate forward */
3563
            if (next == fm->top) {
3564
              size_t tsize = fm->topsize += psize;
3565
              fm->top = p;
3566
              p->head = tsize | PINUSE_BIT;
3567
              if (p == fm->dv) {
3568
                fm->dv = 0;
3569
                fm->dvsize = 0;
3570
              }
3571
              if (should_trim(fm, tsize))
3572
                sys_trim(fm, 0);
3573
              goto postaction;
3574
            }
3575
            else if (next == fm->dv) {
3576
              size_t dsize = fm->dvsize += psize;
3577
              fm->dv = p;
3578
              set_size_and_pinuse_of_free_chunk(p, dsize);
3579
              goto postaction;
3580
            }
3581
            else {
3582
              size_t nsize = chunksize(next);
3583
              psize += nsize;
3584
              unlink_chunk(fm, next, nsize);
3585
              set_size_and_pinuse_of_free_chunk(p, psize);
3586
              if (p == fm->dv) {
3587
                fm->dvsize = psize;
3588
                goto postaction;
3589
              }
3590
            }
3591
          }
3592
          else
3593
            set_free_with_pinuse(p, psize, next);
3594
          insert_chunk(fm, p, psize);
3595
          check_free_chunk(fm, p);
3596
          goto postaction;
3597
        }
3598
      }
3599
    erroraction:
3600
      USAGE_ERROR_ACTION(fm, p);
3601
    postaction:
3602
      POSTACTION(fm);
3603
    }
3604
  }
3605
#if !FOOTERS
3606
#undef fm
3607
#endif /* FOOTERS */
3608
}
3609
 
3610
void* dlcalloc(size_t n_elements, size_t elem_size) {
3611
  void* mem;
3612
  size_t req = 0;
3613
  if (n_elements != 0) {
3614
    req = n_elements * elem_size;
3615
    if (((n_elements | elem_size) & ~(size_t)0xffff) &&
3616
        (req / n_elements != elem_size))
3617
      req = MAX_SIZE_T; /* force downstream failure on overflow */
3618
  }
3619
  mem = dlmalloc(req);
3620
  if (mem != 0 && calloc_must_clear(mem2chunk(mem)))
3621
    memset(mem, 0, req);
3622
  return mem;
3623
}
3624
 
3625
void* dlrealloc(void* oldmem, size_t bytes) {
3626
  if (oldmem == 0)
3627
    return dlmalloc(bytes);
3628
#ifdef REALLOC_ZERO_BYTES_FREES
3629
  if (bytes == 0) {
3630
    dlfree(oldmem);
3631
    return 0;
3632
  }
3633
#endif /* REALLOC_ZERO_BYTES_FREES */
3634
  else {
3635
#if ! FOOTERS
3636
    mstate m = gm;
3637
#else /* FOOTERS */
3638
    mstate m = get_mstate_for(mem2chunk(oldmem));
3639
    if (!ok_magic(m)) {
3640
      USAGE_ERROR_ACTION(m, oldmem);
3641
      return 0;
3642
    }
3643
#endif /* FOOTERS */
3644
    return internal_realloc(m, oldmem, bytes);
3645
  }
3646
}
3647
 
3648
void* dlmemalign(size_t alignment, size_t bytes) {
3649
  return internal_memalign(gm, alignment, bytes);
3650
}
3651
 
3652
void** dlindependent_calloc(size_t n_elements, size_t elem_size,
3653
                                 void* chunks[]) {
3654
  size_t sz = elem_size; /* serves as 1-element array */
3655
  return ialloc(gm, n_elements, &sz, 3, chunks);
3656
}
3657
 
3658
void** dlindependent_comalloc(size_t n_elements, size_t sizes[],
3659
                                   void* chunks[]) {
3660
  return ialloc(gm, n_elements, sizes, 0, chunks);
3661
}
3662
 
3663
void* dlvalloc(size_t bytes) {
3664
  size_t pagesz;
3665
  init_mparams();
3666
  pagesz = mparams.page_size;
3667
  return dlmemalign(pagesz, bytes);
3668
}
3669
 
3670
void* dlpvalloc(size_t bytes) {
3671
  size_t pagesz;
3672
  init_mparams();
3673
  pagesz = mparams.page_size;
3674
  return dlmemalign(pagesz, (bytes + pagesz - SIZE_T_ONE) & ~(pagesz - SIZE_T_ONE));
3675
}
3676
 
3677
int dlmalloc_trim(size_t pad) {
3678
  int result = 0;
3679
  if (!PREACTION(gm)) {
3680
    result = sys_trim(gm, pad);
3681
    POSTACTION(gm);
3682
  }
3683
  return result;
3684
}
3685
 
3686
size_t dlmalloc_footprint(void) {
3687
  return gm->footprint;
3688
}
3689
 
3690
size_t dlmalloc_max_footprint(void) {
3691
  return gm->max_footprint;
3692
}
3693
 
3694
#if !NO_MALLINFO
3695
struct mallinfo dlmallinfo(void) {
3696
  return internal_mallinfo(gm);
3697
}
3698
#endif /* NO_MALLINFO */
3699
 
3700
void dlmalloc_stats() {
3701
  internal_malloc_stats(gm);
3702
}
3703
 
3704
size_t dlmalloc_usable_size(void* mem) {
3705
  if (mem != 0) {
3706
    mchunkptr p = mem2chunk(mem);
3707
    if (cinuse(p))
3708
      return chunksize(p) - overhead_for(p);
3709
  }
3710
  return 0;
3711
}
3712
 
3713
int dlmallopt(int param_number, int value) {
3714
  return change_mparam(param_number, value);
3715
}
3716
 
3717
#endif /* !ONLY_MSPACES */
3718
 
3719
/* ----------------------------- user mspaces ---------------------------- */
3720
 
3721
#if MSPACES
3722
 
3723
static mstate init_user_mstate(char* tbase, size_t tsize) {
3724
  size_t msize = pad_request(sizeof(struct malloc_state));
3725
  mchunkptr mn;
3726
  mchunkptr msp = align_as_chunk(tbase);
3727
  mstate m = (mstate)(chunk2mem(msp));
3728
  memset(m, 0, msize);
3729
  INITIAL_LOCK(&m->mutex);
3730
  msp->head = (msize|PINUSE_BIT|CINUSE_BIT);
3731
  m->seg.base = m->least_addr = tbase;
3732
  m->seg.size = m->footprint = m->max_footprint = tsize;
3733
  m->magic = mparams.magic;
3734
  m->mflags = mparams.default_mflags;
3735
  disable_contiguous(m);
3736
  init_bins(m);
3737
  mn = next_chunk(mem2chunk(m));
3738
  init_top(m, mn, (size_t)((tbase + tsize) - (char*)mn) - TOP_FOOT_SIZE);
3739
  check_top_chunk(m, m->top);
3740
  return m;
3741
}
3742
 
3743
mspace create_mspace(size_t capacity, int locked) {
3744
  mstate m = 0;
3745
  size_t msize = pad_request(sizeof(struct malloc_state));
3746
  init_mparams(); /* Ensure pagesize etc initialized */
3747
 
3748
  if (capacity < (size_t) -(msize + TOP_FOOT_SIZE + mparams.page_size)) {
3749
    size_t rs = ((capacity == 0)? mparams.granularity :
3750
                 (capacity + TOP_FOOT_SIZE + msize));
3751
    size_t tsize = granularity_align(rs);
3752
    char* tbase = (char*)(CALL_MMAP(tsize));
3753
    if (tbase != CMFAIL) {
3754
      m = init_user_mstate(tbase, tsize);
3755
      m->seg.sflags = IS_MMAPPED_BIT;
3756
      set_lock(m, locked);
3757
    }
3758
  }
3759
  return (mspace)m;
3760
}
3761
 
3762
mspace create_mspace_with_base(void* base, size_t capacity, int locked) {
3763
  mstate m = 0;
3764
  size_t msize = pad_request(sizeof(struct malloc_state));
3765
  init_mparams(); /* Ensure pagesize etc initialized */
3766
 
3767
  if (capacity > msize + TOP_FOOT_SIZE &&
3768
      capacity < (size_t) -(msize + TOP_FOOT_SIZE + mparams.page_size)) {
3769
    m = init_user_mstate((char*)base, capacity);
3770
    m->seg.sflags = EXTERN_BIT;
3771
    set_lock(m, locked);
3772
  }
3773
  return (mspace)m;
3774
}
3775
 
3776
size_t destroy_mspace(mspace msp) {
3777
  size_t freed = 0;
3778
  mstate ms = (mstate)msp;
3779
  if (ok_magic(ms)) {
3780
    msegmentptr sp = &ms->seg;
3781
    while (sp != 0) {
3782
      char* base = sp->base;
3783
      size_t size = sp->size;
3784
      flag_t flag = sp->sflags;
3785
      sp = sp->next;
3786
      if ((flag & IS_MMAPPED_BIT) && !(flag & EXTERN_BIT) &&
3787
          CALL_MUNMAP(base, size) == 0)
3788
        freed += size;
3789
    }
3790
  }
3791
  else {
3792
    USAGE_ERROR_ACTION(ms,ms);
3793
  }
3794
  return freed;
3795
}
3796
 
3797
/*
3798
  mspace versions of routines are near-clones of the global
3799
  versions. This is not so nice but better than the alternatives.
3800
*/
3801
 
3802
 
3803
void* mspace_malloc(mspace msp, size_t bytes) {
3804
  mstate ms = (mstate)msp;
3805
  if (!ok_magic(ms)) {
3806
    USAGE_ERROR_ACTION(ms,ms);
3807
    return 0;
3808
  }
3809
  if (!PREACTION(ms)) {
3810
    void* mem;
3811
    size_t nb;
3812
    if (bytes <= MAX_SMALL_REQUEST) {
3813
      bindex_t idx;
3814
      binmap_t smallbits;
3815
      nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes);
3816
      idx = small_index(nb);
3817
      smallbits = ms->smallmap >> idx;
3818
 
3819
      if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */
3820
        mchunkptr b, p;
3821
        idx += ~smallbits & 1;       /* Uses next bin if idx empty */
3822
        b = smallbin_at(ms, idx);
3823
        p = b->fd;
3824
        assert(chunksize(p) == small_index2size(idx));
3825
        unlink_first_small_chunk(ms, b, p, idx);
3826
        set_inuse_and_pinuse(ms, p, small_index2size(idx));
3827
        mem = chunk2mem(p);
3828
        check_malloced_chunk(ms, mem, nb);
3829
        goto postaction;
3830
      }
3831
 
3832
      else if (nb > ms->dvsize) {
3833
        if (smallbits != 0) { /* Use chunk in next nonempty smallbin */
3834
          mchunkptr b, p, r;
3835
          size_t rsize;
3836
          bindex_t i;
3837
          binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx));
3838
          binmap_t leastbit = least_bit(leftbits);
3839
          compute_bit2idx(leastbit, i);
3840
          b = smallbin_at(ms, i);
3841
          p = b->fd;
3842
          assert(chunksize(p) == small_index2size(i));
3843
          unlink_first_small_chunk(ms, b, p, i);
3844
          rsize = small_index2size(i) - nb;
3845
          /* Fit here cannot be remainderless if 4byte sizes */
3846
          if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE)
3847
            set_inuse_and_pinuse(ms, p, small_index2size(i));
3848
          else {
3849
            set_size_and_pinuse_of_inuse_chunk(ms, p, nb);
3850
            r = chunk_plus_offset(p, nb);
3851
            set_size_and_pinuse_of_free_chunk(r, rsize);
3852
            replace_dv(ms, r, rsize);
3853
          }
3854
          mem = chunk2mem(p);
3855
          check_malloced_chunk(ms, mem, nb);
3856
          goto postaction;
3857
        }
3858
 
3859
        else if (ms->treemap != 0 && (mem = tmalloc_small(ms, nb)) != 0) {
3860
          check_malloced_chunk(ms, mem, nb);
3861
          goto postaction;
3862
        }
3863
      }
3864
    }
3865
    else if (bytes >= MAX_REQUEST)
3866
      nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */
3867
    else {
3868
      nb = pad_request(bytes);
3869
      if (ms->treemap != 0 && (mem = tmalloc_large(ms, nb)) != 0) {
3870
        check_malloced_chunk(ms, mem, nb);
3871
        goto postaction;
3872
      }
3873
    }
3874
 
3875
    if (nb <= ms->dvsize) {
3876
      size_t rsize = ms->dvsize - nb;
3877
      mchunkptr p = ms->dv;
3878
      if (rsize >= MIN_CHUNK_SIZE) { /* split dv */
3879
        mchunkptr r = ms->dv = chunk_plus_offset(p, nb);
3880
        ms->dvsize = rsize;
3881
        set_size_and_pinuse_of_free_chunk(r, rsize);
3882
        set_size_and_pinuse_of_inuse_chunk(ms, p, nb);
3883
      }
3884
      else { /* exhaust dv */
3885
        size_t dvs = ms->dvsize;
3886
        ms->dvsize = 0;
3887
        ms->dv = 0;
3888
        set_inuse_and_pinuse(ms, p, dvs);
3889
      }
3890
      mem = chunk2mem(p);
3891
      check_malloced_chunk(ms, mem, nb);
3892
      goto postaction;
3893
    }
3894
 
3895
    else if (nb < ms->topsize) { /* Split top */
3896
      size_t rsize = ms->topsize -= nb;
3897
      mchunkptr p = ms->top;
3898
      mchunkptr r = ms->top = chunk_plus_offset(p, nb);
3899
      r->head = rsize | PINUSE_BIT;
3900
      set_size_and_pinuse_of_inuse_chunk(ms, p, nb);
3901
      mem = chunk2mem(p);
3902
      check_top_chunk(ms, ms->top);
3903
      check_malloced_chunk(ms, mem, nb);
3904
      goto postaction;
3905
    }
3906
 
3907
    mem = sys_alloc(ms, nb);
3908
 
3909
  postaction:
3910
    POSTACTION(ms);
3911
    return mem;
3912
  }
3913
 
3914
  return 0;
3915
}
3916
 
3917
void mspace_free(mspace msp, void* mem) {
3918
  if (mem != 0) {
3919
    mchunkptr p  = mem2chunk(mem);
3920
#if FOOTERS
3921
    mstate fm = get_mstate_for(p);
3922
#else /* FOOTERS */
3923
    mstate fm = (mstate)msp;
3924
#endif /* FOOTERS */
3925
    if (!ok_magic(fm)) {
3926
      USAGE_ERROR_ACTION(fm, p);
3927
      return;
3928
    }
3929
    if (!PREACTION(fm)) {
3930
      check_inuse_chunk(fm, p);
3931
      if (RTCHECK(ok_address(fm, p) && ok_cinuse(p))) {
3932
        size_t psize = chunksize(p);
3933
        mchunkptr next = chunk_plus_offset(p, psize);
3934
        if (!pinuse(p)) {
3935
          size_t prevsize = p->prev_foot;
3936
          if ((prevsize & IS_MMAPPED_BIT) != 0) {
3937
            prevsize &= ~IS_MMAPPED_BIT;
3938
            psize += prevsize + MMAP_FOOT_PAD;
3939
            if (CALL_MUNMAP((char*)p - prevsize, psize) == 0)
3940
              fm->footprint -= psize;
3941
            goto postaction;
3942
          }
3943
          else {
3944
            mchunkptr prev = chunk_minus_offset(p, prevsize);
3945
            psize += prevsize;
3946
            p = prev;
3947
            if (RTCHECK(ok_address(fm, prev))) { /* consolidate backward */
3948
              if (p != fm->dv) {
3949
                unlink_chunk(fm, p, prevsize);
3950
              }
3951
              else if ((next->head & INUSE_BITS) == INUSE_BITS) {
3952
                fm->dvsize = psize;
3953
                set_free_with_pinuse(p, psize, next);
3954
                goto postaction;
3955
              }
3956
            }
3957
            else
3958
              goto erroraction;
3959
          }
3960
        }
3961
 
3962
        if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) {
3963
          if (!cinuse(next)) {  /* consolidate forward */
3964
            if (next == fm->top) {
3965
              size_t tsize = fm->topsize += psize;
3966
              fm->top = p;
3967
              p->head = tsize | PINUSE_BIT;
3968
              if (p == fm->dv) {
3969
                fm->dv = 0;
3970
                fm->dvsize = 0;
3971
              }
3972
              if (should_trim(fm, tsize))
3973
                sys_trim(fm, 0);
3974
              goto postaction;
3975
            }
3976
            else if (next == fm->dv) {
3977
              size_t dsize = fm->dvsize += psize;
3978
              fm->dv = p;
3979
              set_size_and_pinuse_of_free_chunk(p, dsize);
3980
              goto postaction;
3981
            }
3982
            else {
3983
              size_t nsize = chunksize(next);
3984
              psize += nsize;
3985
              unlink_chunk(fm, next, nsize);
3986
              set_size_and_pinuse_of_free_chunk(p, psize);
3987
              if (p == fm->dv) {
3988
                fm->dvsize = psize;
3989
                goto postaction;
3990
              }
3991
            }
3992
          }
3993
          else
3994
            set_free_with_pinuse(p, psize, next);
3995
          insert_chunk(fm, p, psize);
3996
          check_free_chunk(fm, p);
3997
          goto postaction;
3998
        }
3999
      }
4000
    erroraction:
4001
      USAGE_ERROR_ACTION(fm, p);
4002
    postaction:
4003
      POSTACTION(fm);
4004
    }
4005
  }
4006
}
4007
 
4008
void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size) {
4009
  void* mem;
4010
  size_t req = 0;
4011
  mstate ms = (mstate)msp;
4012
  if (!ok_magic(ms)) {
4013
    USAGE_ERROR_ACTION(ms,ms);
4014
    return 0;
4015
  }
4016
  if (n_elements != 0) {
4017
    req = n_elements * elem_size;
4018
    if (((n_elements | elem_size) & ~(size_t)0xffff) &&
4019
        (req / n_elements != elem_size))
4020
      req = MAX_SIZE_T; /* force downstream failure on overflow */
4021
  }
4022
  mem = internal_malloc(ms, req);
4023
  if (mem != 0 && calloc_must_clear(mem2chunk(mem)))
4024
    memset(mem, 0, req);
4025
  return mem;
4026
}
4027
 
4028
void* mspace_realloc(mspace msp, void* oldmem, size_t bytes) {
4029
  if (oldmem == 0)
4030
    return mspace_malloc(msp, bytes);
4031
#ifdef REALLOC_ZERO_BYTES_FREES
4032
  if (bytes == 0) {
4033
    mspace_free(msp, oldmem);
4034
    return 0;
4035
  }
4036
#endif /* REALLOC_ZERO_BYTES_FREES */
4037
  else {
4038
#if FOOTERS
4039
    mchunkptr p  = mem2chunk(oldmem);
4040
    mstate ms = get_mstate_for(p);
4041
#else /* FOOTERS */
4042
    mstate ms = (mstate)msp;
4043
#endif /* FOOTERS */
4044
    if (!ok_magic(ms)) {
4045
      USAGE_ERROR_ACTION(ms,ms);
4046
      return 0;
4047
    }
4048
    return internal_realloc(ms, oldmem, bytes);
4049
  }
4050
}
4051
 
4052
void* mspace_memalign(mspace msp, size_t alignment, size_t bytes) {
4053
  mstate ms = (mstate)msp;
4054
  if (!ok_magic(ms)) {
4055
    USAGE_ERROR_ACTION(ms,ms);
4056
    return 0;
4057
  }
4058
  return internal_memalign(ms, alignment, bytes);
4059
}
4060
 
4061
void** mspace_independent_calloc(mspace msp, size_t n_elements,
4062
                                 size_t elem_size, void* chunks[]) {
4063
  size_t sz = elem_size; /* serves as 1-element array */
4064
  mstate ms = (mstate)msp;
4065
  if (!ok_magic(ms)) {
4066
    USAGE_ERROR_ACTION(ms,ms);
4067
    return 0;
4068
  }
4069
  return ialloc(ms, n_elements, &sz, 3, chunks);
4070
}
4071
 
4072
void** mspace_independent_comalloc(mspace msp, size_t n_elements,
4073
                                   size_t sizes[], void* chunks[]) {
4074
  mstate ms = (mstate)msp;
4075
  if (!ok_magic(ms)) {
4076
    USAGE_ERROR_ACTION(ms,ms);
4077
    return 0;
4078
  }
4079
  return ialloc(ms, n_elements, sizes, 0, chunks);
4080
}
4081
 
4082
int mspace_trim(mspace msp, size_t pad) {
4083
  int result = 0;
4084
  mstate ms = (mstate)msp;
4085
  if (ok_magic(ms)) {
4086
    if (!PREACTION(ms)) {
4087
      result = sys_trim(ms, pad);
4088
      POSTACTION(ms);
4089
    }
4090
  }
4091
  else {
4092
    USAGE_ERROR_ACTION(ms,ms);
4093
  }
4094
  return result;
4095
}
4096
 
4097
void mspace_malloc_stats(mspace msp) {
4098
  mstate ms = (mstate)msp;
4099
  if (ok_magic(ms)) {
4100
    internal_malloc_stats(ms);
4101
  }
4102
  else {
4103
    USAGE_ERROR_ACTION(ms,ms);
4104
  }
4105
}
4106
 
4107
size_t mspace_footprint(mspace msp) {
4108
  size_t result;
4109
  mstate ms = (mstate)msp;
4110
  if (ok_magic(ms)) {
4111
    result = ms->footprint;
4112
  }
4113
  USAGE_ERROR_ACTION(ms,ms);
4114
  return result;
4115
}
4116
 
4117
 
4118
size_t mspace_max_footprint(mspace msp) {
4119
  size_t result;
4120
  mstate ms = (mstate)msp;
4121
  if (ok_magic(ms)) {
4122
    result = ms->max_footprint;
4123
  }
4124
  USAGE_ERROR_ACTION(ms,ms);
4125
  return result;
4126
}
4127
 
4128
 
4129
#if !NO_MALLINFO
4130
struct mallinfo mspace_mallinfo(mspace msp) {
4131
  mstate ms = (mstate)msp;
4132
  if (!ok_magic(ms)) {
4133
    USAGE_ERROR_ACTION(ms,ms);
4134
  }
4135
  return internal_mallinfo(ms);
4136
}
4137
#endif /* NO_MALLINFO */
4138
 
4139
int mspace_mallopt(int param_number, int value) {
4140
  return change_mparam(param_number, value);
4141
}
4142
 
4143
#endif /* MSPACES */
4144
 
4145
/* -------------------- Alternative MORECORE functions ------------------- */
4146
 
4147
/*
4148
  Guidelines for creating a custom version of MORECORE:
4149
 
4150
  * For best performance, MORECORE should allocate in multiples of pagesize.
4151
  * MORECORE may allocate more memory than requested. (Or even less,
4152
      but this will usually result in a malloc failure.)
4153
  * MORECORE must not allocate memory when given argument zero, but
4154
      instead return one past the end address of memory from previous
4155
      nonzero call.
4156
  * For best performance, consecutive calls to MORECORE with positive
4157
      arguments should return increasing addresses, indicating that
4158
      space has been contiguously extended.
4159
  * Even though consecutive calls to MORECORE need not return contiguous
4160
      addresses, it must be OK for malloc'ed chunks to span multiple
4161
      regions in those cases where they do happen to be contiguous.
4162
  * MORECORE need not handle negative arguments -- it may instead
4163
      just return MFAIL when given negative arguments.
4164
      Negative arguments are always multiples of pagesize. MORECORE
4165
      must not misinterpret negative args as large positive unsigned
4166
      args. You can suppress all such calls from even occurring by defining
4167
      MORECORE_CANNOT_TRIM,
4168
 
4169
  As an example alternative MORECORE, here is a custom allocator
4170
  kindly contributed for pre-OSX macOS.  It uses virtually but not
4171
  necessarily physically contiguous non-paged memory (locked in,
4172
  present and won't get swapped out).  You can use it by uncommenting
4173
  this section, adding some #includes, and setting up the appropriate
4174
  defines above:
4175
 
4176
      #define MORECORE osMoreCore
4177
 
4178
  There is also a shutdown routine that should somehow be called for
4179
  cleanup upon program exit.
4180
 
4181
  #define MAX_POOL_ENTRIES 100
4182
  #define MINIMUM_MORECORE_SIZE  (64 * 1024U)
4183
  static int next_os_pool;
4184
  void *our_os_pools[MAX_POOL_ENTRIES];
4185
 
4186
  void *osMoreCore(int size)
4187
  {
4188
    void *ptr = 0;
4189
    static void *sbrk_top = 0;
4190
 
4191
    if (size > 0)
4192
    {
4193
      if (size < MINIMUM_MORECORE_SIZE)
4194
         size = MINIMUM_MORECORE_SIZE;
4195
      if (CurrentExecutionLevel() == kTaskLevel)
4196
         ptr = PoolAllocateResident(size + RM_PAGE_SIZE, 0);
4197
      if (ptr == 0)
4198
      {
4199
        return (void *) MFAIL;
4200
      }
4201
      // save ptrs so they can be freed during cleanup
4202
      our_os_pools[next_os_pool] = ptr;
4203
      next_os_pool++;
4204
      ptr = (void *) ((((size_t) ptr) + RM_PAGE_MASK) & ~RM_PAGE_MASK);
4205
      sbrk_top = (char *) ptr + size;
4206
      return ptr;
4207
    }
4208
    else if (size < 0)
4209
    {
4210
      // we don't currently support shrink behavior
4211
      return (void *) MFAIL;
4212
    }
4213
    else
4214
    {
4215
      return sbrk_top;
4216
    }
4217
  }
4218
 
4219
  // cleanup any allocated memory pools
4220
  // called as last thing before shutting down driver
4221
 
4222
  void osCleanupMem(void)
4223
  {
4224
    void **ptr;
4225
 
4226
    for (ptr = our_os_pools; ptr < &our_os_pools[MAX_POOL_ENTRIES]; ptr++)
4227
      if (*ptr)
4228
      {
4229
         PoolDeallocate(*ptr);
4230
         *ptr = 0;
4231
      }
4232
  }
4233
 
4234
*/
4235
 
4236
 
4237
/* -----------------------------------------------------------------------
4238
History:
4239
    V2.8.3 Thu Sep 22 11:16:32 2005  Doug Lea  (dl at gee)
4240
      * Add max_footprint functions
4241
      * Ensure all appropriate literals are size_t
4242
      * Fix conditional compilation problem for some #define settings
4243
      * Avoid concatenating segments with the one provided
4244
        in create_mspace_with_base
4245
      * Rename some variables to avoid compiler shadowing warnings
4246
      * Use explicit lock initialization.
4247
      * Better handling of sbrk interference.
4248
      * Simplify and fix segment insertion, trimming and mspace_destroy
4249
      * Reinstate REALLOC_ZERO_BYTES_FREES option from 2.7.x
4250
      * Thanks especially to Dennis Flanagan for help on these.
4251
 
4252
    V2.8.2 Sun Jun 12 16:01:10 2005  Doug Lea  (dl at gee)
4253
      * Fix memalign brace error.
4254
 
4255
    V2.8.1 Wed Jun  8 16:11:46 2005  Doug Lea  (dl at gee)
4256
      * Fix improper #endif nesting in C++
4257
      * Add explicit casts needed for C++
4258
 
4259
    V2.8.0 Mon May 30 14:09:02 2005  Doug Lea  (dl at gee)
4260
      * Use trees for large bins
4261
      * Support mspaces
4262
      * Use segments to unify sbrk-based and mmap-based system allocation,
4263
        removing need for emulation on most platforms without sbrk.
4264
      * Default safety checks
4265
      * Optional footer checks. Thanks to William Robertson for the idea.
4266
      * Internal code refactoring
4267
      * Incorporate suggestions and platform-specific changes.
4268
        Thanks to Dennis Flanagan, Colin Plumb, Niall Douglas,
4269
        Aaron Bachmann,  Emery Berger, and others.
4270
      * Speed up non-fastbin processing enough to remove fastbins.
4271
      * Remove useless cfree() to avoid conflicts with other apps.
4272
      * Remove internal memcpy, memset. Compilers handle builtins better.
4273
      * Remove some options that no one ever used and rename others.
4274
 
4275
    V2.7.2 Sat Aug 17 09:07:30 2002  Doug Lea  (dl at gee)
4276
      * Fix malloc_state bitmap array misdeclaration
4277
 
4278
    V2.7.1 Thu Jul 25 10:58:03 2002  Doug Lea  (dl at gee)
4279
      * Allow tuning of FIRST_SORTED_BIN_SIZE
4280
      * Use PTR_UINT as type for all ptr->int casts. Thanks to John Belmonte.
4281
      * Better detection and support for non-contiguousness of MORECORE.
4282
        Thanks to Andreas Mueller, Conal Walsh, and Wolfram Gloger
4283
      * Bypass most of malloc if no frees. Thanks To Emery Berger.
4284
      * Fix freeing of old top non-contiguous chunk im sysmalloc.
4285
      * Raised default trim and map thresholds to 256K.
4286
      * Fix mmap-related #defines. Thanks to Lubos Lunak.
4287
      * Fix copy macros; added LACKS_FCNTL_H. Thanks to Neal Walfield.
4288
      * Branch-free bin calculation
4289
      * Default trim and mmap thresholds now 256K.
4290
 
4291
    V2.7.0 Sun Mar 11 14:14:06 2001  Doug Lea  (dl at gee)
4292
      * Introduce independent_comalloc and independent_calloc.
4293
        Thanks to Michael Pachos for motivation and help.
4294
      * Make optional .h file available
4295
      * Allow > 2GB requests on 32bit systems.
4296
      * new WIN32 sbrk, mmap, munmap, lock code from <Walter@GeNeSys-e.de>.
4297
        Thanks also to Andreas Mueller <a.mueller at paradatec.de>,
4298
        and Anonymous.
4299
      * Allow override of MALLOC_ALIGNMENT (Thanks to Ruud Waij for
4300
        helping test this.)
4301
      * memalign: check alignment arg
4302
      * realloc: don't try to shift chunks backwards, since this
4303
        leads to  more fragmentation in some programs and doesn't
4304
        seem to help in any others.
4305
      * Collect all cases in malloc requiring system memory into sysmalloc
4306
      * Use mmap as backup to sbrk
4307
      * Place all internal state in malloc_state
4308
      * Introduce fastbins (although similar to 2.5.1)
4309
      * Many minor tunings and cosmetic improvements
4310
      * Introduce USE_PUBLIC_MALLOC_WRAPPERS, USE_MALLOC_LOCK
4311
      * Introduce MALLOC_FAILURE_ACTION, MORECORE_CONTIGUOUS
4312
        Thanks to Tony E. Bennett <tbennett@nvidia.com> and others.
4313
      * Include errno.h to support default failure action.
4314
 
4315
    V2.6.6 Sun Dec  5 07:42:19 1999  Doug Lea  (dl at gee)
4316
      * return null for negative arguments
4317
      * Added Several WIN32 cleanups from Martin C. Fong <mcfong at yahoo.com>
4318
         * Add 'LACKS_SYS_PARAM_H' for those systems without 'sys/param.h'
4319
          (e.g. WIN32 platforms)
4320
         * Cleanup header file inclusion for WIN32 platforms
4321
         * Cleanup code to avoid Microsoft Visual C++ compiler complaints
4322
         * Add 'USE_DL_PREFIX' to quickly allow co-existence with existing
4323
           memory allocation routines
4324
         * Set 'malloc_getpagesize' for WIN32 platforms (needs more work)
4325
         * Use 'assert' rather than 'ASSERT' in WIN32 code to conform to
4326
           usage of 'assert' in non-WIN32 code
4327
         * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to
4328
           avoid infinite loop
4329
      * Always call 'fREe()' rather than 'free()'
4330
 
4331
    V2.6.5 Wed Jun 17 15:57:31 1998  Doug Lea  (dl at gee)
4332
      * Fixed ordering problem with boundary-stamping
4333
 
4334
    V2.6.3 Sun May 19 08:17:58 1996  Doug Lea  (dl at gee)
4335
      * Added pvalloc, as recommended by H.J. Liu
4336
      * Added 64bit pointer support mainly from Wolfram Gloger
4337
      * Added anonymously donated WIN32 sbrk emulation
4338
      * Malloc, calloc, getpagesize: add optimizations from Raymond Nijssen
4339
      * malloc_extend_top: fix mask error that caused wastage after
4340
        foreign sbrks
4341
      * Add linux mremap support code from HJ Liu
4342
 
4343
    V2.6.2 Tue Dec  5 06:52:55 1995  Doug Lea  (dl at gee)
4344
      * Integrated most documentation with the code.
4345
      * Add support for mmap, with help from
4346
        Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
4347
      * Use last_remainder in more cases.
4348
      * Pack bins using idea from  colin@nyx10.cs.du.edu
4349
      * Use ordered bins instead of best-fit threshhold
4350
      * Eliminate block-local decls to simplify tracing and debugging.
4351
      * Support another case of realloc via move into top
4352
      * Fix error occuring when initial sbrk_base not word-aligned.
4353
      * Rely on page size for units instead of SBRK_UNIT to
4354
        avoid surprises about sbrk alignment conventions.
4355
      * Add mallinfo, mallopt. Thanks to Raymond Nijssen
4356
        (raymond@es.ele.tue.nl) for the suggestion.
4357
      * Add `pad' argument to malloc_trim and top_pad mallopt parameter.
4358
      * More precautions for cases where other routines call sbrk,
4359
        courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
4360
      * Added macros etc., allowing use in linux libc from
4361
        H.J. Lu (hjl@gnu.ai.mit.edu)
4362
      * Inverted this history list
4363
 
4364
    V2.6.1 Sat Dec  2 14:10:57 1995  Doug Lea  (dl at gee)
4365
      * Re-tuned and fixed to behave more nicely with V2.6.0 changes.
4366
      * Removed all preallocation code since under current scheme
4367
        the work required to undo bad preallocations exceeds
4368
        the work saved in good cases for most test programs.
4369
      * No longer use return list or unconsolidated bins since
4370
        no scheme using them consistently outperforms those that don't
4371
        given above changes.
4372
      * Use best fit for very large chunks to prevent some worst-cases.
4373
      * Added some support for debugging
4374
 
4375
    V2.6.0 Sat Nov  4 07:05:23 1995  Doug Lea  (dl at gee)
4376
      * Removed footers when chunks are in use. Thanks to
4377
        Paul Wilson (wilson@cs.texas.edu) for the suggestion.
4378
 
4379
    V2.5.4 Wed Nov  1 07:54:51 1995  Doug Lea  (dl at gee)
4380
      * Added malloc_trim, with help from Wolfram Gloger
4381
        (wmglo@Dent.MED.Uni-Muenchen.DE).
4382
 
4383
    V2.5.3 Tue Apr 26 10:16:01 1994  Doug Lea  (dl at g)
4384
 
4385
    V2.5.2 Tue Apr  5 16:20:40 1994  Doug Lea  (dl at g)
4386
      * realloc: try to expand in both directions
4387
      * malloc: swap order of clean-bin strategy;
4388
      * realloc: only conditionally expand backwards
4389
      * Try not to scavenge used bins
4390
      * Use bin counts as a guide to preallocation
4391
      * Occasionally bin return list chunks in first scan
4392
      * Add a few optimizations from colin@nyx10.cs.du.edu
4393
 
4394
    V2.5.1 Sat Aug 14 15:40:43 1993  Doug Lea  (dl at g)
4395
      * faster bin computation & slightly different binning
4396
      * merged all consolidations to one part of malloc proper
4397
         (eliminating old malloc_find_space & malloc_clean_bin)
4398
      * Scan 2 returns chunks (not just 1)
4399
      * Propagate failure in realloc if malloc returns 0
4400
      * Add stuff to allow compilation on non-ANSI compilers
4401
          from kpv@research.att.com
4402
 
4403
    V2.5 Sat Aug  7 07:41:59 1993  Doug Lea  (dl at g.oswego.edu)
4404
      * removed potential for odd address access in prev_chunk
4405
      * removed dependency on getpagesize.h
4406
      * misc cosmetics and a bit more internal documentation
4407
      * anticosmetics: mangled names in macros to evade debugger strangeness
4408
      * tested on sparc, hp-700, dec-mips, rs6000
4409
          with gcc & native cc (hp, dec only) allowing
4410
          Detlefs & Zorn comparison study (in SIGPLAN Notices.)
4411
 
4412
    Trial version Fri Aug 28 13:14:29 1992  Doug Lea  (dl at g.oswego.edu)
4413
      * Based loosely on libg++-1.2X malloc. (It retains some of the overall
4414
         structure of old version,  but most details differ.)
4415
 
4416
*/
1656 cejka 4417
 
4418
/** @}
4419
 */
4420