The R Project SVN R

Rev

Rev 26945 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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