aboutsummaryrefslogtreecommitdiffstats
path: root/src/gmalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gmalloc.c')
-rw-r--r--src/gmalloc.c607
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
61extern "C" 42extern "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. */
71extern __ptr_t malloc PP ((__malloc_size_t __size)); 50extern 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. */
74extern __ptr_t realloc PP ((__ptr_t __ptr, __malloc_size_t __size)); 53extern 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. */
76extern __ptr_t calloc PP ((__malloc_size_t __nmemb, __malloc_size_t __size)); 55extern 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'. */
78extern void free PP ((__ptr_t __ptr)); 57extern 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
82extern __ptr_t memalign PP ((__malloc_size_t __alignment, 61extern void *memalign (size_t, size_t);
83 __malloc_size_t __size)); 62extern int posix_memalign (void **, size_t, size_t);
84extern 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))
90extern __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. */
95extern void malloc_enable_thread PP ((void)); 67extern 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. */
161extern __malloc_size_t _heapindex; 131extern size_t _heapindex;
162 132
163/* Limit of valid info table indices. */ 133/* Limit of valid info table indices. */
164extern __malloc_size_t _heaplimit; 134extern size_t _heaplimit;
165 135
166/* Doubly linked lists of free fragments. */ 136/* Doubly linked lists of free fragments. */
167struct list 137struct list
@@ -177,26 +147,26 @@ extern struct list _fraghead[];
177struct alignlist 147struct 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 };
183extern struct alignlist *_aligned_blocks; 153extern struct alignlist *_aligned_blocks;
184 154
185/* Instrumentation. */ 155/* Instrumentation. */
186extern __malloc_size_t _chunks_used; 156extern size_t _chunks_used;
187extern __malloc_size_t _bytes_used; 157extern size_t _bytes_used;
188extern __malloc_size_t _chunks_free; 158extern size_t _chunks_free;
189extern __malloc_size_t _bytes_free; 159extern 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. */
194extern __ptr_t _malloc_internal PP ((__malloc_size_t __size)); 164extern void *_malloc_internal (size_t);
195extern __ptr_t _realloc_internal PP ((__ptr_t __ptr, __malloc_size_t __size)); 165extern void *_realloc_internal (void *, size_t);
196extern void _free_internal PP ((__ptr_t __ptr)); 166extern void _free_internal (void *);
197extern __ptr_t _malloc_internal_nolock PP ((__malloc_size_t __size)); 167extern void *_malloc_internal_nolock (size_t);
198extern __ptr_t _realloc_internal_nolock PP ((__ptr_t __ptr, __malloc_size_t __size)); 168extern void *_realloc_internal_nolock (void *, size_t);
199extern void _free_internal_nolock PP ((__ptr_t __ptr)); 169extern void _free_internal_nolock (void *);
200 170
201#ifdef USE_PTHREAD 171#ifdef USE_PTHREAD
202extern pthread_mutex_t _malloc_mutex, _aligned_blocks_mutex; 172extern 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. */
235extern __ptr_t malloc_find_object_address PP ((__ptr_t __ptr)); 203extern 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. */
239extern __ptr_t (*__morecore) PP ((__malloc_ptrdiff_t __size)); 207extern void *(*__morecore) (ptrdiff_t size);
240 208
241/* Default value of `__morecore'. */ 209/* Default value of `__morecore'. */
242extern __ptr_t __default_morecore PP ((__malloc_ptrdiff_t __size)); 210extern 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. */
246extern void (*__after_morecore_hook) PP ((void)); 214extern 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)'. */
250extern __malloc_size_t __malloc_extra_blocks; 218extern 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. */
253extern int __malloc_initialized; 221extern int __malloc_initialized;
254/* Function called to initialize malloc data structures. */ 222/* Function called to initialize malloc data structures. */
255extern int __malloc_initialize PP ((void)); 223extern int __malloc_initialize (void);
256 224
257/* Hooks for debugging versions. */ 225/* Hooks for debugging versions. */
258extern void (*__malloc_initialize_hook) PP ((void)); 226extern void (*__malloc_initialize_hook) (void);
259extern void (*__free_hook) PP ((__ptr_t __ptr)); 227extern void (*__free_hook) (void *ptr);
260extern __ptr_t (*__malloc_hook) PP ((__malloc_size_t __size)); 228extern void *(*__malloc_hook) (size_t size);
261extern __ptr_t (*__realloc_hook) PP ((__ptr_t __ptr, __malloc_size_t __size)); 229extern void *(*__realloc_hook) (void *ptr, size_t size);
262extern __ptr_t (*__memalign_hook) PP ((__malloc_size_t __size, 230extern 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'. */
280extern int mcheck PP ((void (*__abortfunc) PP ((enum mcheck_status)))); 247extern 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. */
285extern enum mcheck_status mprobe PP ((__ptr_t __ptr)); 252extern enum mcheck_status mprobe (void *ptr);
286 253
287/* Activate a standard collection of tracing hooks. */ 254/* Activate a standard collection of tracing hooks. */
288extern void mtrace PP ((void)); 255extern void mtrace (void);
289extern void muntrace PP ((void)); 256extern void muntrace (void);
290 257
291/* Statistics available to the user. */ 258/* Statistics available to the user. */
292struct mstats 259struct 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. */
302extern struct mstats mstats PP ((void)); 269extern 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. */
305extern void memory_warnings PP ((__ptr_t __start, 272extern 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. */
312extern __ptr_t r_alloc PP ((__ptr_t *__handleptr, __malloc_size_t __size));
313
314/* Free the storage allocated in HANDLEPTR. */
315extern void r_alloc_free PP ((__ptr_t *__handleptr));
316
317/* Adjust the block at HANDLEPTR to be SIZE bytes long. */
318extern __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
365extern __ptr_t bss_sbrk PP ((ptrdiff_t __size)); 313extern void *bss_sbrk (ptrdiff_t size);
366extern int bss_sbrk_did_unexec; 314extern int bss_sbrk_did_unexec;
367char *bss_sbrk_heapbase; /* _heapbase for static heap */ 315char *bss_sbrk_heapbase; /* _heapbase for static heap */
368malloc_info *bss_sbrk_heapinfo; /* _heapinfo for static heap */ 316malloc_info *bss_sbrk_heapinfo; /* _heapinfo for static heap */
369#endif 317#endif
370__ptr_t (*__morecore) PP ((__malloc_ptrdiff_t __size)) = __default_morecore; 318void *(*__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)); 321void *(*__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. */
376char *_heapbase; 324char *_heapbase;
@@ -379,30 +327,30 @@ char *_heapbase;
379malloc_info *_heapinfo; 327malloc_info *_heapinfo;
380 328
381/* Number of info entries. */ 329/* Number of info entries. */
382static __malloc_size_t heapsize; 330static size_t heapsize;
383 331
384/* Search index in the info table. */ 332/* Search index in the info table. */
385__malloc_size_t _heapindex; 333size_t _heapindex;
386 334
387/* Limit of valid info table indices. */ 335/* Limit of valid info table indices. */
388__malloc_size_t _heaplimit; 336size_t _heaplimit;
389 337
390/* Free lists for each fragment size. */ 338/* Free lists for each fragment size. */
391struct list _fraghead[BLOCKLOG]; 339struct list _fraghead[BLOCKLOG];
392 340
393/* Instrumentation. */ 341/* Instrumentation. */
394__malloc_size_t _chunks_used; 342size_t _chunks_used;
395__malloc_size_t _bytes_used; 343size_t _bytes_used;
396__malloc_size_t _chunks_free; 344size_t _chunks_free;
397__malloc_size_t _bytes_free; 345size_t _bytes_free;
398 346
399/* Are you experienced? */ 347/* Are you experienced? */
400int __malloc_initialized; 348int __malloc_initialized;
401 349
402__malloc_size_t __malloc_extra_blocks; 350size_t __malloc_extra_blocks;
403 351
404void (*__malloc_initialize_hook) PP ((void)); 352void (*__malloc_initialize_hook) (void);
405void (*__after_morecore_hook) PP ((void)); 353void (*__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
421static int state_protected_p; 369static int state_protected_p;
422static __malloc_size_t last_state_size; 370static size_t last_state_size;
423static malloc_info *last_heapinfo; 371static malloc_info *last_heapinfo;
424 372
425void 373void
426protect_malloc_state (protect_p) 374protect_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. */
456static __ptr_t align PP ((__malloc_size_t)); 403static void *
457static __ptr_t 404align (size_t size)
458align (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. */
491static __ptr_t get_contiguous_space PP ((__malloc_ptrdiff_t, __ptr_t)); 434static void *
492static __ptr_t 435get_contiguous_space (ptrdiff_t size, void *position)
493get_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)
525static inline void 465static inline void
526register_heapinfo (void) 466register_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;
548int _malloc_thread_enabled_p; 488int _malloc_thread_enabled_p;
549 489
550static void 490static void
551malloc_atfork_handler_prepare () 491malloc_atfork_handler_prepare (void)
552{ 492{
553 LOCK (); 493 LOCK ();
554 LOCK_ALIGNED_BLOCKS (); 494 LOCK_ALIGNED_BLOCKS ();
555} 495}
556 496
557static void 497static void
558malloc_atfork_handler_parent () 498malloc_atfork_handler_parent (void)
559{ 499{
560 UNLOCK_ALIGNED_BLOCKS (); 500 UNLOCK_ALIGNED_BLOCKS ();
561 UNLOCK (); 501 UNLOCK ();
562} 502}
563 503
564static void 504static void
565malloc_atfork_handler_child () 505malloc_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. */
572void 512void
573malloc_enable_thread () 513malloc_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
591static void 531static void
592malloc_initialize_1 () 532malloc_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. */
632int 572int
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. */
647static __ptr_t morecore_nolock PP ((__malloc_size_t)); 587static void *
648static __ptr_t 588morecore_nolock (size_t size)
649morecore_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 700void *
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 898void *
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 910void *
976malloc (size) 911malloc (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 935extern void *_malloc (size_t);
1002_malloc (size) 936extern void _free (void *);
1003 __malloc_size_t size; 937extern void *_realloc (void *, size_t);
938
939void *
940_malloc (size_t size)
1004{ 941{
1005 return malloc (size); 942 return malloc (size);
1006} 943}
1007 944
1008void 945void
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 951void *
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. */
1053void (*__free_hook) PP ((__ptr_t __ptr)); 982void (*__free_hook) (void *__ptr);
1054 983
1055/* List of blocks allocated by memalign. */ 984/* List of blocks allocated by memalign. */
1056struct alignlist *_aligned_blocks = NULL; 985struct 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. */
1060void 989void
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. */
1315void 1241void
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
1326void 1251void
1327free (ptr) 1252free (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)
1340weak_alias (free, cfree) 1264weak_alias (free, cfree)
1341#else 1265#else
1342void 1266void
1343cfree (ptr) 1267cfree (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 1300void *
1384special_realloc (ptr, size) 1301special_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)); 1320void *(*__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 1328void *
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 1450void *
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 1462void *
1552realloc (ptr, size) 1463realloc (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 1495void *
1592calloc (nmemb, size) 1496calloc (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
1618the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 1520the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
1619MA 02110-1301, USA. */ 1521MA 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> 1531extern void *__sbrk (ptrdiff_t increment);
1635extern __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 1537void *
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
1675not, write to the Free Software Foundation, Inc., 51 Franklin Street, 1566not, write to the Free Software Foundation, Inc., 51 Franklin Street,
1676Fifth Floor, Boston, MA 02110-1301, USA. */ 1567Fifth Floor, Boston, MA 02110-1301, USA. */
1677 1568
1678#ifndef _MALLOC_INTERNAL 1569void *(*__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 1571void *
1687memalign (alignment, size) 1572memalign (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
1769int 1652int
1770posix_memalign (memptr, alignment, size) 1653posix_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 1698extern 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
1829extern 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
1703extern int getpagesize (void);
1839#endif 1704#endif
1840 1705
1841static __malloc_size_t pagesize; 1706static size_t pagesize;
1842 1707
1843__ptr_t 1708void *
1844valloc (size) 1709valloc (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. */
1890static void (*old_free_hook) (__ptr_t ptr); 1746static void (*old_free_hook) (void *ptr);
1891static __ptr_t (*old_malloc_hook) (__malloc_size_t size); 1747static void *(*old_malloc_hook) (size_t size);
1892static __ptr_t (*old_realloc_hook) (__ptr_t ptr, __malloc_size_t size); 1748static 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. */
1895static void (*abortfunc) (enum mcheck_status); 1751static 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
1904struct hdr 1760struct 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
1910static enum mcheck_status checkhdr (const struct hdr *);
1911static enum mcheck_status 1766static enum mcheck_status
1912checkhdr (hdr) 1767checkhdr (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
1936static void freehook (__ptr_t);
1937static void 1790static void
1938freehook (ptr) 1791freehook (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
1958static __ptr_t mallochook (__malloc_size_t); 1810static void *
1959static __ptr_t 1811mallochook (size_t size)
1960mallochook (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
1978static __ptr_t reallochook (__ptr_t, __malloc_size_t); 1828static void *
1979static __ptr_t 1829reallochook (void *ptr, size_t size)
1980reallochook (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
2015static void 1862static void
2016mabort (status) 1863mabort (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)
2047static int mcheck_used = 0; 1893static int mcheck_used = 0;
2048 1894
2049int 1895int
2050mcheck (func) 1896mcheck (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
2070enum mcheck_status 1915enum mcheck_status
2071mprobe (__ptr_t ptr) 1916mprobe (void *ptr)
2072{ 1917{
2073 return mcheck_used ? checkhdr (ptr) : MCHECK_DISABLED; 1918 return mcheck_used ? checkhdr (ptr) : MCHECK_DISABLED;
2074} 1919}