OpenBSD_malloc_Linux.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032
  1. /* Version 1.83 for Linux.
  2. * Compilation: gcc -shared -fPIC -O2 OpenBSD_malloc_Linux.c -o malloc.so
  3. * Launching: LD_PRELOAD=/path/to/malloc.so firefox
  4. */
  5. /* $OpenBSD: malloc.c,v 1.83 2006/05/14 19:53:40 otto Exp $ */
  6. /*
  7. * ----------------------------------------------------------------------------
  8. * "THE BEER-WARE LICENSE" (Revision 42):
  9. * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
  10. * can do whatever you want with this stuff. If we meet some day, and you think
  11. * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
  12. * ----------------------------------------------------------------------------
  13. */
  14. /*
  15. * Defining MALLOC_EXTRA_SANITY will enable extra checks which are
  16. * related to internal conditions and consistency in malloc.c. This has
  17. * a noticeable runtime performance hit, and generally will not do you
  18. * any good unless you fiddle with the internals of malloc or want
  19. * to catch random pointer corruption as early as possible.
  20. */
  21. #ifndef MALLOC_EXTRA_SANITY
  22. #undef MALLOC_EXTRA_SANITY
  23. #endif
  24. /*
  25. * Defining MALLOC_STATS will enable you to call malloc_dump() and set
  26. * the [dD] options in the MALLOC_OPTIONS environment variable.
  27. * It has no run-time performance hit, but does pull in stdio...
  28. */
  29. #ifndef MALLOC_STATS
  30. #undef MALLOC_STATS
  31. #endif
  32. /*
  33. * What to use for Junk. This is the byte value we use to fill with
  34. * when the 'J' option is enabled.
  35. */
  36. #define SOME_JUNK 0xd0 /* as in "Duh" :-) */
  37. #include <sys/types.h>
  38. #include <sys/time.h>
  39. #include <sys/resource.h>
  40. #include <sys/param.h>
  41. #include <sys/mman.h>
  42. #include <sys/uio.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <limits.h>
  49. #include <errno.h>
  50. #include <err.h>
  51. /* For SIZE_T_MAX */
  52. #include "torint.h"
  53. //#include "thread_private.h"
  54. /*
  55. * The basic parameters you can tweak.
  56. *
  57. * malloc_pageshift pagesize = 1 << malloc_pageshift
  58. * It's probably best if this is the native
  59. * page size, but it shouldn't have to be.
  60. *
  61. * malloc_minsize minimum size of an allocation in bytes.
  62. * If this is too small it's too much work
  63. * to manage them. This is also the smallest
  64. * unit of alignment used for the storage
  65. * returned by malloc/realloc.
  66. *
  67. */
  68. static int align = 0;
  69. static size_t g_alignment = 0;
  70. extern int __libc_enable_secure;
  71. static int issetugid(void)
  72. {
  73. if (__libc_enable_secure) return 1;
  74. if (getuid() != geteuid()) return 1;
  75. if (getgid() != getegid()) return 1;
  76. return 0;
  77. }
  78. #define PGSHIFT 12
  79. #define MADV_FREE MADV_DONTNEED
  80. #include <pthread.h>
  81. static pthread_mutex_t gen_mutex = PTHREAD_MUTEX_INITIALIZER;
  82. #define _MALLOC_LOCK_INIT() {;}
  83. #define _MALLOC_LOCK() {pthread_mutex_lock(&gen_mutex);}
  84. #define _MALLOC_UNLOCK() {pthread_mutex_unlock(&gen_mutex);}
  85. #if defined(__sparc__) || defined(__alpha__)
  86. #define malloc_pageshift 13U
  87. #endif
  88. #if defined(__ia64__)
  89. #define malloc_pageshift 14U
  90. #endif
  91. #ifndef malloc_pageshift
  92. #define malloc_pageshift (PGSHIFT)
  93. #endif
  94. /*
  95. * No user serviceable parts behind this point.
  96. *
  97. * This structure describes a page worth of chunks.
  98. */
  99. struct pginfo {
  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_long bits[1];/* Which chunks are free */
  107. };
  108. /* How many bits per u_long in the bitmap */
  109. #define MALLOC_BITS (int)((NBBY * sizeof(u_long)))
  110. /*
  111. * This structure describes a number of free pages.
  112. */
  113. struct pgfree {
  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 *pdir; /* pointer to the base page's dir */
  118. size_t size; /* number of bytes free */
  119. };
  120. /*
  121. * Magic values to put in the page_directory
  122. */
  123. #define MALLOC_NOT_MINE ((struct pginfo*) 0)
  124. #define MALLOC_FREE ((struct pginfo*) 1)
  125. #define MALLOC_FIRST ((struct pginfo*) 2)
  126. #define MALLOC_FOLLOW ((struct pginfo*) 3)
  127. #define MALLOC_MAGIC ((struct pginfo*) 4)
  128. #ifndef malloc_minsize
  129. #define malloc_minsize 16UL
  130. #endif
  131. #if !defined(malloc_pagesize)
  132. #define malloc_pagesize (1UL<<malloc_pageshift)
  133. #endif
  134. #if ((1UL<<malloc_pageshift) != malloc_pagesize)
  135. #error "(1UL<<malloc_pageshift) != malloc_pagesize"
  136. #endif
  137. #ifndef malloc_maxsize
  138. #define malloc_maxsize ((malloc_pagesize)>>1)
  139. #endif
  140. /* A mask for the offset inside a page. */
  141. #define malloc_pagemask ((malloc_pagesize)-1)
  142. #define pageround(foo) (((foo) + (malloc_pagemask)) & ~malloc_pagemask)
  143. #define ptr2index(foo) (((u_long)(foo) >> malloc_pageshift)+malloc_pageshift)
  144. #define index2ptr(idx) ((void*)(((idx)-malloc_pageshift)<<malloc_pageshift))
  145. /* Set when initialization has been done */
  146. static unsigned int malloc_started;
  147. /* Number of free pages we cache */
  148. static unsigned int malloc_cache = 16;
  149. /* Structure used for linking discrete directory pages. */
  150. struct pdinfo {
  151. struct pginfo **base;
  152. struct pdinfo *prev;
  153. struct pdinfo *next;
  154. u_long dirnum;
  155. };
  156. static struct pdinfo *last_dir; /* Caches to the last and previous */
  157. static struct pdinfo *prev_dir; /* referenced directory pages. */
  158. static size_t pdi_off;
  159. static u_long pdi_mod;
  160. #define PD_IDX(num) ((num) / (malloc_pagesize/sizeof(struct pginfo *)))
  161. #define PD_OFF(num) ((num) & ((malloc_pagesize/sizeof(struct pginfo *))-1))
  162. #define PI_IDX(index) ((index) / pdi_mod)
  163. #define PI_OFF(index) ((index) % pdi_mod)
  164. /* The last index in the page directory we care about */
  165. static u_long last_index;
  166. /* Pointer to page directory. Allocated "as if with" malloc */
  167. static struct pginfo **page_dir;
  168. /* Free pages line up here */
  169. static struct pgfree free_list;
  170. /* Abort(), user doesn't handle problems. */
  171. static int malloc_abort = 2;
  172. /* Are we trying to die ? */
  173. static int suicide;
  174. #ifdef MALLOC_STATS
  175. /* dump statistics */
  176. static int malloc_stats;
  177. #endif
  178. /* avoid outputting warnings? */
  179. static int malloc_silent;
  180. /* always realloc ? */
  181. static int malloc_realloc;
  182. /* mprotect free pages PROT_NONE? */
  183. static int malloc_freeprot;
  184. /* use guard pages after allocations? */
  185. static size_t malloc_guard = 0;
  186. static size_t malloc_guarded;
  187. /* align pointers to end of page? */
  188. static int malloc_ptrguard;
  189. static int malloc_hint = 1;
  190. /* xmalloc behaviour ? */
  191. static int malloc_xmalloc;
  192. /* zero fill ? */
  193. static int malloc_zero;
  194. /* junk fill ? */
  195. static int malloc_junk;
  196. #ifdef __FreeBSD__
  197. /* utrace ? */
  198. static int malloc_utrace;
  199. struct ut {
  200. void *p;
  201. size_t s;
  202. void *r;
  203. };
  204. void utrace(struct ut *, int);
  205. #define UTRACE(a, b, c) \
  206. if (malloc_utrace) \
  207. {struct ut u; u.p=a; u.s = b; u.r=c; utrace(&u, sizeof u);}
  208. #else /* !__FreeBSD__ */
  209. #define UTRACE(a,b,c)
  210. #endif
  211. /* Status of malloc. */
  212. static int malloc_active;
  213. /* Allocated memory. */
  214. static size_t malloc_used;
  215. /* My last break. */
  216. static caddr_t malloc_brk;
  217. /* One location cache for free-list holders. */
  218. static struct pgfree *px;
  219. /* Compile-time options. */
  220. char *malloc_options;
  221. /* Name of the current public function. */
  222. static const char *malloc_func;
  223. #define MMAP(size) \
  224. mmap((void *)0, (size), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, \
  225. -1, (off_t)0)
  226. /*
  227. * Necessary function declarations.
  228. */
  229. static void *imalloc(size_t size);
  230. static void ifree(void *ptr);
  231. static void *irealloc(void *ptr, size_t size);
  232. static void *malloc_bytes(size_t size);
  233. /*
  234. * Function for page directory lookup.
  235. */
  236. static int
  237. pdir_lookup(u_long index, struct pdinfo ** pdi)
  238. {
  239. struct pdinfo *spi;
  240. u_long pidx = PI_IDX(index);
  241. if (last_dir != NULL && PD_IDX(last_dir->dirnum) == pidx)
  242. *pdi = last_dir;
  243. else if (prev_dir != NULL && PD_IDX(prev_dir->dirnum) == pidx)
  244. *pdi = prev_dir;
  245. else if (last_dir != NULL && prev_dir != NULL) {
  246. if ((PD_IDX(last_dir->dirnum) > pidx) ?
  247. (PD_IDX(last_dir->dirnum) - pidx) :
  248. (pidx - PD_IDX(last_dir->dirnum))
  249. < (PD_IDX(prev_dir->dirnum) > pidx) ?
  250. (PD_IDX(prev_dir->dirnum) - pidx) :
  251. (pidx - PD_IDX(prev_dir->dirnum)))
  252. *pdi = last_dir;
  253. else
  254. *pdi = prev_dir;
  255. if (PD_IDX((*pdi)->dirnum) > pidx) {
  256. for (spi = (*pdi)->prev;
  257. spi != NULL && PD_IDX(spi->dirnum) > pidx;
  258. spi = spi->prev)
  259. *pdi = spi;
  260. if (spi != NULL)
  261. *pdi = spi;
  262. } else
  263. for (spi = (*pdi)->next;
  264. spi != NULL && PD_IDX(spi->dirnum) <= pidx;
  265. spi = spi->next)
  266. *pdi = spi;
  267. } else {
  268. *pdi = (struct pdinfo *) ((caddr_t) page_dir + pdi_off);
  269. for (spi = *pdi;
  270. spi != NULL && PD_IDX(spi->dirnum) <= pidx;
  271. spi = spi->next)
  272. *pdi = spi;
  273. }
  274. return ((PD_IDX((*pdi)->dirnum) == pidx) ? 0 :
  275. (PD_IDX((*pdi)->dirnum) > pidx) ? 1 : -1);
  276. }
  277. #ifdef MALLOC_STATS
  278. void
  279. malloc_dump(int fd)
  280. {
  281. char buf[1024];
  282. struct pginfo **pd;
  283. struct pgfree *pf;
  284. struct pdinfo *pi;
  285. u_long j;
  286. pd = page_dir;
  287. pi = (struct pdinfo *) ((caddr_t) pd + pdi_off);
  288. /* print out all the pages */
  289. for (j = 0; j <= last_index;) {
  290. snprintf(buf, sizeof buf, "%08lx %5lu ", j << malloc_pageshift, j);
  291. write(fd, buf, strlen(buf));
  292. if (pd[PI_OFF(j)] == MALLOC_NOT_MINE) {
  293. for (j++; j <= last_index && pd[PI_OFF(j)] == MALLOC_NOT_MINE;) {
  294. if (!PI_OFF(++j)) {
  295. if ((pi = pi->next) == NULL ||
  296. PD_IDX(pi->dirnum) != PI_IDX(j))
  297. break;
  298. pd = pi->base;
  299. j += pdi_mod;
  300. }
  301. }
  302. j--;
  303. snprintf(buf, sizeof buf, ".. %5lu not mine\n", j);
  304. write(fd, buf, strlen(buf));
  305. } else if (pd[PI_OFF(j)] == MALLOC_FREE) {
  306. for (j++; j <= last_index && pd[PI_OFF(j)] == MALLOC_FREE;) {
  307. if (!PI_OFF(++j)) {
  308. if ((pi = pi->next) == NULL ||
  309. PD_IDX(pi->dirnum) != PI_IDX(j))
  310. break;
  311. pd = pi->base;
  312. j += pdi_mod;
  313. }
  314. }
  315. j--;
  316. snprintf(buf, sizeof buf, ".. %5lu free\n", j);
  317. write(fd, buf, strlen(buf));
  318. } else if (pd[PI_OFF(j)] == MALLOC_FIRST) {
  319. for (j++; j <= last_index && pd[PI_OFF(j)] == MALLOC_FOLLOW;) {
  320. if (!PI_OFF(++j)) {
  321. if ((pi = pi->next) == NULL ||
  322. PD_IDX(pi->dirnum) != PI_IDX(j))
  323. break;
  324. pd = pi->base;
  325. j += pdi_mod;
  326. }
  327. }
  328. j--;
  329. snprintf(buf, sizeof buf, ".. %5lu in use\n", j);
  330. write(fd, buf, strlen(buf));
  331. } else if (pd[PI_OFF(j)] < MALLOC_MAGIC) {
  332. snprintf(buf, sizeof buf, "(%p)\n", pd[PI_OFF(j)]);
  333. write(fd, buf, strlen(buf));
  334. } else {
  335. snprintf(buf, sizeof buf, "%p %d (of %d) x %d @ %p --> %p\n",
  336. pd[PI_OFF(j)], pd[PI_OFF(j)]->free,
  337. pd[PI_OFF(j)]->total, pd[PI_OFF(j)]->size,
  338. pd[PI_OFF(j)]->page, pd[PI_OFF(j)]->next);
  339. write(fd, buf, strlen(buf));
  340. }
  341. if (!PI_OFF(++j)) {
  342. if ((pi = pi->next) == NULL)
  343. break;
  344. pd = pi->base;
  345. j += (1 + PD_IDX(pi->dirnum) - PI_IDX(j)) * pdi_mod;
  346. }
  347. }
  348. for (pf = free_list.next; pf; pf = pf->next) {
  349. snprintf(buf, sizeof buf, "Free: @%p [%p...%p[ %ld ->%p <-%p\n",
  350. pf, pf->page, (char *)pf->page + pf->size,
  351. pf->size, pf->prev, pf->next);
  352. write(fd, buf, strlen(buf));
  353. if (pf == pf->next) {
  354. snprintf(buf, sizeof buf, "Free_list loops\n");
  355. write(fd, buf, strlen(buf));
  356. break;
  357. }
  358. }
  359. /* print out various info */
  360. snprintf(buf, sizeof buf, "Minsize\t%lu\n", malloc_minsize);
  361. write(fd, buf, strlen(buf));
  362. snprintf(buf, sizeof buf, "Maxsize\t%lu\n", malloc_maxsize);
  363. write(fd, buf, strlen(buf));
  364. snprintf(buf, sizeof buf, "Pagesize\t%lu\n", malloc_pagesize);
  365. write(fd, buf, strlen(buf));
  366. snprintf(buf, sizeof buf, "Pageshift\t%u\n", malloc_pageshift);
  367. write(fd, buf, strlen(buf));
  368. snprintf(buf, sizeof buf, "In use\t%lu\n", (u_long) malloc_used);
  369. write(fd, buf, strlen(buf));
  370. snprintf(buf, sizeof buf, "Guarded\t%lu\n", (u_long) malloc_guarded);
  371. write(fd, buf, strlen(buf));
  372. }
  373. #endif /* MALLOC_STATS */
  374. extern char *__progname;
  375. static void
  376. wrterror(const char *p)
  377. {
  378. const char *q = " error: ";
  379. struct iovec iov[5];
  380. iov[0].iov_base = __progname;
  381. iov[0].iov_len = strlen(__progname);
  382. iov[1].iov_base = (char*)malloc_func;
  383. iov[1].iov_len = strlen(malloc_func);
  384. iov[2].iov_base = (char*)q;
  385. iov[2].iov_len = strlen(q);
  386. iov[3].iov_base = (char*)p;
  387. iov[3].iov_len = strlen(p);
  388. iov[4].iov_base = (char*)"\n";
  389. iov[4].iov_len = 1;
  390. writev(STDERR_FILENO, iov, 5);
  391. suicide = 1;
  392. #ifdef MALLOC_STATS
  393. if (malloc_stats)
  394. malloc_dump(STDERR_FILENO);
  395. #endif /* MALLOC_STATS */
  396. malloc_active--;
  397. if (malloc_abort)
  398. abort();
  399. }
  400. static void
  401. wrtwarning(const char *p)
  402. {
  403. const char *q = " warning: ";
  404. struct iovec iov[5];
  405. if (malloc_abort)
  406. wrterror(p);
  407. else if (malloc_silent)
  408. return;
  409. iov[0].iov_base = __progname;
  410. iov[0].iov_len = strlen(__progname);
  411. iov[1].iov_base = (char*)malloc_func;
  412. iov[1].iov_len = strlen(malloc_func);
  413. iov[2].iov_base = (char*)q;
  414. iov[2].iov_len = strlen(q);
  415. iov[3].iov_base = (char*)p;
  416. iov[3].iov_len = strlen(p);
  417. iov[4].iov_base = (char*)"\n";
  418. iov[4].iov_len = 1;
  419. writev(STDERR_FILENO, iov, 5);
  420. }
  421. #ifdef MALLOC_STATS
  422. static void
  423. malloc_exit(void)
  424. {
  425. char *q = "malloc() warning: Couldn't dump stats\n";
  426. int save_errno = errno, fd;
  427. fd = open("malloc.out", O_RDWR|O_APPEND);
  428. if (fd != -1) {
  429. malloc_dump(fd);
  430. close(fd);
  431. } else
  432. write(STDERR_FILENO, q, strlen(q));
  433. errno = save_errno;
  434. }
  435. #endif /* MALLOC_STATS */
  436. /*
  437. * Allocate aligned mmaped chunk
  438. */
  439. static void *MMAP_A(size_t pages, size_t alignment)
  440. {
  441. void *j, *p;
  442. size_t first_size, rest, begin, end;
  443. if (pages%malloc_pagesize != 0)
  444. pages = pages - pages%malloc_pagesize + malloc_pagesize;
  445. first_size = pages + alignment - malloc_pagesize;
  446. p = MMAP(first_size);
  447. rest = ((size_t)p) % alignment;
  448. j = (rest == 0) ? p : (void*) ((size_t)p + alignment - rest);
  449. begin = (size_t)j - (size_t)p;
  450. if (begin != 0) munmap(p, begin);
  451. end = (size_t)p + first_size - ((size_t)j + pages);
  452. if(end != 0) munmap( (void*) ((size_t)j + pages), end);
  453. return j;
  454. }
  455. /*
  456. * Allocate a number of pages from the OS
  457. */
  458. static void *
  459. map_pages(size_t pages)
  460. {
  461. struct pdinfo *pi, *spi;
  462. struct pginfo **pd;
  463. u_long idx, pidx, lidx;
  464. caddr_t result, tail;
  465. u_long index, lindex;
  466. void *pdregion = NULL;
  467. size_t dirs, cnt;
  468. pages <<= malloc_pageshift;
  469. if (!align)
  470. result = MMAP(pages + malloc_guard);
  471. else {
  472. result = MMAP_A(pages + malloc_guard, g_alignment);
  473. }
  474. if (result == MAP_FAILED) {
  475. #ifdef MALLOC_EXTRA_SANITY
  476. wrtwarning("(ES): map_pages fails");
  477. #endif /* MALLOC_EXTRA_SANITY */
  478. errno = ENOMEM;
  479. return (NULL);
  480. }
  481. index = ptr2index(result);
  482. tail = result + pages + malloc_guard;
  483. lindex = ptr2index(tail) - 1;
  484. if (malloc_guard)
  485. mprotect(result + pages, malloc_guard, PROT_NONE);
  486. pidx = PI_IDX(index);
  487. lidx = PI_IDX(lindex);
  488. if (tail > malloc_brk) {
  489. malloc_brk = tail;
  490. last_index = lindex;
  491. }
  492. dirs = lidx - pidx;
  493. /* Insert directory pages, if needed. */
  494. if (pdir_lookup(index, &pi) != 0)
  495. dirs++;
  496. if (dirs > 0) {
  497. pdregion = MMAP(malloc_pagesize * dirs);
  498. if (pdregion == MAP_FAILED) {
  499. munmap(result, tail - result);
  500. #ifdef MALLOC_EXTRA_SANITY
  501. wrtwarning("(ES): map_pages fails");
  502. #endif
  503. errno = ENOMEM;
  504. return (NULL);
  505. }
  506. }
  507. cnt = 0;
  508. for (idx = pidx, spi = pi; idx <= lidx; idx++) {
  509. if (pi == NULL || PD_IDX(pi->dirnum) != idx) {
  510. pd = (struct pginfo **)((char *)pdregion +
  511. cnt * malloc_pagesize);
  512. cnt++;
  513. memset(pd, 0, malloc_pagesize);
  514. pi = (struct pdinfo *) ((caddr_t) pd + pdi_off);
  515. pi->base = pd;
  516. pi->prev = spi;
  517. pi->next = spi->next;
  518. pi->dirnum = idx * (malloc_pagesize /
  519. sizeof(struct pginfo *));
  520. if (spi->next != NULL)
  521. spi->next->prev = pi;
  522. spi->next = pi;
  523. }
  524. if (idx > pidx && idx < lidx) {
  525. pi->dirnum += pdi_mod;
  526. } else if (idx == pidx) {
  527. if (pidx == lidx) {
  528. pi->dirnum += (u_long)(tail - result) >>
  529. malloc_pageshift;
  530. } else {
  531. pi->dirnum += pdi_mod - PI_OFF(index);
  532. }
  533. } else {
  534. pi->dirnum += PI_OFF(ptr2index(tail - 1)) + 1;
  535. }
  536. #ifdef MALLOC_EXTRA_SANITY
  537. if (PD_OFF(pi->dirnum) > pdi_mod || PD_IDX(pi->dirnum) > idx) {
  538. wrterror("(ES): pages directory overflow");
  539. errno = EFAULT;
  540. return (NULL);
  541. }
  542. #endif /* MALLOC_EXTRA_SANITY */
  543. if (idx == pidx && pi != last_dir) {
  544. prev_dir = last_dir;
  545. last_dir = pi;
  546. }
  547. spi = pi;
  548. pi = spi->next;
  549. }
  550. #ifdef MALLOC_EXTRA_SANITY
  551. if (cnt > dirs)
  552. wrtwarning("(ES): cnt > dirs");
  553. #endif /* MALLOC_EXTRA_SANITY */
  554. if (cnt < dirs)
  555. munmap((char *)pdregion + cnt * malloc_pagesize,
  556. (dirs - cnt) * malloc_pagesize);
  557. return (result);
  558. }
  559. /*
  560. * Initialize the world
  561. */
  562. static void
  563. malloc_init(void)
  564. {
  565. char *p, b[64];
  566. int i, j, save_errno = errno;
  567. _MALLOC_LOCK_INIT();
  568. #ifdef MALLOC_EXTRA_SANITY
  569. malloc_junk = 1;
  570. #endif /* MALLOC_EXTRA_SANITY */
  571. for (i = 0; i < 3; i++) {
  572. switch (i) {
  573. case 0:
  574. j = readlink("/etc/malloc.conf", b, sizeof b - 1);
  575. if (j <= 0)
  576. continue;
  577. b[j] = '\0';
  578. p = b;
  579. break;
  580. case 1:
  581. if (issetugid() == 0)
  582. p = getenv("MALLOC_OPTIONS");
  583. else
  584. continue;
  585. break;
  586. case 2:
  587. p = malloc_options;
  588. break;
  589. default:
  590. p = NULL;
  591. }
  592. for (; p != NULL && *p != '\0'; p++) {
  593. switch (*p) {
  594. case '>':
  595. malloc_cache <<= 1;
  596. break;
  597. case '<':
  598. malloc_cache >>= 1;
  599. break;
  600. case 'a':
  601. malloc_abort = 0;
  602. break;
  603. case 'A':
  604. malloc_abort = 1;
  605. break;
  606. #ifdef MALLOC_STATS
  607. case 'd':
  608. malloc_stats = 0;
  609. break;
  610. case 'D':
  611. malloc_stats = 1;
  612. break;
  613. #endif /* MALLOC_STATS */
  614. case 'f':
  615. malloc_freeprot = 0;
  616. break;
  617. case 'F':
  618. malloc_freeprot = 1;
  619. break;
  620. case 'g':
  621. malloc_guard = 0;
  622. break;
  623. case 'G':
  624. malloc_guard = malloc_pagesize;
  625. break;
  626. case 'h':
  627. malloc_hint = 0;
  628. break;
  629. case 'H':
  630. malloc_hint = 1;
  631. break;
  632. case 'j':
  633. malloc_junk = 0;
  634. break;
  635. case 'J':
  636. malloc_junk = 1;
  637. break;
  638. case 'n':
  639. malloc_silent = 0;
  640. break;
  641. case 'N':
  642. malloc_silent = 1;
  643. break;
  644. case 'p':
  645. malloc_ptrguard = 0;
  646. break;
  647. case 'P':
  648. malloc_ptrguard = 1;
  649. break;
  650. case 'r':
  651. malloc_realloc = 0;
  652. break;
  653. case 'R':
  654. malloc_realloc = 1;
  655. break;
  656. #ifdef __FreeBSD__
  657. case 'u':
  658. malloc_utrace = 0;
  659. break;
  660. case 'U':
  661. malloc_utrace = 1;
  662. break;
  663. #endif /* __FreeBSD__ */
  664. case 'x':
  665. malloc_xmalloc = 0;
  666. break;
  667. case 'X':
  668. malloc_xmalloc = 1;
  669. break;
  670. case 'z':
  671. malloc_zero = 0;
  672. break;
  673. case 'Z':
  674. malloc_zero = 1;
  675. break;
  676. default:
  677. j = malloc_abort;
  678. malloc_abort = 0;
  679. wrtwarning("unknown char in MALLOC_OPTIONS");
  680. malloc_abort = j;
  681. break;
  682. }
  683. }
  684. }
  685. UTRACE(0, 0, 0);
  686. /*
  687. * We want junk in the entire allocation, and zero only in the part
  688. * the user asked for.
  689. */
  690. if (malloc_zero)
  691. malloc_junk = 1;
  692. #ifdef MALLOC_STATS
  693. if (malloc_stats && (atexit(malloc_exit) == -1))
  694. wrtwarning("atexit(2) failed."
  695. " Will not be able to dump malloc stats on exit");
  696. #endif /* MALLOC_STATS */
  697. if (malloc_pagesize != getpagesize()) {
  698. wrterror("malloc() replacement compiled with a different "
  699. "page size from what we're running with. Failing.");
  700. errno = ENOMEM;
  701. return;
  702. }
  703. /* Allocate one page for the page directory. */
  704. page_dir = (struct pginfo **)MMAP(malloc_pagesize);
  705. if (page_dir == MAP_FAILED) {
  706. wrterror("mmap(2) failed, check limits");
  707. errno = ENOMEM;
  708. return;
  709. }
  710. pdi_off = (malloc_pagesize - sizeof(struct pdinfo)) & ~(malloc_minsize - 1);
  711. pdi_mod = pdi_off / sizeof(struct pginfo *);
  712. last_dir = (struct pdinfo *) ((caddr_t) page_dir + pdi_off);
  713. last_dir->base = page_dir;
  714. last_dir->prev = last_dir->next = NULL;
  715. last_dir->dirnum = malloc_pageshift;
  716. /* Been here, done that. */
  717. malloc_started++;
  718. /* Recalculate the cache size in bytes, and make sure it's nonzero. */
  719. if (!malloc_cache)
  720. malloc_cache++;
  721. malloc_cache <<= malloc_pageshift;
  722. errno = save_errno;
  723. }
  724. /*
  725. * Allocate a number of complete pages
  726. */
  727. static void *
  728. malloc_pages(size_t size)
  729. {
  730. void *p, *delay_free = NULL, *tp;
  731. size_t i;
  732. struct pginfo **pd;
  733. struct pdinfo *pi;
  734. u_long pidx, index;
  735. struct pgfree *pf;
  736. size = pageround(size) + malloc_guard;
  737. p = NULL;
  738. /* Look for free pages before asking for more */
  739. if (!align)
  740. for (pf = free_list.next; pf; pf = pf->next) {
  741. #ifdef MALLOC_EXTRA_SANITY
  742. if (pf->size & malloc_pagemask) {
  743. wrterror("(ES): junk length entry on free_list");
  744. errno = EFAULT;
  745. return (NULL);
  746. }
  747. if (!pf->size) {
  748. wrterror("(ES): zero length entry on free_list");
  749. errno = EFAULT;
  750. return (NULL);
  751. }
  752. if (pf->page > (pf->page + pf->size)) {
  753. wrterror("(ES): sick entry on free_list");
  754. errno = EFAULT;
  755. return (NULL);
  756. }
  757. if ((pi = pf->pdir) == NULL) {
  758. wrterror("(ES): invalid page directory on free-list");
  759. errno = EFAULT;
  760. return (NULL);
  761. }
  762. if ((pidx = PI_IDX(ptr2index(pf->page))) != PD_IDX(pi->dirnum)) {
  763. wrterror("(ES): directory index mismatch on free-list");
  764. errno = EFAULT;
  765. return (NULL);
  766. }
  767. pd = pi->base;
  768. if (pd[PI_OFF(ptr2index(pf->page))] != MALLOC_FREE) {
  769. wrterror("(ES): non-free first page on free-list");
  770. errno = EFAULT;
  771. return (NULL);
  772. }
  773. pidx = PI_IDX(ptr2index((pf->page) + (pf->size)) - 1);
  774. for (pi = pf->pdir; pi != NULL && PD_IDX(pi->dirnum) < pidx;
  775. pi = pi->next)
  776. ;
  777. if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
  778. wrterror("(ES): last page not referenced in page directory");
  779. errno = EFAULT;
  780. return (NULL);
  781. }
  782. pd = pi->base;
  783. if (pd[PI_OFF(ptr2index((pf->page) + (pf->size)) - 1)] != MALLOC_FREE) {
  784. wrterror("(ES): non-free last page on free-list");
  785. errno = EFAULT;
  786. return (NULL);
  787. }
  788. #endif /* MALLOC_EXTRA_SANITY */
  789. if (pf->size < size)
  790. continue;
  791. if (pf->size == size) {
  792. p = pf->page;
  793. pi = pf->pdir;
  794. if (pf->next != NULL)
  795. pf->next->prev = pf->prev;
  796. pf->prev->next = pf->next;
  797. delay_free = pf;
  798. break;
  799. }
  800. p = pf->page;
  801. pf->page = (char *) pf->page + size;
  802. pf->size -= size;
  803. pidx = PI_IDX(ptr2index(pf->page));
  804. for (pi = pf->pdir; pi != NULL && PD_IDX(pi->dirnum) < pidx;
  805. pi = pi->next)
  806. ;
  807. if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
  808. wrterror("(ES): hole in directories");
  809. errno = EFAULT;
  810. return (NULL);
  811. }
  812. tp = pf->pdir;
  813. pf->pdir = pi;
  814. pi = tp;
  815. break;
  816. }
  817. size -= malloc_guard;
  818. #ifdef MALLOC_EXTRA_SANITY
  819. if (p != NULL && pi != NULL) {
  820. pidx = PD_IDX(pi->dirnum);
  821. pd = pi->base;
  822. }
  823. if (p != NULL && pd[PI_OFF(ptr2index(p))] != MALLOC_FREE) {
  824. wrterror("(ES): allocated non-free page on free-list");
  825. errno = EFAULT;
  826. return (NULL);
  827. }
  828. #endif /* MALLOC_EXTRA_SANITY */
  829. if (p != NULL && (malloc_guard || malloc_freeprot))
  830. mprotect(p, size, PROT_READ | PROT_WRITE);
  831. size >>= malloc_pageshift;
  832. /* Map new pages */
  833. if (p == NULL)
  834. p = map_pages(size);
  835. if (p != NULL) {
  836. index = ptr2index(p);
  837. pidx = PI_IDX(index);
  838. pdir_lookup(index, &pi);
  839. #ifdef MALLOC_EXTRA_SANITY
  840. if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
  841. wrterror("(ES): mapped pages not found in directory");
  842. errno = EFAULT;
  843. return (NULL);
  844. }
  845. #endif /* MALLOC_EXTRA_SANITY */
  846. if (pi != last_dir) {
  847. prev_dir = last_dir;
  848. last_dir = pi;
  849. }
  850. pd = pi->base;
  851. pd[PI_OFF(index)] = MALLOC_FIRST;
  852. for (i = 1; i < size; i++) {
  853. if (!PI_OFF(index + i)) {
  854. pidx++;
  855. pi = pi->next;
  856. #ifdef MALLOC_EXTRA_SANITY
  857. if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
  858. wrterror("(ES): hole in mapped pages directory");
  859. errno = EFAULT;
  860. return (NULL);
  861. }
  862. #endif /* MALLOC_EXTRA_SANITY */
  863. pd = pi->base;
  864. }
  865. pd[PI_OFF(index + i)] = MALLOC_FOLLOW;
  866. }
  867. if (malloc_guard) {
  868. if (!PI_OFF(index + i)) {
  869. pidx++;
  870. pi = pi->next;
  871. #ifdef MALLOC_EXTRA_SANITY
  872. if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
  873. wrterror("(ES): hole in mapped pages directory");
  874. errno = EFAULT;
  875. return (NULL);
  876. }
  877. #endif /* MALLOC_EXTRA_SANITY */
  878. pd = pi->base;
  879. }
  880. pd[PI_OFF(index + i)] = MALLOC_FIRST;
  881. }
  882. malloc_used += size << malloc_pageshift;
  883. malloc_guarded += malloc_guard;
  884. if (malloc_junk)
  885. memset(p, SOME_JUNK, size << malloc_pageshift);
  886. }
  887. if (delay_free) {
  888. if (px == NULL)
  889. px = delay_free;
  890. else
  891. ifree(delay_free);
  892. }
  893. return (p);
  894. }
  895. /*
  896. * Allocate a page of fragments
  897. */
  898. static __inline__ int
  899. malloc_make_chunks(int bits)
  900. {
  901. struct pginfo *bp, **pd;
  902. struct pdinfo *pi;
  903. #ifdef MALLOC_EXTRA_SANITY
  904. u_long pidx;
  905. #endif /* MALLOC_EXTRA_SANITY */
  906. void *pp;
  907. long i, k;
  908. size_t l;
  909. /* Allocate a new bucket */
  910. pp = malloc_pages((size_t) malloc_pagesize);
  911. if (pp == NULL)
  912. return (0);
  913. /* Find length of admin structure */
  914. l = sizeof *bp - sizeof(u_long);
  915. l += sizeof(u_long) *
  916. (((malloc_pagesize >> bits) + MALLOC_BITS - 1) / MALLOC_BITS);
  917. /* Don't waste more than two chunks on this */
  918. /*
  919. * If we are to allocate a memory protected page for the malloc(0)
  920. * case (when bits=0), it must be from a different page than the
  921. * pginfo page.
  922. * --> Treat it like the big chunk alloc, get a second data page.
  923. */
  924. if (bits != 0 && (1UL << (bits)) <= l + l) {
  925. bp = (struct pginfo *) pp;
  926. } else {
  927. bp = (struct pginfo *) imalloc(l);
  928. if (bp == NULL) {
  929. ifree(pp);
  930. return (0);
  931. }
  932. }
  933. /* memory protect the page allocated in the malloc(0) case */
  934. if (bits == 0) {
  935. bp->size = 0;
  936. bp->shift = 1;
  937. i = malloc_minsize - 1;
  938. while (i >>= 1)
  939. bp->shift++;
  940. bp->total = bp->free = malloc_pagesize >> bp->shift;
  941. bp->page = pp;
  942. k = mprotect(pp, malloc_pagesize, PROT_NONE);
  943. if (k < 0) {
  944. ifree(pp);
  945. ifree(bp);
  946. return (0);
  947. }
  948. } else {
  949. bp->size = (1UL << bits);
  950. bp->shift = bits;
  951. bp->total = bp->free = malloc_pagesize >> bits;
  952. bp->page = pp;
  953. }
  954. /* set all valid bits in the bitmap */
  955. k = bp->total;
  956. i = 0;
  957. /* Do a bunch at a time */
  958. for (; (k - i) >= MALLOC_BITS; i += MALLOC_BITS)
  959. bp->bits[i / MALLOC_BITS] = ~0UL;
  960. for (; i < k; i++)
  961. bp->bits[i / MALLOC_BITS] |= 1UL << (i % MALLOC_BITS);
  962. k = (long)l;
  963. if (bp == bp->page) {
  964. /* Mark the ones we stole for ourselves */
  965. for (i = 0; k > 0; i++) {
  966. bp->bits[i / MALLOC_BITS] &= ~(1UL << (i % MALLOC_BITS));
  967. bp->free--;
  968. bp->total--;
  969. k -= (1 << bits);
  970. }
  971. }
  972. /* MALLOC_LOCK */
  973. pdir_lookup(ptr2index(pp), &pi);
  974. #ifdef MALLOC_EXTRA_SANITY
  975. pidx = PI_IDX(ptr2index(pp));
  976. if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
  977. wrterror("(ES): mapped pages not found in directory");
  978. errno = EFAULT;
  979. return (0);
  980. }
  981. #endif /* MALLOC_EXTRA_SANITY */
  982. if (pi != last_dir) {
  983. prev_dir = last_dir;
  984. last_dir = pi;
  985. }
  986. pd = pi->base;
  987. pd[PI_OFF(ptr2index(pp))] = bp;
  988. bp->next = page_dir[bits];
  989. page_dir[bits] = bp;
  990. /* MALLOC_UNLOCK */
  991. return (1);
  992. }
  993. /*
  994. * Allocate a fragment
  995. */
  996. static void *
  997. malloc_bytes(size_t size)
  998. {
  999. int i, j;
  1000. size_t k;
  1001. u_long u, *lp;
  1002. struct pginfo *bp;
  1003. /* Don't bother with anything less than this */
  1004. /* unless we have a malloc(0) requests */
  1005. if (size != 0 && size < malloc_minsize)
  1006. size = malloc_minsize;
  1007. /* Find the right bucket */
  1008. if (size == 0)
  1009. j = 0;
  1010. else {
  1011. j = 1;
  1012. i = size - 1;
  1013. while (i >>= 1)
  1014. j++;
  1015. }
  1016. /* If it's empty, make a page more of that size chunks */
  1017. if (page_dir[j] == NULL && !malloc_make_chunks(j))
  1018. return (NULL);
  1019. bp = page_dir[j];
  1020. /* Find first word of bitmap which isn't empty */
  1021. for (lp = bp->bits; !*lp; lp++);
  1022. /* Find that bit, and tweak it */
  1023. u = 1;
  1024. k = 0;
  1025. while (!(*lp & u)) {
  1026. u += u;
  1027. k++;
  1028. }
  1029. if (malloc_guard) {
  1030. /* Walk to a random position. */
  1031. // i = arc4random() % bp->free;
  1032. i = rand() % bp->free;
  1033. while (i > 0) {
  1034. u += u;
  1035. k++;
  1036. if (k >= MALLOC_BITS) {
  1037. lp++;
  1038. u = 1;
  1039. k = 0;
  1040. }
  1041. #ifdef MALLOC_EXTRA_SANITY
  1042. if (lp - bp->bits > (bp->total - 1) / MALLOC_BITS) {
  1043. wrterror("chunk overflow");
  1044. errno = EFAULT;
  1045. return (NULL);
  1046. }
  1047. #endif /* MALLOC_EXTRA_SANITY */
  1048. if (*lp & u)
  1049. i--;
  1050. }
  1051. }
  1052. *lp ^= u;
  1053. /* If there are no more free, remove from free-list */
  1054. if (!--bp->free) {
  1055. page_dir[j] = bp->next;
  1056. bp->next = NULL;
  1057. }
  1058. /* Adjust to the real offset of that chunk */
  1059. k += (lp - bp->bits) * MALLOC_BITS;
  1060. k <<= bp->shift;
  1061. if (malloc_junk && bp->size != 0)
  1062. memset((char *)bp->page + k, SOME_JUNK, (size_t)bp->size);
  1063. return ((u_char *) bp->page + k);
  1064. }
  1065. /*
  1066. * Magic so that malloc(sizeof(ptr)) is near the end of the page.
  1067. */
  1068. #define PTR_GAP (malloc_pagesize - sizeof(void *))
  1069. #define PTR_SIZE (sizeof(void *))
  1070. #define PTR_ALIGNED(p) (((unsigned long)p & malloc_pagemask) == PTR_GAP)
  1071. /*
  1072. * Allocate a piece of memory
  1073. */
  1074. static void *
  1075. imalloc(size_t size)
  1076. {
  1077. void *result;
  1078. int ptralloc = 0;
  1079. if (!malloc_started)
  1080. malloc_init();
  1081. if (suicide)
  1082. abort();
  1083. /* does not matter if malloc_bytes fails */
  1084. if (px == NULL)
  1085. px = malloc_bytes(sizeof *px);
  1086. if (malloc_ptrguard && size == PTR_SIZE) {
  1087. ptralloc = 1;
  1088. size = malloc_pagesize;
  1089. }
  1090. if ((size + malloc_pagesize) < size) { /* Check for overflow */
  1091. result = NULL;
  1092. errno = ENOMEM;
  1093. } else if (size <= malloc_maxsize)
  1094. result = malloc_bytes(size);
  1095. else
  1096. result = malloc_pages(size);
  1097. if (malloc_abort == 1 && result == NULL)
  1098. wrterror("allocation failed");
  1099. if (malloc_zero && result != NULL)
  1100. memset(result, 0, size);
  1101. if (result && ptralloc)
  1102. return ((char *) result + PTR_GAP);
  1103. return (result);
  1104. }
  1105. /*
  1106. * Change the size of an allocation.
  1107. */
  1108. static void *
  1109. irealloc(void *ptr, size_t size)
  1110. {
  1111. void *p;
  1112. size_t osize;
  1113. u_long index, i;
  1114. struct pginfo **mp;
  1115. struct pginfo **pd;
  1116. struct pdinfo *pi;
  1117. #ifdef MALLOC_EXTRA_SANITY
  1118. u_long pidx;
  1119. #endif /* MALLOC_EXTRA_SANITY */
  1120. if (suicide)
  1121. abort();
  1122. if (!malloc_started) {
  1123. wrtwarning("malloc() has never been called");
  1124. return (NULL);
  1125. }
  1126. if (malloc_ptrguard && PTR_ALIGNED(ptr)) {
  1127. if (size <= PTR_SIZE)
  1128. return (ptr);
  1129. p = imalloc(size);
  1130. if (p)
  1131. memcpy(p, ptr, PTR_SIZE);
  1132. ifree(ptr);
  1133. return (p);
  1134. }
  1135. index = ptr2index(ptr);
  1136. if (index < malloc_pageshift) {
  1137. wrtwarning("junk pointer, too low to make sense");
  1138. return (NULL);
  1139. }
  1140. if (index > last_index) {
  1141. wrtwarning("junk pointer, too high to make sense");
  1142. return (NULL);
  1143. }
  1144. pdir_lookup(index, &pi);
  1145. #ifdef MALLOC_EXTRA_SANITY
  1146. pidx = PI_IDX(index);
  1147. if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
  1148. wrterror("(ES): mapped pages not found in directory");
  1149. errno = EFAULT;
  1150. return (NULL);
  1151. }
  1152. #endif /* MALLOC_EXTRA_SANITY */
  1153. if (pi != last_dir) {
  1154. prev_dir = last_dir;
  1155. last_dir = pi;
  1156. }
  1157. pd = pi->base;
  1158. mp = &pd[PI_OFF(index)];
  1159. if (*mp == MALLOC_FIRST) { /* Page allocation */
  1160. /* Check the pointer */
  1161. if ((u_long) ptr & malloc_pagemask) {
  1162. wrtwarning("modified (page-) pointer");
  1163. return (NULL);
  1164. }
  1165. /* Find the size in bytes */
  1166. i = index;
  1167. if (!PI_OFF(++i)) {
  1168. pi = pi->next;
  1169. if (pi != NULL && PD_IDX(pi->dirnum) != PI_IDX(i))
  1170. pi = NULL;
  1171. if (pi != NULL)
  1172. pd = pi->base;
  1173. }
  1174. for (osize = malloc_pagesize;
  1175. pi != NULL && pd[PI_OFF(i)] == MALLOC_FOLLOW;) {
  1176. osize += malloc_pagesize;
  1177. if (!PI_OFF(++i)) {
  1178. pi = pi->next;
  1179. if (pi != NULL && PD_IDX(pi->dirnum) != PI_IDX(i))
  1180. pi = NULL;
  1181. if (pi != NULL)
  1182. pd = pi->base;
  1183. }
  1184. }
  1185. if (!malloc_realloc && size <= osize &&
  1186. size > osize - malloc_pagesize) {
  1187. if (malloc_junk)
  1188. memset((char *)ptr + size, SOME_JUNK, osize - size);
  1189. return (ptr); /* ..don't do anything else. */
  1190. }
  1191. } else if (*mp >= MALLOC_MAGIC) { /* Chunk allocation */
  1192. /* Check the pointer for sane values */
  1193. if ((u_long) ptr & ((1UL << ((*mp)->shift)) - 1)) {
  1194. wrtwarning("modified (chunk-) pointer");
  1195. return (NULL);
  1196. }
  1197. /* Find the chunk index in the page */
  1198. i = ((u_long) ptr & malloc_pagemask) >> (*mp)->shift;
  1199. /* Verify that it isn't a free chunk already */
  1200. if ((*mp)->bits[i / MALLOC_BITS] & (1UL << (i % MALLOC_BITS))) {
  1201. wrtwarning("chunk is already free");
  1202. return (NULL);
  1203. }
  1204. osize = (*mp)->size;
  1205. if (!malloc_realloc && size <= osize &&
  1206. (size > osize / 2 || osize == malloc_minsize)) {
  1207. if (malloc_junk)
  1208. memset((char *) ptr + size, SOME_JUNK, osize - size);
  1209. return (ptr); /* ..don't do anything else. */
  1210. }
  1211. } else {
  1212. wrtwarning("irealloc: pointer to wrong page");
  1213. return (NULL);
  1214. }
  1215. p = imalloc(size);
  1216. if (p != NULL) {
  1217. /* copy the lesser of the two sizes, and free the old one */
  1218. /* Don't move from/to 0 sized region !!! */
  1219. if (osize != 0 && size != 0) {
  1220. if (osize < size)
  1221. memcpy(p, ptr, osize);
  1222. else
  1223. memcpy(p, ptr, size);
  1224. }
  1225. ifree(ptr);
  1226. }
  1227. return (p);
  1228. }
  1229. /*
  1230. * Free a sequence of pages
  1231. */
  1232. static __inline__ void
  1233. free_pages(void *ptr, u_long index, struct pginfo * info)
  1234. {
  1235. u_long i, pidx, lidx;
  1236. size_t l, cachesize = 0;
  1237. struct pginfo **pd;
  1238. struct pdinfo *pi, *spi;
  1239. struct pgfree *pf, *pt = NULL;
  1240. caddr_t tail;
  1241. if (info == MALLOC_FREE) {
  1242. wrtwarning("page is already free");
  1243. return;
  1244. }
  1245. if (info != MALLOC_FIRST) {
  1246. wrtwarning("free_pages: pointer to wrong page");
  1247. return;
  1248. }
  1249. if ((u_long) ptr & malloc_pagemask) {
  1250. wrtwarning("modified (page-) pointer");
  1251. return;
  1252. }
  1253. /* Count how many pages and mark them free at the same time */
  1254. pidx = PI_IDX(index);
  1255. pdir_lookup(index, &pi);
  1256. #ifdef MALLOC_EXTRA_SANITY
  1257. if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
  1258. wrterror("(ES): mapped pages not found in directory");
  1259. errno = EFAULT;
  1260. return;
  1261. }
  1262. #endif /* MALLOC_EXTRA_SANITY */
  1263. spi = pi; /* Save page index for start of region. */
  1264. pd = pi->base;
  1265. pd[PI_OFF(index)] = MALLOC_FREE;
  1266. i = 1;
  1267. if (!PI_OFF(index + i)) {
  1268. pi = pi->next;
  1269. if (pi == NULL || PD_IDX(pi->dirnum) != PI_IDX(index + i))
  1270. pi = NULL;
  1271. else
  1272. pd = pi->base;
  1273. }
  1274. while (pi != NULL && pd[PI_OFF(index + i)] == MALLOC_FOLLOW) {
  1275. pd[PI_OFF(index + i)] = MALLOC_FREE;
  1276. i++;
  1277. if (!PI_OFF(index + i)) {
  1278. if ((pi = pi->next) == NULL ||
  1279. PD_IDX(pi->dirnum) != PI_IDX(index + i))
  1280. pi = NULL;
  1281. else
  1282. pd = pi->base;
  1283. }
  1284. }
  1285. l = i << malloc_pageshift;
  1286. if (malloc_junk)
  1287. memset(ptr, SOME_JUNK, l);
  1288. malloc_used -= l;
  1289. malloc_guarded -= malloc_guard;
  1290. if (malloc_guard) {
  1291. #ifdef MALLOC_EXTRA_SANITY
  1292. if (pi == NULL || PD_IDX(pi->dirnum) != PI_IDX(index + i)) {
  1293. wrterror("(ES): hole in mapped pages directory");
  1294. errno = EFAULT;
  1295. return;
  1296. }
  1297. #endif /* MALLOC_EXTRA_SANITY */
  1298. pd[PI_OFF(index + i)] = MALLOC_FREE;
  1299. l += malloc_guard;
  1300. }
  1301. tail = (caddr_t)ptr + l;
  1302. if (malloc_hint)
  1303. madvise(ptr, l, MADV_FREE);
  1304. if (malloc_freeprot)
  1305. mprotect(ptr, l, PROT_NONE);
  1306. /* Add to free-list. */
  1307. if (px == NULL && (px = malloc_bytes(sizeof *px)) == NULL)
  1308. goto not_return;
  1309. px->page = ptr;
  1310. px->pdir = spi;
  1311. px->size = l;
  1312. if (free_list.next == NULL) {
  1313. /* Nothing on free list, put this at head. */
  1314. px->next = NULL;
  1315. px->prev = &free_list;
  1316. free_list.next = px;
  1317. pf = px;
  1318. px = NULL;
  1319. } else {
  1320. /*
  1321. * Find the right spot, leave pf pointing to the modified
  1322. * entry.
  1323. */
  1324. /* Race ahead here, while calculating cache size. */
  1325. for (pf = free_list.next;
  1326. (caddr_t)ptr > ((caddr_t)pf->page + pf->size)
  1327. && pf->next != NULL;
  1328. pf = pf->next)
  1329. cachesize += pf->size;
  1330. /* Finish cache size calculation. */
  1331. pt = pf;
  1332. while (pt) {
  1333. cachesize += pt->size;
  1334. pt = pt->next;
  1335. }
  1336. if ((caddr_t)pf->page > tail) {
  1337. /* Insert before entry */
  1338. px->next = pf;
  1339. px->prev = pf->prev;
  1340. pf->prev = px;
  1341. px->prev->next = px;
  1342. pf = px;
  1343. px = NULL;
  1344. } else if (((caddr_t)pf->page + pf->size) == ptr) {
  1345. /* Append to the previous entry. */
  1346. cachesize -= pf->size;
  1347. pf->size += l;
  1348. if (pf->next != NULL &&
  1349. pf->next->page == ((caddr_t)pf->page + pf->size)) {
  1350. /* And collapse the next too. */
  1351. pt = pf->next;
  1352. pf->size += pt->size;
  1353. pf->next = pt->next;
  1354. if (pf->next != NULL)
  1355. pf->next->prev = pf;
  1356. }
  1357. } else if (pf->page == tail) {
  1358. /* Prepend to entry. */
  1359. cachesize -= pf->size;
  1360. pf->size += l;
  1361. pf->page = ptr;
  1362. pf->pdir = spi;
  1363. } else if (pf->next == NULL) {
  1364. /* Append at tail of chain. */
  1365. px->next = NULL;
  1366. px->prev = pf;
  1367. pf->next = px;
  1368. pf = px;
  1369. px = NULL;
  1370. } else {
  1371. wrterror("freelist is destroyed");
  1372. errno = EFAULT;
  1373. return;
  1374. }
  1375. }
  1376. if (pf->pdir != last_dir) {
  1377. prev_dir = last_dir;
  1378. last_dir = pf->pdir;
  1379. }
  1380. /* Return something to OS ? */
  1381. if (pf->size > (malloc_cache - cachesize)) {
  1382. /*
  1383. * Keep the cache intact. Notice that the '>' above guarantees that
  1384. * the pf will always have at least one page afterwards.
  1385. */
  1386. if (munmap((char *) pf->page + (malloc_cache - cachesize),
  1387. pf->size - (malloc_cache - cachesize)) != 0)
  1388. goto not_return;
  1389. tail = (caddr_t)pf->page + pf->size;
  1390. lidx = ptr2index(tail) - 1;
  1391. pf->size = malloc_cache - cachesize;
  1392. index = ptr2index((caddr_t)pf->page + pf->size);
  1393. pidx = PI_IDX(index);
  1394. if (prev_dir != NULL && PD_IDX(prev_dir->dirnum) >= pidx)
  1395. prev_dir = NULL; /* Will be wiped out below ! */
  1396. for (pi = pf->pdir; pi != NULL && PD_IDX(pi->dirnum) < pidx;
  1397. pi = pi->next)
  1398. ;
  1399. spi = pi;
  1400. if (pi != NULL && PD_IDX(pi->dirnum) == pidx) {
  1401. pd = pi->base;
  1402. for (i = index; i <= lidx;) {
  1403. if (pd[PI_OFF(i)] != MALLOC_NOT_MINE) {
  1404. pd[PI_OFF(i)] = MALLOC_NOT_MINE;
  1405. #ifdef MALLOC_EXTRA_SANITY
  1406. if (!PD_OFF(pi->dirnum)) {
  1407. wrterror("(ES): pages directory underflow");
  1408. errno = EFAULT;
  1409. return;
  1410. }
  1411. #endif /* MALLOC_EXTRA_SANITY */
  1412. pi->dirnum--;
  1413. }
  1414. #ifdef MALLOC_EXTRA_SANITY
  1415. else
  1416. wrtwarning("(ES): page already unmapped");
  1417. #endif /* MALLOC_EXTRA_SANITY */
  1418. i++;
  1419. if (!PI_OFF(i)) {
  1420. /*
  1421. * If no page in that dir, free
  1422. * directory page.
  1423. */
  1424. if (!PD_OFF(pi->dirnum)) {
  1425. /* Remove from list. */
  1426. if (spi == pi)
  1427. spi = pi->prev;
  1428. if (pi->prev != NULL)
  1429. pi->prev->next = pi->next;
  1430. if (pi->next != NULL)
  1431. pi->next->prev = pi->prev;
  1432. pi = pi->next;
  1433. munmap(pd, malloc_pagesize);
  1434. } else
  1435. pi = pi->next;
  1436. if (pi == NULL ||
  1437. PD_IDX(pi->dirnum) != PI_IDX(i))
  1438. break;
  1439. pd = pi->base;
  1440. }
  1441. }
  1442. if (pi && !PD_OFF(pi->dirnum)) {
  1443. /* Resulting page dir is now empty. */
  1444. /* Remove from list. */
  1445. if (spi == pi) /* Update spi only if first. */
  1446. spi = pi->prev;
  1447. if (pi->prev != NULL)
  1448. pi->prev->next = pi->next;
  1449. if (pi->next != NULL)
  1450. pi->next->prev = pi->prev;
  1451. pi = pi->next;
  1452. munmap(pd, malloc_pagesize);
  1453. }
  1454. }
  1455. if (pi == NULL && malloc_brk == tail) {
  1456. /* Resize down the malloc upper boundary. */
  1457. last_index = index - 1;
  1458. malloc_brk = index2ptr(index);
  1459. }
  1460. /* XXX: We could realloc/shrink the pagedir here I guess. */
  1461. if (pf->size == 0) { /* Remove from free-list as well. */
  1462. if (px)
  1463. ifree(px);
  1464. if ((px = pf->prev) != &free_list) {
  1465. if (pi == NULL && last_index == (index - 1)) {
  1466. if (spi == NULL) {
  1467. malloc_brk = NULL;
  1468. i = 11;
  1469. } else {
  1470. pd = spi->base;
  1471. if (PD_IDX(spi->dirnum) < pidx)
  1472. index =
  1473. ((PD_IDX(spi->dirnum) + 1) *
  1474. pdi_mod) - 1;
  1475. for (pi = spi, i = index;
  1476. pd[PI_OFF(i)] == MALLOC_NOT_MINE;
  1477. i--)
  1478. #ifdef MALLOC_EXTRA_SANITY
  1479. if (!PI_OFF(i)) {
  1480. pi = pi->prev;
  1481. if (pi == NULL || i == 0)
  1482. break;
  1483. pd = pi->base;
  1484. i = (PD_IDX(pi->dirnum) + 1) * pdi_mod;
  1485. }
  1486. #else /* !MALLOC_EXTRA_SANITY */
  1487. {
  1488. }
  1489. #endif /* MALLOC_EXTRA_SANITY */
  1490. malloc_brk = index2ptr(i + 1);
  1491. }
  1492. last_index = i;
  1493. }
  1494. if ((px->next = pf->next) != NULL)
  1495. px->next->prev = px;
  1496. } else {
  1497. if ((free_list.next = pf->next) != NULL)
  1498. free_list.next->prev = &free_list;
  1499. }
  1500. px = pf;
  1501. last_dir = prev_dir;
  1502. prev_dir = NULL;
  1503. }
  1504. }
  1505. not_return:
  1506. if (pt != NULL)
  1507. ifree(pt);
  1508. }
  1509. /*
  1510. * Free a chunk, and possibly the page it's on, if the page becomes empty.
  1511. */
  1512. /* ARGSUSED */
  1513. static __inline__ void
  1514. free_bytes(void *ptr, u_long index, struct pginfo * info)
  1515. {
  1516. struct pginfo **mp, **pd;
  1517. struct pdinfo *pi;
  1518. #ifdef MALLOC_EXTRA_SANITY
  1519. u_long pidx;
  1520. #endif /* MALLOC_EXTRA_SANITY */
  1521. void *vp;
  1522. long i;
  1523. (void) index;
  1524. /* Find the chunk number on the page */
  1525. i = ((u_long) ptr & malloc_pagemask) >> info->shift;
  1526. if ((u_long) ptr & ((1UL << (info->shift)) - 1)) {
  1527. wrtwarning("modified (chunk-) pointer");
  1528. return;
  1529. }
  1530. if (info->bits[i / MALLOC_BITS] & (1UL << (i % MALLOC_BITS))) {
  1531. wrtwarning("chunk is already free");
  1532. return;
  1533. }
  1534. if (malloc_junk && info->size != 0)
  1535. memset(ptr, SOME_JUNK, (size_t)info->size);
  1536. info->bits[i / MALLOC_BITS] |= 1UL << (i % MALLOC_BITS);
  1537. info->free++;
  1538. if (info->size != 0)
  1539. mp = page_dir + info->shift;
  1540. else
  1541. mp = page_dir;
  1542. if (info->free == 1) {
  1543. /* Page became non-full */
  1544. /* Insert in address order */
  1545. while (*mp != NULL && (*mp)->next != NULL &&
  1546. (*mp)->next->page < info->page)
  1547. mp = &(*mp)->next;
  1548. info->next = *mp;
  1549. *mp = info;
  1550. return;
  1551. }
  1552. if (info->free != info->total)
  1553. return;
  1554. /* Find & remove this page in the queue */
  1555. while (*mp != info) {
  1556. mp = &((*mp)->next);
  1557. #ifdef MALLOC_EXTRA_SANITY
  1558. if (!*mp) {
  1559. wrterror("(ES): Not on queue");
  1560. errno = EFAULT;
  1561. return;
  1562. }
  1563. #endif /* MALLOC_EXTRA_SANITY */
  1564. }
  1565. *mp = info->next;
  1566. /* Free the page & the info structure if need be */
  1567. pdir_lookup(ptr2index(info->page), &pi);
  1568. #ifdef MALLOC_EXTRA_SANITY
  1569. pidx = PI_IDX(ptr2index(info->page));
  1570. if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
  1571. wrterror("(ES): mapped pages not found in directory");
  1572. errno = EFAULT;
  1573. return;
  1574. }
  1575. #endif /* MALLOC_EXTRA_SANITY */
  1576. if (pi != last_dir) {
  1577. prev_dir = last_dir;
  1578. last_dir = pi;
  1579. }
  1580. pd = pi->base;
  1581. pd[PI_OFF(ptr2index(info->page))] = MALLOC_FIRST;
  1582. /* If the page was mprotected, unprotect it before releasing it */
  1583. if (info->size == 0)
  1584. mprotect(info->page, malloc_pagesize, PROT_READ | PROT_WRITE);
  1585. vp = info->page; /* Order is important ! */
  1586. if (vp != (void *) info)
  1587. ifree(info);
  1588. ifree(vp);
  1589. }
  1590. static void
  1591. ifree(void *ptr)
  1592. {
  1593. struct pginfo *info, **pd;
  1594. u_long index;
  1595. #ifdef MALLOC_EXTRA_SANITY
  1596. u_long pidx;
  1597. #endif /* MALLOC_EXTRA_SANITY */
  1598. struct pdinfo *pi;
  1599. if (!malloc_started) {
  1600. wrtwarning("malloc() has never been called");
  1601. return;
  1602. }
  1603. /* If we're already sinking, don't make matters any worse. */
  1604. if (suicide)
  1605. return;
  1606. if (malloc_ptrguard && PTR_ALIGNED(ptr))
  1607. ptr = (char *) ptr - PTR_GAP;
  1608. index = ptr2index(ptr);
  1609. if (index < malloc_pageshift) {
  1610. warnx("(%p)", ptr);
  1611. wrtwarning("ifree: junk pointer, too low to make sense");
  1612. return;
  1613. }
  1614. if (index > last_index) {
  1615. warnx("(%p)", ptr);
  1616. wrtwarning("ifree: junk pointer, too high to make sense");
  1617. return;
  1618. }
  1619. pdir_lookup(index, &pi);
  1620. #ifdef MALLOC_EXTRA_SANITY
  1621. pidx = PI_IDX(index);
  1622. if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {
  1623. wrterror("(ES): mapped pages not found in directory");
  1624. errno = EFAULT;
  1625. return;
  1626. }
  1627. #endif /* MALLOC_EXTRA_SANITY */
  1628. if (pi != last_dir) {
  1629. prev_dir = last_dir;
  1630. last_dir = pi;
  1631. }
  1632. pd = pi->base;
  1633. info = pd[PI_OFF(index)];
  1634. if (info < MALLOC_MAGIC)
  1635. free_pages(ptr, index, info);
  1636. else
  1637. free_bytes(ptr, index, info);
  1638. /* does not matter if malloc_bytes fails */
  1639. if (px == NULL)
  1640. px = malloc_bytes(sizeof *px);
  1641. return;
  1642. }
  1643. /*
  1644. * Common function for handling recursion. Only
  1645. * print the error message once, to avoid making the problem
  1646. * potentially worse.
  1647. */
  1648. static void
  1649. malloc_recurse(void)
  1650. {
  1651. static int noprint;
  1652. if (noprint == 0) {
  1653. noprint = 1;
  1654. wrtwarning("recursive call");
  1655. }
  1656. malloc_active--;
  1657. _MALLOC_UNLOCK();
  1658. errno = EDEADLK;
  1659. }
  1660. /*
  1661. * These are the public exported interface routines.
  1662. */
  1663. void *
  1664. malloc(size_t size)
  1665. {
  1666. void *r;
  1667. if (!align)
  1668. _MALLOC_LOCK();
  1669. malloc_func = " in malloc():";
  1670. if (malloc_active++) {
  1671. malloc_recurse();
  1672. return (NULL);
  1673. }
  1674. r = imalloc(size);
  1675. UTRACE(0, size, r);
  1676. malloc_active--;
  1677. if (!align)
  1678. _MALLOC_UNLOCK();
  1679. if (malloc_xmalloc && r == NULL) {
  1680. wrterror("out of memory");
  1681. errno = ENOMEM;
  1682. }
  1683. return (r);
  1684. }
  1685. void
  1686. free(void *ptr)
  1687. {
  1688. /* This is legal. XXX quick path */
  1689. if (ptr == NULL)
  1690. return;
  1691. _MALLOC_LOCK();
  1692. malloc_func = " in free():";
  1693. if (malloc_active++) {
  1694. malloc_recurse();
  1695. return;
  1696. }
  1697. ifree(ptr);
  1698. UTRACE(ptr, 0, 0);
  1699. malloc_active--;
  1700. _MALLOC_UNLOCK();
  1701. return;
  1702. }
  1703. void *
  1704. realloc(void *ptr, size_t size)
  1705. {
  1706. void *r;
  1707. _MALLOC_LOCK();
  1708. malloc_func = " in realloc():";
  1709. if (malloc_active++) {
  1710. malloc_recurse();
  1711. return (NULL);
  1712. }
  1713. if (ptr == NULL)
  1714. r = imalloc(size);
  1715. else
  1716. r = irealloc(ptr, size);
  1717. UTRACE(ptr, size, r);
  1718. malloc_active--;
  1719. _MALLOC_UNLOCK();
  1720. if (malloc_xmalloc && r == NULL) {
  1721. wrterror("out of memory");
  1722. errno = ENOMEM;
  1723. }
  1724. return (r);
  1725. }
  1726. #ifndef SIZE_MAX
  1727. //#if defined(__i386__)||defined(__arm__)||defined(__powerpc__)
  1728. //#define SIZE_MAX 0xffffffff
  1729. //#endif
  1730. //#if defined(__x86_64__)
  1731. //#define SIZE_MAX 0xffffffffffffffff
  1732. //#endif
  1733. #define SIZE_MAX SIZE_T_MAX
  1734. #endif
  1735. void *
  1736. calloc(size_t num, size_t size)
  1737. {
  1738. void *p;
  1739. if (num && SIZE_MAX / num < size) {
  1740. fprintf(stderr,"OOOOPS");
  1741. errno = ENOMEM;
  1742. return NULL;
  1743. }
  1744. size *= num;
  1745. p = malloc(size);
  1746. if (p)
  1747. memset(p, 0, size);
  1748. return(p);
  1749. }
  1750. static int ispowerof2 (size_t a) {
  1751. size_t b;
  1752. for (b = 1ULL << (sizeof(size_t)*NBBY - 1); b > 1; b >>= 1)
  1753. if (b == a)
  1754. return 1;
  1755. return 0;
  1756. }
  1757. int posix_memalign(void **memptr, size_t alignment, size_t size)
  1758. {
  1759. void *r;
  1760. if ((alignment < PTR_SIZE) || (alignment%PTR_SIZE != 0)) return EINVAL;
  1761. if (!ispowerof2(alignment)) return EINVAL;
  1762. if (alignment < malloc_minsize) alignment = malloc_minsize;
  1763. size_t max = alignment > size ? alignment : size;
  1764. if (alignment <= malloc_pagesize)
  1765. r = malloc(max);
  1766. else {
  1767. _MALLOC_LOCK();
  1768. align = 1;
  1769. g_alignment = alignment;
  1770. r = malloc(size);
  1771. align=0;
  1772. _MALLOC_UNLOCK();
  1773. }
  1774. *memptr = r;
  1775. if (!r) return ENOMEM;
  1776. return 0;
  1777. }
  1778. void *memalign(size_t boundary, size_t size)
  1779. {
  1780. void *r;
  1781. posix_memalign(&r, boundary, size);
  1782. return r;
  1783. }
  1784. void *valloc(size_t size)
  1785. {
  1786. void *r;
  1787. posix_memalign(&r, malloc_pagesize, size);
  1788. return r;
  1789. }
  1790. size_t malloc_good_size(size_t size)
  1791. {
  1792. if (size == 0) {
  1793. return 1;
  1794. } else if (size <= malloc_maxsize) {
  1795. int i, j;
  1796. /* round up to the nearest power of 2, with same approach
  1797. * as malloc_bytes() uses. */
  1798. j = 1;
  1799. i = size - 1;
  1800. while (i >>= 1)
  1801. j++;
  1802. return ((size_t)1) << j;
  1803. } else {
  1804. return pageround(size);
  1805. }
  1806. }