OpenBSD_malloc_Linux.c 45 KB

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