123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995 |
- #ifndef MALLOC_EXTRA_SANITY
- #undef MALLOC_EXTRA_SANITY
- #endif
- #ifndef MALLOC_STATS
- #undef MALLOC_STATS
- #endif
- #define SOME_JUNK 0xd0
- #include <sys/types.h>
- #include <sys/time.h>
- #include <sys/resource.h>
- #include <sys/param.h>
- #include <sys/mman.h>
- #include <sys/uio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <limits.h>
- #include <errno.h>
- #include <err.h>
- static int align = 0;
- static size_t g_alignment = 0;
- extern int __libc_enable_secure;
- static int issetugid(void)
- {
- if (__libc_enable_secure) return 1;
- if (getuid() != geteuid()) return 1;
- if (getgid() != getegid()) return 1;
- return 0;
- }
- #define PGSHIFT 12
- #define MADV_FREE MADV_DONTNEED
- #include <pthread.h>
- static pthread_mutex_t gen_mutex = PTHREAD_MUTEX_INITIALIZER;
- #define _MALLOC_LOCK_INIT() {;}
- #define _MALLOC_LOCK() {pthread_mutex_lock(&gen_mutex);}
- #define _MALLOC_UNLOCK() {pthread_mutex_unlock(&gen_mutex);}
- #if defined(__sparc__)
- #define malloc_pageshift 13U
- #endif
- #ifndef malloc_pageshift
- #define malloc_pageshift (PGSHIFT)
- #endif
- struct pginfo {
- struct pginfo *next;
- void *page;
- u_short size;
- u_short shift;
- u_short free;
- u_short total;
- u_long bits[1];
- };
- #define MALLOC_BITS (NBBY * sizeof(u_long))
- struct pgfree {
- struct pgfree *next;
- struct pgfree *prev;
- void *page;
- void *pdir;
- size_t size;
- };
- #define MALLOC_NOT_MINE ((struct pginfo*) 0)
- #define MALLOC_FREE ((struct pginfo*) 1)
- #define MALLOC_FIRST ((struct pginfo*) 2)
- #define MALLOC_FOLLOW ((struct pginfo*) 3)
- #define MALLOC_MAGIC ((struct pginfo*) 4)
- #ifndef malloc_minsize
- #define malloc_minsize 16UL
- #endif
- #if !defined(malloc_pagesize)
- #define malloc_pagesize (1UL<<malloc_pageshift)
- #endif
- #if ((1UL<<malloc_pageshift) != malloc_pagesize)
- #error "(1UL<<malloc_pageshift) != malloc_pagesize"
- #endif
- #ifndef malloc_maxsize
- #define malloc_maxsize ((malloc_pagesize)>>1)
- #endif
- #define malloc_pagemask ((malloc_pagesize)-1)
- #define pageround(foo) (((foo) + (malloc_pagemask)) & ~malloc_pagemask)
- #define ptr2index(foo) (((u_long)(foo) >> malloc_pageshift)+malloc_pageshift)
- #define index2ptr(idx) ((void*)(((idx)-malloc_pageshift)<<malloc_pageshift))
- static unsigned int malloc_started;
- static unsigned int malloc_cache = 16;
- struct pdinfo {
- struct pginfo **base;
- struct pdinfo *prev;
- struct pdinfo *next;
- u_long dirnum;
- };
- static struct pdinfo *last_dir;
- static struct pdinfo *prev_dir;
- static size_t pdi_off;
- static u_long pdi_mod;
- #define PD_IDX(num) ((num) / (malloc_pagesize/sizeof(struct pginfo *)))
- #define PD_OFF(num) ((num) & ((malloc_pagesize/sizeof(struct pginfo *))-1))
- #define PI_IDX(index) ((index) / pdi_mod)
- #define PI_OFF(index) ((index) % pdi_mod)
- static u_long last_index;
- static struct pginfo **page_dir;
- static struct pgfree free_list;
- static int malloc_abort = 2;
- static int suicide;
- #ifdef MALLOC_STATS
- static int malloc_stats;
- #endif
- static int malloc_silent;
- static int malloc_realloc;
- static int malloc_freeprot;
- static size_t malloc_guard = 0;
- static size_t malloc_guarded;
- static int malloc_ptrguard;
- static int malloc_hint = 1;
- static int malloc_xmalloc;
- static int malloc_zero;
- static int malloc_junk;
- #ifdef __FreeBSD__
- static int malloc_utrace;
- struct ut {
- void *p;
- size_t s;
- void *r;
- };
- void utrace(struct ut *, int);
- #define UTRACE(a, b, c) \
- if (malloc_utrace) \
- {struct ut u; u.p=a; u.s = b; u.r=c; utrace(&u, sizeof u);}
- #else
- #define UTRACE(a,b,c)
- #endif
- static int malloc_active;
- static size_t malloc_used;
- static caddr_t malloc_brk;
- static struct pgfree *px;
- char *malloc_options;
- static char *malloc_func;
- #define MMAP(size) \
- mmap((void *)0, (size), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, \
- -1, (off_t)0)
- static void *imalloc(size_t size);
- static void ifree(void *ptr);
- static void *irealloc(void *ptr, size_t size);
- static void *malloc_bytes(size_t size);
- static int
- pdir_lookup(u_long index, struct pdinfo ** pdi)
- {
- struct pdinfo *spi;
- u_long pidx = PI_IDX(index);
- if (last_dir != NULL && PD_IDX(last_dir->dirnum) == pidx)
- *pdi = last_dir;
- else if (prev_dir != NULL && PD_IDX(prev_dir->dirnum) == pidx)
- *pdi = prev_dir;
- else if (last_dir != NULL && prev_dir != NULL) {
- if ((PD_IDX(last_dir->dirnum) > pidx) ?
- (PD_IDX(last_dir->dirnum) - pidx) :
- (pidx - PD_IDX(last_dir->dirnum))
- < (PD_IDX(prev_dir->dirnum) > pidx) ?
- (PD_IDX(prev_dir->dirnum) - pidx) :
- (pidx - PD_IDX(prev_dir->dirnum)))
- *pdi = last_dir;
- else
- *pdi = prev_dir;
- if (PD_IDX((*pdi)->dirnum) > pidx) {
- for (spi = (*pdi)->prev;
- spi != NULL && PD_IDX(spi->dirnum) > pidx;
- spi = spi->prev)
- *pdi = spi;
- if (spi != NULL)
- *pdi = spi;
- } else
- for (spi = (*pdi)->next;
- spi != NULL && PD_IDX(spi->dirnum) <= pidx;
- spi = spi->next)
- *pdi = spi;
- } else {
- *pdi = (struct pdinfo *) ((caddr_t) page_dir + pdi_off);
- for (spi = *pdi;
- spi != NULL && PD_IDX(spi->dirnum) <= pidx;
- spi = spi->next)
- *pdi = spi;
- }
- return ((PD_IDX((*pdi)->dirnum) == pidx) ? 0 :
- (PD_IDX((*pdi)->dirnum) > pidx) ? 1 : -1);
- }
- #ifdef MALLOC_STATS
- void
- malloc_dump(int fd)
- {
- char buf[1024];
- struct pginfo **pd;
- struct pgfree *pf;
- struct pdinfo *pi;
- u_long j;
- pd = page_dir;
- pi = (struct pdinfo *) ((caddr_t) pd + pdi_off);
-
- for (j = 0; j <= last_index;) {
- snprintf(buf, sizeof buf, "%08lx %5lu ", j << malloc_pageshift, j);
- write(fd, buf, strlen(buf));
- if (pd[PI_OFF(j)] == MALLOC_NOT_MINE) {
- for (j++; j <= last_index && pd[PI_OFF(j)] == MALLOC_NOT_MINE;) {
- if (!PI_OFF(++j)) {
- if ((pi = pi->next) == NULL ||
- PD_IDX(pi->dirnum) != PI_IDX(j))
- break;
- pd = pi->base;
- j += pdi_mod;
- }
- }
- j--;
- snprintf(buf, sizeof buf, ".. %5lu not mine\n", j);
- write(fd, buf, strlen(buf));
- } else if (pd[PI_OFF(j)] == MALLOC_FREE) {
- for (j++; j <= last_index && pd[PI_OFF(j)] == MALLOC_FREE;) {
- if (!PI_OFF(++j)) {
- if ((pi = pi->next) == NULL ||
- PD_IDX(pi->dirnum) != PI_IDX(j))
- break;
- pd = pi->base;
- j += pdi_mod;
- }
- }
- j--;
- snprintf(buf, sizeof buf, ".. %5lu free\n", j);
- write(fd, buf, strlen(buf));
- } else if (pd[PI_OFF(j)] == MALLOC_FIRST) {
- for (j++; j <= last_index && pd[PI_OFF(j)] == MALLOC_FOLLOW;) {
- if (!PI_OFF(++j)) {
- if ((pi = pi->next) == NULL ||
- PD_IDX(pi->dirnum) != PI_IDX(j))
- break;
- pd = pi->base;
- j += pdi_mod;
- }
- }
- j--;
- snprintf(buf, sizeof buf, ".. %5lu in use\n", j);
- write(fd, buf, strlen(buf));
- } else if (pd[PI_OFF(j)] < MALLOC_MAGIC) {
- snprintf(buf, sizeof buf, "(%p)\n", pd[PI_OFF(j)]);
- write(fd, buf, strlen(buf));
- } else {
- snprintf(buf, sizeof buf, "%p %d (of %d) x %d @ %p --> %p\n",
- pd[PI_OFF(j)], pd[PI_OFF(j)]->free,
- pd[PI_OFF(j)]->total, pd[PI_OFF(j)]->size,
- pd[PI_OFF(j)]->page, pd[PI_OFF(j)]->next);
- write(fd, buf, strlen(buf));
- }
- if (!PI_OFF(++j)) {
- if ((pi = pi->next) == NULL)
- break;
- pd = pi->base;
- j += (1 + PD_IDX(pi->dirnum) - PI_IDX(j)) * pdi_mod;
- }
- }
- for (pf = free_list.next; pf; pf = pf->next) {
- snprintf(buf, sizeof buf, "Free: @%p [%p...%p[ %ld ->%p <-%p\n",
- pf, pf->page, (char *)pf->page + pf->size,
- pf->size, pf->prev, pf->next);
- write(fd, buf, strlen(buf));
- if (pf == pf->next) {
- snprintf(buf, sizeof buf, "Free_list loops\n");
- write(fd, buf, strlen(buf));
- break;
- }
- }
-
- snprintf(buf, sizeof buf, "Minsize\t%lu\n", malloc_minsize);
- write(fd, buf, strlen(buf));
- snprintf(buf, sizeof buf, "Maxsize\t%lu\n", malloc_maxsize);
- write(fd, buf, strlen(buf));
- snprintf(buf, sizeof buf, "Pagesize\t%lu\n", malloc_pagesize);
- write(fd, buf, strlen(buf));
- snprintf(buf, sizeof buf, "Pageshift\t%u\n", malloc_pageshift);
- write(fd, buf, strlen(buf));
- snprintf(buf, sizeof buf, "In use\t%lu\n", (u_long) malloc_used);
- write(fd, buf, strlen(buf));
- snprintf(buf, sizeof buf, "Guarded\t%lu\n", (u_long) malloc_guarded);
- write(fd, buf, strlen(buf));
- }
- #endif
- extern char *__progname;
- static void
- wrterror(char *p)
- {
- char *q = " error: ";
- struct iovec iov[5];
- iov[0].iov_base = __progname;
- iov[0].iov_len = strlen(__progname);
- iov[1].iov_base = malloc_func;
- iov[1].iov_len = strlen(malloc_func);
- iov[2].iov_base = q;
- iov[2].iov_len = strlen(q);
- iov[3].iov_base = p;
- iov[3].iov_len = strlen(p);
- iov[4].iov_base = "\n";
- iov[4].iov_len = 1;
- writev(STDERR_FILENO, iov, 5);
- suicide = 1;
- #ifdef MALLOC_STATS
- if (malloc_stats)
- malloc_dump(STDERR_FILENO);
- #endif
- malloc_active--;
- if (malloc_abort)
- abort();
- }
- static void
- wrtwarning(char *p)
- {
- char *q = " warning: ";
- struct iovec iov[5];
- if (malloc_abort)
- wrterror(p);
- else if (malloc_silent)
- return;
- iov[0].iov_base = __progname;
- iov[0].iov_len = strlen(__progname);
- iov[1].iov_base = malloc_func;
- iov[1].iov_len = strlen(malloc_func);
- iov[2].iov_base = q;
- iov[2].iov_len = strlen(q);
- iov[3].iov_base = p;
- iov[3].iov_len = strlen(p);
- iov[4].iov_base = "\n";
- iov[4].iov_len = 1;
-
- writev(STDERR_FILENO, iov, 5);
- }
- #ifdef MALLOC_STATS
- static void
- malloc_exit(void)
- {
- char *q = "malloc() warning: Couldn't dump stats\n";
- int save_errno = errno, fd;
- fd = open("malloc.out", O_RDWR|O_APPEND);
- if (fd != -1) {
- malloc_dump(fd);
- close(fd);
- } else
- write(STDERR_FILENO, q, strlen(q));
- errno = save_errno;
- }
- #endif
- static void *MMAP_A(size_t pages, size_t alignment)
- {
- void *j;
- if (pages%malloc_pagesize != 0)
- pages = pages - pages%malloc_pagesize + malloc_pagesize;
- size_t first_size = pages + alignment - malloc_pagesize;
- void *p = MMAP(first_size);
- size_t rest = ((size_t)p) % alignment;
- j = (rest == 0) ? p : (void*) ((size_t)p + alignment - rest);
- size_t begin = (size_t)j - (size_t)p;
- if (begin != 0) munmap(p, begin);
- size_t end = (size_t)p + first_size - ((size_t)j + pages);
- if(end != 0) munmap( (void*) ((size_t)j + pages), end);
- return j;
- }
- static void *
- map_pages(size_t pages)
- {
- struct pdinfo *pi, *spi;
- struct pginfo **pd;
- u_long idx, pidx, lidx;
- caddr_t result, tail;
- u_long index, lindex;
- void *pdregion = NULL;
- size_t dirs, cnt;
- pages <<= malloc_pageshift;
- if (!align)
- result = MMAP(pages + malloc_guard);
- else {
- result = MMAP_A(pages + malloc_guard, g_alignment);
- }
- if (result == MAP_FAILED) {
- #ifdef MALLOC_EXTRA_SANITY
- wrtwarning("(ES): map_pages fails");
- #endif
- errno = ENOMEM;
- return (NULL);
- }
- index = ptr2index(result);
- tail = result + pages + malloc_guard;
- lindex = ptr2index(tail) - 1;
- if (malloc_guard)
- mprotect(result + pages, malloc_guard, PROT_NONE);
- pidx = PI_IDX(index);
- lidx = PI_IDX(lindex);
- if (tail > malloc_brk) {
- malloc_brk = tail;
- last_index = lindex;
- }
- dirs = lidx - pidx;
-
- if (pdir_lookup(index, &pi) != 0)
- dirs++;
- if (dirs > 0) {
- pdregion = MMAP(malloc_pagesize * dirs);
- if (pdregion == MAP_FAILED) {
- munmap(result, tail - result);
- #ifdef MALLOC_EXTRA_SANITY
- wrtwarning("(ES): map_pages fails");
- #endif
- errno = ENOMEM;
- return (NULL);
- }
- }
- cnt = 0;
- for (idx = pidx, spi = pi; idx <= lidx; idx++) {
- if (pi == NULL || PD_IDX(pi->dirnum) != idx) {
- pd = (struct pginfo **)((char *)pdregion +
- cnt * malloc_pagesize);
- cnt++;
- memset(pd, 0, malloc_pagesize);
- pi = (struct pdinfo *) ((caddr_t) pd + pdi_off);
- pi->base = pd;
- pi->prev = spi;
- pi->next = spi->next;
- pi->dirnum = idx * (malloc_pagesize /
- sizeof(struct pginfo *));
- if (spi->next != NULL)
- spi->next->prev = pi;
- spi->next = pi;
- }
- if (idx > pidx && idx < lidx) {
- pi->dirnum += pdi_mod;
- } else if (idx == pidx) {
- if (pidx == lidx) {
- pi->dirnum += (u_long)(tail - result) >>
- malloc_pageshift;
- } else {
- pi->dirnum += pdi_mod - PI_OFF(index);
- }
- } else {
- pi->dirnum += PI_OFF(ptr2index(tail - 1)) + 1;
- }
- #ifdef MALLOC_EXTRA_SANITY
- if (PD_OFF(pi->dirnum) > pdi_mod || PD_IDX(pi->dirnum) > idx) {
- wrterror("(ES): pages directory overflow");
- errno = EFAULT;
- return (NULL);
- }
- #endif
- if (idx == pidx && pi != last_dir) {
- prev_dir = last_dir;
- last_dir = pi;
- }
- spi = pi;
- pi = spi->next;
- }
- #ifdef MALLOC_EXTRA_SANITY
- if (cnt > dirs)
- wrtwarning("(ES): cnt > dirs");
- #endif
- if (cnt < dirs)
- munmap((char *)pdregion + cnt * malloc_pagesize,
- (dirs - cnt) * malloc_pagesize);
- return (result);
- }
- static void
- malloc_init(void)
- {
- char *p, b[64];
- int i, j, save_errno = errno;
- _MALLOC_LOCK_INIT();
- #ifdef MALLOC_EXTRA_SANITY
- malloc_junk = 1;
- #endif
- for (i = 0; i < 3; i++) {
- switch (i) {
- case 0:
- j = readlink("/etc/malloc.conf", b, sizeof b - 1);
- if (j <= 0)
- continue;
- b[j] = '\0';
- p = b;
- break;
- case 1:
- if (issetugid() == 0)
- p = getenv("MALLOC_OPTIONS");
- else
- continue;
- break;
- case 2:
- p = malloc_options;
- break;
- default:
- p = NULL;
- }
- for (; p != NULL && *p != '\0'; p++) {
- switch (*p) {
- case '>':
- malloc_cache <<= 1;
- break;
- case '<':
- malloc_cache >>= 1;
- break;
- case 'a':
- malloc_abort = 0;
- break;
- case 'A':
- malloc_abort = 1;
- break;
- #ifdef MALLOC_STATS
- case 'd':
- malloc_stats = 0;
- break;
- case 'D':
- malloc_stats = 1;
- break;
- #endif
- case 'f':
- malloc_freeprot = 0;
- break;
- case 'F':
- malloc_freeprot = 1;
- break;
- case 'g':
- malloc_guard = 0;
- break;
- case 'G':
- malloc_guard = malloc_pagesize;
- break;
- case 'h':
- malloc_hint = 0;
- break;
- case 'H':
- malloc_hint = 1;
- break;
- case 'j':
- malloc_junk = 0;
- break;
- case 'J':
- malloc_junk = 1;
- break;
- case 'n':
- malloc_silent = 0;
- break;
- case 'N':
- malloc_silent = 1;
- break;
- case 'p':
- malloc_ptrguard = 0;
- break;
- case 'P':
- malloc_ptrguard = 1;
- break;
- case 'r':
- malloc_realloc = 0;
- break;
- case 'R':
- malloc_realloc = 1;
- break;
- #ifdef __FreeBSD__
- case 'u':
- malloc_utrace = 0;
- break;
- case 'U':
- malloc_utrace = 1;
- break;
- #endif
- case 'x':
- malloc_xmalloc = 0;
- break;
- case 'X':
- malloc_xmalloc = 1;
- break;
- case 'z':
- malloc_zero = 0;
- break;
- case 'Z':
- malloc_zero = 1;
- break;
- default:
- j = malloc_abort;
- malloc_abort = 0;
- wrtwarning("unknown char in MALLOC_OPTIONS");
- malloc_abort = j;
- break;
- }
- }
- }
- UTRACE(0, 0, 0);
-
- if (malloc_zero)
- malloc_junk = 1;
- #ifdef MALLOC_STATS
- if (malloc_stats && (atexit(malloc_exit) == -1))
- wrtwarning("atexit(2) failed."
- " Will not be able to dump malloc stats on exit");
- #endif
-
- page_dir = (struct pginfo **)MMAP(malloc_pagesize);
- if (page_dir == MAP_FAILED) {
- wrterror("mmap(2) failed, check limits");
- errno = ENOMEM;
- return;
- }
- pdi_off = (malloc_pagesize - sizeof(struct pdinfo)) & ~(malloc_minsize - 1);
- pdi_mod = pdi_off / sizeof(struct pginfo *);
- last_dir = (struct pdinfo *) ((caddr_t) page_dir + pdi_off);
- last_dir->base = page_dir;
- last_dir->prev = last_dir->next = NULL;
- last_dir->dirnum = malloc_pageshift;
-
- malloc_started++;
-
- if (!malloc_cache)
- malloc_cache++;
- malloc_cache <<= malloc_pageshift;
- errno = save_errno;
- }
- static void *
- malloc_pages(size_t size)
- {
- void *p, *delay_free = NULL, *tp;
- int i;
- struct pginfo **pd;
- struct pdinfo *pi;
- u_long pidx, index;
- struct pgfree *pf;
- size = pageround(size) + malloc_guard;
- p = NULL;
-
- if (!align)
- for (pf = free_list.next; pf; pf = pf->next) {
- #ifdef MALLOC_EXTRA_SANITY
- if (pf->size & malloc_pagemask) {
- wrterror("(ES): junk length entry on free_list");
- errno = EFAULT;
- return (NULL);
- }
- if (!pf->size) {
- wrterror("(ES): zero length entry on free_list");
- errno = EFAULT;
- return (NULL);
- }
- if (pf->page > (pf->page + pf->size)) {
- wrterror("(ES): sick entry on free_list");
- errno = EFAULT;
- return (NULL);
- }
- if ((pi = pf->pdir) == NULL) {
- wrterror("(ES): invalid page directory on free-list");
- errno = EFAULT;
- return (NULL);
- }
- if ((pidx = PI_IDX(ptr2index(pf->page))) != PD_IDX(pi->dirnum)) {
- wrterror("(ES): directory index mismatch on free-list");
- errno = EFAULT;
- return (NULL);
- }
- pd = pi->base;
- if (pd[PI_OFF(ptr2index(pf->page))] != MALLOC_FREE) {
- wrterror("(ES): non-free first page on free-list");
- errno = EFAULT;
- return (NULL);
- }
- pidx = PI_IDX(ptr2index((pf->page) + (pf->size)) - 1);
- for (pi = pf->pdir; pi != NULL && PD_IDX(pi->dirnum) < pidx;
- pi = pi->next)
- ;
- if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
- wrterror("(ES): last page not referenced in page directory");
- errno = EFAULT;
- return (NULL);
- }
- pd = pi->base;
- if (pd[PI_OFF(ptr2index((pf->page) + (pf->size)) - 1)] != MALLOC_FREE) {
- wrterror("(ES): non-free last page on free-list");
- errno = EFAULT;
- return (NULL);
- }
- #endif
- if (pf->size < size)
- continue;
- if (pf->size == size) {
- p = pf->page;
- pi = pf->pdir;
- if (pf->next != NULL)
- pf->next->prev = pf->prev;
- pf->prev->next = pf->next;
- delay_free = pf;
- break;
- }
- p = pf->page;
- pf->page = (char *) pf->page + size;
- pf->size -= size;
- pidx = PI_IDX(ptr2index(pf->page));
- for (pi = pf->pdir; pi != NULL && PD_IDX(pi->dirnum) < pidx;
- pi = pi->next)
- ;
- if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
- wrterror("(ES): hole in directories");
- errno = EFAULT;
- return (NULL);
- }
- tp = pf->pdir;
- pf->pdir = pi;
- pi = tp;
- break;
- }
- size -= malloc_guard;
- #ifdef MALLOC_EXTRA_SANITY
- if (p != NULL && pi != NULL) {
- pidx = PD_IDX(pi->dirnum);
- pd = pi->base;
- }
- if (p != NULL && pd[PI_OFF(ptr2index(p))] != MALLOC_FREE) {
- wrterror("(ES): allocated non-free page on free-list");
- errno = EFAULT;
- return (NULL);
- }
- #endif
- if (p != NULL && (malloc_guard || malloc_freeprot))
- mprotect(p, size, PROT_READ | PROT_WRITE);
- size >>= malloc_pageshift;
-
- if (p == NULL)
- p = map_pages(size);
- if (p != NULL) {
- index = ptr2index(p);
- pidx = PI_IDX(index);
- pdir_lookup(index, &pi);
- #ifdef MALLOC_EXTRA_SANITY
- if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
- wrterror("(ES): mapped pages not found in directory");
- errno = EFAULT;
- return (NULL);
- }
- #endif
- if (pi != last_dir) {
- prev_dir = last_dir;
- last_dir = pi;
- }
- pd = pi->base;
- pd[PI_OFF(index)] = MALLOC_FIRST;
- for (i = 1; i < size; i++) {
- if (!PI_OFF(index + i)) {
- pidx++;
- pi = pi->next;
- #ifdef MALLOC_EXTRA_SANITY
- if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
- wrterror("(ES): hole in mapped pages directory");
- errno = EFAULT;
- return (NULL);
- }
- #endif
- pd = pi->base;
- }
- pd[PI_OFF(index + i)] = MALLOC_FOLLOW;
- }
- if (malloc_guard) {
- if (!PI_OFF(index + i)) {
- pidx++;
- pi = pi->next;
- #ifdef MALLOC_EXTRA_SANITY
- if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
- wrterror("(ES): hole in mapped pages directory");
- errno = EFAULT;
- return (NULL);
- }
- #endif
- pd = pi->base;
- }
- pd[PI_OFF(index + i)] = MALLOC_FIRST;
- }
- malloc_used += size << malloc_pageshift;
- malloc_guarded += malloc_guard;
- if (malloc_junk)
- memset(p, SOME_JUNK, size << malloc_pageshift);
- }
- if (delay_free) {
- if (px == NULL)
- px = delay_free;
- else
- ifree(delay_free);
- }
- return (p);
- }
- static __inline__ int
- malloc_make_chunks(int bits)
- {
- struct pginfo *bp, **pd;
- struct pdinfo *pi;
- #ifdef MALLOC_EXTRA_SANITY
- u_long pidx;
- #endif
- void *pp;
- long i, k;
- size_t l;
-
- pp = malloc_pages((size_t) malloc_pagesize);
- if (pp == NULL)
- return (0);
-
- l = sizeof *bp - sizeof(u_long);
- l += sizeof(u_long) *
- (((malloc_pagesize >> bits) + MALLOC_BITS - 1) / MALLOC_BITS);
-
-
- if (bits != 0 && (1UL << (bits)) <= l + l) {
- bp = (struct pginfo *) pp;
- } else {
- bp = (struct pginfo *) imalloc(l);
- if (bp == NULL) {
- ifree(pp);
- return (0);
- }
- }
-
- if (bits == 0) {
- bp->size = 0;
- bp->shift = 1;
- i = malloc_minsize - 1;
- while (i >>= 1)
- bp->shift++;
- bp->total = bp->free = malloc_pagesize >> bp->shift;
- bp->page = pp;
- k = mprotect(pp, malloc_pagesize, PROT_NONE);
- if (k < 0) {
- ifree(pp);
- ifree(bp);
- return (0);
- }
- } else {
- bp->size = (1UL << bits);
- bp->shift = bits;
- bp->total = bp->free = malloc_pagesize >> bits;
- bp->page = pp;
- }
-
- k = bp->total;
- i = 0;
-
- for (; (k - i) >= MALLOC_BITS; i += MALLOC_BITS)
- bp->bits[i / MALLOC_BITS] = ~0UL;
- for (; i < k; i++)
- bp->bits[i / MALLOC_BITS] |= 1UL << (i % MALLOC_BITS);
- k = (long)l;
- if (bp == bp->page) {
-
- for (i = 0; k > 0; i++) {
- bp->bits[i / MALLOC_BITS] &= ~(1UL << (i % MALLOC_BITS));
- bp->free--;
- bp->total--;
- k -= (1 << bits);
- }
- }
-
- pdir_lookup(ptr2index(pp), &pi);
- #ifdef MALLOC_EXTRA_SANITY
- pidx = PI_IDX(ptr2index(pp));
- if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
- wrterror("(ES): mapped pages not found in directory");
- errno = EFAULT;
- return (0);
- }
- #endif
- if (pi != last_dir) {
- prev_dir = last_dir;
- last_dir = pi;
- }
- pd = pi->base;
- pd[PI_OFF(ptr2index(pp))] = bp;
- bp->next = page_dir[bits];
- page_dir[bits] = bp;
-
- return (1);
- }
- static void *
- malloc_bytes(size_t size)
- {
- int i, j;
- size_t k;
- u_long u, *lp;
- struct pginfo *bp;
-
-
- if (size != 0 && size < malloc_minsize)
- size = malloc_minsize;
-
- if (size == 0)
- j = 0;
- else {
- j = 1;
- i = size - 1;
- while (i >>= 1)
- j++;
- }
-
- if (page_dir[j] == NULL && !malloc_make_chunks(j))
- return (NULL);
- bp = page_dir[j];
-
- for (lp = bp->bits; !*lp; lp++);
-
- u = 1;
- k = 0;
- while (!(*lp & u)) {
- u += u;
- k++;
- }
- if (malloc_guard) {
-
- i = rand() % bp->free;
- while (i > 0) {
- u += u;
- k++;
- if (k >= MALLOC_BITS) {
- lp++;
- u = 1;
- k = 0;
- }
- #ifdef MALLOC_EXTRA_SANITY
- if (lp - bp->bits > (bp->total - 1) / MALLOC_BITS) {
- wrterror("chunk overflow");
- errno = EFAULT;
- return (NULL);
- }
- #endif
- if (*lp & u)
- i--;
- }
- }
- *lp ^= u;
-
- if (!--bp->free) {
- page_dir[j] = bp->next;
- bp->next = NULL;
- }
-
- k += (lp - bp->bits) * MALLOC_BITS;
- k <<= bp->shift;
- if (malloc_junk && bp->size != 0)
- memset((char *)bp->page + k, SOME_JUNK, (size_t)bp->size);
- return ((u_char *) bp->page + k);
- }
- #define PTR_GAP (malloc_pagesize - sizeof(void *))
- #define PTR_SIZE (sizeof(void *))
- #define PTR_ALIGNED(p) (((unsigned long)p & malloc_pagemask) == PTR_GAP)
- static void *
- imalloc(size_t size)
- {
- void *result;
- int ptralloc = 0;
- if (!malloc_started)
- malloc_init();
- if (suicide)
- abort();
-
- if (px == NULL)
- px = malloc_bytes(sizeof *px);
- if (malloc_ptrguard && size == PTR_SIZE) {
- ptralloc = 1;
- size = malloc_pagesize;
- }
- if ((size + malloc_pagesize) < size) {
- result = NULL;
- errno = ENOMEM;
- } else if (size <= malloc_maxsize)
- result = malloc_bytes(size);
- else
- result = malloc_pages(size);
- if (malloc_abort == 1 && result == NULL)
- wrterror("allocation failed");
- if (malloc_zero && result != NULL)
- memset(result, 0, size);
- if (result && ptralloc)
- return ((char *) result + PTR_GAP);
- return (result);
- }
- static void *
- irealloc(void *ptr, size_t size)
- {
- void *p;
- size_t osize;
- u_long index, i;
- struct pginfo **mp;
- struct pginfo **pd;
- struct pdinfo *pi;
- #ifdef MALLOC_EXTRA_SANITY
- u_long pidx;
- #endif
- if (suicide)
- abort();
- if (!malloc_started) {
- wrtwarning("malloc() has never been called");
- return (NULL);
- }
- if (malloc_ptrguard && PTR_ALIGNED(ptr)) {
- if (size <= PTR_SIZE)
- return (ptr);
- p = imalloc(size);
- if (p)
- memcpy(p, ptr, PTR_SIZE);
- ifree(ptr);
- return (p);
- }
- index = ptr2index(ptr);
- if (index < malloc_pageshift) {
- wrtwarning("junk pointer, too low to make sense");
- return (NULL);
- }
- if (index > last_index) {
- wrtwarning("junk pointer, too high to make sense");
- return (NULL);
- }
- pdir_lookup(index, &pi);
- #ifdef MALLOC_EXTRA_SANITY
- pidx = PI_IDX(index);
- if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
- wrterror("(ES): mapped pages not found in directory");
- errno = EFAULT;
- return (NULL);
- }
- #endif
- if (pi != last_dir) {
- prev_dir = last_dir;
- last_dir = pi;
- }
- pd = pi->base;
- mp = &pd[PI_OFF(index)];
- if (*mp == MALLOC_FIRST) {
-
- if ((u_long) ptr & malloc_pagemask) {
- wrtwarning("modified (page-) pointer");
- return (NULL);
- }
-
- i = index;
- if (!PI_OFF(++i)) {
- pi = pi->next;
- if (pi != NULL && PD_IDX(pi->dirnum) != PI_IDX(i))
- pi = NULL;
- if (pi != NULL)
- pd = pi->base;
- }
- for (osize = malloc_pagesize;
- pi != NULL && pd[PI_OFF(i)] == MALLOC_FOLLOW;) {
- osize += malloc_pagesize;
- if (!PI_OFF(++i)) {
- pi = pi->next;
- if (pi != NULL && PD_IDX(pi->dirnum) != PI_IDX(i))
- pi = NULL;
- if (pi != NULL)
- pd = pi->base;
- }
- }
- if (!malloc_realloc && size <= osize &&
- size > osize - malloc_pagesize) {
- if (malloc_junk)
- memset((char *)ptr + size, SOME_JUNK, osize - size);
- return (ptr);
- }
- } else if (*mp >= MALLOC_MAGIC) {
-
- if ((u_long) ptr & ((1UL << ((*mp)->shift)) - 1)) {
- wrtwarning("modified (chunk-) pointer");
- return (NULL);
- }
-
- i = ((u_long) ptr & malloc_pagemask) >> (*mp)->shift;
-
- if ((*mp)->bits[i / MALLOC_BITS] & (1UL << (i % MALLOC_BITS))) {
- wrtwarning("chunk is already free");
- return (NULL);
- }
- osize = (*mp)->size;
- if (!malloc_realloc && size <= osize &&
- (size > osize / 2 || osize == malloc_minsize)) {
- if (malloc_junk)
- memset((char *) ptr + size, SOME_JUNK, osize - size);
- return (ptr);
- }
- } else {
- wrtwarning("irealloc: pointer to wrong page");
- return (NULL);
- }
- p = imalloc(size);
- if (p != NULL) {
-
-
- if (osize != 0 && size != 0) {
- if (osize < size)
- memcpy(p, ptr, osize);
- else
- memcpy(p, ptr, size);
- }
- ifree(ptr);
- }
- return (p);
- }
- static __inline__ void
- free_pages(void *ptr, u_long index, struct pginfo * info)
- {
- u_long i, pidx, lidx;
- size_t l, cachesize = 0;
- struct pginfo **pd;
- struct pdinfo *pi, *spi;
- struct pgfree *pf, *pt = NULL;
- caddr_t tail;
- if (info == MALLOC_FREE) {
- wrtwarning("page is already free");
- return;
- }
- if (info != MALLOC_FIRST) {
- wrtwarning("free_pages: pointer to wrong page");
- return;
- }
- if ((u_long) ptr & malloc_pagemask) {
- wrtwarning("modified (page-) pointer");
- return;
- }
-
- pidx = PI_IDX(index);
- pdir_lookup(index, &pi);
- #ifdef MALLOC_EXTRA_SANITY
- if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
- wrterror("(ES): mapped pages not found in directory");
- errno = EFAULT;
- return;
- }
- #endif
- spi = pi;
- pd = pi->base;
- pd[PI_OFF(index)] = MALLOC_FREE;
- i = 1;
- if (!PI_OFF(index + i)) {
- pi = pi->next;
- if (pi == NULL || PD_IDX(pi->dirnum) != PI_IDX(index + i))
- pi = NULL;
- else
- pd = pi->base;
- }
- while (pi != NULL && pd[PI_OFF(index + i)] == MALLOC_FOLLOW) {
- pd[PI_OFF(index + i)] = MALLOC_FREE;
- i++;
- if (!PI_OFF(index + i)) {
- if ((pi = pi->next) == NULL ||
- PD_IDX(pi->dirnum) != PI_IDX(index + i))
- pi = NULL;
- else
- pd = pi->base;
- }
- }
- l = i << malloc_pageshift;
- if (malloc_junk)
- memset(ptr, SOME_JUNK, l);
- malloc_used -= l;
- malloc_guarded -= malloc_guard;
- if (malloc_guard) {
- #ifdef MALLOC_EXTRA_SANITY
- if (pi == NULL || PD_IDX(pi->dirnum) != PI_IDX(index + i)) {
- wrterror("(ES): hole in mapped pages directory");
- errno = EFAULT;
- return;
- }
- #endif
- pd[PI_OFF(index + i)] = MALLOC_FREE;
- l += malloc_guard;
- }
- tail = (caddr_t)ptr + l;
- if (malloc_hint)
- madvise(ptr, l, MADV_FREE);
- if (malloc_freeprot)
- mprotect(ptr, l, PROT_NONE);
-
- if (px == NULL && (px = malloc_bytes(sizeof *px)) == NULL)
- goto not_return;
- px->page = ptr;
- px->pdir = spi;
- px->size = l;
- if (free_list.next == NULL) {
-
- px->next = NULL;
- px->prev = &free_list;
- free_list.next = px;
- pf = px;
- px = NULL;
- } else {
-
-
- for (pf = free_list.next;
- (caddr_t)ptr > ((caddr_t)pf->page + pf->size)
- && pf->next != NULL;
- pf = pf->next)
- cachesize += pf->size;
-
- pt = pf;
- while (pt) {
- cachesize += pt->size;
- pt = pt->next;
- }
- if ((caddr_t)pf->page > tail) {
-
- px->next = pf;
- px->prev = pf->prev;
- pf->prev = px;
- px->prev->next = px;
- pf = px;
- px = NULL;
- } else if (((caddr_t)pf->page + pf->size) == ptr) {
-
- cachesize -= pf->size;
- pf->size += l;
- if (pf->next != NULL &&
- pf->next->page == ((caddr_t)pf->page + pf->size)) {
-
- pt = pf->next;
- pf->size += pt->size;
- pf->next = pt->next;
- if (pf->next != NULL)
- pf->next->prev = pf;
- }
- } else if (pf->page == tail) {
-
- cachesize -= pf->size;
- pf->size += l;
- pf->page = ptr;
- pf->pdir = spi;
- } else if (pf->next == NULL) {
-
- px->next = NULL;
- px->prev = pf;
- pf->next = px;
- pf = px;
- px = NULL;
- } else {
- wrterror("freelist is destroyed");
- errno = EFAULT;
- return;
- }
- }
- if (pf->pdir != last_dir) {
- prev_dir = last_dir;
- last_dir = pf->pdir;
- }
-
- if (pf->size > (malloc_cache - cachesize)) {
-
- if (munmap((char *) pf->page + (malloc_cache - cachesize),
- pf->size - (malloc_cache - cachesize)) != 0)
- goto not_return;
- tail = (caddr_t)pf->page + pf->size;
- lidx = ptr2index(tail) - 1;
- pf->size = malloc_cache - cachesize;
- index = ptr2index((caddr_t)pf->page + pf->size);
- pidx = PI_IDX(index);
- if (prev_dir != NULL && PD_IDX(prev_dir->dirnum) >= pidx)
- prev_dir = NULL;
- for (pi = pf->pdir; pi != NULL && PD_IDX(pi->dirnum) < pidx;
- pi = pi->next)
- ;
- spi = pi;
- if (pi != NULL && PD_IDX(pi->dirnum) == pidx) {
- pd = pi->base;
- for (i = index; i <= lidx;) {
- if (pd[PI_OFF(i)] != MALLOC_NOT_MINE) {
- pd[PI_OFF(i)] = MALLOC_NOT_MINE;
- #ifdef MALLOC_EXTRA_SANITY
- if (!PD_OFF(pi->dirnum)) {
- wrterror("(ES): pages directory underflow");
- errno = EFAULT;
- return;
- }
- #endif
- pi->dirnum--;
- }
- #ifdef MALLOC_EXTRA_SANITY
- else
- wrtwarning("(ES): page already unmapped");
- #endif
- i++;
- if (!PI_OFF(i)) {
-
- if (!PD_OFF(pi->dirnum)) {
-
- if (spi == pi)
- spi = pi->prev;
- if (pi->prev != NULL)
- pi->prev->next = pi->next;
- if (pi->next != NULL)
- pi->next->prev = pi->prev;
- pi = pi->next;
- munmap(pd, malloc_pagesize);
- } else
- pi = pi->next;
- if (pi == NULL ||
- PD_IDX(pi->dirnum) != PI_IDX(i))
- break;
- pd = pi->base;
- }
- }
- if (pi && !PD_OFF(pi->dirnum)) {
-
-
- if (spi == pi)
- spi = pi->prev;
- if (pi->prev != NULL)
- pi->prev->next = pi->next;
- if (pi->next != NULL)
- pi->next->prev = pi->prev;
- pi = pi->next;
- munmap(pd, malloc_pagesize);
- }
- }
- if (pi == NULL && malloc_brk == tail) {
-
- last_index = index - 1;
- malloc_brk = index2ptr(index);
- }
-
- if (pf->size == 0) {
- if (px)
- ifree(px);
- if ((px = pf->prev) != &free_list) {
- if (pi == NULL && last_index == (index - 1)) {
- if (spi == NULL) {
- malloc_brk = NULL;
- i = 11;
- } else {
- pd = spi->base;
- if (PD_IDX(spi->dirnum) < pidx)
- index =
- ((PD_IDX(spi->dirnum) + 1) *
- pdi_mod) - 1;
- for (pi = spi, i = index;
- pd[PI_OFF(i)] == MALLOC_NOT_MINE;
- i--)
- #ifdef MALLOC_EXTRA_SANITY
- if (!PI_OFF(i)) {
- pi = pi->prev;
- if (pi == NULL || i == 0)
- break;
- pd = pi->base;
- i = (PD_IDX(pi->dirnum) + 1) * pdi_mod;
- }
- #else
- {
- }
- #endif
- malloc_brk = index2ptr(i + 1);
- }
- last_index = i;
- }
- if ((px->next = pf->next) != NULL)
- px->next->prev = px;
- } else {
- if ((free_list.next = pf->next) != NULL)
- free_list.next->prev = &free_list;
- }
- px = pf;
- last_dir = prev_dir;
- prev_dir = NULL;
- }
- }
- not_return:
- if (pt != NULL)
- ifree(pt);
- }
- static __inline__ void
- free_bytes(void *ptr, u_long index, struct pginfo * info)
- {
- struct pginfo **mp, **pd;
- struct pdinfo *pi;
- #ifdef MALLOC_EXTRA_SANITY
- u_long pidx;
- #endif
- void *vp;
- long i;
-
- i = ((u_long) ptr & malloc_pagemask) >> info->shift;
- if ((u_long) ptr & ((1UL << (info->shift)) - 1)) {
- wrtwarning("modified (chunk-) pointer");
- return;
- }
- if (info->bits[i / MALLOC_BITS] & (1UL << (i % MALLOC_BITS))) {
- wrtwarning("chunk is already free");
- return;
- }
- if (malloc_junk && info->size != 0)
- memset(ptr, SOME_JUNK, (size_t)info->size);
- info->bits[i / MALLOC_BITS] |= 1UL << (i % MALLOC_BITS);
- info->free++;
- if (info->size != 0)
- mp = page_dir + info->shift;
- else
- mp = page_dir;
- if (info->free == 1) {
-
-
- while (*mp != NULL && (*mp)->next != NULL &&
- (*mp)->next->page < info->page)
- mp = &(*mp)->next;
- info->next = *mp;
- *mp = info;
- return;
- }
- if (info->free != info->total)
- return;
-
- while (*mp != info) {
- mp = &((*mp)->next);
- #ifdef MALLOC_EXTRA_SANITY
- if (!*mp) {
- wrterror("(ES): Not on queue");
- errno = EFAULT;
- return;
- }
- #endif
- }
- *mp = info->next;
-
- pdir_lookup(ptr2index(info->page), &pi);
- #ifdef MALLOC_EXTRA_SANITY
- pidx = PI_IDX(ptr2index(info->page));
- if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
- wrterror("(ES): mapped pages not found in directory");
- errno = EFAULT;
- return;
- }
- #endif
- if (pi != last_dir) {
- prev_dir = last_dir;
- last_dir = pi;
- }
- pd = pi->base;
- pd[PI_OFF(ptr2index(info->page))] = MALLOC_FIRST;
-
- if (info->size == 0)
- mprotect(info->page, malloc_pagesize, PROT_READ | PROT_WRITE);
- vp = info->page;
- if (vp != (void *) info)
- ifree(info);
- ifree(vp);
- }
- static void
- ifree(void *ptr)
- {
- struct pginfo *info, **pd;
- u_long index;
- #ifdef MALLOC_EXTRA_SANITY
- u_long pidx;
- #endif
- struct pdinfo *pi;
- if (!malloc_started) {
- wrtwarning("malloc() has never been called");
- return;
- }
-
- if (suicide)
- return;
- if (malloc_ptrguard && PTR_ALIGNED(ptr))
- ptr = (char *) ptr - PTR_GAP;
- index = ptr2index(ptr);
- if (index < malloc_pageshift) {
- warnx("(%p)", ptr);
- wrtwarning("ifree: junk pointer, too low to make sense");
- return;
- }
- if (index > last_index) {
- warnx("(%p)", ptr);
- wrtwarning("ifree: junk pointer, too high to make sense");
- return;
- }
- pdir_lookup(index, &pi);
- #ifdef MALLOC_EXTRA_SANITY
- pidx = PI_IDX(index);
- if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
- wrterror("(ES): mapped pages not found in directory");
- errno = EFAULT;
- return;
- }
- #endif
- if (pi != last_dir) {
- prev_dir = last_dir;
- last_dir = pi;
- }
- pd = pi->base;
- info = pd[PI_OFF(index)];
- if (info < MALLOC_MAGIC)
- free_pages(ptr, index, info);
- else
- free_bytes(ptr, index, info);
-
- if (px == NULL)
- px = malloc_bytes(sizeof *px);
- return;
- }
- static void
- malloc_recurse(void)
- {
- static int noprint;
- if (noprint == 0) {
- noprint = 1;
- wrtwarning("recursive call");
- }
- malloc_active--;
- _MALLOC_UNLOCK();
- errno = EDEADLK;
- }
- void *
- malloc(size_t size)
- {
- void *r;
- if (!align)
- _MALLOC_LOCK();
- malloc_func = " in malloc():";
- if (malloc_active++) {
- malloc_recurse();
- return (NULL);
- }
- r = imalloc(size);
- UTRACE(0, size, r);
- malloc_active--;
- if (!align)
- _MALLOC_UNLOCK();
- if (malloc_xmalloc && r == NULL) {
- wrterror("out of memory");
- errno = ENOMEM;
- }
- return (r);
- }
- void
- free(void *ptr)
- {
-
- if (ptr == NULL)
- return;
- _MALLOC_LOCK();
- malloc_func = " in free():";
- if (malloc_active++) {
- malloc_recurse();
- return;
- }
- ifree(ptr);
- UTRACE(ptr, 0, 0);
- malloc_active--;
- _MALLOC_UNLOCK();
- return;
- }
- void *
- realloc(void *ptr, size_t size)
- {
- void *r;
- _MALLOC_LOCK();
- malloc_func = " in realloc():";
- if (malloc_active++) {
- malloc_recurse();
- return (NULL);
- }
- if (ptr == NULL)
- r = imalloc(size);
- else
- r = irealloc(ptr, size);
- UTRACE(ptr, size, r);
- malloc_active--;
- _MALLOC_UNLOCK();
- if (malloc_xmalloc && r == NULL) {
- wrterror("out of memory");
- errno = ENOMEM;
- }
- return (r);
- }
- #if defined(__i386__)||defined(__arm__)||defined(__powerpc__)
- #define SIZE_MAX 0xffffffff
- #endif
- #if defined(__x86_64__)
- #define SIZE_MAX 0xffffffffffffffff
- #endif
- void *
- calloc(size_t num, size_t size)
- {
- void *p;
-
- if (num && SIZE_MAX / num < size) {
- fprintf(stderr,"OOOOPS");
- errno = ENOMEM;
- return NULL;
- }
- size *= num;
- p = malloc(size);
- if (p)
- memset(p, 0, size);
- return(p);
- }
- static int ispowerof2 (size_t a) {
- size_t b;
- for (b = 1ULL << (sizeof(size_t)*NBBY - 1); b > 1; b >>= 1)
- if (b == a)
- return 1;
- return 0;
- }
- int posix_memalign(void **memptr, size_t alignment, size_t size)
- {
- void *r;
- if ((alignment < PTR_SIZE) || (alignment%PTR_SIZE != 0)) return EINVAL;
- if (!ispowerof2(alignment)) return EINVAL;
- if (alignment < malloc_minsize) alignment = malloc_minsize;
- size_t max = alignment > size ? alignment : size;
- if (alignment <= malloc_pagesize)
- r = malloc(max);
- else {
- _MALLOC_LOCK();
- align = 1;
- g_alignment = alignment;
- r = malloc(size);
- align=0;
- _MALLOC_UNLOCK();
- }
- *memptr = r;
- if (!r) return ENOMEM;
- return 0;
- }
- void *memalign(size_t boundary, size_t size)
- {
- void *r;
- posix_memalign(&r, boundary, size);
- return r;
- }
- void *valloc(size_t size)
- {
- void *r;
- posix_memalign(&r, malloc_pagesize, size);
- return r;
- }
|