123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- #include <stdlib.h>
- #include <unistd.h>
- #include <err.h>
- #include <stdint.h> // for uintptr_t on non-OS/161 platforms
- #undef MALLOCDEBUG
- #if defined(__mips__) || defined(__i386__)
- #define MALLOC32
- #elif defined(__alpha__)
- #define MALLOC64
- #else
- #error "please fix me"
- #endif
- struct mheader {
- #if defined(MALLOC32)
- #define MBLOCKSIZE 8
- #define MBLOCKSHIFT 3
- #define MMAGIC 2
-
- unsigned mh_prevblock:29;
- unsigned mh_pad:1;
- unsigned mh_magic1:2;
- unsigned mh_nextblock:29;
- unsigned mh_inuse:1;
- unsigned mh_magic2:2;
- #elif defined(MALLOC64)
- #define MBLOCKSIZE 16
- #define MBLOCKSHIFT 4
- #define MMAGIC 6
-
- unsigned mh_prevblock:62;
- unsigned mh_pad:1;
- unsigned mh_magic1:3;
- unsigned mh_nextblock:62;
- unsigned mh_inuse:1;
- unsigned mh_magic2:3;
- #else
- #error "please fix me"
- #endif
- };
- #define M_NEXTOFF(mh) ((size_t)(((size_t)((mh)->mh_nextblock))<<MBLOCKSHIFT))
- #define M_PREVOFF(mh) ((size_t)(((size_t)((mh)->mh_prevblock))<<MBLOCKSHIFT))
- #define M_NEXT(mh) ((struct mheader *)(((char*)(mh))+M_NEXTOFF(mh)))
- #define M_PREV(mh) ((struct mheader *)(((char*)(mh))-M_PREVOFF(mh)))
- #define M_DATA(mh) ((void *)((mh)+1))
- #define M_SIZE(mh) (M_NEXTOFF(mh)-MBLOCKSIZE)
- #define M_OK(mh) ((mh)->mh_magic1==MMAGIC && (mh)->mh_magic2==MMAGIC)
- #define M_MKFIELD(off) ((off)>>MBLOCKSHIFT)
- static uintptr_t __heapbase, __heaptop;
- static
- void
- __malloc_init(void)
- {
- void *x;
-
- if (sizeof(struct mheader) != MBLOCKSIZE) {
- errx(1, "malloc: Internal error - MBLOCKSIZE wrong");
- }
- if ((MBLOCKSIZE & (MBLOCKSIZE-1))!=0) {
- errx(1, "malloc: Internal error - MBLOCKSIZE not power of 2");
- }
- if (1<<MBLOCKSHIFT != MBLOCKSIZE) {
- errx(1, "malloc: Internal error - MBLOCKSHIFT wrong");
- }
-
- if (__heapbase!=0 || __heaptop!=0) {
- errx(1, "malloc: Internal error - bad init call");
- }
-
- x = sbrk(0);
- if (x==(void *)-1) {
- err(1, "malloc: initial sbrk failed");
- }
- if (x==(void *) 0) {
- errx(1, "malloc: Internal error - heap began at 0");
- }
- __heapbase = __heaptop = (uintptr_t)x;
-
- if (__heapbase % MBLOCKSIZE != 0) {
- size_t adjust = MBLOCKSIZE - (__heapbase % MBLOCKSIZE);
- x = sbrk(adjust);
- if (x==(void *)-1) {
- err(1, "malloc: sbrk failed aligning heap base");
- }
- if ((uintptr_t)x != __heapbase) {
- err(1, "malloc: heap base moved during init");
- }
- #ifdef MALLOCDEBUG
- warnx("malloc: adjusted heap base upwards by %lu bytes",
- (unsigned long) adjust);
- #endif
- __heapbase += adjust;
- __heaptop = __heapbase;
- }
- }
- #ifdef MALLOCDEBUG
- static
- void
- __malloc_dump(void)
- {
- struct mheader *mh;
- uintptr_t i;
- size_t rightprevblock;
- warnx("heap: ************************************************");
- rightprevblock = 0;
- for (i=__heapbase; i<__heaptop; i += M_NEXTOFF(mh)) {
- mh = (struct mheader *) i;
- if (!M_OK(mh)) {
- errx(1, "malloc: Heap corrupt; header at 0x%lx"
- " has bad magic bits",
- (unsigned long) i);
- }
- if (mh->mh_prevblock != rightprevblock) {
- errx(1, "malloc: Heap corrupt; header at 0x%lx"
- " has bad previous-block size %lu "
- "(should be %lu)",
- (unsigned long) i,
- (unsigned long) mh->mh_prevblock << MBLOCKSHIFT,
- (unsigned long) rightprevblock << MBLOCKSHIFT);
- }
- rightprevblock = mh->mh_nextblock;
- warnx("heap: 0x%lx 0x%-6lx (next: 0x%lx) %s",
- (unsigned long) i + MBLOCKSIZE,
- (unsigned long) M_SIZE(mh),
- (unsigned long) (i+M_NEXTOFF(mh)),
- mh->mh_inuse ? "INUSE" : "FREE");
- }
- if (i!=__heaptop) {
- errx(1, "malloc: Heap corrupt; ran off end");
- }
- warnx("heap: ************************************************");
- }
- #endif
- static
- void *
- __malloc_sbrk(size_t size)
- {
- void *x;
- x = sbrk(size);
- if (x == (void *)-1) {
- return NULL;
- }
- if ((uintptr_t)x != __heaptop) {
- errx(1, "malloc: Internal error - "
- "heap top moved itself from 0x%lx to 0x%lx",
- (unsigned long) __heaptop,
- (unsigned long) (uintptr_t) x);
- }
- __heaptop += size;
- return x;
- }
- static
- void
- __malloc_split(struct mheader *mh, size_t size)
- {
- struct mheader *mhnext, *mhnew;
- size_t oldsize;
- if (size % MBLOCKSIZE != 0) {
- errx(1, "malloc: Internal error (size %lu passed to split)",
- (unsigned long) size);
- }
- if (M_SIZE(mh) - size < 2*MBLOCKSIZE) {
-
- return;
- }
- mhnext = M_NEXT(mh);
- oldsize = M_SIZE(mh);
- mh->mh_nextblock = M_MKFIELD(size + MBLOCKSIZE);
-
- mhnew = M_NEXT(mh);
- if (mhnew==mhnext) {
- errx(1, "malloc: Internal error (split screwed up?)");
- }
- mhnew->mh_prevblock = M_MKFIELD(size + MBLOCKSIZE);
- mhnew->mh_pad = 0;
- mhnew->mh_magic1 = MMAGIC;
- mhnew->mh_nextblock = M_MKFIELD(oldsize - size);
- mhnew->mh_inuse = 0;
- mhnew->mh_magic2 = MMAGIC;
- if (mhnext != (struct mheader *) __heaptop) {
- mhnext->mh_prevblock = mhnew->mh_nextblock;
- }
- }
- void *
- malloc(size_t size)
- {
- struct mheader *mh;
- uintptr_t i;
- size_t rightprevblock;
- if (__heapbase==0) {
- __malloc_init();
- }
- if (__heapbase==0 || __heaptop==0 || __heapbase > __heaptop) {
- warnx("malloc: Internal error - local data corrupt");
- errx(1, "malloc: heapbase 0x%lx; heaptop 0x%lx",
- (unsigned long) __heapbase, (unsigned long) __heaptop);
- }
- #ifdef MALLOCDEBUG
- warnx("malloc: about to allocate %lu (0x%lx) bytes",
- (unsigned long) size, (unsigned long) size);
- __malloc_dump();
- #endif
-
- size = ((size + MBLOCKSIZE - 1) & ~(size_t)(MBLOCKSIZE-1));
-
- rightprevblock = 0;
- for (i=__heapbase; i<__heaptop; i += M_NEXTOFF(mh)) {
- mh = (struct mheader *) i;
- if (!M_OK(mh)) {
- errx(1, "malloc: Heap corrupt; header at 0x%lx"
- " has bad magic bits",
- (unsigned long) i);
- }
- if (mh->mh_prevblock != rightprevblock) {
- errx(1, "malloc: Heap corrupt; header at 0x%lx"
- " has bad previous-block size %lu "
- "(should be %lu)",
- (unsigned long) i,
- (unsigned long) mh->mh_prevblock << MBLOCKSHIFT,
- (unsigned long) rightprevblock << MBLOCKSHIFT);
- }
- rightprevblock = mh->mh_nextblock;
-
- if (mh->mh_inuse) {
- continue;
- }
-
- if (M_SIZE(mh) < size) {
- continue;
- }
-
- __malloc_split(mh, size);
-
- mh->mh_inuse = 1;
- #ifdef MALLOCDEBUG
- warnx("malloc: allocating at %p", M_DATA(mh));
- __malloc_dump();
- #endif
- return M_DATA(mh);
- }
- if (i!=__heaptop) {
- errx(1, "malloc: Heap corrupt; ran off end");
- }
-
- mh = __malloc_sbrk(size + MBLOCKSIZE);
- if (mh == NULL) {
- return NULL;
- }
- mh->mh_prevblock = rightprevblock;
- mh->mh_magic1 = MMAGIC;
- mh->mh_magic2 = MMAGIC;
- mh->mh_pad = 0;
- mh->mh_inuse = 1;
- mh->mh_nextblock = M_MKFIELD(size + MBLOCKSIZE);
- #ifdef MALLOCDEBUG
- warnx("malloc: allocating at %p", M_DATA(mh));
- __malloc_dump();
- #endif
- return M_DATA(mh);
- }
- static
- void
- __malloc_deadbeef(void *ptr, size_t size)
- {
- uint32_t *x = ptr;
- size_t i, n = size/sizeof(uint32_t);
- for (i=0; i<n; i++) {
- x[i] = 0xdeadbeef;
- }
- }
- static
- void
- __malloc_trymerge(struct mheader *mh, struct mheader *mhnext)
- {
- struct mheader *mhnextnext;
- if (mh->mh_nextblock != mhnext->mh_prevblock) {
- errx(1, "free: Heap corrupt (%p and %p inconsistent)",
- mh, mhnext);
- }
- if (mh->mh_inuse || mhnext->mh_inuse) {
-
- return;
- }
- mhnextnext = M_NEXT(mhnext);
- mh->mh_nextblock = M_MKFIELD(MBLOCKSIZE + M_SIZE(mh) +
- MBLOCKSIZE + M_SIZE(mhnext));
- if (mhnextnext != (struct mheader *)__heaptop) {
- mhnextnext->mh_prevblock = mh->mh_nextblock;
- }
-
- __malloc_deadbeef(mhnext, sizeof(struct mheader));
- }
- void
- free(void *x)
- {
- struct mheader *mh, *mhnext, *mhprev;
- if (x==NULL) {
-
- return;
- }
-
- if (__heapbase==0 || __heaptop==0 || __heapbase > __heaptop) {
- warnx("free: Internal error - local data corrupt");
- errx(1, "free: heapbase 0x%lx; heaptop 0x%lx",
- (unsigned long) __heapbase, (unsigned long) __heaptop);
- }
-
- if ((uintptr_t)x < __heapbase || (uintptr_t)x >= __heaptop) {
- errx(1, "free: Invalid pointer %p freed (out of range)", x);
- }
- #ifdef MALLOCDEBUG
- warnx("free: about to free %p", x);
- __malloc_dump();
- #endif
- mh = ((struct mheader *)x)-1;
- if (!M_OK(mh)) {
- errx(1, "free: Invalid pointer %p freed (corrupt header)", x);
- }
- if (!mh->mh_inuse) {
- errx(1, "free: Invalid pointer %p freed (already free)", x);
- }
-
- mh->mh_inuse = 0;
-
- __malloc_deadbeef(M_DATA(mh), M_SIZE(mh));
-
- mhnext = M_NEXT(mh);
- if (mhnext != (struct mheader *)__heaptop) {
- __malloc_trymerge(mh, mhnext);
- }
-
- if (mh != (struct mheader *)__heapbase) {
- mhprev = M_PREV(mh);
- __malloc_trymerge(mhprev, mh);
- }
- #ifdef MALLOCDEBUG
- warnx("free: freed %p", x);
- __malloc_dump();
- #endif
- }
|