The R Project SVN R

Rev

Rev 20730 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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