OpenBSD_malloc_Linux.c 45 KB

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