diff options
Diffstat (limited to 'src/gmalloc.c')
| -rw-r--r-- | src/gmalloc.c | 607 |
1 files changed, 226 insertions, 381 deletions
diff --git a/src/gmalloc.c b/src/gmalloc.c index 7b5e6df009b..0df050e127a 100644 --- a/src/gmalloc.c +++ b/src/gmalloc.c | |||
| @@ -1,9 +1,3 @@ | |||
| 1 | /* This file is no longer automatically generated from libc. */ | ||
| 2 | |||
| 3 | #define _MALLOC_INTERNAL | ||
| 4 | |||
| 5 | /* The malloc headers and source files from the C library follow here. */ | ||
| 6 | |||
| 7 | /* Declarations for `malloc' and friends. | 1 | /* Declarations for `malloc' and friends. |
| 8 | Copyright (C) 1990, 1991, 1992, 1993, 1995, 1996, 1999, 2002, 2003, 2004, | 2 | Copyright (C) 1990, 1991, 1992, 1993, 1995, 1996, 1999, 2002, 2003, 2004, |
| 9 | 2005, 2006, 2007 Free Software Foundation, Inc. | 3 | 2005, 2006, 2007 Free Software Foundation, Inc. |
| @@ -27,12 +21,6 @@ Fifth Floor, Boston, MA 02110-1301, USA. | |||
| 27 | The author may be reached (Email) at the address mike@ai.mit.edu, | 21 | The author may be reached (Email) at the address mike@ai.mit.edu, |
| 28 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ | 22 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ |
| 29 | 23 | ||
| 30 | #ifndef _MALLOC_H | ||
| 31 | |||
| 32 | #define _MALLOC_H 1 | ||
| 33 | |||
| 34 | #ifdef _MALLOC_INTERNAL | ||
| 35 | |||
| 36 | #ifdef HAVE_CONFIG_H | 24 | #ifdef HAVE_CONFIG_H |
| 37 | #include <config.h> | 25 | #include <config.h> |
| 38 | #endif | 26 | #endif |
| @@ -41,62 +29,44 @@ Fifth Floor, Boston, MA 02110-1301, USA. | |||
| 41 | #define USE_PTHREAD | 29 | #define USE_PTHREAD |
| 42 | #endif | 30 | #endif |
| 43 | 31 | ||
| 44 | #undef PP | ||
| 45 | #define PP(args) args | ||
| 46 | #undef __ptr_t | ||
| 47 | #define __ptr_t void * | ||
| 48 | |||
| 49 | #include <string.h> | 32 | #include <string.h> |
| 50 | #include <limits.h> | 33 | #include <limits.h> |
| 34 | #include <stdint.h> | ||
| 51 | #include <unistd.h> | 35 | #include <unistd.h> |
| 52 | 36 | ||
| 53 | #ifdef USE_PTHREAD | 37 | #ifdef USE_PTHREAD |
| 54 | #include <pthread.h> | 38 | #include <pthread.h> |
| 55 | #endif | 39 | #endif |
| 56 | 40 | ||
| 57 | #endif /* _MALLOC_INTERNAL. */ | ||
| 58 | |||
| 59 | |||
| 60 | #ifdef __cplusplus | 41 | #ifdef __cplusplus |
| 61 | extern "C" | 42 | extern "C" |
| 62 | { | 43 | { |
| 63 | #endif | 44 | #endif |
| 64 | 45 | ||
| 65 | #include <stddef.h> | 46 | #include <stddef.h> |
| 66 | #define __malloc_size_t size_t | ||
| 67 | #define __malloc_ptrdiff_t ptrdiff_t | ||
| 68 | 47 | ||
| 69 | 48 | ||
| 70 | /* Allocate SIZE bytes of memory. */ | 49 | /* Allocate SIZE bytes of memory. */ |
| 71 | extern __ptr_t malloc PP ((__malloc_size_t __size)); | 50 | extern void *malloc (size_t size); |
| 72 | /* Re-allocate the previously allocated block | 51 | /* Re-allocate the previously allocated block |
| 73 | in __ptr_t, making the new block SIZE bytes long. */ | 52 | in ptr, making the new block SIZE bytes long. */ |
| 74 | extern __ptr_t realloc PP ((__ptr_t __ptr, __malloc_size_t __size)); | 53 | extern void *realloc (void *ptr, size_t size); |
| 75 | /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */ | 54 | /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */ |
| 76 | extern __ptr_t calloc PP ((__malloc_size_t __nmemb, __malloc_size_t __size)); | 55 | extern void *calloc (size_t nmemb, size_t size); |
| 77 | /* Free a block allocated by `malloc', `realloc' or `calloc'. */ | 56 | /* Free a block allocated by `malloc', `realloc' or `calloc'. */ |
| 78 | extern void free PP ((__ptr_t __ptr)); | 57 | extern void free (void *ptr); |
| 79 | 58 | ||
| 80 | /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */ | 59 | /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */ |
| 81 | #if !defined (_MALLOC_INTERNAL) || defined (MSDOS) /* Avoid conflict. */ | 60 | #ifdef MSDOS |
| 82 | extern __ptr_t memalign PP ((__malloc_size_t __alignment, | 61 | extern void *memalign (size_t, size_t); |
| 83 | __malloc_size_t __size)); | 62 | extern int posix_memalign (void **, size_t, size_t); |
| 84 | extern int posix_memalign PP ((__ptr_t *, __malloc_size_t, | ||
| 85 | __malloc_size_t size)); | ||
| 86 | #endif | ||
| 87 | |||
| 88 | /* Allocate SIZE bytes on a page boundary. */ | ||
| 89 | #if ! (defined (_MALLOC_INTERNAL) && defined (GMALLOC_INHIBIT_VALLOC)) | ||
| 90 | extern __ptr_t valloc PP ((__malloc_size_t __size)); | ||
| 91 | #endif | 63 | #endif |
| 92 | 64 | ||
| 93 | #ifdef USE_PTHREAD | 65 | #ifdef USE_PTHREAD |
| 94 | /* Set up mutexes and make malloc etc. thread-safe. */ | 66 | /* Set up mutexes and make malloc etc. thread-safe. */ |
| 95 | extern void malloc_enable_thread PP ((void)); | 67 | extern void malloc_enable_thread (void); |
| 96 | #endif | 68 | #endif |
| 97 | 69 | ||
| 98 | #ifdef _MALLOC_INTERNAL | ||
| 99 | |||
| 100 | /* The allocator divides the heap into blocks of fixed size; large | 70 | /* The allocator divides the heap into blocks of fixed size; large |
| 101 | requests receive one or more whole blocks, and small requests | 71 | requests receive one or more whole blocks, and small requests |
| 102 | receive a fragment of a block. Fragment sizes are powers of two, | 72 | receive a fragment of a block. Fragment sizes are powers of two, |
| @@ -128,22 +98,22 @@ typedef union | |||
| 128 | { | 98 | { |
| 129 | struct | 99 | struct |
| 130 | { | 100 | { |
| 131 | __malloc_size_t nfree; /* Free frags in a fragmented block. */ | 101 | size_t nfree; /* Free frags in a fragmented block. */ |
| 132 | __malloc_size_t first; /* First free fragment of the block. */ | 102 | size_t first; /* First free fragment of the block. */ |
| 133 | } frag; | 103 | } frag; |
| 134 | /* For a large object, in its first block, this has the number | 104 | /* For a large object, in its first block, this has the number |
| 135 | of blocks in the object. In the other blocks, this has a | 105 | of blocks in the object. In the other blocks, this has a |
| 136 | negative number which says how far back the first block is. */ | 106 | negative number which says how far back the first block is. */ |
| 137 | __malloc_ptrdiff_t size; | 107 | ptrdiff_t size; |
| 138 | } info; | 108 | } info; |
| 139 | } busy; | 109 | } busy; |
| 140 | /* Heap information for a free block | 110 | /* Heap information for a free block |
| 141 | (that may be the first of a free cluster). */ | 111 | (that may be the first of a free cluster). */ |
| 142 | struct | 112 | struct |
| 143 | { | 113 | { |
| 144 | __malloc_size_t size; /* Size (in blocks) of a free cluster. */ | 114 | size_t size; /* Size (in blocks) of a free cluster. */ |
| 145 | __malloc_size_t next; /* Index of next free cluster. */ | 115 | size_t next; /* Index of next free cluster. */ |
| 146 | __malloc_size_t prev; /* Index of previous free cluster. */ | 116 | size_t prev; /* Index of previous free cluster. */ |
| 147 | } free; | 117 | } free; |
| 148 | } malloc_info; | 118 | } malloc_info; |
| 149 | 119 | ||
| @@ -155,13 +125,13 @@ extern malloc_info *_heapinfo; | |||
| 155 | 125 | ||
| 156 | /* Address to block number and vice versa. */ | 126 | /* Address to block number and vice versa. */ |
| 157 | #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1) | 127 | #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1) |
| 158 | #define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase)) | 128 | #define ADDRESS(B) ((void *) (((B) - 1) * BLOCKSIZE + _heapbase)) |
| 159 | 129 | ||
| 160 | /* Current search index for the heap table. */ | 130 | /* Current search index for the heap table. */ |
| 161 | extern __malloc_size_t _heapindex; | 131 | extern size_t _heapindex; |
| 162 | 132 | ||
| 163 | /* Limit of valid info table indices. */ | 133 | /* Limit of valid info table indices. */ |
| 164 | extern __malloc_size_t _heaplimit; | 134 | extern size_t _heaplimit; |
| 165 | 135 | ||
| 166 | /* Doubly linked lists of free fragments. */ | 136 | /* Doubly linked lists of free fragments. */ |
| 167 | struct list | 137 | struct list |
| @@ -177,26 +147,26 @@ extern struct list _fraghead[]; | |||
| 177 | struct alignlist | 147 | struct alignlist |
| 178 | { | 148 | { |
| 179 | struct alignlist *next; | 149 | struct alignlist *next; |
| 180 | __ptr_t aligned; /* The address that memaligned returned. */ | 150 | void *aligned; /* The address that memaligned returned. */ |
| 181 | __ptr_t exact; /* The address that malloc returned. */ | 151 | void *exact; /* The address that malloc returned. */ |
| 182 | }; | 152 | }; |
| 183 | extern struct alignlist *_aligned_blocks; | 153 | extern struct alignlist *_aligned_blocks; |
| 184 | 154 | ||
| 185 | /* Instrumentation. */ | 155 | /* Instrumentation. */ |
| 186 | extern __malloc_size_t _chunks_used; | 156 | extern size_t _chunks_used; |
| 187 | extern __malloc_size_t _bytes_used; | 157 | extern size_t _bytes_used; |
| 188 | extern __malloc_size_t _chunks_free; | 158 | extern size_t _chunks_free; |
| 189 | extern __malloc_size_t _bytes_free; | 159 | extern size_t _bytes_free; |
| 190 | 160 | ||
| 191 | /* Internal versions of `malloc', `realloc', and `free' | 161 | /* Internal versions of `malloc', `realloc', and `free' |
| 192 | used when these functions need to call each other. | 162 | used when these functions need to call each other. |
| 193 | They are the same but don't call the hooks. */ | 163 | They are the same but don't call the hooks. */ |
| 194 | extern __ptr_t _malloc_internal PP ((__malloc_size_t __size)); | 164 | extern void *_malloc_internal (size_t); |
| 195 | extern __ptr_t _realloc_internal PP ((__ptr_t __ptr, __malloc_size_t __size)); | 165 | extern void *_realloc_internal (void *, size_t); |
| 196 | extern void _free_internal PP ((__ptr_t __ptr)); | 166 | extern void _free_internal (void *); |
| 197 | extern __ptr_t _malloc_internal_nolock PP ((__malloc_size_t __size)); | 167 | extern void *_malloc_internal_nolock (size_t); |
| 198 | extern __ptr_t _realloc_internal_nolock PP ((__ptr_t __ptr, __malloc_size_t __size)); | 168 | extern void *_realloc_internal_nolock (void *, size_t); |
| 199 | extern void _free_internal_nolock PP ((__ptr_t __ptr)); | 169 | extern void _free_internal_nolock (void *); |
| 200 | 170 | ||
| 201 | #ifdef USE_PTHREAD | 171 | #ifdef USE_PTHREAD |
| 202 | extern pthread_mutex_t _malloc_mutex, _aligned_blocks_mutex; | 172 | extern pthread_mutex_t _malloc_mutex, _aligned_blocks_mutex; |
| @@ -228,39 +198,36 @@ extern int _malloc_thread_enabled_p; | |||
| 228 | #define UNLOCK_ALIGNED_BLOCKS() | 198 | #define UNLOCK_ALIGNED_BLOCKS() |
| 229 | #endif | 199 | #endif |
| 230 | 200 | ||
| 231 | #endif /* _MALLOC_INTERNAL. */ | ||
| 232 | |||
| 233 | /* Given an address in the middle of a malloc'd object, | 201 | /* Given an address in the middle of a malloc'd object, |
| 234 | return the address of the beginning of the object. */ | 202 | return the address of the beginning of the object. */ |
| 235 | extern __ptr_t malloc_find_object_address PP ((__ptr_t __ptr)); | 203 | extern void *malloc_find_object_address (void *ptr); |
| 236 | 204 | ||
| 237 | /* Underlying allocation function; successive calls should | 205 | /* Underlying allocation function; successive calls should |
| 238 | return contiguous pieces of memory. */ | 206 | return contiguous pieces of memory. */ |
| 239 | extern __ptr_t (*__morecore) PP ((__malloc_ptrdiff_t __size)); | 207 | extern void *(*__morecore) (ptrdiff_t size); |
| 240 | 208 | ||
| 241 | /* Default value of `__morecore'. */ | 209 | /* Default value of `__morecore'. */ |
| 242 | extern __ptr_t __default_morecore PP ((__malloc_ptrdiff_t __size)); | 210 | extern void *__default_morecore (ptrdiff_t size); |
| 243 | 211 | ||
| 244 | /* If not NULL, this function is called after each time | 212 | /* If not NULL, this function is called after each time |
| 245 | `__morecore' is called to increase the data size. */ | 213 | `__morecore' is called to increase the data size. */ |
| 246 | extern void (*__after_morecore_hook) PP ((void)); | 214 | extern void (*__after_morecore_hook) (void); |
| 247 | 215 | ||
| 248 | /* Number of extra blocks to get each time we ask for more core. | 216 | /* Number of extra blocks to get each time we ask for more core. |
| 249 | This reduces the frequency of calling `(*__morecore)'. */ | 217 | This reduces the frequency of calling `(*__morecore)'. */ |
| 250 | extern __malloc_size_t __malloc_extra_blocks; | 218 | extern size_t __malloc_extra_blocks; |
| 251 | 219 | ||
| 252 | /* Nonzero if `malloc' has been called and done its initialization. */ | 220 | /* Nonzero if `malloc' has been called and done its initialization. */ |
| 253 | extern int __malloc_initialized; | 221 | extern int __malloc_initialized; |
| 254 | /* Function called to initialize malloc data structures. */ | 222 | /* Function called to initialize malloc data structures. */ |
| 255 | extern int __malloc_initialize PP ((void)); | 223 | extern int __malloc_initialize (void); |
| 256 | 224 | ||
| 257 | /* Hooks for debugging versions. */ | 225 | /* Hooks for debugging versions. */ |
| 258 | extern void (*__malloc_initialize_hook) PP ((void)); | 226 | extern void (*__malloc_initialize_hook) (void); |
| 259 | extern void (*__free_hook) PP ((__ptr_t __ptr)); | 227 | extern void (*__free_hook) (void *ptr); |
| 260 | extern __ptr_t (*__malloc_hook) PP ((__malloc_size_t __size)); | 228 | extern void *(*__malloc_hook) (size_t size); |
| 261 | extern __ptr_t (*__realloc_hook) PP ((__ptr_t __ptr, __malloc_size_t __size)); | 229 | extern void *(*__realloc_hook) (void *ptr, size_t size); |
| 262 | extern __ptr_t (*__memalign_hook) PP ((__malloc_size_t __size, | 230 | extern void *(*__memalign_hook) (size_t size, size_t alignment); |
| 263 | __malloc_size_t __alignment)); | ||
| 264 | 231 | ||
| 265 | /* Return values for `mprobe': these are the kinds of inconsistencies that | 232 | /* Return values for `mprobe': these are the kinds of inconsistencies that |
| 266 | `mcheck' enables detection of. */ | 233 | `mcheck' enables detection of. */ |
| @@ -277,52 +244,37 @@ enum mcheck_status | |||
| 277 | before `malloc' is ever called. ABORTFUNC is called with an error code | 244 | before `malloc' is ever called. ABORTFUNC is called with an error code |
| 278 | (see enum above) when an inconsistency is detected. If ABORTFUNC is | 245 | (see enum above) when an inconsistency is detected. If ABORTFUNC is |
| 279 | null, the standard function prints on stderr and then calls `abort'. */ | 246 | null, the standard function prints on stderr and then calls `abort'. */ |
| 280 | extern int mcheck PP ((void (*__abortfunc) PP ((enum mcheck_status)))); | 247 | extern int mcheck (void (*abortfunc) (enum mcheck_status)); |
| 281 | 248 | ||
| 282 | /* Check for aberrations in a particular malloc'd block. You must have | 249 | /* Check for aberrations in a particular malloc'd block. You must have |
| 283 | called `mcheck' already. These are the same checks that `mcheck' does | 250 | called `mcheck' already. These are the same checks that `mcheck' does |
| 284 | when you free or reallocate a block. */ | 251 | when you free or reallocate a block. */ |
| 285 | extern enum mcheck_status mprobe PP ((__ptr_t __ptr)); | 252 | extern enum mcheck_status mprobe (void *ptr); |
| 286 | 253 | ||
| 287 | /* Activate a standard collection of tracing hooks. */ | 254 | /* Activate a standard collection of tracing hooks. */ |
| 288 | extern void mtrace PP ((void)); | 255 | extern void mtrace (void); |
| 289 | extern void muntrace PP ((void)); | 256 | extern void muntrace (void); |
| 290 | 257 | ||
| 291 | /* Statistics available to the user. */ | 258 | /* Statistics available to the user. */ |
| 292 | struct mstats | 259 | struct mstats |
| 293 | { | 260 | { |
| 294 | __malloc_size_t bytes_total; /* Total size of the heap. */ | 261 | size_t bytes_total; /* Total size of the heap. */ |
| 295 | __malloc_size_t chunks_used; /* Chunks allocated by the user. */ | 262 | size_t chunks_used; /* Chunks allocated by the user. */ |
| 296 | __malloc_size_t bytes_used; /* Byte total of user-allocated chunks. */ | 263 | size_t bytes_used; /* Byte total of user-allocated chunks. */ |
| 297 | __malloc_size_t chunks_free; /* Chunks in the free list. */ | 264 | size_t chunks_free; /* Chunks in the free list. */ |
| 298 | __malloc_size_t bytes_free; /* Byte total of chunks in the free list. */ | 265 | size_t bytes_free; /* Byte total of chunks in the free list. */ |
| 299 | }; | 266 | }; |
| 300 | 267 | ||
| 301 | /* Pick up the current statistics. */ | 268 | /* Pick up the current statistics. */ |
| 302 | extern struct mstats mstats PP ((void)); | 269 | extern struct mstats mstats (void); |
| 303 | 270 | ||
| 304 | /* Call WARNFUN with a warning message when memory usage is high. */ | 271 | /* Call WARNFUN with a warning message when memory usage is high. */ |
| 305 | extern void memory_warnings PP ((__ptr_t __start, | 272 | extern void memory_warnings (void *start, void (*warnfun) (const char *)); |
| 306 | void (*__warnfun) PP ((const char *)))); | ||
| 307 | |||
| 308 | |||
| 309 | /* Relocating allocator. */ | ||
| 310 | |||
| 311 | /* Allocate SIZE bytes, and store the address in *HANDLEPTR. */ | ||
| 312 | extern __ptr_t r_alloc PP ((__ptr_t *__handleptr, __malloc_size_t __size)); | ||
| 313 | |||
| 314 | /* Free the storage allocated in HANDLEPTR. */ | ||
| 315 | extern void r_alloc_free PP ((__ptr_t *__handleptr)); | ||
| 316 | |||
| 317 | /* Adjust the block at HANDLEPTR to be SIZE bytes long. */ | ||
| 318 | extern __ptr_t r_re_alloc PP ((__ptr_t *__handleptr, __malloc_size_t __size)); | ||
| 319 | |||
| 320 | 273 | ||
| 321 | #ifdef __cplusplus | 274 | #ifdef __cplusplus |
| 322 | } | 275 | } |
| 323 | #endif | 276 | #endif |
| 324 | 277 | ||
| 325 | #endif /* malloc.h */ | ||
| 326 | /* Memory allocator `malloc'. | 278 | /* Memory allocator `malloc'. |
| 327 | Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc. | 279 | Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc. |
| 328 | Written May 1989 by Mike Haertel. | 280 | Written May 1989 by Mike Haertel. |
| @@ -345,10 +297,6 @@ Fifth Floor, Boston, MA 02110-1301, USA. | |||
| 345 | The author may be reached (Email) at the address mike@ai.mit.edu, | 297 | The author may be reached (Email) at the address mike@ai.mit.edu, |
| 346 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ | 298 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ |
| 347 | 299 | ||
| 348 | #ifndef _MALLOC_INTERNAL | ||
| 349 | #define _MALLOC_INTERNAL | ||
| 350 | #include <malloc.h> | ||
| 351 | #endif | ||
| 352 | #include <errno.h> | 300 | #include <errno.h> |
| 353 | 301 | ||
| 354 | /* On Cygwin there are two heaps. temacs uses the static heap | 302 | /* On Cygwin there are two heaps. temacs uses the static heap |
| @@ -362,15 +310,15 @@ Fifth Floor, Boston, MA 02110-1301, USA. | |||
| 362 | this is changed in the future, we'll have to similarly deal with | 310 | this is changed in the future, we'll have to similarly deal with |
| 363 | reinitializing ralloc. */ | 311 | reinitializing ralloc. */ |
| 364 | #ifdef CYGWIN | 312 | #ifdef CYGWIN |
| 365 | extern __ptr_t bss_sbrk PP ((ptrdiff_t __size)); | 313 | extern void *bss_sbrk (ptrdiff_t size); |
| 366 | extern int bss_sbrk_did_unexec; | 314 | extern int bss_sbrk_did_unexec; |
| 367 | char *bss_sbrk_heapbase; /* _heapbase for static heap */ | 315 | char *bss_sbrk_heapbase; /* _heapbase for static heap */ |
| 368 | malloc_info *bss_sbrk_heapinfo; /* _heapinfo for static heap */ | 316 | malloc_info *bss_sbrk_heapinfo; /* _heapinfo for static heap */ |
| 369 | #endif | 317 | #endif |
| 370 | __ptr_t (*__morecore) PP ((__malloc_ptrdiff_t __size)) = __default_morecore; | 318 | void *(*__morecore) (ptrdiff_t size) = __default_morecore; |
| 371 | 319 | ||
| 372 | /* Debugging hook for `malloc'. */ | 320 | /* Debugging hook for `malloc'. */ |
| 373 | __ptr_t (*__malloc_hook) PP ((__malloc_size_t __size)); | 321 | void *(*__malloc_hook) (size_t size); |
| 374 | 322 | ||
| 375 | /* Pointer to the base of the first block. */ | 323 | /* Pointer to the base of the first block. */ |
| 376 | char *_heapbase; | 324 | char *_heapbase; |
| @@ -379,30 +327,30 @@ char *_heapbase; | |||
| 379 | malloc_info *_heapinfo; | 327 | malloc_info *_heapinfo; |
| 380 | 328 | ||
| 381 | /* Number of info entries. */ | 329 | /* Number of info entries. */ |
| 382 | static __malloc_size_t heapsize; | 330 | static size_t heapsize; |
| 383 | 331 | ||
| 384 | /* Search index in the info table. */ | 332 | /* Search index in the info table. */ |
| 385 | __malloc_size_t _heapindex; | 333 | size_t _heapindex; |
| 386 | 334 | ||
| 387 | /* Limit of valid info table indices. */ | 335 | /* Limit of valid info table indices. */ |
| 388 | __malloc_size_t _heaplimit; | 336 | size_t _heaplimit; |
| 389 | 337 | ||
| 390 | /* Free lists for each fragment size. */ | 338 | /* Free lists for each fragment size. */ |
| 391 | struct list _fraghead[BLOCKLOG]; | 339 | struct list _fraghead[BLOCKLOG]; |
| 392 | 340 | ||
| 393 | /* Instrumentation. */ | 341 | /* Instrumentation. */ |
| 394 | __malloc_size_t _chunks_used; | 342 | size_t _chunks_used; |
| 395 | __malloc_size_t _bytes_used; | 343 | size_t _bytes_used; |
| 396 | __malloc_size_t _chunks_free; | 344 | size_t _chunks_free; |
| 397 | __malloc_size_t _bytes_free; | 345 | size_t _bytes_free; |
| 398 | 346 | ||
| 399 | /* Are you experienced? */ | 347 | /* Are you experienced? */ |
| 400 | int __malloc_initialized; | 348 | int __malloc_initialized; |
| 401 | 349 | ||
| 402 | __malloc_size_t __malloc_extra_blocks; | 350 | size_t __malloc_extra_blocks; |
| 403 | 351 | ||
| 404 | void (*__malloc_initialize_hook) PP ((void)); | 352 | void (*__malloc_initialize_hook) (void); |
| 405 | void (*__after_morecore_hook) PP ((void)); | 353 | void (*__after_morecore_hook) (void); |
| 406 | 354 | ||
| 407 | #if defined GC_MALLOC_CHECK && defined GC_PROTECT_MALLOC_STATE | 355 | #if defined GC_MALLOC_CHECK && defined GC_PROTECT_MALLOC_STATE |
| 408 | 356 | ||
| @@ -419,12 +367,11 @@ void (*__after_morecore_hook) PP ((void)); | |||
| 419 | #include <sys/mman.h> | 367 | #include <sys/mman.h> |
| 420 | 368 | ||
| 421 | static int state_protected_p; | 369 | static int state_protected_p; |
| 422 | static __malloc_size_t last_state_size; | 370 | static size_t last_state_size; |
| 423 | static malloc_info *last_heapinfo; | 371 | static malloc_info *last_heapinfo; |
| 424 | 372 | ||
| 425 | void | 373 | void |
| 426 | protect_malloc_state (protect_p) | 374 | protect_malloc_state (int protect_p) |
| 427 | int protect_p; | ||
| 428 | { | 375 | { |
| 429 | /* If _heapinfo has been relocated, make sure its old location | 376 | /* If _heapinfo has been relocated, make sure its old location |
| 430 | isn't left read-only; it will be reused by malloc. */ | 377 | isn't left read-only; it will be reused by malloc. */ |
| @@ -453,29 +400,25 @@ protect_malloc_state (protect_p) | |||
| 453 | 400 | ||
| 454 | 401 | ||
| 455 | /* Aligned allocation. */ | 402 | /* Aligned allocation. */ |
| 456 | static __ptr_t align PP ((__malloc_size_t)); | 403 | static void * |
| 457 | static __ptr_t | 404 | align (size_t size) |
| 458 | align (size) | ||
| 459 | __malloc_size_t size; | ||
| 460 | { | 405 | { |
| 461 | __ptr_t result; | 406 | void *result; |
| 462 | unsigned long int adj; | 407 | ptrdiff_t adj; |
| 463 | 408 | ||
| 464 | /* align accepts an unsigned argument, but __morecore accepts a | 409 | /* align accepts an unsigned argument, but __morecore accepts a |
| 465 | signed one. This could lead to trouble if SIZE overflows a | 410 | signed one. This could lead to trouble if SIZE overflows the |
| 466 | signed int type accepted by __morecore. We just punt in that | 411 | ptrdiff_t type accepted by __morecore. We just punt in that |
| 467 | case, since they are requesting a ludicrous amount anyway. */ | 412 | case, since they are requesting a ludicrous amount anyway. */ |
| 468 | if ((__malloc_ptrdiff_t)size < 0) | 413 | if (PTRDIFF_MAX < size) |
| 469 | result = 0; | 414 | result = 0; |
| 470 | else | 415 | else |
| 471 | result = (*__morecore) (size); | 416 | result = (*__morecore) (size); |
| 472 | adj = (unsigned long int) ((unsigned long int) ((char *) result - | 417 | adj = (uintptr_t) result % BLOCKSIZE; |
| 473 | (char *) NULL)) % BLOCKSIZE; | ||
| 474 | if (adj != 0) | 418 | if (adj != 0) |
| 475 | { | 419 | { |
| 476 | __ptr_t new; | ||
| 477 | adj = BLOCKSIZE - adj; | 420 | adj = BLOCKSIZE - adj; |
| 478 | new = (*__morecore) (adj); | 421 | (*__morecore) (adj); |
| 479 | result = (char *) result + adj; | 422 | result = (char *) result + adj; |
| 480 | } | 423 | } |
| 481 | 424 | ||
| @@ -488,14 +431,11 @@ align (size) | |||
| 488 | /* Get SIZE bytes, if we can get them starting at END. | 431 | /* Get SIZE bytes, if we can get them starting at END. |
| 489 | Return the address of the space we got. | 432 | Return the address of the space we got. |
| 490 | If we cannot get space at END, fail and return 0. */ | 433 | If we cannot get space at END, fail and return 0. */ |
| 491 | static __ptr_t get_contiguous_space PP ((__malloc_ptrdiff_t, __ptr_t)); | 434 | static void * |
| 492 | static __ptr_t | 435 | get_contiguous_space (ptrdiff_t size, void *position) |
| 493 | get_contiguous_space (size, position) | ||
| 494 | __malloc_ptrdiff_t size; | ||
| 495 | __ptr_t position; | ||
| 496 | { | 436 | { |
| 497 | __ptr_t before; | 437 | void *before; |
| 498 | __ptr_t after; | 438 | void *after; |
| 499 | 439 | ||
| 500 | before = (*__morecore) (0); | 440 | before = (*__morecore) (0); |
| 501 | /* If we can tell in advance that the break is at the wrong place, | 441 | /* If we can tell in advance that the break is at the wrong place, |
| @@ -525,7 +465,7 @@ get_contiguous_space (size, position) | |||
| 525 | static inline void | 465 | static inline void |
| 526 | register_heapinfo (void) | 466 | register_heapinfo (void) |
| 527 | { | 467 | { |
| 528 | __malloc_size_t block, blocks; | 468 | size_t block, blocks; |
| 529 | 469 | ||
| 530 | block = BLOCK (_heapinfo); | 470 | block = BLOCK (_heapinfo); |
| 531 | blocks = BLOCKIFY (heapsize * sizeof (malloc_info)); | 471 | blocks = BLOCKIFY (heapsize * sizeof (malloc_info)); |
| @@ -548,21 +488,21 @@ pthread_mutex_t _aligned_blocks_mutex = PTHREAD_MUTEX_INITIALIZER; | |||
| 548 | int _malloc_thread_enabled_p; | 488 | int _malloc_thread_enabled_p; |
| 549 | 489 | ||
| 550 | static void | 490 | static void |
| 551 | malloc_atfork_handler_prepare () | 491 | malloc_atfork_handler_prepare (void) |
| 552 | { | 492 | { |
| 553 | LOCK (); | 493 | LOCK (); |
| 554 | LOCK_ALIGNED_BLOCKS (); | 494 | LOCK_ALIGNED_BLOCKS (); |
| 555 | } | 495 | } |
| 556 | 496 | ||
| 557 | static void | 497 | static void |
| 558 | malloc_atfork_handler_parent () | 498 | malloc_atfork_handler_parent (void) |
| 559 | { | 499 | { |
| 560 | UNLOCK_ALIGNED_BLOCKS (); | 500 | UNLOCK_ALIGNED_BLOCKS (); |
| 561 | UNLOCK (); | 501 | UNLOCK (); |
| 562 | } | 502 | } |
| 563 | 503 | ||
| 564 | static void | 504 | static void |
| 565 | malloc_atfork_handler_child () | 505 | malloc_atfork_handler_child (void) |
| 566 | { | 506 | { |
| 567 | UNLOCK_ALIGNED_BLOCKS (); | 507 | UNLOCK_ALIGNED_BLOCKS (); |
| 568 | UNLOCK (); | 508 | UNLOCK (); |
| @@ -570,7 +510,7 @@ malloc_atfork_handler_child () | |||
| 570 | 510 | ||
| 571 | /* Set up mutexes and make malloc etc. thread-safe. */ | 511 | /* Set up mutexes and make malloc etc. thread-safe. */ |
| 572 | void | 512 | void |
| 573 | malloc_enable_thread () | 513 | malloc_enable_thread (void) |
| 574 | { | 514 | { |
| 575 | if (_malloc_thread_enabled_p) | 515 | if (_malloc_thread_enabled_p) |
| 576 | return; | 516 | return; |
| @@ -589,7 +529,7 @@ malloc_enable_thread () | |||
| 589 | #endif | 529 | #endif |
| 590 | 530 | ||
| 591 | static void | 531 | static void |
| 592 | malloc_initialize_1 () | 532 | malloc_initialize_1 (void) |
| 593 | { | 533 | { |
| 594 | #ifdef GC_MCHECK | 534 | #ifdef GC_MCHECK |
| 595 | mcheck (NULL); | 535 | mcheck (NULL); |
| @@ -609,7 +549,7 @@ malloc_initialize_1 () | |||
| 609 | (*__malloc_initialize_hook) (); | 549 | (*__malloc_initialize_hook) (); |
| 610 | 550 | ||
| 611 | heapsize = HEAP / BLOCKSIZE; | 551 | heapsize = HEAP / BLOCKSIZE; |
| 612 | _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info)); | 552 | _heapinfo = align (heapsize * sizeof (malloc_info)); |
| 613 | if (_heapinfo == NULL) | 553 | if (_heapinfo == NULL) |
| 614 | return; | 554 | return; |
| 615 | memset (_heapinfo, 0, heapsize * sizeof (malloc_info)); | 555 | memset (_heapinfo, 0, heapsize * sizeof (malloc_info)); |
| @@ -630,7 +570,7 @@ malloc_initialize_1 () | |||
| 630 | main will call malloc which calls this function. That is before any threads | 570 | main will call malloc which calls this function. That is before any threads |
| 631 | or signal handlers has been set up, so we don't need thread protection. */ | 571 | or signal handlers has been set up, so we don't need thread protection. */ |
| 632 | int | 572 | int |
| 633 | __malloc_initialize () | 573 | __malloc_initialize (void) |
| 634 | { | 574 | { |
| 635 | if (__malloc_initialized) | 575 | if (__malloc_initialized) |
| 636 | return 0; | 576 | return 0; |
| @@ -644,14 +584,12 @@ static int morecore_recursing; | |||
| 644 | 584 | ||
| 645 | /* Get neatly aligned memory, initializing or | 585 | /* Get neatly aligned memory, initializing or |
| 646 | growing the heap info table as necessary. */ | 586 | growing the heap info table as necessary. */ |
| 647 | static __ptr_t morecore_nolock PP ((__malloc_size_t)); | 587 | static void * |
| 648 | static __ptr_t | 588 | morecore_nolock (size_t size) |
| 649 | morecore_nolock (size) | ||
| 650 | __malloc_size_t size; | ||
| 651 | { | 589 | { |
| 652 | __ptr_t result; | 590 | void *result; |
| 653 | malloc_info *newinfo, *oldinfo; | 591 | malloc_info *newinfo, *oldinfo; |
| 654 | __malloc_size_t newsize; | 592 | size_t newsize; |
| 655 | 593 | ||
| 656 | if (morecore_recursing) | 594 | if (morecore_recursing) |
| 657 | /* Avoid recursion. The caller will know how to handle a null return. */ | 595 | /* Avoid recursion. The caller will know how to handle a null return. */ |
| @@ -664,7 +602,7 @@ morecore_nolock (size) | |||
| 664 | PROTECT_MALLOC_STATE (0); | 602 | PROTECT_MALLOC_STATE (0); |
| 665 | 603 | ||
| 666 | /* Check if we need to grow the info table. */ | 604 | /* Check if we need to grow the info table. */ |
| 667 | if ((__malloc_size_t) BLOCK ((char *) result + size) > heapsize) | 605 | if ((size_t) BLOCK ((char *) result + size) > heapsize) |
| 668 | { | 606 | { |
| 669 | /* Calculate the new _heapinfo table size. We do not account for the | 607 | /* Calculate the new _heapinfo table size. We do not account for the |
| 670 | added blocks in the table itself, as we hope to place them in | 608 | added blocks in the table itself, as we hope to place them in |
| @@ -673,7 +611,7 @@ morecore_nolock (size) | |||
| 673 | newsize = heapsize; | 611 | newsize = heapsize; |
| 674 | do | 612 | do |
| 675 | newsize *= 2; | 613 | newsize *= 2; |
| 676 | while ((__malloc_size_t) BLOCK ((char *) result + size) > newsize); | 614 | while ((size_t) BLOCK ((char *) result + size) > newsize); |
| 677 | 615 | ||
| 678 | /* We must not reuse existing core for the new info table when called | 616 | /* We must not reuse existing core for the new info table when called |
| 679 | from realloc in the case of growing a large block, because the | 617 | from realloc in the case of growing a large block, because the |
| @@ -689,8 +627,8 @@ morecore_nolock (size) | |||
| 689 | `morecore_recursing' flag and return null. */ | 627 | `morecore_recursing' flag and return null. */ |
| 690 | int save = errno; /* Don't want to clobber errno with ENOMEM. */ | 628 | int save = errno; /* Don't want to clobber errno with ENOMEM. */ |
| 691 | morecore_recursing = 1; | 629 | morecore_recursing = 1; |
| 692 | newinfo = (malloc_info *) _realloc_internal_nolock | 630 | newinfo = _realloc_internal_nolock (_heapinfo, |
| 693 | (_heapinfo, newsize * sizeof (malloc_info)); | 631 | newsize * sizeof (malloc_info)); |
| 694 | morecore_recursing = 0; | 632 | morecore_recursing = 0; |
| 695 | if (newinfo == NULL) | 633 | if (newinfo == NULL) |
| 696 | errno = save; | 634 | errno = save; |
| @@ -710,7 +648,7 @@ morecore_nolock (size) | |||
| 710 | /* Allocate new space for the malloc info table. */ | 648 | /* Allocate new space for the malloc info table. */ |
| 711 | while (1) | 649 | while (1) |
| 712 | { | 650 | { |
| 713 | newinfo = (malloc_info *) align (newsize * sizeof (malloc_info)); | 651 | newinfo = align (newsize * sizeof (malloc_info)); |
| 714 | 652 | ||
| 715 | /* Did it fail? */ | 653 | /* Did it fail? */ |
| 716 | if (newinfo == NULL) | 654 | if (newinfo == NULL) |
| @@ -721,8 +659,8 @@ morecore_nolock (size) | |||
| 721 | 659 | ||
| 722 | /* Is it big enough to record status for its own space? | 660 | /* Is it big enough to record status for its own space? |
| 723 | If so, we win. */ | 661 | If so, we win. */ |
| 724 | if ((__malloc_size_t) BLOCK ((char *) newinfo | 662 | if ((size_t) BLOCK ((char *) newinfo |
| 725 | + newsize * sizeof (malloc_info)) | 663 | + newsize * sizeof (malloc_info)) |
| 726 | < newsize) | 664 | < newsize) |
| 727 | break; | 665 | break; |
| 728 | 666 | ||
| @@ -759,13 +697,12 @@ morecore_nolock (size) | |||
| 759 | } | 697 | } |
| 760 | 698 | ||
| 761 | /* Allocate memory from the heap. */ | 699 | /* Allocate memory from the heap. */ |
| 762 | __ptr_t | 700 | void * |
| 763 | _malloc_internal_nolock (size) | 701 | _malloc_internal_nolock (size_t size) |
| 764 | __malloc_size_t size; | ||
| 765 | { | 702 | { |
| 766 | __ptr_t result; | 703 | void *result; |
| 767 | __malloc_size_t block, blocks, lastblocks, start; | 704 | size_t block, blocks, lastblocks, start; |
| 768 | register __malloc_size_t i; | 705 | register size_t i; |
| 769 | struct list *next; | 706 | struct list *next; |
| 770 | 707 | ||
| 771 | /* ANSI C allows `malloc (0)' to either return NULL, or to return a | 708 | /* ANSI C allows `malloc (0)' to either return NULL, or to return a |
| @@ -790,7 +727,7 @@ _malloc_internal_nolock (size) | |||
| 790 | { | 727 | { |
| 791 | /* Small allocation to receive a fragment of a block. | 728 | /* Small allocation to receive a fragment of a block. |
| 792 | Determine the logarithm to base two of the fragment size. */ | 729 | Determine the logarithm to base two of the fragment size. */ |
| 793 | register __malloc_size_t log = 1; | 730 | register size_t log = 1; |
| 794 | --size; | 731 | --size; |
| 795 | while ((size /= 2) != 0) | 732 | while ((size /= 2) != 0) |
| 796 | ++log; | 733 | ++log; |
| @@ -803,15 +740,14 @@ _malloc_internal_nolock (size) | |||
| 803 | /* There are free fragments of this size. | 740 | /* There are free fragments of this size. |
| 804 | Pop a fragment out of the fragment list and return it. | 741 | Pop a fragment out of the fragment list and return it. |
| 805 | Update the block's nfree and first counters. */ | 742 | Update the block's nfree and first counters. */ |
| 806 | result = (__ptr_t) next; | 743 | result = next; |
| 807 | next->prev->next = next->next; | 744 | next->prev->next = next->next; |
| 808 | if (next->next != NULL) | 745 | if (next->next != NULL) |
| 809 | next->next->prev = next->prev; | 746 | next->next->prev = next->prev; |
| 810 | block = BLOCK (result); | 747 | block = BLOCK (result); |
| 811 | if (--_heapinfo[block].busy.info.frag.nfree != 0) | 748 | if (--_heapinfo[block].busy.info.frag.nfree != 0) |
| 812 | _heapinfo[block].busy.info.frag.first = (unsigned long int) | 749 | _heapinfo[block].busy.info.frag.first = |
| 813 | ((unsigned long int) ((char *) next->next - (char *) NULL) | 750 | (uintptr_t) next->next % BLOCKSIZE >> log; |
| 814 | % BLOCKSIZE) >> log; | ||
| 815 | 751 | ||
| 816 | /* Update the statistics. */ | 752 | /* Update the statistics. */ |
| 817 | ++_chunks_used; | 753 | ++_chunks_used; |
| @@ -843,7 +779,7 @@ _malloc_internal_nolock (size) | |||
| 843 | next->prev = &_fraghead[log]; | 779 | next->prev = &_fraghead[log]; |
| 844 | _fraghead[log].next = next; | 780 | _fraghead[log].next = next; |
| 845 | 781 | ||
| 846 | for (i = 2; i < (__malloc_size_t) (BLOCKSIZE >> log); ++i) | 782 | for (i = 2; i < (size_t) (BLOCKSIZE >> log); ++i) |
| 847 | { | 783 | { |
| 848 | next = (struct list *) ((char *) result + (i << log)); | 784 | next = (struct list *) ((char *) result + (i << log)); |
| 849 | next->next = _fraghead[log].next; | 785 | next->next = _fraghead[log].next; |
| @@ -877,7 +813,7 @@ _malloc_internal_nolock (size) | |||
| 877 | if (block == start) | 813 | if (block == start) |
| 878 | { | 814 | { |
| 879 | /* Need to get more from the system. Get a little extra. */ | 815 | /* Need to get more from the system. Get a little extra. */ |
| 880 | __malloc_size_t wantblocks = blocks + __malloc_extra_blocks; | 816 | size_t wantblocks = blocks + __malloc_extra_blocks; |
| 881 | block = _heapinfo[0].free.prev; | 817 | block = _heapinfo[0].free.prev; |
| 882 | lastblocks = _heapinfo[block].free.size; | 818 | lastblocks = _heapinfo[block].free.size; |
| 883 | /* Check to see if the new core will be contiguous with the | 819 | /* Check to see if the new core will be contiguous with the |
| @@ -959,11 +895,10 @@ _malloc_internal_nolock (size) | |||
| 959 | return result; | 895 | return result; |
| 960 | } | 896 | } |
| 961 | 897 | ||
| 962 | __ptr_t | 898 | void * |
| 963 | _malloc_internal (size) | 899 | _malloc_internal (size_t size) |
| 964 | __malloc_size_t size; | ||
| 965 | { | 900 | { |
| 966 | __ptr_t result; | 901 | void *result; |
| 967 | 902 | ||
| 968 | LOCK (); | 903 | LOCK (); |
| 969 | result = _malloc_internal_nolock (size); | 904 | result = _malloc_internal_nolock (size); |
| @@ -972,11 +907,10 @@ _malloc_internal (size) | |||
| 972 | return result; | 907 | return result; |
| 973 | } | 908 | } |
| 974 | 909 | ||
| 975 | __ptr_t | 910 | void * |
| 976 | malloc (size) | 911 | malloc (size_t size) |
| 977 | __malloc_size_t size; | ||
| 978 | { | 912 | { |
| 979 | __ptr_t (*hook) (__malloc_size_t); | 913 | void *(*hook) (size_t); |
| 980 | 914 | ||
| 981 | if (!__malloc_initialized && !__malloc_initialize ()) | 915 | if (!__malloc_initialized && !__malloc_initialize ()) |
| 982 | return NULL; | 916 | return NULL; |
| @@ -998,24 +932,24 @@ malloc (size) | |||
| 998 | /* On some ANSI C systems, some libc functions call _malloc, _free | 932 | /* On some ANSI C systems, some libc functions call _malloc, _free |
| 999 | and _realloc. Make them use the GNU functions. */ | 933 | and _realloc. Make them use the GNU functions. */ |
| 1000 | 934 | ||
| 1001 | __ptr_t | 935 | extern void *_malloc (size_t); |
| 1002 | _malloc (size) | 936 | extern void _free (void *); |
| 1003 | __malloc_size_t size; | 937 | extern void *_realloc (void *, size_t); |
| 938 | |||
| 939 | void * | ||
| 940 | _malloc (size_t size) | ||
| 1004 | { | 941 | { |
| 1005 | return malloc (size); | 942 | return malloc (size); |
| 1006 | } | 943 | } |
| 1007 | 944 | ||
| 1008 | void | 945 | void |
| 1009 | _free (ptr) | 946 | _free (void *ptr) |
| 1010 | __ptr_t ptr; | ||
| 1011 | { | 947 | { |
| 1012 | free (ptr); | 948 | free (ptr); |
| 1013 | } | 949 | } |
| 1014 | 950 | ||
| 1015 | __ptr_t | 951 | void * |
| 1016 | _realloc (ptr, size) | 952 | _realloc (void *ptr, size_t size) |
| 1017 | __ptr_t ptr; | ||
| 1018 | __malloc_size_t size; | ||
| 1019 | { | 953 | { |
| 1020 | return realloc (ptr, size); | 954 | return realloc (ptr, size); |
| 1021 | } | 955 | } |
| @@ -1043,14 +977,9 @@ Fifth Floor, Boston, MA 02110-1301, USA. | |||
| 1043 | The author may be reached (Email) at the address mike@ai.mit.edu, | 977 | The author may be reached (Email) at the address mike@ai.mit.edu, |
| 1044 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ | 978 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ |
| 1045 | 979 | ||
| 1046 | #ifndef _MALLOC_INTERNAL | ||
| 1047 | #define _MALLOC_INTERNAL | ||
| 1048 | #include <malloc.h> | ||
| 1049 | #endif | ||
| 1050 | |||
| 1051 | 980 | ||
| 1052 | /* Debugging hook for free. */ | 981 | /* Debugging hook for free. */ |
| 1053 | void (*__free_hook) PP ((__ptr_t __ptr)); | 982 | void (*__free_hook) (void *__ptr); |
| 1054 | 983 | ||
| 1055 | /* List of blocks allocated by memalign. */ | 984 | /* List of blocks allocated by memalign. */ |
| 1056 | struct alignlist *_aligned_blocks = NULL; | 985 | struct alignlist *_aligned_blocks = NULL; |
| @@ -1058,15 +987,14 @@ struct alignlist *_aligned_blocks = NULL; | |||
| 1058 | /* Return memory to the heap. | 987 | /* Return memory to the heap. |
| 1059 | Like `_free_internal' but don't lock mutex. */ | 988 | Like `_free_internal' but don't lock mutex. */ |
| 1060 | void | 989 | void |
| 1061 | _free_internal_nolock (ptr) | 990 | _free_internal_nolock (void *ptr) |
| 1062 | __ptr_t ptr; | ||
| 1063 | { | 991 | { |
| 1064 | int type; | 992 | int type; |
| 1065 | __malloc_size_t block, blocks; | 993 | size_t block, blocks; |
| 1066 | register __malloc_size_t i; | 994 | register size_t i; |
| 1067 | struct list *prev, *next; | 995 | struct list *prev, *next; |
| 1068 | __ptr_t curbrk; | 996 | void *curbrk; |
| 1069 | const __malloc_size_t lesscore_threshold | 997 | const size_t lesscore_threshold |
| 1070 | /* Threshold of free space at which we will return some to the system. */ | 998 | /* Threshold of free space at which we will return some to the system. */ |
| 1071 | = FINAL_FREE_BLOCKS + 2 * __malloc_extra_blocks; | 999 | = FINAL_FREE_BLOCKS + 2 * __malloc_extra_blocks; |
| 1072 | 1000 | ||
| @@ -1076,7 +1004,7 @@ _free_internal_nolock (ptr) | |||
| 1076 | return; | 1004 | return; |
| 1077 | 1005 | ||
| 1078 | #ifdef CYGWIN | 1006 | #ifdef CYGWIN |
| 1079 | if (ptr < _heapbase) | 1007 | if ((char *) ptr < _heapbase) |
| 1080 | /* We're being asked to free something in the static heap. */ | 1008 | /* We're being asked to free something in the static heap. */ |
| 1081 | return; | 1009 | return; |
| 1082 | #endif | 1010 | #endif |
| @@ -1162,12 +1090,12 @@ _free_internal_nolock (ptr) | |||
| 1162 | It's possible that moving _heapinfo will allow us to | 1090 | It's possible that moving _heapinfo will allow us to |
| 1163 | return some space to the system. */ | 1091 | return some space to the system. */ |
| 1164 | 1092 | ||
| 1165 | __malloc_size_t info_block = BLOCK (_heapinfo); | 1093 | size_t info_block = BLOCK (_heapinfo); |
| 1166 | __malloc_size_t info_blocks = _heapinfo[info_block].busy.info.size; | 1094 | size_t info_blocks = _heapinfo[info_block].busy.info.size; |
| 1167 | __malloc_size_t prev_block = _heapinfo[block].free.prev; | 1095 | size_t prev_block = _heapinfo[block].free.prev; |
| 1168 | __malloc_size_t prev_blocks = _heapinfo[prev_block].free.size; | 1096 | size_t prev_blocks = _heapinfo[prev_block].free.size; |
| 1169 | __malloc_size_t next_block = _heapinfo[block].free.next; | 1097 | size_t next_block = _heapinfo[block].free.next; |
| 1170 | __malloc_size_t next_blocks = _heapinfo[next_block].free.size; | 1098 | size_t next_blocks = _heapinfo[next_block].free.size; |
| 1171 | 1099 | ||
| 1172 | if (/* Win if this block being freed is last in core, the info table | 1100 | if (/* Win if this block being freed is last in core, the info table |
| 1173 | is just before it, the previous free block is just before the | 1101 | is just before it, the previous free block is just before the |
| @@ -1190,7 +1118,7 @@ _free_internal_nolock (ptr) | |||
| 1190 | ) | 1118 | ) |
| 1191 | { | 1119 | { |
| 1192 | malloc_info *newinfo; | 1120 | malloc_info *newinfo; |
| 1193 | __malloc_size_t oldlimit = _heaplimit; | 1121 | size_t oldlimit = _heaplimit; |
| 1194 | 1122 | ||
| 1195 | /* Free the old info table, clearing _heaplimit to avoid | 1123 | /* Free the old info table, clearing _heaplimit to avoid |
| 1196 | recursion into this code. We don't want to return the | 1124 | recursion into this code. We don't want to return the |
| @@ -1205,8 +1133,7 @@ _free_internal_nolock (ptr) | |||
| 1205 | _heapindex = 0; | 1133 | _heapindex = 0; |
| 1206 | 1134 | ||
| 1207 | /* Allocate new space for the info table and move its data. */ | 1135 | /* Allocate new space for the info table and move its data. */ |
| 1208 | newinfo = (malloc_info *) _malloc_internal_nolock (info_blocks | 1136 | newinfo = _malloc_internal_nolock (info_blocks * BLOCKSIZE); |
| 1209 | * BLOCKSIZE); | ||
| 1210 | PROTECT_MALLOC_STATE (0); | 1137 | PROTECT_MALLOC_STATE (0); |
| 1211 | memmove (newinfo, _heapinfo, info_blocks * BLOCKSIZE); | 1138 | memmove (newinfo, _heapinfo, info_blocks * BLOCKSIZE); |
| 1212 | _heapinfo = newinfo; | 1139 | _heapinfo = newinfo; |
| @@ -1222,7 +1149,7 @@ _free_internal_nolock (ptr) | |||
| 1222 | /* Now see if we can return stuff to the system. */ | 1149 | /* Now see if we can return stuff to the system. */ |
| 1223 | if (block + blocks == _heaplimit && blocks >= lesscore_threshold) | 1150 | if (block + blocks == _heaplimit && blocks >= lesscore_threshold) |
| 1224 | { | 1151 | { |
| 1225 | register __malloc_size_t bytes = blocks * BLOCKSIZE; | 1152 | register size_t bytes = blocks * BLOCKSIZE; |
| 1226 | _heaplimit -= blocks; | 1153 | _heaplimit -= blocks; |
| 1227 | (*__morecore) (-bytes); | 1154 | (*__morecore) (-bytes); |
| 1228 | _heapinfo[_heapinfo[block].free.prev].free.next | 1155 | _heapinfo[_heapinfo[block].free.prev].free.next |
| @@ -1255,7 +1182,7 @@ _free_internal_nolock (ptr) | |||
| 1255 | /* If all fragments of this block are free, remove them | 1182 | /* If all fragments of this block are free, remove them |
| 1256 | from the fragment list and free the whole block. */ | 1183 | from the fragment list and free the whole block. */ |
| 1257 | next = prev; | 1184 | next = prev; |
| 1258 | for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> type); ++i) | 1185 | for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i) |
| 1259 | next = next->next; | 1186 | next = next->next; |
| 1260 | prev->prev->next = next; | 1187 | prev->prev->next = next; |
| 1261 | if (next != NULL) | 1188 | if (next != NULL) |
| @@ -1280,7 +1207,7 @@ _free_internal_nolock (ptr) | |||
| 1280 | /* If some fragments of this block are free, link this | 1207 | /* If some fragments of this block are free, link this |
| 1281 | fragment into the fragment list after the first free | 1208 | fragment into the fragment list after the first free |
| 1282 | fragment of this block. */ | 1209 | fragment of this block. */ |
| 1283 | next = (struct list *) ptr; | 1210 | next = ptr; |
| 1284 | next->next = prev->next; | 1211 | next->next = prev->next; |
| 1285 | next->prev = prev; | 1212 | next->prev = prev; |
| 1286 | prev->next = next; | 1213 | prev->next = next; |
| @@ -1293,11 +1220,10 @@ _free_internal_nolock (ptr) | |||
| 1293 | /* No fragments of this block are free, so link this | 1220 | /* No fragments of this block are free, so link this |
| 1294 | fragment into the fragment list and announce that | 1221 | fragment into the fragment list and announce that |
| 1295 | it is the first free fragment of this block. */ | 1222 | it is the first free fragment of this block. */ |
| 1296 | prev = (struct list *) ptr; | 1223 | prev = ptr; |
| 1297 | _heapinfo[block].busy.info.frag.nfree = 1; | 1224 | _heapinfo[block].busy.info.frag.nfree = 1; |
| 1298 | _heapinfo[block].busy.info.frag.first = (unsigned long int) | 1225 | _heapinfo[block].busy.info.frag.first = |
| 1299 | ((unsigned long int) ((char *) ptr - (char *) NULL) | 1226 | (uintptr_t) ptr % BLOCKSIZE >> type; |
| 1300 | % BLOCKSIZE >> type); | ||
| 1301 | prev->next = _fraghead[type].next; | 1227 | prev->next = _fraghead[type].next; |
| 1302 | prev->prev = &_fraghead[type]; | 1228 | prev->prev = &_fraghead[type]; |
| 1303 | prev->prev->next = prev; | 1229 | prev->prev->next = prev; |
| @@ -1313,8 +1239,7 @@ _free_internal_nolock (ptr) | |||
| 1313 | /* Return memory to the heap. | 1239 | /* Return memory to the heap. |
| 1314 | Like `free' but don't call a __free_hook if there is one. */ | 1240 | Like `free' but don't call a __free_hook if there is one. */ |
| 1315 | void | 1241 | void |
| 1316 | _free_internal (ptr) | 1242 | _free_internal (void *ptr) |
| 1317 | __ptr_t ptr; | ||
| 1318 | { | 1243 | { |
| 1319 | LOCK (); | 1244 | LOCK (); |
| 1320 | _free_internal_nolock (ptr); | 1245 | _free_internal_nolock (ptr); |
| @@ -1324,10 +1249,9 @@ _free_internal (ptr) | |||
| 1324 | /* Return memory to the heap. */ | 1249 | /* Return memory to the heap. */ |
| 1325 | 1250 | ||
| 1326 | void | 1251 | void |
| 1327 | free (ptr) | 1252 | free (void *ptr) |
| 1328 | __ptr_t ptr; | ||
| 1329 | { | 1253 | { |
| 1330 | void (*hook) (__ptr_t) = __free_hook; | 1254 | void (*hook) (void *) = __free_hook; |
| 1331 | 1255 | ||
| 1332 | if (hook != NULL) | 1256 | if (hook != NULL) |
| 1333 | (*hook) (ptr); | 1257 | (*hook) (ptr); |
| @@ -1340,8 +1264,7 @@ free (ptr) | |||
| 1340 | weak_alias (free, cfree) | 1264 | weak_alias (free, cfree) |
| 1341 | #else | 1265 | #else |
| 1342 | void | 1266 | void |
| 1343 | cfree (ptr) | 1267 | cfree (void *ptr) |
| 1344 | __ptr_t ptr; | ||
| 1345 | { | 1268 | { |
| 1346 | free (ptr); | 1269 | free (ptr); |
| 1347 | } | 1270 | } |
| @@ -1368,32 +1291,24 @@ Fifth Floor, Boston, MA 02110-1301, USA. | |||
| 1368 | The author may be reached (Email) at the address mike@ai.mit.edu, | 1291 | The author may be reached (Email) at the address mike@ai.mit.edu, |
| 1369 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ | 1292 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ |
| 1370 | 1293 | ||
| 1371 | #ifndef _MALLOC_INTERNAL | ||
| 1372 | #define _MALLOC_INTERNAL | ||
| 1373 | #include <malloc.h> | ||
| 1374 | #endif | ||
| 1375 | |||
| 1376 | |||
| 1377 | #define min(A, B) ((A) < (B) ? (A) : (B)) | 1294 | #define min(A, B) ((A) < (B) ? (A) : (B)) |
| 1378 | 1295 | ||
| 1379 | /* On Cygwin the dumped emacs may try to realloc storage allocated in | 1296 | /* On Cygwin the dumped emacs may try to realloc storage allocated in |
| 1380 | the static heap. We just malloc space in the new heap and copy the | 1297 | the static heap. We just malloc space in the new heap and copy the |
| 1381 | data. */ | 1298 | data. */ |
| 1382 | #ifdef CYGWIN | 1299 | #ifdef CYGWIN |
| 1383 | __ptr_t | 1300 | void * |
| 1384 | special_realloc (ptr, size) | 1301 | special_realloc (void *ptr, size_t size) |
| 1385 | __ptr_t ptr; | ||
| 1386 | __malloc_size_t size; | ||
| 1387 | { | 1302 | { |
| 1388 | __ptr_t result; | 1303 | void *result; |
| 1389 | int type; | 1304 | int type; |
| 1390 | __malloc_size_t block, oldsize; | 1305 | size_t block, oldsize; |
| 1391 | 1306 | ||
| 1392 | block = ((char *) ptr - bss_sbrk_heapbase) / BLOCKSIZE + 1; | 1307 | block = ((char *) ptr - bss_sbrk_heapbase) / BLOCKSIZE + 1; |
| 1393 | type = bss_sbrk_heapinfo[block].busy.type; | 1308 | type = bss_sbrk_heapinfo[block].busy.type; |
| 1394 | oldsize = | 1309 | oldsize = |
| 1395 | type == 0 ? bss_sbrk_heapinfo[block].busy.info.size * BLOCKSIZE | 1310 | type == 0 ? bss_sbrk_heapinfo[block].busy.info.size * BLOCKSIZE |
| 1396 | : (__malloc_size_t) 1 << type; | 1311 | : (size_t) 1 << type; |
| 1397 | result = _malloc_internal_nolock (size); | 1312 | result = _malloc_internal_nolock (size); |
| 1398 | if (result != NULL) | 1313 | if (result != NULL) |
| 1399 | memcpy (result, ptr, min (oldsize, size)); | 1314 | memcpy (result, ptr, min (oldsize, size)); |
| @@ -1402,7 +1317,7 @@ special_realloc (ptr, size) | |||
| 1402 | #endif | 1317 | #endif |
| 1403 | 1318 | ||
| 1404 | /* Debugging hook for realloc. */ | 1319 | /* Debugging hook for realloc. */ |
| 1405 | __ptr_t (*__realloc_hook) PP ((__ptr_t __ptr, __malloc_size_t __size)); | 1320 | void *(*__realloc_hook) (void *ptr, size_t size); |
| 1406 | 1321 | ||
| 1407 | /* Resize the given region to the new size, returning a pointer | 1322 | /* Resize the given region to the new size, returning a pointer |
| 1408 | to the (possibly moved) region. This is optimized for speed; | 1323 | to the (possibly moved) region. This is optimized for speed; |
| @@ -1410,14 +1325,12 @@ __ptr_t (*__realloc_hook) PP ((__ptr_t __ptr, __malloc_size_t __size)); | |||
| 1410 | achieved by unconditionally allocating and copying to a | 1325 | achieved by unconditionally allocating and copying to a |
| 1411 | new region. This module has incestuous knowledge of the | 1326 | new region. This module has incestuous knowledge of the |
| 1412 | internals of both free and malloc. */ | 1327 | internals of both free and malloc. */ |
| 1413 | __ptr_t | 1328 | void * |
| 1414 | _realloc_internal_nolock (ptr, size) | 1329 | _realloc_internal_nolock (void *ptr, size_t size) |
| 1415 | __ptr_t ptr; | ||
| 1416 | __malloc_size_t size; | ||
| 1417 | { | 1330 | { |
| 1418 | __ptr_t result; | 1331 | void *result; |
| 1419 | int type; | 1332 | int type; |
| 1420 | __malloc_size_t block, blocks, oldlimit; | 1333 | size_t block, blocks, oldlimit; |
| 1421 | 1334 | ||
| 1422 | if (size == 0) | 1335 | if (size == 0) |
| 1423 | { | 1336 | { |
| @@ -1428,7 +1341,7 @@ _realloc_internal_nolock (ptr, size) | |||
| 1428 | return _malloc_internal_nolock (size); | 1341 | return _malloc_internal_nolock (size); |
| 1429 | 1342 | ||
| 1430 | #ifdef CYGWIN | 1343 | #ifdef CYGWIN |
| 1431 | if (ptr < _heapbase) | 1344 | if ((char *) ptr < _heapbase) |
| 1432 | /* ptr points into the static heap */ | 1345 | /* ptr points into the static heap */ |
| 1433 | return special_realloc (ptr, size); | 1346 | return special_realloc (ptr, size); |
| 1434 | #endif | 1347 | #endif |
| @@ -1497,7 +1410,7 @@ _realloc_internal_nolock (ptr, size) | |||
| 1497 | (void) _malloc_internal_nolock (blocks * BLOCKSIZE); | 1410 | (void) _malloc_internal_nolock (blocks * BLOCKSIZE); |
| 1498 | else | 1411 | else |
| 1499 | { | 1412 | { |
| 1500 | __ptr_t previous | 1413 | void *previous |
| 1501 | = _malloc_internal_nolock ((block - _heapindex) * BLOCKSIZE); | 1414 | = _malloc_internal_nolock ((block - _heapindex) * BLOCKSIZE); |
| 1502 | (void) _malloc_internal_nolock (blocks * BLOCKSIZE); | 1415 | (void) _malloc_internal_nolock (blocks * BLOCKSIZE); |
| 1503 | _free_internal_nolock (previous); | 1416 | _free_internal_nolock (previous); |
| @@ -1512,8 +1425,8 @@ _realloc_internal_nolock (ptr, size) | |||
| 1512 | default: | 1425 | default: |
| 1513 | /* Old size is a fragment; type is logarithm | 1426 | /* Old size is a fragment; type is logarithm |
| 1514 | to base two of the fragment size. */ | 1427 | to base two of the fragment size. */ |
| 1515 | if (size > (__malloc_size_t) (1 << (type - 1)) && | 1428 | if (size > (size_t) (1 << (type - 1)) && |
| 1516 | size <= (__malloc_size_t) (1 << type)) | 1429 | size <= (size_t) (1 << type)) |
| 1517 | /* The new size is the same kind of fragment. */ | 1430 | /* The new size is the same kind of fragment. */ |
| 1518 | result = ptr; | 1431 | result = ptr; |
| 1519 | else | 1432 | else |
| @@ -1523,7 +1436,7 @@ _realloc_internal_nolock (ptr, size) | |||
| 1523 | result = _malloc_internal_nolock (size); | 1436 | result = _malloc_internal_nolock (size); |
| 1524 | if (result == NULL) | 1437 | if (result == NULL) |
| 1525 | goto out; | 1438 | goto out; |
| 1526 | memcpy (result, ptr, min (size, (__malloc_size_t) 1 << type)); | 1439 | memcpy (result, ptr, min (size, (size_t) 1 << type)); |
| 1527 | _free_internal_nolock (ptr); | 1440 | _free_internal_nolock (ptr); |
| 1528 | } | 1441 | } |
| 1529 | break; | 1442 | break; |
| @@ -1534,12 +1447,10 @@ _realloc_internal_nolock (ptr, size) | |||
| 1534 | return result; | 1447 | return result; |
| 1535 | } | 1448 | } |
| 1536 | 1449 | ||
| 1537 | __ptr_t | 1450 | void * |
| 1538 | _realloc_internal (ptr, size) | 1451 | _realloc_internal (void *ptr, size_t size) |
| 1539 | __ptr_t ptr; | ||
| 1540 | __malloc_size_t size; | ||
| 1541 | { | 1452 | { |
| 1542 | __ptr_t result; | 1453 | void *result; |
| 1543 | 1454 | ||
| 1544 | LOCK (); | 1455 | LOCK (); |
| 1545 | result = _realloc_internal_nolock (ptr, size); | 1456 | result = _realloc_internal_nolock (ptr, size); |
| @@ -1548,12 +1459,10 @@ _realloc_internal (ptr, size) | |||
| 1548 | return result; | 1459 | return result; |
| 1549 | } | 1460 | } |
| 1550 | 1461 | ||
| 1551 | __ptr_t | 1462 | void * |
| 1552 | realloc (ptr, size) | 1463 | realloc (void *ptr, size_t size) |
| 1553 | __ptr_t ptr; | ||
| 1554 | __malloc_size_t size; | ||
| 1555 | { | 1464 | { |
| 1556 | __ptr_t (*hook) (__ptr_t, __malloc_size_t); | 1465 | void *(*hook) (void *, size_t); |
| 1557 | 1466 | ||
| 1558 | if (!__malloc_initialized && !__malloc_initialize ()) | 1467 | if (!__malloc_initialized && !__malloc_initialize ()) |
| 1559 | return NULL; | 1468 | return NULL; |
| @@ -1581,19 +1490,12 @@ Fifth Floor, Boston, MA 02110-1301, USA. | |||
| 1581 | The author may be reached (Email) at the address mike@ai.mit.edu, | 1490 | The author may be reached (Email) at the address mike@ai.mit.edu, |
| 1582 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ | 1491 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ |
| 1583 | 1492 | ||
| 1584 | #ifndef _MALLOC_INTERNAL | ||
| 1585 | #define _MALLOC_INTERNAL | ||
| 1586 | #include <malloc.h> | ||
| 1587 | #endif | ||
| 1588 | |||
| 1589 | /* Allocate an array of NMEMB elements each SIZE bytes long. | 1493 | /* Allocate an array of NMEMB elements each SIZE bytes long. |
| 1590 | The entire array is initialized to zeros. */ | 1494 | The entire array is initialized to zeros. */ |
| 1591 | __ptr_t | 1495 | void * |
| 1592 | calloc (nmemb, size) | 1496 | calloc (register size_t nmemb, register size_t size) |
| 1593 | register __malloc_size_t nmemb; | ||
| 1594 | register __malloc_size_t size; | ||
| 1595 | { | 1497 | { |
| 1596 | register __ptr_t result = malloc (nmemb * size); | 1498 | register void *result = malloc (nmemb * size); |
| 1597 | 1499 | ||
| 1598 | if (result != NULL) | 1500 | if (result != NULL) |
| 1599 | (void) memset (result, 0, nmemb * size); | 1501 | (void) memset (result, 0, nmemb * size); |
| @@ -1618,11 +1520,6 @@ along with the GNU C Library; see the file COPYING. If not, write to | |||
| 1618 | the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, | 1520 | the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, |
| 1619 | MA 02110-1301, USA. */ | 1521 | MA 02110-1301, USA. */ |
| 1620 | 1522 | ||
| 1621 | #ifndef _MALLOC_INTERNAL | ||
| 1622 | #define _MALLOC_INTERNAL | ||
| 1623 | #include <malloc.h> | ||
| 1624 | #endif | ||
| 1625 | |||
| 1626 | /* uClibc defines __GNU_LIBRARY__, but it is not completely | 1523 | /* uClibc defines __GNU_LIBRARY__, but it is not completely |
| 1627 | compatible. */ | 1524 | compatible. */ |
| 1628 | #if !defined (__GNU_LIBRARY__) || defined (__UCLIBC__) | 1525 | #if !defined (__GNU_LIBRARY__) || defined (__UCLIBC__) |
| @@ -1631,30 +1528,24 @@ MA 02110-1301, USA. */ | |||
| 1631 | /* It is best not to declare this and cast its result on foreign operating | 1528 | /* It is best not to declare this and cast its result on foreign operating |
| 1632 | systems with potentially hostile include files. */ | 1529 | systems with potentially hostile include files. */ |
| 1633 | 1530 | ||
| 1634 | #include <stddef.h> | 1531 | extern void *__sbrk (ptrdiff_t increment); |
| 1635 | extern __ptr_t __sbrk PP ((ptrdiff_t increment)); | ||
| 1636 | #endif /* __GNU_LIBRARY__ && ! defined (__UCLIBC__) */ | 1532 | #endif /* __GNU_LIBRARY__ && ! defined (__UCLIBC__) */ |
| 1637 | 1533 | ||
| 1638 | #ifndef NULL | ||
| 1639 | #define NULL 0 | ||
| 1640 | #endif | ||
| 1641 | |||
| 1642 | /* Allocate INCREMENT more bytes of data space, | 1534 | /* Allocate INCREMENT more bytes of data space, |
| 1643 | and return the start of data space, or NULL on errors. | 1535 | and return the start of data space, or NULL on errors. |
| 1644 | If INCREMENT is negative, shrink data space. */ | 1536 | If INCREMENT is negative, shrink data space. */ |
| 1645 | __ptr_t | 1537 | void * |
| 1646 | __default_morecore (increment) | 1538 | __default_morecore (ptrdiff_t increment) |
| 1647 | __malloc_ptrdiff_t increment; | ||
| 1648 | { | 1539 | { |
| 1649 | __ptr_t result; | 1540 | void *result; |
| 1650 | #if defined (CYGWIN) | 1541 | #if defined (CYGWIN) |
| 1651 | if (!bss_sbrk_did_unexec) | 1542 | if (!bss_sbrk_did_unexec) |
| 1652 | { | 1543 | { |
| 1653 | return bss_sbrk (increment); | 1544 | return bss_sbrk (increment); |
| 1654 | } | 1545 | } |
| 1655 | #endif | 1546 | #endif |
| 1656 | result = (__ptr_t) __sbrk (increment); | 1547 | result = (void *) __sbrk (increment); |
| 1657 | if (result == (__ptr_t) -1) | 1548 | if (result == (void *) -1) |
| 1658 | return NULL; | 1549 | return NULL; |
| 1659 | return result; | 1550 | return result; |
| 1660 | } | 1551 | } |
| @@ -1675,22 +1566,14 @@ License along with this library; see the file COPYING. If | |||
| 1675 | not, write to the Free Software Foundation, Inc., 51 Franklin Street, | 1566 | not, write to the Free Software Foundation, Inc., 51 Franklin Street, |
| 1676 | Fifth Floor, Boston, MA 02110-1301, USA. */ | 1567 | Fifth Floor, Boston, MA 02110-1301, USA. */ |
| 1677 | 1568 | ||
| 1678 | #ifndef _MALLOC_INTERNAL | 1569 | void *(*__memalign_hook) (size_t size, size_t alignment); |
| 1679 | #define _MALLOC_INTERNAL | ||
| 1680 | #include <malloc.h> | ||
| 1681 | #endif | ||
| 1682 | |||
| 1683 | __ptr_t (*__memalign_hook) PP ((__malloc_size_t __size, | ||
| 1684 | __malloc_size_t __alignment)); | ||
| 1685 | 1570 | ||
| 1686 | __ptr_t | 1571 | void * |
| 1687 | memalign (alignment, size) | 1572 | memalign (size_t alignment, size_t size) |
| 1688 | __malloc_size_t alignment; | ||
| 1689 | __malloc_size_t size; | ||
| 1690 | { | 1573 | { |
| 1691 | __ptr_t result; | 1574 | void *result; |
| 1692 | unsigned long int adj, lastadj; | 1575 | size_t adj, lastadj; |
| 1693 | __ptr_t (*hook) (__malloc_size_t, __malloc_size_t) = __memalign_hook; | 1576 | void *(*hook) (size_t, size_t) = __memalign_hook; |
| 1694 | 1577 | ||
| 1695 | if (hook) | 1578 | if (hook) |
| 1696 | return (*hook) (alignment, size); | 1579 | return (*hook) (alignment, size); |
| @@ -1703,7 +1586,7 @@ memalign (alignment, size) | |||
| 1703 | 1586 | ||
| 1704 | /* Figure out how much we will need to pad this particular block | 1587 | /* Figure out how much we will need to pad this particular block |
| 1705 | to achieve the required alignment. */ | 1588 | to achieve the required alignment. */ |
| 1706 | adj = (unsigned long int) ((char *) result - (char *) NULL) % alignment; | 1589 | adj = (uintptr_t) result % alignment; |
| 1707 | 1590 | ||
| 1708 | do | 1591 | do |
| 1709 | { | 1592 | { |
| @@ -1714,7 +1597,7 @@ memalign (alignment, size) | |||
| 1714 | return NULL; | 1597 | return NULL; |
| 1715 | 1598 | ||
| 1716 | lastadj = adj; | 1599 | lastadj = adj; |
| 1717 | adj = (unsigned long int) ((char *) result - (char *) NULL) % alignment; | 1600 | adj = (uintptr_t) result % alignment; |
| 1718 | /* It's conceivable we might have been so unlucky as to get a | 1601 | /* It's conceivable we might have been so unlucky as to get a |
| 1719 | different block with weaker alignment. If so, this block is too | 1602 | different block with weaker alignment. If so, this block is too |
| 1720 | short to contain SIZE after alignment correction. So we must | 1603 | short to contain SIZE after alignment correction. So we must |
| @@ -1735,7 +1618,7 @@ memalign (alignment, size) | |||
| 1735 | break; | 1618 | break; |
| 1736 | if (l == NULL) | 1619 | if (l == NULL) |
| 1737 | { | 1620 | { |
| 1738 | l = (struct alignlist *) malloc (sizeof (struct alignlist)); | 1621 | l = malloc (sizeof (struct alignlist)); |
| 1739 | if (l != NULL) | 1622 | if (l != NULL) |
| 1740 | { | 1623 | { |
| 1741 | l->next = _aligned_blocks; | 1624 | l->next = _aligned_blocks; |
| @@ -1767,15 +1650,12 @@ memalign (alignment, size) | |||
| 1767 | #endif | 1650 | #endif |
| 1768 | 1651 | ||
| 1769 | int | 1652 | int |
| 1770 | posix_memalign (memptr, alignment, size) | 1653 | posix_memalign (void **memptr, size_t alignment, size_t size) |
| 1771 | __ptr_t *memptr; | ||
| 1772 | __malloc_size_t alignment; | ||
| 1773 | __malloc_size_t size; | ||
| 1774 | { | 1654 | { |
| 1775 | __ptr_t mem; | 1655 | void *mem; |
| 1776 | 1656 | ||
| 1777 | if (alignment == 0 | 1657 | if (alignment == 0 |
| 1778 | || alignment % sizeof (__ptr_t) != 0 | 1658 | || alignment % sizeof (void *) != 0 |
| 1779 | || (alignment & (alignment - 1)) != 0) | 1659 | || (alignment & (alignment - 1)) != 0) |
| 1780 | return EINVAL; | 1660 | return EINVAL; |
| 1781 | 1661 | ||
| @@ -1809,43 +1689,27 @@ Fifth Floor, Boston, MA 02110-1301, USA. | |||
| 1809 | The author may be reached (Email) at the address mike@ai.mit.edu, | 1689 | The author may be reached (Email) at the address mike@ai.mit.edu, |
| 1810 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ | 1690 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ |
| 1811 | 1691 | ||
| 1812 | #if defined (_MALLOC_INTERNAL) && defined (GMALLOC_INHIBIT_VALLOC) | ||
| 1813 | |||
| 1814 | /* Emacs defines GMALLOC_INHIBIT_VALLOC to avoid this definition | 1692 | /* Emacs defines GMALLOC_INHIBIT_VALLOC to avoid this definition |
| 1815 | on MSDOS, where it conflicts with a system header file. */ | 1693 | on MSDOS, where it conflicts with a system header file. */ |
| 1816 | 1694 | ||
| 1817 | #define ELIDE_VALLOC | 1695 | #ifndef GMALLOC_INHIBIT_VALLOC |
| 1818 | 1696 | ||
| 1819 | #endif | 1697 | /* Allocate SIZE bytes on a page boundary. */ |
| 1820 | 1698 | extern void *valloc (size_t); | |
| 1821 | #ifndef ELIDE_VALLOC | ||
| 1822 | |||
| 1823 | #if defined (__GNU_LIBRARY__) || defined (_LIBC) | ||
| 1824 | #include <stddef.h> | ||
| 1825 | #include <sys/cdefs.h> | ||
| 1826 | #if defined (__GLIBC__) && __GLIBC__ >= 2 | ||
| 1827 | /* __getpagesize is already declared in <unistd.h> with return type int */ | ||
| 1828 | #else | ||
| 1829 | extern size_t __getpagesize PP ((void)); | ||
| 1830 | #endif | ||
| 1831 | #else | ||
| 1832 | #include "getpagesize.h" | ||
| 1833 | #define __getpagesize() getpagesize () | ||
| 1834 | #endif | ||
| 1835 | 1699 | ||
| 1836 | #ifndef _MALLOC_INTERNAL | 1700 | #if defined _SC_PAGESIZE || !defined HAVE_GETPAGESIZE |
| 1837 | #define _MALLOC_INTERNAL | 1701 | # include "getpagesize.h" |
| 1838 | #include <malloc.h> | 1702 | #elif !defined getpagesize |
| 1703 | extern int getpagesize (void); | ||
| 1839 | #endif | 1704 | #endif |
| 1840 | 1705 | ||
| 1841 | static __malloc_size_t pagesize; | 1706 | static size_t pagesize; |
| 1842 | 1707 | ||
| 1843 | __ptr_t | 1708 | void * |
| 1844 | valloc (size) | 1709 | valloc (size_t size) |
| 1845 | __malloc_size_t size; | ||
| 1846 | { | 1710 | { |
| 1847 | if (pagesize == 0) | 1711 | if (pagesize == 0) |
| 1848 | pagesize = __getpagesize (); | 1712 | pagesize = getpagesize (); |
| 1849 | 1713 | ||
| 1850 | return memalign (pagesize, size); | 1714 | return memalign (pagesize, size); |
| 1851 | } | 1715 | } |
| @@ -1876,41 +1740,31 @@ Fifth Floor, Boston, MA 02110-1301, USA. | |||
| 1876 | The author may be reached (Email) at the address mike@ai.mit.edu, | 1740 | The author may be reached (Email) at the address mike@ai.mit.edu, |
| 1877 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ | 1741 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ |
| 1878 | 1742 | ||
| 1879 | #ifdef emacs | ||
| 1880 | #include <stdio.h> | ||
| 1881 | #else | ||
| 1882 | #ifndef _MALLOC_INTERNAL | ||
| 1883 | #define _MALLOC_INTERNAL | ||
| 1884 | #include <malloc.h> | ||
| 1885 | #include <stdio.h> | 1743 | #include <stdio.h> |
| 1886 | #endif | ||
| 1887 | #endif | ||
| 1888 | 1744 | ||
| 1889 | /* Old hook values. */ | 1745 | /* Old hook values. */ |
| 1890 | static void (*old_free_hook) (__ptr_t ptr); | 1746 | static void (*old_free_hook) (void *ptr); |
| 1891 | static __ptr_t (*old_malloc_hook) (__malloc_size_t size); | 1747 | static void *(*old_malloc_hook) (size_t size); |
| 1892 | static __ptr_t (*old_realloc_hook) (__ptr_t ptr, __malloc_size_t size); | 1748 | static void *(*old_realloc_hook) (void *ptr, size_t size); |
| 1893 | 1749 | ||
| 1894 | /* Function to call when something awful happens. */ | 1750 | /* Function to call when something awful happens. */ |
| 1895 | static void (*abortfunc) (enum mcheck_status); | 1751 | static void (*abortfunc) (enum mcheck_status); |
| 1896 | 1752 | ||
| 1897 | /* Arbitrary magical numbers. */ | 1753 | /* Arbitrary magical numbers. */ |
| 1898 | #define MAGICWORD 0xfedabeeb | 1754 | #define MAGICWORD (SIZE_MAX / 11 ^ SIZE_MAX / 13 << 3) |
| 1899 | #define MAGICFREE 0xd8675309 | 1755 | #define MAGICFREE (SIZE_MAX / 17 ^ SIZE_MAX / 19 << 4) |
| 1900 | #define MAGICBYTE ((char) 0xd7) | 1756 | #define MAGICBYTE ((char) 0xd7) |
| 1901 | #define MALLOCFLOOD ((char) 0x93) | 1757 | #define MALLOCFLOOD ((char) 0x93) |
| 1902 | #define FREEFLOOD ((char) 0x95) | 1758 | #define FREEFLOOD ((char) 0x95) |
| 1903 | 1759 | ||
| 1904 | struct hdr | 1760 | struct hdr |
| 1905 | { | 1761 | { |
| 1906 | __malloc_size_t size; /* Exact size requested by user. */ | 1762 | size_t size; /* Exact size requested by user. */ |
| 1907 | unsigned long int magic; /* Magic number to check header integrity. */ | 1763 | size_t magic; /* Magic number to check header integrity. */ |
| 1908 | }; | 1764 | }; |
| 1909 | 1765 | ||
| 1910 | static enum mcheck_status checkhdr (const struct hdr *); | ||
| 1911 | static enum mcheck_status | 1766 | static enum mcheck_status |
| 1912 | checkhdr (hdr) | 1767 | checkhdr (const struct hdr *hdr) |
| 1913 | const struct hdr *hdr; | ||
| 1914 | { | 1768 | { |
| 1915 | enum mcheck_status status; | 1769 | enum mcheck_status status; |
| 1916 | switch (hdr->magic) | 1770 | switch (hdr->magic) |
| @@ -1933,10 +1787,8 @@ checkhdr (hdr) | |||
| 1933 | return status; | 1787 | return status; |
| 1934 | } | 1788 | } |
| 1935 | 1789 | ||
| 1936 | static void freehook (__ptr_t); | ||
| 1937 | static void | 1790 | static void |
| 1938 | freehook (ptr) | 1791 | freehook (void *ptr) |
| 1939 | __ptr_t ptr; | ||
| 1940 | { | 1792 | { |
| 1941 | struct hdr *hdr; | 1793 | struct hdr *hdr; |
| 1942 | 1794 | ||
| @@ -1955,15 +1807,13 @@ freehook (ptr) | |||
| 1955 | __free_hook = freehook; | 1807 | __free_hook = freehook; |
| 1956 | } | 1808 | } |
| 1957 | 1809 | ||
| 1958 | static __ptr_t mallochook (__malloc_size_t); | 1810 | static void * |
| 1959 | static __ptr_t | 1811 | mallochook (size_t size) |
| 1960 | mallochook (size) | ||
| 1961 | __malloc_size_t size; | ||
| 1962 | { | 1812 | { |
| 1963 | struct hdr *hdr; | 1813 | struct hdr *hdr; |
| 1964 | 1814 | ||
| 1965 | __malloc_hook = old_malloc_hook; | 1815 | __malloc_hook = old_malloc_hook; |
| 1966 | hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1); | 1816 | hdr = malloc (sizeof (struct hdr) + size + 1); |
| 1967 | __malloc_hook = mallochook; | 1817 | __malloc_hook = mallochook; |
| 1968 | if (hdr == NULL) | 1818 | if (hdr == NULL) |
| 1969 | return NULL; | 1819 | return NULL; |
| @@ -1971,18 +1821,15 @@ mallochook (size) | |||
| 1971 | hdr->size = size; | 1821 | hdr->size = size; |
| 1972 | hdr->magic = MAGICWORD; | 1822 | hdr->magic = MAGICWORD; |
| 1973 | ((char *) &hdr[1])[size] = MAGICBYTE; | 1823 | ((char *) &hdr[1])[size] = MAGICBYTE; |
| 1974 | memset ((__ptr_t) (hdr + 1), MALLOCFLOOD, size); | 1824 | memset (hdr + 1, MALLOCFLOOD, size); |
| 1975 | return (__ptr_t) (hdr + 1); | 1825 | return hdr + 1; |
| 1976 | } | 1826 | } |
| 1977 | 1827 | ||
| 1978 | static __ptr_t reallochook (__ptr_t, __malloc_size_t); | 1828 | static void * |
| 1979 | static __ptr_t | 1829 | reallochook (void *ptr, size_t size) |
| 1980 | reallochook (ptr, size) | ||
| 1981 | __ptr_t ptr; | ||
| 1982 | __malloc_size_t size; | ||
| 1983 | { | 1830 | { |
| 1984 | struct hdr *hdr = NULL; | 1831 | struct hdr *hdr = NULL; |
| 1985 | __malloc_size_t osize = 0; | 1832 | size_t osize = 0; |
| 1986 | 1833 | ||
| 1987 | if (ptr) | 1834 | if (ptr) |
| 1988 | { | 1835 | { |
| @@ -1997,7 +1844,7 @@ reallochook (ptr, size) | |||
| 1997 | __free_hook = old_free_hook; | 1844 | __free_hook = old_free_hook; |
| 1998 | __malloc_hook = old_malloc_hook; | 1845 | __malloc_hook = old_malloc_hook; |
| 1999 | __realloc_hook = old_realloc_hook; | 1846 | __realloc_hook = old_realloc_hook; |
| 2000 | hdr = (struct hdr *) realloc ((__ptr_t) hdr, sizeof (struct hdr) + size + 1); | 1847 | hdr = realloc (hdr, sizeof (struct hdr) + size + 1); |
| 2001 | __free_hook = freehook; | 1848 | __free_hook = freehook; |
| 2002 | __malloc_hook = mallochook; | 1849 | __malloc_hook = mallochook; |
| 2003 | __realloc_hook = reallochook; | 1850 | __realloc_hook = reallochook; |
| @@ -2009,12 +1856,11 @@ reallochook (ptr, size) | |||
| 2009 | ((char *) &hdr[1])[size] = MAGICBYTE; | 1856 | ((char *) &hdr[1])[size] = MAGICBYTE; |
| 2010 | if (size > osize) | 1857 | if (size > osize) |
| 2011 | memset ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize); | 1858 | memset ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize); |
| 2012 | return (__ptr_t) (hdr + 1); | 1859 | return hdr + 1; |
| 2013 | } | 1860 | } |
| 2014 | 1861 | ||
| 2015 | static void | 1862 | static void |
| 2016 | mabort (status) | 1863 | mabort (enum mcheck_status status) |
| 2017 | enum mcheck_status status; | ||
| 2018 | { | 1864 | { |
| 2019 | const char *msg; | 1865 | const char *msg; |
| 2020 | switch (status) | 1866 | switch (status) |
| @@ -2047,8 +1893,7 @@ mabort (status) | |||
| 2047 | static int mcheck_used = 0; | 1893 | static int mcheck_used = 0; |
| 2048 | 1894 | ||
| 2049 | int | 1895 | int |
| 2050 | mcheck (func) | 1896 | mcheck (void (*func) (enum mcheck_status)) |
| 2051 | void (*func) (enum mcheck_status); | ||
| 2052 | { | 1897 | { |
| 2053 | abortfunc = (func != NULL) ? func : &mabort; | 1898 | abortfunc = (func != NULL) ? func : &mabort; |
| 2054 | 1899 | ||
| @@ -2068,7 +1913,7 @@ mcheck (func) | |||
| 2068 | } | 1913 | } |
| 2069 | 1914 | ||
| 2070 | enum mcheck_status | 1915 | enum mcheck_status |
| 2071 | mprobe (__ptr_t ptr) | 1916 | mprobe (void *ptr) |
| 2072 | { | 1917 | { |
| 2073 | return mcheck_used ? checkhdr (ptr) : MCHECK_DISABLED; | 1918 | return mcheck_used ? checkhdr (ptr) : MCHECK_DISABLED; |
| 2074 | } | 1919 | } |