mm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /* mm.c - functions for memory manager */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2005,2007,2008,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. The design of this memory manager.
  21. This is a simple implementation of malloc with a few extensions. These are
  22. the extensions:
  23. - memalign is implemented efficiently.
  24. - multiple regions may be used as free space. They may not be
  25. contiguous.
  26. Regions are managed by a singly linked list, and the meta information is
  27. stored in the beginning of each region. Space after the meta information
  28. is used to allocate memory.
  29. The memory space is used as cells instead of bytes for simplicity. This
  30. is important for some CPUs which may not access multiple bytes at a time
  31. when the first byte is not aligned at a certain boundary (typically,
  32. 4-byte or 8-byte). The size of each cell is equal to the size of struct
  33. grub_mm_header, so the header of each allocated/free block fits into one
  34. cell precisely. One cell is 16 bytes on 32-bit platforms and 32 bytes
  35. on 64-bit platforms.
  36. There are two types of blocks: allocated blocks and free blocks.
  37. In allocated blocks, the header of each block has only its size. Note that
  38. this size is based on cells but not on bytes. The header is located right
  39. before the returned pointer, that is, the header resides at the previous
  40. cell.
  41. Free blocks constitutes a ring, using a singly linked list. The first free
  42. block is pointed to by the meta information of a region. The allocator
  43. attempts to pick up the second block instead of the first one. This is
  44. a typical optimization against defragmentation, and makes the
  45. implementation a bit easier.
  46. For safety, both allocated blocks and free ones are marked by magic
  47. numbers. Whenever anything unexpected is detected, GRUB aborts the
  48. operation.
  49. */
  50. #include <config.h>
  51. #include <grub/mm.h>
  52. #include <grub/misc.h>
  53. #include <grub/err.h>
  54. #include <grub/types.h>
  55. #include <grub/disk.h>
  56. #include <grub/dl.h>
  57. #ifdef MM_DEBUG
  58. # undef grub_malloc
  59. # undef grub_zalloc
  60. # undef grub_realloc
  61. # undef grub_free
  62. # undef grub_memalign
  63. GRUB_EXPORT(grub_mm_debug);
  64. GRUB_EXPORT(grub_debug_malloc);
  65. GRUB_EXPORT(grub_debug_zalloc);
  66. GRUB_EXPORT(grub_debug_free);
  67. GRUB_EXPORT(grub_debug_realloc);
  68. GRUB_EXPORT(grub_debug_memalign);
  69. GRUB_EXPORT(grub_mm_get_free);
  70. #endif
  71. GRUB_EXPORT(grub_malloc);
  72. GRUB_EXPORT(grub_zalloc);
  73. GRUB_EXPORT(grub_free);
  74. GRUB_EXPORT(grub_realloc);
  75. GRUB_EXPORT(grub_memalign);
  76. /* Magic words. */
  77. #define GRUB_MM_FREE_MAGIC 0x2d3c2808
  78. #define GRUB_MM_ALLOC_MAGIC 0x6db08fa4
  79. typedef struct grub_mm_header
  80. {
  81. struct grub_mm_header *next;
  82. grub_size_t size;
  83. grub_size_t magic;
  84. #if GRUB_CPU_SIZEOF_VOID_P == 4
  85. char padding[4];
  86. #elif GRUB_CPU_SIZEOF_VOID_P == 8
  87. char padding[8];
  88. #else
  89. # error "unknown word size"
  90. #endif
  91. }
  92. *grub_mm_header_t;
  93. #if GRUB_CPU_SIZEOF_VOID_P == 4
  94. # define GRUB_MM_ALIGN_LOG2 4
  95. #elif GRUB_CPU_SIZEOF_VOID_P == 8
  96. # define GRUB_MM_ALIGN_LOG2 5
  97. #endif
  98. #define GRUB_MM_ALIGN (1 << GRUB_MM_ALIGN_LOG2)
  99. typedef struct grub_mm_region
  100. {
  101. struct grub_mm_header *first;
  102. struct grub_mm_region *next;
  103. grub_addr_t addr;
  104. grub_size_t size;
  105. }
  106. *grub_mm_region_t;
  107. static grub_mm_region_t base;
  108. /* Get a header from the pointer PTR, and set *P and *R to a pointer
  109. to the header and a pointer to its region, respectively. PTR must
  110. be allocated. */
  111. static void
  112. get_header_from_pointer (void *ptr, grub_mm_header_t *p, grub_mm_region_t *r)
  113. {
  114. if ((grub_addr_t) ptr & (GRUB_MM_ALIGN - 1))
  115. grub_fatal ("unaligned pointer %p", ptr);
  116. for (*r = base; *r; *r = (*r)->next)
  117. if ((grub_addr_t) ptr > (*r)->addr
  118. && (grub_addr_t) ptr <= (*r)->addr + (*r)->size)
  119. break;
  120. if (! *r)
  121. grub_fatal ("out of range pointer %p", ptr);
  122. *p = (grub_mm_header_t) ptr - 1;
  123. if ((*p)->magic != GRUB_MM_ALLOC_MAGIC)
  124. grub_fatal ("alloc magic is broken at %p", *p);
  125. }
  126. /* Initialize a region starting from ADDR and whose size is SIZE,
  127. to use it as free space. */
  128. void
  129. grub_mm_init_region (void *addr, grub_size_t size)
  130. {
  131. grub_mm_header_t h;
  132. grub_mm_region_t r, *p, q;
  133. #if 0
  134. grub_printf ("Using memory for heap: start=%p, end=%p\n", addr, addr + (unsigned int) size);
  135. #endif
  136. /* Allocate a region from the head. */
  137. r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
  138. size -= (char *) r - (char *) addr + sizeof (*r);
  139. /* If this region is too small, ignore it. */
  140. if (size < GRUB_MM_ALIGN)
  141. return;
  142. h = (grub_mm_header_t) ((char *) r + GRUB_MM_ALIGN);
  143. h->next = h;
  144. h->magic = GRUB_MM_FREE_MAGIC;
  145. h->size = (size >> GRUB_MM_ALIGN_LOG2);
  146. r->first = h;
  147. r->addr = (grub_addr_t) h;
  148. r->size = (h->size << GRUB_MM_ALIGN_LOG2);
  149. /* Find where to insert this region. Put a smaller one before bigger ones,
  150. to prevent fragmentation. */
  151. for (p = &base, q = *p; q; p = &(q->next), q = *p)
  152. if (q->size > r->size)
  153. break;
  154. *p = r;
  155. r->next = q;
  156. }
  157. /* Allocate the number of units N with the alignment ALIGN from the ring
  158. buffer starting from *FIRST. ALIGN must be a power of two. Both N and
  159. ALIGN are in units of GRUB_MM_ALIGN. Return a non-NULL if successful,
  160. otherwise return NULL. */
  161. static void *
  162. grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align)
  163. {
  164. grub_mm_header_t p, q;
  165. /* When everything is allocated side effect is that *first will have alloc
  166. magic marked, meaning that there is no room in this region. */
  167. if ((*first)->magic == GRUB_MM_ALLOC_MAGIC)
  168. return 0;
  169. /* Try to search free slot for allocation in this memory region. */
  170. for (q = *first, p = q->next; ; q = p, p = p->next)
  171. {
  172. grub_off_t extra;
  173. extra = ((grub_addr_t) (p + 1) >> GRUB_MM_ALIGN_LOG2) % align;
  174. if (extra)
  175. extra = align - extra;
  176. if (! p)
  177. grub_fatal ("null in the ring");
  178. if (p->magic != GRUB_MM_FREE_MAGIC)
  179. grub_fatal ("free magic is broken at %p: 0x%x", p, p->magic);
  180. if (p->size >= n + extra)
  181. {
  182. if (extra == 0 && p->size == n)
  183. {
  184. /* There is no special alignment requirement and memory block
  185. is complete match.
  186. 1. Just mark memory block as allocated and remove it from
  187. free list.
  188. Result:
  189. +---------------+ previous block's next
  190. | alloc, size=n | |
  191. +---------------+ v
  192. */
  193. q->next = p->next;
  194. }
  195. else if (align == 1 || p->size == n + extra)
  196. {
  197. /* There might be alignment requirement, when taking it into
  198. account memory block fits in.
  199. 1. Allocate new area at end of memory block.
  200. 2. Reduce size of available blocks from original node.
  201. 3. Mark new area as allocated and "remove" it from free
  202. list.
  203. Result:
  204. +---------------+
  205. | free, size-=n | next --+
  206. +---------------+ |
  207. | alloc, size=n | |
  208. +---------------+ v
  209. */
  210. p->size -= n;
  211. p += p->size;
  212. }
  213. else if (extra == 0)
  214. {
  215. grub_mm_header_t r;
  216. r = p + extra + n;
  217. r->magic = GRUB_MM_FREE_MAGIC;
  218. r->size = p->size - extra - n;
  219. r->next = p->next;
  220. q->next = r;
  221. if (q == p)
  222. {
  223. q = r;
  224. r->next = r;
  225. }
  226. }
  227. else
  228. {
  229. /* There is alignment requirement and there is room in memory
  230. block. Split memory block to three pieces.
  231. 1. Create new memory block right after section being
  232. allocated. Mark it as free.
  233. 2. Add new memory block to free chain.
  234. 3. Mark current memory block having only extra blocks.
  235. 4. Advance to aligned block and mark that as allocated and
  236. "remove" it from free list.
  237. Result:
  238. +------------------------------+
  239. | free, size=extra | next --+
  240. +------------------------------+ |
  241. | alloc, size=n | |
  242. +------------------------------+ |
  243. | free, size=orig.size-extra-n | <------+, next --+
  244. +------------------------------+ v
  245. */
  246. grub_mm_header_t r;
  247. r = p + extra + n;
  248. r->magic = GRUB_MM_FREE_MAGIC;
  249. r->size = p->size - extra - n;
  250. r->next = p->next;
  251. p->size = extra;
  252. p->next = r;
  253. p += extra;
  254. }
  255. p->magic = GRUB_MM_ALLOC_MAGIC;
  256. p->size = n;
  257. /* Mark find as a start marker for next allocation to fasten it.
  258. This will have side effect of fragmenting memory as small
  259. pieces before this will be un-used. */
  260. *first = q;
  261. return p + 1;
  262. }
  263. /* Search was completed without result. */
  264. if (p == *first)
  265. break;
  266. }
  267. return 0;
  268. }
  269. /* Allocate SIZE bytes with the alignment ALIGN and return the pointer. */
  270. void *
  271. grub_memalign (grub_size_t align, grub_size_t size)
  272. {
  273. grub_mm_region_t r;
  274. grub_size_t n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1;
  275. int count = 0;
  276. align = (align >> GRUB_MM_ALIGN_LOG2);
  277. if (align == 0)
  278. align = 1;
  279. again:
  280. for (r = base; r; r = r->next)
  281. {
  282. void *p;
  283. p = grub_real_malloc (&(r->first), n, align);
  284. if (p)
  285. return p;
  286. }
  287. /* If failed, increase free memory somehow. */
  288. switch (count)
  289. {
  290. case 0:
  291. /* Invalidate disk caches. */
  292. grub_disk_cache_invalidate_all ();
  293. count++;
  294. goto again;
  295. case 1:
  296. /* Unload unneeded modules. */
  297. grub_dl_unload_unneeded ();
  298. count++;
  299. goto again;
  300. default:
  301. break;
  302. }
  303. grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
  304. return 0;
  305. }
  306. /* Allocate SIZE bytes and return the pointer. */
  307. void *
  308. grub_malloc (grub_size_t size)
  309. {
  310. return grub_memalign (0, size);
  311. }
  312. /* Allocate SIZE bytes, clear them and return the pointer. */
  313. void *
  314. grub_zalloc (grub_size_t size)
  315. {
  316. void *ret;
  317. ret = grub_memalign (0, size);
  318. if (ret)
  319. grub_memset (ret, 0, size);
  320. return ret;
  321. }
  322. /* Deallocate the pointer PTR. */
  323. void
  324. grub_free (void *ptr)
  325. {
  326. grub_mm_header_t p;
  327. grub_mm_region_t r;
  328. if (! ptr)
  329. return;
  330. get_header_from_pointer (ptr, &p, &r);
  331. if (r->first->magic == GRUB_MM_ALLOC_MAGIC)
  332. {
  333. p->magic = GRUB_MM_FREE_MAGIC;
  334. r->first = p->next = p;
  335. }
  336. else
  337. {
  338. grub_mm_header_t q;
  339. #if 0
  340. q = r->first;
  341. do
  342. {
  343. grub_printf ("%s:%d: q=%p, q->size=0x%x, q->magic=0x%x\n",
  344. GRUB_FILE, __LINE__, q, q->size, q->magic);
  345. q = q->next;
  346. }
  347. while (q != r->first);
  348. #endif
  349. for (q = r->first; q >= p || q->next <= p; q = q->next)
  350. {
  351. if (q->magic != GRUB_MM_FREE_MAGIC)
  352. grub_fatal ("free magic is broken at %p: 0x%x", q, q->magic);
  353. if (q >= q->next && (q < p || q->next > p))
  354. break;
  355. }
  356. p->magic = GRUB_MM_FREE_MAGIC;
  357. p->next = q->next;
  358. q->next = p;
  359. if (p + p->size == p->next)
  360. {
  361. if (p->next == q)
  362. q = p;
  363. p->next->magic = 0;
  364. p->size += p->next->size;
  365. p->next = p->next->next;
  366. }
  367. if (q + q->size == p)
  368. {
  369. p->magic = 0;
  370. q->size += p->size;
  371. q->next = p->next;
  372. }
  373. r->first = q;
  374. }
  375. }
  376. /* Reallocate SIZE bytes and return the pointer. The contents will be
  377. the same as that of PTR. */
  378. void *
  379. grub_realloc (void *ptr, grub_size_t size)
  380. {
  381. grub_mm_header_t p;
  382. grub_mm_region_t r;
  383. void *q;
  384. grub_size_t n;
  385. if (! ptr)
  386. return grub_malloc (size);
  387. if (! size)
  388. {
  389. grub_free (ptr);
  390. return 0;
  391. }
  392. /* FIXME: Not optimal. */
  393. n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1;
  394. get_header_from_pointer (ptr, &p, &r);
  395. if (p->size >= n)
  396. return ptr;
  397. q = grub_malloc (size);
  398. if (! q)
  399. return q;
  400. grub_memcpy (q, ptr, size);
  401. grub_free (ptr);
  402. return q;
  403. }
  404. #ifdef MM_DEBUG
  405. int grub_mm_debug = 0;
  406. void
  407. grub_mm_dump_free (void)
  408. {
  409. grub_mm_region_t r;
  410. for (r = base; r; r = r->next)
  411. {
  412. grub_mm_header_t p;
  413. /* Follow the free list. */
  414. p = r->first;
  415. do
  416. {
  417. if (p->magic != GRUB_MM_FREE_MAGIC)
  418. grub_fatal ("free magic is broken at %p: 0x%x", p, p->magic);
  419. grub_printf ("F:%p:%u:%p\n",
  420. p, (unsigned int) p->size << GRUB_MM_ALIGN_LOG2, p->next);
  421. p = p->next;
  422. }
  423. while (p != r->first);
  424. }
  425. grub_printf ("\n");
  426. }
  427. void
  428. grub_mm_dump (unsigned lineno)
  429. {
  430. grub_mm_region_t r;
  431. grub_printf ("called at line %u\n", lineno);
  432. for (r = base; r; r = r->next)
  433. {
  434. grub_mm_header_t p;
  435. for (p = (grub_mm_header_t) ((r->addr + GRUB_MM_ALIGN - 1)
  436. & (~(GRUB_MM_ALIGN - 1)));
  437. (grub_addr_t) p < r->addr + r->size;
  438. p++)
  439. {
  440. switch (p->magic)
  441. {
  442. case GRUB_MM_FREE_MAGIC:
  443. grub_printf ("F:%p:%u:%p\n",
  444. p, (unsigned int) p->size << GRUB_MM_ALIGN_LOG2, p->next);
  445. break;
  446. case GRUB_MM_ALLOC_MAGIC:
  447. grub_printf ("A:%p:%u\n", p, (unsigned int) p->size << GRUB_MM_ALIGN_LOG2);
  448. break;
  449. }
  450. }
  451. }
  452. grub_printf ("\n");
  453. }
  454. void *
  455. grub_debug_malloc (const char *file, int line, grub_size_t size)
  456. {
  457. void *ptr;
  458. if (grub_mm_debug)
  459. grub_printf ("%s:%d: malloc (0x%zx) = ", file, line, size);
  460. ptr = grub_malloc (size);
  461. if (grub_mm_debug)
  462. grub_printf ("%p\n", ptr);
  463. return ptr;
  464. }
  465. void *
  466. grub_debug_zalloc (const char *file, int line, grub_size_t size)
  467. {
  468. void *ptr;
  469. if (grub_mm_debug)
  470. grub_printf ("%s:%d: zalloc (0x%zx) = ", file, line, size);
  471. ptr = grub_zalloc (size);
  472. if (grub_mm_debug)
  473. grub_printf ("%p\n", ptr);
  474. return ptr;
  475. }
  476. void
  477. grub_debug_free (const char *file, int line, void *ptr)
  478. {
  479. if (grub_mm_debug)
  480. grub_printf ("%s:%d: free (%p)\n", file, line, ptr);
  481. grub_free (ptr);
  482. }
  483. void *
  484. grub_debug_realloc (const char *file, int line, void *ptr, grub_size_t size)
  485. {
  486. if (grub_mm_debug)
  487. grub_printf ("%s:%d: realloc (%p, 0x%zx) = ", file, line, ptr, size);
  488. ptr = grub_realloc (ptr, size);
  489. if (grub_mm_debug)
  490. grub_printf ("%p\n", ptr);
  491. return ptr;
  492. }
  493. void *
  494. grub_debug_memalign (const char *file, int line, grub_size_t align,
  495. grub_size_t size)
  496. {
  497. void *ptr;
  498. if (grub_mm_debug)
  499. grub_printf ("%s:%d: memalign (0x%zx, 0x%zx) = ",
  500. file, line, align, size);
  501. ptr = grub_memalign (align, size);
  502. if (grub_mm_debug)
  503. grub_printf ("%p\n", ptr);
  504. return ptr;
  505. }
  506. #endif /* MM_DEBUG */