malloc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * User-level malloc and free implementation.
  31. *
  32. * This is a basic first-fit allocator. It's intended to be simple and
  33. * easy to follow. It performs abysmally if the heap becomes larger than
  34. * physical memory. To get (much) better out-of-core performance, port
  35. * the kernel's malloc. :-)
  36. */
  37. #include <stdlib.h>
  38. #include <unistd.h>
  39. #include <err.h>
  40. #include <stdint.h> // for uintptr_t on non-OS/161 platforms
  41. #undef MALLOCDEBUG
  42. #if defined(__mips__) || defined(__i386__)
  43. #define MALLOC32
  44. #elif defined(__alpha__)
  45. #define MALLOC64
  46. #else
  47. #error "please fix me"
  48. #endif
  49. /*
  50. * malloc block header.
  51. *
  52. * mh_prevblock is the downwards offset to the previous header, 0 if this
  53. * is the bottom of the heap.
  54. *
  55. * mh_nextblock is the upwards offset to the next header.
  56. *
  57. * mh_pad is unused.
  58. * mh_inuse is 1 if the block is in use, 0 if it is free.
  59. * mh_magic* should always be a fixed value.
  60. *
  61. * MBLOCKSIZE should equal sizeof(struct mheader) and be a power of 2.
  62. * MBLOCKSHIFT is the log base 2 of MBLOCKSIZE.
  63. * MMAGIC is the value for mh_magic*.
  64. */
  65. struct mheader {
  66. #if defined(MALLOC32)
  67. #define MBLOCKSIZE 8
  68. #define MBLOCKSHIFT 3
  69. #define MMAGIC 2
  70. /*
  71. * 32-bit platform. size_t is 32 bits (4 bytes).
  72. * Block size is 8 bytes.
  73. */
  74. unsigned mh_prevblock:29;
  75. unsigned mh_pad:1;
  76. unsigned mh_magic1:2;
  77. unsigned mh_nextblock:29;
  78. unsigned mh_inuse:1;
  79. unsigned mh_magic2:2;
  80. #elif defined(MALLOC64)
  81. #define MBLOCKSIZE 16
  82. #define MBLOCKSHIFT 4
  83. #define MMAGIC 6
  84. /*
  85. * 64-bit platform. size_t is 64 bits (8 bytes)
  86. * Block size is 16 bytes.
  87. */
  88. unsigned mh_prevblock:62;
  89. unsigned mh_pad:1;
  90. unsigned mh_magic1:3;
  91. unsigned mh_nextblock:62;
  92. unsigned mh_inuse:1;
  93. unsigned mh_magic2:3;
  94. #else
  95. #error "please fix me"
  96. #endif
  97. };
  98. /*
  99. * Operator macros on struct mheader.
  100. *
  101. * M_NEXT/PREVOFF: return offset to next/previous header
  102. * M_NEXT/PREV: return next/previous header
  103. *
  104. * M_DATA: return data pointer of a header
  105. * M_SIZE: return data size of a header
  106. *
  107. * M_OK: true if the magic values are correct
  108. *
  109. * M_MKFIELD: prepare a value for mh_next/prevblock.
  110. * (value should include the header size)
  111. */
  112. #define M_NEXTOFF(mh) ((size_t)(((size_t)((mh)->mh_nextblock))<<MBLOCKSHIFT))
  113. #define M_PREVOFF(mh) ((size_t)(((size_t)((mh)->mh_prevblock))<<MBLOCKSHIFT))
  114. #define M_NEXT(mh) ((struct mheader *)(((char*)(mh))+M_NEXTOFF(mh)))
  115. #define M_PREV(mh) ((struct mheader *)(((char*)(mh))-M_PREVOFF(mh)))
  116. #define M_DATA(mh) ((void *)((mh)+1))
  117. #define M_SIZE(mh) (M_NEXTOFF(mh)-MBLOCKSIZE)
  118. #define M_OK(mh) ((mh)->mh_magic1==MMAGIC && (mh)->mh_magic2==MMAGIC)
  119. #define M_MKFIELD(off) ((off)>>MBLOCKSHIFT)
  120. ////////////////////////////////////////////////////////////
  121. /*
  122. * Static variables - the bottom and top addresses of the heap.
  123. */
  124. static uintptr_t __heapbase, __heaptop;
  125. /*
  126. * Setup function.
  127. */
  128. static
  129. void
  130. __malloc_init(void)
  131. {
  132. void *x;
  133. /*
  134. * Check various assumed properties of the sizes.
  135. */
  136. if (sizeof(struct mheader) != MBLOCKSIZE) {
  137. errx(1, "malloc: Internal error - MBLOCKSIZE wrong");
  138. }
  139. if ((MBLOCKSIZE & (MBLOCKSIZE-1))!=0) {
  140. errx(1, "malloc: Internal error - MBLOCKSIZE not power of 2");
  141. }
  142. if (1<<MBLOCKSHIFT != MBLOCKSIZE) {
  143. errx(1, "malloc: Internal error - MBLOCKSHIFT wrong");
  144. }
  145. /* init should only be called once. */
  146. if (__heapbase!=0 || __heaptop!=0) {
  147. errx(1, "malloc: Internal error - bad init call");
  148. }
  149. /* Use sbrk to find the base of the heap. */
  150. x = sbrk(0);
  151. if (x==(void *)-1) {
  152. err(1, "malloc: initial sbrk failed");
  153. }
  154. if (x==(void *) 0) {
  155. errx(1, "malloc: Internal error - heap began at 0");
  156. }
  157. __heapbase = __heaptop = (uintptr_t)x;
  158. /*
  159. * Make sure the heap base is aligned the way we want it.
  160. * (On OS/161, it will begin on a page boundary. But on
  161. * an arbitrary Unix, it may not be, as traditionally it
  162. * begins at _end.)
  163. */
  164. if (__heapbase % MBLOCKSIZE != 0) {
  165. size_t adjust = MBLOCKSIZE - (__heapbase % MBLOCKSIZE);
  166. x = sbrk(adjust);
  167. if (x==(void *)-1) {
  168. err(1, "malloc: sbrk failed aligning heap base");
  169. }
  170. if ((uintptr_t)x != __heapbase) {
  171. err(1, "malloc: heap base moved during init");
  172. }
  173. #ifdef MALLOCDEBUG
  174. warnx("malloc: adjusted heap base upwards by %lu bytes",
  175. (unsigned long) adjust);
  176. #endif
  177. __heapbase += adjust;
  178. __heaptop = __heapbase;
  179. }
  180. }
  181. ////////////////////////////////////////////////////////////
  182. #ifdef MALLOCDEBUG
  183. /*
  184. * Debugging print function to iterate and dump the entire heap.
  185. */
  186. static
  187. void
  188. __malloc_dump(void)
  189. {
  190. struct mheader *mh;
  191. uintptr_t i;
  192. size_t rightprevblock;
  193. warnx("heap: ************************************************");
  194. rightprevblock = 0;
  195. for (i=__heapbase; i<__heaptop; i += M_NEXTOFF(mh)) {
  196. mh = (struct mheader *) i;
  197. if (!M_OK(mh)) {
  198. errx(1, "malloc: Heap corrupt; header at 0x%lx"
  199. " has bad magic bits",
  200. (unsigned long) i);
  201. }
  202. if (mh->mh_prevblock != rightprevblock) {
  203. errx(1, "malloc: Heap corrupt; header at 0x%lx"
  204. " has bad previous-block size %lu "
  205. "(should be %lu)",
  206. (unsigned long) i,
  207. (unsigned long) mh->mh_prevblock << MBLOCKSHIFT,
  208. (unsigned long) rightprevblock << MBLOCKSHIFT);
  209. }
  210. rightprevblock = mh->mh_nextblock;
  211. warnx("heap: 0x%lx 0x%-6lx (next: 0x%lx) %s",
  212. (unsigned long) i + MBLOCKSIZE,
  213. (unsigned long) M_SIZE(mh),
  214. (unsigned long) (i+M_NEXTOFF(mh)),
  215. mh->mh_inuse ? "INUSE" : "FREE");
  216. }
  217. if (i!=__heaptop) {
  218. errx(1, "malloc: Heap corrupt; ran off end");
  219. }
  220. warnx("heap: ************************************************");
  221. }
  222. #endif /* MALLOCDEBUG */
  223. ////////////////////////////////////////////////////////////
  224. /*
  225. * Get more memory (at the top of the heap) using sbrk, and
  226. * return a pointer to it.
  227. */
  228. static
  229. void *
  230. __malloc_sbrk(size_t size)
  231. {
  232. void *x;
  233. x = sbrk(size);
  234. if (x == (void *)-1) {
  235. return NULL;
  236. }
  237. if ((uintptr_t)x != __heaptop) {
  238. errx(1, "malloc: Internal error - "
  239. "heap top moved itself from 0x%lx to 0x%lx",
  240. (unsigned long) __heaptop,
  241. (unsigned long) (uintptr_t) x);
  242. }
  243. __heaptop += size;
  244. return x;
  245. }
  246. /*
  247. * Make a new (free) block from the block passed in, leaving size
  248. * bytes for data in the current block. size must be a multiple of
  249. * MBLOCKSIZE.
  250. *
  251. * Only split if the excess space is at least twice the blocksize -
  252. * one blocksize to hold a header and one for data.
  253. */
  254. static
  255. void
  256. __malloc_split(struct mheader *mh, size_t size)
  257. {
  258. struct mheader *mhnext, *mhnew;
  259. size_t oldsize;
  260. if (size % MBLOCKSIZE != 0) {
  261. errx(1, "malloc: Internal error (size %lu passed to split)",
  262. (unsigned long) size);
  263. }
  264. if (M_SIZE(mh) - size < 2*MBLOCKSIZE) {
  265. /* no room */
  266. return;
  267. }
  268. mhnext = M_NEXT(mh);
  269. oldsize = M_SIZE(mh);
  270. mh->mh_nextblock = M_MKFIELD(size + MBLOCKSIZE);
  271. mhnew = M_NEXT(mh);
  272. if (mhnew==mhnext) {
  273. errx(1, "malloc: Internal error (split screwed up?)");
  274. }
  275. mhnew->mh_prevblock = M_MKFIELD(size + MBLOCKSIZE);
  276. mhnew->mh_pad = 0;
  277. mhnew->mh_magic1 = MMAGIC;
  278. mhnew->mh_nextblock = M_MKFIELD(oldsize - size);
  279. mhnew->mh_inuse = 0;
  280. mhnew->mh_magic2 = MMAGIC;
  281. if (mhnext != (struct mheader *) __heaptop) {
  282. mhnext->mh_prevblock = mhnew->mh_nextblock;
  283. }
  284. }
  285. /*
  286. * malloc itself.
  287. */
  288. void *
  289. malloc(size_t size)
  290. {
  291. struct mheader *mh;
  292. uintptr_t i;
  293. size_t rightprevblock;
  294. if (__heapbase==0) {
  295. __malloc_init();
  296. }
  297. if (__heapbase==0 || __heaptop==0 || __heapbase > __heaptop) {
  298. warnx("malloc: Internal error - local data corrupt");
  299. errx(1, "malloc: heapbase 0x%lx; heaptop 0x%lx",
  300. (unsigned long) __heapbase, (unsigned long) __heaptop);
  301. }
  302. #ifdef MALLOCDEBUG
  303. warnx("malloc: about to allocate %lu (0x%lx) bytes",
  304. (unsigned long) size, (unsigned long) size);
  305. __malloc_dump();
  306. #endif
  307. /* Round size up to an integral number of blocks. */
  308. size = ((size + MBLOCKSIZE - 1) & ~(size_t)(MBLOCKSIZE-1));
  309. /*
  310. * First-fit search algorithm for available blocks.
  311. * Check to make sure the next/previous sizes all agree.
  312. */
  313. rightprevblock = 0;
  314. for (i=__heapbase; i<__heaptop; i += M_NEXTOFF(mh)) {
  315. mh = (struct mheader *) i;
  316. if (!M_OK(mh)) {
  317. errx(1, "malloc: Heap corrupt; header at 0x%lx"
  318. " has bad magic bits",
  319. (unsigned long) i);
  320. }
  321. if (mh->mh_prevblock != rightprevblock) {
  322. errx(1, "malloc: Heap corrupt; header at 0x%lx"
  323. " has bad previous-block size %lu "
  324. "(should be %lu)",
  325. (unsigned long) i,
  326. (unsigned long) mh->mh_prevblock << MBLOCKSHIFT,
  327. (unsigned long) rightprevblock << MBLOCKSHIFT);
  328. }
  329. rightprevblock = mh->mh_nextblock;
  330. /* Can't allocate a block that's in use. */
  331. if (mh->mh_inuse) {
  332. continue;
  333. }
  334. /* Can't allocate a block that isn't big enough. */
  335. if (M_SIZE(mh) < size) {
  336. continue;
  337. }
  338. /* Try splitting block. */
  339. __malloc_split(mh, size);
  340. /*
  341. * Now, allocate.
  342. */
  343. mh->mh_inuse = 1;
  344. #ifdef MALLOCDEBUG
  345. warnx("malloc: allocating at %p", M_DATA(mh));
  346. __malloc_dump();
  347. #endif
  348. return M_DATA(mh);
  349. }
  350. if (i!=__heaptop) {
  351. errx(1, "malloc: Heap corrupt; ran off end");
  352. }
  353. /*
  354. * Didn't find anything. Expand the heap.
  355. */
  356. mh = __malloc_sbrk(size + MBLOCKSIZE);
  357. if (mh == NULL) {
  358. return NULL;
  359. }
  360. mh->mh_prevblock = rightprevblock;
  361. mh->mh_magic1 = MMAGIC;
  362. mh->mh_magic2 = MMAGIC;
  363. mh->mh_pad = 0;
  364. mh->mh_inuse = 1;
  365. mh->mh_nextblock = M_MKFIELD(size + MBLOCKSIZE);
  366. #ifdef MALLOCDEBUG
  367. warnx("malloc: allocating at %p", M_DATA(mh));
  368. __malloc_dump();
  369. #endif
  370. return M_DATA(mh);
  371. }
  372. ////////////////////////////////////////////////////////////
  373. /*
  374. * Clear a range of memory with 0xdeadbeef.
  375. * ptr must be suitably aligned.
  376. */
  377. static
  378. void
  379. __malloc_deadbeef(void *ptr, size_t size)
  380. {
  381. uint32_t *x = ptr;
  382. size_t i, n = size/sizeof(uint32_t);
  383. for (i=0; i<n; i++) {
  384. x[i] = 0xdeadbeef;
  385. }
  386. }
  387. /*
  388. * Attempt to merge two adjacent blocks (mh below mhnext).
  389. */
  390. static
  391. void
  392. __malloc_trymerge(struct mheader *mh, struct mheader *mhnext)
  393. {
  394. struct mheader *mhnextnext;
  395. if (mh->mh_nextblock != mhnext->mh_prevblock) {
  396. errx(1, "free: Heap corrupt (%p and %p inconsistent)",
  397. mh, mhnext);
  398. }
  399. if (mh->mh_inuse || mhnext->mh_inuse) {
  400. /* can't merge */
  401. return;
  402. }
  403. mhnextnext = M_NEXT(mhnext);
  404. mh->mh_nextblock = M_MKFIELD(MBLOCKSIZE + M_SIZE(mh) +
  405. MBLOCKSIZE + M_SIZE(mhnext));
  406. if (mhnextnext != (struct mheader *)__heaptop) {
  407. mhnextnext->mh_prevblock = mh->mh_nextblock;
  408. }
  409. /* Deadbeef out the memory used by the now-obsolete header */
  410. __malloc_deadbeef(mhnext, sizeof(struct mheader));
  411. }
  412. /*
  413. * The actual free() implementation.
  414. */
  415. void
  416. free(void *x)
  417. {
  418. struct mheader *mh, *mhnext, *mhprev;
  419. if (x==NULL) {
  420. /* safest practice */
  421. return;
  422. }
  423. /* Consistency check. */
  424. if (__heapbase==0 || __heaptop==0 || __heapbase > __heaptop) {
  425. warnx("free: Internal error - local data corrupt");
  426. errx(1, "free: heapbase 0x%lx; heaptop 0x%lx",
  427. (unsigned long) __heapbase, (unsigned long) __heaptop);
  428. }
  429. /* Don't allow freeing pointers that aren't on the heap. */
  430. if ((uintptr_t)x < __heapbase || (uintptr_t)x >= __heaptop) {
  431. errx(1, "free: Invalid pointer %p freed (out of range)", x);
  432. }
  433. #ifdef MALLOCDEBUG
  434. warnx("free: about to free %p", x);
  435. __malloc_dump();
  436. #endif
  437. mh = ((struct mheader *)x)-1;
  438. if (!M_OK(mh)) {
  439. errx(1, "free: Invalid pointer %p freed (corrupt header)", x);
  440. }
  441. if (!mh->mh_inuse) {
  442. errx(1, "free: Invalid pointer %p freed (already free)", x);
  443. }
  444. /* mark it free */
  445. mh->mh_inuse = 0;
  446. /* wipe it */
  447. __malloc_deadbeef(M_DATA(mh), M_SIZE(mh));
  448. /* Try merging with the block above (but not if we're at the top) */
  449. mhnext = M_NEXT(mh);
  450. if (mhnext != (struct mheader *)__heaptop) {
  451. __malloc_trymerge(mh, mhnext);
  452. }
  453. /* Try merging with the block below (but not if we're at the bottom) */
  454. if (mh != (struct mheader *)__heapbase) {
  455. mhprev = M_PREV(mh);
  456. __malloc_trymerge(mhprev, mh);
  457. }
  458. #ifdef MALLOCDEBUG
  459. warnx("free: freed %p", x);
  460. __malloc_dump();
  461. #endif
  462. }