tlsf.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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. #if defined(__cplusplus)
  9. #define tlsf_decl inline
  10. #else
  11. #define tlsf_decl static
  12. #endif
  13. #define LUAT_LOG_TAG "tlsf"
  14. #include "luat_log.h"
  15. #ifdef printf
  16. #undef printf
  17. #define printf LLOGE
  18. #endif
  19. /*
  20. ** Architecture-specific bit manipulation routines.
  21. **
  22. ** TLSF achieves O(1) cost for malloc and free operations by limiting
  23. ** the search for a free block to a free list of guaranteed size
  24. ** adequate to fulfill the request, combined with efficient free list
  25. ** queries using bitmasks and architecture-specific bit-manipulation
  26. ** routines.
  27. **
  28. ** Most modern processors provide instructions to count leading zeroes
  29. ** in a word, find the lowest and highest set bit, etc. These
  30. ** specific implementations will be used when available, falling back
  31. ** to a reasonably efficient generic implementation.
  32. **
  33. ** NOTE: TLSF spec relies on ffs/fls returning value 0..31.
  34. ** ffs/fls return 1-32 by default, returning 0 for error.
  35. */
  36. /*
  37. ** Detect whether or not we are building for a 32- or 64-bit (LP/LLP)
  38. ** architecture. There is no reliable portable method at compile-time.
  39. */
  40. #if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) \
  41. || defined (_WIN64) || defined (__LP64__) || defined (__LLP64__)
  42. #define TLSF_64BIT
  43. #endif
  44. /*
  45. ** gcc 3.4 and above have builtin support, specialized for architecture.
  46. ** Some compilers masquerade as gcc; patchlevel test filters them out.
  47. */
  48. #if defined (__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) \
  49. && defined (__GNUC_PATCHLEVEL__)
  50. #if defined (__SNC__)
  51. /* SNC for Playstation 3. */
  52. tlsf_decl int tlsf_ffs(unsigned int word)
  53. {
  54. const unsigned int reverse = word & (~word + 1);
  55. const int bit = 32 - __builtin_clz(reverse);
  56. return bit - 1;
  57. }
  58. #else
  59. tlsf_decl int tlsf_ffs(unsigned int word)
  60. {
  61. return __builtin_ffs(word) - 1;
  62. }
  63. #endif
  64. tlsf_decl int tlsf_fls(unsigned int word)
  65. {
  66. const int bit = word ? 32 - __builtin_clz(word) : 0;
  67. return bit - 1;
  68. }
  69. #elif defined (_MSC_VER) && (_MSC_VER >= 1400) && (defined (_M_IX86) || defined (_M_X64))
  70. /* Microsoft Visual C++ support on x86/X64 architectures. */
  71. #include <intrin.h>
  72. #pragma intrinsic(_BitScanReverse)
  73. #pragma intrinsic(_BitScanForward)
  74. tlsf_decl int tlsf_fls(unsigned int word)
  75. {
  76. unsigned long index;
  77. return _BitScanReverse(&index, word) ? index : -1;
  78. }
  79. tlsf_decl int tlsf_ffs(unsigned int word)
  80. {
  81. unsigned long index;
  82. return _BitScanForward(&index, word) ? index : -1;
  83. }
  84. #elif defined (_MSC_VER) && defined (_M_PPC)
  85. /* Microsoft Visual C++ support on PowerPC architectures. */
  86. #include <ppcintrinsics.h>
  87. tlsf_decl int tlsf_fls(unsigned int word)
  88. {
  89. const int bit = 32 - _CountLeadingZeros(word);
  90. return bit - 1;
  91. }
  92. tlsf_decl int tlsf_ffs(unsigned int word)
  93. {
  94. const unsigned int reverse = word & (~word + 1);
  95. const int bit = 32 - _CountLeadingZeros(reverse);
  96. return bit - 1;
  97. }
  98. #elif defined (__ARMCC_VERSION)
  99. /* RealView Compilation Tools for ARM */
  100. tlsf_decl int tlsf_ffs(unsigned int word)
  101. {
  102. const unsigned int reverse = word & (~word + 1);
  103. const int bit = 32 - __clz(reverse);
  104. return bit - 1;
  105. }
  106. tlsf_decl int tlsf_fls(unsigned int word)
  107. {
  108. const int bit = word ? 32 - __clz(word) : 0;
  109. return bit - 1;
  110. }
  111. #elif defined (__ghs__)
  112. /* Green Hills support for PowerPC */
  113. #include <ppc_ghs.h>
  114. tlsf_decl int tlsf_ffs(unsigned int word)
  115. {
  116. const unsigned int reverse = word & (~word + 1);
  117. const int bit = 32 - __CLZ32(reverse);
  118. return bit - 1;
  119. }
  120. tlsf_decl int tlsf_fls(unsigned int word)
  121. {
  122. const int bit = word ? 32 - __CLZ32(word) : 0;
  123. return bit - 1;
  124. }
  125. #else
  126. /* Fall back to generic implementation. */
  127. tlsf_decl int tlsf_fls_generic(unsigned int word)
  128. {
  129. int bit = 32;
  130. if (!word) bit -= 1;
  131. if (!(word & 0xffff0000)) { word <<= 16; bit -= 16; }
  132. if (!(word & 0xff000000)) { word <<= 8; bit -= 8; }
  133. if (!(word & 0xf0000000)) { word <<= 4; bit -= 4; }
  134. if (!(word & 0xc0000000)) { word <<= 2; bit -= 2; }
  135. if (!(word & 0x80000000)) { word <<= 1; bit -= 1; }
  136. return bit;
  137. }
  138. /* Implement ffs in terms of fls. */
  139. tlsf_decl int tlsf_ffs(unsigned int word)
  140. {
  141. return tlsf_fls_generic(word & (~word + 1)) - 1;
  142. }
  143. tlsf_decl int tlsf_fls(unsigned int word)
  144. {
  145. return tlsf_fls_generic(word) - 1;
  146. }
  147. #endif
  148. /* Possibly 64-bit version of tlsf_fls. */
  149. #if defined (TLSF_64BIT)
  150. tlsf_decl int tlsf_fls_sizet(size_t size)
  151. {
  152. int high = (int)(size >> 32);
  153. int bits = 0;
  154. if (high)
  155. {
  156. bits = 32 + tlsf_fls(high);
  157. }
  158. else
  159. {
  160. bits = tlsf_fls((int)size & 0xffffffff);
  161. }
  162. return bits;
  163. }
  164. #else
  165. #define tlsf_fls_sizet tlsf_fls
  166. #endif
  167. #undef tlsf_decl
  168. /*
  169. ** Constants.
  170. */
  171. /* Public constants: may be modified. */
  172. enum tlsf_public
  173. {
  174. /* log2 of number of linear subdivisions of block sizes. Larger
  175. ** values require more memory in the control structure. Values of
  176. ** 4 or 5 are typical.
  177. */
  178. SL_INDEX_COUNT_LOG2 = 5,
  179. };
  180. /* Private constants: do not modify. */
  181. enum tlsf_private
  182. {
  183. #if defined (TLSF_64BIT)
  184. /* All allocation sizes and addresses are aligned to 8 bytes. */
  185. ALIGN_SIZE_LOG2 = 3,
  186. #else
  187. /* All allocation sizes and addresses are aligned to 4 bytes. */
  188. ALIGN_SIZE_LOG2 = 2,
  189. #endif
  190. ALIGN_SIZE = (1 << ALIGN_SIZE_LOG2),
  191. /*
  192. ** We support allocations of sizes up to (1 << FL_INDEX_MAX) bits.
  193. ** However, because we linearly subdivide the second-level lists, and
  194. ** our minimum size granularity is 4 bytes, it doesn't make sense to
  195. ** create first-level lists for sizes smaller than SL_INDEX_COUNT * 4,
  196. ** or (1 << (SL_INDEX_COUNT_LOG2 + 2)) bytes, as there we will be
  197. ** trying to split size ranges into more slots than we have available.
  198. ** Instead, we calculate the minimum threshold size, and place all
  199. ** blocks below that size into the 0th first-level list.
  200. */
  201. #if defined (TLSF_64BIT)
  202. /*
  203. ** TODO: We can increase this to support larger sizes, at the expense
  204. ** of more overhead in the TLSF structure.
  205. */
  206. FL_INDEX_MAX = 32,
  207. #else
  208. FL_INDEX_MAX = 30,
  209. #endif
  210. SL_INDEX_COUNT = (1 << SL_INDEX_COUNT_LOG2),
  211. FL_INDEX_SHIFT = (SL_INDEX_COUNT_LOG2 + ALIGN_SIZE_LOG2),
  212. FL_INDEX_COUNT = (FL_INDEX_MAX - FL_INDEX_SHIFT + 1),
  213. SMALL_BLOCK_SIZE = (1 << FL_INDEX_SHIFT),
  214. };
  215. /*
  216. ** Cast and min/max macros.
  217. */
  218. #define tlsf_cast(t, exp) ((t) (exp))
  219. #define tlsf_min(a, b) ((a) < (b) ? (a) : (b))
  220. #define tlsf_max(a, b) ((a) > (b) ? (a) : (b))
  221. /*
  222. ** Set assert macro, if it has not been provided by the user.
  223. */
  224. #if !defined (tlsf_assert)
  225. #define tlsf_assert assert
  226. #endif
  227. /*
  228. ** Static assertion mechanism.
  229. */
  230. #define _tlsf_glue2(x, y) x ## y
  231. #define _tlsf_glue(x, y) _tlsf_glue2(x, y)
  232. #define tlsf_static_assert(exp) \
  233. typedef char _tlsf_glue(static_assert, __LINE__) [(exp) ? 1 : -1]
  234. /* This code has been tested on 32- and 64-bit (LP/LLP) architectures. */
  235. tlsf_static_assert(sizeof(int) * CHAR_BIT == 32);
  236. tlsf_static_assert(sizeof(size_t) * CHAR_BIT >= 32);
  237. tlsf_static_assert(sizeof(size_t) * CHAR_BIT <= 64);
  238. /* SL_INDEX_COUNT must be <= number of bits in sl_bitmap's storage type. */
  239. tlsf_static_assert(sizeof(unsigned int) * CHAR_BIT >= SL_INDEX_COUNT);
  240. /* Ensure we've properly tuned our sizes. */
  241. tlsf_static_assert(ALIGN_SIZE == SMALL_BLOCK_SIZE / SL_INDEX_COUNT);
  242. /*
  243. ** Data structures and associated constants.
  244. */
  245. /*
  246. ** Block header structure.
  247. **
  248. ** There are several implementation subtleties involved:
  249. ** - The prev_phys_block field is only valid if the previous block is free.
  250. ** - The prev_phys_block field is actually stored at the end of the
  251. ** previous block. It appears at the beginning of this structure only to
  252. ** simplify the implementation.
  253. ** - The next_free / prev_free fields are only valid if the block is free.
  254. */
  255. typedef struct block_header_t
  256. {
  257. /* Points to the previous physical block. */
  258. struct block_header_t* prev_phys_block;
  259. /* The size of this block, excluding the block header. */
  260. size_t size;
  261. /* Next and previous free blocks. */
  262. struct block_header_t* next_free;
  263. struct block_header_t* prev_free;
  264. } block_header_t;
  265. /*
  266. ** Since block sizes are always at least a multiple of 4, the two least
  267. ** significant bits of the size field are used to store the block status:
  268. ** - bit 0: whether block is busy or free
  269. ** - bit 1: whether previous block is busy or free
  270. */
  271. static const size_t block_header_free_bit = 1 << 0;
  272. static const size_t block_header_prev_free_bit = 1 << 1;
  273. /*
  274. ** The size of the block header exposed to used blocks is the size field.
  275. ** The prev_phys_block field is stored *inside* the previous free block.
  276. */
  277. static const size_t block_header_overhead = sizeof(size_t);
  278. /* User data starts directly after the size field in a used block. */
  279. static const size_t block_start_offset =
  280. offsetof(block_header_t, size) + sizeof(size_t);
  281. /*
  282. ** A free block must be large enough to store its header minus the size of
  283. ** the prev_phys_block field, and no larger than the number of addressable
  284. ** bits for FL_INDEX.
  285. */
  286. static const size_t block_size_min =
  287. sizeof(block_header_t) - sizeof(block_header_t*);
  288. static const size_t block_size_max = tlsf_cast(size_t, 1) << FL_INDEX_MAX;
  289. /* The TLSF control structure. */
  290. typedef struct control_t
  291. {
  292. /* Empty lists point at this block to indicate they are free. */
  293. block_header_t block_null;
  294. /* Bitmaps for free lists. */
  295. unsigned int fl_bitmap;
  296. unsigned int sl_bitmap[FL_INDEX_COUNT];
  297. /* Head of free lists. */
  298. block_header_t* blocks[FL_INDEX_COUNT][SL_INDEX_COUNT];
  299. } control_t;
  300. /* A type used for casting when doing pointer arithmetic. */
  301. typedef ptrdiff_t tlsfptr_t;
  302. /*
  303. ** block_header_t member functions.
  304. */
  305. static size_t block_size(const block_header_t* block)
  306. {
  307. return block->size & ~(block_header_free_bit | block_header_prev_free_bit);
  308. }
  309. static void block_set_size(block_header_t* block, size_t size)
  310. {
  311. const size_t oldsize = block->size;
  312. block->size = size | (oldsize & (block_header_free_bit | block_header_prev_free_bit));
  313. }
  314. static int block_is_last(const block_header_t* block)
  315. {
  316. return block_size(block) == 0;
  317. }
  318. static int block_is_free(const block_header_t* block)
  319. {
  320. return tlsf_cast(int, block->size & block_header_free_bit);
  321. }
  322. static void block_set_free(block_header_t* block)
  323. {
  324. block->size |= block_header_free_bit;
  325. }
  326. static void block_set_used(block_header_t* block)
  327. {
  328. block->size &= ~block_header_free_bit;
  329. }
  330. static int block_is_prev_free(const block_header_t* block)
  331. {
  332. return tlsf_cast(int, block->size & block_header_prev_free_bit);
  333. }
  334. static void block_set_prev_free(block_header_t* block)
  335. {
  336. block->size |= block_header_prev_free_bit;
  337. }
  338. static void block_set_prev_used(block_header_t* block)
  339. {
  340. block->size &= ~block_header_prev_free_bit;
  341. }
  342. static block_header_t* block_from_ptr(const void* ptr)
  343. {
  344. return tlsf_cast(block_header_t*,
  345. tlsf_cast(unsigned char*, ptr) - block_start_offset);
  346. }
  347. static void* block_to_ptr(const block_header_t* block)
  348. {
  349. return tlsf_cast(void*,
  350. tlsf_cast(unsigned char*, block) + block_start_offset);
  351. }
  352. /* Return location of next block after block of given size. */
  353. static block_header_t* offset_to_block(const void* ptr, size_t size)
  354. {
  355. return tlsf_cast(block_header_t*, tlsf_cast(tlsfptr_t, ptr) + size);
  356. }
  357. /* Return location of previous block. */
  358. static block_header_t* block_prev(const block_header_t* block)
  359. {
  360. tlsf_assert(block_is_prev_free(block) && "previous block must be free");
  361. return block->prev_phys_block;
  362. }
  363. /* Return location of next existing block. */
  364. static block_header_t* block_next(const block_header_t* block)
  365. {
  366. block_header_t* next = offset_to_block(block_to_ptr(block),
  367. block_size(block) - block_header_overhead);
  368. tlsf_assert(!block_is_last(block));
  369. return next;
  370. }
  371. /* Link a new block with its physical neighbor, return the neighbor. */
  372. static block_header_t* block_link_next(block_header_t* block)
  373. {
  374. block_header_t* next = block_next(block);
  375. next->prev_phys_block = block;
  376. return next;
  377. }
  378. static void block_mark_as_free(block_header_t* block)
  379. {
  380. /* Link the block to the next block, first. */
  381. block_header_t* next = block_link_next(block);
  382. block_set_prev_free(next);
  383. block_set_free(block);
  384. }
  385. static void block_mark_as_used(block_header_t* block)
  386. {
  387. block_header_t* next = block_next(block);
  388. block_set_prev_used(next);
  389. block_set_used(block);
  390. }
  391. static size_t align_up(size_t x, size_t align)
  392. {
  393. tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two");
  394. return (x + (align - 1)) & ~(align - 1);
  395. }
  396. static size_t align_down(size_t x, size_t align)
  397. {
  398. tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two");
  399. return x - (x & (align - 1));
  400. }
  401. static void* align_ptr(const void* ptr, size_t align)
  402. {
  403. const tlsfptr_t aligned =
  404. (tlsf_cast(tlsfptr_t, ptr) + (align - 1)) & ~(align - 1);
  405. tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two");
  406. return tlsf_cast(void*, aligned);
  407. }
  408. /*
  409. ** Adjust an allocation size to be aligned to word size, and no smaller
  410. ** than internal minimum.
  411. */
  412. static size_t adjust_request_size(size_t size, size_t align)
  413. {
  414. size_t adjust = 0;
  415. if (size)
  416. {
  417. const size_t aligned = align_up(size, align);
  418. /* aligned sized must not exceed block_size_max or we'll go out of bounds on sl_bitmap */
  419. if (aligned < block_size_max)
  420. {
  421. adjust = tlsf_max(aligned, block_size_min);
  422. }
  423. }
  424. return adjust;
  425. }
  426. /*
  427. ** TLSF utility functions. In most cases, these are direct translations of
  428. ** the documentation found in the white paper.
  429. */
  430. static void mapping_insert(size_t size, int* fli, int* sli)
  431. {
  432. int fl, sl;
  433. if (size < SMALL_BLOCK_SIZE)
  434. {
  435. /* Store small blocks in first list. */
  436. fl = 0;
  437. sl = tlsf_cast(int, size) / (SMALL_BLOCK_SIZE / SL_INDEX_COUNT);
  438. }
  439. else
  440. {
  441. fl = tlsf_fls_sizet(size);
  442. sl = tlsf_cast(int, size >> (fl - SL_INDEX_COUNT_LOG2)) ^ (1 << SL_INDEX_COUNT_LOG2);
  443. fl -= (FL_INDEX_SHIFT - 1);
  444. }
  445. *fli = fl;
  446. *sli = sl;
  447. }
  448. /* This version rounds up to the next block size (for allocations) */
  449. static void mapping_search(size_t size, int* fli, int* sli)
  450. {
  451. if (size >= SMALL_BLOCK_SIZE)
  452. {
  453. const size_t round = (1 << (tlsf_fls_sizet(size) - SL_INDEX_COUNT_LOG2)) - 1;
  454. size += round;
  455. }
  456. mapping_insert(size, fli, sli);
  457. }
  458. static block_header_t* search_suitable_block(control_t* control, int* fli, int* sli)
  459. {
  460. int fl = *fli;
  461. int sl = *sli;
  462. /*
  463. ** First, search for a block in the list associated with the given
  464. ** fl/sl index.
  465. */
  466. unsigned int sl_map = control->sl_bitmap[fl] & (~0U << sl);
  467. if (!sl_map)
  468. {
  469. /* No block exists. Search in the next largest first-level list. */
  470. const unsigned int fl_map = control->fl_bitmap & (~0U << (fl + 1));
  471. if (!fl_map)
  472. {
  473. /* No free blocks available, memory has been exhausted. */
  474. return 0;
  475. }
  476. fl = tlsf_ffs(fl_map);
  477. *fli = fl;
  478. sl_map = control->sl_bitmap[fl];
  479. }
  480. tlsf_assert(sl_map && "internal error - second level bitmap is null");
  481. sl = tlsf_ffs(sl_map);
  482. *sli = sl;
  483. /* Return the first block in the free list. */
  484. return control->blocks[fl][sl];
  485. }
  486. /* Remove a free block from the free list.*/
  487. static void remove_free_block(control_t* control, block_header_t* block, int fl, int sl)
  488. {
  489. block_header_t* prev = block->prev_free;
  490. block_header_t* next = block->next_free;
  491. tlsf_assert(prev && "prev_free field can not be null");
  492. tlsf_assert(next && "next_free field can not be null");
  493. next->prev_free = prev;
  494. prev->next_free = next;
  495. /* If this block is the head of the free list, set new head. */
  496. if (control->blocks[fl][sl] == block)
  497. {
  498. control->blocks[fl][sl] = next;
  499. /* If the new head is null, clear the bitmap. */
  500. if (next == &control->block_null)
  501. {
  502. control->sl_bitmap[fl] &= ~(1U << sl);
  503. /* If the second bitmap is now empty, clear the fl bitmap. */
  504. if (!control->sl_bitmap[fl])
  505. {
  506. control->fl_bitmap &= ~(1U << fl);
  507. }
  508. }
  509. }
  510. }
  511. /* Insert a free block into the free block list. */
  512. static void insert_free_block(control_t* control, block_header_t* block, int fl, int sl)
  513. {
  514. block_header_t* current = control->blocks[fl][sl];
  515. tlsf_assert(current && "free list cannot have a null entry");
  516. tlsf_assert(block && "cannot insert a null entry into the free list");
  517. block->next_free = current;
  518. block->prev_free = &control->block_null;
  519. current->prev_free = block;
  520. tlsf_assert(block_to_ptr(block) == align_ptr(block_to_ptr(block), ALIGN_SIZE)
  521. && "block not aligned properly");
  522. /*
  523. ** Insert the new block at the head of the list, and mark the first-
  524. ** and second-level bitmaps appropriately.
  525. */
  526. control->blocks[fl][sl] = block;
  527. control->fl_bitmap |= (1U << fl);
  528. control->sl_bitmap[fl] |= (1U << sl);
  529. }
  530. /* Remove a given block from the free list. */
  531. static void block_remove(control_t* control, block_header_t* block)
  532. {
  533. int fl, sl;
  534. mapping_insert(block_size(block), &fl, &sl);
  535. remove_free_block(control, block, fl, sl);
  536. }
  537. /* Insert a given block into the free list. */
  538. static void block_insert(control_t* control, block_header_t* block)
  539. {
  540. int fl, sl;
  541. mapping_insert(block_size(block), &fl, &sl);
  542. insert_free_block(control, block, fl, sl);
  543. }
  544. static int block_can_split(block_header_t* block, size_t size)
  545. {
  546. return block_size(block) >= sizeof(block_header_t) + size;
  547. }
  548. /* Split a block into two, the second of which is free. */
  549. static block_header_t* block_split(block_header_t* block, size_t size)
  550. {
  551. /* Calculate the amount of space left in the remaining block. */
  552. block_header_t* remaining =
  553. offset_to_block(block_to_ptr(block), size - block_header_overhead);
  554. const size_t remain_size = block_size(block) - (size + block_header_overhead);
  555. tlsf_assert(block_to_ptr(remaining) == align_ptr(block_to_ptr(remaining), ALIGN_SIZE)
  556. && "remaining block not aligned properly");
  557. tlsf_assert(block_size(block) == remain_size + size + block_header_overhead);
  558. block_set_size(remaining, remain_size);
  559. tlsf_assert(block_size(remaining) >= block_size_min && "block split with invalid size");
  560. block_set_size(block, size);
  561. block_mark_as_free(remaining);
  562. return remaining;
  563. }
  564. /* Absorb a free block's storage into an adjacent previous free block. */
  565. static block_header_t* block_absorb(block_header_t* prev, block_header_t* block)
  566. {
  567. tlsf_assert(!block_is_last(prev) && "previous block can't be last");
  568. /* Note: Leaves flags untouched. */
  569. prev->size += block_size(block) + block_header_overhead;
  570. block_link_next(prev);
  571. return prev;
  572. }
  573. /* Merge a just-freed block with an adjacent previous free block. */
  574. static block_header_t* block_merge_prev(control_t* control, block_header_t* block)
  575. {
  576. if (block_is_prev_free(block))
  577. {
  578. block_header_t* prev = block_prev(block);
  579. tlsf_assert(prev && "prev physical block can't be null");
  580. tlsf_assert(block_is_free(prev) && "prev block is not free though marked as such");
  581. block_remove(control, prev);
  582. block = block_absorb(prev, block);
  583. }
  584. return block;
  585. }
  586. /* Merge a just-freed block with an adjacent free block. */
  587. static block_header_t* block_merge_next(control_t* control, block_header_t* block)
  588. {
  589. block_header_t* next = block_next(block);
  590. tlsf_assert(next && "next physical block can't be null");
  591. if (block_is_free(next))
  592. {
  593. tlsf_assert(!block_is_last(block) && "previous block can't be last");
  594. block_remove(control, next);
  595. block = block_absorb(block, next);
  596. }
  597. return block;
  598. }
  599. /* Trim any trailing block space off the end of a block, return to pool. */
  600. static void block_trim_free(control_t* control, block_header_t* block, size_t size)
  601. {
  602. tlsf_assert(block_is_free(block) && "block must be free");
  603. if (block_can_split(block, size))
  604. {
  605. block_header_t* remaining_block = block_split(block, size);
  606. block_link_next(block);
  607. block_set_prev_free(remaining_block);
  608. block_insert(control, remaining_block);
  609. }
  610. }
  611. /* Trim any trailing block space off the end of a used block, return to pool. */
  612. static void block_trim_used(control_t* control, block_header_t* block, size_t size)
  613. {
  614. tlsf_assert(!block_is_free(block) && "block must be used");
  615. if (block_can_split(block, size))
  616. {
  617. /* If the next block is free, we must coalesce. */
  618. block_header_t* remaining_block = block_split(block, size);
  619. block_set_prev_used(remaining_block);
  620. remaining_block = block_merge_next(control, remaining_block);
  621. block_insert(control, remaining_block);
  622. }
  623. }
  624. static block_header_t* block_trim_free_leading(control_t* control, block_header_t* block, size_t size)
  625. {
  626. block_header_t* remaining_block = block;
  627. if (block_can_split(block, size))
  628. {
  629. /* We want the 2nd block. */
  630. remaining_block = block_split(block, size - block_header_overhead);
  631. block_set_prev_free(remaining_block);
  632. block_link_next(block);
  633. block_insert(control, block);
  634. }
  635. return remaining_block;
  636. }
  637. static block_header_t* block_locate_free(control_t* control, size_t size)
  638. {
  639. int fl = 0, sl = 0;
  640. block_header_t* block = 0;
  641. if (size)
  642. {
  643. mapping_search(size, &fl, &sl);
  644. /*
  645. ** mapping_search can futz with the size, so for excessively large sizes it can sometimes wind up
  646. ** with indices that are off the end of the block array.
  647. ** So, we protect against that here, since this is the only callsite of mapping_search.
  648. ** Note that we don't need to check sl, since it comes from a modulo operation that guarantees it's always in range.
  649. */
  650. if (fl < FL_INDEX_COUNT)
  651. {
  652. block = search_suitable_block(control, &fl, &sl);
  653. }
  654. }
  655. if (block)
  656. {
  657. tlsf_assert(block_size(block) >= size);
  658. remove_free_block(control, block, fl, sl);
  659. }
  660. return block;
  661. }
  662. static void* block_prepare_used(control_t* control, block_header_t* block, size_t size)
  663. {
  664. void* p = 0;
  665. if (block)
  666. {
  667. tlsf_assert(size && "size must be non-zero");
  668. block_trim_free(control, block, size);
  669. block_mark_as_used(block);
  670. p = block_to_ptr(block);
  671. }
  672. return p;
  673. }
  674. /* Clear structure and point all empty lists at the null block. */
  675. static void control_construct(control_t* control)
  676. {
  677. int i, j;
  678. control->block_null.next_free = &control->block_null;
  679. control->block_null.prev_free = &control->block_null;
  680. control->fl_bitmap = 0;
  681. for (i = 0; i < FL_INDEX_COUNT; ++i)
  682. {
  683. control->sl_bitmap[i] = 0;
  684. for (j = 0; j < SL_INDEX_COUNT; ++j)
  685. {
  686. control->blocks[i][j] = &control->block_null;
  687. }
  688. }
  689. }
  690. /*
  691. ** Debugging utilities.
  692. */
  693. typedef struct integrity_t
  694. {
  695. int prev_status;
  696. int status;
  697. } integrity_t;
  698. #define tlsf_insist(x) { tlsf_assert(x); if (!(x)) { status--; } }
  699. static void integrity_walker(void* ptr, size_t size, int used, void* user)
  700. {
  701. block_header_t* block = block_from_ptr(ptr);
  702. integrity_t* integ = tlsf_cast(integrity_t*, user);
  703. const int this_prev_status = block_is_prev_free(block) ? 1 : 0;
  704. const int this_status = block_is_free(block) ? 1 : 0;
  705. const size_t this_block_size = block_size(block);
  706. int status = 0;
  707. (void)used;
  708. tlsf_insist(integ->prev_status == this_prev_status && "prev status incorrect");
  709. tlsf_insist(size == this_block_size && "block size incorrect");
  710. integ->prev_status = this_status;
  711. integ->status += status;
  712. }
  713. int tlsf_check(tlsf_t tlsf)
  714. {
  715. int i, j;
  716. control_t* control = tlsf_cast(control_t*, tlsf);
  717. int status = 0;
  718. /* Check that the free lists and bitmaps are accurate. */
  719. for (i = 0; i < FL_INDEX_COUNT; ++i)
  720. {
  721. for (j = 0; j < SL_INDEX_COUNT; ++j)
  722. {
  723. const int fl_map = control->fl_bitmap & (1U << i);
  724. const int sl_list = control->sl_bitmap[i];
  725. const int sl_map = sl_list & (1U << j);
  726. const block_header_t* block = control->blocks[i][j];
  727. /* Check that first- and second-level lists agree. */
  728. if (!fl_map)
  729. {
  730. tlsf_insist(!sl_map && "second-level map must be null");
  731. }
  732. if (!sl_map)
  733. {
  734. tlsf_insist(block == &control->block_null && "block list must be null");
  735. continue;
  736. }
  737. /* Check that there is at least one free block. */
  738. tlsf_insist(sl_list && "no free blocks in second-level map");
  739. tlsf_insist(block != &control->block_null && "block should not be null");
  740. while (block != &control->block_null)
  741. {
  742. int fli, sli;
  743. tlsf_insist(block_is_free(block) && "block should be free");
  744. tlsf_insist(!block_is_prev_free(block) && "blocks should have coalesced");
  745. tlsf_insist(!block_is_free(block_next(block)) && "blocks should have coalesced");
  746. tlsf_insist(block_is_prev_free(block_next(block)) && "block should be free");
  747. tlsf_insist(block_size(block) >= block_size_min && "block not minimum size");
  748. mapping_insert(block_size(block), &fli, &sli);
  749. tlsf_insist(fli == i && sli == j && "block size indexed in wrong list");
  750. block = block->next_free;
  751. }
  752. }
  753. }
  754. return status;
  755. }
  756. #undef tlsf_insist
  757. static void default_walker(void* ptr, size_t size, int used, void* user)
  758. {
  759. (void)user;
  760. printf("\t%p %s size: %x (%p)\n", ptr, used ? "used" : "free", (unsigned int)size, block_from_ptr(ptr));
  761. }
  762. void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user)
  763. {
  764. tlsf_walker pool_walker = walker ? walker : default_walker;
  765. block_header_t* block =
  766. offset_to_block(pool, -(int)block_header_overhead);
  767. while (block && !block_is_last(block))
  768. {
  769. pool_walker(
  770. block_to_ptr(block),
  771. block_size(block),
  772. !block_is_free(block),
  773. user);
  774. block = block_next(block);
  775. }
  776. }
  777. size_t tlsf_block_size(void* ptr)
  778. {
  779. size_t size = 0;
  780. if (ptr)
  781. {
  782. const block_header_t* block = block_from_ptr(ptr);
  783. size = block_size(block);
  784. }
  785. return size;
  786. }
  787. int tlsf_check_pool(pool_t pool)
  788. {
  789. /* Check that the blocks are physically correct. */
  790. integrity_t integ = { 0, 0 };
  791. tlsf_walk_pool(pool, integrity_walker, &integ);
  792. return integ.status;
  793. }
  794. /*
  795. ** Size of the TLSF structures in a given memory block passed to
  796. ** tlsf_create, equal to the size of a control_t
  797. */
  798. size_t tlsf_size(void)
  799. {
  800. return sizeof(control_t);
  801. }
  802. size_t tlsf_align_size(void)
  803. {
  804. return ALIGN_SIZE;
  805. }
  806. size_t tlsf_block_size_min(void)
  807. {
  808. return block_size_min;
  809. }
  810. size_t tlsf_block_size_max(void)
  811. {
  812. return block_size_max;
  813. }
  814. /*
  815. ** Overhead of the TLSF structures in a given memory block passed to
  816. ** tlsf_add_pool, equal to the overhead of a free block and the
  817. ** sentinel block.
  818. */
  819. size_t tlsf_pool_overhead(void)
  820. {
  821. return 2 * block_header_overhead;
  822. }
  823. size_t tlsf_alloc_overhead(void)
  824. {
  825. return block_header_overhead;
  826. }
  827. pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
  828. {
  829. block_header_t* block;
  830. block_header_t* next;
  831. const size_t pool_overhead = tlsf_pool_overhead();
  832. const size_t pool_bytes = align_down(bytes - pool_overhead, ALIGN_SIZE);
  833. if (((ptrdiff_t)mem % ALIGN_SIZE) != 0)
  834. {
  835. printf("tlsf_add_pool: Memory must be aligned by %u bytes.\n",
  836. (unsigned int)ALIGN_SIZE);
  837. return 0;
  838. }
  839. if (pool_bytes < block_size_min || pool_bytes > block_size_max)
  840. {
  841. #if defined (TLSF_64BIT)
  842. printf("tlsf_add_pool: Memory size must be between 0x%x and 0x%x00 bytes.\n",
  843. (unsigned int)(pool_overhead + block_size_min),
  844. (unsigned int)((pool_overhead + block_size_max) / 256));
  845. #else
  846. printf("tlsf_add_pool: Memory size must be between %u and %u bytes.\n",
  847. (unsigned int)(pool_overhead + block_size_min),
  848. (unsigned int)(pool_overhead + block_size_max));
  849. #endif
  850. return 0;
  851. }
  852. /*
  853. ** Create the main free block. Offset the start of the block slightly
  854. ** so that the prev_phys_block field falls outside of the pool -
  855. ** it will never be used.
  856. */
  857. block = offset_to_block(mem, -(tlsfptr_t)block_header_overhead);
  858. block_set_size(block, pool_bytes);
  859. block_set_free(block);
  860. block_set_prev_used(block);
  861. block_insert(tlsf_cast(control_t*, tlsf), block);
  862. /* Split the block to create a zero-size sentinel block. */
  863. next = block_link_next(block);
  864. block_set_size(next, 0);
  865. block_set_used(next);
  866. block_set_prev_free(next);
  867. return mem;
  868. }
  869. void tlsf_remove_pool(tlsf_t tlsf, pool_t pool)
  870. {
  871. control_t* control = tlsf_cast(control_t*, tlsf);
  872. block_header_t* block = offset_to_block(pool, -(int)block_header_overhead);
  873. int fl = 0, sl = 0;
  874. tlsf_assert(block_is_free(block) && "block should be free");
  875. tlsf_assert(!block_is_free(block_next(block)) && "next block should not be free");
  876. tlsf_assert(block_size(block_next(block)) == 0 && "next block size should be zero");
  877. mapping_insert(block_size(block), &fl, &sl);
  878. remove_free_block(control, block, fl, sl);
  879. }
  880. /*
  881. ** TLSF main interface.
  882. */
  883. #if _DEBUG
  884. int test_ffs_fls()
  885. {
  886. /* Verify ffs/fls work properly. */
  887. int rv = 0;
  888. rv += (tlsf_ffs(0) == -1) ? 0 : 0x1;
  889. rv += (tlsf_fls(0) == -1) ? 0 : 0x2;
  890. rv += (tlsf_ffs(1) == 0) ? 0 : 0x4;
  891. rv += (tlsf_fls(1) == 0) ? 0 : 0x8;
  892. rv += (tlsf_ffs(0x80000000) == 31) ? 0 : 0x10;
  893. rv += (tlsf_ffs(0x80008000) == 15) ? 0 : 0x20;
  894. rv += (tlsf_fls(0x80000008) == 31) ? 0 : 0x40;
  895. rv += (tlsf_fls(0x7FFFFFFF) == 30) ? 0 : 0x80;
  896. #if defined (TLSF_64BIT)
  897. rv += (tlsf_fls_sizet(0x80000000) == 31) ? 0 : 0x100;
  898. rv += (tlsf_fls_sizet(0x100000000) == 32) ? 0 : 0x200;
  899. rv += (tlsf_fls_sizet(0xffffffffffffffff) == 63) ? 0 : 0x400;
  900. #endif
  901. if (rv)
  902. {
  903. printf("test_ffs_fls: %x ffs/fls tests failed.\n", rv);
  904. }
  905. return rv;
  906. }
  907. #endif
  908. tlsf_t tlsf_create(void* mem)
  909. {
  910. #if _DEBUG
  911. if (test_ffs_fls())
  912. {
  913. return 0;
  914. }
  915. #endif
  916. if (((tlsfptr_t)mem % ALIGN_SIZE) != 0)
  917. {
  918. printf("tlsf_create: Memory must be aligned to %u bytes.\n",
  919. (unsigned int)ALIGN_SIZE);
  920. return 0;
  921. }
  922. control_construct(tlsf_cast(control_t*, mem));
  923. return tlsf_cast(tlsf_t, mem);
  924. }
  925. tlsf_t tlsf_create_with_pool(void* mem, size_t bytes)
  926. {
  927. tlsf_t tlsf = tlsf_create(mem);
  928. tlsf_add_pool(tlsf, (char*)mem + tlsf_size(), bytes - tlsf_size());
  929. return tlsf;
  930. }
  931. void tlsf_destroy(tlsf_t tlsf)
  932. {
  933. /* Nothing to do. */
  934. (void)tlsf;
  935. }
  936. pool_t tlsf_get_pool(tlsf_t tlsf)
  937. {
  938. return tlsf_cast(pool_t, (char*)tlsf + tlsf_size());
  939. }
  940. void* tlsf_malloc(tlsf_t tlsf, size_t size)
  941. {
  942. control_t* control = tlsf_cast(control_t*, tlsf);
  943. const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
  944. block_header_t* block = block_locate_free(control, adjust);
  945. return block_prepare_used(control, block, adjust);
  946. }
  947. void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size)
  948. {
  949. control_t* control = tlsf_cast(control_t*, tlsf);
  950. const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
  951. /*
  952. ** We must allocate an additional minimum block size bytes so that if
  953. ** our free block will leave an alignment gap which is smaller, we can
  954. ** trim a leading free block and release it back to the pool. We must
  955. ** do this because the previous physical block is in use, therefore
  956. ** the prev_phys_block field is not valid, and we can't simply adjust
  957. ** the size of that block.
  958. */
  959. const size_t gap_minimum = sizeof(block_header_t);
  960. const size_t size_with_gap = adjust_request_size(adjust + align + gap_minimum, align);
  961. /*
  962. ** If alignment is less than or equals base alignment, we're done.
  963. ** If we requested 0 bytes, return null, as tlsf_malloc(0) does.
  964. */
  965. const size_t aligned_size = (adjust && align > ALIGN_SIZE) ? size_with_gap : adjust;
  966. block_header_t* block = block_locate_free(control, aligned_size);
  967. /* This can't be a static assert. */
  968. tlsf_assert(sizeof(block_header_t) == block_size_min + block_header_overhead);
  969. if (block)
  970. {
  971. void* ptr = block_to_ptr(block);
  972. void* aligned = align_ptr(ptr, align);
  973. size_t gap = tlsf_cast(size_t,
  974. tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr));
  975. /* If gap size is too small, offset to next aligned boundary. */
  976. if (gap && gap < gap_minimum)
  977. {
  978. const size_t gap_remain = gap_minimum - gap;
  979. const size_t offset = tlsf_max(gap_remain, align);
  980. const void* next_aligned = tlsf_cast(void*,
  981. tlsf_cast(tlsfptr_t, aligned) + offset);
  982. aligned = align_ptr(next_aligned, align);
  983. gap = tlsf_cast(size_t,
  984. tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr));
  985. }
  986. if (gap)
  987. {
  988. tlsf_assert(gap >= gap_minimum && "gap size too small");
  989. block = block_trim_free_leading(control, block, gap);
  990. }
  991. }
  992. return block_prepare_used(control, block, adjust);
  993. }
  994. void tlsf_free(tlsf_t tlsf, void* ptr)
  995. {
  996. /* Don't attempt to free a NULL pointer. */
  997. if (ptr)
  998. {
  999. control_t* control = tlsf_cast(control_t*, tlsf);
  1000. block_header_t* block = block_from_ptr(ptr);
  1001. tlsf_assert(!block_is_free(block) && "block already marked as free");
  1002. block_mark_as_free(block);
  1003. block = block_merge_prev(control, block);
  1004. block = block_merge_next(control, block);
  1005. block_insert(control, block);
  1006. }
  1007. }
  1008. /*
  1009. ** The TLSF block information provides us with enough information to
  1010. ** provide a reasonably intelligent implementation of realloc, growing or
  1011. ** shrinking the currently allocated block as required.
  1012. **
  1013. ** This routine handles the somewhat esoteric edge cases of realloc:
  1014. ** - a non-zero size with a null pointer will behave like malloc
  1015. ** - a zero size with a non-null pointer will behave like free
  1016. ** - a request that cannot be satisfied will leave the original buffer
  1017. ** untouched
  1018. ** - an extended buffer size will leave the newly-allocated area with
  1019. ** contents undefined
  1020. */
  1021. void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
  1022. {
  1023. control_t* control = tlsf_cast(control_t*, tlsf);
  1024. void* p = 0;
  1025. /* Zero-size requests are treated as free. */
  1026. if (ptr && size == 0)
  1027. {
  1028. tlsf_free(tlsf, ptr);
  1029. }
  1030. /* Requests with NULL pointers are treated as malloc. */
  1031. else if (!ptr)
  1032. {
  1033. p = tlsf_malloc(tlsf, size);
  1034. }
  1035. else
  1036. {
  1037. block_header_t* block = block_from_ptr(ptr);
  1038. block_header_t* next = block_next(block);
  1039. const size_t cursize = block_size(block);
  1040. const size_t combined = cursize + block_size(next) + block_header_overhead;
  1041. const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
  1042. tlsf_assert(!block_is_free(block) && "block already marked as free");
  1043. /*
  1044. ** If the next block is used, or when combined with the current
  1045. ** block, does not offer enough space, we must reallocate and copy.
  1046. */
  1047. if (adjust > cursize && (!block_is_free(next) || adjust > combined))
  1048. {
  1049. p = tlsf_malloc(tlsf, size);
  1050. if (p)
  1051. {
  1052. const size_t minsize = tlsf_min(cursize, size);
  1053. memcpy(p, ptr, minsize);
  1054. tlsf_free(tlsf, ptr);
  1055. }
  1056. }
  1057. else
  1058. {
  1059. /* Do we need to expand to the next block? */
  1060. if (adjust > cursize)
  1061. {
  1062. block_merge_next(control, block);
  1063. block_mark_as_used(block);
  1064. }
  1065. /* Trim the resulting block and return the original pointer. */
  1066. block_trim_used(control, block, adjust);
  1067. p = ptr;
  1068. }
  1069. }
  1070. return p;
  1071. }
  1072. //=------------------------------------------------
  1073. // more function for usage data
  1074. typedef struct walker_ctx
  1075. {
  1076. size_t total;
  1077. size_t used;
  1078. }walker_ctx_t;
  1079. static void stat_walker(void* ptr, size_t size, int used, void* user) {
  1080. walker_ctx_t *ctx = (walker_ctx_t*)user;
  1081. ctx->total = ctx->total + size;
  1082. if (used != 0)
  1083. ctx->used = ctx->used + size;
  1084. }
  1085. int tlsf_stat(pool_t pool, size_t *total, size_t *used, size_t *maxused) {
  1086. walker_ctx_t ctx = {
  1087. .total = *total,
  1088. .used = *used
  1089. };
  1090. tlsf_walk_pool(pool, stat_walker, &ctx);
  1091. *total = ctx.total;
  1092. *used = ctx.used;
  1093. *maxused = ctx.used;
  1094. return 0;
  1095. }