Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tlsf.c 29KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. #include <assert.h>
  2. #include <limits.h>
  3. #include <stddef.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "tlsf.h"
  8. #include "tlsfbits.h"
  9. /*
  10. ** Constants.
  11. */
  12. /* Public constants: may be modified. */
  13. enum tlsf_public
  14. {
  15. /* log2 of number of linear subdivisions of block sizes. */
  16. SL_INDEX_COUNT_LOG2 = 5,
  17. };
  18. /* Private constants: do not modify. */
  19. enum tlsf_private
  20. {
  21. #if defined (TLSF_64BIT)
  22. /* All allocation sizes and addresses are aligned to 8 bytes. */
  23. ALIGN_SIZE_LOG2 = 3,
  24. #else
  25. /* All allocation sizes and addresses are aligned to 4 bytes. */
  26. ALIGN_SIZE_LOG2 = 2,
  27. #endif
  28. ALIGN_SIZE = (1 << ALIGN_SIZE_LOG2),
  29. /*
  30. ** We support allocations of sizes up to (1 << FL_INDEX_MAX) bits.
  31. ** However, because we linearly subdivide the second-level lists, and
  32. ** our minimum size granularity is 4 bytes, it doesn't make sense to
  33. ** create first-level lists for sizes smaller than SL_INDEX_COUNT * 4,
  34. ** or (1 << (SL_INDEX_COUNT_LOG2 + 2)) bytes, as there we will be
  35. ** trying to split size ranges into more slots than we have available.
  36. ** Instead, we calculate the minimum threshold size, and place all
  37. ** blocks below that size into the 0th first-level list.
  38. */
  39. #if defined (TLSF_64BIT)
  40. /*
  41. ** TODO: We can increase this to support larger sizes, at the expense
  42. ** of more overhead in the TLSF structure.
  43. */
  44. FL_INDEX_MAX = 32,
  45. #else
  46. FL_INDEX_MAX = 30,
  47. #endif
  48. SL_INDEX_COUNT = (1 << SL_INDEX_COUNT_LOG2),
  49. FL_INDEX_SHIFT = (SL_INDEX_COUNT_LOG2 + ALIGN_SIZE_LOG2),
  50. FL_INDEX_COUNT = (FL_INDEX_MAX - FL_INDEX_SHIFT + 1),
  51. SMALL_BLOCK_SIZE = (1 << FL_INDEX_SHIFT),
  52. };
  53. /*
  54. ** Cast and min/max macros.
  55. */
  56. #define tlsf_cast(t, exp) ((t) (exp))
  57. #define tlsf_min(a, b) ((a) < (b) ? (a) : (b))
  58. #define tlsf_max(a, b) ((a) > (b) ? (a) : (b))
  59. /*
  60. ** Set assert macro, if it has not been provided by the user.
  61. */
  62. #if !defined (tlsf_assert)
  63. #define tlsf_assert assert
  64. #endif
  65. /*
  66. ** Static assertion mechanism.
  67. */
  68. #define _tlsf_glue2(x, y) x ## y
  69. #define _tlsf_glue(x, y) _tlsf_glue2(x, y)
  70. #define tlsf_static_assert(exp) \
  71. typedef char _tlsf_glue(static_assert, __LINE__) [(exp) ? 1 : -1]
  72. /* This code has been tested on 32- and 64-bit (LP/LLP) architectures. */
  73. tlsf_static_assert(sizeof(int) * CHAR_BIT == 32);
  74. tlsf_static_assert(sizeof(size_t) * CHAR_BIT >= 32);
  75. tlsf_static_assert(sizeof(size_t) * CHAR_BIT <= 64);
  76. /* SL_INDEX_COUNT must be <= number of bits in sl_bitmap's storage type. */
  77. tlsf_static_assert(sizeof(unsigned int) * CHAR_BIT >= SL_INDEX_COUNT);
  78. /* Ensure we've properly tuned our sizes. */
  79. tlsf_static_assert(ALIGN_SIZE == SMALL_BLOCK_SIZE / SL_INDEX_COUNT);
  80. /*
  81. ** Data structures and associated constants.
  82. */
  83. /*
  84. ** Block header structure.
  85. **
  86. ** There are several implementation subtleties involved:
  87. ** - The prev_phys_block field is only valid if the previous block is free.
  88. ** - The prev_phys_block field is actually stored at the end of the
  89. ** previous block. It appears at the beginning of this structure only to
  90. ** simplify the implementation.
  91. ** - The next_free / prev_free fields are only valid if the block is free.
  92. */
  93. typedef struct block_header_t
  94. {
  95. /* Points to the previous physical block. */
  96. struct block_header_t* prev_phys_block;
  97. /* The size of this block, excluding the block header. */
  98. size_t size;
  99. /* Next and previous free blocks. */
  100. struct block_header_t* next_free;
  101. struct block_header_t* prev_free;
  102. } block_header_t;
  103. /*
  104. ** Since block sizes are always at least a multiple of 4, the two least
  105. ** significant bits of the size field are used to store the block status:
  106. ** - bit 0: whether block is busy or free
  107. ** - bit 1: whether previous block is busy or free
  108. */
  109. static const size_t block_header_free_bit = 1 << 0;
  110. static const size_t block_header_prev_free_bit = 1 << 1;
  111. /*
  112. ** The size of the block header exposed to used blocks is the size field.
  113. ** The prev_phys_block field is stored *inside* the previous free block.
  114. */
  115. static const size_t block_header_overhead = sizeof(size_t);
  116. /* User data starts directly after the size field in a used block. */
  117. static const size_t block_start_offset =
  118. offsetof(block_header_t, size) + sizeof(size_t);
  119. /*
  120. ** A free block must be large enough to store its header minus the size of
  121. ** the prev_phys_block field, and no larger than the number of addressable
  122. ** bits for FL_INDEX.
  123. */
  124. static const size_t block_size_min =
  125. sizeof(block_header_t) - sizeof(block_header_t*);
  126. static const size_t block_size_max = tlsf_cast(size_t, 1) << FL_INDEX_MAX;
  127. /* The TLSF control structure. */
  128. typedef struct control_t
  129. {
  130. /* Empty lists point at this block to indicate they are free. */
  131. block_header_t block_null;
  132. /* Bitmaps for free lists. */
  133. unsigned int fl_bitmap;
  134. unsigned int sl_bitmap[FL_INDEX_COUNT];
  135. /* Head of free lists. */
  136. block_header_t* blocks[FL_INDEX_COUNT][SL_INDEX_COUNT];
  137. } control_t;
  138. /* A type used for casting when doing pointer arithmetic. */
  139. typedef ptrdiff_t tlsfptr_t;
  140. /*
  141. ** block_header_t member functions.
  142. */
  143. static size_t block_size(const block_header_t* block)
  144. {
  145. return block->size & ~(block_header_free_bit | block_header_prev_free_bit);
  146. }
  147. static void block_set_size(block_header_t* block, size_t size)
  148. {
  149. const size_t oldsize = block->size;
  150. block->size = size | (oldsize & (block_header_free_bit | block_header_prev_free_bit));
  151. }
  152. static int block_is_last(const block_header_t* block)
  153. {
  154. return 0 == block_size(block);
  155. }
  156. static int block_is_free(const block_header_t* block)
  157. {
  158. return tlsf_cast(int, block->size & block_header_free_bit);
  159. }
  160. static void block_set_free(block_header_t* block)
  161. {
  162. block->size |= block_header_free_bit;
  163. }
  164. static void block_set_used(block_header_t* block)
  165. {
  166. block->size &= ~block_header_free_bit;
  167. }
  168. static int block_is_prev_free(const block_header_t* block)
  169. {
  170. return tlsf_cast(int, block->size & block_header_prev_free_bit);
  171. }
  172. static void block_set_prev_free(block_header_t* block)
  173. {
  174. block->size |= block_header_prev_free_bit;
  175. }
  176. static void block_set_prev_used(block_header_t* block)
  177. {
  178. block->size &= ~block_header_prev_free_bit;
  179. }
  180. static block_header_t* block_from_ptr(const void* ptr)
  181. {
  182. return tlsf_cast(block_header_t*,
  183. tlsf_cast(unsigned char*, ptr) - block_start_offset);
  184. }
  185. static void* block_to_ptr(const block_header_t* block)
  186. {
  187. return tlsf_cast(void*,
  188. tlsf_cast(unsigned char*, block) + block_start_offset);
  189. }
  190. /* Return location of next block after block of given size. */
  191. static block_header_t* offset_to_block(const void* ptr, size_t size)
  192. {
  193. return tlsf_cast(block_header_t*, tlsf_cast(tlsfptr_t, ptr) + size);
  194. }
  195. /* Return location of previous block. */
  196. static block_header_t* block_prev(const block_header_t* block)
  197. {
  198. return block->prev_phys_block;
  199. }
  200. /* Return location of next existing block. */
  201. static block_header_t* block_next(const block_header_t* block)
  202. {
  203. block_header_t* next = offset_to_block(block_to_ptr(block),
  204. block_size(block) - block_header_overhead);
  205. tlsf_assert(!block_is_last(block));
  206. return next;
  207. }
  208. /* Link a new block with its physical neighbor, return the neighbor. */
  209. static block_header_t* block_link_next(block_header_t* block)
  210. {
  211. block_header_t* next = block_next(block);
  212. next->prev_phys_block = block;
  213. return next;
  214. }
  215. static void block_mark_as_free(block_header_t* block)
  216. {
  217. /* Link the block to the next block, first. */
  218. block_header_t* next = block_link_next(block);
  219. block_set_prev_free(next);
  220. block_set_free(block);
  221. }
  222. static void block_mark_as_used(block_header_t* block)
  223. {
  224. block_header_t* next = block_next(block);
  225. block_set_prev_used(next);
  226. block_set_used(block);
  227. }
  228. static size_t align_up(size_t x, size_t align)
  229. {
  230. tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two");
  231. return (x + (align - 1)) & ~(align - 1);
  232. }
  233. static size_t align_down(size_t x, size_t align)
  234. {
  235. tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two");
  236. return x - (x & (align - 1));
  237. }
  238. static void* align_ptr(const void* ptr, size_t align)
  239. {
  240. const tlsfptr_t aligned =
  241. (tlsf_cast(tlsfptr_t, ptr) + (align - 1)) & ~(align - 1);
  242. tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two");
  243. return tlsf_cast(void*, aligned);
  244. }
  245. /*
  246. ** Adjust an allocation size to be aligned to word size, and no smaller
  247. ** than internal minimum.
  248. */
  249. static size_t adjust_request_size(size_t size, size_t align)
  250. {
  251. size_t adjust = 0;
  252. if (size && size < block_size_max)
  253. {
  254. const size_t aligned = align_up(size, align);
  255. adjust = tlsf_max(aligned, block_size_min);
  256. }
  257. return adjust;
  258. }
  259. /*
  260. ** TLSF utility functions. In most cases, these are direct translations of
  261. ** the documentation found in the white paper.
  262. */
  263. static void mapping_insert(size_t size, int* fli, int* sli)
  264. {
  265. int fl, sl;
  266. if (size < SMALL_BLOCK_SIZE)
  267. {
  268. /* Store small blocks in first list. */
  269. fl = 0;
  270. sl = tlsf_cast(int, size) / (SMALL_BLOCK_SIZE / SL_INDEX_COUNT);
  271. }
  272. else
  273. {
  274. fl = tlsf_fls_sizet(size);
  275. sl = tlsf_cast(int, size >> (fl - SL_INDEX_COUNT_LOG2)) ^ (1 << SL_INDEX_COUNT_LOG2);
  276. fl -= (FL_INDEX_SHIFT - 1);
  277. }
  278. *fli = fl;
  279. *sli = sl;
  280. }
  281. /* This version rounds up to the next block size (for allocations) */
  282. static void mapping_search(size_t size, int* fli, int* sli)
  283. {
  284. if (size >= (1 << SL_INDEX_COUNT_LOG2))
  285. {
  286. const size_t round = (1 << (tlsf_fls_sizet(size) - SL_INDEX_COUNT_LOG2)) - 1;
  287. size += round;
  288. }
  289. mapping_insert(size, fli, sli);
  290. }
  291. static block_header_t* search_suitable_block(control_t* control, int* fli, int* sli)
  292. {
  293. int fl = *fli;
  294. int sl = *sli;
  295. /*
  296. ** First, search for a block in the list associated with the given
  297. ** fl/sl index.
  298. */
  299. unsigned int sl_map = control->sl_bitmap[fl] & (~0 << sl);
  300. if (!sl_map)
  301. {
  302. /* No block exists. Search in the next largest first-level list. */
  303. const unsigned int fl_map = control->fl_bitmap & (~0 << (fl + 1));
  304. if (!fl_map)
  305. {
  306. /* No free blocks available, memory has been exhausted. */
  307. return 0;
  308. }
  309. fl = tlsf_ffs(fl_map);
  310. *fli = fl;
  311. sl_map = control->sl_bitmap[fl];
  312. }
  313. tlsf_assert(sl_map && "internal error - second level bitmap is null");
  314. sl = tlsf_ffs(sl_map);
  315. *sli = sl;
  316. /* Return the first block in the free list. */
  317. return control->blocks[fl][sl];
  318. }
  319. /* Remove a free block from the free list.*/
  320. static void remove_free_block(control_t* control, block_header_t* block, int fl, int sl)
  321. {
  322. block_header_t* prev = block->prev_free;
  323. block_header_t* next = block->next_free;
  324. tlsf_assert(prev && "prev_free field can not be null");
  325. tlsf_assert(next && "next_free field can not be null");
  326. next->prev_free = prev;
  327. prev->next_free = next;
  328. /* If this block is the head of the free list, set new head. */
  329. if (control->blocks[fl][sl] == block)
  330. {
  331. control->blocks[fl][sl] = next;
  332. /* If the new head is null, clear the bitmap. */
  333. if (next == &control->block_null)
  334. {
  335. control->sl_bitmap[fl] &= ~(1 << sl);
  336. /* If the second bitmap is now empty, clear the fl bitmap. */
  337. if (!control->sl_bitmap[fl])
  338. {
  339. control->fl_bitmap &= ~(1 << fl);
  340. }
  341. }
  342. }
  343. }
  344. /* Insert a free block into the free block list. */
  345. static void insert_free_block(control_t* control, block_header_t* block, int fl, int sl)
  346. {
  347. block_header_t* current = control->blocks[fl][sl];
  348. tlsf_assert(current && "free list cannot have a null entry");
  349. tlsf_assert(block && "cannot insert a null entry into the free list");
  350. block->next_free = current;
  351. block->prev_free = &control->block_null;
  352. current->prev_free = block;
  353. tlsf_assert(block_to_ptr(block) == align_ptr(block_to_ptr(block), ALIGN_SIZE)
  354. && "block not aligned properly");
  355. /*
  356. ** Insert the new block at the head of the list, and mark the first-
  357. ** and second-level bitmaps appropriately.
  358. */
  359. control->blocks[fl][sl] = block;
  360. control->fl_bitmap |= (1 << fl);
  361. control->sl_bitmap[fl] |= (1 << sl);
  362. }
  363. /* Remove a given block from the free list. */
  364. static void block_remove(control_t* control, block_header_t* block)
  365. {
  366. int fl, sl;
  367. mapping_insert(block_size(block), &fl, &sl);
  368. remove_free_block(control, block, fl, sl);
  369. }
  370. /* Insert a given block into the free list. */
  371. static void block_insert(control_t* control, block_header_t* block)
  372. {
  373. int fl, sl;
  374. mapping_insert(block_size(block), &fl, &sl);
  375. insert_free_block(control, block, fl, sl);
  376. }
  377. static int block_can_split(block_header_t* block, size_t size)
  378. {
  379. return block_size(block) >= sizeof(block_header_t) + size;
  380. }
  381. /* Split a block into two, the second of which is free. */
  382. static block_header_t* block_split(block_header_t* block, size_t size)
  383. {
  384. /* Calculate the amount of space left in the remaining block. */
  385. block_header_t* remaining =
  386. offset_to_block(block_to_ptr(block), size - block_header_overhead);
  387. const size_t remain_size = block_size(block) - (size + block_header_overhead);
  388. tlsf_assert(block_to_ptr(remaining) == align_ptr(block_to_ptr(remaining), ALIGN_SIZE)
  389. && "remaining block not aligned properly");
  390. tlsf_assert(block_size(block) == remain_size + size + block_header_overhead);
  391. block_set_size(remaining, remain_size);
  392. tlsf_assert(block_size(remaining) >= block_size_min && "block split with invalid size");
  393. block_set_size(block, size);
  394. block_mark_as_free(remaining);
  395. return remaining;
  396. }
  397. /* Absorb a free block's storage into an adjacent previous free block. */
  398. static block_header_t* block_absorb(block_header_t* prev, block_header_t* block)
  399. {
  400. tlsf_assert(!block_is_last(prev) && "previous block can't be last!");
  401. /* Note: Leaves flags untouched. */
  402. prev->size += block_size(block) + block_header_overhead;
  403. block_link_next(prev);
  404. return prev;
  405. }
  406. /* Merge a just-freed block with an adjacent previous free block. */
  407. static block_header_t* block_merge_prev(control_t* control, block_header_t* block)
  408. {
  409. if (block_is_prev_free(block))
  410. {
  411. block_header_t* prev = block_prev(block);
  412. tlsf_assert(prev && "prev physical block can't be null");
  413. tlsf_assert(block_is_free(prev) && "prev block is not free though marked as such");
  414. block_remove(control, prev);
  415. block = block_absorb(prev, block);
  416. }
  417. return block;
  418. }
  419. /* Merge a just-freed block with an adjacent free block. */
  420. static block_header_t* block_merge_next(control_t* control, block_header_t* block)
  421. {
  422. block_header_t* next = block_next(block);
  423. tlsf_assert(next && "next physical block can't be null");
  424. if (block_is_free(next))
  425. {
  426. tlsf_assert(!block_is_last(block) && "previous block can't be last!");
  427. block_remove(control, next);
  428. block = block_absorb(block, next);
  429. }
  430. return block;
  431. }
  432. /* Trim any trailing block space off the end of a block, return to pool. */
  433. static void block_trim_free(control_t* control, block_header_t* block, size_t size)
  434. {
  435. tlsf_assert(block_is_free(block) && "block must be free");
  436. if (block_can_split(block, size))
  437. {
  438. block_header_t* remaining_block = block_split(block, size);
  439. block_link_next(block);
  440. block_set_prev_free(remaining_block);
  441. block_insert(control, remaining_block);
  442. }
  443. }
  444. /* Trim any trailing block space off the end of a used block, return to pool. */
  445. static void block_trim_used(control_t* control, block_header_t* block, size_t size)
  446. {
  447. tlsf_assert(!block_is_free(block) && "block must be used");
  448. if (block_can_split(block, size))
  449. {
  450. /* If the next block is free, we must coalesce. */
  451. block_header_t* remaining_block = block_split(block, size);
  452. block_set_prev_used(remaining_block);
  453. remaining_block = block_merge_next(control, remaining_block);
  454. block_insert(control, remaining_block);
  455. }
  456. }
  457. static block_header_t* block_trim_free_leading(control_t* control, block_header_t* block, size_t size)
  458. {
  459. block_header_t* remaining_block = block;
  460. if (block_can_split(block, size))
  461. {
  462. /* We want the 2nd block. */
  463. remaining_block = block_split(block, size - block_header_overhead);
  464. block_set_prev_free(remaining_block);
  465. block_link_next(block);
  466. block_insert(control, block);
  467. }
  468. return remaining_block;
  469. }
  470. static block_header_t* block_locate_free(control_t* control, size_t size)
  471. {
  472. int fl = 0, sl = 0;
  473. block_header_t* block = 0;
  474. if (size)
  475. {
  476. mapping_search(size, &fl, &sl);
  477. block = search_suitable_block(control, &fl, &sl);
  478. if(block && !block->size)
  479. block = NULL;
  480. }
  481. if (block)
  482. {
  483. tlsf_assert(block_size(block) >= size);
  484. remove_free_block(control, block, fl, sl);
  485. }
  486. return block;
  487. }
  488. static void* block_prepare_used(control_t* control, block_header_t* block, size_t size)
  489. {
  490. void* p = 0;
  491. if (block)
  492. {
  493. block_trim_free(control, block, size);
  494. block_mark_as_used(block);
  495. p = block_to_ptr(block);
  496. }
  497. return p;
  498. }
  499. /* Clear structure and point all empty lists at the null block. */
  500. static void control_construct(control_t* control)
  501. {
  502. int i, j;
  503. control->block_null.next_free = &control->block_null;
  504. control->block_null.prev_free = &control->block_null;
  505. control->fl_bitmap = 0;
  506. for (i = 0; i < FL_INDEX_COUNT; ++i)
  507. {
  508. control->sl_bitmap[i] = 0;
  509. for (j = 0; j < SL_INDEX_COUNT; ++j)
  510. {
  511. control->blocks[i][j] = &control->block_null;
  512. }
  513. }
  514. }
  515. /*
  516. ** Debugging utilities.
  517. */
  518. typedef struct integrity_t
  519. {
  520. int prev_status;
  521. int status;
  522. } integrity_t;
  523. #define tlsf_insist(x) { tlsf_assert(x); if (!(x)) { status--; } }
  524. static void integrity_walker(void* ptr, size_t size, int used, void* user)
  525. {
  526. (void) used;
  527. block_header_t* block = block_from_ptr(ptr);
  528. integrity_t* integ = tlsf_cast(integrity_t*, user);
  529. const int this_prev_status = block_is_prev_free(block) ? 1 : 0;
  530. const int this_status = block_is_free(block) ? 1 : 0;
  531. const size_t this_block_size = block_size(block);
  532. int status = 0;
  533. tlsf_insist(integ->prev_status == this_prev_status && "prev status incorrect");
  534. tlsf_insist(size == this_block_size && "block size incorrect");
  535. integ->prev_status = this_status;
  536. integ->status += status;
  537. }
  538. int tlsf_check(tlsf_t tlsf)
  539. {
  540. int i, j;
  541. control_t* control = tlsf_cast(control_t*, tlsf);
  542. int status = 0;
  543. /* Check that the free lists and bitmaps are accurate. */
  544. for (i = 0; i < FL_INDEX_COUNT; ++i)
  545. {
  546. for (j = 0; j < SL_INDEX_COUNT; ++j)
  547. {
  548. const int fl_map = control->fl_bitmap & (1 << i);
  549. const int sl_list = control->sl_bitmap[i];
  550. const int sl_map = sl_list & (1 << j);
  551. const block_header_t* block = control->blocks[i][j];
  552. /* Check that first- and second-level lists agree. */
  553. if (!fl_map)
  554. {
  555. tlsf_insist(!sl_map && "second-level map must be null");
  556. }
  557. if (!sl_map)
  558. {
  559. tlsf_insist(block == &control->block_null && "block list must be null");
  560. continue;
  561. }
  562. /* Check that there is at least one free block. */
  563. tlsf_insist(sl_list && "no free blocks in second-level map");
  564. tlsf_insist(block != &control->block_null && "block should not be null");
  565. while (block != &control->block_null)
  566. {
  567. int fli, sli;
  568. tlsf_insist(block_is_free(block) && "block should be free");
  569. tlsf_insist(!block_is_prev_free(block) && "blocks should have coalesced");
  570. tlsf_insist(!block_is_free(block_next(block)) && "blocks should have coalesced");
  571. tlsf_insist(block_is_prev_free(block_next(block)) && "block should be free");
  572. tlsf_insist(block_size(block) >= block_size_min && "block not minimum size");
  573. mapping_insert(block_size(block), &fli, &sli);
  574. tlsf_insist(fli == i && sli == j && "block size indexed in wrong list");
  575. block = block->next_free;
  576. }
  577. }
  578. }
  579. return status;
  580. }
  581. #undef tlsf_insist
  582. static void default_walker(void* ptr, size_t size, int used, void* user)
  583. {
  584. (void)user;
  585. printf("\t%p %s size: %x (%p)\n", ptr, used ? "used" : "free", (unsigned int)size, block_from_ptr(ptr));
  586. }
  587. void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user)
  588. {
  589. tlsf_walker pool_walker = walker ? walker : default_walker;
  590. block_header_t* block =
  591. offset_to_block(pool, -(int)block_header_overhead);
  592. while (block && !block_is_last(block))
  593. {
  594. pool_walker(
  595. block_to_ptr(block),
  596. block_size(block),
  597. !block_is_free(block),
  598. user);
  599. block = block_next(block);
  600. }
  601. }
  602. size_t tlsf_block_size(void* ptr)
  603. {
  604. size_t size = 0;
  605. if (ptr)
  606. {
  607. const block_header_t* block = block_from_ptr(ptr);
  608. size = block_size(block);
  609. }
  610. return size;
  611. }
  612. int tlsf_check_pool(pool_t pool)
  613. {
  614. /* Check that the blocks are physically correct. */
  615. integrity_t integ = { 0, 0 };
  616. tlsf_walk_pool(pool, integrity_walker, &integ);
  617. return integ.status;
  618. }
  619. /*
  620. ** Size of the TLSF structures in a given memory block passed to
  621. ** tlsf_create, equal to the size of a control_t
  622. */
  623. size_t tlsf_size()
  624. {
  625. return sizeof(control_t);
  626. }
  627. size_t tlsf_align_size()
  628. {
  629. return ALIGN_SIZE;
  630. }
  631. size_t tlsf_block_size_min()
  632. {
  633. return block_size_min;
  634. }
  635. size_t tlsf_block_size_max()
  636. {
  637. return block_size_max;
  638. }
  639. /*
  640. ** Overhead of the TLSF structures in a given memory block passes to
  641. ** tlsf_add_pool, equal to the overhead of a free block and the
  642. ** sentinel block.
  643. */
  644. size_t tlsf_pool_overhead()
  645. {
  646. return 2 * block_header_overhead;
  647. }
  648. size_t tlsf_alloc_overhead()
  649. {
  650. return block_header_overhead;
  651. }
  652. pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
  653. {
  654. block_header_t* block;
  655. block_header_t* next;
  656. const size_t pool_overhead = tlsf_pool_overhead();
  657. const size_t pool_bytes = align_down(bytes - pool_overhead, ALIGN_SIZE);
  658. if (((ptrdiff_t)mem % ALIGN_SIZE) != 0)
  659. {
  660. printf("tlsf_add_pool: Memory must be aligned by %u bytes.\n",
  661. (unsigned int)ALIGN_SIZE);
  662. return 0;
  663. }
  664. if (pool_bytes < block_size_min || pool_bytes > block_size_max)
  665. {
  666. #if defined (TLSF_64BIT)
  667. printf("tlsf_add_pool: Memory size must be between 0x%x and 0x%x00 bytes.\n",
  668. (unsigned int)(pool_overhead + block_size_min),
  669. (unsigned int)((pool_overhead + block_size_max) / 256));
  670. #else
  671. printf("tlsf_add_pool: Memory size must be between %u and %u bytes.\n",
  672. (unsigned int)(pool_overhead + block_size_min),
  673. (unsigned int)(pool_overhead + block_size_max));
  674. #endif
  675. return 0;
  676. }
  677. /*
  678. ** Create the main free block. Offset the start of the block slightly
  679. ** so that the prev_phys_block field falls outside of the pool -
  680. ** it will never be used.
  681. */
  682. block = offset_to_block(mem, -(tlsfptr_t)block_header_overhead);
  683. block_set_size(block, pool_bytes);
  684. block_set_free(block);
  685. block_set_prev_used(block);
  686. block_insert(tlsf_cast(control_t*, tlsf), block);
  687. /* Split the block to create a zero-size sentinel block. */
  688. next = block_link_next(block);
  689. block_set_size(next, 0);
  690. block_set_used(next);
  691. block_set_prev_free(next);
  692. return mem;
  693. }
  694. void tlsf_remove_pool(tlsf_t tlsf, pool_t pool)
  695. {
  696. control_t* control = tlsf_cast(control_t*, tlsf);
  697. block_header_t* block = offset_to_block(pool, -(int)block_header_overhead);
  698. int fl = 0, sl = 0;
  699. tlsf_assert(block_is_free(block) && "block should be free");
  700. tlsf_assert(!block_is_free(block_next(block)) && "next block should not be free");
  701. tlsf_assert(block_size(block_next(block)) == 0 && "next block size should be zero");
  702. mapping_insert(block_size(block), &fl, &sl);
  703. remove_free_block(control, block, fl, sl);
  704. }
  705. /*
  706. ** TLSF main interface.
  707. */
  708. #if _DEBUG
  709. int test_ffs_fls()
  710. {
  711. /* Verify ffs/fls work properly. */
  712. int rv = 0;
  713. rv += (tlsf_ffs(0) == -1) ? 0 : 0x1;
  714. rv += (tlsf_fls(0) == -1) ? 0 : 0x2;
  715. rv += (tlsf_ffs(1) == 0) ? 0 : 0x4;
  716. rv += (tlsf_fls(1) == 0) ? 0 : 0x8;
  717. rv += (tlsf_ffs(0x80000000) == 31) ? 0 : 0x10;
  718. rv += (tlsf_ffs(0x80008000) == 15) ? 0 : 0x20;
  719. rv += (tlsf_fls(0x80000008) == 31) ? 0 : 0x40;
  720. rv += (tlsf_fls(0x7FFFFFFF) == 30) ? 0 : 0x80;
  721. #if defined (TLSF_64BIT)
  722. rv += (tlsf_fls_sizet(0x80000000) == 31) ? 0 : 0x100;
  723. rv += (tlsf_fls_sizet(0x100000000) == 32) ? 0 : 0x200;
  724. rv += (tlsf_fls_sizet(0xffffffffffffffff) == 63) ? 0 : 0x400;
  725. #endif
  726. if (rv)
  727. {
  728. printf("tlsf_create: %x ffs/fls tests failed!\n", rv);
  729. }
  730. return rv;
  731. }
  732. #endif
  733. tlsf_t tlsf_create(void* mem)
  734. {
  735. #if _DEBUG
  736. if (test_ffs_fls())
  737. {
  738. return 0;
  739. }
  740. #endif
  741. if (((tlsfptr_t)mem % ALIGN_SIZE) != 0)
  742. {
  743. printf("tlsf_create: Memory must be aligned to %u bytes.\n",
  744. (unsigned int)ALIGN_SIZE);
  745. return 0;
  746. }
  747. control_construct(tlsf_cast(control_t*, mem));
  748. return tlsf_cast(tlsf_t, mem);
  749. }
  750. tlsf_t tlsf_create_with_pool(void* mem, size_t bytes)
  751. {
  752. tlsf_t tlsf = tlsf_create(mem);
  753. tlsf_add_pool(tlsf, (char*)mem + tlsf_size(), bytes - tlsf_size());
  754. return tlsf;
  755. }
  756. void tlsf_destroy(tlsf_t tlsf)
  757. {
  758. /* Nothing to do. */
  759. (void)tlsf;
  760. }
  761. pool_t tlsf_get_pool(tlsf_t tlsf)
  762. {
  763. return tlsf_cast(pool_t, (char*)tlsf + tlsf_size());
  764. }
  765. void* tlsf_malloc(tlsf_t tlsf, size_t size)
  766. {
  767. control_t* control = tlsf_cast(control_t*, tlsf);
  768. const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
  769. block_header_t* block = block_locate_free(control, adjust);
  770. return block_prepare_used(control, block, adjust);
  771. }
  772. void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size)
  773. {
  774. control_t* control = tlsf_cast(control_t*, tlsf);
  775. const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
  776. /*
  777. ** We must allocate an additional minimum block size bytes so that if
  778. ** our free block will leave an alignment gap which is smaller, we can
  779. ** trim a leading free block and release it back to the pool. We must
  780. ** do this because the previous physical block is in use, therefore
  781. ** the prev_phys_block field is not valid, and we can't simply adjust
  782. ** the size of that block.
  783. */
  784. const size_t gap_minimum = sizeof(block_header_t);
  785. const size_t size_with_gap = adjust_request_size(adjust + align + gap_minimum, align);
  786. /* If alignment is less than or equals base alignment, we're done. */
  787. const size_t aligned_size = (align <= ALIGN_SIZE) ? adjust : size_with_gap;
  788. block_header_t* block = block_locate_free(control, aligned_size);
  789. /* This can't be a static assert. */
  790. tlsf_assert(sizeof(block_header_t) == block_size_min + block_header_overhead);
  791. if (block)
  792. {
  793. void* ptr = block_to_ptr(block);
  794. void* aligned = align_ptr(ptr, align);
  795. size_t gap = tlsf_cast(size_t,
  796. tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr));
  797. /* If gap size is too small, offset to next aligned boundary. */
  798. if (gap && gap < gap_minimum)
  799. {
  800. const size_t gap_remain = gap_minimum - gap;
  801. const size_t offset = tlsf_max(gap_remain, align);
  802. const void* next_aligned = tlsf_cast(void*,
  803. tlsf_cast(tlsfptr_t, aligned) + offset);
  804. aligned = align_ptr(next_aligned, align);
  805. gap = tlsf_cast(size_t,
  806. tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr));
  807. }
  808. if (gap)
  809. {
  810. tlsf_assert(gap >= gap_minimum && "gap size too small");
  811. block = block_trim_free_leading(control, block, gap);
  812. }
  813. }
  814. return block_prepare_used(control, block, adjust);
  815. }
  816. void tlsf_free(tlsf_t tlsf, void* ptr)
  817. {
  818. /* Don't attempt to free a NULL pointer. */
  819. if (ptr)
  820. {
  821. control_t* control = tlsf_cast(control_t*, tlsf);
  822. block_header_t* block = block_from_ptr(ptr);
  823. tlsf_assert(!block_is_free(block) && "block already marked as free");
  824. block_mark_as_free(block);
  825. block = block_merge_prev(control, block);
  826. block = block_merge_next(control, block);
  827. block_insert(control, block);
  828. }
  829. }
  830. /*
  831. ** The TLSF block information provides us with enough information to
  832. ** provide a reasonably intelligent implementation of realloc, growing or
  833. ** shrinking the currently allocated block as required.
  834. **
  835. ** This routine handles the somewhat esoteric edge cases of realloc:
  836. ** - a non-zero size with a null pointer will behave like malloc
  837. ** - a zero size with a non-null pointer will behave like free
  838. ** - a request that cannot be satisfied will leave the original buffer
  839. ** untouched
  840. ** - an extended buffer size will leave the newly-allocated area with
  841. ** contents undefined
  842. */
  843. void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
  844. {
  845. control_t* control = tlsf_cast(control_t*, tlsf);
  846. void* p = 0;
  847. /* Zero-size requests are treated as free. */
  848. if (ptr && size == 0)
  849. {
  850. tlsf_free(tlsf, ptr);
  851. }
  852. /* Requests with NULL pointers are treated as malloc. */
  853. else if (!ptr)
  854. {
  855. p = tlsf_malloc(tlsf, size);
  856. }
  857. else
  858. {
  859. block_header_t* block = block_from_ptr(ptr);
  860. block_header_t* next = block_next(block);
  861. const size_t cursize = block_size(block);
  862. const size_t combined = cursize + block_size(next) + block_header_overhead;
  863. const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
  864. tlsf_assert(!block_is_free(block) && "block already marked as free");
  865. /*
  866. ** If the next block is used, or when combined with the current
  867. ** block, does not offer enough space, we must reallocate and copy.
  868. */
  869. if (adjust > cursize && (!block_is_free(next) || adjust > combined))
  870. {
  871. p = tlsf_malloc(tlsf, size);
  872. if (p)
  873. {
  874. const size_t minsize = tlsf_min(cursize, size);
  875. memcpy(p, ptr, minsize);
  876. tlsf_free(tlsf, ptr);
  877. }
  878. }
  879. else
  880. {
  881. /* Do we need to expand to the next block? */
  882. if (adjust > cursize)
  883. {
  884. block_merge_next(control, block);
  885. block_mark_as_used(block);
  886. }
  887. /* Trim the resulting block and return the original pointer. */
  888. block_trim_used(control, block, adjust);
  889. p = ptr;
  890. }
  891. }
  892. return p;
  893. }