1 /* $NetBSD: malloc.c,v 1.51 2007/12/12 17:56:10 macallan Exp $ */
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
11 * From FreeBSD: malloc.c,v 1.91 2006/01/12 07:28:20 jasone
16 * Defining MALLOC_EXTRA_SANITY will enable extra checks which are related
17 * to internal conditions and consistency in malloc.c. This has a
18 * noticeable runtime performance hit, and generally will not do you
19 * any good unless you fiddle with the internals of malloc or want
20 * to catch random pointer corruption as early as possible.
22 #ifndef MALLOC_EXTRA_SANITY
23 #undef MALLOC_EXTRA_SANITY
27 * What to use for Junk. This is the byte value we use to fill with
28 * when the 'J' option is enabled.
30 #define SOME_JUNK 0xd0 /* as in "Duh" :-) */
33 * The basic parameters you can tweak.
35 * malloc_minsize minimum size of an allocation in bytes.
36 * If this is too small it's too much work
37 * to manage them. This is also the smallest
38 * unit of alignment used for the storage
39 * returned by malloc/realloc.
43 #include "namespace.h"
44 #if defined(__FreeBSD__)
45 # if defined(__i386__)
46 # define malloc_minsize 16U
48 # if defined(__ia64__)
49 # define malloc_pageshift 13U
50 # define malloc_minsize 16U
52 # if defined(__alpha__)
53 # define malloc_pageshift 13U
54 # define malloc_minsize 16U
56 # if defined(__sparc64__)
57 # define malloc_pageshift 13U
58 # define malloc_minsize 16U
60 # if defined(__amd64__)
61 # define malloc_pageshift 12U
62 # define malloc_minsize 16U
65 # define malloc_pageshift 12U
66 # define malloc_minsize 16U
71 #include <sys/cdefs.h>
72 void utrace(struct ut
*, int);
75 * Make malloc/free/realloc thread-safe in libc for use with
78 # include "libc_private.h"
79 # include "spinlock.h"
80 static spinlock_t thread_lock
= _SPINLOCK_INITIALIZER
;
81 # define _MALLOC_LOCK() if (__isthreaded) _SPINLOCK(&thread_lock);
82 # define _MALLOC_UNLOCK() if (__isthreaded) _SPINUNLOCK(&thread_lock);
83 #endif /* __FreeBSD__ */
85 #include <sys/types.h>
86 #if defined(__NetBSD__)
87 # define malloc_minsize 16U
89 # define UTRACE_LABEL "malloc",
90 #include <sys/cdefs.h>
92 #if defined(LIBC_SCCS) && !defined(lint)
93 __RCSID("$NetBSD: malloc.c,v 1.51 2007/12/12 17:56:10 macallan Exp $");
94 #endif /* LIBC_SCCS and not lint */
95 int utrace(const char *, void *, size_t);
97 #include <reentrant.h>
98 extern int __isthreaded
;
99 static mutex_t thread_lock
= MUTEX_INITIALIZER
;
100 #define _MALLOC_LOCK() if (__isthreaded) mutex_lock(&thread_lock);
101 #define _MALLOC_UNLOCK() if (__isthreaded) mutex_unlock(&thread_lock);
102 #endif /* __NetBSD__ */
104 #if defined(__sparc__) && defined(sun)
105 # define malloc_minsize 16U
106 # define MAP_ANON (0)
108 # define MMAP_FD fdzero
109 # define INIT_MMAP() \
110 { if ((fdzero = open(_PATH_DEVZERO, O_RDWR, 0000)) == -1) \
111 wrterror("open of /dev/zero"); }
112 #endif /* __sparc__ */
114 /* Insert your combination here... */
115 #if defined(__FOOCPU__) && defined(__BAROS__)
116 # define malloc_minsize 16U
117 #endif /* __FOOCPU__ && __BAROS__ */
120 #define ZEROSIZEPTR ((void *)(uintptr_t)(1UL << (malloc_pageshift - 1)))
124 * No user serviceable parts behind this point.
126 #include <sys/types.h>
127 #include <sys/mman.h>
138 * This structure describes a page worth of chunks.
142 struct pginfo
*next
; /* next on the free list */
143 void *page
; /* Pointer to the page */
144 u_short size
; /* size of this page's chunks */
145 u_short shift
; /* How far to shift for this size chunks */
146 u_short free
; /* How many free chunks */
147 u_short total
; /* How many chunk */
148 u_int bits
[1]; /* Which chunks are free */
152 * This structure describes a number of free pages.
156 struct pgfree
*next
; /* next run of free pages */
157 struct pgfree
*prev
; /* prev run of free pages */
158 void *page
; /* pointer to free pages */
159 void *end
; /* pointer to end of free pages */
160 size_t size
; /* number of bytes free */
164 * How many bits per u_int in the bitmap.
165 * Change only if not 8 bits/byte
167 #define MALLOC_BITS ((int)(8*sizeof(u_int)))
170 * Magic values to put in the page_directory
172 #define MALLOC_NOT_MINE ((struct pginfo*) 0)
173 #define MALLOC_FREE ((struct pginfo*) 1)
174 #define MALLOC_FIRST ((struct pginfo*) 2)
175 #define MALLOC_FOLLOW ((struct pginfo*) 3)
176 #define MALLOC_MAGIC ((struct pginfo*) 4)
179 * Page size related parameters, computed at run-time.
181 static size_t malloc_pagesize
;
182 static size_t malloc_pageshift
;
183 static size_t malloc_pagemask
;
185 #ifndef malloc_minsize
186 #define malloc_minsize 16U
189 #ifndef malloc_maxsize
190 #define malloc_maxsize ((malloc_pagesize)>>1)
193 #define pageround(foo) (((foo) + (malloc_pagemask))&(~(malloc_pagemask)))
194 #define ptr2idx(foo) \
195 (((size_t)(uintptr_t)(foo) >> malloc_pageshift)-malloc_origo)
198 #define _MALLOC_LOCK()
201 #ifndef _MALLOC_UNLOCK
202 #define _MALLOC_UNLOCK()
214 #define MADV_FREE MADV_DONTNEED
217 /* Number of free pages we cache */
218 static size_t malloc_cache
= 16;
220 /* The offset from pagenumber to index into the page directory */
221 static size_t malloc_origo
;
223 /* The last index in the page directory we care about */
224 static size_t last_idx
;
226 /* Pointer to page directory. Allocated "as if with" malloc */
227 static struct pginfo
**page_dir
;
229 /* How many slots in the page directory */
230 static size_t malloc_ninfo
;
232 /* Free pages line up here */
233 static struct pgfree free_list
;
235 /* Abort(), user doesn't handle problems. */
236 static int malloc_abort
;
238 /* Are we trying to die ? */
241 /* always realloc ? */
242 static int malloc_realloc
;
244 /* pass the kernel a hint on free pages ? */
245 #if defined(MADV_FREE)
246 static int malloc_hint
= 0;
249 /* xmalloc behaviour ? */
250 static int malloc_xmalloc
;
252 /* sysv behaviour for malloc(0) ? */
253 static int malloc_sysv
;
256 static int malloc_zero
;
259 static int malloc_junk
;
264 static int malloc_utrace
;
266 struct ut
{ void *p
; size_t s
; void *r
; };
268 #define UTRACE(a, b, c) \
269 if (malloc_utrace) { \
271 u.p=a; u.s = b; u.r=c; \
272 utrace(UTRACE_LABEL (void *) &u, sizeof u); \
274 #else /* !HAS_UTRACE */
275 #define UTRACE(a,b,c)
276 #endif /* HAS_UTRACE */
279 static void *malloc_brk
;
281 /* one location cache for free-list holders */
282 static struct pgfree
*px
;
284 /* compile-time options */
285 const char *_malloc_options
;
287 /* Name of the current public function */
288 static const char *malloc_func
;
292 mmap(NULL, (size), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, \
296 * Necessary function declarations
298 static int extend_pgdir(size_t idx
);
299 static void *imalloc(size_t size
);
300 static void ifree(void *ptr
);
301 static void *irealloc(void *ptr
, size_t size
);
304 wrtmessage(const char *p1
, const char *p2
, const char *p3
, const char *p4
)
307 write(STDERR_FILENO
, p1
, strlen(p1
));
308 write(STDERR_FILENO
, p2
, strlen(p2
));
309 write(STDERR_FILENO
, p3
, strlen(p3
));
310 write(STDERR_FILENO
, p4
, strlen(p4
));
313 void (*_malloc_message
)(const char *p1
, const char *p2
, const char *p3
,
314 const char *p4
) = wrtmessage
;
316 wrterror(const char *p
)
320 _malloc_message(getprogname(), malloc_func
, " error: ", p
);
325 wrtwarning(const char *p
)
329 * Sensitive processes, somewhat arbitrarily defined here as setuid,
330 * setgid, root and wheel cannot afford to have malloc mistakes.
332 if (malloc_abort
|| issetugid() || getuid() == 0 || getgid() == 0)
337 * Allocate a number of pages from the OS
340 map_pages(size_t pages
)
342 caddr_t result
, rresult
, tail
;
343 intptr_t bytes
= pages
<< malloc_pageshift
;
345 if (bytes
< 0 || (size_t)bytes
< pages
) {
350 if ((result
= sbrk(bytes
)) == (void *)-1)
354 * Round to a page, in case sbrk(2) did not do this for us
356 rresult
= (caddr_t
)pageround((size_t)(uintptr_t)result
);
357 if (result
< rresult
) {
358 /* make sure we have enough space to fit bytes */
359 if (sbrk((intptr_t)(rresult
- result
)) == (void *) -1) {
360 /* we failed, put everything back */
362 wrterror("brk(2) failed [internal error]\n");
366 tail
= rresult
+ (size_t)bytes
;
368 last_idx
= ptr2idx(tail
) - 1;
371 if ((last_idx
+1) >= malloc_ninfo
&& !extend_pgdir(last_idx
)) {
373 last_idx
= ptr2idx(malloc_brk
) - 1;
374 /* Put back break point since we failed. */
376 wrterror("brk(2) failed [internal error]\n");
384 * Extend page directory
387 extend_pgdir(size_t idx
)
389 struct pginfo
**new, **old
;
390 size_t newlen
, oldlen
;
392 /* check for overflow */
393 if ((((~(1UL << ((sizeof(size_t) * NBBY
) - 1)) / sizeof(*page_dir
)) + 1)
394 + (malloc_pagesize
/ sizeof *page_dir
)) < idx
) {
399 /* Make it this many pages */
400 newlen
= pageround(idx
* sizeof *page_dir
) + malloc_pagesize
;
402 /* remember the old mapping size */
403 oldlen
= malloc_ninfo
* sizeof *page_dir
;
406 * NOTE: we allocate new pages and copy the directory rather than tempt
407 * fate by trying to "grow" the region.. There is nothing to prevent
408 * us from accidentally re-mapping space that's been allocated by our caller
409 * via dlopen() or other mmap().
411 * The copy problem is not too bad, as there is 4K of page index per
412 * 4MB of malloc arena.
414 * We can totally avoid the copy if we open a file descriptor to associate
415 * the anon mappings with. Then, when we remap the pages at the new
416 * address, the old pages will be "magically" remapped.. But this means
417 * keeping open a "secret" file descriptor.....
422 if (new == MAP_FAILED
)
425 /* Copy the old stuff */
426 memcpy(new, page_dir
, oldlen
);
428 /* register the new size */
429 malloc_ninfo
= newlen
/ sizeof *page_dir
;
431 /* swap the pointers */
435 /* Now free the old stuff */
441 * Initialize the world
450 int save_errno
= errno
;
453 * Compute page-size related variables.
455 malloc_pagesize
= (size_t)sysconf(_SC_PAGESIZE
);
456 malloc_pagemask
= malloc_pagesize
- 1;
457 for (malloc_pageshift
= 0;
458 (1UL << malloc_pageshift
) != malloc_pagesize
;
464 #ifdef MALLOC_EXTRA_SANITY
466 #endif /* MALLOC_EXTRA_SANITY */
468 for (i
= 0; i
< 3; i
++) {
470 j
= readlink("/etc/malloc.conf", b
, sizeof b
- 1);
475 } else if (i
== 1 && issetugid() == 0) {
476 p
= getenv("MALLOC_OPTIONS");
482 for (; p
!= NULL
&& *p
!= '\0'; p
++) {
484 case '>': malloc_cache
<<= 1; break;
485 case '<': malloc_cache
>>= 1; break;
486 case 'a': malloc_abort
= 0; break;
487 case 'A': malloc_abort
= 1; break;
488 case 'h': malloc_hint
= 0; break;
489 case 'H': malloc_hint
= 1; break;
490 case 'r': malloc_realloc
= 0; break;
491 case 'R': malloc_realloc
= 1; break;
492 case 'j': malloc_junk
= 0; break;
493 case 'J': malloc_junk
= 1; break;
495 case 'u': malloc_utrace
= 0; break;
496 case 'U': malloc_utrace
= 1; break;
498 case 'v': malloc_sysv
= 0; break;
499 case 'V': malloc_sysv
= 1; break;
500 case 'x': malloc_xmalloc
= 0; break;
501 case 'X': malloc_xmalloc
= 1; break;
502 case 'z': malloc_zero
= 0; break;
503 case 'Z': malloc_zero
= 1; break;
505 _malloc_message(getprogname(), malloc_func
,
506 " warning: ", "unknown char in MALLOC_OPTIONS\n");
515 * We want junk in the entire allocation, and zero only in the part
516 * the user asked for.
521 /* Allocate one page for the page directory */
522 page_dir
= MMAP(malloc_pagesize
);
524 if (page_dir
== MAP_FAILED
)
525 wrterror("mmap(2) failed, check limits.\n");
528 * We need a maximum of malloc_pageshift buckets, steal these from the
529 * front of the page_directory;
531 malloc_origo
= pageround((size_t)(uintptr_t)sbrk((intptr_t)0))
533 malloc_origo
-= malloc_pageshift
;
535 malloc_ninfo
= malloc_pagesize
/ sizeof *page_dir
;
537 /* Recalculate the cache size in bytes, and make sure it's nonzero */
542 malloc_cache
<<= malloc_pageshift
;
545 * This is a nice hack from Kaleb Keithly (kaleb@x.org).
546 * We can sbrk(2) further back when we keep this on a low address.
548 px
= imalloc(sizeof *px
);
554 * Allocate a number of complete pages
557 malloc_pages(size_t size
)
559 void *p
, *delay_free
= NULL
;
564 idx
= pageround(size
);
573 /* Look for free pages before asking for more */
574 for(pf
= free_list
.next
; pf
; pf
= pf
->next
) {
576 #ifdef MALLOC_EXTRA_SANITY
577 if (pf
->size
& malloc_pagemask
)
578 wrterror("(ES): junk length entry on free_list.\n");
580 wrterror("(ES): zero length entry on free_list.\n");
581 if (pf
->page
== pf
->end
)
582 wrterror("(ES): zero entry on free_list.\n");
583 if (pf
->page
> pf
->end
)
584 wrterror("(ES): sick entry on free_list.\n");
585 if ((void*)pf
->page
>= (void*)sbrk(0))
586 wrterror("(ES): entry on free_list past brk.\n");
587 if (page_dir
[ptr2idx(pf
->page
)] != MALLOC_FREE
)
588 wrterror("(ES): non-free first page on free-list.\n");
589 if (page_dir
[ptr2idx(pf
->end
)-1] != MALLOC_FREE
)
590 wrterror("(ES): non-free last page on free-list.\n");
591 #endif /* MALLOC_EXTRA_SANITY */
596 if (pf
->size
== size
) {
598 if (pf
->next
!= NULL
)
599 pf
->next
->prev
= pf
->prev
;
600 pf
->prev
->next
= pf
->next
;
606 pf
->page
= (char *)pf
->page
+ size
;
611 #ifdef MALLOC_EXTRA_SANITY
612 if (p
!= NULL
&& page_dir
[ptr2idx(p
)] != MALLOC_FREE
)
613 wrterror("(ES): allocated non-free page on free-list.\n");
614 #endif /* MALLOC_EXTRA_SANITY */
616 size
>>= malloc_pageshift
;
625 page_dir
[idx
] = MALLOC_FIRST
;
627 page_dir
[idx
+i
] = MALLOC_FOLLOW
;
630 memset(p
, SOME_JUNK
, size
<< malloc_pageshift
);
644 * Allocate a page of fragments
648 malloc_make_chunks(int bits
)
655 /* Allocate a new bucket */
656 pp
= malloc_pages(malloc_pagesize
);
660 /* Find length of admin structure */
661 l
= (long)offsetof(struct pginfo
, bits
[0]);
662 l
+= (long)sizeof bp
->bits
[0] *
663 (((malloc_pagesize
>> bits
)+MALLOC_BITS
-1) / MALLOC_BITS
);
665 /* Don't waste more than two chunks on this */
666 if ((1<<(bits
)) <= l
+l
) {
667 bp
= (struct pginfo
*)pp
;
669 bp
= imalloc((size_t)l
);
676 bp
->size
= (1<<bits
);
678 bp
->total
= bp
->free
= (u_short
)(malloc_pagesize
>> bits
);
681 /* set all valid bits in the bitmap */
685 /* Do a bunch at a time */
686 for(;k
-i
>= MALLOC_BITS
; i
+= MALLOC_BITS
)
687 bp
->bits
[i
/ MALLOC_BITS
] = ~0U;
690 bp
->bits
[i
/MALLOC_BITS
] |= 1<<(i
%MALLOC_BITS
);
692 if (bp
== bp
->page
) {
693 /* Mark the ones we stole for ourselves */
694 for(i
= 0; l
> 0; i
++) {
695 bp
->bits
[i
/ MALLOC_BITS
] &= ~(1 << (i
% MALLOC_BITS
));
698 l
-= (long)(1 << bits
);
704 page_dir
[ptr2idx(pp
)] = bp
;
706 bp
->next
= page_dir
[bits
];
715 * Allocate a fragment
718 malloc_bytes(size_t size
)
727 /* Don't bother with anything less than this */
728 if (size
< malloc_minsize
)
729 size
= malloc_minsize
;
732 /* Find the right bucket */
738 /* If it's empty, make a page more of that size chunks */
739 if (page_dir
[j
] == NULL
&& !malloc_make_chunks(j
))
744 /* Find first word of bitmap which isn't empty */
745 for (lp
= bp
->bits
; !*lp
; lp
++)
748 /* Find that bit, and tweak it */
757 /* If there are no more free, remove from free-list */
759 page_dir
[j
] = bp
->next
;
763 /* Adjust to the real offset of that chunk */
764 k
+= (lp
-bp
->bits
)*MALLOC_BITS
;
768 memset((u_char
*)bp
->page
+ k
, SOME_JUNK
, (size_t)bp
->size
);
770 return (u_char
*)bp
->page
+ k
;
774 * Allocate a piece of memory
784 if ((size
+ malloc_pagesize
) < size
) /* Check for overflow */
786 else if ((size
+ malloc_pagesize
) >= (uintptr_t)page_dir
)
788 else if (size
<= malloc_maxsize
)
789 result
= malloc_bytes(size
);
791 result
= malloc_pages(size
);
793 if (malloc_abort
&& result
== NULL
)
794 wrterror("allocation failed.\n");
796 if (malloc_zero
&& result
!= NULL
)
797 memset(result
, 0, size
);
803 * Change the size of an allocation.
806 irealloc(void *ptr
, size_t size
)
818 if (idx
< malloc_pageshift
) {
819 wrtwarning("junk pointer, too low to make sense.\n");
823 if (idx
> last_idx
) {
824 wrtwarning("junk pointer, too high to make sense.\n");
830 if (*mp
== MALLOC_FIRST
) { /* Page allocation */
832 /* Check the pointer */
833 if ((size_t)(uintptr_t)ptr
& malloc_pagemask
) {
834 wrtwarning("modified (page-) pointer.\n");
838 /* Find the size in bytes */
839 for (osize
= malloc_pagesize
; *++mp
== MALLOC_FOLLOW
;)
840 osize
+= malloc_pagesize
;
842 if (!malloc_realloc
&& /* unless we have to, */
843 size
<= osize
&& /* .. or are too small, */
844 size
> (osize
- malloc_pagesize
)) { /* .. or can free a page, */
846 memset((u_char
*)ptr
+ size
, SOME_JUNK
, osize
-size
);
847 return ptr
; /* don't do anything. */
850 } else if (*mp
>= MALLOC_MAGIC
) { /* Chunk allocation */
852 /* Check the pointer for sane values */
853 if (((size_t)(uintptr_t)ptr
& ((*mp
)->size
-1))) {
854 wrtwarning("modified (chunk-) pointer.\n");
858 /* Find the chunk index in the page */
859 i
= ((size_t)(uintptr_t)ptr
& malloc_pagemask
) >> (*mp
)->shift
;
861 /* Verify that it isn't a free chunk already */
862 if ((*mp
)->bits
[i
/MALLOC_BITS
] & (1UL << (i
% MALLOC_BITS
))) {
863 wrtwarning("chunk is already free.\n");
869 if (!malloc_realloc
&& /* Unless we have to, */
870 size
<= osize
&& /* ..or are too small, */
871 (size
> osize
/ 2 || /* ..or could use a smaller size, */
872 osize
== malloc_minsize
)) { /* ..(if there is one) */
874 memset((u_char
*)ptr
+ size
, SOME_JUNK
, osize
-size
);
875 return ptr
; /* ..Don't do anything */
879 wrtwarning("pointer to wrong page.\n");
886 /* copy the lesser of the two sizes, and free the old one */
889 else if (osize
< size
)
890 memcpy(p
, ptr
, osize
);
892 memcpy(p
, ptr
, size
);
899 * Free a sequence of pages
903 free_pages(void *ptr
, size_t idx
, struct pginfo
*info
)
906 struct pgfree
*pf
, *pt
=NULL
;
910 if (info
== MALLOC_FREE
) {
911 wrtwarning("page is already free.\n");
915 if (info
!= MALLOC_FIRST
) {
916 wrtwarning("pointer to wrong page.\n");
920 if ((size_t)(uintptr_t)ptr
& malloc_pagemask
) {
921 wrtwarning("modified (page-) pointer.\n");
925 /* Count how many pages and mark them free at the same time */
926 page_dir
[idx
] = MALLOC_FREE
;
927 for (i
= 1; page_dir
[idx
+i
] == MALLOC_FOLLOW
; i
++)
928 page_dir
[idx
+ i
] = MALLOC_FREE
;
930 l
= i
<< malloc_pageshift
;
933 memset(ptr
, SOME_JUNK
, l
);
936 madvise(ptr
, l
, MADV_FREE
);
938 tail
= (char *)ptr
+l
;
940 /* add to free-list */
942 px
= imalloc(sizeof *px
); /* This cannot fail... */
946 if (free_list
.next
== NULL
) {
948 /* Nothing on free list, put this at head */
949 px
->next
= free_list
.next
;
950 px
->prev
= &free_list
;
957 /* Find the right spot, leave pf pointing to the modified entry. */
958 tail
= (char *)ptr
+l
;
960 for(pf
= free_list
.next
; pf
->end
< ptr
&& pf
->next
!= NULL
;
962 ; /* Race ahead here */
964 if (pf
->page
> tail
) {
965 /* Insert before entry */
972 } else if (pf
->end
== ptr
) {
973 /* Append to the previous entry */
974 pf
->end
= (char *)pf
->end
+ l
;
976 if (pf
->next
!= NULL
&& pf
->end
== pf
->next
->page
) {
977 /* And collapse the next too. */
980 pf
->size
+= pt
->size
;
982 if (pf
->next
!= NULL
)
985 } else if (pf
->page
== tail
) {
986 /* Prepend to entry */
989 } else if (pf
->next
== NULL
) {
990 /* Append at tail of chain */
997 wrterror("freelist is destroyed.\n");
1001 /* Return something to OS ? */
1002 if (pf
->next
== NULL
&& /* If we're the last one, */
1003 pf
->size
> malloc_cache
&& /* ..and the cache is full, */
1004 pf
->end
== malloc_brk
&& /* ..and none behind us, */
1005 malloc_brk
== sbrk((intptr_t)0)) { /* ..and it's OK to do... */
1008 * Keep the cache intact. Notice that the '>' above guarantees that
1009 * the pf will always have at least one page afterwards.
1011 pf
->end
= (char *)pf
->page
+ malloc_cache
;
1012 pf
->size
= malloc_cache
;
1015 malloc_brk
= pf
->end
;
1017 idx
= ptr2idx(pf
->end
);
1019 for(i
=idx
;i
<= last_idx
;)
1020 page_dir
[i
++] = MALLOC_NOT_MINE
;
1024 /* XXX: We could realloc/shrink the pagedir here I guess. */
1031 * Free a chunk, and possibly the page it's on, if the page becomes empty.
1035 free_bytes(void *ptr
, size_t idx
, struct pginfo
*info
)
1041 /* Find the chunk number on the page */
1042 i
= ((size_t)(uintptr_t)ptr
& malloc_pagemask
) >> info
->shift
;
1044 if (((size_t)(uintptr_t)ptr
& (info
->size
-1))) {
1045 wrtwarning("modified (chunk-) pointer.\n");
1049 if (info
->bits
[i
/MALLOC_BITS
] & (1UL << (i
% MALLOC_BITS
))) {
1050 wrtwarning("chunk is already free.\n");
1055 memset(ptr
, SOME_JUNK
, (size_t)info
->size
);
1057 info
->bits
[i
/MALLOC_BITS
] |= (u_int
)(1UL << (i
% MALLOC_BITS
));
1060 mp
= page_dir
+ info
->shift
;
1062 if (info
->free
== 1) {
1064 /* Page became non-full */
1066 mp
= page_dir
+ info
->shift
;
1067 /* Insert in address order */
1068 while (*mp
&& (*mp
)->next
&& (*mp
)->next
->page
< info
->page
)
1075 if (info
->free
!= info
->total
)
1078 /* Find & remove this page in the queue */
1079 while (*mp
!= info
) {
1080 mp
= &((*mp
)->next
);
1081 #ifdef MALLOC_EXTRA_SANITY
1083 wrterror("(ES): Not on queue.\n");
1084 #endif /* MALLOC_EXTRA_SANITY */
1088 /* Free the page & the info structure if need be */
1089 page_dir
[idx
] = MALLOC_FIRST
;
1090 vp
= info
->page
; /* Order is important ! */
1091 if(vp
!= (void*)info
)
1099 struct pginfo
*info
;
1106 /* If we're already sinking, don't make matters any worse. */
1112 if (idx
< malloc_pageshift
) {
1113 wrtwarning("junk pointer, too low to make sense.\n");
1117 if (idx
> last_idx
) {
1118 wrtwarning("junk pointer, too high to make sense.\n");
1122 info
= page_dir
[idx
];
1124 if (info
< MALLOC_MAGIC
)
1125 free_pages(ptr
, idx
, info
);
1127 free_bytes(ptr
, idx
, info
);
1131 static int malloc_active
; /* Recusion flag for public interface. */
1132 static unsigned malloc_started
; /* Set when initialization has been done */
1135 pubrealloc(void *ptr
, size_t size
, const char *func
)
1141 * If a thread is inside our code with a functional lock held, and then
1142 * catches a signal which calls us again, we would get a deadlock if the
1143 * lock is not of a recursive type.
1147 if (malloc_active
> 0) {
1148 if (malloc_active
== 1) {
1149 wrtwarning("recursive call\n");
1158 if (!malloc_started
) {
1160 wrtwarning("malloc() has never been called\n");
1170 if (ptr
== ZEROSIZEPTR
)
1172 if (malloc_sysv
&& !size
) {
1180 } else if (ptr
== NULL
) {
1184 r
= irealloc(ptr
, size
);
1187 UTRACE(ptr
, size
, r
);
1190 if (malloc_xmalloc
&& err
)
1191 wrterror("out of memory\n");
1198 * These are the public exported interface routines.
1205 return pubrealloc(NULL
, size
, " in malloc():");
1209 posix_memalign(void **memptr
, size_t alignment
, size_t size
)
1214 if (!malloc_started
) {
1218 /* Make sure that alignment is a large enough power of 2. */
1219 if (((alignment
- 1) & alignment
) != 0 || alignment
< sizeof(void *) ||
1220 alignment
> malloc_pagesize
)
1224 * (size | alignment) is enough to assure the requested alignment, since
1225 * the allocator always allocates power-of-two blocks.
1227 err
= errno
; /* Protect errno against changes in pubrealloc(). */
1228 result
= pubrealloc(NULL
, (size
| alignment
), " in posix_memalign()");
1239 calloc(size_t num
, size_t size
)
1243 if (size
!= 0 && (num
* size
) / size
!= num
) {
1244 /* size_t overflow. */
1249 ret
= pubrealloc(NULL
, num
* size
, " in calloc():");
1252 memset(ret
, 0, num
* size
);
1261 pubrealloc(ptr
, 0, " in free():");
1265 realloc(void *ptr
, size_t size
)
1268 return pubrealloc(ptr
, size
, " in realloc():");
1272 * Begin library-private functions, used by threading libraries for protection
1273 * of malloc during fork(). These functions are only called if the program is
1274 * running in threaded mode, so there is no need to check whether the program
1279 _malloc_prefork(void)
1286 _malloc_postfork(void)