The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
10347 ripley 1
/*
26855 ripley 2
  This is a version (aka dlmalloc) of malloc/free/realloc written by
3
  Doug Lea and released to the public domain.  Use, modify, and
4
  redistribute this code without permission or acknowledgement in any
5
  way you wish.  Send questions, comments, complaints, performance
6
  data, etc to dl@cs.oswego.edu
10347 ripley 7
 
26855 ripley 8
* VERSION 2.7.2 Sat Aug 17 09:07:30 2002  Doug Lea  (dl at gee)
10347 ripley 9
 
10
   Note: There may be an updated version of this malloc obtainable at
26855 ripley 11
           ftp://gee.cs.oswego.edu/pub/misc/malloc.c
10347 ripley 12
         Check before installing!
13
 
26855 ripley 14
* Quickstart
15
 
16
  This library is all in one file to simplify the most common usage:
17
  ftp it, compile it (-O), and link it into another program. All
18
  of the compile-time options default to reasonable values for use on
19
  most unix platforms. Compile -DWIN32 for reasonable defaults on windows.
20
  You might later want to step through various compile-time and dynamic
21
  tuning options.
22
 
23
  For convenience, an include file for code using this malloc is at:
24
     ftp://gee.cs.oswego.edu/pub/misc/malloc-2.7.1.h
25
  You don't really need this .h file unless you call functions not
26
  defined in your system include files.  The .h file contains only the
27
  excerpts from this file needed for using this malloc on ANSI C/C++
28
  systems, so long as you haven't changed compile-time options about
29
  naming and tuning parameters.  If you do, then you can create your
30
  own malloc.h that does include all settings by cutting at the point
31
  indicated below.
32
 
10347 ripley 33
* Why use this malloc?
34
 
35
  This is not the fastest, most space-conserving, most portable, or
36
  most tunable malloc ever written. However it is among the fastest
37
  while also being among the most space-conserving, portable and tunable.
38
  Consistent balance across these factors results in a good general-purpose
26855 ripley 39
  allocator for malloc-intensive programs.
10347 ripley 40
 
26855 ripley 41
  The main properties of the algorithms are:
42
  * For large (>= 512 bytes) requests, it is a pure best-fit allocator,
43
    with ties normally decided via FIFO (i.e. least recently used).
44
  * For small (<= 64 bytes by default) requests, it is a caching
45
    allocator, that maintains pools of quickly recycled chunks.
46
  * In between, and for combinations of large and small requests, it does
47
    the best it can trying to meet both goals at once.
48
  * For very large requests (>= 128KB by default), it relies on system
49
    memory mapping facilities, if supported.
10347 ripley 50
 
26855 ripley 51
  For a longer but slightly out of date high-level description, see
52
     http://gee.cs.oswego.edu/dl/html/malloc.html
10347 ripley 53
 
26855 ripley 54
  You may already by default be using a C library containing a malloc
55
  that is  based on some version of this malloc (for example in
56
  linux). You might still want to use the one in this file in order to
57
  customize settings or to avoid overheads associated with library
58
  versions.
10347 ripley 59
 
26855 ripley 60
* Contents, described in more detail in "description of public routines" below.
10347 ripley 61
 
26855 ripley 62
  Standard (ANSI/SVID/...)  functions:
63
    malloc(size_t n);
64
    calloc(size_t n_elements, size_t element_size);
65
    free(Void_t* p);
66
    realloc(Void_t* p, size_t n);
67
    memalign(size_t alignment, size_t n);
68
    valloc(size_t n);
69
    mallinfo()
70
    mallopt(int parameter_number, int parameter_value)
10347 ripley 71
 
26855 ripley 72
  Additional functions:
73
    independent_calloc(size_t n_elements, size_t size, Void_t* chunks[]);
74
    independent_comalloc(size_t n_elements, size_t sizes[], Void_t* chunks[]);
75
    pvalloc(size_t n);
76
    cfree(Void_t* p);
77
    malloc_trim(size_t pad);
78
    malloc_usable_size(Void_t* p);
79
    malloc_stats();
10347 ripley 80
 
26855 ripley 81
* Vital statistics:
82
 
83
  Supported pointer representation:       4 or 8 bytes
84
  Supported size_t  representation:       4 or 8 bytes 
10347 ripley 85
       Note that size_t is allowed to be 4 bytes even if pointers are 8.
26855 ripley 86
       You can adjust this by defining INTERNAL_SIZE_T
10347 ripley 87
 
26855 ripley 88
  Alignment:                              2 * sizeof(size_t) (default)
89
       (i.e., 8 byte alignment with 4byte size_t). This suffices for
90
       nearly all current machines and C compilers. However, you can
91
       define MALLOC_ALIGNMENT to be wider than this if necessary.
92
 
93
  Minimum overhead per allocated chunk:   4 or 8 bytes
94
       Each malloced chunk has a hidden word of overhead holding size
10347 ripley 95
       and status information.
96
 
97
  Minimum allocated size: 4-byte ptrs:  16 bytes    (including 4 overhead)
98
                          8-byte ptrs:  24/32 bytes (including, 4/8 overhead)
99
 
100
       When a chunk is freed, 12 (for 4byte ptrs) or 20 (for 8 byte
101
       ptrs but 4 byte size) or 24 (for 8/8) additional bytes are
26855 ripley 102
       needed; 4 (8) for a trailing size field and 8 (16) bytes for
103
       free list pointers. Thus, the minimum allocatable size is
104
       16/24/32 bytes.
10347 ripley 105
 
106
       Even a request for zero bytes (i.e., malloc(0)) returns a
107
       pointer to something of the minimum allocatable size.
108
 
26855 ripley 109
       The maximum overhead wastage (i.e., number of extra bytes
110
       allocated than were requested in malloc) is less than or equal
111
       to the minimum size, except for requests >= mmap_threshold that
112
       are serviced via mmap(), where the worst case wastage is 2 *
113
       sizeof(size_t) bytes plus the remainder from a system page (the
114
       minimal mmap unit); typically 4096 or 8192 bytes.
10347 ripley 115
 
26855 ripley 116
  Maximum allocated size:  4-byte size_t: 2^32 minus about two pages 
117
                           8-byte size_t: 2^64 minus about two pages
118
 
119
       It is assumed that (possibly signed) size_t values suffice to
10347 ripley 120
       represent chunk sizes. `Possibly signed' is due to the fact
121
       that `size_t' may be defined on a system as either a signed or
26855 ripley 122
       an unsigned type. The ISO C standard says that it must be
123
       unsigned, but a few systems are known not to adhere to this.
124
       Additionally, even when size_t is unsigned, sbrk (which is by
125
       default used to obtain memory from system) accepts signed
126
       arguments, and may not be able to handle size_t-wide arguments
127
       with negative sign bit.  Generally, values that would
128
       appear as negative after accounting for overhead and alignment
129
       are supported only via mmap(), which does not have this
130
       limitation.
10347 ripley 131
 
26855 ripley 132
       Requests for sizes outside the allowed range will perform an optional
133
       failure action and then return null. (Requests may also
134
       also fail because a system is out of memory.)
10347 ripley 135
 
26855 ripley 136
  Thread-safety: NOT thread-safe unless USE_MALLOC_LOCK defined
10347 ripley 137
 
26855 ripley 138
       When USE_MALLOC_LOCK is defined, wrappers are created to
139
       surround every public call with either a pthread mutex or
140
       a win32 spinlock (depending on WIN32). This is not
141
       especially fast, and can be a major bottleneck.
142
       It is designed only to provide minimal protection
143
       in concurrent environments, and to provide a basis for
144
       extensions.  If you are using malloc in a concurrent program,
145
       you would be far better off obtaining ptmalloc, which is
146
       derived from a version of this malloc, and is well-tuned for
147
       concurrent programs. (See http://www.malloc.de) Note that
148
       even when USE_MALLOC_LOCK is defined, you can can guarantee
149
       full thread-safety only if no threads acquire memory through 
150
       direct calls to MORECORE or other system-level allocators.
10347 ripley 151
 
26855 ripley 152
  Compliance: I believe it is compliant with the 1997 Single Unix Specification
153
       (See http://www.opennc.org). Also SVID/XPG, ANSI C, and probably 
154
       others as well.
10347 ripley 155
 
156
* Synopsis of compile-time options:
157
 
158
    People have reported using previous versions of this malloc on all
159
    versions of Unix, sometimes by tweaking some of the defines
160
    below. It has been tested most extensively on Solaris and
161
    Linux. It is also reported to work on WIN32 platforms.
26855 ripley 162
    People also report using it in stand-alone embedded systems.
10347 ripley 163
 
26855 ripley 164
    The implementation is in straight, hand-tuned ANSI C.  It is not
165
    at all modular. (Sorry!)  It uses a lot of macros.  To be at all
166
    usable, this code should be compiled using an optimizing compiler
167
    (for example gcc -O3) that can simplify expressions and control
168
    paths. (FAQ: some macros import variables as arguments rather than
169
    declare locals because people reported that some debuggers
170
    otherwise get confused.)
10347 ripley 171
 
26855 ripley 172
    OPTION                     DEFAULT VALUE
10347 ripley 173
 
26855 ripley 174
    Compilation Environment options:
10347 ripley 175
 
26855 ripley 176
    __STD_C                    derived from C compiler defines
177
    WIN32                      NOT defined
178
    HAVE_MEMCPY                defined
179
    USE_MEMCPY                 1 if HAVE_MEMCPY is defined
180
    HAVE_MMAP                  defined as 1 
181
    MMAP_CLEARS                1
182
    HAVE_MREMAP                0 unless linux defined
183
    malloc_getpagesize         derived from system #includes, or 4096 if not
184
    HAVE_USR_INCLUDE_MALLOC_H  NOT defined
185
    LACKS_UNISTD_H             NOT defined unless WIN32
186
    LACKS_SYS_PARAM_H          NOT defined unless WIN32
187
    LACKS_SYS_MMAN_H           NOT defined unless WIN32
188
    LACKS_FCNTL_H              NOT defined
189
 
190
    Changing default word sizes:
191
 
192
    INTERNAL_SIZE_T            size_t
193
    MALLOC_ALIGNMENT           2 * sizeof(INTERNAL_SIZE_T)
194
    PTR_UINT                   unsigned long
195
    CHUNK_SIZE_T               unsigned long
196
 
197
    Configuration and functionality options:
198
 
199
    USE_DL_PREFIX              NOT defined
200
    USE_PUBLIC_MALLOC_WRAPPERS NOT defined
201
    USE_MALLOC_LOCK            NOT defined
202
    DEBUG                      NOT defined
203
    REALLOC_ZERO_BYTES_FREES   NOT defined
204
    MALLOC_FAILURE_ACTION      errno = ENOMEM, if __STD_C defined, else no-op
205
    TRIM_FASTBINS              0
206
    FIRST_SORTED_BIN_SIZE      512
207
 
208
    Options for customizing MORECORE:
209
 
210
    MORECORE                   sbrk
211
    MORECORE_CONTIGUOUS        1 
212
    MORECORE_CANNOT_TRIM       NOT defined
213
    MMAP_AS_MORECORE_SIZE      (1024 * 1024) 
214
 
215
    Tuning options that are also dynamically changeable via mallopt:
216
 
217
    DEFAULT_MXFAST             64
218
    DEFAULT_TRIM_THRESHOLD     256 * 1024
219
    DEFAULT_TOP_PAD            0
220
    DEFAULT_MMAP_THRESHOLD     256 * 1024
221
    DEFAULT_MMAP_MAX           65536
222
 
223
    There are several other #defined constants and macros that you
224
    probably don't want to touch unless you are extending or adapting malloc.
10347 ripley 225
*/
226
 
26855 ripley 227
/*
228
  WIN32 sets up defaults for MS environment and compilers.
229
  Otherwise defaults are for unix.
230
*/
10347 ripley 231
 
26855 ripley 232
/* #define WIN32 */
10347 ripley 233
 
26855 ripley 234
#ifdef WIN32
10347 ripley 235
 
26855 ripley 236
#define WIN32_LEAN_AND_MEAN
237
#include <windows.h>
238
 
239
/* Win32 doesn't supply or need the following headers */
240
/*#define LACKS_UNISTD_H*/
241
#define LACKS_SYS_PARAM_H
242
#define LACKS_SYS_MMAN_H
243
 
244
/* Use the supplied emulation of sbrk */
245
#define MORECORE sbrk
246
#define MORECORE_CONTIGUOUS 0
247
#define MORECORE_FAILURE    ((void*)(-1))
248
 
249
/* Use the supplied emulation of mmap and munmap */
250
#define HAVE_MMAP 1
251
#define MUNMAP_FAILURE  (-1)
252
#define MMAP_CLEARS 1
253
 
254
/* These values don't really matter in windows mmap emulation */
255
#define MAP_PRIVATE 1
256
#define MAP_ANONYMOUS 2
257
#define PROT_READ 1
258
#define PROT_WRITE 2
259
 
260
/* Emulation functions defined at the end of this file */
261
 
262
/* If USE_MALLOC_LOCK, use supplied critical-section-based lock functions */
263
#ifdef USE_MALLOC_LOCK
264
static int slwait(int *sl);
265
static int slrelease(int *sl);
266
#endif
267
 
268
static long getpagesize(void);
269
static long getregionsize(void);
270
static void *sbrk(long size);
271
static void *mmap(void *ptr, long size, long prot, long type, long handle, long arg);
272
static long munmap(void *ptr, long size);
273
 
274
static void vminfo (unsigned long*free, unsigned long*reserved, unsigned long*committed);
26945 ripley 275
/* Commented out for R incompatibility
26855 ripley 276
static int cpuinfo (int whole, unsigned long*kernel, unsigned long*user);
277
*/
278
 
279
#endif
280
 
281
/*
282
  __STD_C should be nonzero if using ANSI-standard C compiler, a C++
283
  compiler, or a C compiler sufficiently close to ANSI to get away
284
  with it.
285
*/
286
 
10347 ripley 287
#ifndef __STD_C
26855 ripley 288
#if defined(__STDC__) || defined(_cplusplus)
10347 ripley 289
#define __STD_C     1
290
#else
291
#define __STD_C     0
26855 ripley 292
#endif 
10347 ripley 293
#endif /*__STD_C*/
294
 
26855 ripley 295
 
296
/*
297
  Void_t* is the pointer type that malloc should say it returns
298
*/
299
 
10347 ripley 300
#ifndef Void_t
10425 ripley 301
#if (__STD_C || defined(WIN32))
10347 ripley 302
#define Void_t      void
303
#else
304
#define Void_t      char
305
#endif
306
#endif /*Void_t*/
307
 
308
#if __STD_C
309
#include <stddef.h>   /* for size_t */
310
#else
311
#include <sys/types.h>
312
#endif
313
 
314
#ifdef __cplusplus
315
extern "C" {
316
#endif
317
 
26855 ripley 318
/* define LACKS_UNISTD_H if your system does not have a <unistd.h>. */
10347 ripley 319
 
26855 ripley 320
/* #define  LACKS_UNISTD_H */
10347 ripley 321
 
26855 ripley 322
#ifndef LACKS_UNISTD_H
323
#include <unistd.h>
324
#endif
10347 ripley 325
 
26855 ripley 326
/* define LACKS_SYS_PARAM_H if your system does not have a <sys/param.h>. */
10347 ripley 327
 
26855 ripley 328
/* #define  LACKS_SYS_PARAM_H */
329
 
330
 
331
#include <stdio.h>    /* needed for malloc_stats */
332
#include <errno.h>    /* needed for optional MALLOC_FAILURE_ACTION */
333
 
334
 
10347 ripley 335
/*
26855 ripley 336
  Debugging:
10347 ripley 337
 
26855 ripley 338
  Because freed chunks may be overwritten with bookkeeping fields, this
339
  malloc will often die when freed memory is overwritten by user
340
  programs.  This can be very effective (albeit in an annoying way)
341
  in helping track down dangling pointers.
10347 ripley 342
 
26855 ripley 343
  If you compile with -DDEBUG, a number of assertion checks are
344
  enabled that will catch more memory errors. You probably won't be
345
  able to make much sense of the actual assertion errors, but they
346
  should help you locate incorrectly overwritten memory.  The
347
  checking is fairly extensive, and will slow down execution
348
  noticeably. Calling malloc_stats or mallinfo with DEBUG set will
349
  attempt to check every non-mmapped allocated and free chunk in the
350
  course of computing the summmaries. (By nature, mmapped regions
351
  cannot be checked very much automatically.)
10347 ripley 352
 
26855 ripley 353
  Setting DEBUG may also be helpful if you are trying to modify
354
  this code. The assertions in the check routines spell out in more
355
  detail the assumptions and invariants underlying the algorithms.
10347 ripley 356
 
26855 ripley 357
  Setting DEBUG does NOT provide an automated mechanism for checking
358
  that all accesses to malloced memory stay within their
359
  bounds. However, there are several add-ons and adaptations of this
360
  or other mallocs available that do this.
10347 ripley 361
*/
362
 
26855 ripley 363
/* #define DEBUG 1 */
364
 
10347 ripley 365
#if DEBUG
366
#include <assert.h>
367
#else
368
#define assert(x) ((void)0)
369
#endif
370
 
26855 ripley 371
/*
372
  The unsigned integer type used for comparing any two chunk sizes.
373
  This should be at least as wide as size_t, but should not be signed.
374
*/
10347 ripley 375
 
26855 ripley 376
#ifndef CHUNK_SIZE_T
377
#define CHUNK_SIZE_T unsigned long
378
#endif
379
 
380
/* 
381
  The unsigned integer type used to hold addresses when they are are
382
  manipulated as integers. Except that it is not defined on all
383
  systems, intptr_t would suffice.
384
*/
385
#ifndef PTR_UINT
386
#define PTR_UINT unsigned long
387
#endif
388
 
389
 
10347 ripley 390
/*
391
  INTERNAL_SIZE_T is the word-size used for internal bookkeeping
26855 ripley 392
  of chunk sizes.
393
 
394
  The default version is the same as size_t.
395
 
396
  While not strictly necessary, it is best to define this as an
397
  unsigned type, even if size_t is a signed type. This may avoid some
398
  artificial size limitations on some systems.
399
 
400
  On a 64-bit machine, you may be able to reduce malloc overhead by
401
  defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the
402
  expense of not being able to handle more than 2^32 of malloced
403
  space. If this limitation is acceptable, you are encouraged to set
404
  this unless you are on a platform requiring 16byte alignments. In
405
  this case the alignment requirements turn out to negate any
406
  potential advantages of decreasing size_t word size.
407
 
408
  Implementors: Beware of the possible combinations of:
409
     - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits,
410
       and might be the same width as int or as long
411
     - size_t might have different width and signedness as INTERNAL_SIZE_T
412
     - int and long might be 32 or 64 bits, and might be the same width
413
  To deal with this, most comparisons and difference computations
414
  among INTERNAL_SIZE_Ts should cast them to CHUNK_SIZE_T, being
415
  aware of the fact that casting an unsigned int to a wider long does
416
  not sign-extend. (This also makes checking for negative numbers
417
  awkward.) Some of these casts result in harmless compiler warnings
418
  on some systems.
10347 ripley 419
*/
420
 
421
#ifndef INTERNAL_SIZE_T
422
#define INTERNAL_SIZE_T size_t
423
#endif
424
 
26855 ripley 425
/* The corresponding word size */
426
#define SIZE_SZ                (sizeof(INTERNAL_SIZE_T))
427
 
428
 
429
 
10347 ripley 430
/*
26855 ripley 431
  MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks.
432
  It must be a power of two at least 2 * SIZE_SZ, even on machines
433
  for which smaller alignments would suffice. It may be defined as
434
  larger than this though. Note however that code and data structures
435
  are optimized for the case of 8-byte alignment.
436
*/
437
 
438
 
439
#ifndef MALLOC_ALIGNMENT
440
#define MALLOC_ALIGNMENT       (2 * SIZE_SZ)
441
#endif
442
 
443
/* The corresponding bit mask value */
444
#define MALLOC_ALIGN_MASK      (MALLOC_ALIGNMENT - 1)
445
 
446
 
447
 
448
/*
10347 ripley 449
  REALLOC_ZERO_BYTES_FREES should be set if a call to
450
  realloc with zero bytes should be the same as a call to free.
451
  Some people think it should. Otherwise, since this malloc
452
  returns a unique pointer for malloc(0), so does realloc(p, 0).
453
*/
454
 
455
/*   #define REALLOC_ZERO_BYTES_FREES */
456
 
26855 ripley 457
/*
458
  TRIM_FASTBINS controls whether free() of a very small chunk can
459
  immediately lead to trimming. Setting to true (1) can reduce memory
460
  footprint, but will almost always slow down programs that use a lot
461
  of small chunks.
10347 ripley 462
 
26855 ripley 463
  Define this only if you are willing to give up some speed to more
464
  aggressively reduce system-level memory footprint when releasing
465
  memory in programs that use many small chunks.  You can get
466
  essentially the same effect by setting MXFAST to 0, but this can
467
  lead to even greater slowdowns in programs using many small chunks.
468
  TRIM_FASTBINS is an in-between compile-time option, that disables
469
  only those chunks bordering topmost memory from being placed in
470
  fastbins.
471
*/
472
 
473
#ifndef TRIM_FASTBINS
474
#define TRIM_FASTBINS  0
475
#endif
476
 
477
 
10347 ripley 478
/*
26855 ripley 479
  USE_DL_PREFIX will prefix all public routines with the string 'dl'.
480
  This is necessary when you only want to use this malloc in one part 
481
  of a program, using your regular system malloc elsewhere.
10347 ripley 482
*/
483
 
26855 ripley 484
/* #define USE_DL_PREFIX */
10425 ripley 485
 
486
 
487
/*
26855 ripley 488
  USE_MALLOC_LOCK causes wrapper functions to surround each
489
  callable routine with pthread mutex lock/unlock.
10425 ripley 490
 
26855 ripley 491
  USE_MALLOC_LOCK forces USE_PUBLIC_MALLOC_WRAPPERS to be defined
10425 ripley 492
*/
26855 ripley 493
 
494
 
495
/* #define USE_MALLOC_LOCK */
496
 
497
 
498
/*
499
  If USE_PUBLIC_MALLOC_WRAPPERS is defined, every public routine is
500
  actually a wrapper function that first calls MALLOC_PREACTION, then
501
  calls the internal routine, and follows it with
502
  MALLOC_POSTACTION. This is needed for locking, but you can also use
503
  this, without USE_MALLOC_LOCK, for purposes of interception,
504
  instrumentation, etc. It is a sad fact that using wrappers often
505
  noticeably degrades performance of malloc-intensive programs.
506
*/
507
 
508
#ifdef USE_MALLOC_LOCK
509
#define USE_PUBLIC_MALLOC_WRAPPERS
510
#else
511
/* #define USE_PUBLIC_MALLOC_WRAPPERS */
10347 ripley 512
#endif
513
 
514
 
26855 ripley 515
/* 
516
   Two-phase name translation.
517
   All of the actual routines are given mangled names.
518
   When wrappers are used, they become the public callable versions.
519
   When DL_PREFIX is used, the callable names are prefixed.
520
*/
521
 
522
#ifndef USE_PUBLIC_MALLOC_WRAPPERS
523
#define cALLOc      public_cALLOc
524
#define fREe        public_fREe
525
#define cFREe       public_cFREe
526
#define mALLOc      public_mALLOc
527
#define mEMALIGn    public_mEMALIGn
528
#define rEALLOc     public_rEALLOc
529
#define vALLOc      public_vALLOc
530
#define pVALLOc     public_pVALLOc
531
#define mALLINFo    public_mALLINFo
532
#define mALLOPt     public_mALLOPt
533
#define mTRIm       public_mTRIm
534
#define mSTATs      public_mSTATs
535
#define mUSABLe     public_mUSABLe
536
#define iCALLOc     public_iCALLOc
537
#define iCOMALLOc   public_iCOMALLOc
538
#endif
539
 
540
#ifdef USE_DL_PREFIX
541
#define public_cALLOc    dlcalloc
542
#define public_fREe      dlfree
543
#define public_cFREe     dlcfree
544
#define public_mALLOc    dlmalloc
545
#define public_mEMALIGn  dlmemalign
546
#define public_rEALLOc   dlrealloc
547
#define public_vALLOc    dlvalloc
548
#define public_pVALLOc   dlpvalloc
549
#define public_mALLINFo  dlmallinfo
550
#define public_mALLOPt   dlmallopt
551
#define public_mTRIm     dlmalloc_trim
552
#define public_mSTATs    dlmalloc_stats
553
#define public_mUSABLe   dlmalloc_usable_size
554
#define public_iCALLOc   dlindependent_calloc
555
#define public_iCOMALLOc dlindependent_comalloc
556
#else /* USE_DL_PREFIX */
26879 ripley 557
#define public_cALLOc    Rm_calloc
558
#define public_fREe      Rm_free
559
#define public_cFREe     Rm_cfree
560
#define public_mALLOc    Rm_malloc
561
#define public_mEMALIGn  Rm_memalign
562
#define public_rEALLOc   Rm_realloc
563
#define public_vALLOc    Rm_valloc
564
#define public_pVALLOc   Rm_pvalloc
26855 ripley 565
#define public_mALLINFo  mallinfo
566
#define public_mALLOPt   mallopt
567
#define public_mTRIm     malloc_trim
568
#define public_mSTATs    malloc_stats
569
#define public_mUSABLe   malloc_usable_size
570
#define public_iCALLOc   independent_calloc
571
#define public_iCOMALLOc independent_comalloc
572
#endif /* USE_DL_PREFIX */
573
 
574
 
10347 ripley 575
/*
576
  HAVE_MEMCPY should be defined if you are not otherwise using
577
  ANSI STD C, but still have memcpy and memset in your C library
578
  and want to use them in calloc and realloc. Otherwise simple
26855 ripley 579
  macro versions are defined below.
10347 ripley 580
 
581
  USE_MEMCPY should be defined as 1 if you actually want to
582
  have memset and memcpy called. People report that the macro
26855 ripley 583
  versions are faster than libc versions on some systems.
584
 
585
  Even if USE_MEMCPY is set to 1, loops to copy/clear small chunks
586
  (of <= 36 bytes) are manually unrolled in realloc and calloc.
10347 ripley 587
*/
588
 
589
#define HAVE_MEMCPY
590
 
591
#ifndef USE_MEMCPY
592
#ifdef HAVE_MEMCPY
593
#define USE_MEMCPY 1
594
#else
595
#define USE_MEMCPY 0
596
#endif
597
#endif
598
 
26855 ripley 599
 
10347 ripley 600
#if (__STD_C || defined(HAVE_MEMCPY))
601
 
26855 ripley 602
#ifdef WIN32
603
/* On Win32 memset and memcpy are already declared in windows.h */
604
#else
10347 ripley 605
#if __STD_C
606
void* memset(void*, int, size_t);
607
void* memcpy(void*, const void*, size_t);
608
#else
609
Void_t* memset();
610
Void_t* memcpy();
611
#endif
612
#endif
10425 ripley 613
#endif
10347 ripley 614
 
26855 ripley 615
/*
616
  MALLOC_FAILURE_ACTION is the action to take before "return 0" when
617
  malloc fails to be able to return memory, either because memory is
618
  exhausted or because of illegal arguments.
619
 
620
  By default, sets errno if running on STD_C platform, else does nothing.  
621
*/
10347 ripley 622
 
26855 ripley 623
#ifndef MALLOC_FAILURE_ACTION
624
#if __STD_C
625
#define MALLOC_FAILURE_ACTION \
626
   errno = ENOMEM;
10347 ripley 627
 
26855 ripley 628
#else
629
#define MALLOC_FAILURE_ACTION
630
#endif
631
#endif
10347 ripley 632
 
26855 ripley 633
/*
634
  MORECORE-related declarations. By default, rely on sbrk
635
*/
10347 ripley 636
 
637
 
26855 ripley 638
#ifdef LACKS_UNISTD_H
639
#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
640
#if __STD_C
641
extern Void_t*     sbrk(ptrdiff_t);
642
#else
643
extern Void_t*     sbrk();
644
#endif
645
#endif
646
#endif
10347 ripley 647
 
26855 ripley 648
/*
649
  MORECORE is the name of the routine to call to obtain more memory
650
  from the system.  See below for general guidance on writing
651
  alternative MORECORE functions, as well as a version for WIN32 and a
652
  sample version for pre-OSX macos.
653
*/
10347 ripley 654
 
26855 ripley 655
#ifndef MORECORE
656
#define MORECORE sbrk
657
#endif
10347 ripley 658
 
26855 ripley 659
/*
660
  MORECORE_FAILURE is the value returned upon failure of MORECORE
661
  as well as mmap. Since it cannot be an otherwise valid memory address,
662
  and must reflect values of standard sys calls, you probably ought not
663
  try to redefine it.
664
*/
665
 
666
#ifndef MORECORE_FAILURE
667
#define MORECORE_FAILURE (-1)
10347 ripley 668
#endif
669
 
26855 ripley 670
/*
671
  If MORECORE_CONTIGUOUS is true, take advantage of fact that
672
  consecutive calls to MORECORE with positive arguments always return
673
  contiguous increasing addresses.  This is true of unix sbrk.  Even
674
  if not defined, when regions happen to be contiguous, malloc will
675
  permit allocations spanning regions obtained from different
676
  calls. But defining this when applicable enables some stronger
677
  consistency checks and space efficiencies. 
678
*/
10347 ripley 679
 
26855 ripley 680
#ifndef MORECORE_CONTIGUOUS
681
#define MORECORE_CONTIGUOUS 1
682
#endif
683
 
10347 ripley 684
/*
26855 ripley 685
  Define MORECORE_CANNOT_TRIM if your version of MORECORE
686
  cannot release space back to the system when given negative
687
  arguments. This is generally necessary only if you are using
688
  a hand-crafted MORECORE function that cannot handle negative arguments.
689
*/
690
 
26945 ripley 691
/* #define MORECORE_CANNOT_TRIM */
26855 ripley 692
 
693
 
694
/*
695
  Define HAVE_MMAP as true to optionally make malloc() use mmap() to
10347 ripley 696
  allocate very large blocks.  These will be returned to the
26855 ripley 697
  operating system immediately after a free(). Also, if mmap
698
  is available, it is used as a backup strategy in cases where
699
  MORECORE fails to provide space from system.
700
 
701
  This malloc is best tuned to work with mmap for large requests.
702
  If you do not have mmap, operations involving very large chunks (1MB
703
  or so) may be slower than you'd like.
10347 ripley 704
*/
705
 
706
#ifndef HAVE_MMAP
707
#define HAVE_MMAP 1
708
#endif
709
 
26855 ripley 710
#if HAVE_MMAP
711
/* 
712
   Standard unix mmap using /dev/zero clears memory so calloc doesn't
713
   need to.
714
*/
715
 
716
#ifndef MMAP_CLEARS
717
#define MMAP_CLEARS 1
718
#endif
719
 
720
#else /* no mmap */
721
#ifndef MMAP_CLEARS
722
#define MMAP_CLEARS 0
723
#endif
724
#endif
725
 
726
 
727
/* 
728
   MMAP_AS_MORECORE_SIZE is the minimum mmap size argument to use if
729
   sbrk fails, and mmap is used as a backup (which is done only if
730
   HAVE_MMAP).  The value must be a multiple of page size.  This
731
   backup strategy generally applies only when systems have "holes" in
732
   address space, so sbrk cannot perform contiguous expansion, but
733
   there is still space available on system.  On systems for which
734
   this is known to be useful (i.e. most linux kernels), this occurs
735
   only when programs allocate huge amounts of memory.  Between this,
736
   and the fact that mmap regions tend to be limited, the size should
737
   be large, to avoid too many mmap calls and thus avoid running out
738
   of kernel resources.
739
*/
740
 
741
#ifndef MMAP_AS_MORECORE_SIZE
742
#define MMAP_AS_MORECORE_SIZE (1024 * 1024)
743
#endif
744
 
10347 ripley 745
/*
746
  Define HAVE_MREMAP to make realloc() use mremap() to re-allocate
747
  large blocks.  This is currently only possible on Linux with
748
  kernel versions newer than 1.3.77.
749
*/
750
 
751
#ifndef HAVE_MREMAP
26855 ripley 752
#ifdef linux
10347 ripley 753
#define HAVE_MREMAP 1
754
#else
755
#define HAVE_MREMAP 0
756
#endif
757
 
26855 ripley 758
#endif /* HAVE_MMAP */
10347 ripley 759
 
760
 
761
/*
26855 ripley 762
  The system page size. To the extent possible, this malloc manages
763
  memory from the system in page-size units.  Note that this value is
764
  cached during initialization into a field of malloc_state. So even
765
  if malloc_getpagesize is a function, it is only called once.
10347 ripley 766
 
26855 ripley 767
  The following mechanics for getpagesize were adapted from bsd/gnu
768
  getpagesize.h. If none of the system-probes here apply, a value of
769
  4096 is used, which should be OK: If they don't apply, then using
770
  the actual value probably doesn't impact performance.
10347 ripley 771
*/
772
 
26855 ripley 773
 
774
#ifndef malloc_getpagesize
775
 
10347 ripley 776
#ifndef LACKS_UNISTD_H
777
#  include <unistd.h>
778
#endif
779
 
780
#  ifdef _SC_PAGESIZE         /* some SVR4 systems omit an underscore */
781
#    ifndef _SC_PAGE_SIZE
782
#      define _SC_PAGE_SIZE _SC_PAGESIZE
783
#    endif
784
#  endif
26855 ripley 785
 
10347 ripley 786
#  ifdef _SC_PAGE_SIZE
787
#    define malloc_getpagesize sysconf(_SC_PAGE_SIZE)
788
#  else
789
#    if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
790
       extern size_t getpagesize();
791
#      define malloc_getpagesize getpagesize()
792
#    else
26855 ripley 793
#      ifdef WIN32 /* use supplied emulation of getpagesize */
794
#        define malloc_getpagesize getpagesize() 
10347 ripley 795
#      else
10425 ripley 796
#        ifndef LACKS_SYS_PARAM_H
797
#          include <sys/param.h>
798
#        endif
799
#        ifdef EXEC_PAGESIZE
800
#          define malloc_getpagesize EXEC_PAGESIZE
10347 ripley 801
#        else
10425 ripley 802
#          ifdef NBPG
803
#            ifndef CLSIZE
804
#              define malloc_getpagesize NBPG
805
#            else
806
#              define malloc_getpagesize (NBPG * CLSIZE)
807
#            endif
10347 ripley 808
#          else
10425 ripley 809
#            ifdef NBPC
810
#              define malloc_getpagesize NBPC
10347 ripley 811
#            else
10425 ripley 812
#              ifdef PAGESIZE
813
#                define malloc_getpagesize PAGESIZE
26855 ripley 814
#              else /* just guess */
815
#                define malloc_getpagesize (4096) 
10425 ripley 816
#              endif
10347 ripley 817
#            endif
818
#          endif
819
#        endif
820
#      endif
821
#    endif
822
#  endif
823
#endif
824
 
825
/*
826
  This version of malloc supports the standard SVID/XPG mallinfo
26855 ripley 827
  routine that returns a struct containing usage properties and
828
  statistics. It should work on any SVID/XPG compliant system that has
829
  a /usr/include/malloc.h defining struct mallinfo. (If you'd like to
830
  install such a thing yourself, cut out the preliminary declarations
831
  as described above and below and save them in a malloc.h file. But
832
  there's no compelling reason to bother to do this.)
10347 ripley 833
 
834
  The main declaration needed is the mallinfo struct that is returned
835
  (by-copy) by mallinfo().  The SVID/XPG malloinfo struct contains a
26855 ripley 836
  bunch of fields that are not even meaningful in this version of
837
  malloc.  These fields are are instead filled by mallinfo() with
838
  other numbers that might be of interest.
10347 ripley 839
 
840
  HAVE_USR_INCLUDE_MALLOC_H should be set if you have a
841
  /usr/include/malloc.h file that includes a declaration of struct
842
  mallinfo.  If so, it is included; else an SVID2/XPG2 compliant
843
  version is declared below.  These must be precisely the same for
26855 ripley 844
  mallinfo() to work.  The original SVID version of this struct,
845
  defined on most systems with mallinfo, declares all fields as
846
  ints. But some others define as unsigned long. If your system
847
  defines the fields using a type of different width than listed here,
848
  you must #include your system version and #define
849
  HAVE_USR_INCLUDE_MALLOC_H.
10347 ripley 850
*/
851
 
852
/* #define HAVE_USR_INCLUDE_MALLOC_H */
853
 
26855 ripley 854
#ifdef HAVE_USR_INCLUDE_MALLOC_H
10347 ripley 855
#include "/usr/include/malloc.h"
856
#else
857
 
858
/* SVID2/XPG mallinfo structure */
859
 
860
struct mallinfo {
26855 ripley 861
  int arena;    /* non-mmapped space allocated from system */
862
  int ordblks;  /* number of free chunks */
863
  int smblks;   /* number of fastbin blocks */
10347 ripley 864
  int hblks;    /* number of mmapped regions */
26855 ripley 865
  int hblkhd;   /* space in mmapped regions */
866
  int usmblks;  /* maximum total allocated space */
867
  int fsmblks;  /* space available in freed fastbin blocks */
10347 ripley 868
  int uordblks; /* total allocated space */
26855 ripley 869
  int fordblks; /* total free space */
10347 ripley 870
  int keepcost; /* top-most, releasable (via malloc_trim) space */
871
};
872
 
26855 ripley 873
/*
874
  SVID/XPG defines four standard parameter numbers for mallopt,
875
  normally defined in malloc.h.  Only one of these (M_MXFAST) is used
876
  in this malloc. The others (M_NLBLKS, M_GRAIN, M_KEEP) don't apply,
877
  so setting them has no effect. But this malloc also supports other
878
  options in mallopt described below.
879
*/
880
#endif
10347 ripley 881
 
26855 ripley 882
/* R Specific declarations here */
10347 ripley 883
 
26855 ripley 884
extern unsigned int R_max_memory;
26895 ripley 885
extern int R_Is_Running;
886
void Rf_warning(const char *, ...);
10347 ripley 887
 
888
 
26855 ripley 889
/* ---------- description of public routines ------------ */
10347 ripley 890
 
26855 ripley 891
/*
892
  malloc(size_t n)
893
  Returns a pointer to a newly allocated chunk of at least n bytes, or null
894
  if no space is available. Additionally, on failure, errno is
895
  set to ENOMEM on ANSI C systems.
10347 ripley 896
 
26855 ripley 897
  If n is zero, malloc returns a minumum-sized chunk. (The minimum
898
  size is 16 bytes on most 32bit systems, and 24 or 32 bytes on 64bit
899
  systems.)  On most systems, size_t is an unsigned type, so calls
900
  with negative arguments are interpreted as requests for huge amounts
901
  of space, which will often fail. The maximum supported value of n
902
  differs across systems, but is in all cases less than the maximum
903
  representable value of a size_t.
904
*/
905
#if __STD_C
906
Void_t*  public_mALLOc(size_t);
907
#else
908
Void_t*  public_mALLOc();
10347 ripley 909
#endif
910
 
911
/*
26855 ripley 912
  free(Void_t* p)
913
  Releases the chunk of memory pointed to by p, that had been previously
914
  allocated using malloc or a related routine such as realloc.
915
  It has no effect if p is null. It can have arbitrary (i.e., bad!)
916
  effects if p has already been freed.
10347 ripley 917
 
26855 ripley 918
  Unless disabled (using mallopt), freeing very large spaces will
919
  when possible, automatically trigger operations that give
920
  back unused memory to the system, thus reducing program footprint.
921
*/
922
#if __STD_C
923
void     public_fREe(Void_t*);
924
#else
925
void     public_fREe();
926
#endif
10347 ripley 927
 
26855 ripley 928
/*
929
  calloc(size_t n_elements, size_t element_size);
930
  Returns a pointer to n_elements * element_size bytes, with all locations
931
  set to zero.
932
*/
933
#if __STD_C
934
Void_t*  public_cALLOc(size_t, size_t);
935
#else
936
Void_t*  public_cALLOc();
937
#endif
10347 ripley 938
 
26855 ripley 939
/*
940
  realloc(Void_t* p, size_t n)
941
  Returns a pointer to a chunk of size n that contains the same data
942
  as does chunk p up to the minimum of (n, p's size) bytes, or null
943
  if no space is available. 
10347 ripley 944
 
26855 ripley 945
  The returned pointer may or may not be the same as p. The algorithm
946
  prefers extending p when possible, otherwise it employs the
947
  equivalent of a malloc-copy-free sequence.
10347 ripley 948
 
26855 ripley 949
  If p is null, realloc is equivalent to malloc.  
10347 ripley 950
 
26855 ripley 951
  If space is not available, realloc returns null, errno is set (if on
952
  ANSI) and p is NOT freed.
10347 ripley 953
 
26855 ripley 954
  if n is for fewer bytes than already held by p, the newly unused
955
  space is lopped off and freed if possible.  Unless the #define
956
  REALLOC_ZERO_BYTES_FREES is set, realloc with a size argument of
957
  zero (re)allocates a minimum-sized chunk.
958
 
959
  Large chunks that were internally obtained via mmap will always
960
  be reallocated using malloc-copy-free sequences unless
961
  the system supports MREMAP (currently only linux).
962
 
963
  The old unix realloc convention of allowing the last-free'd chunk
964
  to be used as an argument to realloc is not supported.
10347 ripley 965
*/
26855 ripley 966
#if __STD_C
967
Void_t*  public_rEALLOc(Void_t*, size_t);
968
#else
969
Void_t*  public_rEALLOc();
970
#endif
10347 ripley 971
 
26855 ripley 972
/*
973
  memalign(size_t alignment, size_t n);
974
  Returns a pointer to a newly allocated chunk of n bytes, aligned
975
  in accord with the alignment argument.
10347 ripley 976
 
26855 ripley 977
  The alignment argument should be a power of two. If the argument is
978
  not a power of two, the nearest greater power is used.
979
  8-byte alignment is guaranteed by normal malloc calls, so don't
980
  bother calling memalign with an argument of 8 or less.
981
 
982
  Overreliance on memalign is a sure way to fragment space.
983
*/
984
#if __STD_C
985
Void_t*  public_mEMALIGn(size_t, size_t);
986
#else
987
Void_t*  public_mEMALIGn();
10347 ripley 988
#endif
989
 
990
/*
26855 ripley 991
  valloc(size_t n);
992
  Equivalent to memalign(pagesize, n), where pagesize is the page
993
  size of the system. If the pagesize is unknown, 4096 is used.
994
*/
995
#if __STD_C
996
Void_t*  public_vALLOc(size_t);
997
#else
998
Void_t*  public_vALLOc();
999
#endif
10347 ripley 1000
 
1001
 
1002
 
26855 ripley 1003
/*
1004
  mallopt(int parameter_number, int parameter_value)
1005
  Sets tunable parameters The format is to provide a
1006
  (parameter-number, parameter-value) pair.  mallopt then sets the
1007
  corresponding parameter to the argument value if it can (i.e., so
1008
  long as the value is meaningful), and returns 1 if successful else
1009
  0.  SVID/XPG/ANSI defines four standard param numbers for mallopt,
1010
  normally defined in malloc.h.  Only one of these (M_MXFAST) is used
1011
  in this malloc. The others (M_NLBLKS, M_GRAIN, M_KEEP) don't apply,
1012
  so setting them has no effect. But this malloc also supports four
1013
  other options in mallopt. See below for details.  Briefly, supported
1014
  parameters are as follows (listed defaults are for "typical"
1015
  configurations).
10347 ripley 1016
 
26855 ripley 1017
  Symbol            param #   default    allowed param values
1018
  M_MXFAST          1         64         0-80  (0 disables fastbins)
1019
  M_TRIM_THRESHOLD -1         256*1024   any   (-1U disables trimming)
1020
  M_TOP_PAD        -2         0          any  
1021
  M_MMAP_THRESHOLD -3         256*1024   any   (or 0 if no MMAP support)
1022
  M_MMAP_MAX       -4         65536      any   (0 disables use of mmap)
1023
*/
1024
#if __STD_C
1025
int      public_mALLOPt(int, int);
1026
#else
1027
int      public_mALLOPt();
1028
#endif
10347 ripley 1029
 
1030
 
26855 ripley 1031
/*
1032
  mallinfo()
1033
  Returns (by copy) a struct containing various summary statistics:
1034
 
1035
  arena:     current total non-mmapped bytes allocated from system 
1036
  ordblks:   the number of free chunks 
1037
  smblks:    the number of fastbin blocks (i.e., small chunks that
1038
               have been freed but not use resused or consolidated)
1039
  hblks:     current number of mmapped regions 
1040
  hblkhd:    total bytes held in mmapped regions 
1041
  usmblks:   the maximum total allocated space. This will be greater
1042
                than current total if trimming has occurred.
1043
  fsmblks:   total bytes held in fastbin blocks 
1044
  uordblks:  current total allocated space (normal or mmapped)
1045
  fordblks:  total free space 
1046
  keepcost:  the maximum number of bytes that could ideally be released
1047
               back to system via malloc_trim. ("ideally" means that
1048
               it ignores page restrictions etc.)
1049
 
1050
  Because these fields are ints, but internal bookkeeping may
1051
  be kept as longs, the reported values may wrap around zero and 
1052
  thus be inaccurate.
10347 ripley 1053
*/
26855 ripley 1054
#if __STD_C
1055
struct mallinfo public_mALLINFo(void);
1056
#else
1057
struct mallinfo public_mALLINFo();
1058
#endif
10347 ripley 1059
 
26855 ripley 1060
/*
1061
  independent_calloc(size_t n_elements, size_t element_size, Void_t* chunks[]);
10347 ripley 1062
 
26855 ripley 1063
  independent_calloc is similar to calloc, but instead of returning a
1064
  single cleared space, it returns an array of pointers to n_elements
1065
  independent elements that can hold contents of size elem_size, each
1066
  of which starts out cleared, and can be independently freed,
1067
  realloc'ed etc. The elements are guaranteed to be adjacently
1068
  allocated (this is not guaranteed to occur with multiple callocs or
1069
  mallocs), which may also improve cache locality in some
1070
  applications.
1071
 
1072
  The "chunks" argument is optional (i.e., may be null, which is
1073
  probably the most typical usage). If it is null, the returned array
1074
  is itself dynamically allocated and should also be freed when it is
1075
  no longer needed. Otherwise, the chunks array must be of at least
1076
  n_elements in length. It is filled in with the pointers to the
1077
  chunks.
1078
 
1079
  In either case, independent_calloc returns this pointer array, or
1080
  null if the allocation failed.  If n_elements is zero and "chunks"
1081
  is null, it returns a chunk representing an array with zero elements
1082
  (which should be freed if not wanted).
1083
 
1084
  Each element must be individually freed when it is no longer
1085
  needed. If you'd like to instead be able to free all at once, you
1086
  should instead use regular calloc and assign pointers into this
1087
  space to represent elements.  (In this case though, you cannot
1088
  independently free elements.)
1089
 
1090
  independent_calloc simplifies and speeds up implementations of many
1091
  kinds of pools.  It may also be useful when constructing large data
1092
  structures that initially have a fixed number of fixed-sized nodes,
1093
  but the number is not known at compile time, and some of the nodes
1094
  may later need to be freed. For example:
1095
 
1096
  struct Node { int item; struct Node* next; };
1097
 
1098
  struct Node* build_list() {
1099
    struct Node** pool;
1100
    int n = read_number_of_nodes_needed();
1101
    if (n <= 0) return 0;
1102
    pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0);
1103
    if (pool == 0) die(); 
1104
    // organize into a linked list... 
1105
    struct Node* first = pool[0];
1106
    for (i = 0; i < n-1; ++i) 
1107
      pool[i]->next = pool[i+1];
1108
    free(pool);     // Can now free the array (or not, if it is needed later)
1109
    return first;
1110
  }
1111
*/
1112
#if __STD_C
1113
Void_t** public_iCALLOc(size_t, size_t, Void_t**);
1114
#else
1115
Void_t** public_iCALLOc();
10347 ripley 1116
#endif
1117
 
1118
/*
26855 ripley 1119
  independent_comalloc(size_t n_elements, size_t sizes[], Void_t* chunks[]);
10347 ripley 1120
 
26855 ripley 1121
  independent_comalloc allocates, all at once, a set of n_elements
1122
  chunks with sizes indicated in the "sizes" array.    It returns
1123
  an array of pointers to these elements, each of which can be
1124
  independently freed, realloc'ed etc. The elements are guaranteed to
1125
  be adjacently allocated (this is not guaranteed to occur with
1126
  multiple callocs or mallocs), which may also improve cache locality
1127
  in some applications.
10347 ripley 1128
 
26855 ripley 1129
  The "chunks" argument is optional (i.e., may be null). If it is null
1130
  the returned array is itself dynamically allocated and should also
1131
  be freed when it is no longer needed. Otherwise, the chunks array
1132
  must be of at least n_elements in length. It is filled in with the
1133
  pointers to the chunks.
10347 ripley 1134
 
26855 ripley 1135
  In either case, independent_comalloc returns this pointer array, or
1136
  null if the allocation failed.  If n_elements is zero and chunks is
1137
  null, it returns a chunk representing an array with zero elements
1138
  (which should be freed if not wanted).
1139
 
1140
  Each element must be individually freed when it is no longer
1141
  needed. If you'd like to instead be able to free all at once, you
1142
  should instead use a single regular malloc, and assign pointers at
1143
  particular offsets in the aggregate space. (In this case though, you 
1144
  cannot independently free elements.)
10347 ripley 1145
 
26855 ripley 1146
  independent_comallac differs from independent_calloc in that each
1147
  element may have a different size, and also that it does not
1148
  automatically clear elements.
10347 ripley 1149
 
26855 ripley 1150
  independent_comalloc can be used to speed up allocation in cases
1151
  where several structs or objects must always be allocated at the
1152
  same time.  For example:
10347 ripley 1153
 
26855 ripley 1154
  struct Head { ... }
1155
  struct Foot { ... }
10347 ripley 1156
 
26855 ripley 1157
  void send_message(char* msg) {
1158
    int msglen = strlen(msg);
1159
    size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) };
1160
    void* chunks[3];
1161
    if (independent_comalloc(3, sizes, chunks) == 0)
1162
      die();
1163
    struct Head* head = (struct Head*)(chunks[0]);
1164
    char*        body = (char*)(chunks[1]);
1165
    struct Foot* foot = (struct Foot*)(chunks[2]);
1166
    // ...
1167
  }
10347 ripley 1168
 
26855 ripley 1169
  In general though, independent_comalloc is worth using only for
1170
  larger values of n_elements. For small values, you probably won't
1171
  detect enough difference from series of malloc calls to bother.
1172
 
1173
  Overuse of independent_comalloc can increase overall memory usage,
1174
  since it cannot reuse existing noncontiguous small chunks that
1175
  might be available for some of the elements.
10347 ripley 1176
*/
26855 ripley 1177
#if __STD_C
1178
Void_t** public_iCOMALLOc(size_t, size_t*, Void_t**);
1179
#else
1180
Void_t** public_iCOMALLOc();
1181
#endif
10347 ripley 1182
 
1183
 
26855 ripley 1184
/*
1185
  pvalloc(size_t n);
1186
  Equivalent to valloc(minimum-page-that-holds(n)), that is,
1187
  round up n to nearest pagesize.
1188
 */
1189
#if __STD_C
1190
Void_t*  public_pVALLOc(size_t);
1191
#else
1192
Void_t*  public_pVALLOc();
1193
#endif
10347 ripley 1194
 
26855 ripley 1195
/*
1196
  cfree(Void_t* p);
1197
  Equivalent to free(p).
1198
 
1199
  cfree is needed/defined on some systems that pair it with calloc,
1200
  for odd historical reasons (such as: cfree is used in example 
1201
  code in the first edition of K&R).
1202
*/
1203
#if __STD_C
1204
void     public_cFREe(Void_t*);
10347 ripley 1205
#else
26855 ripley 1206
void     public_cFREe();
10347 ripley 1207
#endif
26855 ripley 1208
 
1209
/*
1210
  malloc_trim(size_t pad);
1211
 
1212
  If possible, gives memory back to the system (via negative
1213
  arguments to sbrk) if there is unused memory at the `high' end of
1214
  the malloc pool. You can call this after freeing large blocks of
1215
  memory to potentially reduce the system-level memory requirements
1216
  of a program. However, it cannot guarantee to reduce memory. Under
1217
  some allocation patterns, some large free blocks of memory will be
1218
  locked between two used chunks, so they cannot be given back to
1219
  the system.
1220
 
1221
  The `pad' argument to malloc_trim represents the amount of free
1222
  trailing space to leave untrimmed. If this argument is zero,
1223
  only the minimum amount of memory to maintain internal data
1224
  structures will be left (one page or less). Non-zero arguments
1225
  can be supplied to maintain enough trailing space to service
1226
  future expected allocations without having to re-obtain memory
1227
  from the system.
1228
 
1229
  Malloc_trim returns 1 if it actually released any memory, else 0.
1230
  On systems that do not support "negative sbrks", it will always
1231
  rreturn 0.
1232
*/
1233
#if __STD_C
1234
int      public_mTRIm(size_t);
1235
#else
1236
int      public_mTRIm();
10347 ripley 1237
#endif
1238
 
1239
/*
26855 ripley 1240
  malloc_usable_size(Void_t* p);
10347 ripley 1241
 
26855 ripley 1242
  Returns the number of bytes you can actually use in
1243
  an allocated chunk, which may be more than you requested (although
1244
  often not) due to alignment and minimum size constraints.
1245
  You can use this many bytes without worrying about
1246
  overwriting other allocated objects. This is not a particularly great
1247
  programming practice. malloc_usable_size can be more useful in
1248
  debugging and assertions, for example:
10347 ripley 1249
 
26855 ripley 1250
  p = malloc(n);
1251
  assert(malloc_usable_size(p) >= 256);
1252
 
10347 ripley 1253
*/
26855 ripley 1254
#if __STD_C
1255
size_t   public_mUSABLe(Void_t*);
1256
#else
1257
size_t   public_mUSABLe();
1258
#endif
10347 ripley 1259
 
26855 ripley 1260
/*
1261
  malloc_stats();
1262
  Prints on stderr the amount of space obtained from the system (both
1263
  via sbrk and mmap), the maximum amount (which may be more than
1264
  current if malloc_trim and/or munmap got called), and the current
1265
  number of bytes allocated via malloc (or realloc, etc) but not yet
1266
  freed. Note that this is the number of bytes allocated, not the
1267
  number requested. It will be larger than the number requested
1268
  because of alignment and bookkeeping overhead. Because it includes
1269
  alignment wastage as being in use, this figure may be greater than
1270
  zero even when no user-level chunks are allocated.
10347 ripley 1271
 
26855 ripley 1272
  The reported current and maximum system memory can be inaccurate if
1273
  a program makes other calls to system memory allocation functions
1274
  (normally sbrk) outside of malloc.
10347 ripley 1275
 
26855 ripley 1276
  malloc_stats prints only the most commonly interesting statistics.
1277
  More information can be obtained by calling mallinfo.
10347 ripley 1278
 
26855 ripley 1279
*/
1280
#if __STD_C
1281
void     public_mSTATs();
1282
#else
1283
void     public_mSTATs();
1284
#endif
1285
 
1286
/* mallopt tuning options */
1287
 
10347 ripley 1288
/*
26855 ripley 1289
  M_MXFAST is the maximum request size used for "fastbins", special bins
1290
  that hold returned chunks without consolidating their spaces. This
1291
  enables future requests for chunks of the same size to be handled
1292
  very quickly, but can increase fragmentation, and thus increase the
1293
  overall memory footprint of a program.
10347 ripley 1294
 
26855 ripley 1295
  This malloc manages fastbins very conservatively yet still
1296
  efficiently, so fragmentation is rarely a problem for values less
1297
  than or equal to the default.  The maximum supported value of MXFAST
1298
  is 80. You wouldn't want it any higher than this anyway.  Fastbins
1299
  are designed especially for use with many small structs, objects or
1300
  strings -- the default handles structs/objects/arrays with sizes up
1301
  to 16 4byte fields, or small strings representing words, tokens,
1302
  etc. Using fastbins for larger objects normally worsens
1303
  fragmentation without improving speed.
1304
 
1305
  M_MXFAST is set in REQUEST size units. It is internally used in
1306
  chunksize units, which adds padding and alignment.  You can reduce
1307
  M_MXFAST to 0 to disable all use of fastbins.  This causes the malloc
1308
  algorithm to be a closer approximation of fifo-best-fit in all cases,
1309
  not just for larger requests, but will generally cause it to be
1310
  slower.
10425 ripley 1311
*/
1312
 
1313
 
26855 ripley 1314
/* M_MXFAST is a standard SVID/XPG tuning option, usually listed in malloc.h */
1315
#ifndef M_MXFAST
1316
#define M_MXFAST            1    
1317
#endif
10425 ripley 1318
 
26855 ripley 1319
#ifndef DEFAULT_MXFAST
1320
#define DEFAULT_MXFAST     64
1321
#endif
10425 ripley 1322
 
1323
 
1324
/*
26855 ripley 1325
  M_TRIM_THRESHOLD is the maximum amount of unused top-most memory
1326
  to keep before releasing via malloc_trim in free().
10425 ripley 1327
 
26855 ripley 1328
  Automatic trimming is mainly useful in long-lived programs.
1329
  Because trimming via sbrk can be slow on some systems, and can
1330
  sometimes be wasteful (in cases where programs immediately
1331
  afterward allocate more large chunks) the value should be high
1332
  enough so that your overall system performance would improve by
1333
  releasing this much memory.
10347 ripley 1334
 
26855 ripley 1335
  The trim threshold and the mmap control parameters (see below)
1336
  can be traded off with one another. Trimming and mmapping are
1337
  two different ways of releasing unused memory back to the
1338
  system. Between these two, it is often possible to keep
1339
  system-level demands of a long-lived program down to a bare
1340
  minimum. For example, in one test suite of sessions measuring
1341
  the XF86 X server on Linux, using a trim threshold of 128K and a
1342
  mmap threshold of 192K led to near-minimal long term resource
1343
  consumption.
10347 ripley 1344
 
26855 ripley 1345
  If you are using this malloc in a long-lived program, it should
1346
  pay to experiment with these values.  As a rough guide, you
1347
  might set to a value close to the average size of a process
1348
  (program) running on your system.  Releasing this much memory
1349
  would allow such a process to run in memory.  Generally, it's
1350
  worth it to tune for trimming rather tham memory mapping when a
1351
  program undergoes phases where several large chunks are
1352
  allocated and released in ways that can reuse each other's
1353
  storage, perhaps mixed with phases where there are no such
1354
  chunks at all.  And in well-behaved long-lived programs,
1355
  controlling release of large blocks via trimming versus mapping
1356
  is usually faster.
10347 ripley 1357
 
26855 ripley 1358
  However, in most programs, these parameters serve mainly as
1359
  protection against the system-level effects of carrying around
1360
  massive amounts of unneeded memory. Since frequent calls to
1361
  sbrk, mmap, and munmap otherwise degrade performance, the default
1362
  parameters are set to relatively high values that serve only as
1363
  safeguards.
10347 ripley 1364
 
26855 ripley 1365
  The trim value must be greater than page size to have any useful
1366
  effect.  To disable trimming completely, you can set to 
1367
  (unsigned long)(-1)
10347 ripley 1368
 
26855 ripley 1369
  Trim settings interact with fastbin (MXFAST) settings: Unless
1370
  TRIM_FASTBINS is defined, automatic trimming never takes place upon
1371
  freeing a chunk with size less than or equal to MXFAST. Trimming is
1372
  instead delayed until subsequent freeing of larger chunks. However,
1373
  you can still force an attempted trim by calling malloc_trim.
10347 ripley 1374
 
26855 ripley 1375
  Also, trimming is not generally possible in cases where
1376
  the main arena is obtained via mmap.
10347 ripley 1377
 
26855 ripley 1378
  Note that the trick some people use of mallocing a huge space and
1379
  then freeing it at program startup, in an attempt to reserve system
1380
  memory, doesn't have the intended effect under automatic trimming,
1381
  since that memory will immediately be returned to the system.
1382
*/
10347 ripley 1383
 
26855 ripley 1384
#define M_TRIM_THRESHOLD       -1
10347 ripley 1385
 
26855 ripley 1386
#ifndef DEFAULT_TRIM_THRESHOLD
1387
#define DEFAULT_TRIM_THRESHOLD (256 * 1024)
10347 ripley 1388
#endif
1389
 
26855 ripley 1390
/*
1391
  M_TOP_PAD is the amount of extra `padding' space to allocate or
1392
  retain whenever sbrk is called. It is used in two ways internally:
10347 ripley 1393
 
26855 ripley 1394
  * When sbrk is called to extend the top of the arena to satisfy
1395
  a new malloc request, this much padding is added to the sbrk
1396
  request.
10347 ripley 1397
 
26855 ripley 1398
  * When malloc_trim is called automatically from free(),
1399
  it is used as the `pad' argument.
10347 ripley 1400
 
26855 ripley 1401
  In both cases, the actual amount of padding is rounded
1402
  so that the end of the arena is always a system page boundary.
10347 ripley 1403
 
26855 ripley 1404
  The main reason for using padding is to avoid calling sbrk so
1405
  often. Having even a small pad greatly reduces the likelihood
1406
  that nearly every malloc request during program start-up (or
1407
  after trimming) will invoke sbrk, which needlessly wastes
1408
  time.
10347 ripley 1409
 
26855 ripley 1410
  Automatic rounding-up to page-size units is normally sufficient
1411
  to avoid measurable overhead, so the default is 0.  However, in
1412
  systems where sbrk is relatively slow, it can pay to increase
1413
  this value, at the expense of carrying around more memory than
1414
  the program needs.
1415
*/
1416
 
1417
#define M_TOP_PAD              -2
1418
 
1419
#ifndef DEFAULT_TOP_PAD
1420
#define DEFAULT_TOP_PAD        (0)
10347 ripley 1421
#endif
1422
 
26855 ripley 1423
/*
1424
  M_MMAP_THRESHOLD is the request size threshold for using mmap()
1425
  to service a request. Requests of at least this size that cannot
1426
  be allocated using already-existing space will be serviced via mmap.
1427
  (If enough normal freed space already exists it is used instead.)
10347 ripley 1428
 
26855 ripley 1429
  Using mmap segregates relatively large chunks of memory so that
1430
  they can be individually obtained and released from the host
1431
  system. A request serviced through mmap is never reused by any
1432
  other request (at least not directly; the system may just so
1433
  happen to remap successive requests to the same locations).
10347 ripley 1434
 
26855 ripley 1435
  Segregating space in this way has the benefits that:
10347 ripley 1436
 
26855 ripley 1437
   1. Mmapped space can ALWAYS be individually released back 
1438
      to the system, which helps keep the system level memory 
1439
      demands of a long-lived program low. 
1440
   2. Mapped memory can never become `locked' between
1441
      other chunks, as can happen with normally allocated chunks, which
1442
      means that even trimming via malloc_trim would not release them.
1443
   3. On some systems with "holes" in address spaces, mmap can obtain
1444
      memory that sbrk cannot.
10347 ripley 1445
 
26855 ripley 1446
  However, it has the disadvantages that:
10347 ripley 1447
 
26855 ripley 1448
   1. The space cannot be reclaimed, consolidated, and then
1449
      used to service later requests, as happens with normal chunks.
1450
   2. It can lead to more wastage because of mmap page alignment
1451
      requirements
1452
   3. It causes malloc performance to be more dependent on host
1453
      system memory management support routines which may vary in
1454
      implementation quality and may impose arbitrary
1455
      limitations. Generally, servicing a request via normal
1456
      malloc steps is faster than going through a system's mmap.
10347 ripley 1457
 
26855 ripley 1458
  The advantages of mmap nearly always outweigh disadvantages for
1459
  "large" chunks, but the value of "large" varies across systems.  The
1460
  default is an empirically derived value that works well in most
1461
  systems.
1462
*/
1463
 
1464
#define M_MMAP_THRESHOLD      -3
1465
 
1466
#ifndef DEFAULT_MMAP_THRESHOLD
1467
#define DEFAULT_MMAP_THRESHOLD (256 * 1024)
10347 ripley 1468
#endif
1469
 
26855 ripley 1470
/*
1471
  M_MMAP_MAX is the maximum number of requests to simultaneously
1472
  service using mmap. This parameter exists because
1473
. Some systems have a limited number of internal tables for
1474
  use by mmap, and using more than a few of them may degrade
1475
  performance.
10347 ripley 1476
 
26855 ripley 1477
  The default is set to a value that serves only as a safeguard.
1478
  Setting to 0 disables use of mmap for servicing large requests.  If
1479
  HAVE_MMAP is not set, the default value is 0, and attempts to set it
1480
  to non-zero values in mallopt will fail.
1481
*/
10347 ripley 1482
 
26855 ripley 1483
#define M_MMAP_MAX             -4
1484
 
1485
#ifndef DEFAULT_MMAP_MAX
1486
#if HAVE_MMAP
1487
#define DEFAULT_MMAP_MAX       (65536)
10347 ripley 1488
#else
26855 ripley 1489
#define DEFAULT_MMAP_MAX       (0)
10347 ripley 1490
#endif
26855 ripley 1491
#endif
10347 ripley 1492
 
1493
#ifdef __cplusplus
1494
};  /* end of extern "C" */
1495
#endif
1496
 
26855 ripley 1497
/* 
1498
  ========================================================================
1499
  To make a fully customizable malloc.h header file, cut everything
1500
  above this line, put into file malloc.h, edit to suit, and #include it 
1501
  on the next line, as well as in programs that use this malloc.
1502
  ========================================================================
1503
*/
10347 ripley 1504
 
26855 ripley 1505
/* #include "malloc.h" */
10347 ripley 1506
 
26855 ripley 1507
/* --------------------- public wrappers ---------------------- */
10425 ripley 1508
 
26855 ripley 1509
#ifdef USE_PUBLIC_MALLOC_WRAPPERS
10347 ripley 1510
 
26855 ripley 1511
/* Declare all routines as internal */
1512
#if __STD_C
1513
static Void_t*  mALLOc(size_t);
1514
static void     fREe(Void_t*);
1515
static Void_t*  rEALLOc(Void_t*, size_t);
1516
static Void_t*  mEMALIGn(size_t, size_t);
1517
static Void_t*  vALLOc(size_t);
1518
static Void_t*  pVALLOc(size_t);
1519
static Void_t*  cALLOc(size_t, size_t);
1520
static Void_t** iCALLOc(size_t, size_t, Void_t**);
1521
static Void_t** iCOMALLOc(size_t, size_t*, Void_t**);
1522
static void     cFREe(Void_t*);
1523
static int      mTRIm(size_t);
1524
static size_t   mUSABLe(Void_t*);
1525
static void     mSTATs();
1526
static int      mALLOPt(int, int);
1527
static struct mallinfo mALLINFo(void);
1528
#else
1529
static Void_t*  mALLOc();
1530
static void     fREe();
1531
static Void_t*  rEALLOc();
1532
static Void_t*  mEMALIGn();
1533
static Void_t*  vALLOc();
1534
static Void_t*  pVALLOc();
1535
static Void_t*  cALLOc();
1536
static Void_t** iCALLOc();
1537
static Void_t** iCOMALLOc();
1538
static void     cFREe();
1539
static int      mTRIm();
1540
static size_t   mUSABLe();
1541
static void     mSTATs();
1542
static int      mALLOPt();
1543
static struct mallinfo mALLINFo();
1544
#endif
1545
 
10347 ripley 1546
/*
26855 ripley 1547
  MALLOC_PREACTION and MALLOC_POSTACTION should be
1548
  defined to return 0 on success, and nonzero on failure.
1549
  The return value of MALLOC_POSTACTION is currently ignored
1550
  in wrapper functions since there is no reasonable default
1551
  action to take on failure.
1552
*/
10347 ripley 1553
 
1554
 
26855 ripley 1555
#ifdef USE_MALLOC_LOCK
1556
 
10347 ripley 1557
#ifdef WIN32
1558
 
26855 ripley 1559
static int mALLOC_MUTEx;
1560
#define MALLOC_PREACTION   slwait(&mALLOC_MUTEx)
1561
#define MALLOC_POSTACTION  slrelease(&mALLOC_MUTEx)
10347 ripley 1562
 
26855 ripley 1563
#else
10347 ripley 1564
 
26855 ripley 1565
#include <pthread.h>
10347 ripley 1566
 
26855 ripley 1567
static pthread_mutex_t mALLOC_MUTEx = PTHREAD_MUTEX_INITIALIZER;
1568
 
1569
#define MALLOC_PREACTION   pthread_mutex_lock(&mALLOC_MUTEx)
1570
#define MALLOC_POSTACTION  pthread_mutex_unlock(&mALLOC_MUTEx)
1571
 
1572
#endif /* USE_MALLOC_LOCK */
1573
 
1574
#else
1575
 
1576
/* Substitute anything you like for these */
1577
 
1578
#define MALLOC_PREACTION   (0)
1579
#define MALLOC_POSTACTION  (0)
1580
 
1581
#endif
1582
 
1583
Void_t* public_mALLOc(size_t bytes) {
1584
  Void_t* m;
1585
  if (MALLOC_PREACTION != 0) {
1586
    return 0;
1587
  }
1588
  m = mALLOc(bytes);
1589
  if (MALLOC_POSTACTION != 0) {
1590
  }
1591
  return m;
10347 ripley 1592
}
1593
 
26855 ripley 1594
void public_fREe(Void_t* m) {
1595
  if (MALLOC_PREACTION != 0) {
1596
    return;
1597
  }
1598
  fREe(m);
1599
  if (MALLOC_POSTACTION != 0) {
1600
  }
1601
}
10425 ripley 1602
 
26855 ripley 1603
Void_t* public_rEALLOc(Void_t* m, size_t bytes) {
1604
  if (MALLOC_PREACTION != 0) {
1605
    return 0;
1606
  }
1607
  m = rEALLOc(m, bytes);
1608
  if (MALLOC_POSTACTION != 0) {
1609
  }
1610
  return m;
1611
}
10425 ripley 1612
 
26855 ripley 1613
Void_t* public_mEMALIGn(size_t alignment, size_t bytes) {
1614
  Void_t* m;
1615
  if (MALLOC_PREACTION != 0) {
1616
    return 0;
1617
  }
1618
  m = mEMALIGn(alignment, bytes);
1619
  if (MALLOC_POSTACTION != 0) {
1620
  }
1621
  return m;
10347 ripley 1622
}
1623
 
26855 ripley 1624
Void_t* public_vALLOc(size_t bytes) {
1625
  Void_t* m;
1626
  if (MALLOC_PREACTION != 0) {
1627
    return 0;
1628
  }
1629
  m = vALLOc(bytes);
1630
  if (MALLOC_POSTACTION != 0) {
1631
  }
1632
  return m;
1633
}
12256 pd 1634
 
26855 ripley 1635
Void_t* public_pVALLOc(size_t bytes) {
1636
  Void_t* m;
1637
  if (MALLOC_PREACTION != 0) {
1638
    return 0;
1639
  }
1640
  m = pVALLOc(bytes);
1641
  if (MALLOC_POSTACTION != 0) {
1642
  }
1643
  return m;
1644
}
10347 ripley 1645
 
26855 ripley 1646
Void_t* public_cALLOc(size_t n, size_t elem_size) {
1647
  Void_t* m;
1648
  if (MALLOC_PREACTION != 0) {
1649
    return 0;
1650
  }
1651
  m = cALLOc(n, elem_size);
1652
  if (MALLOC_POSTACTION != 0) {
1653
  }
1654
  return m;
1655
}
11007 ripley 1656
 
10347 ripley 1657
 
26855 ripley 1658
Void_t** public_iCALLOc(size_t n, size_t elem_size, Void_t** chunks) {
1659
  Void_t** m;
1660
  if (MALLOC_PREACTION != 0) {
1661
    return 0;
1662
  }
1663
  m = iCALLOc(n, elem_size, chunks);
1664
  if (MALLOC_POSTACTION != 0) {
1665
  }
1666
  return m;
1667
}
10347 ripley 1668
 
26855 ripley 1669
Void_t** public_iCOMALLOc(size_t n, size_t sizes[], Void_t** chunks) {
1670
  Void_t** m;
1671
  if (MALLOC_PREACTION != 0) {
1672
    return 0;
1673
  }
1674
  m = iCOMALLOc(n, sizes, chunks);
1675
  if (MALLOC_POSTACTION != 0) {
1676
  }
1677
  return m;
1678
}
10347 ripley 1679
 
26855 ripley 1680
void public_cFREe(Void_t* m) {
1681
  if (MALLOC_PREACTION != 0) {
1682
    return;
1683
  }
1684
  cFREe(m);
1685
  if (MALLOC_POSTACTION != 0) {
1686
  }
10347 ripley 1687
}
1688
 
26855 ripley 1689
int public_mTRIm(size_t s) {
1690
  int result;
1691
  if (MALLOC_PREACTION != 0) {
1692
    return 0;
1693
  }
1694
  result = mTRIm(s);
1695
  if (MALLOC_POSTACTION != 0) {
1696
  }
1697
  return result;
1698
}
10347 ripley 1699
 
26855 ripley 1700
size_t public_mUSABLe(Void_t* m) {
1701
  size_t result;
1702
  if (MALLOC_PREACTION != 0) {
1703
    return 0;
1704
  }
1705
  result = mUSABLe(m);
1706
  if (MALLOC_POSTACTION != 0) {
1707
  }
1708
  return result;
1709
}
1710
 
1711
void public_mSTATs() {
1712
  if (MALLOC_PREACTION != 0) {
1713
    return;
1714
  }
1715
  mSTATs();
1716
  if (MALLOC_POSTACTION != 0) {
1717
  }
1718
}
1719
 
1720
struct mallinfo public_mALLINFo() {
1721
  struct mallinfo m;
1722
  if (MALLOC_PREACTION != 0) {
1723
    struct mallinfo nm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1724
    return nm;
1725
  }
1726
  m = mALLINFo();
1727
  if (MALLOC_POSTACTION != 0) {
1728
  }
1729
  return m;
1730
}
1731
 
1732
int public_mALLOPt(int p, int v) {
1733
  int result;
1734
  if (MALLOC_PREACTION != 0) {
1735
    return 0;
1736
  }
1737
  result = mALLOPt(p, v);
1738
  if (MALLOC_POSTACTION != 0) {
1739
  }
1740
  return result;
1741
}
1742
 
1743
#endif
1744
 
1745
 
1746
 
1747
/* ------------- Optional versions of memcopy ---------------- */
1748
 
1749
 
1750
#if USE_MEMCPY
1751
 
1752
/* 
1753
  Note: memcpy is ONLY invoked with non-overlapping regions,
1754
  so the (usually slower) memmove is not needed.
1755
*/
1756
 
1757
#define MALLOC_COPY(dest, src, nbytes)  memcpy(dest, src, nbytes)
1758
#define MALLOC_ZERO(dest, nbytes)       memset(dest, 0,   nbytes)
1759
 
1760
#else /* !USE_MEMCPY */
1761
 
1762
/* Use Duff's device for good zeroing/copying performance. */
1763
 
1764
#define MALLOC_ZERO(charp, nbytes)                                            \
1765
do {                                                                          \
1766
  INTERNAL_SIZE_T* mzp = (INTERNAL_SIZE_T*)(charp);                           \
1767
  CHUNK_SIZE_T  mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T);                     \
1768
  long mcn;                                                                   \
1769
  if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; }             \
1770
  switch (mctmp) {                                                            \
1771
    case 0: for(;;) { *mzp++ = 0;                                             \
1772
    case 7:           *mzp++ = 0;                                             \
1773
    case 6:           *mzp++ = 0;                                             \
1774
    case 5:           *mzp++ = 0;                                             \
1775
    case 4:           *mzp++ = 0;                                             \
1776
    case 3:           *mzp++ = 0;                                             \
1777
    case 2:           *mzp++ = 0;                                             \
1778
    case 1:           *mzp++ = 0; if(mcn <= 0) break; mcn--; }                \
1779
  }                                                                           \
1780
} while(0)
1781
 
1782
#define MALLOC_COPY(dest,src,nbytes)                                          \
1783
do {                                                                          \
1784
  INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) src;                            \
1785
  INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) dest;                           \
1786
  CHUNK_SIZE_T  mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T);                     \
1787
  long mcn;                                                                   \
1788
  if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; }             \
1789
  switch (mctmp) {                                                            \
1790
    case 0: for(;;) { *mcdst++ = *mcsrc++;                                    \
1791
    case 7:           *mcdst++ = *mcsrc++;                                    \
1792
    case 6:           *mcdst++ = *mcsrc++;                                    \
1793
    case 5:           *mcdst++ = *mcsrc++;                                    \
1794
    case 4:           *mcdst++ = *mcsrc++;                                    \
1795
    case 3:           *mcdst++ = *mcsrc++;                                    \
1796
    case 2:           *mcdst++ = *mcsrc++;                                    \
1797
    case 1:           *mcdst++ = *mcsrc++; if(mcn <= 0) break; mcn--; }       \
1798
  }                                                                           \
1799
} while(0)
1800
 
1801
#endif
1802
 
1803
/* ------------------ MMAP support ------------------  */
1804
 
1805
 
1806
#if HAVE_MMAP
1807
 
1808
#ifndef LACKS_FCNTL_H
1809
#include <fcntl.h>
1810
#endif
1811
 
1812
#ifndef LACKS_SYS_MMAN_H
1813
#include <sys/mman.h>
1814
#endif
1815
 
1816
#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
1817
#define MAP_ANONYMOUS MAP_ANON
1818
#endif
1819
 
1820
/* 
1821
   Nearly all versions of mmap support MAP_ANONYMOUS, 
1822
   so the following is unlikely to be needed, but is
1823
   supplied just in case.
1824
*/
1825
 
1826
#ifndef MAP_ANONYMOUS
1827
 
1828
static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */
1829
 
1830
#define MMAP(addr, size, prot, flags) ((dev_zero_fd < 0) ? \
1831
 (dev_zero_fd = open("/dev/zero", O_RDWR), \
1832
  mmap((addr), (size), (prot), (flags), dev_zero_fd, 0)) : \
1833
   mmap((addr), (size), (prot), (flags), dev_zero_fd, 0))
1834
 
1835
#else
1836
 
1837
#define MMAP(addr, size, prot, flags) \
1838
 (mmap((addr), (size), (prot), (flags)|MAP_ANONYMOUS, -1, 0))
1839
 
1840
#endif
1841
 
1842
 
1843
#endif /* HAVE_MMAP */
1844
 
1845
 
10347 ripley 1846
/*
26855 ripley 1847
  -----------------------  Chunk representations -----------------------
10347 ripley 1848
*/
1849
 
1850
 
26855 ripley 1851
/*
1852
  This struct declaration is misleading (but accurate and necessary).
1853
  It declares a "view" into memory allowing access to necessary
1854
  fields at known offsets from a given base. See explanation below.
1855
*/
1856
 
1857
struct malloc_chunk {
1858
 
1859
  INTERNAL_SIZE_T      prev_size;  /* Size of previous chunk (if free).  */
1860
  INTERNAL_SIZE_T      size;       /* Size in bytes, including overhead. */
1861
 
1862
  struct malloc_chunk* fd;         /* double links -- used only if free. */
10347 ripley 1863
  struct malloc_chunk* bk;
1864
};
1865
 
26855 ripley 1866
 
10347 ripley 1867
typedef struct malloc_chunk* mchunkptr;
1868
 
1869
/*
1870
   malloc_chunk details:
1871
 
1872
    (The following includes lightly edited explanations by Colin Plumb.)
1873
 
1874
    Chunks of memory are maintained using a `boundary tag' method as
1875
    described in e.g., Knuth or Standish.  (See the paper by Paul
1876
    Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a
1877
    survey of such techniques.)  Sizes of free chunks are stored both
1878
    in the front of each chunk and at the end.  This makes
1879
    consolidating fragmented chunks into bigger chunks very fast.  The
1880
    size fields also hold bits representing whether chunks are free or
1881
    in use.
1882
 
1883
    An allocated chunk looks like this:
1884
 
1885
 
1886
    chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1887
            |             Size of previous chunk, if allocated            | |
1888
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1889
            |             Size of chunk, in bytes                         |P|
1890
      mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1891
            |             User data starts here...                          .
1892
            .                                                               .
1893
            .             (malloc_usable_space() bytes)                     .
1894
            .                                                               |
1895
nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1896
            |             Size of chunk                                     |
1897
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1898
 
1899
 
1900
    Where "chunk" is the front of the chunk for the purpose of most of
1901
    the malloc code, but "mem" is the pointer that is returned to the
1902
    user.  "Nextchunk" is the beginning of the next contiguous chunk.
1903
 
1904
    Chunks always begin on even word boundries, so the mem portion
1905
    (which is returned to the user) is also on an even word boundary, and
26855 ripley 1906
    thus at least double-word aligned.
10347 ripley 1907
 
1908
    Free chunks are stored in circular doubly-linked lists, and look like this:
1909
 
1910
    chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1911
            |             Size of previous chunk                            |
1912
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1913
    `head:' |             Size of chunk, in bytes                         |P|
1914
      mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1915
            |             Forward pointer to next chunk in list             |
1916
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1917
            |             Back pointer to previous chunk in list            |
1918
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1919
            |             Unused space (may be 0 bytes long)                .
1920
            .                                                               .
1921
            .                                                               |
1922
nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1923
    `foot:' |             Size of chunk, in bytes                           |
1924
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1925
 
1926
    The P (PREV_INUSE) bit, stored in the unused low-order bit of the
1927
    chunk size (which is always a multiple of two words), is an in-use
1928
    bit for the *previous* chunk.  If that bit is *clear*, then the
1929
    word before the current chunk size contains the previous chunk
1930
    size, and can be used to find the front of the previous chunk.
26855 ripley 1931
    The very first chunk allocated always has this bit set,
1932
    preventing access to non-existent (or non-owned) memory. If
1933
    prev_inuse is set for any given chunk, then you CANNOT determine
1934
    the size of the previous chunk, and might even get a memory
1935
    addressing fault when trying to do so.
10347 ripley 1936
 
1937
    Note that the `foot' of the current chunk is actually represented
26855 ripley 1938
    as the prev_size of the NEXT chunk. This makes it easier to
1939
    deal with alignments etc but can be very confusing when trying
1940
    to extend or adapt this code.
10347 ripley 1941
 
1942
    The two exceptions to all this are
1943
 
26855 ripley 1944
     1. The special chunk `top' doesn't bother using the
1945
        trailing size field since there is no next contiguous chunk
1946
        that would have to index off it. After initialization, `top'
1947
        is forced to always exist.  If it would become less than
1948
        MINSIZE bytes long, it is replenished.
10347 ripley 1949
 
1950
     2. Chunks allocated via mmap, which have the second-lowest-order
1951
        bit (IS_MMAPPED) set in their size fields.  Because they are
26855 ripley 1952
        allocated one-by-one, each must contain its own trailing size field.
10347 ripley 1953
 
26855 ripley 1954
*/
10347 ripley 1955
 
26855 ripley 1956
/*
1957
  ---------- Size and alignment checks and conversions ----------
10347 ripley 1958
*/
1959
 
26855 ripley 1960
/* conversion from malloc headers to user pointers, and back */
10347 ripley 1961
 
26855 ripley 1962
#define chunk2mem(p)   ((Void_t*)((char*)(p) + 2*SIZE_SZ))
1963
#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ))
10347 ripley 1964
 
26855 ripley 1965
/* The smallest possible chunk */
1966
#define MIN_CHUNK_SIZE        (sizeof(struct malloc_chunk))
10347 ripley 1967
 
26855 ripley 1968
/* The smallest size we can malloc is an aligned minimal chunk */
10347 ripley 1969
 
26855 ripley 1970
#define MINSIZE  \
1971
  (CHUNK_SIZE_T)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK))
10347 ripley 1972
 
26855 ripley 1973
/* Check if m has acceptable alignment */
10347 ripley 1974
 
26855 ripley 1975
#define aligned_OK(m)  (((PTR_UINT)((m)) & (MALLOC_ALIGN_MASK)) == 0)
10347 ripley 1976
 
1977
 
26855 ripley 1978
/* 
1979
   Check if a request is so large that it would wrap around zero when
1980
   padded and aligned. To simplify some other code, the bound is made
1981
   low enough so that adding MINSIZE will also not wrap around sero.
1982
*/
10347 ripley 1983
 
26855 ripley 1984
#define REQUEST_OUT_OF_RANGE(req)                                 \
1985
  ((CHUNK_SIZE_T)(req) >=                                        \
1986
   (CHUNK_SIZE_T)(INTERNAL_SIZE_T)(-2 * MINSIZE))    
10347 ripley 1987
 
26855 ripley 1988
/* pad request bytes into a usable size -- internal version */
10347 ripley 1989
 
26855 ripley 1990
#define request2size(req)                                         \
1991
  (((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE)  ?             \
1992
   MINSIZE :                                                      \
1993
   ((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)
10347 ripley 1994
 
26855 ripley 1995
/*  Same, except also perform argument check */
10347 ripley 1996
 
26855 ripley 1997
#define checked_request2size(req, sz)                             \
1998
  if (REQUEST_OUT_OF_RANGE(req)) {                                \
1999
    MALLOC_FAILURE_ACTION;                                        \
2000
    return 0;                                                     \
2001
  }                                                               \
2002
  (sz) = request2size(req);                                              
10347 ripley 2003
 
2004
/*
26855 ripley 2005
  --------------- Physical chunk operations ---------------
10347 ripley 2006
*/
2007
 
2008
 
2009
/* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */
2010
#define PREV_INUSE 0x1
2011
 
26855 ripley 2012
/* extract inuse bit of previous chunk */
2013
#define prev_inuse(p)       ((p)->size & PREV_INUSE)
2014
 
2015
 
10347 ripley 2016
/* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */
2017
#define IS_MMAPPED 0x2
2018
 
26855 ripley 2019
/* check for mmap()'ed chunk */
2020
#define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED)
10347 ripley 2021
 
26855 ripley 2022
/* 
2023
  Bits to mask off when extracting size 
2024
 
2025
  Note: IS_MMAPPED is intentionally not masked off from size field in
2026
  macros for which mmapped chunks should never be seen. This should
2027
  cause helpful core dumps to occur if it is tried by accident by
2028
  people extending or adapting this malloc.
2029
*/
10347 ripley 2030
#define SIZE_BITS (PREV_INUSE|IS_MMAPPED)
2031
 
26855 ripley 2032
/* Get size, ignoring use bits */
2033
#define chunksize(p)         ((p)->size & ~(SIZE_BITS))
10347 ripley 2034
 
26855 ripley 2035
 
10347 ripley 2036
/* Ptr to next physical malloc_chunk. */
2037
#define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->size & ~PREV_INUSE) ))
2038
 
2039
/* Ptr to previous physical malloc_chunk */
26855 ripley 2040
#define prev_chunk(p) ((mchunkptr)( ((char*)(p)) - ((p)->prev_size) ))
10347 ripley 2041
 
2042
/* Treat space at ptr + offset as a chunk */
2043
#define chunk_at_offset(p, s)  ((mchunkptr)(((char*)(p)) + (s)))
2044
 
2045
/* extract p's inuse bit */
2046
#define inuse(p)\
2047
((((mchunkptr)(((char*)(p))+((p)->size & ~PREV_INUSE)))->size) & PREV_INUSE)
2048
 
26855 ripley 2049
/* set/clear chunk as being inuse without otherwise disturbing */
10347 ripley 2050
#define set_inuse(p)\
2051
((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size |= PREV_INUSE
2052
 
2053
#define clear_inuse(p)\
2054
((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size &= ~(PREV_INUSE)
2055
 
26855 ripley 2056
 
10347 ripley 2057
/* check/set/clear inuse bits in known places */
2058
#define inuse_bit_at_offset(p, s)\
2059
 (((mchunkptr)(((char*)(p)) + (s)))->size & PREV_INUSE)
2060
 
2061
#define set_inuse_bit_at_offset(p, s)\
2062
 (((mchunkptr)(((char*)(p)) + (s)))->size |= PREV_INUSE)
2063
 
2064
#define clear_inuse_bit_at_offset(p, s)\
2065
 (((mchunkptr)(((char*)(p)) + (s)))->size &= ~(PREV_INUSE))
2066
 
2067
 
26855 ripley 2068
/* Set size at head, without disturbing its use bit */
2069
#define set_head_size(p, s)  ((p)->size = (((p)->size & PREV_INUSE) | (s)))
10347 ripley 2070
 
26855 ripley 2071
/* Set size/use field */
2072
#define set_head(p, s)       ((p)->size = (s))
2073
 
2074
/* Set size at footer (only when chunk is not in use) */
2075
#define set_foot(p, s)       (((mchunkptr)((char*)(p) + (s)))->prev_size = (s))
2076
 
2077
 
10347 ripley 2078
/*
26855 ripley 2079
  -------------------- Internal data structures --------------------
2080
 
2081
   All internal state is held in an instance of malloc_state defined
2082
   below. There are no other static variables, except in two optional
2083
   cases: 
2084
   * If USE_MALLOC_LOCK is defined, the mALLOC_MUTEx declared above. 
2085
   * If HAVE_MMAP is true, but mmap doesn't support
2086
     MAP_ANONYMOUS, a dummy file descriptor for mmap.
2087
 
2088
   Beware of lots of tricks that minimize the total bookkeeping space
2089
   requirements. The result is a little over 1K bytes (for 4byte
2090
   pointers and size_t.)
10347 ripley 2091
*/
2092
 
26855 ripley 2093
/*
2094
  Bins
10347 ripley 2095
 
26855 ripley 2096
    An array of bin headers for free chunks. Each bin is doubly
2097
    linked.  The bins are approximately proportionally (log) spaced.
2098
    There are a lot of these bins (128). This may look excessive, but
2099
    works very well in practice.  Most bins hold sizes that are
2100
    unusual as malloc request sizes, but are more usual for fragments
2101
    and consolidated sets of chunks, which is what these bins hold, so
2102
    they can be found quickly.  All procedures maintain the invariant
2103
    that no consolidated chunk physically borders another one, so each
2104
    chunk in a list is known to be preceeded and followed by either
2105
    inuse chunks or the ends of memory.
10347 ripley 2106
 
26855 ripley 2107
    Chunks in bins are kept in size order, with ties going to the
2108
    approximately least recently used chunk. Ordering isn't needed
2109
    for the small bins, which all contain the same-sized chunks, but
2110
    facilitates best-fit allocation for larger chunks. These lists
2111
    are just sequential. Keeping them in order almost never requires
2112
    enough traversal to warrant using fancier ordered data
2113
    structures.  
10347 ripley 2114
 
26855 ripley 2115
    Chunks of the same size are linked with the most
2116
    recently freed at the front, and allocations are taken from the
2117
    back.  This results in LRU (FIFO) allocation order, which tends
2118
    to give each chunk an equal opportunity to be consolidated with
2119
    adjacent freed chunks, resulting in larger free chunks and less
2120
    fragmentation.
10347 ripley 2121
 
26855 ripley 2122
    To simplify use in double-linked lists, each bin header acts
2123
    as a malloc_chunk. This avoids special-casing for headers.
2124
    But to conserve space and improve locality, we allocate
2125
    only the fd/bk pointers of bins, and then use repositioning tricks
2126
    to treat these as the fields of a malloc_chunk*.  
2127
*/
10347 ripley 2128
 
26855 ripley 2129
typedef struct malloc_chunk* mbinptr;
10347 ripley 2130
 
26855 ripley 2131
/* addressing -- note that bin_at(0) does not exist */
2132
#define bin_at(m, i) ((mbinptr)((char*)&((m)->bins[(i)<<1]) - (SIZE_SZ<<1)))
10347 ripley 2133
 
26855 ripley 2134
/* analog of ++bin */
2135
#define next_bin(b)  ((mbinptr)((char*)(b) + (sizeof(mchunkptr)<<1)))
10347 ripley 2136
 
26855 ripley 2137
/* Reminders about list directionality within bins */
2138
#define first(b)     ((b)->fd)
2139
#define last(b)      ((b)->bk)
10347 ripley 2140
 
26855 ripley 2141
/* Take a chunk off a bin list */
2142
#define unlink(P, BK, FD) {                                            \
2143
  FD = P->fd;                                                          \
2144
  BK = P->bk;                                                          \
2145
  FD->bk = BK;                                                         \
2146
  BK->fd = FD;                                                         \
2147
}
10347 ripley 2148
 
2149
/*
26855 ripley 2150
  Indexing
10347 ripley 2151
 
2152
    Bins for sizes < 512 bytes contain chunks of all the same size, spaced
26855 ripley 2153
    8 bytes apart. Larger bins are approximately logarithmically spaced:
10347 ripley 2154
 
2155
    64 bins of size       8
2156
    32 bins of size      64
2157
    16 bins of size     512
2158
     8 bins of size    4096
2159
     4 bins of size   32768
2160
     2 bins of size  262144
2161
     1 bin  of size what's left
2162
 
26855 ripley 2163
    The bins top out around 1MB because we expect to service large
2164
    requests via mmap.
2165
*/
10347 ripley 2166
 
26855 ripley 2167
#define NBINS              96
2168
#define NSMALLBINS         32
2169
#define SMALLBIN_WIDTH      8
2170
#define MIN_LARGE_SIZE    256
10347 ripley 2171
 
26855 ripley 2172
#define in_smallbin_range(sz)  \
2173
  ((CHUNK_SIZE_T)(sz) < (CHUNK_SIZE_T)MIN_LARGE_SIZE)
2174
 
2175
#define smallbin_index(sz)     (((unsigned)(sz)) >> 3)
2176
 
2177
/*
2178
  Compute index for size. We expect this to be inlined when
2179
  compiled with optimization, else not, which works out well.
10347 ripley 2180
*/
26855 ripley 2181
static int largebin_index(unsigned int sz) {
2182
  unsigned int  x = sz >> SMALLBIN_WIDTH; 
2183
  unsigned int m;            /* bit position of highest set bit of m */
10347 ripley 2184
 
26855 ripley 2185
  if (x >= 0x10000) return NBINS-1;
10347 ripley 2186
 
26855 ripley 2187
  /* On intel, use BSRL instruction to find highest bit */
2188
#if defined(__GNUC__) && defined(i386)
10347 ripley 2189
 
26855 ripley 2190
  __asm__("bsrl %1,%0\n\t"
2191
          : "=r" (m) 
2192
          : "g"  (x));
10347 ripley 2193
 
26855 ripley 2194
#else
2195
  {
2196
    /*
2197
      Based on branch-free nlz algorithm in chapter 5 of Henry
2198
      S. Warren Jr's book "Hacker's Delight".
2199
    */
10347 ripley 2200
 
26855 ripley 2201
    unsigned int n = ((x - 0x100) >> 16) & 8;
2202
    x <<= n; 
2203
    m = ((x - 0x1000) >> 16) & 4;
2204
    n += m; 
2205
    x <<= m; 
2206
    m = ((x - 0x4000) >> 16) & 2;
2207
    n += m; 
2208
    x = (x << m) >> 14;
2209
    m = 13 - n + (x & ~(x>>1));
2210
  }
2211
#endif
2212
 
2213
  /* Use next 2 bits to create finer-granularity bins */
2214
  return NSMALLBINS + (m << 2) + ((sz >> (m + 6)) & 3);
2215
}
2216
 
2217
#define bin_index(sz) \
2218
 ((in_smallbin_range(sz)) ? smallbin_index(sz) : largebin_index(sz))
2219
 
10347 ripley 2220
/*
26855 ripley 2221
  FIRST_SORTED_BIN_SIZE is the chunk size corresponding to the
2222
  first bin that is maintained in sorted order. This must
2223
  be the smallest size corresponding to a given bin.
2224
 
2225
  Normally, this should be MIN_LARGE_SIZE. But you can weaken
2226
  best fit guarantees to sometimes speed up malloc by increasing value.
2227
  Doing this means that malloc may choose a chunk that is 
2228
  non-best-fitting by up to the width of the bin.
2229
 
2230
  Some useful cutoff values:
2231
      512 - all bins sorted
2232
     2560 - leaves bins <=     64 bytes wide unsorted  
2233
    12288 - leaves bins <=    512 bytes wide unsorted
2234
    65536 - leaves bins <=   4096 bytes wide unsorted
2235
   262144 - leaves bins <=  32768 bytes wide unsorted
2236
       -1 - no bins sorted (not recommended!)
10347 ripley 2237
*/
2238
 
26855 ripley 2239
#define FIRST_SORTED_BIN_SIZE MIN_LARGE_SIZE 
2240
/* #define FIRST_SORTED_BIN_SIZE 65536 */
10347 ripley 2241
 
26855 ripley 2242
/*
2243
  Unsorted chunks
10347 ripley 2244
 
26855 ripley 2245
    All remainders from chunk splits, as well as all returned chunks,
2246
    are first placed in the "unsorted" bin. They are then placed
2247
    in regular bins after malloc gives them ONE chance to be used before
2248
    binning. So, basically, the unsorted_chunks list acts as a queue,
2249
    with chunks being placed on it in free (and malloc_consolidate),
2250
    and taken off (to be either used or placed in bins) in malloc.
2251
*/
2252
 
2253
/* The otherwise unindexable 1-bin is used to hold unsorted chunks. */
2254
#define unsorted_chunks(M)          (bin_at(M, 1))
2255
 
10347 ripley 2256
/*
26855 ripley 2257
  Top
2258
 
2259
    The top-most available chunk (i.e., the one bordering the end of
2260
    available memory) is treated specially. It is never included in
2261
    any bin, is used only if no other chunk is available, and is
2262
    released back to the system if it is very large (see
2263
    M_TRIM_THRESHOLD).  Because top initially
2264
    points to its own bin with initial zero size, thus forcing
2265
    extension on the first malloc request, we avoid having any special
2266
    code in malloc to check whether it even exists yet. But we still
2267
    need to do so when getting memory from system, so we make
2268
    initial_top treat the bin as a legal but unusable chunk during the
2269
    interval between initialization and the first call to
2270
    sYSMALLOc. (This is somewhat delicate, since it relies on
2271
    the 2 preceding words to be zero during this interval as well.)
10347 ripley 2272
*/
2273
 
26855 ripley 2274
/* Conveniently, the unsorted bin can be used as dummy top on first call */
2275
#define initial_top(M)              (unsorted_chunks(M))
10347 ripley 2276
 
26855 ripley 2277
/*
2278
  Binmap
10347 ripley 2279
 
26855 ripley 2280
    To help compensate for the large number of bins, a one-level index
2281
    structure is used for bin-by-bin searching.  `binmap' is a
2282
    bitvector recording whether bins are definitely empty so they can
2283
    be skipped over during during traversals.  The bits are NOT always
2284
    cleared as soon as bins are empty, but instead only
2285
    when they are noticed to be empty during traversal in malloc.
2286
*/
10347 ripley 2287
 
26855 ripley 2288
/* Conservatively use 32 bits per map word, even if on 64bit system */
2289
#define BINMAPSHIFT      5
2290
#define BITSPERMAP       (1U << BINMAPSHIFT)
2291
#define BINMAPSIZE       (NBINS / BITSPERMAP)
10347 ripley 2292
 
26855 ripley 2293
#define idx2block(i)     ((i) >> BINMAPSHIFT)
2294
#define idx2bit(i)       ((1U << ((i) & ((1U << BINMAPSHIFT)-1))))
10347 ripley 2295
 
26855 ripley 2296
#define mark_bin(m,i)    ((m)->binmap[idx2block(i)] |=  idx2bit(i))
2297
#define unmark_bin(m,i)  ((m)->binmap[idx2block(i)] &= ~(idx2bit(i)))
2298
#define get_binmap(m,i)  ((m)->binmap[idx2block(i)] &   idx2bit(i))
10347 ripley 2299
 
26855 ripley 2300
/*
2301
  Fastbins
10347 ripley 2302
 
26855 ripley 2303
    An array of lists holding recently freed small chunks.  Fastbins
2304
    are not doubly linked.  It is faster to single-link them, and
2305
    since chunks are never removed from the middles of these lists,
2306
    double linking is not necessary. Also, unlike regular bins, they
2307
    are not even processed in FIFO order (they use faster LIFO) since
2308
    ordering doesn't much matter in the transient contexts in which
2309
    fastbins are normally used.
2310
 
2311
    Chunks in fastbins keep their inuse bit set, so they cannot
2312
    be consolidated with other free chunks. malloc_consolidate
2313
    releases all chunks in fastbins and consolidates them with
2314
    other free chunks. 
2315
*/
2316
 
2317
typedef struct malloc_chunk* mfastbinptr;
2318
 
2319
/* offset 2 to use otherwise unindexable first 2 bins */
2320
#define fastbin_index(sz)        ((((unsigned int)(sz)) >> 3) - 2)
2321
 
2322
/* The maximum fastbin request size we support */
2323
#define MAX_FAST_SIZE     80
2324
 
2325
#define NFASTBINS  (fastbin_index(request2size(MAX_FAST_SIZE))+1)
2326
 
10347 ripley 2327
/*
26855 ripley 2328
  FASTBIN_CONSOLIDATION_THRESHOLD is the size of a chunk in free()
2329
  that triggers automatic consolidation of possibly-surrounding
2330
  fastbin chunks. This is a heuristic, so the exact value should not
2331
  matter too much. It is defined at half the default trim threshold as a
2332
  compromise heuristic to only attempt consolidation if it is likely
2333
  to lead to trimming. However, it is not dynamically tunable, since
2334
  consolidation reduces fragmentation surrounding loarge chunks even 
2335
  if trimming is not used.
10347 ripley 2336
*/
2337
 
26855 ripley 2338
#define FASTBIN_CONSOLIDATION_THRESHOLD  \
2339
  ((unsigned long)(DEFAULT_TRIM_THRESHOLD) >> 1)
2340
 
10347 ripley 2341
/*
26855 ripley 2342
  Since the lowest 2 bits in max_fast don't matter in size comparisons, 
2343
  they are used as flags.
10347 ripley 2344
*/
2345
 
26855 ripley 2346
/*
2347
  ANYCHUNKS_BIT held in max_fast indicates that there may be any
2348
  freed chunks at all. It is set true when entering a chunk into any
2349
  bin.
2350
*/
10347 ripley 2351
 
26855 ripley 2352
#define ANYCHUNKS_BIT        (1U)
10347 ripley 2353
 
26855 ripley 2354
#define have_anychunks(M)     (((M)->max_fast &  ANYCHUNKS_BIT))
2355
#define set_anychunks(M)      ((M)->max_fast |=  ANYCHUNKS_BIT)
2356
#define clear_anychunks(M)    ((M)->max_fast &= ~ANYCHUNKS_BIT)
2357
 
10347 ripley 2358
/*
26855 ripley 2359
  FASTCHUNKS_BIT held in max_fast indicates that there are probably
2360
  some fastbin chunks. It is set true on entering a chunk into any
2361
  fastbin, and cleared only in malloc_consolidate.
10347 ripley 2362
*/
2363
 
26855 ripley 2364
#define FASTCHUNKS_BIT        (2U)
10347 ripley 2365
 
26855 ripley 2366
#define have_fastchunks(M)   (((M)->max_fast &  FASTCHUNKS_BIT))
2367
#define set_fastchunks(M)    ((M)->max_fast |=  (FASTCHUNKS_BIT|ANYCHUNKS_BIT))
2368
#define clear_fastchunks(M)  ((M)->max_fast &= ~(FASTCHUNKS_BIT))
10347 ripley 2369
 
26855 ripley 2370
/* 
2371
   Set value of max_fast. 
2372
   Use impossibly small value if 0.
2373
*/
2374
 
2375
#define set_max_fast(M, s) \
2376
  (M)->max_fast = (((s) == 0)? SMALLBIN_WIDTH: request2size(s)) | \
2377
  ((M)->max_fast &  (FASTCHUNKS_BIT|ANYCHUNKS_BIT))
2378
 
2379
#define get_max_fast(M) \
2380
  ((M)->max_fast & ~(FASTCHUNKS_BIT | ANYCHUNKS_BIT))
2381
 
2382
 
10347 ripley 2383
/*
26855 ripley 2384
  morecore_properties is a status word holding dynamically discovered
2385
  or controlled properties of the morecore function
10347 ripley 2386
*/
2387
 
26855 ripley 2388
#define MORECORE_CONTIGUOUS_BIT  (1U)
10347 ripley 2389
 
26855 ripley 2390
#define contiguous(M) \
2391
        (((M)->morecore_properties &  MORECORE_CONTIGUOUS_BIT))
2392
#define noncontiguous(M) \
2393
        (((M)->morecore_properties &  MORECORE_CONTIGUOUS_BIT) == 0)
2394
#define set_contiguous(M) \
2395
        ((M)->morecore_properties |=  MORECORE_CONTIGUOUS_BIT)
2396
#define set_noncontiguous(M) \
2397
        ((M)->morecore_properties &= ~MORECORE_CONTIGUOUS_BIT)
10347 ripley 2398
 
2399
 
26855 ripley 2400
/*
2401
   ----------- Internal state representation and initialization -----------
2402
*/
10347 ripley 2403
 
26855 ripley 2404
struct malloc_state {
10347 ripley 2405
 
26855 ripley 2406
  /* The maximum chunk size to be eligible for fastbin */
2407
  INTERNAL_SIZE_T  max_fast;   /* low 2 bits used as flags */
10347 ripley 2408
 
26855 ripley 2409
  /* Fastbins */
2410
  mfastbinptr      fastbins[NFASTBINS];
10347 ripley 2411
 
26855 ripley 2412
  /* Base of the topmost chunk -- not otherwise kept in a bin */
2413
  mchunkptr        top;
10347 ripley 2414
 
26855 ripley 2415
  /* The remainder from the most recent split of a small request */
2416
  mchunkptr        last_remainder;
10347 ripley 2417
 
26855 ripley 2418
  /* Normal bins packed as described above */
2419
  mchunkptr        bins[NBINS * 2];
10347 ripley 2420
 
26855 ripley 2421
  /* Bitmap of bins. Trailing zero map handles cases of largest binned size */
2422
  unsigned int     binmap[BINMAPSIZE+1];
10347 ripley 2423
 
26855 ripley 2424
  /* Tunable parameters */
2425
  CHUNK_SIZE_T     trim_threshold;
2426
  INTERNAL_SIZE_T  top_pad;
2427
  INTERNAL_SIZE_T  mmap_threshold;
10347 ripley 2428
 
26855 ripley 2429
  /* Memory map support */
2430
  int              n_mmaps;
2431
  int              n_mmaps_max;
2432
  int              max_n_mmaps;
10347 ripley 2433
 
26855 ripley 2434
  /* Cache malloc_getpagesize */
2435
  unsigned int     pagesize;    
10347 ripley 2436
 
26855 ripley 2437
  /* Track properties of MORECORE */
2438
  unsigned int     morecore_properties;
10347 ripley 2439
 
26855 ripley 2440
  /* Statistics */
2441
  INTERNAL_SIZE_T  mmapped_mem;
2442
  INTERNAL_SIZE_T  sbrked_mem;
2443
  INTERNAL_SIZE_T  max_sbrked_mem;
2444
  INTERNAL_SIZE_T  max_mmapped_mem;
2445
  INTERNAL_SIZE_T  max_total_mem;
2446
};
10347 ripley 2447
 
26855 ripley 2448
typedef struct malloc_state *mstate;
10347 ripley 2449
 
26855 ripley 2450
/* 
2451
   There is exactly one instance of this struct in this malloc.
2452
   If you are adapting this malloc in a way that does NOT use a static
2453
   malloc_state, you MUST explicitly zero-fill it before using. This
2454
   malloc relies on the property that malloc_state is initialized to
2455
   all zeroes (as is true of C statics).
2456
*/
10347 ripley 2457
 
26855 ripley 2458
static struct malloc_state av_;  /* never directly referenced */
2459
 
10347 ripley 2460
/*
26855 ripley 2461
   All uses of av_ are via get_malloc_state().
2462
   At most one "call" to get_malloc_state is made per invocation of
2463
   the public versions of malloc and free, but other routines
2464
   that in turn invoke malloc and/or free may call more then once. 
2465
   Also, it is called in check* routines if DEBUG is set.
10347 ripley 2466
*/
2467
 
26855 ripley 2468
#define get_malloc_state() (&(av_))
10347 ripley 2469
 
26855 ripley 2470
/*
2471
  Initialize a malloc_state struct.
10347 ripley 2472
 
26855 ripley 2473
  This is called only from within malloc_consolidate, which needs
2474
  be called in the same contexts anyway.  It is never called directly
2475
  outside of malloc_consolidate because some optimizing compilers try
2476
  to inline it at all call points, which turns out not to be an
2477
  optimization at all. (Inlining it in malloc_consolidate is fine though.)
2478
*/
2479
 
2480
#if __STD_C
2481
static void malloc_init_state(mstate av)
2482
#else
2483
static void malloc_init_state(av) mstate av;
2484
#endif
2485
{
2486
  int     i;
2487
  mbinptr bin;
2488
 
2489
  /* Establish circular links for normal bins */
2490
  for (i = 1; i < NBINS; ++i) { 
2491
    bin = bin_at(av,i);
2492
    bin->fd = bin->bk = bin;
2493
  }
2494
 
2495
  av->top_pad        = DEFAULT_TOP_PAD;
2496
  av->n_mmaps_max    = DEFAULT_MMAP_MAX;
2497
  av->mmap_threshold = DEFAULT_MMAP_THRESHOLD;
2498
  av->trim_threshold = DEFAULT_TRIM_THRESHOLD;
2499
 
2500
#if MORECORE_CONTIGUOUS
2501
  set_contiguous(av);
2502
#else
2503
  set_noncontiguous(av);
2504
#endif
2505
 
2506
 
2507
  set_max_fast(av, DEFAULT_MXFAST);
2508
 
2509
  av->top            = initial_top(av);
2510
  av->pagesize       = malloc_getpagesize;
2511
}
2512
 
2513
/* 
2514
   Other internal utilities operating on mstates
2515
*/
2516
 
2517
#if __STD_C
2518
static Void_t*  sYSMALLOc(INTERNAL_SIZE_T, mstate);
2519
#ifndef MORECORE_CANNOT_TRIM        
2520
static int      sYSTRIm(size_t, mstate);
2521
#endif
2522
static void     malloc_consolidate(mstate);
2523
static Void_t** iALLOc(size_t, size_t*, int, Void_t**);
2524
#else
2525
static Void_t*  sYSMALLOc();
26945 ripley 2526
#ifndef MORECORE_CANNOT_TRIM        
26855 ripley 2527
static int      sYSTRIm();
26945 ripley 2528
#endif
26855 ripley 2529
static void     malloc_consolidate();
2530
static Void_t** iALLOc();
2531
#endif
2532
 
10347 ripley 2533
/*
26855 ripley 2534
  Debugging support
2535
 
10347 ripley 2536
  These routines make a number of assertions about the states
2537
  of data structures that should be true at all times. If any
2538
  are not true, it's very likely that a user program has somehow
2539
  trashed memory. (It's also possible that there is a coding error
2540
  in malloc. In which case, please report it!)
2541
*/
2542
 
26855 ripley 2543
#if ! DEBUG
2544
 
2545
#define check_chunk(P)
2546
#define check_free_chunk(P)
2547
#define check_inuse_chunk(P)
2548
#define check_remalloced_chunk(P,N)
2549
#define check_malloced_chunk(P,N)
2550
#define check_malloc_state()
2551
 
2552
#else
2553
#define check_chunk(P)              do_check_chunk(P)
2554
#define check_free_chunk(P)         do_check_free_chunk(P)
2555
#define check_inuse_chunk(P)        do_check_inuse_chunk(P)
2556
#define check_remalloced_chunk(P,N) do_check_remalloced_chunk(P,N)
2557
#define check_malloced_chunk(P,N)   do_check_malloced_chunk(P,N)
2558
#define check_malloc_state()        do_check_malloc_state()
2559
 
2560
/*
2561
  Properties of all chunks
2562
*/
2563
 
10347 ripley 2564
#if __STD_C
2565
static void do_check_chunk(mchunkptr p)
2566
#else
2567
static void do_check_chunk(p) mchunkptr p;
2568
#endif
2569
{
26855 ripley 2570
  mstate av = get_malloc_state();
2571
  CHUNK_SIZE_T  sz = chunksize(p);
2572
  /* min and max possible addresses assuming contiguous allocation */
2573
  char* max_address = (char*)(av->top) + chunksize(av->top);
2574
  char* min_address = max_address - av->sbrked_mem;
10347 ripley 2575
 
26855 ripley 2576
  if (!chunk_is_mmapped(p)) {
2577
 
2578
    /* Has legal address ... */
2579
    if (p != av->top) {
2580
      if (contiguous(av)) {
2581
        assert(((char*)p) >= min_address);
2582
        assert(((char*)p + sz) <= ((char*)(av->top)));
2583
      }
2584
    }
2585
    else {
2586
      /* top size is always at least MINSIZE */
2587
      assert((CHUNK_SIZE_T)(sz) >= MINSIZE);
2588
      /* top predecessor always marked inuse */
2589
      assert(prev_inuse(p));
2590
    }
2591
 
2592
  }
2593
  else {
2594
#if HAVE_MMAP
2595
    /* address is outside main heap  */
2596
    if (contiguous(av) && av->top != initial_top(av)) {
2597
      assert(((char*)p) < min_address || ((char*)p) > max_address);
2598
    }
2599
    /* chunk is page-aligned */
2600
    assert(((p->prev_size + sz) & (av->pagesize-1)) == 0);
2601
    /* mem is aligned */
2602
    assert(aligned_OK(chunk2mem(p)));
2603
#else
2604
    /* force an appropriate assert violation if debug set */
2605
    assert(!chunk_is_mmapped(p));
2606
#endif
2607
  }
10347 ripley 2608
}
2609
 
26855 ripley 2610
/*
2611
  Properties of free chunks
2612
*/
10347 ripley 2613
 
2614
#if __STD_C
2615
static void do_check_free_chunk(mchunkptr p)
2616
#else
2617
static void do_check_free_chunk(p) mchunkptr p;
2618
#endif
2619
{
26855 ripley 2620
  mstate av = get_malloc_state();
2621
 
10347 ripley 2622
  INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
2623
  mchunkptr next = chunk_at_offset(p, sz);
2624
 
2625
  do_check_chunk(p);
2626
 
26855 ripley 2627
  /* Chunk must claim to be free ... */
10347 ripley 2628
  assert(!inuse(p));
26855 ripley 2629
  assert (!chunk_is_mmapped(p));
10347 ripley 2630
 
2631
  /* Unless a special marker, must have OK fields */
26855 ripley 2632
  if ((CHUNK_SIZE_T)(sz) >= MINSIZE)
10347 ripley 2633
  {
2634
    assert((sz & MALLOC_ALIGN_MASK) == 0);
2635
    assert(aligned_OK(chunk2mem(p)));
2636
    /* ... matching footer field */
2637
    assert(next->prev_size == sz);
2638
    /* ... and is fully consolidated */
2639
    assert(prev_inuse(p));
26855 ripley 2640
    assert (next == av->top || inuse(next));
10347 ripley 2641
 
2642
    /* ... and has minimally sane links */
2643
    assert(p->fd->bk == p);
2644
    assert(p->bk->fd == p);
2645
  }
2646
  else /* markers are always of size SIZE_SZ */
2647
    assert(sz == SIZE_SZ);
2648
}
2649
 
26855 ripley 2650
/*
2651
  Properties of inuse chunks
2652
*/
2653
 
10347 ripley 2654
#if __STD_C
2655
static void do_check_inuse_chunk(mchunkptr p)
2656
#else
2657
static void do_check_inuse_chunk(p) mchunkptr p;
2658
#endif
2659
{
26855 ripley 2660
  mstate av = get_malloc_state();
2661
  mchunkptr next;
10347 ripley 2662
  do_check_chunk(p);
2663
 
26855 ripley 2664
  if (chunk_is_mmapped(p))
2665
    return; /* mmapped chunks have no next/prev */
2666
 
10347 ripley 2667
  /* Check whether it claims to be in use ... */
2668
  assert(inuse(p));
2669
 
26855 ripley 2670
  next = next_chunk(p);
2671
 
10347 ripley 2672
  /* ... and is surrounded by OK chunks.
2673
    Since more things can be checked with free chunks than inuse ones,
2674
    if an inuse chunk borders them and debug is on, it's worth doing them.
2675
  */
26855 ripley 2676
  if (!prev_inuse(p))  {
2677
    /* Note that we cannot even look at prev unless it is not inuse */
10347 ripley 2678
    mchunkptr prv = prev_chunk(p);
2679
    assert(next_chunk(prv) == p);
2680
    do_check_free_chunk(prv);
2681
  }
26855 ripley 2682
 
2683
  if (next == av->top) {
10347 ripley 2684
    assert(prev_inuse(next));
2685
    assert(chunksize(next) >= MINSIZE);
2686
  }
2687
  else if (!inuse(next))
2688
    do_check_free_chunk(next);
2689
}
2690
 
26855 ripley 2691
/*
2692
  Properties of chunks recycled from fastbins
2693
*/
2694
 
10347 ripley 2695
#if __STD_C
26855 ripley 2696
static void do_check_remalloced_chunk(mchunkptr p, INTERNAL_SIZE_T s)
10347 ripley 2697
#else
26855 ripley 2698
static void do_check_remalloced_chunk(p, s) mchunkptr p; INTERNAL_SIZE_T s;
10347 ripley 2699
#endif
2700
{
2701
  INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
2702
 
2703
  do_check_inuse_chunk(p);
2704
 
2705
  /* Legal size ... */
2706
  assert((sz & MALLOC_ALIGN_MASK) == 0);
26855 ripley 2707
  assert((CHUNK_SIZE_T)(sz) >= MINSIZE);
10347 ripley 2708
  /* ... and alignment */
2709
  assert(aligned_OK(chunk2mem(p)));
26855 ripley 2710
  /* chunk is less than MINSIZE more than request */
2711
  assert((long)(sz) - (long)(s) >= 0);
2712
  assert((long)(sz) - (long)(s + MINSIZE) < 0);
10347 ripley 2713
}
2714
 
26855 ripley 2715
/*
2716
  Properties of nonrecycled chunks at the point they are malloced
2717
*/
10347 ripley 2718
 
26855 ripley 2719
#if __STD_C
2720
static void do_check_malloced_chunk(mchunkptr p, INTERNAL_SIZE_T s)
10347 ripley 2721
#else
26855 ripley 2722
static void do_check_malloced_chunk(p, s) mchunkptr p; INTERNAL_SIZE_T s;
10347 ripley 2723
#endif
26855 ripley 2724
{
2725
  /* same as recycled case ... */
2726
  do_check_remalloced_chunk(p, s);
10347 ripley 2727
 
26855 ripley 2728
  /*
2729
    ... plus,  must obey implementation invariant that prev_inuse is
2730
    always true of any allocated chunk; i.e., that each allocated
2731
    chunk borders either a previously allocated and still in-use
2732
    chunk, or the base of its memory arena. This is ensured
2733
    by making all allocations from the the `lowest' part of any found
2734
    chunk.  This does not necessarily hold however for chunks
2735
    recycled via fastbins.
2736
  */
10347 ripley 2737
 
26855 ripley 2738
  assert(prev_inuse(p));
2739
}
10347 ripley 2740
 
2741
 
2742
/*
26855 ripley 2743
  Properties of malloc_state.
10347 ripley 2744
 
26855 ripley 2745
  This may be useful for debugging malloc, as well as detecting user
2746
  programmer errors that somehow write into malloc_state.
2747
 
2748
  If you are extending or experimenting with this malloc, you can
2749
  probably figure out how to hack this routine to print out or
2750
  display chunk addresses, sizes, bins, and other instrumentation.
10347 ripley 2751
*/
2752
 
26855 ripley 2753
static void do_check_malloc_state()
2754
{
2755
  mstate av = get_malloc_state();
2756
  int i;
2757
  mchunkptr p;
2758
  mchunkptr q;
2759
  mbinptr b;
2760
  unsigned int binbit;
2761
  int empty;
2762
  unsigned int idx;
2763
  INTERNAL_SIZE_T size;
2764
  CHUNK_SIZE_T  total = 0;
2765
  int max_fast_bin;
10347 ripley 2766
 
26855 ripley 2767
  /* internal size_t must be no wider than pointer type */
2768
  assert(sizeof(INTERNAL_SIZE_T) <= sizeof(char*));
10347 ripley 2769
 
26855 ripley 2770
  /* alignment is a power of 2 */
2771
  assert((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT-1)) == 0);
10347 ripley 2772
 
26855 ripley 2773
  /* cannot run remaining checks until fully initialized */
2774
  if (av->top == 0 || av->top == initial_top(av))
2775
    return;
10347 ripley 2776
 
26855 ripley 2777
  /* pagesize is a power of 2 */
2778
  assert((av->pagesize & (av->pagesize-1)) == 0);
10347 ripley 2779
 
26855 ripley 2780
  /* properties of fastbins */
10347 ripley 2781
 
26855 ripley 2782
  /* max_fast is in allowed range */
2783
  assert(get_max_fast(av) <= request2size(MAX_FAST_SIZE));
10347 ripley 2784
 
26855 ripley 2785
  max_fast_bin = fastbin_index(av->max_fast);
10347 ripley 2786
 
26855 ripley 2787
  for (i = 0; i < NFASTBINS; ++i) {
2788
    p = av->fastbins[i];
10347 ripley 2789
 
26855 ripley 2790
    /* all bins past max_fast are empty */
2791
    if (i > max_fast_bin)
2792
      assert(p == 0);
10347 ripley 2793
 
26855 ripley 2794
    while (p != 0) {
2795
      /* each chunk claims to be inuse */
2796
      do_check_inuse_chunk(p);
2797
      total += chunksize(p);
2798
      /* chunk belongs in this bin */
2799
      assert(fastbin_index(chunksize(p)) == i);
2800
      p = p->fd;
2801
    }
2802
  }
10347 ripley 2803
 
26855 ripley 2804
  if (total != 0)
2805
    assert(have_fastchunks(av));
2806
  else if (!have_fastchunks(av))
2807
    assert(total == 0);
10347 ripley 2808
 
26855 ripley 2809
  /* check normal bins */
2810
  for (i = 1; i < NBINS; ++i) {
2811
    b = bin_at(av,i);
10347 ripley 2812
 
26855 ripley 2813
    /* binmap is accurate (except for bin 1 == unsorted_chunks) */
2814
    if (i >= 2) {
2815
      binbit = get_binmap(av,i);
2816
      empty = last(b) == b;
2817
      if (!binbit)
2818
        assert(empty);
2819
      else if (!empty)
2820
        assert(binbit);
2821
    }
10347 ripley 2822
 
26855 ripley 2823
    for (p = last(b); p != b; p = p->bk) {
2824
      /* each chunk claims to be free */
2825
      do_check_free_chunk(p);
2826
      size = chunksize(p);
2827
      total += size;
2828
      if (i >= 2) {
2829
        /* chunk belongs in bin */
2830
        idx = bin_index(size);
2831
        assert(idx == i);
2832
        /* lists are sorted */
2833
        if ((CHUNK_SIZE_T) size >= (CHUNK_SIZE_T)(FIRST_SORTED_BIN_SIZE)) {
2834
          assert(p->bk == b || 
2835
                 (CHUNK_SIZE_T)chunksize(p->bk) >= 
2836
                 (CHUNK_SIZE_T)chunksize(p));
2837
        }
2838
      }
2839
      /* chunk is followed by a legal chain of inuse chunks */
2840
      for (q = next_chunk(p);
2841
           (q != av->top && inuse(q) && 
2842
             (CHUNK_SIZE_T)(chunksize(q)) >= MINSIZE);
2843
           q = next_chunk(q))
2844
        do_check_inuse_chunk(q);
2845
    }
2846
  }
10347 ripley 2847
 
26855 ripley 2848
  /* top chunk is OK */
2849
  check_chunk(av->top);
10347 ripley 2850
 
26855 ripley 2851
  /* sanity checks for statistics */
10347 ripley 2852
 
26855 ripley 2853
  assert(total <= (CHUNK_SIZE_T)(av->max_total_mem));
2854
  assert(av->n_mmaps >= 0);
2855
  assert(av->n_mmaps <= av->max_n_mmaps);
10347 ripley 2856
 
26855 ripley 2857
  assert((CHUNK_SIZE_T)(av->sbrked_mem) <=
2858
         (CHUNK_SIZE_T)(av->max_sbrked_mem));
10347 ripley 2859
 
26855 ripley 2860
  assert((CHUNK_SIZE_T)(av->mmapped_mem) <=
2861
         (CHUNK_SIZE_T)(av->max_mmapped_mem));
2862
 
2863
  assert((CHUNK_SIZE_T)(av->max_total_mem) >=
2864
         (CHUNK_SIZE_T)(av->mmapped_mem) + (CHUNK_SIZE_T)(av->sbrked_mem));
2865
}
10347 ripley 2866
#endif
2867
 
2868
 
26855 ripley 2869
/* ----------- Routines dealing with system allocation -------------- */
10347 ripley 2870
 
26855 ripley 2871
/*
2872
  sysmalloc handles malloc cases requiring more memory from the system.
2873
  On entry, it is assumed that av->top does not have enough
2874
  space to service request for nb bytes, thus requiring that av->top
2875
  be extended or replaced.
2876
*/
10347 ripley 2877
 
2878
#if __STD_C
26855 ripley 2879
static Void_t* sYSMALLOc(INTERNAL_SIZE_T nb, mstate av)
10347 ripley 2880
#else
26855 ripley 2881
static Void_t* sYSMALLOc(nb, av) INTERNAL_SIZE_T nb; mstate av;
10347 ripley 2882
#endif
2883
{
26855 ripley 2884
  mchunkptr       old_top;        /* incoming value of av->top */
2885
  INTERNAL_SIZE_T old_size;       /* its size */
2886
  char*           old_end;        /* its end address */
10347 ripley 2887
 
26855 ripley 2888
  long            size;           /* arg to first MORECORE or mmap call */
2889
  char*           brk;            /* return value from MORECORE */
10347 ripley 2890
 
26855 ripley 2891
  long            correction;     /* arg to 2nd MORECORE call */
2892
  char*           snd_brk;        /* 2nd return val */
10347 ripley 2893
 
26855 ripley 2894
  INTERNAL_SIZE_T front_misalign; /* unusable bytes at front of new space */
2895
  INTERNAL_SIZE_T end_misalign;   /* partial page left at end of new space */
2896
  char*           aligned_brk;    /* aligned offset into brk */
10347 ripley 2897
 
26855 ripley 2898
  mchunkptr       p;              /* the allocated/returned chunk */
2899
  mchunkptr       remainder;      /* remainder from allocation */
2900
  CHUNK_SIZE_T    remainder_size; /* its size */
10347 ripley 2901
 
26855 ripley 2902
  CHUNK_SIZE_T    sum;            /* for updating stats */
10347 ripley 2903
 
26855 ripley 2904
  size_t          pagemask  = av->pagesize - 1;
10347 ripley 2905
 
26855 ripley 2906
  /*
2907
    If there is space available in fastbins, consolidate and retry
2908
    malloc from scratch rather than getting memory from system.  This
2909
    can occur only if nb is in smallbin range so we didn't consolidate
2910
    upon entry to malloc. It is much easier to handle this case here
2911
    than in malloc proper.
2912
  */
10347 ripley 2913
 
26855 ripley 2914
  if (have_fastchunks(av)) {
2915
    assert(in_smallbin_range(nb));
2916
    malloc_consolidate(av);
2917
    return mALLOc(nb - MALLOC_ALIGN_MASK);
2918
  }
10347 ripley 2919
 
2920
 
26855 ripley 2921
#if HAVE_MMAP
10347 ripley 2922
 
26855 ripley 2923
  /*
2924
    If have mmap, and the request size meets the mmap threshold, and
2925
    the system supports mmap, and there are few enough currently
2926
    allocated mmapped regions, try to directly map this request
2927
    rather than expanding top.
2928
  */
10347 ripley 2929
 
26855 ripley 2930
  if ((CHUNK_SIZE_T)(nb) >= (CHUNK_SIZE_T)(av->mmap_threshold) &&
2931
      (av->n_mmaps < av->n_mmaps_max)) {
10347 ripley 2932
 
26855 ripley 2933
    char* mm;             /* return value from mmap call*/
10347 ripley 2934
 
26855 ripley 2935
    /*
2936
      Round up size to nearest page.  For mmapped chunks, the overhead
2937
      is one SIZE_SZ unit larger than for normal chunks, because there
2938
      is no following chunk whose prev_size field could be used.
2939
    */
2940
    size = (nb + SIZE_SZ + MALLOC_ALIGN_MASK + pagemask) & ~pagemask;
10347 ripley 2941
 
26945 ripley 2942
    /* DBT - Check that we are not about to exceed the R memory limit */
2943
    if (size + av->mmapped_mem + av->sbrked_mem > R_max_memory) {
2944
	 if(R_Is_Running) 
2945
	     Rf_warning("Reached total allocation of %dMb: see help(memory.size)", R_max_memory/1048576);
2946
	 return (void*) 0;
2947
    }
2948
 
26855 ripley 2949
    /* Don't try if size wraps around 0 */
2950
    if ((CHUNK_SIZE_T)(size) > (CHUNK_SIZE_T)(nb)) {
10347 ripley 2951
 
26855 ripley 2952
      mm = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE));
2953
 
2954
      if (mm != (char*)(MORECORE_FAILURE)) {
2955
 
2956
        /*
2957
          The offset to the start of the mmapped region is stored
2958
          in the prev_size field of the chunk. This allows us to adjust
2959
          returned start address to meet alignment requirements here 
2960
          and in memalign(), and still be able to compute proper
2961
          address argument for later munmap in free() and realloc().
2962
        */
2963
 
2964
        front_misalign = (INTERNAL_SIZE_T)chunk2mem(mm) & MALLOC_ALIGN_MASK;
2965
        if (front_misalign > 0) {
2966
          correction = MALLOC_ALIGNMENT - front_misalign;
2967
          p = (mchunkptr)(mm + correction);
2968
          p->prev_size = correction;
2969
          set_head(p, (size - correction) |IS_MMAPPED);
2970
        }
2971
        else {
2972
          p = (mchunkptr)mm;
2973
          p->prev_size = 0;
2974
          set_head(p, size|IS_MMAPPED);
2975
        }
2976
 
2977
        /* update statistics */
2978
 
2979
        if (++av->n_mmaps > av->max_n_mmaps) 
2980
          av->max_n_mmaps = av->n_mmaps;
2981
 
2982
        sum = av->mmapped_mem += size;
2983
        if (sum > (CHUNK_SIZE_T)(av->max_mmapped_mem)) 
2984
          av->max_mmapped_mem = sum;
2985
        sum += av->sbrked_mem;
2986
        if (sum > (CHUNK_SIZE_T)(av->max_total_mem)) 
2987
          av->max_total_mem = sum;
10347 ripley 2988
 
26855 ripley 2989
        check_chunk(p);
2990
 
2991
        return chunk2mem(p);
2992
      }
2993
    }
2994
  }
2995
#endif
10347 ripley 2996
 
26855 ripley 2997
  /* Record incoming configuration of top */
10347 ripley 2998
 
26855 ripley 2999
  old_top  = av->top;
3000
  old_size = chunksize(old_top);
3001
  old_end  = (char*)(chunk_at_offset(old_top, old_size));
10347 ripley 3002
 
26855 ripley 3003
  brk = snd_brk = (char*)(MORECORE_FAILURE); 
10347 ripley 3004
 
26855 ripley 3005
  /* 
3006
     If not the first time through, we require old_size to be
3007
     at least MINSIZE and to have prev_inuse set.
3008
  */
10347 ripley 3009
 
26855 ripley 3010
  assert((old_top == initial_top(av) && old_size == 0) || 
3011
         ((CHUNK_SIZE_T) (old_size) >= MINSIZE &&
3012
          prev_inuse(old_top)));
10347 ripley 3013
 
26855 ripley 3014
  /* Precondition: not enough current space to satisfy nb request */
3015
  assert((CHUNK_SIZE_T)(old_size) < (CHUNK_SIZE_T)(nb + MINSIZE));
10347 ripley 3016
 
26855 ripley 3017
  /* Precondition: all fastbins are consolidated */
3018
  assert(!have_fastchunks(av));
10347 ripley 3019
 
3020
 
26855 ripley 3021
  /* Request enough space for nb + pad + overhead */
10347 ripley 3022
 
26855 ripley 3023
  size = nb + av->top_pad + MINSIZE;
10347 ripley 3024
 
26855 ripley 3025
  /*
3026
    If contiguous, we can subtract out existing space that we hope to
3027
    combine with new space. We add it back later only if
3028
    we don't actually get contiguous space.
3029
  */
10347 ripley 3030
 
26855 ripley 3031
  if (contiguous(av))
3032
    size -= old_size;
10347 ripley 3033
 
26855 ripley 3034
  /*
3035
    Round to a multiple of page size.
3036
    If MORECORE is not contiguous, this ensures that we only call it
3037
    with whole-page arguments.  And if MORECORE is contiguous and
3038
    this is not first time through, this preserves page-alignment of
3039
    previous calls. Otherwise, we correct to page-align below.
3040
  */
10347 ripley 3041
 
26855 ripley 3042
  size = (size + pagemask) & ~pagemask;
10347 ripley 3043
 
26945 ripley 3044
  /* DBT - Check that we are not about to exceed the R memory limit */
3045
  if (size + av->mmapped_mem + av->sbrked_mem > R_max_memory) {
3046
	if(R_Is_Running) 
3047
	    Rf_warning("Reached total allocation of %dMb: see help(memory.size)", R_max_memory/1048576);
3048
	return (void*)-1;
3049
  }
3050
 
26855 ripley 3051
  /*
3052
    Don't try to call MORECORE if argument is so big as to appear
3053
    negative. Note that since mmap takes size_t arg, it may succeed
3054
    below even if we cannot call MORECORE.
3055
  */
10425 ripley 3056
 
26855 ripley 3057
  if (size > 0) 
3058
    brk = (char*)(MORECORE(size));
10347 ripley 3059
 
26855 ripley 3060
  /*
3061
    If have mmap, try using it as a backup when MORECORE fails or
3062
    cannot be used. This is worth doing on systems that have "holes" in
3063
    address space, so sbrk cannot extend to give contiguous space, but
3064
    space is available elsewhere.  Note that we ignore mmap max count
3065
    and threshold limits, since the space will not be used as a
3066
    segregated mmap region.
3067
  */
10347 ripley 3068
 
26855 ripley 3069
#if HAVE_MMAP
3070
  if (brk == (char*)(MORECORE_FAILURE)) {
10347 ripley 3071
 
26855 ripley 3072
    /* Cannot merge with old top, so add its size back in */
3073
    if (contiguous(av))
3074
      size = (size + old_size + pagemask) & ~pagemask;
10347 ripley 3075
 
26855 ripley 3076
    /* If we are relying on mmap as backup, then use larger units */
3077
    if ((CHUNK_SIZE_T)(size) < (CHUNK_SIZE_T)(MMAP_AS_MORECORE_SIZE))
3078
      size = MMAP_AS_MORECORE_SIZE;
10347 ripley 3079
 
26855 ripley 3080
    /* Don't try if size wraps around 0 */
3081
    if ((CHUNK_SIZE_T)(size) > (CHUNK_SIZE_T)(nb)) {
3082
 
3083
      brk = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE));
3084
 
3085
      if (brk != (char*)(MORECORE_FAILURE)) {
3086
 
3087
        /* We do not need, and cannot use, another sbrk call to find end */
3088
        snd_brk = brk + size;
3089
 
3090
        /* 
3091
           Record that we no longer have a contiguous sbrk region. 
3092
           After the first time mmap is used as backup, we do not
3093
           ever rely on contiguous space since this could incorrectly
3094
           bridge regions.
3095
        */
3096
        set_noncontiguous(av);
10347 ripley 3097
      }
3098
    }
3099
  }
26855 ripley 3100
#endif
10347 ripley 3101
 
26855 ripley 3102
  if (brk != (char*)(MORECORE_FAILURE)) {
3103
    av->sbrked_mem += size;
10347 ripley 3104
 
26855 ripley 3105
    /*
3106
      If MORECORE extends previous space, we can likewise extend top size.
3107
    */
3108
 
3109
    if (brk == old_end && snd_brk == (char*)(MORECORE_FAILURE)) {
3110
      set_head(old_top, (size + old_size) | PREV_INUSE);
3111
    }
10347 ripley 3112
 
26855 ripley 3113
    /*
3114
      Otherwise, make adjustments:
3115
 
3116
      * If the first time through or noncontiguous, we need to call sbrk
3117
        just to find out where the end of memory lies.
10347 ripley 3118
 
26855 ripley 3119
      * We need to ensure that all returned chunks from malloc will meet
3120
        MALLOC_ALIGNMENT
10347 ripley 3121
 
26855 ripley 3122
      * If there was an intervening foreign sbrk, we need to adjust sbrk
3123
        request size to account for fact that we will not be able to
3124
        combine new space with existing space in old_top.
10347 ripley 3125
 
26855 ripley 3126
      * Almost all systems internally allocate whole pages at a time, in
3127
        which case we might as well use the whole last page of request.
3128
        So we allocate enough more memory to hit a page boundary now,
3129
        which in turn causes future contiguous calls to page-align.
3130
    */
3131
 
3132
    else {
3133
      front_misalign = 0;
3134
      end_misalign = 0;
3135
      correction = 0;
3136
      aligned_brk = brk;
10347 ripley 3137
 
26855 ripley 3138
      /*
3139
        If MORECORE returns an address lower than we have seen before,
3140
        we know it isn't really contiguous.  This and some subsequent
3141
        checks help cope with non-conforming MORECORE functions and
3142
        the presence of "foreign" calls to MORECORE from outside of
3143
        malloc or by other threads.  We cannot guarantee to detect
3144
        these in all cases, but cope with the ones we do detect.
3145
      */
3146
      if (contiguous(av) && old_size != 0 && brk < old_end) {
3147
        set_noncontiguous(av);
3148
      }
3149
 
3150
      /* handle contiguous cases */
3151
      if (contiguous(av)) { 
10347 ripley 3152
 
26855 ripley 3153
        /* 
3154
           We can tolerate forward non-contiguities here (usually due
3155
           to foreign calls) but treat them as part of our space for
3156
           stats reporting.
3157
        */
3158
        if (old_size != 0) 
3159
          av->sbrked_mem += brk - old_end;
3160
 
3161
        /* Guarantee alignment of first new chunk made from this space */
10347 ripley 3162
 
26855 ripley 3163
        front_misalign = (INTERNAL_SIZE_T)chunk2mem(brk) & MALLOC_ALIGN_MASK;
3164
        if (front_misalign > 0) {
10347 ripley 3165
 
26855 ripley 3166
          /*
3167
            Skip over some bytes to arrive at an aligned position.
3168
            We don't need to specially mark these wasted front bytes.
3169
            They will never be accessed anyway because
3170
            prev_inuse of av->top (and any chunk created from its start)
3171
            is always true after initialization.
3172
          */
10347 ripley 3173
 
26855 ripley 3174
          correction = MALLOC_ALIGNMENT - front_misalign;
3175
          aligned_brk += correction;
3176
        }
3177
 
3178
        /*
3179
          If this isn't adjacent to existing space, then we will not
3180
          be able to merge with old_top space, so must add to 2nd request.
3181
        */
3182
 
3183
        correction += old_size;
3184
 
3185
        /* Extend the end address to hit a page boundary */
3186
        end_misalign = (INTERNAL_SIZE_T)(brk + size + correction);
3187
        correction += ((end_misalign + pagemask) & ~pagemask) - end_misalign;
3188
 
3189
        assert(correction >= 0);
3190
        snd_brk = (char*)(MORECORE(correction));
3191
 
3192
        if (snd_brk == (char*)(MORECORE_FAILURE)) {
3193
          /*
3194
            If can't allocate correction, try to at least find out current
3195
            brk.  It might be enough to proceed without failing.
3196
          */
3197
          correction = 0;
3198
          snd_brk = (char*)(MORECORE(0));
3199
        }
3200
        else if (snd_brk < brk) {
3201
          /*
3202
            If the second call gives noncontiguous space even though
3203
            it says it won't, the only course of action is to ignore
3204
            results of second call, and conservatively estimate where
3205
            the first call left us. Also set noncontiguous, so this
3206
            won't happen again, leaving at most one hole.
3207
 
3208
            Note that this check is intrinsically incomplete.  Because
3209
            MORECORE is allowed to give more space than we ask for,
3210
            there is no reliable way to detect a noncontiguity
3211
            producing a forward gap for the second call.
3212
          */
3213
          snd_brk = brk + size;
3214
          correction = 0;
3215
          set_noncontiguous(av);
3216
        }
10347 ripley 3217
 
26855 ripley 3218
      }
3219
 
3220
      /* handle non-contiguous cases */
3221
      else { 
3222
        /* MORECORE/mmap must correctly align */
3223
        assert(aligned_OK(chunk2mem(brk)));
3224
 
3225
        /* Find out current end of memory */
3226
        if (snd_brk == (char*)(MORECORE_FAILURE)) {
3227
          snd_brk = (char*)(MORECORE(0));
3228
          av->sbrked_mem += snd_brk - brk - size;
3229
        }
3230
      }
3231
 
3232
      /* Adjust top based on results of second sbrk */
3233
      if (snd_brk != (char*)(MORECORE_FAILURE)) {
3234
        av->top = (mchunkptr)aligned_brk;
3235
        set_head(av->top, (snd_brk - aligned_brk + correction) | PREV_INUSE);
3236
        av->sbrked_mem += correction;
3237
 
3238
        /*
3239
          If not the first time through, we either have a
3240
          gap due to foreign sbrk or a non-contiguous region.  Insert a
3241
          double fencepost at old_top to prevent consolidation with space
3242
          we don't own. These fenceposts are artificial chunks that are
3243
          marked as inuse and are in any case too small to use.  We need
3244
          two to make sizes and alignments work out.
3245
        */
3246
 
3247
        if (old_size != 0) {
3248
          /* 
3249
             Shrink old_top to insert fenceposts, keeping size a
3250
             multiple of MALLOC_ALIGNMENT. We know there is at least
3251
             enough space in old_top to do this.
3252
          */
3253
          old_size = (old_size - 3*SIZE_SZ) & ~MALLOC_ALIGN_MASK;
3254
          set_head(old_top, old_size | PREV_INUSE);
3255
 
3256
          /*
3257
            Note that the following assignments completely overwrite
3258
            old_top when old_size was previously MINSIZE.  This is
3259
            intentional. We need the fencepost, even if old_top otherwise gets
3260
            lost.
3261
          */
3262
          chunk_at_offset(old_top, old_size          )->size =
3263
            SIZE_SZ|PREV_INUSE;
10347 ripley 3264
 
26855 ripley 3265
          chunk_at_offset(old_top, old_size + SIZE_SZ)->size =
3266
            SIZE_SZ|PREV_INUSE;
10347 ripley 3267
 
26855 ripley 3268
          /* 
3269
             If possible, release the rest, suppressing trimming.
3270
          */
3271
          if (old_size >= MINSIZE) {
3272
            INTERNAL_SIZE_T tt = av->trim_threshold;
3273
            av->trim_threshold = (INTERNAL_SIZE_T)(-1);
3274
            fREe(chunk2mem(old_top));
3275
            av->trim_threshold = tt;
3276
          }
3277
        }
3278
      }
3279
    }
3280
 
3281
    /* Update statistics */
3282
    sum = av->sbrked_mem;
3283
    if (sum > (CHUNK_SIZE_T)(av->max_sbrked_mem))
3284
      av->max_sbrked_mem = sum;
3285
 
3286
    sum += av->mmapped_mem;
3287
    if (sum > (CHUNK_SIZE_T)(av->max_total_mem))
3288
      av->max_total_mem = sum;
10347 ripley 3289
 
26855 ripley 3290
    check_malloc_state();
3291
 
3292
    /* finally, do the allocation */
10347 ripley 3293
 
26855 ripley 3294
    p = av->top;
3295
    size = chunksize(p);
3296
 
3297
    /* check that one of the above allocation paths succeeded */
3298
    if ((CHUNK_SIZE_T)(size) >= (CHUNK_SIZE_T)(nb + MINSIZE)) {
3299
      remainder_size = size - nb;
3300
      remainder = chunk_at_offset(p, nb);
3301
      av->top = remainder;
3302
      set_head(p, nb | PREV_INUSE);
3303
      set_head(remainder, remainder_size | PREV_INUSE);
3304
      check_malloced_chunk(p, nb);
3305
      return chunk2mem(p);
3306
    }
10347 ripley 3307
 
26855 ripley 3308
  }
10347 ripley 3309
 
26855 ripley 3310
  /* catch all failure paths */
3311
  MALLOC_FAILURE_ACTION;
3312
  return 0;
3313
}
3314
 
3315
 
3316
 
3317
 
3318
#ifndef MORECORE_CANNOT_TRIM        
3319
/*
3320
  sYSTRIm is an inverse of sorts to sYSMALLOc.  It gives memory back
3321
  to the system (via negative arguments to sbrk) if there is unused
3322
  memory at the `high' end of the malloc pool. It is called
3323
  automatically by free() when top space exceeds the trim
3324
  threshold. It is also called by the public malloc_trim routine.  It
3325
  returns 1 if it actually released any memory, else 0.
10347 ripley 3326
*/
3327
 
3328
#if __STD_C
26855 ripley 3329
static int sYSTRIm(size_t pad, mstate av)
3330
#else
3331
static int sYSTRIm(pad, av) size_t pad; mstate av;
3332
#endif
3333
{
3334
  long  top_size;        /* Amount of top-most memory */
3335
  long  extra;           /* Amount to release */
3336
  long  released;        /* Amount actually released */
3337
  char* current_brk;     /* address returned by pre-check sbrk call */
3338
  char* new_brk;         /* address returned by post-check sbrk call */
3339
  size_t pagesz;
3340
 
3341
  pagesz = av->pagesize;
3342
  top_size = chunksize(av->top);
3343
 
3344
  /* Release in pagesize units, keeping at least one page */
3345
  extra = ((top_size - pad - MINSIZE + (pagesz-1)) / pagesz - 1) * pagesz;
3346
 
3347
  if (extra > 0) {
3348
 
3349
    /*
3350
      Only proceed if end of memory is where we last set it.
3351
      This avoids problems if there were foreign sbrk calls.
3352
    */
3353
    current_brk = (char*)(MORECORE(0));
3354
    if (current_brk == (char*)(av->top) + top_size) {
3355
 
3356
      /*
3357
        Attempt to release memory. We ignore MORECORE return value,
3358
        and instead call again to find out where new end of memory is.
3359
        This avoids problems if first call releases less than we asked,
3360
        of if failure somehow altered brk value. (We could still
3361
        encounter problems if it altered brk in some very bad way,
3362
        but the only thing we can do is adjust anyway, which will cause
3363
        some downstream failure.)
3364
      */
3365
 
3366
      MORECORE(-extra);
3367
      new_brk = (char*)(MORECORE(0));
3368
 
3369
      if (new_brk != (char*)MORECORE_FAILURE) {
3370
        released = (long)(current_brk - new_brk);
3371
 
3372
        if (released != 0) {
3373
          /* Success. Adjust top. */
3374
          av->sbrked_mem -= released;
3375
          set_head(av->top, (top_size - released) | PREV_INUSE);
3376
          check_malloc_state();
3377
          return 1;
3378
        }
3379
      }
3380
    }
3381
  }
3382
  return 0;
3383
}
3384
#endif
3385
 
3386
/*
3387
  ------------------------------ malloc ------------------------------
3388
*/
3389
 
3390
 
3391
#if __STD_C
10347 ripley 3392
Void_t* mALLOc(size_t bytes)
3393
#else
26855 ripley 3394
  Void_t* mALLOc(bytes) size_t bytes;
10347 ripley 3395
#endif
3396
{
26855 ripley 3397
  mstate av = get_malloc_state();
10347 ripley 3398
 
26855 ripley 3399
  INTERNAL_SIZE_T nb;               /* normalized request size */
3400
  unsigned int    idx;              /* associated bin index */
3401
  mbinptr         bin;              /* associated bin */
3402
  mfastbinptr*    fb;               /* associated fastbin */
10347 ripley 3403
 
26855 ripley 3404
  mchunkptr       victim;           /* inspected/selected chunk */
3405
  INTERNAL_SIZE_T size;             /* its size */
3406
  int             victim_index;     /* its bin index */
10425 ripley 3407
 
26855 ripley 3408
  mchunkptr       remainder;        /* remainder from a split */
3409
  CHUNK_SIZE_T    remainder_size;   /* its size */
10425 ripley 3410
 
26855 ripley 3411
  unsigned int    block;            /* bit map traverser */
3412
  unsigned int    bit;              /* bit map traverser */
3413
  unsigned int    map;              /* current word of binmap */
10347 ripley 3414
 
26855 ripley 3415
  mchunkptr       fwd;              /* misc temp for linking */
3416
  mchunkptr       bck;              /* misc temp for linking */
10347 ripley 3417
 
26855 ripley 3418
  /*
3419
    Convert request size to internal form by adding SIZE_SZ bytes
3420
    overhead plus possibly more to obtain necessary alignment and/or
3421
    to obtain a size of at least MINSIZE, the smallest allocatable
3422
    size. Also, checked_request2size traps (returning 0) request sizes
3423
    that are so large that they wrap around zero when padded and
3424
    aligned.
3425
  */
10347 ripley 3426
 
26855 ripley 3427
  checked_request2size(bytes, nb);
10347 ripley 3428
 
26855 ripley 3429
  /*
3430
    Bypass search if no frees yet
3431
   */
3432
  if (!have_anychunks(av)) {
3433
    if (av->max_fast == 0) /* initialization check */
3434
      malloc_consolidate(av);
3435
    goto use_top;
3436
  }
3437
 
3438
  /*
3439
    If the size qualifies as a fastbin, first check corresponding bin.
3440
  */
3441
 
3442
  if ((CHUNK_SIZE_T)(nb) <= (CHUNK_SIZE_T)(av->max_fast)) { 
3443
    fb = &(av->fastbins[(fastbin_index(nb))]);
3444
    if ( (victim = *fb) != 0) {
3445
      *fb = victim->fd;
3446
      check_remalloced_chunk(victim, nb);
10347 ripley 3447
      return chunk2mem(victim);
3448
    }
3449
  }
3450
 
26855 ripley 3451
  /*
3452
    If a small request, check regular bin.  Since these "smallbins"
3453
    hold one size each, no searching within bins is necessary.
3454
    (For a large request, we need to wait until unsorted chunks are
3455
    processed to find best fit. But for small ones, fits are exact
3456
    anyway, so we can check now, which is faster.)
3457
  */
10347 ripley 3458
 
26855 ripley 3459
  if (in_smallbin_range(nb)) {
3460
    idx = smallbin_index(nb);
3461
    bin = bin_at(av,idx);
10347 ripley 3462
 
26855 ripley 3463
    if ( (victim = last(bin)) != bin) {
3464
      bck = victim->bk;
3465
      set_inuse_bit_at_offset(victim, nb);
3466
      bin->bk = bck;
3467
      bck->fd = bin;
3468
 
3469
      check_malloced_chunk(victim, nb);
3470
      return chunk2mem(victim);
10347 ripley 3471
    }
26855 ripley 3472
  }
10347 ripley 3473
 
26855 ripley 3474
  /* 
3475
     If this is a large request, consolidate fastbins before continuing.
3476
     While it might look excessive to kill all fastbins before
3477
     even seeing if there is space available, this avoids
3478
     fragmentation problems normally associated with fastbins.
3479
     Also, in practice, programs tend to have runs of either small or
3480
     large requests, but less often mixtures, so consolidation is not 
3481
     invoked all that often in most programs. And the programs that
3482
     it is called frequently in otherwise tend to fragment.
3483
  */
10347 ripley 3484
 
26855 ripley 3485
  else {
3486
    idx = largebin_index(nb);
3487
    if (have_fastchunks(av)) 
3488
      malloc_consolidate(av);
10347 ripley 3489
  }
3490
 
26855 ripley 3491
  /*
3492
    Process recently freed or remaindered chunks, taking one only if
3493
    it is exact fit, or, if this a small request, the chunk is remainder from
3494
    the most recent non-exact fit.  Place other traversed chunks in
3495
    bins.  Note that this step is the only place in any routine where
3496
    chunks are placed in bins.
3497
  */
3498
 
3499
  while ( (victim = unsorted_chunks(av)->bk) != unsorted_chunks(av)) {
3500
    bck = victim->bk;
3501
    size = chunksize(victim);
3502
 
3503
    /* 
3504
       If a small request, try to use last remainder if it is the
3505
       only chunk in unsorted bin.  This helps promote locality for
3506
       runs of consecutive small requests. This is the only
3507
       exception to best-fit, and applies only when there is
3508
       no exact fit for a small chunk.
3509
    */
3510
 
3511
    if (in_smallbin_range(nb) && 
3512
        bck == unsorted_chunks(av) &&
3513
        victim == av->last_remainder &&
3514
        (CHUNK_SIZE_T)(size) > (CHUNK_SIZE_T)(nb + MINSIZE)) {
3515
 
3516
      /* split and reattach remainder */
3517
      remainder_size = size - nb;
10347 ripley 3518
      remainder = chunk_at_offset(victim, nb);
26855 ripley 3519
      unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
3520
      av->last_remainder = remainder; 
3521
      remainder->bk = remainder->fd = unsorted_chunks(av);
3522
 
10347 ripley 3523
      set_head(victim, nb | PREV_INUSE);
3524
      set_head(remainder, remainder_size | PREV_INUSE);
3525
      set_foot(remainder, remainder_size);
26855 ripley 3526
 
10347 ripley 3527
      check_malloced_chunk(victim, nb);
3528
      return chunk2mem(victim);
3529
    }
26855 ripley 3530
 
3531
    /* remove from unsorted list */
3532
    unsorted_chunks(av)->bk = bck;
3533
    bck->fd = unsorted_chunks(av);
3534
 
3535
    /* Take now instead of binning if exact fit */
3536
 
3537
    if (size == nb) {
3538
      set_inuse_bit_at_offset(victim, size);
10347 ripley 3539
      check_malloced_chunk(victim, nb);
3540
      return chunk2mem(victim);
3541
    }
26855 ripley 3542
 
3543
    /* place chunk in bin */
3544
 
3545
    if (in_smallbin_range(size)) {
3546
      victim_index = smallbin_index(size);
3547
      bck = bin_at(av, victim_index);
3548
      fwd = bck->fd;
3549
    }
3550
    else {
3551
      victim_index = largebin_index(size);
3552
      bck = bin_at(av, victim_index);
3553
      fwd = bck->fd;
3554
 
3555
      if (fwd != bck) {
3556
        /* if smaller than smallest, place first */
3557
        if ((CHUNK_SIZE_T)(size) < (CHUNK_SIZE_T)(bck->bk->size)) {
3558
          fwd = bck;
3559
          bck = bck->bk;
3560
        }
3561
        else if ((CHUNK_SIZE_T)(size) >= 
3562
                 (CHUNK_SIZE_T)(FIRST_SORTED_BIN_SIZE)) {
3563
 
3564
          /* maintain large bins in sorted order */
3565
          size |= PREV_INUSE; /* Or with inuse bit to speed comparisons */
3566
          while ((CHUNK_SIZE_T)(size) < (CHUNK_SIZE_T)(fwd->size)) 
3567
            fwd = fwd->fd;
3568
          bck = fwd->bk;
3569
        }
3570
      }
3571
    }
3572
 
3573
    mark_bin(av, victim_index);
3574
    victim->bk = bck;
3575
    victim->fd = fwd;
3576
    fwd->bk = victim;
3577
    bck->fd = victim;
10347 ripley 3578
  }
26855 ripley 3579
 
3580
  /*
3581
    If a large request, scan through the chunks of current bin to
3582
    find one that fits.  (This will be the smallest that fits unless
3583
    FIRST_SORTED_BIN_SIZE has been changed from default.)  This is
3584
    the only step where an unbounded number of chunks might be
3585
    scanned without doing anything useful with them. However the
3586
    lists tend to be short.
3587
  */
3588
 
3589
  if (!in_smallbin_range(nb)) {
3590
    bin = bin_at(av, idx);
3591
 
3592
    for (victim = last(bin); victim != bin; victim = victim->bk) {
3593
      size = chunksize(victim);
3594
 
3595
      if ((CHUNK_SIZE_T)(size) >= (CHUNK_SIZE_T)(nb)) {
3596
        remainder_size = size - nb;
3597
        unlink(victim, bck, fwd);
3598
 
3599
        /* Exhaust */
3600
        if (remainder_size < MINSIZE)  {
3601
          set_inuse_bit_at_offset(victim, size);
3602
          check_malloced_chunk(victim, nb);
3603
          return chunk2mem(victim);
3604
        }
3605
        /* Split */
3606
        else {
3607
          remainder = chunk_at_offset(victim, nb);
3608
          unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
3609
          remainder->bk = remainder->fd = unsorted_chunks(av);
3610
          set_head(victim, nb | PREV_INUSE);
3611
          set_head(remainder, remainder_size | PREV_INUSE);
3612
          set_foot(remainder, remainder_size);
3613
          check_malloced_chunk(victim, nb);
3614
          return chunk2mem(victim);
3615
        } 
3616
      }
3617
    }    
3618
  }
10347 ripley 3619
 
3620
  /*
26855 ripley 3621
    Search for a chunk by scanning bins, starting with next largest
3622
    bin. This search is strictly by best-fit; i.e., the smallest
3623
    (with ties going to approximately the least recently used) chunk
3624
    that fits is selected.
3625
 
3626
    The bitmap avoids needing to check that most blocks are nonempty.
10347 ripley 3627
  */
26855 ripley 3628
 
3629
  ++idx;
3630
  bin = bin_at(av,idx);
3631
  block = idx2block(idx);
3632
  map = av->binmap[block];
3633
  bit = idx2bit(idx);
3634
 
3635
  for (;;) {
3636
 
3637
    /* Skip rest of block if there are no more set bits in this block.  */
3638
    if (bit > map || bit == 0) {
3639
      do {
3640
        if (++block >= BINMAPSIZE)  /* out of bins */
3641
          goto use_top;
3642
      } while ( (map = av->binmap[block]) == 0);
3643
 
3644
      bin = bin_at(av, (block << BINMAPSHIFT));
3645
      bit = 1;
3646
    }
3647
 
3648
    /* Advance to bin with set bit. There must be one. */
3649
    while ((bit & map) == 0) {
3650
      bin = next_bin(bin);
3651
      bit <<= 1;
3652
      assert(bit != 0);
3653
    }
3654
 
3655
    /* Inspect the bin. It is likely to be non-empty */
3656
    victim = last(bin);
3657
 
3658
    /*  If a false alarm (empty bin), clear the bit. */
3659
    if (victim == bin) {
3660
      av->binmap[block] = map &= ~bit; /* Write through */
3661
      bin = next_bin(bin);
3662
      bit <<= 1;
3663
    }
3664
 
3665
    else {
3666
      size = chunksize(victim);
3667
 
3668
      /*  We know the first chunk in this bin is big enough to use. */
3669
      assert((CHUNK_SIZE_T)(size) >= (CHUNK_SIZE_T)(nb));
3670
 
3671
      remainder_size = size - nb;
3672
 
3673
      /* unlink */
3674
      bck = victim->bk;
3675
      bin->bk = bck;
3676
      bck->fd = bin;
3677
 
3678
      /* Exhaust */
3679
      if (remainder_size < MINSIZE) {
3680
        set_inuse_bit_at_offset(victim, size);
3681
        check_malloced_chunk(victim, nb);
3682
        return chunk2mem(victim);
10347 ripley 3683
      }
26855 ripley 3684
 
3685
      /* Split */
3686
      else {
3687
        remainder = chunk_at_offset(victim, nb);
3688
 
3689
        unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
3690
        remainder->bk = remainder->fd = unsorted_chunks(av);
3691
        /* advertise as last remainder */
3692
        if (in_smallbin_range(nb)) 
3693
          av->last_remainder = remainder; 
3694
 
3695
        set_head(victim, nb | PREV_INUSE);
3696
        set_head(remainder, remainder_size | PREV_INUSE);
3697
        set_foot(remainder, remainder_size);
3698
        check_malloced_chunk(victim, nb);
3699
        return chunk2mem(victim);
3700
      }
10347 ripley 3701
    }
26855 ripley 3702
  }
10347 ripley 3703
 
26855 ripley 3704
  use_top:    
3705
  /*
3706
    If large enough, split off the chunk bordering the end of memory
3707
    (held in av->top). Note that this is in accord with the best-fit
3708
    search rule.  In effect, av->top is treated as larger (and thus
3709
    less well fitting) than any other available chunk since it can
3710
    be extended to be as large as necessary (up to system
3711
    limitations).
3712
 
3713
    We require that av->top always exists (i.e., has size >=
3714
    MINSIZE) after initialization, so if it would otherwise be
3715
    exhuasted by current request, it is replenished. (The main
3716
    reason for ensuring it exists is that we may need MINSIZE space
3717
    to put in fenceposts in sysmalloc.)
3718
  */
3719
 
3720
  victim = av->top;
3721
  size = chunksize(victim);
3722
 
3723
  if ((CHUNK_SIZE_T)(size) >= (CHUNK_SIZE_T)(nb + MINSIZE)) {
3724
    remainder_size = size - nb;
3725
    remainder = chunk_at_offset(victim, nb);
3726
    av->top = remainder;
3727
    set_head(victim, nb | PREV_INUSE);
3728
    set_head(remainder, remainder_size | PREV_INUSE);
3729
 
3730
    check_malloced_chunk(victim, nb);
3731
    return chunk2mem(victim);
3732
  }
3733
 
3734
  /* 
3735
     If no space in top, relay to handle system-dependent cases 
3736
  */
3737
  return sYSMALLOc(nb, av);    
3738
}
10347 ripley 3739
 
26855 ripley 3740
/*
3741
  ------------------------------ free ------------------------------
3742
*/
10347 ripley 3743
 
26855 ripley 3744
#if __STD_C
3745
void fREe(Void_t* mem)
3746
#else
3747
void fREe(mem) Void_t* mem;
3748
#endif
3749
{
3750
  mstate av = get_malloc_state();
10347 ripley 3751
 
26855 ripley 3752
  mchunkptr       p;           /* chunk corresponding to mem */
3753
  INTERNAL_SIZE_T size;        /* its size */
3754
  mfastbinptr*    fb;          /* associated fastbin */
3755
  mchunkptr       nextchunk;   /* next contiguous chunk */
3756
  INTERNAL_SIZE_T nextsize;    /* its size */
3757
  int             nextinuse;   /* true if nextchunk is used */
3758
  INTERNAL_SIZE_T prevsize;    /* size of previous contiguous chunk */
3759
  mchunkptr       bck;         /* misc temp for linking */
3760
  mchunkptr       fwd;         /* misc temp for linking */
10347 ripley 3761
 
26855 ripley 3762
  /* free(0) has no effect */
3763
  if (mem != 0) {
3764
    p = mem2chunk(mem);
3765
    size = chunksize(p);
10347 ripley 3766
 
26855 ripley 3767
    check_inuse_chunk(p);
10347 ripley 3768
 
26855 ripley 3769
    /*
3770
      If eligible, place chunk on a fastbin so it can be found
3771
      and used quickly in malloc.
3772
    */
10347 ripley 3773
 
26855 ripley 3774
    if ((CHUNK_SIZE_T)(size) <= (CHUNK_SIZE_T)(av->max_fast)
10347 ripley 3775
 
26855 ripley 3776
#if TRIM_FASTBINS
3777
        /* 
3778
           If TRIM_FASTBINS set, don't place chunks
3779
           bordering top into fastbins
3780
        */
3781
        && (chunk_at_offset(p, size) != av->top)
3782
#endif
3783
        ) {
10347 ripley 3784
 
26855 ripley 3785
      set_fastchunks(av);
3786
      fb = &(av->fastbins[fastbin_index(size)]);
3787
      p->fd = *fb;
3788
      *fb = p;
3789
    }
10347 ripley 3790
 
26855 ripley 3791
    /*
3792
       Consolidate other non-mmapped chunks as they arrive.
3793
    */
10347 ripley 3794
 
26855 ripley 3795
    else if (!chunk_is_mmapped(p)) {
3796
      set_anychunks(av);
10347 ripley 3797
 
26855 ripley 3798
      nextchunk = chunk_at_offset(p, size);
3799
      nextsize = chunksize(nextchunk);
10347 ripley 3800
 
26855 ripley 3801
      /* consolidate backward */
3802
      if (!prev_inuse(p)) {
3803
        prevsize = p->prev_size;
3804
        size += prevsize;
3805
        p = chunk_at_offset(p, -((long) prevsize));
3806
        unlink(p, bck, fwd);
3807
      }
10347 ripley 3808
 
26855 ripley 3809
      if (nextchunk != av->top) {
3810
        /* get and clear inuse bit */
3811
        nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
3812
        set_head(nextchunk, nextsize);
10347 ripley 3813
 
26855 ripley 3814
        /* consolidate forward */
3815
        if (!nextinuse) {
3816
          unlink(nextchunk, bck, fwd);
3817
          size += nextsize;
3818
        }
10347 ripley 3819
 
26855 ripley 3820
        /*
3821
          Place the chunk in unsorted chunk list. Chunks are
3822
          not placed into regular bins until after they have
3823
          been given one chance to be used in malloc.
3824
        */
10347 ripley 3825
 
26855 ripley 3826
        bck = unsorted_chunks(av);
3827
        fwd = bck->fd;
3828
        p->bk = bck;
3829
        p->fd = fwd;
3830
        bck->fd = p;
3831
        fwd->bk = p;
10347 ripley 3832
 
26855 ripley 3833
        set_head(p, size | PREV_INUSE);
3834
        set_foot(p, size);
3835
 
3836
        check_free_chunk(p);
3837
      }
10347 ripley 3838
 
26855 ripley 3839
      /*
3840
         If the chunk borders the current high end of memory,
3841
         consolidate into top
3842
      */
10347 ripley 3843
 
26855 ripley 3844
      else {
3845
        size += nextsize;
3846
        set_head(p, size | PREV_INUSE);
3847
        av->top = p;
3848
        check_chunk(p);
3849
      }
10347 ripley 3850
 
26855 ripley 3851
      /*
3852
        If freeing a large space, consolidate possibly-surrounding
3853
        chunks. Then, if the total unused topmost memory exceeds trim
3854
        threshold, ask malloc_trim to reduce top.
10347 ripley 3855
 
26855 ripley 3856
        Unless max_fast is 0, we don't know if there are fastbins
3857
        bordering top, so we cannot tell for sure whether threshold
3858
        has been reached unless fastbins are consolidated.  But we
3859
        don't want to consolidate on each free.  As a compromise,
3860
        consolidation is performed if FASTBIN_CONSOLIDATION_THRESHOLD
3861
        is reached.
3862
      */
10347 ripley 3863
 
26855 ripley 3864
      if ((CHUNK_SIZE_T)(size) >= FASTBIN_CONSOLIDATION_THRESHOLD) { 
3865
        if (have_fastchunks(av)) 
3866
          malloc_consolidate(av);
10347 ripley 3867
 
26855 ripley 3868
#ifndef MORECORE_CANNOT_TRIM        
3869
        if ((CHUNK_SIZE_T)(chunksize(av->top)) >= 
3870
            (CHUNK_SIZE_T)(av->trim_threshold))
3871
          sYSTRIm(av->top_pad, av);
3872
#endif
3873
      }
10347 ripley 3874
 
26855 ripley 3875
    }
3876
    /*
3877
      If the chunk was allocated via mmap, release via munmap()
3878
      Note that if HAVE_MMAP is false but chunk_is_mmapped is
3879
      true, then user must have overwritten memory. There's nothing
3880
      we can do to catch this error unless DEBUG is set, in which case
3881
      check_inuse_chunk (above) will have triggered error.
3882
    */
10347 ripley 3883
 
26855 ripley 3884
    else {
3885
#if HAVE_MMAP
3886
      int ret;
3887
      INTERNAL_SIZE_T offset = p->prev_size;
3888
      av->n_mmaps--;
3889
      av->mmapped_mem -= (size + offset);
3890
      ret = munmap((char*)p - offset, size + offset);
3891
      /* munmap returns non-zero on failure */
3892
      assert(ret == 0);
3893
#endif
3894
    }
3895
  }
3896
}
10347 ripley 3897
 
26855 ripley 3898
/*
3899
  ------------------------- malloc_consolidate -------------------------
10347 ripley 3900
 
26855 ripley 3901
  malloc_consolidate is a specialized version of free() that tears
3902
  down chunks held in fastbins.  Free itself cannot be used for this
3903
  purpose since, among other things, it might place chunks back onto
3904
  fastbins.  So, instead, we need to use a minor variant of the same
3905
  code.
3906
 
3907
  Also, because this routine needs to be called the first time through
3908
  malloc anyway, it turns out to be the perfect place to trigger
3909
  initialization code.
10347 ripley 3910
*/
3911
 
3912
#if __STD_C
26855 ripley 3913
static void malloc_consolidate(mstate av)
10347 ripley 3914
#else
26855 ripley 3915
static void malloc_consolidate(av) mstate av;
10347 ripley 3916
#endif
3917
{
26855 ripley 3918
  mfastbinptr*    fb;                 /* current fastbin being consolidated */
3919
  mfastbinptr*    maxfb;              /* last fastbin (for loop control) */
3920
  mchunkptr       p;                  /* current chunk being consolidated */
3921
  mchunkptr       nextp;              /* next chunk to consolidate */
3922
  mchunkptr       unsorted_bin;       /* bin header */
3923
  mchunkptr       first_unsorted;     /* chunk to link to */
10347 ripley 3924
 
26855 ripley 3925
  /* These have same use as in free() */
3926
  mchunkptr       nextchunk;
3927
  INTERNAL_SIZE_T size;
3928
  INTERNAL_SIZE_T nextsize;
3929
  INTERNAL_SIZE_T prevsize;
3930
  int             nextinuse;
3931
  mchunkptr       bck;
3932
  mchunkptr       fwd;
10347 ripley 3933
 
26855 ripley 3934
  /*
3935
    If max_fast is 0, we know that av hasn't
3936
    yet been initialized, in which case do so below
3937
  */
10347 ripley 3938
 
26855 ripley 3939
  if (av->max_fast != 0) {
3940
    clear_fastchunks(av);
10347 ripley 3941
 
26855 ripley 3942
    unsorted_bin = unsorted_chunks(av);
10347 ripley 3943
 
26855 ripley 3944
    /*
3945
      Remove each chunk from fast bin and consolidate it, placing it
3946
      then in unsorted bin. Among other reasons for doing this,
3947
      placing in unsorted bin avoids needing to calculate actual bins
3948
      until malloc is sure that chunks aren't immediately going to be
3949
      reused anyway.
3950
    */
3951
 
3952
    maxfb = &(av->fastbins[fastbin_index(av->max_fast)]);
3953
    fb = &(av->fastbins[0]);
3954
    do {
3955
      if ( (p = *fb) != 0) {
3956
        *fb = 0;
3957
 
3958
        do {
3959
          check_inuse_chunk(p);
3960
          nextp = p->fd;
3961
 
3962
          /* Slightly streamlined version of consolidation code in free() */
3963
          size = p->size & ~PREV_INUSE;
3964
          nextchunk = chunk_at_offset(p, size);
3965
          nextsize = chunksize(nextchunk);
3966
 
3967
          if (!prev_inuse(p)) {
3968
            prevsize = p->prev_size;
3969
            size += prevsize;
3970
            p = chunk_at_offset(p, -((long) prevsize));
3971
            unlink(p, bck, fwd);
3972
          }
3973
 
3974
          if (nextchunk != av->top) {
3975
            nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
3976
            set_head(nextchunk, nextsize);
3977
 
3978
            if (!nextinuse) {
3979
              size += nextsize;
3980
              unlink(nextchunk, bck, fwd);
3981
            }
3982
 
3983
            first_unsorted = unsorted_bin->fd;
3984
            unsorted_bin->fd = p;
3985
            first_unsorted->bk = p;
3986
 
3987
            set_head(p, size | PREV_INUSE);
3988
            p->bk = unsorted_bin;
3989
            p->fd = first_unsorted;
3990
            set_foot(p, size);
3991
          }
3992
 
3993
          else {
3994
            size += nextsize;
3995
            set_head(p, size | PREV_INUSE);
3996
            av->top = p;
3997
          }
3998
 
3999
        } while ( (p = nextp) != 0);
4000
 
4001
      }
4002
    } while (fb++ != maxfb);
10347 ripley 4003
  }
26855 ripley 4004
  else {
4005
    malloc_init_state(av);
4006
    check_malloc_state();
10347 ripley 4007
  }
4008
}
4009
 
4010
/*
26855 ripley 4011
  ------------------------------ realloc ------------------------------
10347 ripley 4012
*/
4013
 
4014
 
4015
#if __STD_C
4016
Void_t* rEALLOc(Void_t* oldmem, size_t bytes)
4017
#else
4018
Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
4019
#endif
4020
{
26855 ripley 4021
  mstate av = get_malloc_state();
10347 ripley 4022
 
26855 ripley 4023
  INTERNAL_SIZE_T  nb;              /* padded request size */
10347 ripley 4024
 
26855 ripley 4025
  mchunkptr        oldp;            /* chunk corresponding to oldmem */
4026
  INTERNAL_SIZE_T  oldsize;         /* its size */
10347 ripley 4027
 
26855 ripley 4028
  mchunkptr        newp;            /* chunk to return */
4029
  INTERNAL_SIZE_T  newsize;         /* its size */
4030
  Void_t*          newmem;          /* corresponding user mem */
10347 ripley 4031
 
26855 ripley 4032
  mchunkptr        next;            /* next contiguous chunk after oldp */
10347 ripley 4033
 
26855 ripley 4034
  mchunkptr        remainder;       /* extra space at end of newp */
4035
  CHUNK_SIZE_T     remainder_size;  /* its size */
10347 ripley 4036
 
26855 ripley 4037
  mchunkptr        bck;             /* misc temp for linking */
4038
  mchunkptr        fwd;             /* misc temp for linking */
10347 ripley 4039
 
26855 ripley 4040
  CHUNK_SIZE_T     copysize;        /* bytes to copy */
4041
  unsigned int     ncopies;         /* INTERNAL_SIZE_T words to copy */
4042
  INTERNAL_SIZE_T* s;               /* copy source */ 
4043
  INTERNAL_SIZE_T* d;               /* copy destination */
4044
 
4045
 
10347 ripley 4046
#ifdef REALLOC_ZERO_BYTES_FREES
26855 ripley 4047
  if (bytes == 0) {
4048
    fREe(oldmem);
4049
    return 0;
4050
  }
10347 ripley 4051
#endif
4052
 
4053
  /* realloc of null is supposed to be same as malloc */
4054
  if (oldmem == 0) return mALLOc(bytes);
4055
 
26855 ripley 4056
  checked_request2size(bytes, nb);
10347 ripley 4057
 
26855 ripley 4058
  oldp    = mem2chunk(oldmem);
4059
  oldsize = chunksize(oldp);
10347 ripley 4060
 
4061
  check_inuse_chunk(oldp);
4062
 
26855 ripley 4063
  if (!chunk_is_mmapped(oldp)) {
10347 ripley 4064
 
26855 ripley 4065
    if ((CHUNK_SIZE_T)(oldsize) >= (CHUNK_SIZE_T)(nb)) {
4066
      /* already big enough; split below */
4067
      newp = oldp;
4068
      newsize = oldsize;
4069
    }
10347 ripley 4070
 
26855 ripley 4071
    else {
4072
      next = chunk_at_offset(oldp, oldsize);
10347 ripley 4073
 
26855 ripley 4074
      /* Try to expand forward into top */
4075
      if (next == av->top &&
4076
          (CHUNK_SIZE_T)(newsize = oldsize + chunksize(next)) >=
4077
          (CHUNK_SIZE_T)(nb + MINSIZE)) {
4078
        set_head_size(oldp, nb);
4079
        av->top = chunk_at_offset(oldp, nb);
4080
        set_head(av->top, (newsize - nb) | PREV_INUSE);
4081
        return chunk2mem(oldp);
10347 ripley 4082
      }
26855 ripley 4083
 
4084
      /* Try to expand forward into next chunk;  split off remainder below */
4085
      else if (next != av->top && 
4086
               !inuse(next) &&
4087
               (CHUNK_SIZE_T)(newsize = oldsize + chunksize(next)) >=
4088
               (CHUNK_SIZE_T)(nb)) {
4089
        newp = oldp;
10347 ripley 4090
        unlink(next, bck, fwd);
4091
      }
4092
 
26855 ripley 4093
      /* allocate, copy, free */
4094
      else {
4095
        newmem = mALLOc(nb - MALLOC_ALIGN_MASK);
4096
        if (newmem == 0)
4097
          return 0; /* propagate failure */
4098
 
4099
        newp = mem2chunk(newmem);
4100
        newsize = chunksize(newp);
4101
 
4102
        /*
4103
          Avoid copy if newp is next chunk after oldp.
4104
        */
4105
        if (newp == next) {
4106
          newsize += oldsize;
4107
          newp = oldp;
4108
        }
4109
        else {
4110
          /*
4111
            Unroll copy of <= 36 bytes (72 if 8byte sizes)
4112
            We know that contents have an odd number of
4113
            INTERNAL_SIZE_T-sized words; minimally 3.
4114
          */
4115
 
4116
          copysize = oldsize - SIZE_SZ;
4117
          s = (INTERNAL_SIZE_T*)(oldmem);
4118
          d = (INTERNAL_SIZE_T*)(newmem);
4119
          ncopies = copysize / sizeof(INTERNAL_SIZE_T);
4120
          assert(ncopies >= 3);
4121
 
4122
          if (ncopies > 9)
4123
            MALLOC_COPY(d, s, copysize);
4124
 
4125
          else {
4126
            *(d+0) = *(s+0);
4127
            *(d+1) = *(s+1);
4128
            *(d+2) = *(s+2);
4129
            if (ncopies > 4) {
4130
              *(d+3) = *(s+3);
4131
              *(d+4) = *(s+4);
4132
              if (ncopies > 6) {
4133
                *(d+5) = *(s+5);
4134
                *(d+6) = *(s+6);
4135
                if (ncopies > 8) {
4136
                  *(d+7) = *(s+7);
4137
                  *(d+8) = *(s+8);
4138
                }
4139
              }
4140
            }
10347 ripley 4141
          }
26855 ripley 4142
 
4143
          fREe(oldmem);
4144
          check_inuse_chunk(newp);
4145
          return chunk2mem(newp);
10347 ripley 4146
        }
4147
      }
4148
    }
4149
 
26855 ripley 4150
    /* If possible, free extra space in old or extended chunk */
10347 ripley 4151
 
26855 ripley 4152
    assert((CHUNK_SIZE_T)(newsize) >= (CHUNK_SIZE_T)(nb));
10347 ripley 4153
 
26855 ripley 4154
    remainder_size = newsize - nb;
10347 ripley 4155
 
26855 ripley 4156
    if (remainder_size < MINSIZE) { /* not enough extra to split off */
4157
      set_head_size(newp, newsize);
4158
      set_inuse_bit_at_offset(newp, newsize);
10347 ripley 4159
    }
26855 ripley 4160
    else { /* split remainder */
4161
      remainder = chunk_at_offset(newp, nb);
4162
      set_head_size(newp, nb);
4163
      set_head(remainder, remainder_size | PREV_INUSE);
4164
      /* Mark remainder as inuse so free() won't complain */
4165
      set_inuse_bit_at_offset(remainder, remainder_size);
4166
      fREe(chunk2mem(remainder)); 
4167
    }
10347 ripley 4168
 
26855 ripley 4169
    check_inuse_chunk(newp);
4170
    return chunk2mem(newp);
10347 ripley 4171
  }
4172
 
26855 ripley 4173
  /*
4174
    Handle mmap cases
4175
  */
10347 ripley 4176
 
26855 ripley 4177
  else {
4178
#if HAVE_MMAP
10347 ripley 4179
 
26855 ripley 4180
#if HAVE_MREMAP
4181
    INTERNAL_SIZE_T offset = oldp->prev_size;
4182
    size_t pagemask = av->pagesize - 1;
4183
    char *cp;
4184
    CHUNK_SIZE_T  sum;
4185
 
4186
    /* Note the extra SIZE_SZ overhead */
4187
    newsize = (nb + offset + SIZE_SZ + pagemask) & ~pagemask;
10347 ripley 4188
 
26855 ripley 4189
    /* don't need to remap if still within same page */
4190
    if (oldsize == newsize - offset) 
4191
      return oldmem;
10347 ripley 4192
 
26855 ripley 4193
    cp = (char*)mremap((char*)oldp - offset, oldsize + offset, newsize, 1);
4194
 
4195
    if (cp != (char*)MORECORE_FAILURE) {
10347 ripley 4196
 
26855 ripley 4197
      newp = (mchunkptr)(cp + offset);
4198
      set_head(newp, (newsize - offset)|IS_MMAPPED);
4199
 
4200
      assert(aligned_OK(chunk2mem(newp)));
4201
      assert((newp->prev_size == offset));
4202
 
4203
      /* update statistics */
4204
      sum = av->mmapped_mem += newsize - oldsize;
4205
      if (sum > (CHUNK_SIZE_T)(av->max_mmapped_mem)) 
4206
        av->max_mmapped_mem = sum;
4207
      sum += av->sbrked_mem;
4208
      if (sum > (CHUNK_SIZE_T)(av->max_total_mem)) 
4209
        av->max_total_mem = sum;
4210
 
4211
      return chunk2mem(newp);
4212
    }
4213
#endif
10347 ripley 4214
 
26855 ripley 4215
    /* Note the extra SIZE_SZ overhead. */
4216
    if ((CHUNK_SIZE_T)(oldsize) >= (CHUNK_SIZE_T)(nb + SIZE_SZ)) 
4217
      newmem = oldmem; /* do nothing */
4218
    else {
4219
      /* Must alloc, copy, free. */
4220
      newmem = mALLOc(nb - MALLOC_ALIGN_MASK);
4221
      if (newmem != 0) {
4222
        MALLOC_COPY(newmem, oldmem, oldsize - 2*SIZE_SZ);
4223
        fREe(oldmem);
4224
      }
4225
    }
4226
    return newmem;
10347 ripley 4227
 
26855 ripley 4228
#else 
4229
    /* If !HAVE_MMAP, but chunk_is_mmapped, user must have overwritten mem */
4230
    check_malloc_state();
4231
    MALLOC_FAILURE_ACTION;
4232
    return 0;
4233
#endif
4234
  }
4235
}
10347 ripley 4236
 
26855 ripley 4237
/*
4238
  ------------------------------ memalign ------------------------------
10347 ripley 4239
*/
4240
 
4241
#if __STD_C
4242
Void_t* mEMALIGn(size_t alignment, size_t bytes)
4243
#else
4244
Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
4245
#endif
4246
{
26855 ripley 4247
  INTERNAL_SIZE_T nb;             /* padded  request size */
4248
  char*           m;              /* memory returned by malloc call */
4249
  mchunkptr       p;              /* corresponding chunk */
4250
  char*           brk;            /* alignment point within p */
4251
  mchunkptr       newp;           /* chunk to return */
4252
  INTERNAL_SIZE_T newsize;        /* its size */
4253
  INTERNAL_SIZE_T leadsize;       /* leading space before alignment point */
4254
  mchunkptr       remainder;      /* spare room at end to split off */
4255
  CHUNK_SIZE_T    remainder_size; /* its size */
4256
  INTERNAL_SIZE_T size;
10347 ripley 4257
 
4258
  /* If need less alignment than we give anyway, just relay to malloc */
4259
 
4260
  if (alignment <= MALLOC_ALIGNMENT) return mALLOc(bytes);
4261
 
4262
  /* Otherwise, ensure that it is at least a minimum chunk size */
4263
 
4264
  if (alignment <  MINSIZE) alignment = MINSIZE;
4265
 
26855 ripley 4266
  /* Make sure alignment is power of 2 (in case MINSIZE is not).  */
4267
  if ((alignment & (alignment - 1)) != 0) {
4268
    size_t a = MALLOC_ALIGNMENT * 2;
4269
    while ((CHUNK_SIZE_T)a < (CHUNK_SIZE_T)alignment) a <<= 1;
4270
    alignment = a;
4271
  }
4272
 
4273
  checked_request2size(bytes, nb);
4274
 
4275
  /*
4276
    Strategy: find a spot within that chunk that meets the alignment
4277
    request, and then possibly free the leading and trailing space.
4278
  */
4279
 
4280
 
10347 ripley 4281
  /* Call malloc with worst case padding to hit alignment. */
4282
 
4283
  m  = (char*)(mALLOc(nb + alignment + MINSIZE));
4284
 
4285
  if (m == 0) return 0; /* propagate failure */
4286
 
4287
  p = mem2chunk(m);
4288
 
26855 ripley 4289
  if ((((PTR_UINT)(m)) % alignment) != 0) { /* misaligned */
4290
 
10347 ripley 4291
    /*
26855 ripley 4292
      Find an aligned spot inside chunk.  Since we need to give back
4293
      leading space in a chunk of at least MINSIZE, if the first
4294
      calculation places us at a spot with less than MINSIZE leader,
4295
      we can move to the next aligned spot -- we've allocated enough
4296
      total room so that this is always possible.
10347 ripley 4297
    */
4298
 
26855 ripley 4299
    brk = (char*)mem2chunk((PTR_UINT)(((PTR_UINT)(m + alignment - 1)) &
4300
                           -((signed long) alignment)));
4301
    if ((CHUNK_SIZE_T)(brk - (char*)(p)) < MINSIZE)
4302
      brk += alignment;
10347 ripley 4303
 
4304
    newp = (mchunkptr)brk;
4305
    leadsize = brk - (char*)(p);
4306
    newsize = chunksize(p) - leadsize;
4307
 
26855 ripley 4308
    /* For mmapped chunks, just adjust offset */
4309
    if (chunk_is_mmapped(p)) {
10347 ripley 4310
      newp->prev_size = p->prev_size + leadsize;
4311
      set_head(newp, newsize|IS_MMAPPED);
4312
      return chunk2mem(newp);
4313
    }
4314
 
26855 ripley 4315
    /* Otherwise, give back leader, use the rest */
10347 ripley 4316
    set_head(newp, newsize | PREV_INUSE);
4317
    set_inuse_bit_at_offset(newp, newsize);
4318
    set_head_size(p, leadsize);
4319
    fREe(chunk2mem(p));
4320
    p = newp;
4321
 
26855 ripley 4322
    assert (newsize >= nb &&
4323
            (((PTR_UINT)(chunk2mem(p))) % alignment) == 0);
10347 ripley 4324
  }
4325
 
4326
  /* Also give back spare room at the end */
26855 ripley 4327
  if (!chunk_is_mmapped(p)) {
4328
    size = chunksize(p);
4329
    if ((CHUNK_SIZE_T)(size) > (CHUNK_SIZE_T)(nb + MINSIZE)) {
4330
      remainder_size = size - nb;
4331
      remainder = chunk_at_offset(p, nb);
4332
      set_head(remainder, remainder_size | PREV_INUSE);
4333
      set_head_size(p, nb);
4334
      fREe(chunk2mem(remainder));
4335
    }
10347 ripley 4336
  }
4337
 
4338
  check_inuse_chunk(p);
4339
  return chunk2mem(p);
26855 ripley 4340
}
10347 ripley 4341
 
26855 ripley 4342
/*
4343
  ------------------------------ calloc ------------------------------
4344
*/
4345
 
4346
#if __STD_C
4347
Void_t* cALLOc(size_t n_elements, size_t elem_size)
4348
#else
4349
Void_t* cALLOc(n_elements, elem_size) size_t n_elements; size_t elem_size;
4350
#endif
4351
{
4352
  mchunkptr p;
4353
  CHUNK_SIZE_T  clearsize;
4354
  CHUNK_SIZE_T  nclears;
4355
  INTERNAL_SIZE_T* d;
4356
 
4357
  Void_t* mem = mALLOc(n_elements * elem_size);
4358
 
4359
  if (mem != 0) {
4360
    p = mem2chunk(mem);
4361
 
4362
    if (!chunk_is_mmapped(p))
4363
    {  
4364
      /*
4365
        Unroll clear of <= 36 bytes (72 if 8byte sizes)
4366
        We know that contents have an odd number of
4367
        INTERNAL_SIZE_T-sized words; minimally 3.
4368
      */
4369
 
4370
      d = (INTERNAL_SIZE_T*)mem;
4371
      clearsize = chunksize(p) - SIZE_SZ;
4372
      nclears = clearsize / sizeof(INTERNAL_SIZE_T);
4373
      assert(nclears >= 3);
4374
 
4375
      if (nclears > 9)
4376
        MALLOC_ZERO(d, clearsize);
4377
 
4378
      else {
4379
        *(d+0) = 0;
4380
        *(d+1) = 0;
4381
        *(d+2) = 0;
4382
        if (nclears > 4) {
4383
          *(d+3) = 0;
4384
          *(d+4) = 0;
4385
          if (nclears > 6) {
4386
            *(d+5) = 0;
4387
            *(d+6) = 0;
4388
            if (nclears > 8) {
4389
              *(d+7) = 0;
4390
              *(d+8) = 0;
4391
            }
4392
          }
4393
        }
4394
      }
4395
    }
4396
#if ! MMAP_CLEARS
4397
    else
4398
    {
4399
      d = (INTERNAL_SIZE_T*)mem;
4400
      /*
4401
        Note the additional SIZE_SZ
4402
      */
4403
      clearsize = chunksize(p) - 2*SIZE_SZ;
4404
      MALLOC_ZERO(d, clearsize);
4405
    }
4406
#endif
4407
  }
4408
  return mem;
10347 ripley 4409
}
4410
 
26855 ripley 4411
/*
4412
  ------------------------------ cfree ------------------------------
4413
*/
10347 ripley 4414
 
26855 ripley 4415
#if __STD_C
4416
void cFREe(Void_t *mem)
4417
#else
4418
void cFREe(mem) Void_t *mem;
4419
#endif
4420
{
4421
  fREe(mem);
4422
}
10347 ripley 4423
 
4424
/*
26855 ripley 4425
  ------------------------- independent_calloc -------------------------
10347 ripley 4426
*/
4427
 
4428
#if __STD_C
26855 ripley 4429
Void_t** iCALLOc(size_t n_elements, size_t elem_size, Void_t* chunks[])
10347 ripley 4430
#else
26855 ripley 4431
Void_t** iCALLOc(n_elements, elem_size, chunks) size_t n_elements; size_t elem_size; Void_t* chunks[];
10347 ripley 4432
#endif
4433
{
26855 ripley 4434
  size_t sz = elem_size; /* serves as 1-element array */
4435
  /* opts arg of 3 means all elements are same size, and should be cleared */
4436
  return iALLOc(n_elements, &sz, 3, chunks);
10347 ripley 4437
}
4438
 
4439
/*
26855 ripley 4440
  ------------------------- independent_comalloc -------------------------
10347 ripley 4441
*/
4442
 
4443
#if __STD_C
26855 ripley 4444
Void_t** iCOMALLOc(size_t n_elements, size_t sizes[], Void_t* chunks[])
10347 ripley 4445
#else
26855 ripley 4446
Void_t** iCOMALLOc(n_elements, sizes, chunks) size_t n_elements; size_t sizes[]; Void_t* chunks[];
10347 ripley 4447
#endif
4448
{
26855 ripley 4449
  return iALLOc(n_elements, sizes, 0, chunks);
10347 ripley 4450
}
4451
 
26855 ripley 4452
 
10347 ripley 4453
/*
26855 ripley 4454
  ------------------------------ ialloc ------------------------------
4455
  ialloc provides common support for independent_X routines, handling all of
4456
  the combinations that can result.
10347 ripley 4457
 
26855 ripley 4458
  The opts arg has:
4459
    bit 0 set if all elements are same size (using sizes[0])
4460
    bit 1 set if elements should be zeroed
10347 ripley 4461
*/
4462
 
26855 ripley 4463
 
10347 ripley 4464
#if __STD_C
26855 ripley 4465
static Void_t** iALLOc(size_t n_elements, 
4466
                       size_t* sizes,  
4467
                       int opts,
4468
                       Void_t* chunks[])
10347 ripley 4469
#else
26855 ripley 4470
static Void_t** iALLOc(n_elements, sizes, opts, chunks) size_t n_elements; size_t* sizes; int opts; Void_t* chunks[];
10347 ripley 4471
#endif
4472
{
26855 ripley 4473
  mstate av = get_malloc_state();
4474
  INTERNAL_SIZE_T element_size;   /* chunksize of each element, if all same */
4475
  INTERNAL_SIZE_T contents_size;  /* total size of elements */
4476
  INTERNAL_SIZE_T array_size;     /* request size of pointer array */
4477
  Void_t*         mem;            /* malloced aggregate space */
4478
  mchunkptr       p;              /* corresponding chunk */
4479
  INTERNAL_SIZE_T remainder_size; /* remaining bytes while splitting */
4480
  Void_t**        marray;         /* either "chunks" or malloced ptr array */
4481
  mchunkptr       array_chunk;    /* chunk for malloced ptr array */
4482
  int             mmx;            /* to disable mmap */
4483
  INTERNAL_SIZE_T size;           
4484
  size_t          i;
10347 ripley 4485
 
26855 ripley 4486
  /* Ensure initialization */
4487
  if (av->max_fast == 0) malloc_consolidate(av);
10347 ripley 4488
 
26855 ripley 4489
  /* compute array length, if needed */
4490
  if (chunks != 0) {
4491
    if (n_elements == 0)
4492
      return chunks; /* nothing to do */
4493
    marray = chunks;
4494
    array_size = 0;
4495
  }
4496
  else {
4497
    /* if empty req, must still return chunk representing empty array */
4498
    if (n_elements == 0) 
4499
      return (Void_t**) mALLOc(0);
4500
    marray = 0;
4501
    array_size = request2size(n_elements * (sizeof(Void_t*)));
4502
  }
10425 ripley 4503
 
26855 ripley 4504
  /* compute total element size */
4505
  if (opts & 0x1) { /* all-same-size */
4506
    element_size = request2size(*sizes);
4507
    contents_size = n_elements * element_size;
4508
  }
4509
  else { /* add up all the sizes */
4510
    element_size = 0;
4511
    contents_size = 0;
4512
    for (i = 0; i != n_elements; ++i) 
4513
      contents_size += request2size(sizes[i]);     
4514
  }
10347 ripley 4515
 
26855 ripley 4516
  /* subtract out alignment bytes from total to minimize overallocation */
4517
  size = contents_size + array_size - MALLOC_ALIGN_MASK;
4518
 
4519
  /* 
4520
     Allocate the aggregate chunk.
4521
     But first disable mmap so malloc won't use it, since
4522
     we would not be able to later free/realloc space internal
4523
     to a segregated mmap region.
4524
 */
4525
  mmx = av->n_mmaps_max;   /* disable mmap */
4526
  av->n_mmaps_max = 0;
4527
  mem = mALLOc(size);
4528
  av->n_mmaps_max = mmx;   /* reset mmap */
4529
  if (mem == 0) 
10347 ripley 4530
    return 0;
4531
 
26855 ripley 4532
  p = mem2chunk(mem);
4533
  assert(!chunk_is_mmapped(p)); 
4534
  remainder_size = chunksize(p);
10347 ripley 4535
 
26855 ripley 4536
  if (opts & 0x2) {       /* optionally clear the elements */
4537
    MALLOC_ZERO(mem, remainder_size - SIZE_SZ - array_size);
4538
  }
10347 ripley 4539
 
26855 ripley 4540
  /* If not provided, allocate the pointer array as final part of chunk */
4541
  if (marray == 0) {
4542
    array_chunk = chunk_at_offset(p, contents_size);
4543
    marray = (Void_t**) (chunk2mem(array_chunk));
4544
    set_head(array_chunk, (remainder_size - contents_size) | PREV_INUSE);
4545
    remainder_size = contents_size;
4546
  }
10347 ripley 4547
 
26855 ripley 4548
  /* split out elements */
4549
  for (i = 0; ; ++i) {
4550
    marray[i] = chunk2mem(p);
4551
    if (i != n_elements-1) {
4552
      if (element_size != 0) 
4553
        size = element_size;
4554
      else
4555
        size = request2size(sizes[i]);          
4556
      remainder_size -= size;
4557
      set_head(p, size | PREV_INUSE);
4558
      p = chunk_at_offset(p, size);
4559
    }
4560
    else { /* the final element absorbs any overallocation slop */
4561
      set_head(p, remainder_size | PREV_INUSE);
4562
      break;
4563
    }
4564
  }
10347 ripley 4565
 
26855 ripley 4566
#if DEBUG
4567
  if (marray != chunks) {
4568
    /* final element must have exactly exhausted chunk */
4569
    if (element_size != 0) 
4570
      assert(remainder_size == element_size);
4571
    else
4572
      assert(remainder_size == request2size(sizes[i]));
4573
    check_inuse_chunk(mem2chunk(marray));
4574
  }
4575
 
4576
  for (i = 0; i != n_elements; ++i)
4577
    check_inuse_chunk(mem2chunk(marray[i]));
10347 ripley 4578
#endif
4579
 
26855 ripley 4580
  return marray;
10347 ripley 4581
}
4582
 
26855 ripley 4583
 
10347 ripley 4584
/*
26855 ripley 4585
  ------------------------------ valloc ------------------------------
4586
*/
10347 ripley 4587
 
26855 ripley 4588
#if __STD_C
4589
Void_t* vALLOc(size_t bytes)
4590
#else
4591
Void_t* vALLOc(bytes) size_t bytes;
4592
#endif
4593
{
4594
  /* Ensure initialization */
4595
  mstate av = get_malloc_state();
4596
  if (av->max_fast == 0) malloc_consolidate(av);
4597
  return mEMALIGn(av->pagesize, bytes);
4598
}
10347 ripley 4599
 
26855 ripley 4600
/*
4601
  ------------------------------ pvalloc ------------------------------
10347 ripley 4602
*/
4603
 
26855 ripley 4604
 
10347 ripley 4605
#if __STD_C
26855 ripley 4606
Void_t* pVALLOc(size_t bytes)
10347 ripley 4607
#else
26855 ripley 4608
Void_t* pVALLOc(bytes) size_t bytes;
10347 ripley 4609
#endif
4610
{
26855 ripley 4611
  mstate av = get_malloc_state();
4612
  size_t pagesz;
4613
 
4614
  /* Ensure initialization */
4615
  if (av->max_fast == 0) malloc_consolidate(av);
4616
  pagesz = av->pagesize;
4617
  return mEMALIGn(pagesz, (bytes + pagesz - 1) & ~(pagesz - 1));
10347 ripley 4618
}
26855 ripley 4619
 
10347 ripley 4620
 
4621
/*
26855 ripley 4622
  ------------------------------ malloc_trim ------------------------------
4623
*/
10347 ripley 4624
 
26855 ripley 4625
#if __STD_C
4626
int mTRIm(size_t pad)
4627
#else
4628
int mTRIm(pad) size_t pad;
4629
#endif
4630
{
4631
  mstate av = get_malloc_state();
4632
  /* Ensure initialization/consolidation */
4633
  malloc_consolidate(av);
10347 ripley 4634
 
26855 ripley 4635
#ifndef MORECORE_CANNOT_TRIM        
4636
  return sYSTRIm(pad, av);
4637
#else
4638
  return 0;
4639
#endif
4640
}
10347 ripley 4641
 
4642
 
26855 ripley 4643
/*
4644
  ------------------------- malloc_usable_size -------------------------
10347 ripley 4645
*/
4646
 
4647
#if __STD_C
26855 ripley 4648
size_t mUSABLe(Void_t* mem)
10347 ripley 4649
#else
26855 ripley 4650
size_t mUSABLe(mem) Void_t* mem;
10347 ripley 4651
#endif
4652
{
26855 ripley 4653
  mchunkptr p;
4654
  if (mem != 0) {
4655
    p = mem2chunk(mem);
4656
    if (chunk_is_mmapped(p))
4657
      return chunksize(p) - 2*SIZE_SZ;
4658
    else if (inuse(p))
4659
      return chunksize(p) - SIZE_SZ;
4660
  }
4661
  return 0;
4662
}
10347 ripley 4663
 
26855 ripley 4664
/*
4665
  ------------------------------ mallinfo ------------------------------
4666
*/
10347 ripley 4667
 
26855 ripley 4668
struct mallinfo mALLINFo()
4669
{
4670
  mstate av = get_malloc_state();
4671
  struct mallinfo mi;
4672
  int i;
4673
  mbinptr b;
4674
  mchunkptr p;
4675
  INTERNAL_SIZE_T avail;
4676
  INTERNAL_SIZE_T fastavail;
4677
  int nblocks;
4678
  int nfastblocks;
10347 ripley 4679
 
26855 ripley 4680
  /* Ensure initialization */
4681
  if (av->top == 0)  malloc_consolidate(av);
10347 ripley 4682
 
26855 ripley 4683
  check_malloc_state();
4684
 
4685
  /* Account for top */
4686
  avail = chunksize(av->top);
4687
  nblocks = 1;  /* top always exists */
4688
 
4689
  /* traverse fastbins */
4690
  nfastblocks = 0;
4691
  fastavail = 0;
4692
 
4693
  for (i = 0; i < NFASTBINS; ++i) {
4694
    for (p = av->fastbins[i]; p != 0; p = p->fd) {
4695
      ++nfastblocks;
4696
      fastavail += chunksize(p);
4697
    }
4698
  }
4699
 
4700
  avail += fastavail;
4701
 
4702
  /* traverse regular bins */
4703
  for (i = 1; i < NBINS; ++i) {
4704
    b = bin_at(av, i);
4705
    for (p = last(b); p != b; p = p->bk) {
4706
      ++nblocks;
4707
      avail += chunksize(p);
4708
    }
4709
  }
4710
 
4711
  mi.smblks = nfastblocks;
4712
  mi.ordblks = nblocks;
4713
  mi.fordblks = avail;
26945 ripley 4714
  mi.uordblks = (av->sbrked_mem - avail) + av->mmapped_mem;
26855 ripley 4715
  mi.arena = av->sbrked_mem;
4716
  mi.hblks = av->n_mmaps;
4717
  mi.hblkhd = av->mmapped_mem;
4718
  mi.fsmblks = fastavail;
4719
  mi.keepcost = chunksize(av->top);
4720
  mi.usmblks = av->max_total_mem;
4721
 
4722
#ifdef TRACE
4723
 
4724
#ifdef WIN32
10347 ripley 4725
  {
26855 ripley 4726
    CHUNK_SIZE_T  free, reserved, committed;
4727
    vminfo (&free, &reserved, &committed);
4728
    printf("free bytes       = %10lu\n", 
4729
            free);
4730
    printf("reserved bytes   = %10lu\n", 
4731
            reserved);
4732
    printf("committed bytes  = %10lu\n", 
4733
            committed);
4734
  }
4735
#endif
10347 ripley 4736
 
4737
 
26855 ripley 4738
  printf("max system bytes = %10lu\n",
4739
          (CHUNK_SIZE_T)(mi.usmblks));
4740
  printf("system bytes     = %10lu\n",
4741
          (CHUNK_SIZE_T)(mi.arena + mi.hblkhd));
4742
  printf("in use bytes     = %10lu\n",
4743
          (CHUNK_SIZE_T)(mi.uordblks + mi.hblkhd));
4744
#endif
10347 ripley 4745
 
26855 ripley 4746
  return mi;
4747
}
4748
 
4749
/*
4750
  ------------------------------ malloc_stats ------------------------------
4751
*/
4752
 
4753
void mSTATs()
4754
{
4755
  struct mallinfo mi = mALLINFo();
4756
 
4757
#ifdef WIN32
4758
  {
4759
    CHUNK_SIZE_T  free, reserved, committed;
4760
    vminfo (&free, &reserved, &committed);
4761
    fprintf(stderr, "free bytes       = %10lu\n", 
4762
            free);
4763
    fprintf(stderr, "reserved bytes   = %10lu\n", 
4764
            reserved);
4765
    fprintf(stderr, "committed bytes  = %10lu\n", 
4766
            committed);
4767
  }
4768
#endif
4769
 
4770
 
4771
  fprintf(stderr, "max system bytes = %10lu\n",
4772
          (CHUNK_SIZE_T)(mi.usmblks));
4773
  fprintf(stderr, "system bytes     = %10lu\n",
4774
          (CHUNK_SIZE_T)(mi.arena + mi.hblkhd));
4775
  fprintf(stderr, "in use bytes     = %10lu\n",
4776
          (CHUNK_SIZE_T)(mi.uordblks + mi.hblkhd));
4777
 
4778
#ifdef WIN32 
4779
/* Commented out for R Build Compatibility
4780
  {
4781
    CHUNK_SIZE_T  kernel, user;
4782
    if (cpuinfo (TRUE, &kernel, &user)) {
4783
      fprintf(stderr, "kernel ms        = %10lu\n", 
4784
              kernel);
4785
      fprintf(stderr, "user ms          = %10lu\n", 
4786
              user);
10347 ripley 4787
    }
4788
  }
26855 ripley 4789
 */
4790
#endif
10347 ripley 4791
}
4792
 
4793
 
4794
/*
26855 ripley 4795
  ------------------------------ mallopt ------------------------------
10347 ripley 4796
*/
4797
 
4798
#if __STD_C
26855 ripley 4799
int mALLOPt(int param_number, int value)
10347 ripley 4800
#else
26855 ripley 4801
int mALLOPt(param_number, value) int param_number; int value;
10347 ripley 4802
#endif
4803
{
26855 ripley 4804
  mstate av = get_malloc_state();
4805
  /* Ensure initialization/consolidation */
4806
  malloc_consolidate(av);
4807
 
4808
  switch(param_number) {
4809
  case M_MXFAST:
4810
    if (value >= 0 && value <= MAX_FAST_SIZE) {
4811
      set_max_fast(av, value);
4812
      return 1;
4813
    }
4814
    else
4815
      return 0;
4816
 
4817
  case M_TRIM_THRESHOLD:
4818
    av->trim_threshold = value;
4819
    return 1;
4820
 
4821
  case M_TOP_PAD:
4822
    av->top_pad = value;
4823
    return 1;
4824
 
4825
  case M_MMAP_THRESHOLD:
4826
    av->mmap_threshold = value;
4827
    return 1;
4828
 
4829
  case M_MMAP_MAX:
4830
#if !HAVE_MMAP
4831
    if (value != 0)
4832
      return 0;
4833
#endif
4834
    av->n_mmaps_max = value;
4835
    return 1;
4836
 
4837
  default:
10347 ripley 4838
    return 0;
4839
  }
4840
}
4841
 
4842
 
26855 ripley 4843
/* 
4844
  -------------------- Alternative MORECORE functions --------------------
4845
*/
10347 ripley 4846
 
4847
 
26855 ripley 4848
/*
4849
  General Requirements for MORECORE.
10347 ripley 4850
 
26855 ripley 4851
  The MORECORE function must have the following properties:
10347 ripley 4852
 
26855 ripley 4853
  If MORECORE_CONTIGUOUS is false:
4854
 
4855
    * MORECORE must allocate in multiples of pagesize. It will
4856
      only be called with arguments that are multiples of pagesize.
4857
 
4858
    * MORECORE(0) must return an address that is at least 
4859
      MALLOC_ALIGNMENT aligned. (Page-aligning always suffices.)
4860
 
4861
  else (i.e. If MORECORE_CONTIGUOUS is true):
4862
 
4863
    * Consecutive calls to MORECORE with positive arguments
4864
      return increasing addresses, indicating that space has been
4865
      contiguously extended. 
4866
 
4867
    * MORECORE need not allocate in multiples of pagesize.
4868
      Calls to MORECORE need not have args of multiples of pagesize.
4869
 
4870
    * MORECORE need not page-align.
4871
 
4872
  In either case:
4873
 
4874
    * MORECORE may allocate more memory than requested. (Or even less,
4875
      but this will generally result in a malloc failure.)
4876
 
4877
    * MORECORE must not allocate memory when given argument zero, but
4878
      instead return one past the end address of memory from previous
4879
      nonzero call. This malloc does NOT call MORECORE(0)
4880
      until at least one call with positive arguments is made, so
4881
      the initial value returned is not important.
4882
 
4883
    * Even though consecutive calls to MORECORE need not return contiguous
4884
      addresses, it must be OK for malloc'ed chunks to span multiple
4885
      regions in those cases where they do happen to be contiguous.
4886
 
4887
    * MORECORE need not handle negative arguments -- it may instead
4888
      just return MORECORE_FAILURE when given negative arguments.
4889
      Negative arguments are always multiples of pagesize. MORECORE
4890
      must not misinterpret negative args as large positive unsigned
4891
      args. You can suppress all such calls from even occurring by defining
4892
      MORECORE_CANNOT_TRIM,
4893
 
4894
  There is some variation across systems about the type of the
4895
  argument to sbrk/MORECORE. If size_t is unsigned, then it cannot
4896
  actually be size_t, because sbrk supports negative args, so it is
4897
  normally the signed type of the same width as size_t (sometimes
4898
  declared as "intptr_t", and sometimes "ptrdiff_t").  It doesn't much
4899
  matter though. Internally, we use "long" as arguments, which should
4900
  work across all reasonable possibilities.
4901
 
4902
  Additionally, if MORECORE ever returns failure for a positive
4903
  request, and HAVE_MMAP is true, then mmap is used as a noncontiguous
4904
  system allocator. This is a useful backup strategy for systems with
4905
  holes in address spaces -- in this case sbrk cannot contiguously
4906
  expand the heap, but mmap may be able to map noncontiguous space.
4907
 
4908
  If you'd like mmap to ALWAYS be used, you can define MORECORE to be
4909
  a function that always returns MORECORE_FAILURE.
4910
 
4911
  Malloc only has limited ability to detect failures of MORECORE
4912
  to supply contiguous space when it says it can. In particular,
4913
  multithreaded programs that do not use locks may result in
4914
  rece conditions across calls to MORECORE that result in gaps
4915
  that cannot be detected as such, and subsequent corruption.
4916
 
4917
  If you are using this malloc with something other than sbrk (or its
4918
  emulation) to supply memory regions, you probably want to set
4919
  MORECORE_CONTIGUOUS as false.  As an example, here is a custom
4920
  allocator kindly contributed for pre-OSX macOS.  It uses virtually
4921
  but not necessarily physically contiguous non-paged memory (locked
4922
  in, present and won't get swapped out).  You can use it by
4923
  uncommenting this section, adding some #includes, and setting up the
4924
  appropriate defines above:
4925
 
4926
      #define MORECORE osMoreCore
4927
      #define MORECORE_CONTIGUOUS 0
4928
 
4929
  There is also a shutdown routine that should somehow be called for
4930
  cleanup upon program exit.
4931
 
4932
  #define MAX_POOL_ENTRIES 100
4933
  #define MINIMUM_MORECORE_SIZE  (64 * 1024)
4934
  static int next_os_pool;
4935
  void *our_os_pools[MAX_POOL_ENTRIES];
4936
 
4937
  void *osMoreCore(int size)
10347 ripley 4938
  {
26855 ripley 4939
    void *ptr = 0;
4940
    static void *sbrk_top = 0;
4941
 
4942
    if (size > 0)
10347 ripley 4943
    {
26855 ripley 4944
      if (size < MINIMUM_MORECORE_SIZE)
4945
         size = MINIMUM_MORECORE_SIZE;
4946
      if (CurrentExecutionLevel() == kTaskLevel)
4947
         ptr = PoolAllocateResident(size + RM_PAGE_SIZE, 0);
4948
      if (ptr == 0)
4949
      {
4950
        return (void *) MORECORE_FAILURE;
4951
      }
4952
      // save ptrs so they can be freed during cleanup
4953
      our_os_pools[next_os_pool] = ptr;
4954
      next_os_pool++;
4955
      ptr = (void *) ((((CHUNK_SIZE_T) ptr) + RM_PAGE_MASK) & ~RM_PAGE_MASK);
4956
      sbrk_top = (char *) ptr + size;
4957
      return ptr;
10347 ripley 4958
    }
26855 ripley 4959
    else if (size < 0)
4960
    {
4961
      // we don't currently support shrink behavior
4962
      return (void *) MORECORE_FAILURE;
4963
    }
4964
    else
4965
    {
4966
      return sbrk_top;
4967
    }
10347 ripley 4968
  }
4969
 
26855 ripley 4970
  // cleanup any allocated memory pools
4971
  // called as last thing before shutting down driver
10347 ripley 4972
 
26855 ripley 4973
  void osCleanupMem(void)
4974
  {
4975
    void **ptr;
10347 ripley 4976
 
26855 ripley 4977
    for (ptr = our_os_pools; ptr < &our_os_pools[MAX_POOL_ENTRIES]; ptr++)
4978
      if (*ptr)
4979
      {
4980
         PoolDeallocate(*ptr);
4981
         *ptr = 0;
4982
      }
4983
  }
10347 ripley 4984
 
26855 ripley 4985
*/
10347 ripley 4986
 
4987
 
26855 ripley 4988
/* 
4989
  -------------------------------------------------------------- 
10347 ripley 4990
 
26855 ripley 4991
  Emulation of sbrk for win32. 
4992
  Donated by J. Walter <Walter@GeNeSys-e.de>.
4993
  For additional information about this code, and malloc on Win32, see 
4994
     http://www.genesys-e.de/jwalter/
10347 ripley 4995
*/
4996
 
26855 ripley 4997
 
4998
#ifdef WIN32
4999
 
5000
#ifdef _DEBUG
5001
/* #define TRACE */
5002
/* #define TRACESB */
10347 ripley 5003
#endif
26855 ripley 5004
 
5005
/* #define TRACE */
5006
/* #define TRACESB */
5007
 
5008
/* Support for USE_MALLOC_LOCK */
5009
#ifdef USE_MALLOC_LOCK
5010
 
5011
/* Wait for spin lock */
5012
static int slwait (int *sl) {
5013
    while (InterlockedCompareExchange ((void **) sl, (void *) 1, (void *) 0) != 0) 
5014
	    Sleep (0);
5015
    return 0;
10347 ripley 5016
}
5017
 
26855 ripley 5018
/* Release spin lock */
5019
static int slrelease (int *sl) {
5020
    InterlockedExchange (sl, 0);
5021
    return 0;
5022
}
10347 ripley 5023
 
26855 ripley 5024
#ifdef NEEDED
5025
/* Spin lock for emulation code */
5026
static int g_sl;
5027
#endif
5028
 
5029
#endif /* USE_MALLOC_LOCK */
5030
 
5031
/* getpagesize for windows */
5032
static long getpagesize (void) {
5033
    static long g_pagesize = 0;
5034
    if (! g_pagesize) {
5035
        SYSTEM_INFO system_info;
5036
        GetSystemInfo (&system_info);
5037
        g_pagesize = system_info.dwPageSize;
5038
    }
5039
    return g_pagesize;
10347 ripley 5040
}
26855 ripley 5041
static long getregionsize (void) {
5042
    static long g_regionsize = 0;
5043
    if (! g_regionsize) {
5044
        SYSTEM_INFO system_info;
5045
        GetSystemInfo (&system_info);
5046
        g_regionsize = system_info.dwAllocationGranularity;
5047
    }
5048
    return g_regionsize;
5049
}
10347 ripley 5050
 
26855 ripley 5051
/* A region list entry */
5052
typedef struct _region_list_entry {
5053
    void *top_allocated;
5054
    void *top_committed;
5055
    void *top_reserved;
5056
    long reserve_size;
5057
    struct _region_list_entry *previous;
5058
} region_list_entry;
10347 ripley 5059
 
26855 ripley 5060
/* Allocate and link a region entry in the region list */
5061
static int region_list_append (region_list_entry **last, void *base_reserved, long reserve_size) {
5062
#ifdef TRACESB
5063
	printf ("region_list_append %p %p %lu\n", last, base_reserved, reserve_size);
5064
#endif
5065
#ifdef DEBUG
5066
	assert(HeapValidate(GetProcessHeap(), 0, NULL));
5067
#endif
5068
    region_list_entry *next = HeapAlloc (GetProcessHeap (), 0, sizeof (region_list_entry));
5069
    if (! next)
5070
	{
5071
#ifdef TRACESB
5072
	    printf ("region_list_append HeapAlloc failed\n");
5073
#endif
5074
        return FALSE;
5075
	}
5076
    next->top_allocated = (char *) base_reserved;
5077
    next->top_committed = (char *) base_reserved;
5078
    next->top_reserved = (char *) base_reserved + reserve_size;
5079
    next->reserve_size = reserve_size;
5080
    next->previous = *last;
5081
    *last = next;
5082
    return TRUE;
5083
}
5084
/* Free and unlink the last region entry from the region list */
5085
static int region_list_remove (region_list_entry **last) {
5086
    region_list_entry *previous = (*last)->previous;
5087
#ifdef TRACESB
5088
	printf ("region_list_remove %p %p\n", last, previous);
5089
#endif
5090
#ifdef DEBUG
5091
	assert(HeapValidate(GetProcessHeap(), 0, NULL));
5092
#endif
5093
    if (! HeapFree (GetProcessHeap (), sizeof (region_list_entry), *last)) {
5094
#ifdef TRACESB
5095
	    printf ("region_list_remove HeapFree failed\n");
5096
#endif
5097
        return FALSE;
5098
	}
5099
    *last = previous;
5100
    return TRUE;
5101
}
10347 ripley 5102
 
26855 ripley 5103
#define CEIL(size,to)	(((size)+(to)-1)&~((to)-1))
5104
#define FLOOR(size,to)	((size)&~((to)-1))
10347 ripley 5105
 
26855 ripley 5106
#define SBRK_SCALE  0
5107
/* #define SBRK_SCALE  1 */
5108
/* #define SBRK_SCALE  2 */
5109
/* #define SBRK_SCALE  4  */
10347 ripley 5110
 
26855 ripley 5111
/* sbrk for windows */
5112
static void *sbrk (long size) {
5113
    static long g_pagesize, g_my_pagesize;
5114
    static long g_regionsize, g_my_regionsize;
5115
    static region_list_entry *g_last;
5116
    void *result = (void *) MORECORE_FAILURE;
10347 ripley 5117
 
26855 ripley 5118
#ifdef TRACESB
5119
    printf ("sbrk %ld\n", size);
5120
#endif
5121
#if defined (USE_MALLOC_LOCK) && defined (NEEDED)
5122
    /* Wait for spin lock */
5123
    slwait (&g_sl);
5124
#endif
5125
    /* First time initialization */
5126
    if (! g_pagesize) {
5127
        g_pagesize = getpagesize ();
5128
        g_my_pagesize = g_pagesize << SBRK_SCALE;
5129
    }
5130
    if (! g_regionsize) {
5131
        g_regionsize = getregionsize ();
5132
        g_my_regionsize = g_regionsize << SBRK_SCALE;
5133
    }
5134
    if (! g_last) {
5135
        if (! region_list_append (&g_last, 0, 0)) 
5136
           goto sbrk_exit;
5137
    }
5138
    /* Assert invariants */
5139
    assert (g_last);
5140
    assert ((char *) g_last->top_reserved - g_last->reserve_size <= (char *) g_last->top_allocated &&
5141
            g_last->top_allocated <= g_last->top_committed);
5142
    assert ((char *) g_last->top_reserved - g_last->reserve_size <= (char *) g_last->top_committed &&
5143
            g_last->top_committed <= g_last->top_reserved &&
5144
            (unsigned) g_last->top_committed % g_pagesize == 0);
5145
    assert ((unsigned) g_last->top_reserved % g_regionsize == 0);
5146
    assert ((unsigned) g_last->reserve_size % g_regionsize == 0);
5147
    /* Allocation requested? */
5148
    if (size >= 0) {
5149
        /* Allocation size is the requested size */
5150
        long allocate_size = size;
5151
        /* Compute the size to commit */
5152
        long to_commit = (char *) g_last->top_allocated + allocate_size - (char *) g_last->top_committed;
5153
        /* Do we reach the commit limit? */
5154
        if (to_commit > 0) {
5155
            /* Round size to commit */
5156
            long commit_size = CEIL (to_commit, g_my_pagesize);
5157
            /* Compute the size to reserve */
5158
            long to_reserve = (char *) g_last->top_committed + commit_size - (char *) g_last->top_reserved;
5159
            /* Do we reach the reserve limit? */
5160
            if (to_reserve > 0) {
5161
                /* Compute the remaining size to commit in the current region */
5162
                long remaining_commit_size = (char *) g_last->top_reserved - (char *) g_last->top_committed;
5163
                if (remaining_commit_size > 0) {
5164
                    /* Assert preconditions */
5165
                    assert ((unsigned) g_last->top_committed % g_pagesize == 0);
5166
                    assert (0 < remaining_commit_size && remaining_commit_size % g_pagesize == 0); {
5167
                        /* Commit this */
5168
                        void *base_committed = VirtualAlloc (g_last->top_committed, remaining_commit_size,
5169
							                                 MEM_COMMIT, PAGE_READWRITE);
5170
                        /* Check returned pointer for consistency */
5171
                        if (base_committed != g_last->top_committed)
5172
                            goto sbrk_exit;
5173
                        /* Assert postconditions */
5174
                        assert ((unsigned) base_committed % g_pagesize == 0);
5175
#ifdef TRACESB
5176
                        printf ("Commit %p %ld\n", base_committed, remaining_commit_size);
5177
#endif
5178
                        /* Adjust the regions commit top */
5179
                        g_last->top_committed = (char *) base_committed + remaining_commit_size;
5180
                    }
5181
                } {
5182
                    /* Now we are going to search and reserve. */
5183
                    int contiguous = -1;
5184
                    int found = FALSE;
5185
                    MEMORY_BASIC_INFORMATION memory_info;
5186
                    void *base_reserved;
5187
                    long reserve_size;
5188
                    do {
5189
                        /* Assume contiguous memory */
5190
                        contiguous = TRUE;
5191
                        /* Round size to reserve */
5192
                        reserve_size = CEIL (to_reserve, g_my_regionsize);
5193
                        /* Start with the current region's top */
5194
                        memory_info.BaseAddress = g_last->top_reserved;
5195
                        /* Assert preconditions */
5196
                        assert ((unsigned) memory_info.BaseAddress % g_pagesize == 0);
5197
                        assert (0 < reserve_size && reserve_size % g_regionsize == 0);
5198
                        while (VirtualQuery (memory_info.BaseAddress, &memory_info, sizeof (memory_info))) {
5199
                            /* Assert postconditions */
5200
                            assert ((unsigned) memory_info.BaseAddress % g_pagesize == 0);
5201
#ifdef TRACESB
5202
                            printf ("Query %p %ld %s\n", memory_info.BaseAddress, memory_info.RegionSize, 
5203
                                    memory_info.State == MEM_FREE ? "FREE": 
5204
                                    (memory_info.State == MEM_RESERVE ? "RESERVED":
5205
                                     (memory_info.State == MEM_COMMIT ? "COMMITTED": "?")));
5206
#endif
5207
                            /* Region is free, well aligned and big enough: we are done */
5208
                            if (memory_info.State == MEM_FREE &&
5209
                                (unsigned) memory_info.BaseAddress % g_regionsize == 0 &&
5210
                                memory_info.RegionSize >= (unsigned) reserve_size) {
5211
                                found = TRUE;
5212
                                break;
5213
                            }
5214
                            /* From now on we can't get contiguous memory! */
5215
                            contiguous = FALSE;
5216
                            /* Recompute size to reserve */
5217
                            reserve_size = CEIL (allocate_size, g_my_regionsize);
26945 ripley 5218
			    /* Wasn't free or didn't fit so move to end of block */
5219
			    memory_info.BaseAddress = (char *) memory_info.BaseAddress + memory_info.RegionSize;
5220
			    /* DBT - round up to next valid boundary */
5221
			    memory_info.BaseAddress = (void *)CEIL((unsigned long)memory_info.BaseAddress, g_regionsize);
26855 ripley 5222
                            /* Assert preconditions */
5223
                            assert ((unsigned) memory_info.BaseAddress % g_pagesize == 0);
5224
                            assert (0 < reserve_size && reserve_size % g_regionsize == 0);
5225
                        }
5226
                        /* Search failed? */
5227
                        if (! found) 
5228
                            goto sbrk_exit;
5229
                        /* Assert preconditions */
5230
                        assert ((unsigned) memory_info.BaseAddress % g_regionsize == 0);
5231
                        assert (0 < reserve_size && reserve_size % g_regionsize == 0);
5232
                        /* Try to reserve this */
5233
                        base_reserved = VirtualAlloc (memory_info.BaseAddress, reserve_size, 
5234
					                                  MEM_RESERVE, PAGE_NOACCESS);
5235
                        if (! base_reserved) {
5236
                            int rc = GetLastError ();
5237
                            if (rc != ERROR_INVALID_ADDRESS) 
5238
                                goto sbrk_exit;
5239
                        }
5240
                        /* A null pointer signals (hopefully) a race condition with another thread. */
5241
                        /* In this case, we try again. */
5242
                    } while (! base_reserved);
5243
                    /* Check returned pointer for consistency */
5244
                    if (memory_info.BaseAddress && base_reserved != memory_info.BaseAddress)
5245
                        goto sbrk_exit;
5246
                    /* Assert postconditions */
5247
                    assert ((unsigned) base_reserved % g_regionsize == 0);
5248
#ifdef TRACESB
5249
                    printf ("Reserve %p %ld\n", base_reserved, reserve_size);
5250
#endif
5251
                    /* Did we get contiguous memory? */
5252
                    if (contiguous) {
5253
                        long start_size = (char *) g_last->top_committed - (char *) g_last->top_allocated;
5254
                        /* Adjust allocation size */
5255
                        allocate_size -= start_size;
5256
                        /* Adjust the regions allocation top */
5257
                        g_last->top_allocated = g_last->top_committed;
5258
                        /* Recompute the size to commit */
5259
                        to_commit = (char *) g_last->top_allocated + allocate_size - (char *) g_last->top_committed;
5260
                        /* Round size to commit */
5261
                        commit_size = CEIL (to_commit, g_my_pagesize);
5262
                    } 
5263
                    /* Append the new region to the list */
5264
                    if (! region_list_append (&g_last, base_reserved, reserve_size))
5265
                        goto sbrk_exit;
5266
                    /* Didn't we get contiguous memory? */
5267
                    if (! contiguous) {
5268
                        /* Recompute the size to commit */
5269
                        to_commit = (char *) g_last->top_allocated + allocate_size - (char *) g_last->top_committed;
5270
                        /* Round size to commit */
5271
                        commit_size = CEIL (to_commit, g_my_pagesize);
5272
                    }
5273
                }
5274
            } 
5275
            /* Assert preconditions */
5276
            assert ((unsigned) g_last->top_committed % g_pagesize == 0);
5277
            assert (0 < commit_size && commit_size % g_pagesize == 0); {
26895 ripley 5278
		mstate av = get_malloc_state();
5279
                void *base_committed;
5280
		if (av->sbrked_mem + av->mmapped_mem + size > R_max_memory) {
5281
		    if(R_Is_Running) 
5282
			Rf_warning("Reached total allocation of %dMb: see help(memory.size)", R_max_memory/1048576);
5283
		    goto sbrk_exit;
5284
		}
26855 ripley 5285
                /* Commit this */
26895 ripley 5286
                base_committed = VirtualAlloc (g_last->top_committed, 
5287
					       commit_size, 
5288
					       MEM_COMMIT, PAGE_READWRITE);
26855 ripley 5289
                /* Check returned pointer for consistency */
5290
                if (base_committed != g_last->top_committed)
5291
                    goto sbrk_exit;
5292
                /* Assert postconditions */
5293
                assert ((unsigned) base_committed % g_pagesize == 0);
5294
#ifdef TRACESB
5295
                printf ("Commit %p %ld\n", base_committed, commit_size);
5296
#endif
5297
                /* Adjust the regions commit top */
5298
                g_last->top_committed = (char *) base_committed + commit_size;
5299
            }
5300
        } 
5301
        /* Adjust the regions allocation top */
5302
        g_last->top_allocated = (char *) g_last->top_allocated + allocate_size;
5303
        result = (char *) g_last->top_allocated - size;
5304
    /* Deallocation requested? */
5305
    } else if (size < 0) {
5306
        long deallocate_size = - size;
5307
        /* As long as we have a region to release */
5308
        while ((char *) g_last->top_allocated - deallocate_size < (char *) g_last->top_reserved - g_last->reserve_size) {
5309
            /* Get the size to release */
5310
            long release_size = g_last->reserve_size;
5311
            /* Get the base address */
5312
            void *base_reserved = (char *) g_last->top_reserved - release_size;
5313
            /* Assert preconditions */
5314
            assert ((unsigned) base_reserved % g_regionsize == 0); 
5315
            assert (0 < release_size && release_size % g_regionsize == 0); {
5316
                /* Release this */
5317
                int rc = VirtualFree (base_reserved, 0, 
5318
                                      MEM_RELEASE);
5319
                /* Check returned code for consistency */
5320
                if (! rc)
5321
                    goto sbrk_exit;
5322
#ifdef TRACESB
5323
                printf ("Release %p %ld\n", base_reserved, release_size);
5324
#endif
5325
            }
5326
            /* Adjust deallocation size */
5327
            deallocate_size -= (char *) g_last->top_allocated - (char *) base_reserved;
5328
            /* Remove the old region from the list */
5329
            if (! region_list_remove (&g_last))
5330
                goto sbrk_exit;
5331
        } {
5332
            /* Compute the size to decommit */
5333
            long to_decommit = (char *) g_last->top_committed - ((char *) g_last->top_allocated - deallocate_size);
5334
            if (to_decommit >= g_my_pagesize) {
5335
                /* Compute the size to decommit */
5336
                long decommit_size = FLOOR (to_decommit, g_my_pagesize);
5337
                /*  Compute the base address */
5338
                void *base_committed = (char *) g_last->top_committed - decommit_size;
5339
                /* Assert preconditions */
5340
                assert ((unsigned) base_committed % g_pagesize == 0);
5341
                assert (0 < decommit_size && decommit_size % g_pagesize == 0); {
5342
                    /* Decommit this */
5343
                    int rc = VirtualFree ((char *) base_committed, decommit_size, 
5344
                                          MEM_DECOMMIT);
5345
                    /* Check returned code for consistency */
5346
                    if (! rc)
5347
                        goto sbrk_exit;
5348
#ifdef TRACESB
5349
                    printf ("Decommit %p %ld\n", base_committed, decommit_size);
5350
#endif
5351
                }
5352
                /* Adjust deallocation size and regions commit and allocate top */
5353
                deallocate_size -= (char *) g_last->top_allocated - (char *) base_committed;
5354
                g_last->top_committed = base_committed;
5355
                g_last->top_allocated = base_committed;
5356
            }
5357
        }
5358
        /* Adjust regions allocate top */
5359
        g_last->top_allocated = (char *) g_last->top_allocated - deallocate_size;
5360
        /* Check for underflow */
5361
        if ((char *) g_last->top_reserved - g_last->reserve_size > (char *) g_last->top_allocated ||
5362
            g_last->top_allocated > g_last->top_committed) {
5363
            /* Adjust regions allocate top */
5364
            g_last->top_allocated = (char *) g_last->top_reserved - g_last->reserve_size;
5365
            goto sbrk_exit;
5366
        }
5367
        result = g_last->top_allocated;
5368
    }
5369
    /* Assert invariants */
5370
    assert (g_last);
5371
    assert ((char *) g_last->top_reserved - g_last->reserve_size <= (char *) g_last->top_allocated &&
5372
            g_last->top_allocated <= g_last->top_committed);
5373
    assert ((char *) g_last->top_reserved - g_last->reserve_size <= (char *) g_last->top_committed &&
5374
            g_last->top_committed <= g_last->top_reserved &&
5375
            (unsigned) g_last->top_committed % g_pagesize == 0);
5376
    assert ((unsigned) g_last->top_reserved % g_regionsize == 0);
5377
    assert ((unsigned) g_last->reserve_size % g_regionsize == 0);
10347 ripley 5378
 
26855 ripley 5379
sbrk_exit:
5380
#if defined (USE_MALLOC_LOCK) && defined (NEEDED)
5381
    /* Release spin lock */
5382
    slrelease (&g_sl);
10347 ripley 5383
#endif
26855 ripley 5384
    return result;
5385
}
5386
 
5387
/* mmap for windows */
5388
static void *mmap (void *ptr, long size, long prot, long type, long handle, long arg) {
5389
    static long g_pagesize;
5390
    static long g_regionsize;
5391
#ifdef TRACESB
5392
    printf ("mmap %ld\n", size);
10347 ripley 5393
#endif
26855 ripley 5394
#if defined (USE_MALLOC_LOCK) && defined (NEEDED)
5395
    /* Wait for spin lock */
5396
    slwait (&g_sl);
5397
#endif
5398
    /* First time initialization */
5399
    if (! g_pagesize) 
5400
        g_pagesize = getpagesize ();
5401
    if (! g_regionsize) 
5402
        g_regionsize = getregionsize ();
5403
    /* Assert preconditions */
5404
    assert ((unsigned) ptr % g_regionsize == 0);
5405
    assert (size % g_pagesize == 0);
5406
    /* Allocate this */
5407
    ptr = VirtualAlloc (ptr, size,
26895 ripley 5408
			MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE);
26855 ripley 5409
    if (! ptr) {
5410
        ptr = (void *) MORECORE_FAILURE;
5411
        goto mmap_exit;
5412
    }
5413
    /* Assert postconditions */
5414
    assert ((unsigned) ptr % g_regionsize == 0);
5415
#ifdef TRACESB
5416
    printf ("Commit %p %ld\n", ptr, size);
5417
#endif
5418
mmap_exit:
5419
#if defined (USE_MALLOC_LOCK) && defined (NEEDED)
5420
    /* Release spin lock */
5421
    slrelease (&g_sl);
5422
#endif
5423
    return ptr;
5424
}
10347 ripley 5425
 
26855 ripley 5426
/* munmap for windows */
5427
static long munmap (void *ptr, long size) {
5428
    static long g_pagesize;
5429
    static long g_regionsize;
5430
    int rc = MUNMAP_FAILURE;
5431
#ifdef TRACESB
5432
    printf ("munmap %p %ld\n", ptr, size);
5433
#endif
5434
#if defined (USE_MALLOC_LOCK) && defined (NEEDED)
5435
    /* Wait for spin lock */
5436
    slwait (&g_sl);
5437
#endif
5438
    /* First time initialization */
5439
    if (! g_pagesize) 
5440
        g_pagesize = getpagesize ();
5441
    if (! g_regionsize) 
5442
        g_regionsize = getregionsize ();
5443
    /* Assert preconditions */
5444
    assert ((unsigned) ptr % g_regionsize == 0);
5445
    assert (size % g_pagesize == 0);
5446
    /* Free this */
5447
    if (! VirtualFree (ptr, 0, 
5448
                       MEM_RELEASE))
5449
        goto munmap_exit;
5450
    rc = 0;
5451
#ifdef TRACESB
5452
    printf ("Release %p %ld\n", ptr, size);
5453
#endif
5454
munmap_exit:
5455
#if defined (USE_MALLOC_LOCK) && defined (NEEDED)
5456
    /* Release spin lock */
5457
    slrelease (&g_sl);
5458
#endif
5459
    return rc;
10347 ripley 5460
}
5461
 
26855 ripley 5462
static void vminfo (CHUNK_SIZE_T  *free, CHUNK_SIZE_T  *reserved, CHUNK_SIZE_T  *committed) {
5463
    MEMORY_BASIC_INFORMATION memory_info;
5464
    memory_info.BaseAddress = 0;
5465
    *free = *reserved = *committed = 0;
5466
    while (VirtualQuery (memory_info.BaseAddress, &memory_info, sizeof (memory_info))) {
5467
        switch (memory_info.State) {
5468
        case MEM_FREE:
5469
            *free += memory_info.RegionSize;
5470
            break;
5471
        case MEM_RESERVE:
5472
            *reserved += memory_info.RegionSize;
5473
            break;
5474
        case MEM_COMMIT:
5475
            *committed += memory_info.RegionSize;
5476
            break;
5477
        }
5478
        memory_info.BaseAddress = (char *) memory_info.BaseAddress + memory_info.RegionSize;
5479
    }
5480
}
5481
/* Commented out for R Build Compatibility
5482
static int cpuinfo (int whole, CHUNK_SIZE_T  *kernel, CHUNK_SIZE_T  *user) {
5483
    if (whole) {
5484
        __int64 creation64, exit64, kernel64, user64;
5485
        int rc = GetProcessTimes (GetCurrentProcess (), 
5486
                                  (FILETIME *) &creation64,  
5487
                                  (FILETIME *) &exit64, 
5488
                                  (FILETIME *) &kernel64, 
5489
                                  (FILETIME *) &user64);
5490
        if (! rc) {
5491
            *kernel = 0;
5492
            *user = 0;
5493
            return FALSE;
5494
        } 
5495
        *kernel = (CHUNK_SIZE_T) (kernel64 / 10000);
5496
        *user = (CHUNK_SIZE_T) (user64 / 10000);
5497
        return TRUE;
5498
    } else {
5499
        __int64 creation64, exit64, kernel64, user64;
5500
        int rc = GetThreadTimes (GetCurrentThread (), 
5501
                                 (FILETIME *) &creation64,  
5502
                                 (FILETIME *) &exit64, 
5503
                                 (FILETIME *) &kernel64, 
5504
                                 (FILETIME *) &user64);
5505
        if (! rc) {
5506
            *kernel = 0;
5507
            *user = 0;
5508
            return FALSE;
5509
        } 
5510
        *kernel = (CHUNK_SIZE_T) (kernel64 / 10000);
5511
        *user = (CHUNK_SIZE_T) (user64 / 10000);
5512
        return TRUE;
5513
    }
5514
}
5515
*/
5516
#endif /* WIN32 */
10347 ripley 5517
 
26855 ripley 5518
/* ------------------------------------------------------------
10347 ripley 5519
History:
26855 ripley 5520
    V2.7.2 Sat Aug 17 09:07:30 2002  Doug Lea  (dl at gee)
5521
      * Fix malloc_state bitmap array misdeclaration
10347 ripley 5522
 
26855 ripley 5523
    V2.7.1 Thu Jul 25 10:58:03 2002  Doug Lea  (dl at gee)
5524
      * Allow tuning of FIRST_SORTED_BIN_SIZE
5525
      * Use PTR_UINT as type for all ptr->int casts. Thanks to John Belmonte.
5526
      * Better detection and support for non-contiguousness of MORECORE. 
5527
        Thanks to Andreas Mueller, Conal Walsh, and Wolfram Gloger
5528
      * Bypass most of malloc if no frees. Thanks To Emery Berger.
5529
      * Fix freeing of old top non-contiguous chunk im sysmalloc.
5530
      * Raised default trim and map thresholds to 256K.
5531
      * Fix mmap-related #defines. Thanks to Lubos Lunak.
5532
      * Fix copy macros; added LACKS_FCNTL_H. Thanks to Neal Walfield.
5533
      * Branch-free bin calculation
5534
      * Default trim and mmap thresholds now 256K.
5535
 
5536
    V2.7.0 Sun Mar 11 14:14:06 2001  Doug Lea  (dl at gee)
5537
      * Introduce independent_comalloc and independent_calloc.
5538
        Thanks to Michael Pachos for motivation and help.
5539
      * Make optional .h file available
5540
      * Allow > 2GB requests on 32bit systems.
5541
      * new WIN32 sbrk, mmap, munmap, lock code from <Walter@GeNeSys-e.de>.
5542
        Thanks also to Andreas Mueller <a.mueller at paradatec.de>,
5543
        and Anonymous.
5544
      * Allow override of MALLOC_ALIGNMENT (Thanks to Ruud Waij for 
5545
        helping test this.)
5546
      * memalign: check alignment arg
5547
      * realloc: don't try to shift chunks backwards, since this
5548
        leads to  more fragmentation in some programs and doesn't
5549
        seem to help in any others.
5550
      * Collect all cases in malloc requiring system memory into sYSMALLOc
5551
      * Use mmap as backup to sbrk
5552
      * Place all internal state in malloc_state
5553
      * Introduce fastbins (although similar to 2.5.1)
5554
      * Many minor tunings and cosmetic improvements
5555
      * Introduce USE_PUBLIC_MALLOC_WRAPPERS, USE_MALLOC_LOCK 
5556
      * Introduce MALLOC_FAILURE_ACTION, MORECORE_CONTIGUOUS
5557
        Thanks to Tony E. Bennett <tbennett@nvidia.com> and others.
5558
      * Include errno.h to support default failure action.
5559
 
10425 ripley 5560
    V2.6.6 Sun Dec  5 07:42:19 1999  Doug Lea  (dl at gee)
5561
      * return null for negative arguments
26855 ripley 5562
      * Added Several WIN32 cleanups from Martin C. Fong <mcfong at yahoo.com>
10425 ripley 5563
         * Add 'LACKS_SYS_PARAM_H' for those systems without 'sys/param.h'
5564
          (e.g. WIN32 platforms)
26855 ripley 5565
         * Cleanup header file inclusion for WIN32 platforms
10425 ripley 5566
         * Cleanup code to avoid Microsoft Visual C++ compiler complaints
5567
         * Add 'USE_DL_PREFIX' to quickly allow co-existence with existing
5568
           memory allocation routines
5569
         * Set 'malloc_getpagesize' for WIN32 platforms (needs more work)
5570
         * Use 'assert' rather than 'ASSERT' in WIN32 code to conform to
26855 ripley 5571
           usage of 'assert' in non-WIN32 code
10425 ripley 5572
         * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to
5573
           avoid infinite loop
5574
      * Always call 'fREe()' rather than 'free()'
5575
 
10347 ripley 5576
    V2.6.5 Wed Jun 17 15:57:31 1998  Doug Lea  (dl at gee)
5577
      * Fixed ordering problem with boundary-stamping
5578
 
5579
    V2.6.3 Sun May 19 08:17:58 1996  Doug Lea  (dl at gee)
5580
      * Added pvalloc, as recommended by H.J. Liu
5581
      * Added 64bit pointer support mainly from Wolfram Gloger
5582
      * Added anonymously donated WIN32 sbrk emulation
5583
      * Malloc, calloc, getpagesize: add optimizations from Raymond Nijssen
5584
      * malloc_extend_top: fix mask error that caused wastage after
5585
        foreign sbrks
5586
      * Add linux mremap support code from HJ Liu
5587
 
5588
    V2.6.2 Tue Dec  5 06:52:55 1995  Doug Lea  (dl at gee)
5589
      * Integrated most documentation with the code.
5590
      * Add support for mmap, with help from
5591
        Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
5592
      * Use last_remainder in more cases.
5593
      * Pack bins using idea from  colin@nyx10.cs.du.edu
5594
      * Use ordered bins instead of best-fit threshhold
5595
      * Eliminate block-local decls to simplify tracing and debugging.
5596
      * Support another case of realloc via move into top
5597
      * Fix error occuring when initial sbrk_base not word-aligned.
5598
      * Rely on page size for units instead of SBRK_UNIT to
5599
        avoid surprises about sbrk alignment conventions.
5600
      * Add mallinfo, mallopt. Thanks to Raymond Nijssen
5601
        (raymond@es.ele.tue.nl) for the suggestion.
5602
      * Add `pad' argument to malloc_trim and top_pad mallopt parameter.
5603
      * More precautions for cases where other routines call sbrk,
5604
        courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
5605
      * Added macros etc., allowing use in linux libc from
5606
        H.J. Lu (hjl@gnu.ai.mit.edu)
5607
      * Inverted this history list
5608
 
5609
    V2.6.1 Sat Dec  2 14:10:57 1995  Doug Lea  (dl at gee)
5610
      * Re-tuned and fixed to behave more nicely with V2.6.0 changes.
5611
      * Removed all preallocation code since under current scheme
5612
        the work required to undo bad preallocations exceeds
5613
        the work saved in good cases for most test programs.
5614
      * No longer use return list or unconsolidated bins since
5615
        no scheme using them consistently outperforms those that don't
5616
        given above changes.
5617
      * Use best fit for very large chunks to prevent some worst-cases.
5618
      * Added some support for debugging
5619
 
5620
    V2.6.0 Sat Nov  4 07:05:23 1995  Doug Lea  (dl at gee)
5621
      * Removed footers when chunks are in use. Thanks to
5622
        Paul Wilson (wilson@cs.texas.edu) for the suggestion.
5623
 
5624
    V2.5.4 Wed Nov  1 07:54:51 1995  Doug Lea  (dl at gee)
5625
      * Added malloc_trim, with help from Wolfram Gloger
5626
        (wmglo@Dent.MED.Uni-Muenchen.DE).
5627
 
5628
    V2.5.3 Tue Apr 26 10:16:01 1994  Doug Lea  (dl at g)
5629
 
5630
    V2.5.2 Tue Apr  5 16:20:40 1994  Doug Lea  (dl at g)
5631
      * realloc: try to expand in both directions
5632
      * malloc: swap order of clean-bin strategy;
5633
      * realloc: only conditionally expand backwards
5634
      * Try not to scavenge used bins
5635
      * Use bin counts as a guide to preallocation
5636
      * Occasionally bin return list chunks in first scan
5637
      * Add a few optimizations from colin@nyx10.cs.du.edu
5638
 
5639
    V2.5.1 Sat Aug 14 15:40:43 1993  Doug Lea  (dl at g)
5640
      * faster bin computation & slightly different binning
5641
      * merged all consolidations to one part of malloc proper
5642
         (eliminating old malloc_find_space & malloc_clean_bin)
5643
      * Scan 2 returns chunks (not just 1)
5644
      * Propagate failure in realloc if malloc returns 0
5645
      * Add stuff to allow compilation on non-ANSI compilers
5646
          from kpv@research.att.com
5647
 
5648
    V2.5 Sat Aug  7 07:41:59 1993  Doug Lea  (dl at g.oswego.edu)
5649
      * removed potential for odd address access in prev_chunk
5650
      * removed dependency on getpagesize.h
5651
      * misc cosmetics and a bit more internal documentation
5652
      * anticosmetics: mangled names in macros to evade debugger strangeness
5653
      * tested on sparc, hp-700, dec-mips, rs6000
5654
          with gcc & native cc (hp, dec only) allowing
5655
          Detlefs & Zorn comparison study (in SIGPLAN Notices.)
5656
 
5657
    Trial version Fri Aug 28 13:14:29 1992  Doug Lea  (dl at g.oswego.edu)
5658
      * Based loosely on libg++-1.2X malloc. (It retains some of the overall
5659
         structure of old version,  but most details differ.)
5660
 
5661
*/
5662
 
26855 ripley 5663
/* Need strdup here so we don't get calls to the wrong heap function inside the library */
26879 ripley 5664
char * Rm_strdup (const char *str)
13587 ripley 5665
{
5666
  char *newstr;
5667
 
26879 ripley 5668
  newstr = (char *) Rm_malloc (strlen (str) + 1);
13587 ripley 5669
  if (newstr)
5670
    strcpy (newstr, str);
5671
  return newstr;
5672
}