1 /* Declarations for `malloc' and friends.
2 Copyright (C) 1990-1993, 1995-1996, 1999, 2002-2007, 2013-2017 Free
3 Software Foundation, Inc.
4 Written May 1989 by Mike Haertel.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public
17 License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 The author may be reached (Email) at the address mike@ai.mit.edu,
20 or (US mail) as Mike Haertel c/o Free Software Foundation. */
24 #if defined HAVE_PTHREAD && !defined HYBRID_MALLOC
44 # if GNUC_PREREQ (4, 2, 0)
45 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
49 #ifndef __MALLOC_HOOK_VOLATILE
50 # define __MALLOC_HOOK_VOLATILE volatile
53 extern void (*__MALLOC_HOOK_VOLATILE __after_morecore_hook
) (void);
54 extern void (*__MALLOC_HOOK_VOLATILE __malloc_initialize_hook
) (void);
55 extern void *(*__morecore
) (ptrdiff_t);
58 /* If HYBRID_MALLOC is defined, then temacs will use malloc,
59 realloc... as defined in this file (and renamed gmalloc,
60 grealloc... via the macros that follow). The dumped emacs,
61 however, will use the system malloc, realloc.... In other source
62 files, malloc, realloc... are renamed hybrid_malloc,
63 hybrid_realloc... via macros in conf_post.h. hybrid_malloc and
64 friends are wrapper functions defined later in this file. */
70 #define malloc gmalloc
71 #define realloc grealloc
72 #define calloc gcalloc
73 #define aligned_alloc galigned_alloc
75 #define malloc_info gmalloc_info
79 # define DUMPED bss_sbrk_did_unexec
91 /* Allocate SIZE bytes of memory. */
92 extern void *malloc (size_t size
) ATTRIBUTE_MALLOC_SIZE ((1));
93 /* Re-allocate the previously allocated block
94 in ptr, making the new block SIZE bytes long. */
95 extern void *realloc (void *ptr
, size_t size
) ATTRIBUTE_ALLOC_SIZE ((2));
96 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
97 extern void *calloc (size_t nmemb
, size_t size
) ATTRIBUTE_MALLOC_SIZE ((1,2));
99 extern void free (void *ptr
);
101 /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
102 extern void *aligned_alloc (size_t, size_t);
104 extern void *memalign (size_t, size_t);
105 extern int posix_memalign (void **, size_t, size_t);
108 /* The allocator divides the heap into blocks of fixed size; large
109 requests receive one or more whole blocks, and small requests
110 receive a fragment of a block. Fragment sizes are powers of two,
111 and all fragments of a block are the same size. When all the
112 fragments in a block have been freed, the block itself is freed. */
113 #define BLOCKLOG (INT_WIDTH > 16 ? 12 : 9)
114 #define BLOCKSIZE (1 << BLOCKLOG)
115 #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
117 /* Determine the amount of memory spanned by the initial heap table
118 (not an absolute limit). */
119 #define HEAP (INT_WIDTH > 16 ? 4194304 : 65536)
121 /* Number of contiguous free blocks allowed to build up at the end of
122 memory before they will be returned to the system. */
123 #define FINAL_FREE_BLOCKS 8
125 /* Data structure giving per-block information. */
128 /* Heap information for a busy block. */
131 /* Zero for a block that is not one of ours (typically,
132 allocated by system malloc), positive for the log base 2 of
133 the fragment size of a fragmented block, -1 for the first
134 block of a multiblock object, and unspecified for later
135 blocks of that object. Type-0 blocks can be present
136 because the system malloc can be invoked by library
137 functions in an undumped Emacs. */
143 size_t nfree
; /* Free frags in a fragmented block. */
144 size_t first
; /* First free fragment of the block. */
146 /* For a large object, in its first block, this has the number
147 of blocks in the object. */
151 /* Heap information for a free block
152 (that may be the first of a free cluster). */
155 size_t size
; /* Size (in blocks) of a free cluster. */
156 size_t next
; /* Index of next free cluster. */
157 size_t prev
; /* Index of previous free cluster. */
161 /* Pointer to first block of the heap. */
162 extern char *_heapbase
;
164 /* Table indexed by block number giving per-block information. */
165 extern malloc_info
*_heapinfo
;
167 /* Address to block number and vice versa. */
168 #define BLOCK(A) ((size_t) ((char *) (A) - _heapbase) / BLOCKSIZE + 1)
169 #define ADDRESS(B) ((void *) (((B) - 1) * BLOCKSIZE + _heapbase))
171 /* Current search index for the heap table. */
172 extern size_t _heapindex
;
174 /* Limit of valid info table indices. */
175 extern size_t _heaplimit
;
177 /* Doubly linked lists of free fragments. */
184 /* Free list headers for each fragment size. */
185 extern struct list _fraghead
[];
187 /* List of blocks allocated with aligned_alloc and friends. */
190 struct alignlist
*next
;
191 void *aligned
; /* The address that aligned_alloc returned. */
192 void *exact
; /* The address that malloc returned. */
194 extern struct alignlist
*_aligned_blocks
;
196 /* Instrumentation. */
197 extern size_t _chunks_used
;
198 extern size_t _bytes_used
;
199 extern size_t _chunks_free
;
200 extern size_t _bytes_free
;
202 /* Internal versions of `malloc', `realloc', and `free'
203 used when these functions need to call each other.
204 They are the same but don't call the hooks. */
205 extern void *_malloc_internal (size_t);
206 extern void *_realloc_internal (void *, size_t);
207 extern void _free_internal (void *);
208 extern void *_malloc_internal_nolock (size_t);
209 extern void *_realloc_internal_nolock (void *, size_t);
210 extern void _free_internal_nolock (void *);
213 extern pthread_mutex_t _malloc_mutex
, _aligned_blocks_mutex
;
214 extern int _malloc_thread_enabled_p
;
217 if (_malloc_thread_enabled_p) \
218 pthread_mutex_lock (&_malloc_mutex); \
222 if (_malloc_thread_enabled_p) \
223 pthread_mutex_unlock (&_malloc_mutex); \
225 #define LOCK_ALIGNED_BLOCKS() \
227 if (_malloc_thread_enabled_p) \
228 pthread_mutex_lock (&_aligned_blocks_mutex); \
230 #define UNLOCK_ALIGNED_BLOCKS() \
232 if (_malloc_thread_enabled_p) \
233 pthread_mutex_unlock (&_aligned_blocks_mutex); \
238 #define LOCK_ALIGNED_BLOCKS()
239 #define UNLOCK_ALIGNED_BLOCKS()
242 /* Nonzero if `malloc' has been called and done its initialization. */
243 extern int __malloc_initialized
;
244 /* Function called to initialize malloc data structures. */
245 extern int __malloc_initialize (void);
249 /* Return values for `mprobe': these are the kinds of inconsistencies that
250 `mcheck' enables detection of. */
253 MCHECK_DISABLED
= -1, /* Consistency checking is not turned on. */
254 MCHECK_OK
, /* Block is fine. */
255 MCHECK_FREE
, /* Block freed twice. */
256 MCHECK_HEAD
, /* Memory before the block was clobbered. */
257 MCHECK_TAIL
/* Memory after the block was clobbered. */
260 /* Activate a standard collection of debugging hooks. This must be called
261 before `malloc' is ever called. ABORTFUNC is called with an error code
262 (see enum above) when an inconsistency is detected. If ABORTFUNC is
263 null, the standard function prints on stderr and then calls `abort'. */
264 extern int mcheck (void (*abortfunc
) (enum mcheck_status
));
266 /* Check for aberrations in a particular malloc'd block. You must have
267 called `mcheck' already. These are the same checks that `mcheck' does
268 when you free or reallocate a block. */
269 extern enum mcheck_status
mprobe (void *ptr
);
271 /* Activate a standard collection of tracing hooks. */
272 extern void mtrace (void);
273 extern void muntrace (void);
275 /* Statistics available to the user. */
278 size_t bytes_total
; /* Total size of the heap. */
279 size_t chunks_used
; /* Chunks allocated by the user. */
280 size_t bytes_used
; /* Byte total of user-allocated chunks. */
281 size_t chunks_free
; /* Chunks in the free list. */
282 size_t bytes_free
; /* Byte total of chunks in the free list. */
285 /* Pick up the current statistics. */
286 extern struct mstats
mstats (void);
296 /* Memory allocator `malloc'.
297 Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
298 Written May 1989 by Mike Haertel.
300 This library is free software; you can redistribute it and/or
301 modify it under the terms of the GNU General Public License as
302 published by the Free Software Foundation; either version 2 of the
303 License, or (at your option) any later version.
305 This library is distributed in the hope that it will be useful,
306 but WITHOUT ANY WARRANTY; without even the implied warranty of
307 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
308 General Public License for more details.
310 You should have received a copy of the GNU General Public
311 License along with this library. If not, see <http://www.gnu.org/licenses/>.
313 The author may be reached (Email) at the address mike@ai.mit.edu,
314 or (US mail) as Mike Haertel c/o Free Software Foundation. */
318 /* Debugging hook for 'malloc'. */
319 static void *(*__MALLOC_HOOK_VOLATILE gmalloc_hook
) (size_t);
321 /* Replacements for traditional glibc malloc hooks, for platforms that
322 do not already have these hooks. Platforms with these hooks all
323 used relaxed ref/def, so it is OK to define them here too. */
324 void (*__MALLOC_HOOK_VOLATILE __malloc_initialize_hook
) (void);
325 void (*__MALLOC_HOOK_VOLATILE __after_morecore_hook
) (void);
326 void *(*__morecore
) (ptrdiff_t);
328 #ifndef HYBRID_MALLOC
330 /* Pointer to the base of the first block. */
333 /* Block information table. Allocated with align/__free (not malloc/free). */
334 malloc_info
*_heapinfo
;
336 /* Search index in the info table. */
339 /* Limit of valid info table indices. */
342 /* Free lists for each fragment size. */
343 struct list _fraghead
[BLOCKLOG
];
345 /* Instrumentation. */
351 /* Are you experienced? */
352 int __malloc_initialized
;
356 static struct list _fraghead
[BLOCKLOG
];
358 #endif /* HYBRID_MALLOC */
360 /* Number of extra blocks to get each time we ask for more core.
361 This reduces the frequency of calling `(*__morecore)'. */
362 #if defined DOUG_LEA_MALLOC || defined HYBRID_MALLOC || defined SYSTEM_MALLOC
365 size_t __malloc_extra_blocks
;
367 /* Number of info entries. */
368 static size_t heapsize
;
370 #if defined GC_MALLOC_CHECK && defined GC_PROTECT_MALLOC_STATE
372 /* Some code for hunting a bug writing into _heapinfo.
374 Call this macro with argument PROT non-zero to protect internal
375 malloc state against writing to it, call it with a zero argument to
376 make it readable and writable.
378 Note that this only works if BLOCKSIZE == page size, which is
379 the case on the i386. */
381 #include <sys/types.h>
382 #include <sys/mman.h>
384 static int state_protected_p
;
385 static size_t last_state_size
;
386 static malloc_info
*last_heapinfo
;
389 protect_malloc_state (int protect_p
)
391 /* If _heapinfo has been relocated, make sure its old location
392 isn't left read-only; it will be reused by malloc. */
393 if (_heapinfo
!= last_heapinfo
395 && state_protected_p
)
396 mprotect (last_heapinfo
, last_state_size
, PROT_READ
| PROT_WRITE
);
398 last_state_size
= _heaplimit
* sizeof *_heapinfo
;
399 last_heapinfo
= _heapinfo
;
401 if (protect_p
!= state_protected_p
)
403 state_protected_p
= protect_p
;
404 if (mprotect (_heapinfo
, last_state_size
,
405 protect_p
? PROT_READ
: PROT_READ
| PROT_WRITE
) != 0)
410 #define PROTECT_MALLOC_STATE(PROT) protect_malloc_state (PROT)
413 #define PROTECT_MALLOC_STATE(PROT) /* empty */
417 /* Aligned allocation. */
424 /* align accepts an unsigned argument, but __morecore accepts a
425 signed one. This could lead to trouble if SIZE overflows the
426 ptrdiff_t type accepted by __morecore. We just punt in that
427 case, since they are requesting a ludicrous amount anyway. */
428 if (PTRDIFF_MAX
< size
)
431 result
= (*__morecore
) (size
);
432 adj
= (uintptr_t) result
% BLOCKSIZE
;
435 adj
= BLOCKSIZE
- adj
;
437 result
= (char *) result
+ adj
;
440 if (__after_morecore_hook
)
441 (*__after_morecore_hook
) ();
446 /* Get SIZE bytes, if we can get them starting at END.
447 Return the address of the space we got.
448 If we cannot get space at END, fail and return 0. */
450 get_contiguous_space (ptrdiff_t size
, void *position
)
455 before
= (*__morecore
) (0);
456 /* If we can tell in advance that the break is at the wrong place,
458 if (before
!= position
)
461 /* Allocate SIZE bytes and get the address of them. */
462 after
= (*__morecore
) (size
);
466 /* It was not contiguous--reject it. */
467 if (after
!= position
)
469 (*__morecore
) (- size
);
477 /* This is called when `_heapinfo' and `heapsize' have just
478 been set to describe a new info table. Set up the table
479 to describe itself and account for it in the statistics. */
481 register_heapinfo (void)
483 size_t block
, blocks
;
485 block
= BLOCK (_heapinfo
);
486 blocks
= BLOCKIFY (heapsize
* sizeof (malloc_info
));
488 /* Account for the _heapinfo block itself in the statistics. */
489 _bytes_used
+= blocks
* BLOCKSIZE
;
492 /* Describe the heapinfo block itself in the heapinfo. */
493 _heapinfo
[block
].busy
.type
= -1;
494 _heapinfo
[block
].busy
.info
.size
= blocks
;
498 pthread_mutex_t _malloc_mutex
= PTHREAD_MUTEX_INITIALIZER
;
499 pthread_mutex_t _aligned_blocks_mutex
= PTHREAD_MUTEX_INITIALIZER
;
500 int _malloc_thread_enabled_p
;
503 malloc_atfork_handler_prepare (void)
506 LOCK_ALIGNED_BLOCKS ();
510 malloc_atfork_handler_parent (void)
512 UNLOCK_ALIGNED_BLOCKS ();
517 malloc_atfork_handler_child (void)
519 UNLOCK_ALIGNED_BLOCKS ();
523 /* Set up mutexes and make malloc etc. thread-safe. */
525 malloc_enable_thread (void)
527 if (_malloc_thread_enabled_p
)
530 /* Some pthread implementations call malloc for statically
531 initialized mutexes when they are used first. To avoid such a
532 situation, we initialize mutexes here while their use is
533 disabled in malloc etc. */
534 pthread_mutex_init (&_malloc_mutex
, NULL
);
535 pthread_mutex_init (&_aligned_blocks_mutex
, NULL
);
536 pthread_atfork (malloc_atfork_handler_prepare
,
537 malloc_atfork_handler_parent
,
538 malloc_atfork_handler_child
);
539 _malloc_thread_enabled_p
= 1;
541 #endif /* USE_PTHREAD */
544 malloc_initialize_1 (void)
550 if (__malloc_initialize_hook
)
551 (*__malloc_initialize_hook
) ();
553 heapsize
= HEAP
/ BLOCKSIZE
;
554 _heapinfo
= align (heapsize
* sizeof (malloc_info
));
555 if (_heapinfo
== NULL
)
557 memset (_heapinfo
, 0, heapsize
* sizeof (malloc_info
));
558 _heapinfo
[0].free
.size
= 0;
559 _heapinfo
[0].free
.next
= _heapinfo
[0].free
.prev
= 0;
561 _heapbase
= (char *) _heapinfo
;
562 _heaplimit
= BLOCK (_heapbase
+ heapsize
* sizeof (malloc_info
));
564 register_heapinfo ();
566 __malloc_initialized
= 1;
567 PROTECT_MALLOC_STATE (1);
571 /* Set everything up and remember that we have.
572 main will call malloc which calls this function. That is before any threads
573 or signal handlers has been set up, so we don't need thread protection. */
575 __malloc_initialize (void)
577 if (__malloc_initialized
)
580 malloc_initialize_1 ();
582 return __malloc_initialized
;
585 static int morecore_recursing
;
587 /* Get neatly aligned memory, initializing or
588 growing the heap info table as necessary. */
590 morecore_nolock (size_t size
)
593 malloc_info
*newinfo
, *oldinfo
;
596 if (morecore_recursing
)
597 /* Avoid recursion. The caller will know how to handle a null return. */
600 result
= align (size
);
604 PROTECT_MALLOC_STATE (0);
606 /* Check if we need to grow the info table. */
607 if (heapsize
< BLOCK ((char *) result
+ size
))
609 /* Calculate the new _heapinfo table size. We do not account for the
610 added blocks in the table itself, as we hope to place them in
611 existing free space, which is already covered by part of the
616 while (newsize
< BLOCK ((char *) result
+ size
));
618 /* We must not reuse existing core for the new info table when called
619 from realloc in the case of growing a large block, because the
620 block being grown is momentarily marked as free. In this case
621 _heaplimit is zero so we know not to reuse space for internal
625 /* First try to allocate the new info table in core we already
626 have, in the usual way using realloc. If realloc cannot
627 extend it in place or relocate it to existing sufficient core,
628 we will get called again, and the code above will notice the
629 `morecore_recursing' flag and return null. */
630 int save
= errno
; /* Don't want to clobber errno with ENOMEM. */
631 morecore_recursing
= 1;
632 newinfo
= _realloc_internal_nolock (_heapinfo
,
633 newsize
* sizeof (malloc_info
));
634 morecore_recursing
= 0;
639 /* We found some space in core, and realloc has put the old
640 table's blocks on the free list. Now zero the new part
641 of the table and install the new table location. */
642 memset (&newinfo
[heapsize
], 0,
643 (newsize
- heapsize
) * sizeof (malloc_info
));
650 /* Allocate new space for the malloc info table. */
653 newinfo
= align (newsize
* sizeof (malloc_info
));
658 (*__morecore
) (-size
);
662 /* Is it big enough to record status for its own space?
664 if (BLOCK ((char *) newinfo
+ newsize
* sizeof (malloc_info
))
668 /* Must try again. First give back most of what we just got. */
669 (*__morecore
) (- newsize
* sizeof (malloc_info
));
673 /* Copy the old table to the beginning of the new,
674 and zero the rest of the new table. */
675 memcpy (newinfo
, _heapinfo
, heapsize
* sizeof (malloc_info
));
676 memset (&newinfo
[heapsize
], 0,
677 (newsize
- heapsize
) * sizeof (malloc_info
));
682 register_heapinfo ();
684 /* Reset _heaplimit so _free_internal never decides
685 it can relocate or resize the info table. */
687 _free_internal_nolock (oldinfo
);
688 PROTECT_MALLOC_STATE (0);
690 /* The new heap limit includes the new table just allocated. */
691 _heaplimit
= BLOCK ((char *) newinfo
+ heapsize
* sizeof (malloc_info
));
696 _heaplimit
= BLOCK ((char *) result
+ size
);
700 /* Allocate memory from the heap. */
702 _malloc_internal_nolock (size_t size
)
705 size_t block
, blocks
, lastblocks
, start
;
709 /* ANSI C allows `malloc (0)' to either return NULL, or to return a
710 valid address you can realloc and free (though not dereference).
712 It turns out that some extant code (sunrpc, at least Ultrix's version)
713 expects `malloc (0)' to return non-NULL and breaks otherwise.
721 PROTECT_MALLOC_STATE (0);
723 if (size
< sizeof (struct list
))
724 size
= sizeof (struct list
);
726 /* Determine the allocation policy based on the request size. */
727 if (size
<= BLOCKSIZE
/ 2)
729 /* Small allocation to receive a fragment of a block.
730 Determine the logarithm to base two of the fragment size. */
731 register size_t log
= 1;
733 while ((size
/= 2) != 0)
736 /* Look in the fragment lists for a
737 free fragment of the desired size. */
738 next
= _fraghead
[log
].next
;
741 /* There are free fragments of this size.
742 Pop a fragment out of the fragment list and return it.
743 Update the block's nfree and first counters. */
745 next
->prev
->next
= next
->next
;
746 if (next
->next
!= NULL
)
747 next
->next
->prev
= next
->prev
;
748 block
= BLOCK (result
);
749 if (--_heapinfo
[block
].busy
.info
.frag
.nfree
!= 0)
750 _heapinfo
[block
].busy
.info
.frag
.first
=
751 (uintptr_t) next
->next
% BLOCKSIZE
>> log
;
753 /* Update the statistics. */
755 _bytes_used
+= 1 << log
;
757 _bytes_free
-= 1 << log
;
761 /* No free fragments of the desired size, so get a new block
762 and break it into fragments, returning the first. */
763 #ifdef GC_MALLOC_CHECK
764 result
= _malloc_internal_nolock (BLOCKSIZE
);
765 PROTECT_MALLOC_STATE (0);
766 #elif defined (USE_PTHREAD)
767 result
= _malloc_internal_nolock (BLOCKSIZE
);
769 result
= malloc (BLOCKSIZE
);
773 PROTECT_MALLOC_STATE (1);
777 /* Link all fragments but the first into the free list. */
778 next
= (struct list
*) ((char *) result
+ (1 << log
));
780 next
->prev
= &_fraghead
[log
];
781 _fraghead
[log
].next
= next
;
783 for (i
= 2; i
< (size_t) (BLOCKSIZE
>> log
); ++i
)
785 next
= (struct list
*) ((char *) result
+ (i
<< log
));
786 next
->next
= _fraghead
[log
].next
;
787 next
->prev
= &_fraghead
[log
];
788 next
->prev
->next
= next
;
789 next
->next
->prev
= next
;
792 /* Initialize the nfree and first counters for this block. */
793 block
= BLOCK (result
);
794 _heapinfo
[block
].busy
.type
= log
;
795 _heapinfo
[block
].busy
.info
.frag
.nfree
= i
- 1;
796 _heapinfo
[block
].busy
.info
.frag
.first
= i
- 1;
798 _chunks_free
+= (BLOCKSIZE
>> log
) - 1;
799 _bytes_free
+= BLOCKSIZE
- (1 << log
);
800 _bytes_used
-= BLOCKSIZE
- (1 << log
);
805 /* Large allocation to receive one or more blocks.
806 Search the free list in a circle starting at the last place visited.
807 If we loop completely around without finding a large enough
808 space we will have to get more memory from the system. */
809 blocks
= BLOCKIFY (size
);
810 start
= block
= _heapindex
;
811 while (_heapinfo
[block
].free
.size
< blocks
)
813 block
= _heapinfo
[block
].free
.next
;
816 /* Need to get more from the system. Get a little extra. */
817 size_t wantblocks
= blocks
+ __malloc_extra_blocks
;
818 block
= _heapinfo
[0].free
.prev
;
819 lastblocks
= _heapinfo
[block
].free
.size
;
820 /* Check to see if the new core will be contiguous with the
821 final free block; if so we don't need to get as much. */
822 if (_heaplimit
!= 0 && block
+ lastblocks
== _heaplimit
&&
823 /* We can't do this if we will have to make the heap info
824 table bigger to accommodate the new space. */
825 block
+ wantblocks
<= heapsize
&&
826 get_contiguous_space ((wantblocks
- lastblocks
) * BLOCKSIZE
,
827 ADDRESS (block
+ lastblocks
)))
829 /* We got it contiguously. Which block we are extending
830 (the `final free block' referred to above) might have
831 changed, if it got combined with a freed info table. */
832 block
= _heapinfo
[0].free
.prev
;
833 _heapinfo
[block
].free
.size
+= (wantblocks
- lastblocks
);
834 _bytes_free
+= (wantblocks
- lastblocks
) * BLOCKSIZE
;
835 _heaplimit
+= wantblocks
- lastblocks
;
838 result
= morecore_nolock (wantblocks
* BLOCKSIZE
);
841 block
= BLOCK (result
);
842 /* Put the new block at the end of the free list. */
843 _heapinfo
[block
].free
.size
= wantblocks
;
844 _heapinfo
[block
].free
.prev
= _heapinfo
[0].free
.prev
;
845 _heapinfo
[block
].free
.next
= 0;
846 _heapinfo
[0].free
.prev
= block
;
847 _heapinfo
[_heapinfo
[block
].free
.prev
].free
.next
= block
;
849 /* Now loop to use some of that block for this allocation. */
853 /* At this point we have found a suitable free list entry.
854 Figure out how to remove what we need from the list. */
855 result
= ADDRESS (block
);
856 if (_heapinfo
[block
].free
.size
> blocks
)
858 /* The block we found has a bit left over,
859 so relink the tail end back into the free list. */
860 _heapinfo
[block
+ blocks
].free
.size
861 = _heapinfo
[block
].free
.size
- blocks
;
862 _heapinfo
[block
+ blocks
].free
.next
863 = _heapinfo
[block
].free
.next
;
864 _heapinfo
[block
+ blocks
].free
.prev
865 = _heapinfo
[block
].free
.prev
;
866 _heapinfo
[_heapinfo
[block
].free
.prev
].free
.next
867 = _heapinfo
[_heapinfo
[block
].free
.next
].free
.prev
868 = _heapindex
= block
+ blocks
;
872 /* The block exactly matches our requirements,
873 so just remove it from the list. */
874 _heapinfo
[_heapinfo
[block
].free
.next
].free
.prev
875 = _heapinfo
[block
].free
.prev
;
876 _heapinfo
[_heapinfo
[block
].free
.prev
].free
.next
877 = _heapindex
= _heapinfo
[block
].free
.next
;
881 _heapinfo
[block
].busy
.type
= -1;
882 _heapinfo
[block
].busy
.info
.size
= blocks
;
884 _bytes_used
+= blocks
* BLOCKSIZE
;
885 _bytes_free
-= blocks
* BLOCKSIZE
;
888 PROTECT_MALLOC_STATE (1);
894 _malloc_internal (size_t size
)
899 result
= _malloc_internal_nolock (size
);
908 void *(*hook
) (size_t);
910 if (!__malloc_initialized
&& !__malloc_initialize ())
913 /* Copy the value of gmalloc_hook to an automatic variable in case
914 gmalloc_hook is modified in another thread between its
915 NULL-check and the use.
917 Note: Strictly speaking, this is not a right solution. We should
918 use mutexes to access non-read-only variables that are shared
919 among multiple threads. We just leave it for compatibility with
920 glibc malloc (i.e., assignments to gmalloc_hook) for now. */
922 return (hook
!= NULL
? *hook
: _malloc_internal
) (size
);
925 #if !(defined (_LIBC) || defined (HYBRID_MALLOC))
927 /* On some ANSI C systems, some libc functions call _malloc, _free
928 and _realloc. Make them use the GNU functions. */
930 extern void *_malloc (size_t);
931 extern void _free (void *);
932 extern void *_realloc (void *, size_t);
935 _malloc (size_t size
)
937 return malloc (size
);
947 _realloc (void *ptr
, size_t size
)
949 return realloc (ptr
, size
);
953 /* Free a block of memory allocated by `malloc'.
954 Copyright 1990, 1991, 1992, 1994, 1995 Free Software Foundation, Inc.
955 Written May 1989 by Mike Haertel.
957 This library is free software; you can redistribute it and/or
958 modify it under the terms of the GNU General Public License as
959 published by the Free Software Foundation; either version 2 of the
960 License, or (at your option) any later version.
962 This library is distributed in the hope that it will be useful,
963 but WITHOUT ANY WARRANTY; without even the implied warranty of
964 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
965 General Public License for more details.
967 You should have received a copy of the GNU General Public
968 License along with this library. If not, see <http://www.gnu.org/licenses/>.
970 The author may be reached (Email) at the address mike@ai.mit.edu,
971 or (US mail) as Mike Haertel c/o Free Software Foundation. */
973 /* Debugging hook for free. */
974 static void (*__MALLOC_HOOK_VOLATILE gfree_hook
) (void *);
976 #ifndef HYBRID_MALLOC
978 /* List of blocks allocated by aligned_alloc. */
979 struct alignlist
*_aligned_blocks
= NULL
;
982 /* Return memory to the heap.
983 Like `_free_internal' but don't lock mutex. */
985 _free_internal_nolock (void *ptr
)
988 size_t block
, blocks
;
990 struct list
*prev
, *next
;
992 const size_t lesscore_threshold
993 /* Threshold of free space at which we will return some to the system. */
994 = FINAL_FREE_BLOCKS
+ 2 * __malloc_extra_blocks
;
996 register struct alignlist
*l
;
1001 PROTECT_MALLOC_STATE (0);
1003 LOCK_ALIGNED_BLOCKS ();
1004 for (l
= _aligned_blocks
; l
!= NULL
; l
= l
->next
)
1005 if (l
->aligned
== ptr
)
1007 l
->aligned
= NULL
; /* Mark the slot in the list as free. */
1011 UNLOCK_ALIGNED_BLOCKS ();
1013 block
= BLOCK (ptr
);
1015 type
= _heapinfo
[block
].busy
.type
;
1019 /* Get as many statistics as early as we can. */
1021 _bytes_used
-= _heapinfo
[block
].busy
.info
.size
* BLOCKSIZE
;
1022 _bytes_free
+= _heapinfo
[block
].busy
.info
.size
* BLOCKSIZE
;
1024 /* Find the free cluster previous to this one in the free list.
1025 Start searching at the last block referenced; this may benefit
1026 programs with locality of allocation. */
1030 i
= _heapinfo
[i
].free
.prev
;
1034 i
= _heapinfo
[i
].free
.next
;
1035 while (i
> 0 && i
< block
);
1036 i
= _heapinfo
[i
].free
.prev
;
1039 /* Determine how to link this block into the free list. */
1040 if (block
== i
+ _heapinfo
[i
].free
.size
)
1042 /* Coalesce this block with its predecessor. */
1043 _heapinfo
[i
].free
.size
+= _heapinfo
[block
].busy
.info
.size
;
1048 /* Really link this block back into the free list. */
1049 _heapinfo
[block
].free
.size
= _heapinfo
[block
].busy
.info
.size
;
1050 _heapinfo
[block
].free
.next
= _heapinfo
[i
].free
.next
;
1051 _heapinfo
[block
].free
.prev
= i
;
1052 _heapinfo
[i
].free
.next
= block
;
1053 _heapinfo
[_heapinfo
[block
].free
.next
].free
.prev
= block
;
1057 /* Now that the block is linked in, see if we can coalesce it
1058 with its successor (by deleting its successor from the list
1059 and adding in its size). */
1060 if (block
+ _heapinfo
[block
].free
.size
== _heapinfo
[block
].free
.next
)
1062 _heapinfo
[block
].free
.size
1063 += _heapinfo
[_heapinfo
[block
].free
.next
].free
.size
;
1064 _heapinfo
[block
].free
.next
1065 = _heapinfo
[_heapinfo
[block
].free
.next
].free
.next
;
1066 _heapinfo
[_heapinfo
[block
].free
.next
].free
.prev
= block
;
1070 /* How many trailing free blocks are there now? */
1071 blocks
= _heapinfo
[block
].free
.size
;
1073 /* Where is the current end of accessible core? */
1074 curbrk
= (*__morecore
) (0);
1076 if (_heaplimit
!= 0 && curbrk
== ADDRESS (_heaplimit
))
1078 /* The end of the malloc heap is at the end of accessible core.
1079 It's possible that moving _heapinfo will allow us to
1080 return some space to the system. */
1082 size_t info_block
= BLOCK (_heapinfo
);
1083 size_t info_blocks
= _heapinfo
[info_block
].busy
.info
.size
;
1084 size_t prev_block
= _heapinfo
[block
].free
.prev
;
1085 size_t prev_blocks
= _heapinfo
[prev_block
].free
.size
;
1086 size_t next_block
= _heapinfo
[block
].free
.next
;
1087 size_t next_blocks
= _heapinfo
[next_block
].free
.size
;
1089 if (/* Win if this block being freed is last in core, the info table
1090 is just before it, the previous free block is just before the
1091 info table, and the two free blocks together form a useful
1092 amount to return to the system. */
1093 (block
+ blocks
== _heaplimit
&&
1094 info_block
+ info_blocks
== block
&&
1095 prev_block
!= 0 && prev_block
+ prev_blocks
== info_block
&&
1096 blocks
+ prev_blocks
>= lesscore_threshold
) ||
1097 /* Nope, not the case. We can also win if this block being
1098 freed is just before the info table, and the table extends
1099 to the end of core or is followed only by a free block,
1100 and the total free space is worth returning to the system. */
1101 (block
+ blocks
== info_block
&&
1102 ((info_block
+ info_blocks
== _heaplimit
&&
1103 blocks
>= lesscore_threshold
) ||
1104 (info_block
+ info_blocks
== next_block
&&
1105 next_block
+ next_blocks
== _heaplimit
&&
1106 blocks
+ next_blocks
>= lesscore_threshold
)))
1109 malloc_info
*newinfo
;
1110 size_t oldlimit
= _heaplimit
;
1112 /* Free the old info table, clearing _heaplimit to avoid
1113 recursion into this code. We don't want to return the
1114 table's blocks to the system before we have copied them to
1115 the new location. */
1117 _free_internal_nolock (_heapinfo
);
1118 _heaplimit
= oldlimit
;
1120 /* Tell malloc to search from the beginning of the heap for
1121 free blocks, so it doesn't reuse the ones just freed. */
1124 /* Allocate new space for the info table and move its data. */
1125 newinfo
= _malloc_internal_nolock (info_blocks
* BLOCKSIZE
);
1126 PROTECT_MALLOC_STATE (0);
1127 memmove (newinfo
, _heapinfo
, info_blocks
* BLOCKSIZE
);
1128 _heapinfo
= newinfo
;
1130 /* We should now have coalesced the free block with the
1131 blocks freed from the old info table. Examine the entire
1132 trailing free block to decide below whether to return some
1134 block
= _heapinfo
[0].free
.prev
;
1135 blocks
= _heapinfo
[block
].free
.size
;
1138 /* Now see if we can return stuff to the system. */
1139 if (block
+ blocks
== _heaplimit
&& blocks
>= lesscore_threshold
)
1141 register size_t bytes
= blocks
* BLOCKSIZE
;
1142 _heaplimit
-= blocks
;
1143 (*__morecore
) (-bytes
);
1144 _heapinfo
[_heapinfo
[block
].free
.prev
].free
.next
1145 = _heapinfo
[block
].free
.next
;
1146 _heapinfo
[_heapinfo
[block
].free
.next
].free
.prev
1147 = _heapinfo
[block
].free
.prev
;
1148 block
= _heapinfo
[block
].free
.prev
;
1150 _bytes_free
-= bytes
;
1154 /* Set the next search to begin at this block. */
1159 /* Do some of the statistics. */
1161 _bytes_used
-= 1 << type
;
1163 _bytes_free
+= 1 << type
;
1165 /* Get the address of the first free fragment in this block. */
1166 prev
= (struct list
*) ((char *) ADDRESS (block
) +
1167 (_heapinfo
[block
].busy
.info
.frag
.first
<< type
));
1169 if (_heapinfo
[block
].busy
.info
.frag
.nfree
== (BLOCKSIZE
>> type
) - 1)
1171 /* If all fragments of this block are free, remove them
1172 from the fragment list and free the whole block. */
1174 for (i
= 1; i
< (size_t) (BLOCKSIZE
>> type
); ++i
)
1176 prev
->prev
->next
= next
;
1178 next
->prev
= prev
->prev
;
1179 _heapinfo
[block
].busy
.type
= -1;
1180 _heapinfo
[block
].busy
.info
.size
= 1;
1182 /* Keep the statistics accurate. */
1184 _bytes_used
+= BLOCKSIZE
;
1185 _chunks_free
-= BLOCKSIZE
>> type
;
1186 _bytes_free
-= BLOCKSIZE
;
1188 #if defined (GC_MALLOC_CHECK) || defined (USE_PTHREAD)
1189 _free_internal_nolock (ADDRESS (block
));
1191 free (ADDRESS (block
));
1194 else if (_heapinfo
[block
].busy
.info
.frag
.nfree
!= 0)
1196 /* If some fragments of this block are free, link this
1197 fragment into the fragment list after the first free
1198 fragment of this block. */
1200 next
->next
= prev
->next
;
1203 if (next
->next
!= NULL
)
1204 next
->next
->prev
= next
;
1205 ++_heapinfo
[block
].busy
.info
.frag
.nfree
;
1209 /* No fragments of this block are free, so link this
1210 fragment into the fragment list and announce that
1211 it is the first free fragment of this block. */
1213 _heapinfo
[block
].busy
.info
.frag
.nfree
= 1;
1214 _heapinfo
[block
].busy
.info
.frag
.first
=
1215 (uintptr_t) ptr
% BLOCKSIZE
>> type
;
1216 prev
->next
= _fraghead
[type
].next
;
1217 prev
->prev
= &_fraghead
[type
];
1218 prev
->prev
->next
= prev
;
1219 if (prev
->next
!= NULL
)
1220 prev
->next
->prev
= prev
;
1225 PROTECT_MALLOC_STATE (1);
1228 /* Return memory to the heap.
1229 Like 'free' but don't call a hook if there is one. */
1231 _free_internal (void *ptr
)
1234 _free_internal_nolock (ptr
);
1238 /* Return memory to the heap. */
1243 void (*hook
) (void *) = gfree_hook
;
1248 _free_internal (ptr
);
1251 #ifndef HYBRID_MALLOC
1252 /* Define the `cfree' alias for `free'. */
1254 weak_alias (free
, cfree
)
1263 /* Change the size of a block allocated by `malloc'.
1264 Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
1265 Written May 1989 by Mike Haertel.
1267 This library is free software; you can redistribute it and/or
1268 modify it under the terms of the GNU General Public License as
1269 published by the Free Software Foundation; either version 2 of the
1270 License, or (at your option) any later version.
1272 This library is distributed in the hope that it will be useful,
1273 but WITHOUT ANY WARRANTY; without even the implied warranty of
1274 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1275 General Public License for more details.
1277 You should have received a copy of the GNU General Public
1278 License along with this library. If not, see <http://www.gnu.org/licenses/>.
1280 The author may be reached (Email) at the address mike@ai.mit.edu,
1281 or (US mail) as Mike Haertel c/o Free Software Foundation. */
1284 #define min(a, b) ((a) < (b) ? (a) : (b))
1287 /* Debugging hook for realloc. */
1288 static void *(*grealloc_hook
) (void *, size_t);
1290 /* Resize the given region to the new size, returning a pointer
1291 to the (possibly moved) region. This is optimized for speed;
1292 some benchmarks seem to indicate that greater compactness is
1293 achieved by unconditionally allocating and copying to a
1294 new region. This module has incestuous knowledge of the
1295 internals of both free and malloc. */
1297 _realloc_internal_nolock (void *ptr
, size_t size
)
1301 size_t block
, blocks
, oldlimit
;
1305 _free_internal_nolock (ptr
);
1306 return _malloc_internal_nolock (0);
1308 else if (ptr
== NULL
)
1309 return _malloc_internal_nolock (size
);
1311 block
= BLOCK (ptr
);
1313 PROTECT_MALLOC_STATE (0);
1315 type
= _heapinfo
[block
].busy
.type
;
1319 /* Maybe reallocate a large block to a small fragment. */
1320 if (size
<= BLOCKSIZE
/ 2)
1322 result
= _malloc_internal_nolock (size
);
1325 memcpy (result
, ptr
, size
);
1326 _free_internal_nolock (ptr
);
1331 /* The new size is a large allocation as well;
1332 see if we can hold it in place. */
1333 blocks
= BLOCKIFY (size
);
1334 if (blocks
< _heapinfo
[block
].busy
.info
.size
)
1336 /* The new size is smaller; return
1337 excess memory to the free list. */
1338 _heapinfo
[block
+ blocks
].busy
.type
= -1;
1339 _heapinfo
[block
+ blocks
].busy
.info
.size
1340 = _heapinfo
[block
].busy
.info
.size
- blocks
;
1341 _heapinfo
[block
].busy
.info
.size
= blocks
;
1342 /* We have just created a new chunk by splitting a chunk in two.
1343 Now we will free this chunk; increment the statistics counter
1344 so it doesn't become wrong when _free_internal decrements it. */
1346 _free_internal_nolock (ADDRESS (block
+ blocks
));
1349 else if (blocks
== _heapinfo
[block
].busy
.info
.size
)
1350 /* No size change necessary. */
1354 /* Won't fit, so allocate a new region that will.
1355 Free the old region first in case there is sufficient
1356 adjacent free space to grow without moving. */
1357 blocks
= _heapinfo
[block
].busy
.info
.size
;
1358 /* Prevent free from actually returning memory to the system. */
1359 oldlimit
= _heaplimit
;
1361 _free_internal_nolock (ptr
);
1362 result
= _malloc_internal_nolock (size
);
1363 PROTECT_MALLOC_STATE (0);
1364 if (_heaplimit
== 0)
1365 _heaplimit
= oldlimit
;
1368 /* Now we're really in trouble. We have to unfree
1369 the thing we just freed. Unfortunately it might
1370 have been coalesced with its neighbors. */
1371 if (_heapindex
== block
)
1372 (void) _malloc_internal_nolock (blocks
* BLOCKSIZE
);
1376 = _malloc_internal_nolock ((block
- _heapindex
) * BLOCKSIZE
);
1377 (void) _malloc_internal_nolock (blocks
* BLOCKSIZE
);
1378 _free_internal_nolock (previous
);
1383 memmove (result
, ptr
, blocks
* BLOCKSIZE
);
1388 /* Old size is a fragment; type is logarithm
1389 to base two of the fragment size. */
1390 if (size
> (size_t) (1 << (type
- 1)) &&
1391 size
<= (size_t) (1 << type
))
1392 /* The new size is the same kind of fragment. */
1396 /* The new size is different; allocate a new space,
1397 and copy the lesser of the new size and the old. */
1398 result
= _malloc_internal_nolock (size
);
1401 memcpy (result
, ptr
, min (size
, (size_t) 1 << type
));
1402 _free_internal_nolock (ptr
);
1407 PROTECT_MALLOC_STATE (1);
1413 _realloc_internal (void *ptr
, size_t size
)
1418 result
= _realloc_internal_nolock (ptr
, size
);
1425 realloc (void *ptr
, size_t size
)
1427 void *(*hook
) (void *, size_t);
1429 if (!__malloc_initialized
&& !__malloc_initialize ())
1432 hook
= grealloc_hook
;
1433 return (hook
!= NULL
? *hook
: _realloc_internal
) (ptr
, size
);
1435 /* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
1437 This library is free software; you can redistribute it and/or
1438 modify it under the terms of the GNU General Public License as
1439 published by the Free Software Foundation; either version 2 of the
1440 License, or (at your option) any later version.
1442 This library is distributed in the hope that it will be useful,
1443 but WITHOUT ANY WARRANTY; without even the implied warranty of
1444 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1445 General Public License for more details.
1447 You should have received a copy of the GNU General Public
1448 License along with this library. If not, see <http://www.gnu.org/licenses/>.
1450 The author may be reached (Email) at the address mike@ai.mit.edu,
1451 or (US mail) as Mike Haertel c/o Free Software Foundation. */
1453 /* Allocate an array of NMEMB elements each SIZE bytes long.
1454 The entire array is initialized to zeros. */
1456 calloc (size_t nmemb
, size_t size
)
1459 size_t bytes
= nmemb
* size
;
1461 if (size
!= 0 && bytes
/ size
!= nmemb
)
1467 result
= malloc (bytes
);
1469 return memset (result
, 0, bytes
);
1472 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
1473 This file is part of the GNU C Library.
1475 The GNU C Library is free software; you can redistribute it and/or modify
1476 it under the terms of the GNU General Public License as published by
1477 the Free Software Foundation; either version 2, or (at your option)
1480 The GNU C Library is distributed in the hope that it will be useful,
1481 but WITHOUT ANY WARRANTY; without even the implied warranty of
1482 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1483 GNU General Public License for more details.
1485 You should have received a copy of the GNU General Public License
1486 along with the GNU C Library. If not, see <http://www.gnu.org/licenses/>. */
1488 /* uClibc defines __GNU_LIBRARY__, but it is not completely
1490 #if !defined (__GNU_LIBRARY__) || defined (__UCLIBC__)
1492 #else /* __GNU_LIBRARY__ && ! defined (__UCLIBC__) */
1493 /* It is best not to declare this and cast its result on foreign operating
1494 systems with potentially hostile include files. */
1496 extern void *__sbrk (ptrdiff_t increment
);
1497 #endif /* __GNU_LIBRARY__ && ! defined (__UCLIBC__) */
1499 /* Allocate INCREMENT more bytes of data space,
1500 and return the start of data space, or NULL on errors.
1501 If INCREMENT is negative, shrink data space. */
1503 gdefault_morecore (ptrdiff_t increment
)
1506 #ifdef HYBRID_MALLOC
1509 return bss_sbrk (increment
);
1512 result
= (void *) __sbrk (increment
);
1513 if (result
== (void *) -1)
1518 void *(*__morecore
) (ptrdiff_t) = gdefault_morecore
;
1520 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
1522 This library is free software; you can redistribute it and/or
1523 modify it under the terms of the GNU General Public License as
1524 published by the Free Software Foundation; either version 2 of the
1525 License, or (at your option) any later version.
1527 This library is distributed in the hope that it will be useful,
1528 but WITHOUT ANY WARRANTY; without even the implied warranty of
1529 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1530 General Public License for more details.
1532 You should have received a copy of the GNU General Public
1533 License along with this library. If not, see <http://www.gnu.org/licenses/>. */
1536 aligned_alloc (size_t alignment
, size_t size
)
1539 size_t adj
, lastadj
;
1541 /* Allocate a block with enough extra space to pad the block with up to
1542 (ALIGNMENT - 1) bytes if necessary. */
1543 if (- size
< alignment
)
1548 result
= malloc (size
+ alignment
- 1);
1552 /* Figure out how much we will need to pad this particular block
1553 to achieve the required alignment. */
1554 adj
= alignment
- (uintptr_t) result
% alignment
;
1555 if (adj
== alignment
)
1558 if (adj
!= alignment
- 1)
1562 /* Reallocate the block with only as much excess as it
1565 result
= malloc (size
+ adj
);
1566 if (result
== NULL
) /* Impossible unless interrupted. */
1570 adj
= alignment
- (uintptr_t) result
% alignment
;
1571 if (adj
== alignment
)
1573 /* It's conceivable we might have been so unlucky as to get
1574 a different block with weaker alignment. If so, this
1575 block is too short to contain SIZE after alignment
1576 correction. So we must try again and get another block,
1578 } while (adj
> lastadj
);
1583 /* Record this block in the list of aligned blocks, so that `free'
1584 can identify the pointer it is passed, which will be in the middle
1585 of an allocated block. */
1587 struct alignlist
*l
;
1588 LOCK_ALIGNED_BLOCKS ();
1589 for (l
= _aligned_blocks
; l
!= NULL
; l
= l
->next
)
1590 if (l
->aligned
== NULL
)
1591 /* This slot is free. Use it. */
1595 l
= malloc (sizeof *l
);
1598 l
->next
= _aligned_blocks
;
1599 _aligned_blocks
= l
;
1605 result
= l
->aligned
= (char *) result
+ adj
;
1607 UNLOCK_ALIGNED_BLOCKS ();
1618 /* Note that memalign and posix_memalign are not used in Emacs. */
1619 #ifndef HYBRID_MALLOC
1620 /* An obsolete alias for aligned_alloc, for any old libraries that use
1624 memalign (size_t alignment
, size_t size
)
1626 return aligned_alloc (alignment
, size
);
1629 /* If HYBRID_MALLOC is defined, we may want to use the system
1630 posix_memalign below. */
1632 posix_memalign (void **memptr
, size_t alignment
, size_t size
)
1637 || alignment
% sizeof (void *) != 0
1638 || (alignment
& (alignment
- 1)) != 0)
1641 mem
= aligned_alloc (alignment
, size
);
1651 /* Allocate memory on a page boundary.
1652 Copyright (C) 1991, 92, 93, 94, 96 Free Software Foundation, Inc.
1654 This library is free software; you can redistribute it and/or
1655 modify it under the terms of the GNU General Public License as
1656 published by the Free Software Foundation; either version 2 of the
1657 License, or (at your option) any later version.
1659 This library is distributed in the hope that it will be useful,
1660 but WITHOUT ANY WARRANTY; without even the implied warranty of
1661 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1662 General Public License for more details.
1664 You should have received a copy of the GNU General Public
1665 License along with this library. If not, see <http://www.gnu.org/licenses/>.
1667 The author may be reached (Email) at the address mike@ai.mit.edu,
1668 or (US mail) as Mike Haertel c/o Free Software Foundation. */
1670 #ifndef HYBRID_MALLOC
1672 # ifndef HAVE_MALLOC_H
1673 /* Allocate SIZE bytes on a page boundary. */
1674 extern void *valloc (size_t);
1677 # if defined _SC_PAGESIZE || !defined HAVE_GETPAGESIZE
1678 # include "getpagesize.h"
1679 # elif !defined getpagesize
1680 extern int getpagesize (void);
1683 static size_t pagesize
;
1686 valloc (size_t size
)
1689 pagesize
= getpagesize ();
1691 return aligned_alloc (pagesize
, size
);
1693 #endif /* HYBRID_MALLOC */
1698 #undef aligned_alloc
1701 #ifdef HYBRID_MALLOC
1702 /* Declare system malloc and friends. */
1703 extern void *malloc (size_t size
);
1704 extern void *realloc (void *ptr
, size_t size
);
1705 extern void *calloc (size_t nmemb
, size_t size
);
1706 extern void free (void *ptr
);
1707 #ifdef HAVE_ALIGNED_ALLOC
1708 extern void *aligned_alloc (size_t alignment
, size_t size
);
1709 #elif defined HAVE_POSIX_MEMALIGN
1710 extern int posix_memalign (void **memptr
, size_t alignment
, size_t size
);
1713 /* Assuming PTR was allocated via the hybrid malloc, return true if
1714 PTR was allocated via gmalloc, not the system malloc. Also, return
1715 true if _heaplimit is zero; this can happen temporarily when
1716 gmalloc calls itself for internal use, and in that case PTR is
1717 already known to be allocated via gmalloc. */
1720 allocated_via_gmalloc (void *ptr
)
1722 size_t block
= BLOCK (ptr
);
1723 size_t blockmax
= _heaplimit
- 1;
1724 return block
<= blockmax
&& _heapinfo
[block
].busy
.type
!= 0;
1727 /* See the comments near the beginning of this file for explanations
1728 of the following functions. */
1731 hybrid_malloc (size_t size
)
1734 return malloc (size
);
1735 return gmalloc (size
);
1739 hybrid_calloc (size_t nmemb
, size_t size
)
1742 return calloc (nmemb
, size
);
1743 return gcalloc (nmemb
, size
);
1747 hybrid_free (void *ptr
)
1749 if (allocated_via_gmalloc (ptr
))
1755 #if defined HAVE_ALIGNED_ALLOC || defined HAVE_POSIX_MEMALIGN
1757 hybrid_aligned_alloc (size_t alignment
, size_t size
)
1760 return galigned_alloc (alignment
, size
);
1761 /* The following is copied from alloc.c */
1762 #ifdef HAVE_ALIGNED_ALLOC
1763 return aligned_alloc (alignment
, size
);
1764 #else /* HAVE_POSIX_MEMALIGN */
1766 return posix_memalign (&p
, alignment
, size
) == 0 ? p
: 0;
1772 hybrid_realloc (void *ptr
, size_t size
)
1776 size_t block
, oldsize
;
1779 return hybrid_malloc (size
);
1780 if (!allocated_via_gmalloc (ptr
))
1781 return realloc (ptr
, size
);
1783 return grealloc (ptr
, size
);
1785 /* The dumped emacs is trying to realloc storage allocated before
1786 dumping via gmalloc. Allocate new space and copy the data. Do
1787 not bother with gfree (ptr), as that would just waste time. */
1788 block
= BLOCK (ptr
);
1789 type
= _heapinfo
[block
].busy
.type
;
1791 type
< 0 ? _heapinfo
[block
].busy
.info
.size
* BLOCKSIZE
1792 : (size_t) 1 << type
;
1793 result
= malloc (size
);
1795 return memcpy (result
, ptr
, min (oldsize
, size
));
1799 #else /* ! HYBRID_MALLOC */
1802 malloc (size_t size
)
1804 return gmalloc (size
);
1808 calloc (size_t nmemb
, size_t size
)
1810 return gcalloc (nmemb
, size
);
1820 aligned_alloc (size_t alignment
, size_t size
)
1822 return galigned_alloc (alignment
, size
);
1826 realloc (void *ptr
, size_t size
)
1828 return grealloc (ptr
, size
);
1831 #endif /* HYBRID_MALLOC */
1835 /* Standard debugging hooks for `malloc'.
1836 Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
1837 Written May 1989 by Mike Haertel.
1839 This library is free software; you can redistribute it and/or
1840 modify it under the terms of the GNU General Public License as
1841 published by the Free Software Foundation; either version 2 of the
1842 License, or (at your option) any later version.
1844 This library is distributed in the hope that it will be useful,
1845 but WITHOUT ANY WARRANTY; without even the implied warranty of
1846 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1847 General Public License for more details.
1849 You should have received a copy of the GNU General Public
1850 License along with this library. If not, see <http://www.gnu.org/licenses/>.
1852 The author may be reached (Email) at the address mike@ai.mit.edu,
1853 or (US mail) as Mike Haertel c/o Free Software Foundation. */
1857 /* Old hook values. */
1858 static void (*old_free_hook
) (void *ptr
);
1859 static void *(*old_malloc_hook
) (size_t size
);
1860 static void *(*old_realloc_hook
) (void *ptr
, size_t size
);
1862 /* Function to call when something awful happens. */
1863 static void (*abortfunc
) (enum mcheck_status
);
1865 /* Arbitrary magical numbers. */
1866 #define MAGICWORD (SIZE_MAX / 11 ^ SIZE_MAX / 13 << 3)
1867 #define MAGICFREE (SIZE_MAX / 17 ^ SIZE_MAX / 19 << 4)
1868 #define MAGICBYTE ((char) 0xd7)
1869 #define MALLOCFLOOD ((char) 0x93)
1870 #define FREEFLOOD ((char) 0x95)
1874 size_t size
; /* Exact size requested by user. */
1875 size_t magic
; /* Magic number to check header integrity. */
1878 static enum mcheck_status
1879 checkhdr (const struct hdr
*hdr
)
1881 enum mcheck_status status
;
1885 status
= MCHECK_HEAD
;
1888 status
= MCHECK_FREE
;
1891 if (((char *) &hdr
[1])[hdr
->size
] != MAGICBYTE
)
1892 status
= MCHECK_TAIL
;
1897 if (status
!= MCHECK_OK
)
1898 (*abortfunc
) (status
);
1903 freehook (void *ptr
)
1909 struct alignlist
*l
;
1911 /* If the block was allocated by aligned_alloc, its real pointer
1912 to free is recorded in _aligned_blocks; find that. */
1913 PROTECT_MALLOC_STATE (0);
1914 LOCK_ALIGNED_BLOCKS ();
1915 for (l
= _aligned_blocks
; l
!= NULL
; l
= l
->next
)
1916 if (l
->aligned
== ptr
)
1918 l
->aligned
= NULL
; /* Mark the slot in the list as free. */
1922 UNLOCK_ALIGNED_BLOCKS ();
1923 PROTECT_MALLOC_STATE (1);
1925 hdr
= ((struct hdr
*) ptr
) - 1;
1927 hdr
->magic
= MAGICFREE
;
1928 memset (ptr
, FREEFLOOD
, hdr
->size
);
1933 gfree_hook
= old_free_hook
;
1935 gfree_hook
= freehook
;
1939 mallochook (size_t size
)
1943 gmalloc_hook
= old_malloc_hook
;
1944 hdr
= malloc (sizeof *hdr
+ size
+ 1);
1945 gmalloc_hook
= mallochook
;
1950 hdr
->magic
= MAGICWORD
;
1951 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
1952 return memset (hdr
+ 1, MALLOCFLOOD
, size
);
1956 reallochook (void *ptr
, size_t size
)
1958 struct hdr
*hdr
= NULL
;
1963 hdr
= ((struct hdr
*) ptr
) - 1;
1968 memset ((char *) ptr
+ size
, FREEFLOOD
, osize
- size
);
1971 gfree_hook
= old_free_hook
;
1972 gmalloc_hook
= old_malloc_hook
;
1973 grealloc_hook
= old_realloc_hook
;
1974 hdr
= realloc (hdr
, sizeof *hdr
+ size
+ 1);
1975 gfree_hook
= freehook
;
1976 gmalloc_hook
= mallochook
;
1977 grealloc_hook
= reallochook
;
1982 hdr
->magic
= MAGICWORD
;
1983 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
1985 memset ((char *) (hdr
+ 1) + osize
, MALLOCFLOOD
, size
- osize
);
1990 mabort (enum mcheck_status status
)
1996 msg
= "memory is consistent, library is buggy";
1999 msg
= "memory clobbered before allocated block";
2002 msg
= "memory clobbered past end of allocated block";
2005 msg
= "block freed twice";
2008 msg
= "bogus mcheck_status, library is buggy";
2011 #ifdef __GNU_LIBRARY__
2014 fprintf (stderr
, "mcheck: %s\n", msg
);
2024 static int mcheck_used
= 0;
2027 mcheck (void (*func
) (enum mcheck_status
))
2029 abortfunc
= (func
!= NULL
) ? func
: &mabort
;
2031 /* These hooks may not be safely inserted if malloc is already in use. */
2032 if (!__malloc_initialized
&& !mcheck_used
)
2034 old_free_hook
= gfree_hook
;
2035 gfree_hook
= freehook
;
2036 old_malloc_hook
= gmalloc_hook
;
2037 gmalloc_hook
= mallochook
;
2038 old_realloc_hook
= grealloc_hook
;
2039 grealloc_hook
= reallochook
;
2043 return mcheck_used
? 0 : -1;
2049 return mcheck_used
? checkhdr (ptr
) : MCHECK_DISABLED
;
2052 #endif /* GC_MCHECK */