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