2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
11 #include "mm/memory.h"
14 static int printf(const char *fmt
, ...)
16 int *args
= (int*)&fmt
+ 1;
17 kePrintList(fmt
, &args
);
22 * Defining MALLOC_EXTRA_SANITY will enable extra checks which are related
23 * to internal conditions and consistency in malloc.c. This has a
24 * noticeable runtime performance hit, and generally will not do you
25 * any good unless you fiddle with the internals of malloc or want
26 * to catch random pointer corruption as early as possible.
28 #ifndef MALLOC_EXTRA_SANITY
29 #define MALLOC_EXTRA_SANITY
33 * What to use for Junk. This is the byte value we use to fill with
34 * when the 'J' option is enabled.
36 #define SOME_JUNK 0xd0 /* as in "Duh" :-) */
37 #define SOME_FREE_JUNK 0xf0
40 * The basic parameters you can tweak.
42 * malloc_minsize minimum size of an allocation in bytes.
43 * If this is too small it's too much work
44 * to manage them. This is also the smallest
45 * unit of alignment used for the storage
46 * returned by malloc/realloc.
50 /* Insert your combination here... */
52 /*#if defined(__FOOCPU__) && defined(__BAROS__)
53 # define malloc_minsize 16U
54 #endif*/ /* __FOOCPU__ && __BAROS__ */
59 void* mem_allocate(uint32_t size
, uint32_t flags
)
61 uintptr_t vaddr
= mmFindFreeKernelPages(MM_MAX_KERNEL_PAGE
,
62 MM_MIN_KERNEL_PAGE
, 0, (size
+ 0xFFF) & ~0xFFF);
65 for (i
= 0; i
< (size
+ 0xFFF) / 0x1000; i
++)
67 uintptr_t paddr
= mmAllocPhysicalMemory(0, 0, 0x1000);
69 mmMapKernelMemory(paddr
, vaddr
+ i
* 0x1000, MM_MAP_READ
| MM_MAP_WRITE
);
73 void mem_free(void *addr
, uint32_t size
)
76 for (i
= 0; i
< (size
+ 0xFFF) / 0x1000; i
++)
78 uintptr_t vaddr
= (uintptr_t)addr
+ i
* 0x1000;
79 uintptr_t paddr
= mmKernelGetPhysAddress(vaddr
);
81 mmFreePhysicalMemory(paddr
, 0x1000);
82 mmMapKernelMemory(paddr
, vaddr
, MM_MAP_READ
| MM_MAP_WRITE
);
88 #define MAP_FAILED NULL
89 #define LOST_MALLOC_PAGESIZE 4096
92 * No user serviceable parts behind this point.
96 * This structure describes a page worth of chunks.
100 struct pginfo
*next
; /* next on the free list */
101 void *page
; /* Pointer to the page */
102 u_short size
; /* size of this page's chunks */
103 u_short shift
; /* How far to shift for this size chunks */
104 u_short free
; /* How many free chunks */
105 u_short total
; /* How many chunk */
106 u_int bits
[1]; /* Which chunks are free */
110 * This structure describes a number of free pages.
114 struct pgfree
*next
; /* next run of free pages */
115 struct pgfree
*prev
; /* prev run of free pages */
116 void *page
; /* pointer to free pages */
117 void *end
; /* pointer to end of free pages */
118 size_t size
; /* number of bytes free */
122 * How many bits per u_int in the bitmap.
123 * Change only if not 8 bits/byte
125 #define MALLOC_BITS ((int)(8*sizeof(u_int)))
128 * Magic values to put in the page_directory
130 #define MALLOC_NOT_MINE ((struct pginfo*) 0)
131 #define MALLOC_FREE ((struct pginfo*) 1)
132 #define MALLOC_FIRST ((struct pginfo*) 2)
133 #define MALLOC_FOLLOW ((struct pginfo*) 3)
134 #define MALLOC_MAGIC ((struct pginfo*) 4)
137 * Page size related parameters, computed at run-time.
139 static size_t malloc_pagesize
;
140 static size_t malloc_pageshift
;
141 static size_t malloc_pagemask
;
143 #ifndef malloc_minsize
144 #define malloc_minsize 16U
147 #ifndef malloc_maxsize
148 #define malloc_maxsize ((malloc_pagesize)>>1)
151 #define pageround(foo) (((foo) + (malloc_pagemask))&(~(malloc_pagemask)))
152 #define ptr2idx(foo) \
153 (((size_t)(uintptr_t)(foo) >> malloc_pageshift)-malloc_origo)
156 #define THREAD_LOCK()
159 #ifndef THREAD_UNLOCK
160 #define THREAD_UNLOCK()
172 #define MADV_FREE MADV_DONTNEED
175 /* Set when initialization has been done */
176 static unsigned malloc_started
= 0;
178 /* Recusion flag for public interface. */
179 static int malloc_active
= 0;
181 /* Number of free pages we cache */
182 static unsigned malloc_cache
= 16;
184 /* The offset from pagenumber to index into the page directory */
185 static size_t malloc_origo
;
187 /* The last index in the page directory we care about */
188 static size_t last_idx
;
190 /* Pointer to page directory. Allocated "as if with" malloc */
191 static struct pginfo
**page_dir
;
193 /* How many slots in the page directory */
194 static unsigned malloc_ninfo
;
196 /* Free pages line up here */
197 static struct pgfree free_list
;
199 /* Abort(), user doesn't handle problems. */
200 static int malloc_abort
;
202 /* Are we trying to die ? */
205 /* always realloc ? */
206 static int malloc_realloc
;
208 /* pass the kernel a hint on free pages ? */
209 static int malloc_hint
= 0;
211 /* xmalloc behaviour ? */
212 static int malloc_xmalloc
;
214 /* sysv behaviour for malloc(0) ? */
215 static int malloc_sysv
;
218 static int malloc_zero
;
221 static int malloc_junk
;
226 static int malloc_utrace
;
228 struct ut
{ void *p
; size_t s
; void *r
; };
230 #define UTRACE(a, b, c) \
231 if (malloc_utrace) { \
233 u.p=a; u.s = b; u.r=c; \
234 utrace(UTRACE_LABEL (void *) &u, sizeof u); \
236 #else /* !HAS_UTRACE */
237 #define UTRACE(a,b,c)
238 #endif /* HAS_UTRACE */
241 static void *malloc_brk
;
243 /* one location cache for free-list holders */
244 static struct pgfree
*px
;
246 /* compile-time options */
247 static char *malloc_options
= "";
249 /* Name of the current public function */
250 static const char *malloc_func
;
254 memset(mem_allocate(size, 0), 0, size)
256 /*#define sbrk(size) \
257 mem_allocate(size, 0)*/
259 #define munmap(start, size) mem_free(start, size)
261 /* mmap(0, (size), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, \
262 MMAP_FD, (off_t)0);*/
265 static int brk(void* end_data_segment) {
270 void init_memory_manager() {}
274 * Necessary function declarations
276 static int extend_pgdir(size_t idx
);
277 static void *imalloc(size_t size
);
278 static void ifree(void *ptr
);
279 static void *irealloc(void *ptr
, size_t size
);
282 wrterror(const char *p
)
284 // const char *progname = getprogname();
285 const char *q
= " error: ";
287 // printf("%s", progname);
288 printf("%s", malloc_func
);
297 wrtwarning(const char *p
)
300 const char *progname = getprogname();
301 const char *q = " warning: ";
306 write(STDERR_FILENO, progname, strlen(progname));
307 write(STDERR_FILENO, malloc_func, strlen(malloc_func));
308 write(STDERR_FILENO, q, strlen(q));
309 write(STDERR_FILENO, p, strlen(p));
314 * Allocate a number of pages from the OS
317 map_pages(size_t pages
)
319 caddr_t rresult
, tail
;
321 rresult
= mem_allocate(pages
<< malloc_pageshift
, 0);
322 if (rresult
== NULL
) {
326 intptr_t bytes
= pages
<< malloc_pageshift
;
328 tail
= rresult
+ (size_t)bytes
;
329 last_idx
= ptr2idx(tail
) - 1;
332 //printf("[origo = %x, tail = %x, last_idx = %x, rresult = %x]", malloc_origo, tail, last_idx, rresult);
334 if ((last_idx
+1) >= malloc_ninfo
&& !extend_pgdir(last_idx
)) {
336 malloc_brk
= rresult
;
337 last_idx
= ptr2idx(malloc_brk
) - 1;
339 /* FIXME: Put back break point since we failed. */
340 /*if (brk(malloc_brk)) {
341 wrterror("[b] brk(2) failed [internal error]\n");
351 caddr_t result
, rresult
, tail
;
352 intptr_t bytes
= pages
<< malloc_pageshift
;
354 if (bytes
< 0 || (size_t)bytes
< pages
) {
359 if ((result
= sbrk(bytes
)) == (void *)-1)
363 * Round to a page, in case sbrk(2) did not do this for us
365 rresult
= (caddr_t
)pageround((size_t)(uintptr_t)result
);
366 if (result
< rresult
) {
367 /* make sure we have enough space to fit bytes */
368 if (sbrk((intptr_t)(rresult
- result
)) == (void *) -1) {
369 /* we failed, put everything back */
370 // FIXME Speicher wirklich zurückgeben
371 /* if (brk(result)) {
372 wrterror("brk(2) failed [internal error]\n");
376 tail
= rresult
+ (size_t)bytes
;
378 last_idx
= ptr2idx(tail
) - 1;
381 if ((last_idx
+1) >= malloc_ninfo
&& !extend_pgdir(last_idx
)) {
383 last_idx
= ptr2idx(malloc_brk
) - 1;
384 /* Put back break point since we failed. */
386 wrterror("[b] brk(2) failed [internal error]\n");
395 * Extend page directory
398 extend_pgdir(size_t idx
)
400 struct pginfo
**new, **old
;
401 size_t newlen
, oldlen
;
403 printf("extend_pgdir %x", idx
);
405 /* check for overflow */
411 ((sizeof(size_t) * NBBY
) - 1)
418 malloc_pagesize
/ sizeof *page_dir
428 /* Make it this many pages */
429 newlen
= pageround(idx
* sizeof *page_dir
) + malloc_pagesize
;
431 /* remember the old mapping size */
432 oldlen
= malloc_ninfo
* sizeof *page_dir
;
435 * NOTE: we allocate new pages and copy the directory rather than tempt
436 * fate by trying to "grow" the region.. There is nothing to prevent
437 * us from accidentally re-mapping space that's been allocated by our caller
438 * via dlopen() or other mmap().
440 * The copy problem is not too bad, as there is 4K of page index per
441 * 4MB of malloc arena.
443 * We can totally avoid the copy if we open a file descriptor to associate
444 * the anon mappings with. Then, when we remap the pages at the new
445 * address, the old pages will be "magically" remapped.. But this means
446 * keeping open a "secret" file descriptor.....
450 new = (struct pginfo
**) MMAP(newlen
);
451 if (new == MAP_FAILED
)
456 /* Copy the old stuff */
457 memcpy(new, page_dir
, oldlen
);
461 /* register the new size */
462 malloc_ninfo
= newlen
/ sizeof *page_dir
;
464 /* swap the pointers */
468 /* Now free the old stuff */
476 * Initialize the world
486 * Compute page-size related variables.
488 malloc_pagesize
= LOST_MALLOC_PAGESIZE
;
489 malloc_pagemask
= malloc_pagesize
- 1;
490 for (malloc_pageshift
= 0;
491 (1UL << malloc_pageshift
) != malloc_pagesize
;
497 #ifdef MALLOC_EXTRA_SANITY
499 #endif /* MALLOC_EXTRA_SANITY */
501 for (i
= 0; i
< 3; i
++) {
504 j
= 0; //readlink("/etc/malloc.conf", b, sizeof b - 1);
510 } /*else if (i == 1) {
511 p = getenv("MALLOC_OPTIONS");
515 for (; p
!= NULL
&& *p
!= '\0'; p
++) {
517 case '>': malloc_cache
<<= 1; break;
518 case '<': malloc_cache
>>= 1; break;
519 case 'a': malloc_abort
= 0; break;
520 case 'A': malloc_abort
= 1; break;
521 case 'h': malloc_hint
= 0; break;
522 case 'H': malloc_hint
= 1; break;
523 case 'r': malloc_realloc
= 0; break;
524 case 'R': malloc_realloc
= 1; break;
525 case 'j': malloc_junk
= 0; break;
526 case 'J': malloc_junk
= 1; break;
528 case 'u': malloc_utrace
= 0; break;
529 case 'U': malloc_utrace
= 1; break;
531 case 'v': malloc_sysv
= 0; break;
532 case 'V': malloc_sysv
= 1; break;
533 case 'x': malloc_xmalloc
= 0; break;
534 case 'X': malloc_xmalloc
= 1; break;
535 case 'z': malloc_zero
= 0; break;
536 case 'Z': malloc_zero
= 1; break;
540 wrtwarning("unknown char in MALLOC_OPTIONS.\n");
550 * We want junk in the entire allocation, and zero only in the part
551 * the user asked for.
557 * If we run with junk (or implicitly from above: zero), we want to
558 * force realloc() to get new storage, so we can DTRT with it.
563 /* Allocate one page for the page directory */
564 page_dir
= (struct pginfo
**) MMAP(malloc_pagesize
);
566 if (page_dir
== MAP_FAILED
)
567 wrterror("mmap(2) failed, check limits.\n");
570 * We need a maximum of malloc_pageshift buckets, steal these from the
571 * front of the page_directory;
573 //malloc_origo = malloc_pageshift;
574 malloc_origo
= ((uint32_t) page_dir
>> malloc_pageshift
) - malloc_pageshift
;
577 pageround((size_t)(uintptr_t)sbrk((intptr_t)0))
579 malloc_origo -= malloc_pageshift;
582 malloc_ninfo
= malloc_pagesize
/ sizeof *page_dir
;
584 /* Recalculate the cache size in bytes, and make sure it's nonzero */
589 malloc_cache
<<= malloc_pageshift
;
592 * This is a nice hack from Kaleb Keithly (kaleb@x.org).
593 * We can sbrk(2) further back when we keep this on a low address.
595 px
= (struct pgfree
*) imalloc (sizeof *px
);
597 /* Been here, done that */
602 * Allocate a number of complete pages
605 malloc_pages(size_t size
)
607 void *p
, *delay_free
= NULL
;
612 //printf("malloc_pages(%d)\n", size);
614 idx
= pageround(size
);
623 /* Look for free pages before asking for more */
624 for(pf
= free_list
.next
; pf
; pf
= pf
->next
) {
626 #ifdef MALLOC_EXTRA_SANITY
627 if (pf
->size
& malloc_pagemask
)
628 wrterror("(ES): junk length entry on free_list.\n");
630 wrterror("(ES): zero length entry on free_list.\n");
631 if (pf
->page
== pf
->end
)
632 wrterror("(ES): zero entry on free_list.\n");
633 if (pf
->page
> pf
->end
)
634 wrterror("(ES): sick entry on free_list.\n");
635 /* if ((void*)pf->page >= (void*)sbrk(0))
636 wrterror("(ES): entry on free_list past brk.\n");*/
637 if (page_dir
[ptr2idx(pf
->page
)] != MALLOC_FREE
)
638 wrterror("(ES): non-free first page on free-list.\n");
639 if (page_dir
[ptr2idx(pf
->end
)-1] != MALLOC_FREE
)
640 wrterror("(ES): non-free last page on free-list.\n");
641 #endif /* MALLOC_EXTRA_SANITY */
646 if (pf
->size
== size
) {
648 if (pf
->next
!= NULL
)
649 pf
->next
->prev
= pf
->prev
;
650 pf
->prev
->next
= pf
->next
;
656 pf
->page
= (char *)pf
->page
+ size
;
661 #ifdef MALLOC_EXTRA_SANITY
662 if (p
!= NULL
&& page_dir
[ptr2idx(p
)] != MALLOC_FREE
)
663 wrterror("(ES): allocated non-free page on free-list.\n");
664 #endif /* MALLOC_EXTRA_SANITY */
666 size
>>= malloc_pageshift
;
675 page_dir
[idx
] = MALLOC_FIRST
;
677 page_dir
[idx
+i
] = MALLOC_FOLLOW
;
680 memset(p
, SOME_JUNK
, size
<< malloc_pageshift
);
694 * Allocate a page of fragments
698 malloc_make_chunks(int bits
)
704 /* Allocate a new bucket */
705 pp
= malloc_pages(malloc_pagesize
);
709 /* Find length of admin structure */
710 l
= (int)offsetof(struct pginfo
, bits
[0]);
711 l
+= sizeof bp
->bits
[0] *
712 (((malloc_pagesize
>> bits
)+MALLOC_BITS
-1) / MALLOC_BITS
);
714 /* Don't waste more than two chunks on this */
715 if ((1<<(bits
)) <= l
+l
) {
716 bp
= (struct pginfo
*)pp
;
718 bp
= (struct pginfo
*)imalloc((size_t)l
);
725 bp
->size
= (1<<bits
);
727 bp
->total
= bp
->free
= malloc_pagesize
>> bits
;
730 /* set all valid bits in the bitmap */
734 /* Do a bunch at a time */
735 for(;k
-i
>= MALLOC_BITS
; i
+= MALLOC_BITS
)
736 bp
->bits
[i
/ MALLOC_BITS
] = ~0U;
739 bp
->bits
[i
/MALLOC_BITS
] |= 1<<(i
%MALLOC_BITS
);
741 if (bp
== bp
->page
) {
742 /* Mark the ones we stole for ourselves */
744 bp
->bits
[i
/MALLOC_BITS
] &= ~(1<<(i
%MALLOC_BITS
));
753 page_dir
[ptr2idx(pp
)] = bp
;
755 bp
->next
= page_dir
[bits
];
764 * Allocate a fragment
767 malloc_bytes(size_t size
)
776 //printf("malloc_bytes(%d)\n", size);
778 /* Don't bother with anything less than this */
779 if (size
< malloc_minsize
)
780 size
= malloc_minsize
;
782 /* Find the right bucket */
788 /* If it's empty, make a page more of that size chunks */
789 if (page_dir
[j
] == NULL
&& !malloc_make_chunks(j
))
794 /* Find first word of bitmap which isn't empty */
795 for (lp
= bp
->bits
; !*lp
; lp
++)
798 /* Find that bit, and tweak it */
807 /* If there are no more free, remove from free-list */
809 page_dir
[j
] = bp
->next
;
813 /* Adjust to the real offset of that chunk */
814 k
+= (lp
-bp
->bits
)*MALLOC_BITS
;
818 memset((u_char
*)bp
->page
+ k
, SOME_JUNK
, (size_t)bp
->size
);
820 return (u_char
*)bp
->page
+ k
;
824 * Allocate a piece of memory
834 if ((size
+ malloc_pagesize
) < size
) /* Check for overflow */
836 else if (size
<= malloc_maxsize
)
837 result
= malloc_bytes(size
);
839 result
= malloc_pages(size
);
841 if (malloc_abort
&& result
== NULL
)
842 wrterror("allocation failed.\n");
844 if (malloc_zero
&& result
!= NULL
)
845 memset(result
, 0, size
);
851 * Change the size of an allocation.
854 irealloc(void *ptr
, size_t size
)
866 if (idx
< malloc_pageshift
) {
867 wrtwarning("junk pointer, too low to make sense.\n");
871 if (idx
> last_idx
) {
872 wrtwarning("junk pointer, too high to make sense.\n");
878 if (*mp
== MALLOC_FIRST
) { /* Page allocation */
880 /* Check the pointer */
881 if ((size_t)(uintptr_t)ptr
& malloc_pagemask
) {
882 wrtwarning("modified (page-) pointer.\n");
886 /* Find the size in bytes */
887 for (osize
= malloc_pagesize
; *++mp
== MALLOC_FOLLOW
;)
888 osize
+= malloc_pagesize
;
890 if (!malloc_realloc
&& /* unless we have to, */
891 size
<= osize
&& /* .. or are too small, */
892 size
> (osize
- malloc_pagesize
)) { /* .. or can free a page, */
893 return ptr
; /* don't do anything. */
896 } else if (*mp
>= MALLOC_MAGIC
) { /* Chunk allocation */
898 /* Check the pointer for sane values */
899 if (((size_t)(uintptr_t)ptr
& ((*mp
)->size
-1))) {
900 wrtwarning("modified (chunk-) pointer.\n");
904 /* Find the chunk index in the page */
905 i
= ((size_t)(uintptr_t)ptr
& malloc_pagemask
) >> (*mp
)->shift
;
907 /* Verify that it isn't a free chunk already */
908 if ((*mp
)->bits
[i
/MALLOC_BITS
] & (1<<(i
%MALLOC_BITS
))) {
909 wrtwarning("chunk is already free.\n");
915 if (!malloc_realloc
&& /* Unless we have to, */
916 size
< osize
&& /* ..or are too small, */
917 (size
> osize
/2 || /* ..or could use a smaller size, */
918 osize
== malloc_minsize
)) { /* ..(if there is one) */
919 return ptr
; /* ..Don't do anything */
923 wrtwarning("pointer to wrong page.\n");
930 /* copy the lesser of the two sizes, and free the old one */
933 else if (osize
< size
)
934 memcpy(p
, ptr
, osize
);
936 memcpy(p
, ptr
, size
);
943 * Free a sequence of pages
947 free_pages(void *ptr
, size_t idx
, struct pginfo
*info
)
950 struct pgfree
*pf
, *pt
=NULL
;
954 if (info
== MALLOC_FREE
) {
955 wrtwarning("page is already free.\n");
959 if (info
!= MALLOC_FIRST
) {
960 wrtwarning("pointer to wrong page.\n");
964 if ((size_t)(uintptr_t)ptr
& malloc_pagemask
) {
965 wrtwarning("modified (page-) pointer.\n");
969 /* Count how many pages and mark them free at the same time */
970 page_dir
[idx
] = MALLOC_FREE
;
971 for (i
= 1; page_dir
[idx
+i
] == MALLOC_FOLLOW
; i
++)
972 page_dir
[idx
+ i
] = MALLOC_FREE
;
974 l
= i
<< malloc_pageshift
;
977 memset(ptr
, SOME_FREE_JUNK
, l
);
980 madvise(ptr, l, MADV_FREE);*/
982 tail
= (char *)ptr
+l
;
984 /* add to free-list */
986 px
= imalloc(sizeof *pt
); /* This cannot fail... */
990 if (free_list
.next
== NULL
) {
992 /* Nothing on free list, put this at head */
993 px
->next
= free_list
.next
;
994 px
->prev
= &free_list
;
1001 /* Find the right spot, leave pf pointing to the modified entry. */
1002 tail
= (char *)ptr
+l
;
1004 for(pf
= free_list
.next
; pf
->end
< ptr
&& pf
->next
!= NULL
;
1006 ; /* Race ahead here */
1008 if (pf
->page
> tail
) {
1009 /* Insert before entry */
1011 px
->prev
= pf
->prev
;
1013 px
->prev
->next
= px
;
1016 } else if (pf
->end
== ptr
) {
1017 /* Append to the previous entry */
1018 pf
->end
= (char *)pf
->end
+ l
;
1020 if (pf
->next
!= NULL
&& pf
->end
== pf
->next
->page
) {
1021 /* And collapse the next too. */
1024 pf
->size
+= pt
->size
;
1025 pf
->next
= pt
->next
;
1026 if (pf
->next
!= NULL
)
1027 pf
->next
->prev
= pf
;
1029 } else if (pf
->page
== tail
) {
1030 /* Prepend to entry */
1033 } else if (pf
->next
== NULL
) {
1034 /* Append at tail of chain */
1041 wrterror("freelist is destroyed.\n");
1045 /* Return something to OS ? */
1048 if (pf
->next
== NULL
&& /* If we're the last one, */
1049 pf
->size
> malloc_cache
&& /* ..and the cache is full, */
1050 pf
->end
== malloc_brk
&& /* ..and none behind us, */
1051 malloc_brk
== sbrk((intptr_t)0)) { /* ..and it's OK to do... */
1054 * Keep the cache intact. Notice that the '>' above guarantees that
1055 * the pf will always have at least one page afterwards.
1057 pf
->end
= (char *)pf
->page
+ malloc_cache
;
1058 pf
->size
= malloc_cache
;
1061 malloc_brk
= pf
->end
;
1063 idx
= ptr2idx(pf
->end
);
1065 for(i
=idx
;i
<= last_idx
;)
1066 page_dir
[i
++] = MALLOC_NOT_MINE
;
1070 /* XXX: We could realloc/shrink the pagedir here I guess. */
1079 * Free a chunk, and possibly the page it's on, if the page becomes empty.
1083 free_bytes(void *ptr
, size_t idx
, struct pginfo
*info
)
1089 /* Find the chunk number on the page */
1090 i
= ((size_t)(uintptr_t)ptr
& malloc_pagemask
) >> info
->shift
;
1092 if (((size_t)(uintptr_t)ptr
& (info
->size
-1))) {
1093 wrtwarning("modified (chunk-) pointer.\n");
1097 if (info
->bits
[i
/MALLOC_BITS
] & (1<<(i
%MALLOC_BITS
))) {
1098 wrtwarning("chunk is already free.\n");
1103 memset(ptr
, SOME_FREE_JUNK
, (size_t)info
->size
);
1105 info
->bits
[i
/MALLOC_BITS
] |= 1<<(i
%MALLOC_BITS
);
1108 mp
= page_dir
+ info
->shift
;
1110 if (info
->free
== 1) {
1112 /* Page became non-full */
1114 mp
= page_dir
+ info
->shift
;
1115 /* Insert in address order */
1116 while (*mp
&& (*mp
)->next
&& (*mp
)->next
->page
< info
->page
)
1123 if (info
->free
!= info
->total
)
1126 /* Find & remove this page in the queue */
1127 while (*mp
!= info
) {
1128 mp
= &((*mp
)->next
);
1129 #ifdef MALLOC_EXTRA_SANITY
1131 wrterror("(ES): Not on queue.\n");
1132 #endif /* MALLOC_EXTRA_SANITY */
1136 /* Free the page & the info structure if need be */
1137 page_dir
[idx
] = MALLOC_FIRST
;
1138 vp
= info
->page
; /* Order is important ! */
1139 if(vp
!= (void*)info
)
1147 struct pginfo
*info
;
1154 /* If we're already sinking, don't make matters any worse. */
1160 if (idx
< malloc_pageshift
) {
1161 wrtwarning("junk pointer, too low to make sense.\n");
1165 if (idx
> last_idx
) {
1166 wrtwarning("junk pointer, too high to make sense.\n");
1170 info
= page_dir
[idx
];
1172 if (info
< MALLOC_MAGIC
)
1173 free_pages(ptr
, idx
, info
);
1175 free_bytes(ptr
, idx
, info
);
1180 * These are the public exported interface routines.
1188 //printf("malloc(%d) => ", size);
1191 malloc_func
= " in malloc():";
1192 if (malloc_active
++) {
1193 wrtwarning("recursive call.\n");
1196 //printf("= NULL [a]\n");
1200 if (!malloc_started
)
1203 if (malloc_sysv
&& !size
)
1211 if (r
== NULL
&& (size
!= 0 || !malloc_sysv
)) {
1213 wrterror("out of memory.\n");
1216 //printf("= 0x%x\n", r);
1224 malloc_func
= " in free():";
1225 if (malloc_active
++) {
1226 wrtwarning("recursive call.\n");
1231 if (!malloc_started
)
1241 realloc(void *ptr
, size_t size
)
1246 malloc_func
= " in realloc():";
1247 if (malloc_active
++) {
1248 wrtwarning("recursive call.\n");
1253 if (!malloc_started
)
1255 if (malloc_sysv
&& !size
) {
1261 r
= irealloc(ptr
, size
);
1263 UTRACE(ptr
, size
, r
);
1266 if (r
== NULL
&& (size
!= 0 || !malloc_sysv
)) {
1268 wrterror("out of memory.\n");
1277 void *calloc(size_t nmemb
, size_t size
) {
1278 void* ptr
= malloc(nmemb
* size
);
1279 memset(ptr
, 0, nmemb
* size
);