1 /* Declarations for `malloc' and friends.
2 Copyright (C) 1990-1993, 1995-1996, 1999, 2002-2007, 2013-2014 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. */
38 #include <w32heap.h> /* for sbrk */
42 extern void emacs_abort (void);
53 /* Allocate SIZE bytes of memory. */
54 extern void *malloc (size_t size
);
55 /* Re-allocate the previously allocated block
56 in ptr, making the new block SIZE bytes long. */
57 extern void *realloc (void *ptr
, size_t size
);
58 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
59 extern void *calloc (size_t nmemb
, size_t size
);
60 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
61 extern void free (void *ptr
);
63 /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
65 extern void *aligned_alloc (size_t, size_t);
66 extern void *memalign (size_t, size_t);
67 extern int posix_memalign (void **, size_t, size_t);
71 /* Set up mutexes and make malloc etc. thread-safe. */
72 extern void malloc_enable_thread (void);
76 extern void emacs_abort (void);
79 /* The allocator divides the heap into blocks of fixed size; large
80 requests receive one or more whole blocks, and small requests
81 receive a fragment of a block. Fragment sizes are powers of two,
82 and all fragments of a block are the same size. When all the
83 fragments in a block have been freed, the block itself is freed. */
84 #define INT_BIT (CHAR_BIT * sizeof (int))
85 #define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
86 #define BLOCKSIZE (1 << BLOCKLOG)
87 #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
89 /* Determine the amount of memory spanned by the initial heap table
90 (not an absolute limit). */
91 #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
93 /* Number of contiguous free blocks allowed to build up at the end of
94 memory before they will be returned to the system. */
95 #define FINAL_FREE_BLOCKS 8
97 /* Data structure giving per-block information. */
100 /* Heap information for a busy block. */
103 /* Zero for a large (multiblock) object, or positive giving the
104 logarithm to the base two of the fragment size. */
110 size_t nfree
; /* Free frags in a fragmented block. */
111 size_t first
; /* First free fragment of the block. */
113 /* For a large object, in its first block, this has the number
114 of blocks in the object. In the other blocks, this has a
115 negative number which says how far back the first block is. */
119 /* Heap information for a free block
120 (that may be the first of a free cluster). */
123 size_t size
; /* Size (in blocks) of a free cluster. */
124 size_t next
; /* Index of next free cluster. */
125 size_t prev
; /* Index of previous free cluster. */
129 /* Pointer to first block of the heap. */
130 extern char *_heapbase
;
132 /* Table indexed by block number giving per-block information. */
133 extern malloc_info
*_heapinfo
;
135 /* Address to block number and vice versa. */
136 #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
137 #define ADDRESS(B) ((void *) (((B) - 1) * BLOCKSIZE + _heapbase))
139 /* Current search index for the heap table. */
140 extern size_t _heapindex
;
142 /* Limit of valid info table indices. */
143 extern size_t _heaplimit
;
145 /* Doubly linked lists of free fragments. */
152 /* Free list headers for each fragment size. */
153 extern struct list _fraghead
[];
155 /* List of blocks allocated with aligned_alloc and friends. */
158 struct alignlist
*next
;
159 void *aligned
; /* The address that aligned_alloc returned. */
160 void *exact
; /* The address that malloc returned. */
162 extern struct alignlist
*_aligned_blocks
;
164 /* Instrumentation. */
165 extern size_t _chunks_used
;
166 extern size_t _bytes_used
;
167 extern size_t _chunks_free
;
168 extern size_t _bytes_free
;
170 /* Internal versions of `malloc', `realloc', and `free'
171 used when these functions need to call each other.
172 They are the same but don't call the hooks. */
173 extern void *_malloc_internal (size_t);
174 extern void *_realloc_internal (void *, size_t);
175 extern void _free_internal (void *);
176 extern void *_malloc_internal_nolock (size_t);
177 extern void *_realloc_internal_nolock (void *, size_t);
178 extern void _free_internal_nolock (void *);
181 extern pthread_mutex_t _malloc_mutex
, _aligned_blocks_mutex
;
182 extern int _malloc_thread_enabled_p
;
185 if (_malloc_thread_enabled_p) \
186 pthread_mutex_lock (&_malloc_mutex); \
190 if (_malloc_thread_enabled_p) \
191 pthread_mutex_unlock (&_malloc_mutex); \
193 #define LOCK_ALIGNED_BLOCKS() \
195 if (_malloc_thread_enabled_p) \
196 pthread_mutex_lock (&_aligned_blocks_mutex); \
198 #define UNLOCK_ALIGNED_BLOCKS() \
200 if (_malloc_thread_enabled_p) \
201 pthread_mutex_unlock (&_aligned_blocks_mutex); \
206 #define LOCK_ALIGNED_BLOCKS()
207 #define UNLOCK_ALIGNED_BLOCKS()
210 /* Given an address in the middle of a malloc'd object,
211 return the address of the beginning of the object. */
212 extern void *malloc_find_object_address (void *ptr
);
214 /* Underlying allocation function; successive calls should
215 return contiguous pieces of memory. */
216 extern void *(*__morecore
) (ptrdiff_t size
);
218 /* Default value of `__morecore'. */
219 extern void *__default_morecore (ptrdiff_t size
);
221 /* If not NULL, this function is called after each time
222 `__morecore' is called to increase the data size. */
223 extern void (*__after_morecore_hook
) (void);
225 /* Number of extra blocks to get each time we ask for more core.
226 This reduces the frequency of calling `(*__morecore)'. */
227 extern size_t __malloc_extra_blocks
;
229 /* Nonzero if `malloc' has been called and done its initialization. */
230 extern int __malloc_initialized
;
231 /* Function called to initialize malloc data structures. */
232 extern int __malloc_initialize (void);
234 /* Hooks for debugging versions. */
235 extern void (*__malloc_initialize_hook
) (void);
236 extern void (*__free_hook
) (void *ptr
);
237 extern void *(*__malloc_hook
) (size_t size
);
238 extern void *(*__realloc_hook
) (void *ptr
, size_t size
);
239 extern void *(*__memalign_hook
) (size_t size
, size_t alignment
);
241 /* Return values for `mprobe': these are the kinds of inconsistencies that
242 `mcheck' enables detection of. */
245 MCHECK_DISABLED
= -1, /* Consistency checking is not turned on. */
246 MCHECK_OK
, /* Block is fine. */
247 MCHECK_FREE
, /* Block freed twice. */
248 MCHECK_HEAD
, /* Memory before the block was clobbered. */
249 MCHECK_TAIL
/* Memory after the block was clobbered. */
252 /* Activate a standard collection of debugging hooks. This must be called
253 before `malloc' is ever called. ABORTFUNC is called with an error code
254 (see enum above) when an inconsistency is detected. If ABORTFUNC is
255 null, the standard function prints on stderr and then calls `abort'. */
256 extern int mcheck (void (*abortfunc
) (enum mcheck_status
));
258 /* Check for aberrations in a particular malloc'd block. You must have
259 called `mcheck' already. These are the same checks that `mcheck' does
260 when you free or reallocate a block. */
261 extern enum mcheck_status
mprobe (void *ptr
);
263 /* Activate a standard collection of tracing hooks. */
264 extern void mtrace (void);
265 extern void muntrace (void);
267 /* Statistics available to the user. */
270 size_t bytes_total
; /* Total size of the heap. */
271 size_t chunks_used
; /* Chunks allocated by the user. */
272 size_t bytes_used
; /* Byte total of user-allocated chunks. */
273 size_t chunks_free
; /* Chunks in the free list. */
274 size_t bytes_free
; /* Byte total of chunks in the free list. */
277 /* Pick up the current statistics. */
278 extern struct mstats
mstats (void);
280 /* Call WARNFUN with a warning message when memory usage is high. */
281 extern void memory_warnings (void *start
, void (*warnfun
) (const char *));
287 /* Memory allocator `malloc'.
288 Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
289 Written May 1989 by Mike Haertel.
291 This library is free software; you can redistribute it and/or
292 modify it under the terms of the GNU General Public License as
293 published by the Free Software Foundation; either version 2 of the
294 License, or (at your option) any later version.
296 This library is distributed in the hope that it will be useful,
297 but WITHOUT ANY WARRANTY; without even the implied warranty of
298 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
299 General Public License for more details.
301 You should have received a copy of the GNU General Public
302 License along with this library. If not, see <http://www.gnu.org/licenses/>.
304 The author may be reached (Email) at the address mike@ai.mit.edu,
305 or (US mail) as Mike Haertel c/o Free Software Foundation. */
309 /* On Cygwin there are two heaps. temacs uses the static heap
310 (defined in sheap.c and managed with bss_sbrk), and the dumped
311 emacs uses the Cygwin heap (managed with sbrk). When emacs starts
312 on Cygwin, it reinitializes malloc, and we save the old info for
313 use by free and realloc if they're called with a pointer into the
316 Currently (2011-08-16) the Cygwin build doesn't use ralloc.c; if
317 this is changed in the future, we'll have to similarly deal with
318 reinitializing ralloc. */
320 extern void *bss_sbrk (ptrdiff_t size
);
321 extern int bss_sbrk_did_unexec
;
322 char *bss_sbrk_heapbase
; /* _heapbase for static heap */
323 malloc_info
*bss_sbrk_heapinfo
; /* _heapinfo for static heap */
325 void *(*__morecore
) (ptrdiff_t size
) = __default_morecore
;
327 /* Debugging hook for `malloc'. */
328 void *(*__malloc_hook
) (size_t size
);
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 /* Number of info entries. */
337 static size_t heapsize
;
339 /* Search index in the info table. */
342 /* Limit of valid info table indices. */
345 /* Free lists for each fragment size. */
346 struct list _fraghead
[BLOCKLOG
];
348 /* Instrumentation. */
354 /* Are you experienced? */
355 int __malloc_initialized
;
357 size_t __malloc_extra_blocks
;
359 void (*__malloc_initialize_hook
) (void);
360 void (*__after_morecore_hook
) (void);
362 #if defined GC_MALLOC_CHECK && defined GC_PROTECT_MALLOC_STATE
364 /* Some code for hunting a bug writing into _heapinfo.
366 Call this macro with argument PROT non-zero to protect internal
367 malloc state against writing to it, call it with a zero argument to
368 make it readable and writable.
370 Note that this only works if BLOCKSIZE == page size, which is
371 the case on the i386. */
373 #include <sys/types.h>
374 #include <sys/mman.h>
376 static int state_protected_p
;
377 static size_t last_state_size
;
378 static malloc_info
*last_heapinfo
;
381 protect_malloc_state (int protect_p
)
383 /* If _heapinfo has been relocated, make sure its old location
384 isn't left read-only; it will be reused by malloc. */
385 if (_heapinfo
!= last_heapinfo
387 && state_protected_p
)
388 mprotect (last_heapinfo
, last_state_size
, PROT_READ
| PROT_WRITE
);
390 last_state_size
= _heaplimit
* sizeof *_heapinfo
;
391 last_heapinfo
= _heapinfo
;
393 if (protect_p
!= state_protected_p
)
395 state_protected_p
= protect_p
;
396 if (mprotect (_heapinfo
, last_state_size
,
397 protect_p
? PROT_READ
: PROT_READ
| PROT_WRITE
) != 0)
402 #define PROTECT_MALLOC_STATE(PROT) protect_malloc_state (PROT)
405 #define PROTECT_MALLOC_STATE(PROT) /* empty */
409 /* Aligned allocation. */
416 /* align accepts an unsigned argument, but __morecore accepts a
417 signed one. This could lead to trouble if SIZE overflows the
418 ptrdiff_t type accepted by __morecore. We just punt in that
419 case, since they are requesting a ludicrous amount anyway. */
420 if (PTRDIFF_MAX
< size
)
423 result
= (*__morecore
) (size
);
424 adj
= (uintptr_t) result
% BLOCKSIZE
;
427 adj
= BLOCKSIZE
- adj
;
429 result
= (char *) result
+ adj
;
432 if (__after_morecore_hook
)
433 (*__after_morecore_hook
) ();
438 /* Get SIZE bytes, if we can get them starting at END.
439 Return the address of the space we got.
440 If we cannot get space at END, fail and return 0. */
442 get_contiguous_space (ptrdiff_t size
, void *position
)
447 before
= (*__morecore
) (0);
448 /* If we can tell in advance that the break is at the wrong place,
450 if (before
!= position
)
453 /* Allocate SIZE bytes and get the address of them. */
454 after
= (*__morecore
) (size
);
458 /* It was not contiguous--reject it. */
459 if (after
!= position
)
461 (*__morecore
) (- size
);
469 /* This is called when `_heapinfo' and `heapsize' have just
470 been set to describe a new info table. Set up the table
471 to describe itself and account for it in the statistics. */
473 register_heapinfo (void)
475 size_t block
, blocks
;
477 block
= BLOCK (_heapinfo
);
478 blocks
= BLOCKIFY (heapsize
* sizeof (malloc_info
));
480 /* Account for the _heapinfo block itself in the statistics. */
481 _bytes_used
+= blocks
* BLOCKSIZE
;
484 /* Describe the heapinfo block itself in the heapinfo. */
485 _heapinfo
[block
].busy
.type
= 0;
486 _heapinfo
[block
].busy
.info
.size
= blocks
;
487 /* Leave back-pointers for malloc_find_address. */
489 _heapinfo
[block
+ blocks
].busy
.info
.size
= -blocks
;
493 /* On Cygwin prior to 1.7.31, pthread_mutexes were ERRORCHECK mutexes
494 by default. When the default changed to NORMAL in Cygwin-1.7.31,
495 deadlocks occurred (bug#18222). As a temporary workaround, we
496 explicitly set the mutexes to be of ERRORCHECK type, restoring the
497 previous behavior. */
499 pthread_mutex_t _malloc_mutex
= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
;
500 pthread_mutex_t _aligned_blocks_mutex
= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
;
501 #else /* not CYGWIN */
502 pthread_mutex_t _malloc_mutex
= PTHREAD_MUTEX_INITIALIZER
;
503 pthread_mutex_t _aligned_blocks_mutex
= PTHREAD_MUTEX_INITIALIZER
;
504 #endif /* not CYGWIN */
505 int _malloc_thread_enabled_p
;
508 malloc_atfork_handler_prepare (void)
511 LOCK_ALIGNED_BLOCKS ();
515 malloc_atfork_handler_parent (void)
517 UNLOCK_ALIGNED_BLOCKS ();
522 malloc_atfork_handler_child (void)
524 UNLOCK_ALIGNED_BLOCKS ();
528 /* Set up mutexes and make malloc etc. thread-safe. */
530 malloc_enable_thread (void)
532 if (_malloc_thread_enabled_p
)
535 /* Some pthread implementations call malloc for statically
536 initialized mutexes when they are used first. To avoid such a
537 situation, we initialize mutexes here while their use is
538 disabled in malloc etc. */
540 /* Use ERRORCHECK mutexes; see comment above. */
541 pthread_mutexattr_t attr
;
542 pthread_mutexattr_init (&attr
);
543 pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_ERRORCHECK
);
544 pthread_mutex_init (&_malloc_mutex
, &attr
);
545 pthread_mutex_init (&_aligned_blocks_mutex
, &attr
);
546 #else /* not CYGWIN */
547 pthread_mutex_init (&_malloc_mutex
, NULL
);
548 pthread_mutex_init (&_aligned_blocks_mutex
, NULL
);
549 #endif /* not CYGWIN */
550 pthread_atfork (malloc_atfork_handler_prepare
,
551 malloc_atfork_handler_parent
,
552 malloc_atfork_handler_child
);
553 _malloc_thread_enabled_p
= 1;
555 #endif /* USE_PTHREAD */
558 malloc_initialize_1 (void)
565 if (bss_sbrk_did_unexec
)
566 /* we're reinitializing the dumped emacs */
568 bss_sbrk_heapbase
= _heapbase
;
569 bss_sbrk_heapinfo
= _heapinfo
;
570 memset (_fraghead
, 0, BLOCKLOG
* sizeof (struct list
));
574 if (__malloc_initialize_hook
)
575 (*__malloc_initialize_hook
) ();
577 heapsize
= HEAP
/ BLOCKSIZE
;
578 _heapinfo
= align (heapsize
* sizeof (malloc_info
));
579 if (_heapinfo
== NULL
)
581 memset (_heapinfo
, 0, heapsize
* sizeof (malloc_info
));
582 _heapinfo
[0].free
.size
= 0;
583 _heapinfo
[0].free
.next
= _heapinfo
[0].free
.prev
= 0;
585 _heapbase
= (char *) _heapinfo
;
586 _heaplimit
= BLOCK (_heapbase
+ heapsize
* sizeof (malloc_info
));
588 register_heapinfo ();
590 __malloc_initialized
= 1;
591 PROTECT_MALLOC_STATE (1);
595 /* Set everything up and remember that we have.
596 main will call malloc which calls this function. That is before any threads
597 or signal handlers has been set up, so we don't need thread protection. */
599 __malloc_initialize (void)
601 if (__malloc_initialized
)
604 malloc_initialize_1 ();
606 return __malloc_initialized
;
609 static int morecore_recursing
;
611 /* Get neatly aligned memory, initializing or
612 growing the heap info table as necessary. */
614 morecore_nolock (size_t size
)
617 malloc_info
*newinfo
, *oldinfo
;
620 if (morecore_recursing
)
621 /* Avoid recursion. The caller will know how to handle a null return. */
624 result
= align (size
);
628 PROTECT_MALLOC_STATE (0);
630 /* Check if we need to grow the info table. */
631 if ((size_t) BLOCK ((char *) result
+ size
) > heapsize
)
633 /* Calculate the new _heapinfo table size. We do not account for the
634 added blocks in the table itself, as we hope to place them in
635 existing free space, which is already covered by part of the
640 while ((size_t) BLOCK ((char *) result
+ size
) > newsize
);
642 /* We must not reuse existing core for the new info table when called
643 from realloc in the case of growing a large block, because the
644 block being grown is momentarily marked as free. In this case
645 _heaplimit is zero so we know not to reuse space for internal
649 /* First try to allocate the new info table in core we already
650 have, in the usual way using realloc. If realloc cannot
651 extend it in place or relocate it to existing sufficient core,
652 we will get called again, and the code above will notice the
653 `morecore_recursing' flag and return null. */
654 int save
= errno
; /* Don't want to clobber errno with ENOMEM. */
655 morecore_recursing
= 1;
656 newinfo
= _realloc_internal_nolock (_heapinfo
,
657 newsize
* sizeof (malloc_info
));
658 morecore_recursing
= 0;
663 /* We found some space in core, and realloc has put the old
664 table's blocks on the free list. Now zero the new part
665 of the table and install the new table location. */
666 memset (&newinfo
[heapsize
], 0,
667 (newsize
- heapsize
) * sizeof (malloc_info
));
674 /* Allocate new space for the malloc info table. */
677 newinfo
= align (newsize
* sizeof (malloc_info
));
682 (*__morecore
) (-size
);
686 /* Is it big enough to record status for its own space?
688 if ((size_t) BLOCK ((char *) newinfo
689 + newsize
* sizeof (malloc_info
))
693 /* Must try again. First give back most of what we just got. */
694 (*__morecore
) (- newsize
* sizeof (malloc_info
));
698 /* Copy the old table to the beginning of the new,
699 and zero the rest of the new table. */
700 memcpy (newinfo
, _heapinfo
, heapsize
* sizeof (malloc_info
));
701 memset (&newinfo
[heapsize
], 0,
702 (newsize
- heapsize
) * sizeof (malloc_info
));
707 register_heapinfo ();
709 /* Reset _heaplimit so _free_internal never decides
710 it can relocate or resize the info table. */
712 _free_internal_nolock (oldinfo
);
713 PROTECT_MALLOC_STATE (0);
715 /* The new heap limit includes the new table just allocated. */
716 _heaplimit
= BLOCK ((char *) newinfo
+ heapsize
* sizeof (malloc_info
));
721 _heaplimit
= BLOCK ((char *) result
+ size
);
725 /* Allocate memory from the heap. */
727 _malloc_internal_nolock (size_t size
)
730 size_t block
, blocks
, lastblocks
, start
;
734 /* ANSI C allows `malloc (0)' to either return NULL, or to return a
735 valid address you can realloc and free (though not dereference).
737 It turns out that some extant code (sunrpc, at least Ultrix's version)
738 expects `malloc (0)' to return non-NULL and breaks otherwise.
746 PROTECT_MALLOC_STATE (0);
748 if (size
< sizeof (struct list
))
749 size
= sizeof (struct list
);
751 /* Determine the allocation policy based on the request size. */
752 if (size
<= BLOCKSIZE
/ 2)
754 /* Small allocation to receive a fragment of a block.
755 Determine the logarithm to base two of the fragment size. */
756 register size_t log
= 1;
758 while ((size
/= 2) != 0)
761 /* Look in the fragment lists for a
762 free fragment of the desired size. */
763 next
= _fraghead
[log
].next
;
766 /* There are free fragments of this size.
767 Pop a fragment out of the fragment list and return it.
768 Update the block's nfree and first counters. */
770 next
->prev
->next
= next
->next
;
771 if (next
->next
!= NULL
)
772 next
->next
->prev
= next
->prev
;
773 block
= BLOCK (result
);
774 if (--_heapinfo
[block
].busy
.info
.frag
.nfree
!= 0)
775 _heapinfo
[block
].busy
.info
.frag
.first
=
776 (uintptr_t) next
->next
% BLOCKSIZE
>> log
;
778 /* Update the statistics. */
780 _bytes_used
+= 1 << log
;
782 _bytes_free
-= 1 << log
;
786 /* No free fragments of the desired size, so get a new block
787 and break it into fragments, returning the first. */
788 #ifdef GC_MALLOC_CHECK
789 result
= _malloc_internal_nolock (BLOCKSIZE
);
790 PROTECT_MALLOC_STATE (0);
791 #elif defined (USE_PTHREAD)
792 result
= _malloc_internal_nolock (BLOCKSIZE
);
794 result
= malloc (BLOCKSIZE
);
798 PROTECT_MALLOC_STATE (1);
802 /* Link all fragments but the first into the free list. */
803 next
= (struct list
*) ((char *) result
+ (1 << log
));
805 next
->prev
= &_fraghead
[log
];
806 _fraghead
[log
].next
= next
;
808 for (i
= 2; i
< (size_t) (BLOCKSIZE
>> log
); ++i
)
810 next
= (struct list
*) ((char *) result
+ (i
<< log
));
811 next
->next
= _fraghead
[log
].next
;
812 next
->prev
= &_fraghead
[log
];
813 next
->prev
->next
= next
;
814 next
->next
->prev
= next
;
817 /* Initialize the nfree and first counters for this block. */
818 block
= BLOCK (result
);
819 _heapinfo
[block
].busy
.type
= log
;
820 _heapinfo
[block
].busy
.info
.frag
.nfree
= i
- 1;
821 _heapinfo
[block
].busy
.info
.frag
.first
= i
- 1;
823 _chunks_free
+= (BLOCKSIZE
>> log
) - 1;
824 _bytes_free
+= BLOCKSIZE
- (1 << log
);
825 _bytes_used
-= BLOCKSIZE
- (1 << log
);
830 /* Large allocation to receive one or more blocks.
831 Search the free list in a circle starting at the last place visited.
832 If we loop completely around without finding a large enough
833 space we will have to get more memory from the system. */
834 blocks
= BLOCKIFY (size
);
835 start
= block
= _heapindex
;
836 while (_heapinfo
[block
].free
.size
< blocks
)
838 block
= _heapinfo
[block
].free
.next
;
841 /* Need to get more from the system. Get a little extra. */
842 size_t wantblocks
= blocks
+ __malloc_extra_blocks
;
843 block
= _heapinfo
[0].free
.prev
;
844 lastblocks
= _heapinfo
[block
].free
.size
;
845 /* Check to see if the new core will be contiguous with the
846 final free block; if so we don't need to get as much. */
847 if (_heaplimit
!= 0 && block
+ lastblocks
== _heaplimit
&&
848 /* We can't do this if we will have to make the heap info
849 table bigger to accommodate the new space. */
850 block
+ wantblocks
<= heapsize
&&
851 get_contiguous_space ((wantblocks
- lastblocks
) * BLOCKSIZE
,
852 ADDRESS (block
+ lastblocks
)))
854 /* We got it contiguously. Which block we are extending
855 (the `final free block' referred to above) might have
856 changed, if it got combined with a freed info table. */
857 block
= _heapinfo
[0].free
.prev
;
858 _heapinfo
[block
].free
.size
+= (wantblocks
- lastblocks
);
859 _bytes_free
+= (wantblocks
- lastblocks
) * BLOCKSIZE
;
860 _heaplimit
+= wantblocks
- lastblocks
;
863 result
= morecore_nolock (wantblocks
* BLOCKSIZE
);
866 block
= BLOCK (result
);
867 /* Put the new block at the end of the free list. */
868 _heapinfo
[block
].free
.size
= wantblocks
;
869 _heapinfo
[block
].free
.prev
= _heapinfo
[0].free
.prev
;
870 _heapinfo
[block
].free
.next
= 0;
871 _heapinfo
[0].free
.prev
= block
;
872 _heapinfo
[_heapinfo
[block
].free
.prev
].free
.next
= block
;
874 /* Now loop to use some of that block for this allocation. */
878 /* At this point we have found a suitable free list entry.
879 Figure out how to remove what we need from the list. */
880 result
= ADDRESS (block
);
881 if (_heapinfo
[block
].free
.size
> blocks
)
883 /* The block we found has a bit left over,
884 so relink the tail end back into the free list. */
885 _heapinfo
[block
+ blocks
].free
.size
886 = _heapinfo
[block
].free
.size
- blocks
;
887 _heapinfo
[block
+ blocks
].free
.next
888 = _heapinfo
[block
].free
.next
;
889 _heapinfo
[block
+ blocks
].free
.prev
890 = _heapinfo
[block
].free
.prev
;
891 _heapinfo
[_heapinfo
[block
].free
.prev
].free
.next
892 = _heapinfo
[_heapinfo
[block
].free
.next
].free
.prev
893 = _heapindex
= block
+ blocks
;
897 /* The block exactly matches our requirements,
898 so just remove it from the list. */
899 _heapinfo
[_heapinfo
[block
].free
.next
].free
.prev
900 = _heapinfo
[block
].free
.prev
;
901 _heapinfo
[_heapinfo
[block
].free
.prev
].free
.next
902 = _heapindex
= _heapinfo
[block
].free
.next
;
906 _heapinfo
[block
].busy
.type
= 0;
907 _heapinfo
[block
].busy
.info
.size
= blocks
;
909 _bytes_used
+= blocks
* BLOCKSIZE
;
910 _bytes_free
-= blocks
* BLOCKSIZE
;
912 /* Mark all the blocks of the object just allocated except for the
913 first with a negative number so you can find the first block by
914 adding that adjustment. */
916 _heapinfo
[block
+ blocks
].busy
.info
.size
= -blocks
;
919 PROTECT_MALLOC_STATE (1);
925 _malloc_internal (size_t size
)
930 result
= _malloc_internal_nolock (size
);
939 void *(*hook
) (size_t);
941 if (!__malloc_initialized
&& !__malloc_initialize ())
944 /* Copy the value of __malloc_hook to an automatic variable in case
945 __malloc_hook is modified in another thread between its
946 NULL-check and the use.
948 Note: Strictly speaking, this is not a right solution. We should
949 use mutexes to access non-read-only variables that are shared
950 among multiple threads. We just leave it for compatibility with
951 glibc malloc (i.e., assignments to __malloc_hook) for now. */
952 hook
= __malloc_hook
;
953 return (hook
!= NULL
? *hook
: _malloc_internal
) (size
);
958 /* On some ANSI C systems, some libc functions call _malloc, _free
959 and _realloc. Make them use the GNU functions. */
961 extern void *_malloc (size_t);
962 extern void _free (void *);
963 extern void *_realloc (void *, size_t);
966 _malloc (size_t size
)
968 return malloc (size
);
978 _realloc (void *ptr
, size_t size
)
980 return realloc (ptr
, size
);
984 /* Free a block of memory allocated by `malloc'.
985 Copyright 1990, 1991, 1992, 1994, 1995 Free Software Foundation, Inc.
986 Written May 1989 by Mike Haertel.
988 This library is free software; you can redistribute it and/or
989 modify it under the terms of the GNU General Public License as
990 published by the Free Software Foundation; either version 2 of the
991 License, or (at your option) any later version.
993 This library is distributed in the hope that it will be useful,
994 but WITHOUT ANY WARRANTY; without even the implied warranty of
995 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
996 General Public License for more details.
998 You should have received a copy of the GNU General Public
999 License along with this library. If not, see <http://www.gnu.org/licenses/>.
1001 The author may be reached (Email) at the address mike@ai.mit.edu,
1002 or (US mail) as Mike Haertel c/o Free Software Foundation. */
1005 /* Debugging hook for free. */
1006 void (*__free_hook
) (void *__ptr
);
1008 /* List of blocks allocated by aligned_alloc. */
1009 struct alignlist
*_aligned_blocks
= NULL
;
1011 /* Return memory to the heap.
1012 Like `_free_internal' but don't lock mutex. */
1014 _free_internal_nolock (void *ptr
)
1017 size_t block
, blocks
;
1019 struct list
*prev
, *next
;
1021 const size_t lesscore_threshold
1022 /* Threshold of free space at which we will return some to the system. */
1023 = FINAL_FREE_BLOCKS
+ 2 * __malloc_extra_blocks
;
1025 register struct alignlist
*l
;
1031 if ((char *) ptr
< _heapbase
)
1032 /* We're being asked to free something in the static heap. */
1036 PROTECT_MALLOC_STATE (0);
1038 LOCK_ALIGNED_BLOCKS ();
1039 for (l
= _aligned_blocks
; l
!= NULL
; l
= l
->next
)
1040 if (l
->aligned
== ptr
)
1042 l
->aligned
= NULL
; /* Mark the slot in the list as free. */
1046 UNLOCK_ALIGNED_BLOCKS ();
1048 block
= BLOCK (ptr
);
1050 type
= _heapinfo
[block
].busy
.type
;
1054 /* Get as many statistics as early as we can. */
1056 _bytes_used
-= _heapinfo
[block
].busy
.info
.size
* BLOCKSIZE
;
1057 _bytes_free
+= _heapinfo
[block
].busy
.info
.size
* BLOCKSIZE
;
1059 /* Find the free cluster previous to this one in the free list.
1060 Start searching at the last block referenced; this may benefit
1061 programs with locality of allocation. */
1065 i
= _heapinfo
[i
].free
.prev
;
1069 i
= _heapinfo
[i
].free
.next
;
1070 while (i
> 0 && i
< block
);
1071 i
= _heapinfo
[i
].free
.prev
;
1074 /* Determine how to link this block into the free list. */
1075 if (block
== i
+ _heapinfo
[i
].free
.size
)
1077 /* Coalesce this block with its predecessor. */
1078 _heapinfo
[i
].free
.size
+= _heapinfo
[block
].busy
.info
.size
;
1083 /* Really link this block back into the free list. */
1084 _heapinfo
[block
].free
.size
= _heapinfo
[block
].busy
.info
.size
;
1085 _heapinfo
[block
].free
.next
= _heapinfo
[i
].free
.next
;
1086 _heapinfo
[block
].free
.prev
= i
;
1087 _heapinfo
[i
].free
.next
= block
;
1088 _heapinfo
[_heapinfo
[block
].free
.next
].free
.prev
= block
;
1092 /* Now that the block is linked in, see if we can coalesce it
1093 with its successor (by deleting its successor from the list
1094 and adding in its size). */
1095 if (block
+ _heapinfo
[block
].free
.size
== _heapinfo
[block
].free
.next
)
1097 _heapinfo
[block
].free
.size
1098 += _heapinfo
[_heapinfo
[block
].free
.next
].free
.size
;
1099 _heapinfo
[block
].free
.next
1100 = _heapinfo
[_heapinfo
[block
].free
.next
].free
.next
;
1101 _heapinfo
[_heapinfo
[block
].free
.next
].free
.prev
= block
;
1105 /* How many trailing free blocks are there now? */
1106 blocks
= _heapinfo
[block
].free
.size
;
1108 /* Where is the current end of accessible core? */
1109 curbrk
= (*__morecore
) (0);
1111 if (_heaplimit
!= 0 && curbrk
== ADDRESS (_heaplimit
))
1113 /* The end of the malloc heap is at the end of accessible core.
1114 It's possible that moving _heapinfo will allow us to
1115 return some space to the system. */
1117 size_t info_block
= BLOCK (_heapinfo
);
1118 size_t info_blocks
= _heapinfo
[info_block
].busy
.info
.size
;
1119 size_t prev_block
= _heapinfo
[block
].free
.prev
;
1120 size_t prev_blocks
= _heapinfo
[prev_block
].free
.size
;
1121 size_t next_block
= _heapinfo
[block
].free
.next
;
1122 size_t next_blocks
= _heapinfo
[next_block
].free
.size
;
1124 if (/* Win if this block being freed is last in core, the info table
1125 is just before it, the previous free block is just before the
1126 info table, and the two free blocks together form a useful
1127 amount to return to the system. */
1128 (block
+ blocks
== _heaplimit
&&
1129 info_block
+ info_blocks
== block
&&
1130 prev_block
!= 0 && prev_block
+ prev_blocks
== info_block
&&
1131 blocks
+ prev_blocks
>= lesscore_threshold
) ||
1132 /* Nope, not the case. We can also win if this block being
1133 freed is just before the info table, and the table extends
1134 to the end of core or is followed only by a free block,
1135 and the total free space is worth returning to the system. */
1136 (block
+ blocks
== info_block
&&
1137 ((info_block
+ info_blocks
== _heaplimit
&&
1138 blocks
>= lesscore_threshold
) ||
1139 (info_block
+ info_blocks
== next_block
&&
1140 next_block
+ next_blocks
== _heaplimit
&&
1141 blocks
+ next_blocks
>= lesscore_threshold
)))
1144 malloc_info
*newinfo
;
1145 size_t oldlimit
= _heaplimit
;
1147 /* Free the old info table, clearing _heaplimit to avoid
1148 recursion into this code. We don't want to return the
1149 table's blocks to the system before we have copied them to
1150 the new location. */
1152 _free_internal_nolock (_heapinfo
);
1153 _heaplimit
= oldlimit
;
1155 /* Tell malloc to search from the beginning of the heap for
1156 free blocks, so it doesn't reuse the ones just freed. */
1159 /* Allocate new space for the info table and move its data. */
1160 newinfo
= _malloc_internal_nolock (info_blocks
* BLOCKSIZE
);
1161 PROTECT_MALLOC_STATE (0);
1162 memmove (newinfo
, _heapinfo
, info_blocks
* BLOCKSIZE
);
1163 _heapinfo
= newinfo
;
1165 /* We should now have coalesced the free block with the
1166 blocks freed from the old info table. Examine the entire
1167 trailing free block to decide below whether to return some
1169 block
= _heapinfo
[0].free
.prev
;
1170 blocks
= _heapinfo
[block
].free
.size
;
1173 /* Now see if we can return stuff to the system. */
1174 if (block
+ blocks
== _heaplimit
&& blocks
>= lesscore_threshold
)
1176 register size_t bytes
= blocks
* BLOCKSIZE
;
1177 _heaplimit
-= blocks
;
1178 (*__morecore
) (-bytes
);
1179 _heapinfo
[_heapinfo
[block
].free
.prev
].free
.next
1180 = _heapinfo
[block
].free
.next
;
1181 _heapinfo
[_heapinfo
[block
].free
.next
].free
.prev
1182 = _heapinfo
[block
].free
.prev
;
1183 block
= _heapinfo
[block
].free
.prev
;
1185 _bytes_free
-= bytes
;
1189 /* Set the next search to begin at this block. */
1194 /* Do some of the statistics. */
1196 _bytes_used
-= 1 << type
;
1198 _bytes_free
+= 1 << type
;
1200 /* Get the address of the first free fragment in this block. */
1201 prev
= (struct list
*) ((char *) ADDRESS (block
) +
1202 (_heapinfo
[block
].busy
.info
.frag
.first
<< type
));
1204 if (_heapinfo
[block
].busy
.info
.frag
.nfree
== (BLOCKSIZE
>> type
) - 1)
1206 /* If all fragments of this block are free, remove them
1207 from the fragment list and free the whole block. */
1209 for (i
= 1; i
< (size_t) (BLOCKSIZE
>> type
); ++i
)
1211 prev
->prev
->next
= next
;
1213 next
->prev
= prev
->prev
;
1214 _heapinfo
[block
].busy
.type
= 0;
1215 _heapinfo
[block
].busy
.info
.size
= 1;
1217 /* Keep the statistics accurate. */
1219 _bytes_used
+= BLOCKSIZE
;
1220 _chunks_free
-= BLOCKSIZE
>> type
;
1221 _bytes_free
-= BLOCKSIZE
;
1223 #if defined (GC_MALLOC_CHECK) || defined (USE_PTHREAD)
1224 _free_internal_nolock (ADDRESS (block
));
1226 free (ADDRESS (block
));
1229 else if (_heapinfo
[block
].busy
.info
.frag
.nfree
!= 0)
1231 /* If some fragments of this block are free, link this
1232 fragment into the fragment list after the first free
1233 fragment of this block. */
1235 next
->next
= prev
->next
;
1238 if (next
->next
!= NULL
)
1239 next
->next
->prev
= next
;
1240 ++_heapinfo
[block
].busy
.info
.frag
.nfree
;
1244 /* No fragments of this block are free, so link this
1245 fragment into the fragment list and announce that
1246 it is the first free fragment of this block. */
1248 _heapinfo
[block
].busy
.info
.frag
.nfree
= 1;
1249 _heapinfo
[block
].busy
.info
.frag
.first
=
1250 (uintptr_t) ptr
% BLOCKSIZE
>> type
;
1251 prev
->next
= _fraghead
[type
].next
;
1252 prev
->prev
= &_fraghead
[type
];
1253 prev
->prev
->next
= prev
;
1254 if (prev
->next
!= NULL
)
1255 prev
->next
->prev
= prev
;
1260 PROTECT_MALLOC_STATE (1);
1263 /* Return memory to the heap.
1264 Like `free' but don't call a __free_hook if there is one. */
1266 _free_internal (void *ptr
)
1269 _free_internal_nolock (ptr
);
1273 /* Return memory to the heap. */
1278 void (*hook
) (void *) = __free_hook
;
1283 _free_internal (ptr
);
1286 /* Define the `cfree' alias for `free'. */
1288 weak_alias (free
, cfree
)
1296 /* Change the size of a block allocated by `malloc'.
1297 Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
1298 Written May 1989 by Mike Haertel.
1300 This library is free software; you can redistribute it and/or
1301 modify it under the terms of the GNU General Public License as
1302 published by the Free Software Foundation; either version 2 of the
1303 License, or (at your option) any later version.
1305 This library is distributed in the hope that it will be useful,
1306 but WITHOUT ANY WARRANTY; without even the implied warranty of
1307 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1308 General Public License for more details.
1310 You should have received a copy of the GNU General Public
1311 License along with this library. If not, see <http://www.gnu.org/licenses/>.
1313 The author may be reached (Email) at the address mike@ai.mit.edu,
1314 or (US mail) as Mike Haertel c/o Free Software Foundation. */
1317 #define min(A, B) ((A) < (B) ? (A) : (B))
1320 /* On Cygwin the dumped emacs may try to realloc storage allocated in
1321 the static heap. We just malloc space in the new heap and copy the
1325 special_realloc (void *ptr
, size_t size
)
1329 size_t block
, oldsize
;
1331 block
= ((char *) ptr
- bss_sbrk_heapbase
) / BLOCKSIZE
+ 1;
1332 type
= bss_sbrk_heapinfo
[block
].busy
.type
;
1334 type
== 0 ? bss_sbrk_heapinfo
[block
].busy
.info
.size
* BLOCKSIZE
1335 : (size_t) 1 << type
;
1336 result
= _malloc_internal_nolock (size
);
1338 return memcpy (result
, ptr
, min (oldsize
, size
));
1343 /* Debugging hook for realloc. */
1344 void *(*__realloc_hook
) (void *ptr
, size_t size
);
1346 /* Resize the given region to the new size, returning a pointer
1347 to the (possibly moved) region. This is optimized for speed;
1348 some benchmarks seem to indicate that greater compactness is
1349 achieved by unconditionally allocating and copying to a
1350 new region. This module has incestuous knowledge of the
1351 internals of both free and malloc. */
1353 _realloc_internal_nolock (void *ptr
, size_t size
)
1357 size_t block
, blocks
, oldlimit
;
1361 _free_internal_nolock (ptr
);
1362 return _malloc_internal_nolock (0);
1364 else if (ptr
== NULL
)
1365 return _malloc_internal_nolock (size
);
1368 if ((char *) ptr
< _heapbase
)
1369 /* ptr points into the static heap */
1370 return special_realloc (ptr
, size
);
1373 block
= BLOCK (ptr
);
1375 PROTECT_MALLOC_STATE (0);
1377 type
= _heapinfo
[block
].busy
.type
;
1381 /* Maybe reallocate a large block to a small fragment. */
1382 if (size
<= BLOCKSIZE
/ 2)
1384 result
= _malloc_internal_nolock (size
);
1387 memcpy (result
, ptr
, size
);
1388 _free_internal_nolock (ptr
);
1393 /* The new size is a large allocation as well;
1394 see if we can hold it in place. */
1395 blocks
= BLOCKIFY (size
);
1396 if (blocks
< _heapinfo
[block
].busy
.info
.size
)
1398 /* The new size is smaller; return
1399 excess memory to the free list. */
1400 _heapinfo
[block
+ blocks
].busy
.type
= 0;
1401 _heapinfo
[block
+ blocks
].busy
.info
.size
1402 = _heapinfo
[block
].busy
.info
.size
- blocks
;
1403 _heapinfo
[block
].busy
.info
.size
= blocks
;
1404 /* We have just created a new chunk by splitting a chunk in two.
1405 Now we will free this chunk; increment the statistics counter
1406 so it doesn't become wrong when _free_internal decrements it. */
1408 _free_internal_nolock (ADDRESS (block
+ blocks
));
1411 else if (blocks
== _heapinfo
[block
].busy
.info
.size
)
1412 /* No size change necessary. */
1416 /* Won't fit, so allocate a new region that will.
1417 Free the old region first in case there is sufficient
1418 adjacent free space to grow without moving. */
1419 blocks
= _heapinfo
[block
].busy
.info
.size
;
1420 /* Prevent free from actually returning memory to the system. */
1421 oldlimit
= _heaplimit
;
1423 _free_internal_nolock (ptr
);
1424 result
= _malloc_internal_nolock (size
);
1425 PROTECT_MALLOC_STATE (0);
1426 if (_heaplimit
== 0)
1427 _heaplimit
= oldlimit
;
1430 /* Now we're really in trouble. We have to unfree
1431 the thing we just freed. Unfortunately it might
1432 have been coalesced with its neighbors. */
1433 if (_heapindex
== block
)
1434 (void) _malloc_internal_nolock (blocks
* BLOCKSIZE
);
1438 = _malloc_internal_nolock ((block
- _heapindex
) * BLOCKSIZE
);
1439 (void) _malloc_internal_nolock (blocks
* BLOCKSIZE
);
1440 _free_internal_nolock (previous
);
1445 memmove (result
, ptr
, blocks
* BLOCKSIZE
);
1450 /* Old size is a fragment; type is logarithm
1451 to base two of the fragment size. */
1452 if (size
> (size_t) (1 << (type
- 1)) &&
1453 size
<= (size_t) (1 << type
))
1454 /* The new size is the same kind of fragment. */
1458 /* The new size is different; allocate a new space,
1459 and copy the lesser of the new size and the old. */
1460 result
= _malloc_internal_nolock (size
);
1463 memcpy (result
, ptr
, min (size
, (size_t) 1 << type
));
1464 _free_internal_nolock (ptr
);
1469 PROTECT_MALLOC_STATE (1);
1475 _realloc_internal (void *ptr
, size_t size
)
1480 result
= _realloc_internal_nolock (ptr
, size
);
1487 realloc (void *ptr
, size_t size
)
1489 void *(*hook
) (void *, size_t);
1491 if (!__malloc_initialized
&& !__malloc_initialize ())
1494 hook
= __realloc_hook
;
1495 return (hook
!= NULL
? *hook
: _realloc_internal
) (ptr
, size
);
1497 /* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
1499 This library is free software; you can redistribute it and/or
1500 modify it under the terms of the GNU General Public License as
1501 published by the Free Software Foundation; either version 2 of the
1502 License, or (at your option) any later version.
1504 This library is distributed in the hope that it will be useful,
1505 but WITHOUT ANY WARRANTY; without even the implied warranty of
1506 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1507 General Public License for more details.
1509 You should have received a copy of the GNU General Public
1510 License along with this library. If not, see <http://www.gnu.org/licenses/>.
1512 The author may be reached (Email) at the address mike@ai.mit.edu,
1513 or (US mail) as Mike Haertel c/o Free Software Foundation. */
1515 /* Allocate an array of NMEMB elements each SIZE bytes long.
1516 The entire array is initialized to zeros. */
1518 calloc (size_t nmemb
, size_t size
)
1521 size_t bytes
= nmemb
* size
;
1523 if (size
!= 0 && bytes
/ size
!= nmemb
)
1529 result
= malloc (bytes
);
1531 return memset (result
, 0, bytes
);
1534 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
1535 This file is part of the GNU C Library.
1537 The GNU C Library is free software; you can redistribute it and/or modify
1538 it under the terms of the GNU General Public License as published by
1539 the Free Software Foundation; either version 2, or (at your option)
1542 The GNU C Library is distributed in the hope that it will be useful,
1543 but WITHOUT ANY WARRANTY; without even the implied warranty of
1544 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1545 GNU General Public License for more details.
1547 You should have received a copy of the GNU General Public License
1548 along with the GNU C Library. If not, see <http://www.gnu.org/licenses/>. */
1550 /* uClibc defines __GNU_LIBRARY__, but it is not completely
1552 #if !defined (__GNU_LIBRARY__) || defined (__UCLIBC__)
1554 #else /* __GNU_LIBRARY__ && ! defined (__UCLIBC__) */
1555 /* It is best not to declare this and cast its result on foreign operating
1556 systems with potentially hostile include files. */
1558 extern void *__sbrk (ptrdiff_t increment
);
1559 #endif /* __GNU_LIBRARY__ && ! defined (__UCLIBC__) */
1561 /* Allocate INCREMENT more bytes of data space,
1562 and return the start of data space, or NULL on errors.
1563 If INCREMENT is negative, shrink data space. */
1565 __default_morecore (ptrdiff_t increment
)
1568 #if defined (CYGWIN)
1569 if (!bss_sbrk_did_unexec
)
1571 return bss_sbrk (increment
);
1574 result
= (void *) __sbrk (increment
);
1575 if (result
== (void *) -1)
1579 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
1581 This library is free software; you can redistribute it and/or
1582 modify it under the terms of the GNU General Public License as
1583 published by the Free Software Foundation; either version 2 of the
1584 License, or (at your option) any later version.
1586 This library is distributed in the hope that it will be useful,
1587 but WITHOUT ANY WARRANTY; without even the implied warranty of
1588 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1589 General Public License for more details.
1591 You should have received a copy of the GNU General Public
1592 License along with this library. If not, see <http://www.gnu.org/licenses/>. */
1594 void *(*__memalign_hook
) (size_t size
, size_t alignment
);
1597 aligned_alloc (size_t alignment
, size_t size
)
1600 size_t adj
, lastadj
;
1601 void *(*hook
) (size_t, size_t) = __memalign_hook
;
1604 return (*hook
) (alignment
, size
);
1606 /* Allocate a block with enough extra space to pad the block with up to
1607 (ALIGNMENT - 1) bytes if necessary. */
1608 if (- size
< alignment
)
1613 result
= malloc (size
+ alignment
- 1);
1617 /* Figure out how much we will need to pad this particular block
1618 to achieve the required alignment. */
1619 adj
= alignment
- (uintptr_t) result
% alignment
;
1620 if (adj
== alignment
)
1623 if (adj
!= alignment
- 1)
1627 /* Reallocate the block with only as much excess as it
1630 result
= malloc (size
+ adj
);
1631 if (result
== NULL
) /* Impossible unless interrupted. */
1635 adj
= alignment
- (uintptr_t) result
% alignment
;
1636 if (adj
== alignment
)
1638 /* It's conceivable we might have been so unlucky as to get
1639 a different block with weaker alignment. If so, this
1640 block is too short to contain SIZE after alignment
1641 correction. So we must try again and get another block,
1643 } while (adj
> lastadj
);
1648 /* Record this block in the list of aligned blocks, so that `free'
1649 can identify the pointer it is passed, which will be in the middle
1650 of an allocated block. */
1652 struct alignlist
*l
;
1653 LOCK_ALIGNED_BLOCKS ();
1654 for (l
= _aligned_blocks
; l
!= NULL
; l
= l
->next
)
1655 if (l
->aligned
== NULL
)
1656 /* This slot is free. Use it. */
1660 l
= malloc (sizeof *l
);
1663 l
->next
= _aligned_blocks
;
1664 _aligned_blocks
= l
;
1670 result
= l
->aligned
= (char *) result
+ adj
;
1672 UNLOCK_ALIGNED_BLOCKS ();
1683 /* An obsolete alias for aligned_alloc, for any old libraries that use
1687 memalign (size_t alignment
, size_t size
)
1689 return aligned_alloc (alignment
, size
);
1693 posix_memalign (void **memptr
, size_t alignment
, size_t size
)
1698 || alignment
% sizeof (void *) != 0
1699 || (alignment
& (alignment
- 1)) != 0)
1702 mem
= aligned_alloc (alignment
, size
);
1711 /* Allocate memory on a page boundary.
1712 Copyright (C) 1991, 92, 93, 94, 96 Free Software Foundation, Inc.
1714 This library is free software; you can redistribute it and/or
1715 modify it under the terms of the GNU General Public License as
1716 published by the Free Software Foundation; either version 2 of the
1717 License, or (at your option) any later version.
1719 This library is distributed in the hope that it will be useful,
1720 but WITHOUT ANY WARRANTY; without even the implied warranty of
1721 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1722 General Public License for more details.
1724 You should have received a copy of the GNU General Public
1725 License along with this library. If not, see <http://www.gnu.org/licenses/>.
1727 The author may be reached (Email) at the address mike@ai.mit.edu,
1728 or (US mail) as Mike Haertel c/o Free Software Foundation. */
1730 /* Allocate SIZE bytes on a page boundary. */
1731 extern void *valloc (size_t);
1733 #if defined _SC_PAGESIZE || !defined HAVE_GETPAGESIZE
1734 # include "getpagesize.h"
1735 #elif !defined getpagesize
1736 extern int getpagesize (void);
1739 static size_t pagesize
;
1742 valloc (size_t size
)
1745 pagesize
= getpagesize ();
1747 return aligned_alloc (pagesize
, size
);
1752 /* Standard debugging hooks for `malloc'.
1753 Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
1754 Written May 1989 by Mike Haertel.
1756 This library is free software; you can redistribute it and/or
1757 modify it under the terms of the GNU General Public License as
1758 published by the Free Software Foundation; either version 2 of the
1759 License, or (at your option) any later version.
1761 This library is distributed in the hope that it will be useful,
1762 but WITHOUT ANY WARRANTY; without even the implied warranty of
1763 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1764 General Public License for more details.
1766 You should have received a copy of the GNU General Public
1767 License along with this library. If not, see <http://www.gnu.org/licenses/>.
1769 The author may be reached (Email) at the address mike@ai.mit.edu,
1770 or (US mail) as Mike Haertel c/o Free Software Foundation. */
1774 /* Old hook values. */
1775 static void (*old_free_hook
) (void *ptr
);
1776 static void *(*old_malloc_hook
) (size_t size
);
1777 static void *(*old_realloc_hook
) (void *ptr
, size_t size
);
1779 /* Function to call when something awful happens. */
1780 static void (*abortfunc
) (enum mcheck_status
);
1782 /* Arbitrary magical numbers. */
1783 #define MAGICWORD (SIZE_MAX / 11 ^ SIZE_MAX / 13 << 3)
1784 #define MAGICFREE (SIZE_MAX / 17 ^ SIZE_MAX / 19 << 4)
1785 #define MAGICBYTE ((char) 0xd7)
1786 #define MALLOCFLOOD ((char) 0x93)
1787 #define FREEFLOOD ((char) 0x95)
1791 size_t size
; /* Exact size requested by user. */
1792 size_t magic
; /* Magic number to check header integrity. */
1795 static enum mcheck_status
1796 checkhdr (const struct hdr
*hdr
)
1798 enum mcheck_status status
;
1802 status
= MCHECK_HEAD
;
1805 status
= MCHECK_FREE
;
1808 if (((char *) &hdr
[1])[hdr
->size
] != MAGICBYTE
)
1809 status
= MCHECK_TAIL
;
1814 if (status
!= MCHECK_OK
)
1815 (*abortfunc
) (status
);
1820 freehook (void *ptr
)
1826 struct alignlist
*l
;
1828 /* If the block was allocated by aligned_alloc, its real pointer
1829 to free is recorded in _aligned_blocks; find that. */
1830 PROTECT_MALLOC_STATE (0);
1831 LOCK_ALIGNED_BLOCKS ();
1832 for (l
= _aligned_blocks
; l
!= NULL
; l
= l
->next
)
1833 if (l
->aligned
== ptr
)
1835 l
->aligned
= NULL
; /* Mark the slot in the list as free. */
1839 UNLOCK_ALIGNED_BLOCKS ();
1840 PROTECT_MALLOC_STATE (1);
1842 hdr
= ((struct hdr
*) ptr
) - 1;
1844 hdr
->magic
= MAGICFREE
;
1845 memset (ptr
, FREEFLOOD
, hdr
->size
);
1850 __free_hook
= old_free_hook
;
1852 __free_hook
= freehook
;
1856 mallochook (size_t size
)
1860 __malloc_hook
= old_malloc_hook
;
1861 hdr
= malloc (sizeof *hdr
+ size
+ 1);
1862 __malloc_hook
= mallochook
;
1867 hdr
->magic
= MAGICWORD
;
1868 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
1869 return memset (hdr
+ 1, MALLOCFLOOD
, size
);
1873 reallochook (void *ptr
, size_t size
)
1875 struct hdr
*hdr
= NULL
;
1880 hdr
= ((struct hdr
*) ptr
) - 1;
1885 memset ((char *) ptr
+ size
, FREEFLOOD
, osize
- size
);
1888 __free_hook
= old_free_hook
;
1889 __malloc_hook
= old_malloc_hook
;
1890 __realloc_hook
= old_realloc_hook
;
1891 hdr
= realloc (hdr
, sizeof *hdr
+ size
+ 1);
1892 __free_hook
= freehook
;
1893 __malloc_hook
= mallochook
;
1894 __realloc_hook
= reallochook
;
1899 hdr
->magic
= MAGICWORD
;
1900 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
1902 memset ((char *) (hdr
+ 1) + osize
, MALLOCFLOOD
, size
- osize
);
1907 mabort (enum mcheck_status status
)
1913 msg
= "memory is consistent, library is buggy";
1916 msg
= "memory clobbered before allocated block";
1919 msg
= "memory clobbered past end of allocated block";
1922 msg
= "block freed twice";
1925 msg
= "bogus mcheck_status, library is buggy";
1928 #ifdef __GNU_LIBRARY__
1931 fprintf (stderr
, "mcheck: %s\n", msg
);
1941 static int mcheck_used
= 0;
1944 mcheck (void (*func
) (enum mcheck_status
))
1946 abortfunc
= (func
!= NULL
) ? func
: &mabort
;
1948 /* These hooks may not be safely inserted if malloc is already in use. */
1949 if (!__malloc_initialized
&& !mcheck_used
)
1951 old_free_hook
= __free_hook
;
1952 __free_hook
= freehook
;
1953 old_malloc_hook
= __malloc_hook
;
1954 __malloc_hook
= mallochook
;
1955 old_realloc_hook
= __realloc_hook
;
1956 __realloc_hook
= reallochook
;
1960 return mcheck_used
? 0 : -1;
1966 return mcheck_used
? checkhdr (ptr
) : MCHECK_DISABLED
;
1969 #endif /* GC_MCHECK */