+#include <assert.h>\r
+#include <limits.h>\r
+#include <stddef.h>\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+\r
+#include "tlsf.h"\r
+\r
+#if defined(__cplusplus)\r
+#define tlsf_decl inline\r
+#else\r
+#define tlsf_decl static\r
+#endif\r
+\r
+/*\r
+** Architecture-specific bit manipulation routines.\r
+**\r
+** TLSF achieves O(1) cost for malloc and free operations by limiting\r
+** the search for a free block to a free list of guaranteed size\r
+** adequate to fulfill the request, combined with efficient free list\r
+** queries using bitmasks and architecture-specific bit-manipulation\r
+** routines.\r
+**\r
+** Most modern processors provide instructions to count leading zeroes\r
+** in a word, find the lowest and highest set bit, etc. These\r
+** specific implementations will be used when available, falling back\r
+** to a reasonably efficient generic implementation.\r
+**\r
+** NOTE: TLSF spec relies on ffs/fls returning value 0..31.\r
+** ffs/fls return 1-32 by default, returning 0 for error.\r
+*/\r
+\r
+/*\r
+** Detect whether or not we are building for a 32- or 64-bit (LP/LLP)\r
+** architecture. There is no reliable portable method at compile-time.\r
+*/\r
+#if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) \\r
+ || defined (_WIN64) || defined (__LP64__) || defined (__LLP64__)\r
+#define TLSF_64BIT\r
+#endif\r
+\r
+/*\r
+** gcc 3.4 and above have builtin support, specialized for architecture.\r
+** Some compilers masquerade as gcc; patchlevel test filters them out.\r
+*/\r
+#if defined (__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) \\r
+ && defined (__GNUC_PATCHLEVEL__)\r
+\r
+#if defined (__SNC__)\r
+/* SNC for Playstation 3. */\r
+\r
+tlsf_decl int tlsf_ffs(unsigned int word)\r
+{\r
+ const unsigned int reverse = word & (~word + 1);\r
+ const int bit = 32 - __builtin_clz(reverse);\r
+ return bit - 1;\r
+}\r
+\r
+#else\r
+\r
+tlsf_decl int tlsf_ffs(unsigned int word)\r
+{\r
+ return __builtin_ffs(word) - 1;\r
+}\r
+\r
+#endif\r
+\r
+tlsf_decl int tlsf_fls(unsigned int word)\r
+{\r
+ const int bit = word ? 32 - __builtin_clz(word) : 0;\r
+ return bit - 1;\r
+}\r
+\r
+#elif defined (_MSC_VER) && (_MSC_VER >= 1400) && (defined (_M_IX86) || defined (_M_X64))\r
+/* Microsoft Visual C++ support on x86/X64 architectures. */\r
+\r
+#include <intrin.h>\r
+\r
+#pragma intrinsic(_BitScanReverse)\r
+#pragma intrinsic(_BitScanForward)\r
+\r
+tlsf_decl int tlsf_fls(unsigned int word)\r
+{\r
+ unsigned long index;\r
+ return _BitScanReverse(&index, word) ? index : -1;\r
+}\r
+\r
+tlsf_decl int tlsf_ffs(unsigned int word)\r
+{\r
+ unsigned long index;\r
+ return _BitScanForward(&index, word) ? index : -1;\r
+}\r
+\r
+#elif defined (_MSC_VER) && defined (_M_PPC)\r
+/* Microsoft Visual C++ support on PowerPC architectures. */\r
+\r
+#include <ppcintrinsics.h>\r
+\r
+tlsf_decl int tlsf_fls(unsigned int word)\r
+{\r
+ const int bit = 32 - _CountLeadingZeros(word);\r
+ return bit - 1;\r
+}\r
+\r
+tlsf_decl int tlsf_ffs(unsigned int word)\r
+{\r
+ const unsigned int reverse = word & (~word + 1);\r
+ const int bit = 32 - _CountLeadingZeros(reverse);\r
+ return bit - 1;\r
+}\r
+\r
+#elif defined (__ARMCC_VERSION)\r
+/* RealView Compilation Tools for ARM */\r
+\r
+tlsf_decl int tlsf_ffs(unsigned int word)\r
+{\r
+ const unsigned int reverse = word & (~word + 1);\r
+ const int bit = 32 - __clz(reverse);\r
+ return bit - 1;\r
+}\r
+\r
+tlsf_decl int tlsf_fls(unsigned int word)\r
+{\r
+ const int bit = word ? 32 - __clz(word) : 0;\r
+ return bit - 1;\r
+}\r
+\r
+#elif defined (__ghs__)\r
+/* Green Hills support for PowerPC */\r
+\r
+#include <ppc_ghs.h>\r
+\r
+tlsf_decl int tlsf_ffs(unsigned int word)\r
+{\r
+ const unsigned int reverse = word & (~word + 1);\r
+ const int bit = 32 - __CLZ32(reverse);\r
+ return bit - 1;\r
+}\r
+\r
+tlsf_decl int tlsf_fls(unsigned int word)\r
+{\r
+ const int bit = word ? 32 - __CLZ32(word) : 0;\r
+ return bit - 1;\r
+}\r
+\r
+#else\r
+/* Fall back to generic implementation. */\r
+\r
+tlsf_decl int tlsf_fls_generic(unsigned int word)\r
+{\r
+ int bit = 32;\r
+\r
+ if (!word) bit -= 1;\r
+ if (!(word & 0xffff0000)) { word <<= 16; bit -= 16; }\r
+ if (!(word & 0xff000000)) { word <<= 8; bit -= 8; }\r
+ if (!(word & 0xf0000000)) { word <<= 4; bit -= 4; }\r
+ if (!(word & 0xc0000000)) { word <<= 2; bit -= 2; }\r
+ if (!(word & 0x80000000)) { word <<= 1; bit -= 1; }\r
+\r
+ return bit;\r
+}\r
+\r
+/* Implement ffs in terms of fls. */\r
+tlsf_decl int tlsf_ffs(unsigned int word)\r
+{\r
+ return tlsf_fls_generic(word & (~word + 1)) - 1;\r
+}\r
+\r
+tlsf_decl int tlsf_fls(unsigned int word)\r
+{\r
+ return tlsf_fls_generic(word) - 1;\r
+}\r
+\r
+#endif\r
+\r
+/* Possibly 64-bit version of tlsf_fls. */\r
+#if defined (TLSF_64BIT)\r
+tlsf_decl int tlsf_fls_sizet(size_t size)\r
+{\r
+ int high = (int)(size >> 32);\r
+ int bits = 0;\r
+ if (high)\r
+ {\r
+ bits = 32 + tlsf_fls(high);\r
+ }\r
+ else\r
+ {\r
+ bits = tlsf_fls((int)size & 0xffffffff);\r
+\r
+ }\r
+ return bits;\r
+}\r
+#else\r
+#define tlsf_fls_sizet tlsf_fls\r
+#endif\r
+\r
+#undef tlsf_decl\r
+\r
+/*\r
+** Constants.\r
+*/\r
+\r
+/* Public constants: may be modified. */\r
+enum tlsf_public\r
+{\r
+ /* log2 of number of linear subdivisions of block sizes. Larger\r
+ ** values require more memory in the control structure. Values of\r
+ ** 4 or 5 are typical.\r
+ */\r
+ SL_INDEX_COUNT_LOG2 = 5,\r
+};\r
+\r
+/* Private constants: do not modify. */\r
+enum tlsf_private\r
+{\r
+#if defined (TLSF_64BIT)\r
+ /* All allocation sizes and addresses are aligned to 8 bytes. */\r
+ ALIGN_SIZE_LOG2 = 3,\r
+#else\r
+ /* All allocation sizes and addresses are aligned to 4 bytes. */\r
+ ALIGN_SIZE_LOG2 = 2,\r
+#endif\r
+ ALIGN_SIZE = (1 << ALIGN_SIZE_LOG2),\r
+\r
+ /*\r
+ ** We support allocations of sizes up to (1 << FL_INDEX_MAX) bits.\r
+ ** However, because we linearly subdivide the second-level lists, and\r
+ ** our minimum size granularity is 4 bytes, it doesn't make sense to\r
+ ** create first-level lists for sizes smaller than SL_INDEX_COUNT * 4,\r
+ ** or (1 << (SL_INDEX_COUNT_LOG2 + 2)) bytes, as there we will be\r
+ ** trying to split size ranges into more slots than we have available.\r
+ ** Instead, we calculate the minimum threshold size, and place all\r
+ ** blocks below that size into the 0th first-level list.\r
+ */\r
+\r
+#if defined (TLSF_64BIT)\r
+ /*\r
+ ** TODO: We can increase this to support larger sizes, at the expense\r
+ ** of more overhead in the TLSF structure.\r
+ */\r
+ FL_INDEX_MAX = 32,\r
+#else\r
+ FL_INDEX_MAX = 30,\r
+#endif\r
+ SL_INDEX_COUNT = (1 << SL_INDEX_COUNT_LOG2),\r
+ FL_INDEX_SHIFT = (SL_INDEX_COUNT_LOG2 + ALIGN_SIZE_LOG2),\r
+ FL_INDEX_COUNT = (FL_INDEX_MAX - FL_INDEX_SHIFT + 1),\r
+\r
+ SMALL_BLOCK_SIZE = (1 << FL_INDEX_SHIFT),\r
+};\r
+\r
+/*\r
+** Cast and min/max macros.\r
+*/\r
+\r
+#define tlsf_cast(t, exp) ((t) (exp))\r
+#define tlsf_min(a, b) ((a) < (b) ? (a) : (b))\r
+#define tlsf_max(a, b) ((a) > (b) ? (a) : (b))\r
+\r
+/*\r
+** Set assert macro, if it has not been provided by the user.\r
+*/\r
+#if !defined (tlsf_assert)\r
+#define tlsf_assert assert\r
+#endif\r
+\r
+/*\r
+** Static assertion mechanism.\r
+*/\r
+\r
+#define _tlsf_glue2(x, y) x ## y\r
+#define _tlsf_glue(x, y) _tlsf_glue2(x, y)\r
+#define tlsf_static_assert(exp) \\r
+ typedef char _tlsf_glue(static_assert, __LINE__) [(exp) ? 1 : -1]\r
+\r
+/* This code has been tested on 32- and 64-bit (LP/LLP) architectures. */\r
+tlsf_static_assert(sizeof(int) * CHAR_BIT == 32);\r
+tlsf_static_assert(sizeof(size_t) * CHAR_BIT >= 32);\r
+tlsf_static_assert(sizeof(size_t) * CHAR_BIT <= 64);\r
+\r
+/* SL_INDEX_COUNT must be <= number of bits in sl_bitmap's storage type. */\r
+tlsf_static_assert(sizeof(unsigned int) * CHAR_BIT >= SL_INDEX_COUNT);\r
+\r
+/* Ensure we've properly tuned our sizes. */\r
+tlsf_static_assert(ALIGN_SIZE == SMALL_BLOCK_SIZE / SL_INDEX_COUNT);\r
+\r
+/*\r
+** Data structures and associated constants.\r
+*/\r
+\r
+/*\r
+** Block header structure.\r
+**\r
+** There are several implementation subtleties involved:\r
+** - The prev_phys_block field is only valid if the previous block is free.\r
+** - The prev_phys_block field is actually stored at the end of the\r
+** previous block. It appears at the beginning of this structure only to\r
+** simplify the implementation.\r
+** - The next_free / prev_free fields are only valid if the block is free.\r
+*/\r
+typedef struct block_header_t\r
+{\r
+ /* Points to the previous physical block. */\r
+ struct block_header_t* prev_phys_block;\r
+\r
+ /* The size of this block, excluding the block header. */\r
+ size_t size;\r
+\r
+ /* Next and previous free blocks. */\r
+ struct block_header_t* next_free;\r
+ struct block_header_t* prev_free;\r
+} block_header_t;\r
+\r
+/*\r
+** Since block sizes are always at least a multiple of 4, the two least\r
+** significant bits of the size field are used to store the block status:\r
+** - bit 0: whether block is busy or free\r
+** - bit 1: whether previous block is busy or free\r
+*/\r
+static const size_t block_header_free_bit = 1 << 0;\r
+static const size_t block_header_prev_free_bit = 1 << 1;\r
+\r
+/*\r
+** The size of the block header exposed to used blocks is the size field.\r
+** The prev_phys_block field is stored *inside* the previous free block.\r
+*/\r
+static const size_t block_header_overhead = sizeof(size_t);\r
+\r
+/* User data starts directly after the size field in a used block. */\r
+static const size_t block_start_offset =\r
+ offsetof(block_header_t, size) + sizeof(size_t);\r
+\r
+/*\r
+** A free block must be large enough to store its header minus the size of\r
+** the prev_phys_block field, and no larger than the number of addressable\r
+** bits for FL_INDEX.\r
+*/\r
+static const size_t block_size_min = \r
+ sizeof(block_header_t) - sizeof(block_header_t*);\r
+static const size_t block_size_max = tlsf_cast(size_t, 1) << FL_INDEX_MAX;\r
+\r
+\r
+/* The TLSF control structure. */\r
+typedef struct control_t\r
+{\r
+ /* Empty lists point at this block to indicate they are free. */\r
+ block_header_t block_null;\r
+\r
+ /* Bitmaps for free lists. */\r
+ unsigned int fl_bitmap;\r
+ unsigned int sl_bitmap[FL_INDEX_COUNT];\r
+\r
+ /* Head of free lists. */\r
+ block_header_t* blocks[FL_INDEX_COUNT][SL_INDEX_COUNT];\r
+} control_t;\r
+\r
+/* A type used for casting when doing pointer arithmetic. */\r
+typedef ptrdiff_t tlsfptr_t;\r
+\r
+/*\r
+** block_header_t member functions.\r
+*/\r
+\r
+static size_t block_size(const block_header_t* block)\r
+{\r
+ return block->size & ~(block_header_free_bit | block_header_prev_free_bit);\r
+}\r
+\r
+static void block_set_size(block_header_t* block, size_t size)\r
+{\r
+ const size_t oldsize = block->size;\r
+ block->size = size | (oldsize & (block_header_free_bit | block_header_prev_free_bit));\r
+}\r
+\r
+static int block_is_last(const block_header_t* block)\r
+{\r
+ return block_size(block) == 0;\r
+}\r
+\r
+static int block_is_free(const block_header_t* block)\r
+{\r
+ return tlsf_cast(int, block->size & block_header_free_bit);\r
+}\r
+\r
+static void block_set_free(block_header_t* block)\r
+{\r
+ block->size |= block_header_free_bit;\r
+}\r
+\r
+static void block_set_used(block_header_t* block)\r
+{\r
+ block->size &= ~block_header_free_bit;\r
+}\r
+\r
+static int block_is_prev_free(const block_header_t* block)\r
+{\r
+ return tlsf_cast(int, block->size & block_header_prev_free_bit);\r
+}\r
+\r
+static void block_set_prev_free(block_header_t* block)\r
+{\r
+ block->size |= block_header_prev_free_bit;\r
+}\r
+\r
+static void block_set_prev_used(block_header_t* block)\r
+{\r
+ block->size &= ~block_header_prev_free_bit;\r
+}\r
+\r
+static block_header_t* block_from_ptr(const void* ptr)\r
+{\r
+ return tlsf_cast(block_header_t*,\r
+ tlsf_cast(unsigned char*, ptr) - block_start_offset);\r
+}\r
+\r
+static void* block_to_ptr(const block_header_t* block)\r
+{\r
+ return tlsf_cast(void*,\r
+ tlsf_cast(unsigned char*, block) + block_start_offset);\r
+}\r
+\r
+/* Return location of next block after block of given size. */\r
+static block_header_t* offset_to_block(const void* ptr, size_t size)\r
+{\r
+ return tlsf_cast(block_header_t*, tlsf_cast(tlsfptr_t, ptr) + size);\r
+}\r
+\r
+/* Return location of previous block. */\r
+static block_header_t* block_prev(const block_header_t* block)\r
+{\r
+ tlsf_assert(block_is_prev_free(block) && "previous block must be free");\r
+ return block->prev_phys_block;\r
+}\r
+\r
+/* Return location of next existing block. */\r
+static block_header_t* block_next(const block_header_t* block)\r
+{\r
+ block_header_t* next = offset_to_block(block_to_ptr(block),\r
+ block_size(block) - block_header_overhead);\r
+ tlsf_assert(!block_is_last(block));\r
+ return next;\r
+}\r
+\r
+/* Link a new block with its physical neighbor, return the neighbor. */\r
+static block_header_t* block_link_next(block_header_t* block)\r
+{\r
+ block_header_t* next = block_next(block);\r
+ next->prev_phys_block = block;\r
+ return next;\r
+}\r
+\r
+static void block_mark_as_free(block_header_t* block)\r
+{\r
+ /* Link the block to the next block, first. */\r
+ block_header_t* next = block_link_next(block);\r
+ block_set_prev_free(next);\r
+ block_set_free(block);\r
+}\r
+\r
+static void block_mark_as_used(block_header_t* block)\r
+{\r
+ block_header_t* next = block_next(block);\r
+ block_set_prev_used(next);\r
+ block_set_used(block);\r
+}\r
+\r
+static size_t align_up(size_t x, size_t align)\r
+{\r
+ tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two");\r
+ return (x + (align - 1)) & ~(align - 1);\r
+}\r
+\r
+static size_t align_down(size_t x, size_t align)\r
+{\r
+ tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two");\r
+ return x - (x & (align - 1));\r
+}\r
+\r
+static void* align_ptr(const void* ptr, size_t align)\r
+{\r
+ const tlsfptr_t aligned =\r
+ (tlsf_cast(tlsfptr_t, ptr) + (align - 1)) & ~(align - 1);\r
+ tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two");\r
+ return tlsf_cast(void*, aligned);\r
+}\r
+\r
+/*\r
+** Adjust an allocation size to be aligned to word size, and no smaller\r
+** than internal minimum.\r
+*/\r
+static size_t adjust_request_size(size_t size, size_t align)\r
+{\r
+ size_t adjust = 0;\r
+ if (size)\r
+ {\r
+ const size_t aligned = align_up(size, align);\r
+\r
+ /* aligned sized must not exceed block_size_max or we'll go out of bounds on sl_bitmap */\r
+ if (aligned < block_size_max) \r
+ {\r
+ adjust = tlsf_max(aligned, block_size_min);\r
+ }\r
+ }\r
+ return adjust;\r
+}\r
+\r
+/*\r
+** TLSF utility functions. In most cases, these are direct translations of\r
+** the documentation found in the white paper.\r
+*/\r
+\r
+static void mapping_insert(size_t size, int* fli, int* sli)\r
+{\r
+ int fl, sl;\r
+ if (size < SMALL_BLOCK_SIZE)\r
+ {\r
+ /* Store small blocks in first list. */\r
+ fl = 0;\r
+ sl = tlsf_cast(int, size) / (SMALL_BLOCK_SIZE / SL_INDEX_COUNT);\r
+ }\r
+ else\r
+ {\r
+ fl = tlsf_fls_sizet(size);\r
+ sl = tlsf_cast(int, size >> (fl - SL_INDEX_COUNT_LOG2)) ^ (1 << SL_INDEX_COUNT_LOG2);\r
+ fl -= (FL_INDEX_SHIFT - 1);\r
+ }\r
+ *fli = fl;\r
+ *sli = sl;\r
+}\r
+\r
+/* This version rounds up to the next block size (for allocations) */\r
+static void mapping_search(size_t size, int* fli, int* sli)\r
+{\r
+ if (size >= SMALL_BLOCK_SIZE)\r
+ {\r
+ const size_t round = (1 << (tlsf_fls_sizet(size) - SL_INDEX_COUNT_LOG2)) - 1;\r
+ size += round;\r
+ }\r
+ mapping_insert(size, fli, sli);\r
+}\r
+\r
+static block_header_t* search_suitable_block(control_t* control, int* fli, int* sli)\r
+{\r
+ int fl = *fli;\r
+ int sl = *sli;\r
+\r
+ /*\r
+ ** First, search for a block in the list associated with the given\r
+ ** fl/sl index.\r
+ */\r
+ unsigned int sl_map = control->sl_bitmap[fl] & (~0U << sl);\r
+ if (!sl_map)\r
+ {\r
+ /* No block exists. Search in the next largest first-level list. */\r
+ const unsigned int fl_map = control->fl_bitmap & (~0U << (fl + 1));\r
+ if (!fl_map)\r
+ {\r
+ /* No free blocks available, memory has been exhausted. */\r
+ return 0;\r
+ }\r
+\r
+ fl = tlsf_ffs(fl_map);\r
+ *fli = fl;\r
+ sl_map = control->sl_bitmap[fl];\r
+ }\r
+ tlsf_assert(sl_map && "internal error - second level bitmap is null");\r
+ sl = tlsf_ffs(sl_map);\r
+ *sli = sl;\r
+\r
+ /* Return the first block in the free list. */\r
+ return control->blocks[fl][sl];\r
+}\r
+\r
+/* Remove a free block from the free list.*/\r
+static void remove_free_block(control_t* control, block_header_t* block, int fl, int sl)\r
+{\r
+ block_header_t* prev = block->prev_free;\r
+ block_header_t* next = block->next_free;\r
+ tlsf_assert(prev && "prev_free field can not be null");\r
+ tlsf_assert(next && "next_free field can not be null");\r
+ next->prev_free = prev;\r
+ prev->next_free = next;\r
+\r
+ /* If this block is the head of the free list, set new head. */\r
+ if (control->blocks[fl][sl] == block)\r
+ {\r
+ control->blocks[fl][sl] = next;\r
+\r
+ /* If the new head is null, clear the bitmap. */\r
+ if (next == &control->block_null)\r
+ {\r
+ control->sl_bitmap[fl] &= ~(1U << sl);\r
+\r
+ /* If the second bitmap is now empty, clear the fl bitmap. */\r
+ if (!control->sl_bitmap[fl])\r
+ {\r
+ control->fl_bitmap &= ~(1U << fl);\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+/* Insert a free block into the free block list. */\r
+static void insert_free_block(control_t* control, block_header_t* block, int fl, int sl)\r
+{\r
+ block_header_t* current = control->blocks[fl][sl];\r
+ tlsf_assert(current && "free list cannot have a null entry");\r
+ tlsf_assert(block && "cannot insert a null entry into the free list");\r
+ block->next_free = current;\r
+ block->prev_free = &control->block_null;\r
+ current->prev_free = block;\r
+\r
+ tlsf_assert(block_to_ptr(block) == align_ptr(block_to_ptr(block), ALIGN_SIZE)\r
+ && "block not aligned properly");\r
+ /*\r
+ ** Insert the new block at the head of the list, and mark the first-\r
+ ** and second-level bitmaps appropriately.\r
+ */\r
+ control->blocks[fl][sl] = block;\r
+ control->fl_bitmap |= (1U << fl);\r
+ control->sl_bitmap[fl] |= (1U << sl);\r
+}\r
+\r
+/* Remove a given block from the free list. */\r
+static void block_remove(control_t* control, block_header_t* block)\r
+{\r
+ int fl, sl;\r
+ mapping_insert(block_size(block), &fl, &sl);\r
+ remove_free_block(control, block, fl, sl);\r
+}\r
+\r
+/* Insert a given block into the free list. */\r
+static void block_insert(control_t* control, block_header_t* block)\r
+{\r
+ int fl, sl;\r
+ mapping_insert(block_size(block), &fl, &sl);\r
+ insert_free_block(control, block, fl, sl);\r
+}\r
+\r
+static int block_can_split(block_header_t* block, size_t size)\r
+{\r
+ return block_size(block) >= sizeof(block_header_t) + size;\r
+}\r
+\r
+/* Split a block into two, the second of which is free. */\r
+static block_header_t* block_split(block_header_t* block, size_t size)\r
+{\r
+ /* Calculate the amount of space left in the remaining block. */\r
+ block_header_t* remaining =\r
+ offset_to_block(block_to_ptr(block), size - block_header_overhead);\r
+\r
+ const size_t remain_size = block_size(block) - (size + block_header_overhead);\r
+\r
+ tlsf_assert(block_to_ptr(remaining) == align_ptr(block_to_ptr(remaining), ALIGN_SIZE)\r
+ && "remaining block not aligned properly");\r
+\r
+ tlsf_assert(block_size(block) == remain_size + size + block_header_overhead);\r
+ block_set_size(remaining, remain_size);\r
+ tlsf_assert(block_size(remaining) >= block_size_min && "block split with invalid size");\r
+\r
+ block_set_size(block, size);\r
+ block_mark_as_free(remaining);\r
+\r
+ return remaining;\r
+}\r
+\r
+/* Absorb a free block's storage into an adjacent previous free block. */\r
+static block_header_t* block_absorb(block_header_t* prev, block_header_t* block)\r
+{\r
+ tlsf_assert(!block_is_last(prev) && "previous block can't be last");\r
+ /* Note: Leaves flags untouched. */\r
+ prev->size += block_size(block) + block_header_overhead;\r
+ block_link_next(prev);\r
+ return prev;\r
+}\r
+\r
+/* Merge a just-freed block with an adjacent previous free block. */\r
+static block_header_t* block_merge_prev(control_t* control, block_header_t* block)\r
+{\r
+ if (block_is_prev_free(block))\r
+ {\r
+ block_header_t* prev = block_prev(block);\r
+ tlsf_assert(prev && "prev physical block can't be null");\r
+ tlsf_assert(block_is_free(prev) && "prev block is not free though marked as such");\r
+ block_remove(control, prev);\r
+ block = block_absorb(prev, block);\r
+ }\r
+\r
+ return block;\r
+}\r
+\r
+/* Merge a just-freed block with an adjacent free block. */\r
+static block_header_t* block_merge_next(control_t* control, block_header_t* block)\r
+{\r
+ block_header_t* next = block_next(block);\r
+ tlsf_assert(next && "next physical block can't be null");\r
+\r
+ if (block_is_free(next))\r
+ {\r
+ tlsf_assert(!block_is_last(block) && "previous block can't be last");\r
+ block_remove(control, next);\r
+ block = block_absorb(block, next);\r
+ }\r
+\r
+ return block;\r
+}\r
+\r
+/* Trim any trailing block space off the end of a block, return to pool. */\r
+static void block_trim_free(control_t* control, block_header_t* block, size_t size)\r
+{\r
+ tlsf_assert(block_is_free(block) && "block must be free");\r
+ if (block_can_split(block, size))\r
+ {\r
+ block_header_t* remaining_block = block_split(block, size);\r
+ block_link_next(block);\r
+ block_set_prev_free(remaining_block);\r
+ block_insert(control, remaining_block);\r
+ }\r
+}\r
+\r
+/* Trim any trailing block space off the end of a used block, return to pool. */\r
+static void block_trim_used(control_t* control, block_header_t* block, size_t size)\r
+{\r
+ tlsf_assert(!block_is_free(block) && "block must be used");\r
+ if (block_can_split(block, size))\r
+ {\r
+ /* If the next block is free, we must coalesce. */\r
+ block_header_t* remaining_block = block_split(block, size);\r
+ block_set_prev_used(remaining_block);\r
+\r
+ remaining_block = block_merge_next(control, remaining_block);\r
+ block_insert(control, remaining_block);\r
+ }\r
+}\r
+\r
+static block_header_t* block_trim_free_leading(control_t* control, block_header_t* block, size_t size)\r
+{\r
+ block_header_t* remaining_block = block;\r
+ if (block_can_split(block, size))\r
+ {\r
+ /* We want the 2nd block. */\r
+ remaining_block = block_split(block, size - block_header_overhead);\r
+ block_set_prev_free(remaining_block);\r
+\r
+ block_link_next(block);\r
+ block_insert(control, block);\r
+ }\r
+\r
+ return remaining_block;\r
+}\r
+\r
+static block_header_t* block_locate_free(control_t* control, size_t size)\r
+{\r
+ int fl = 0, sl = 0;\r
+ block_header_t* block = 0;\r
+\r
+ if (size)\r
+ {\r
+ mapping_search(size, &fl, &sl);\r
+ \r
+ /*\r
+ ** mapping_search can futz with the size, so for excessively large sizes it can sometimes wind up \r
+ ** with indices that are off the end of the block array.\r
+ ** So, we protect against that here, since this is the only callsite of mapping_search.\r
+ ** Note that we don't need to check sl, since it comes from a modulo operation that guarantees it's always in range.\r
+ */\r
+ if (fl < FL_INDEX_COUNT)\r
+ {\r
+ block = search_suitable_block(control, &fl, &sl);\r
+ }\r
+ }\r
+\r
+ if (block)\r
+ {\r
+ tlsf_assert(block_size(block) >= size);\r
+ remove_free_block(control, block, fl, sl);\r
+ }\r
+\r
+ return block;\r
+}\r
+\r
+static void* block_prepare_used(control_t* control, block_header_t* block, size_t size)\r
+{\r
+ void* p = 0;\r
+ if (block)\r
+ {\r
+ tlsf_assert(size && "size must be non-zero");\r
+ block_trim_free(control, block, size);\r
+ block_mark_as_used(block);\r
+ p = block_to_ptr(block);\r
+ }\r
+ return p;\r
+}\r
+\r
+/* Clear structure and point all empty lists at the null block. */\r
+static void control_construct(control_t* control)\r
+{\r
+ int i, j;\r
+\r
+ control->block_null.next_free = &control->block_null;\r
+ control->block_null.prev_free = &control->block_null;\r
+\r
+ control->fl_bitmap = 0;\r
+ for (i = 0; i < FL_INDEX_COUNT; ++i)\r
+ {\r
+ control->sl_bitmap[i] = 0;\r
+ for (j = 0; j < SL_INDEX_COUNT; ++j)\r
+ {\r
+ control->blocks[i][j] = &control->block_null;\r
+ }\r
+ }\r
+}\r
+\r
+/*\r
+** Debugging utilities.\r
+*/\r
+\r
+typedef struct integrity_t\r
+{\r
+ int prev_status;\r
+ int status;\r
+} integrity_t;\r
+\r
+#define tlsf_insist(x) { tlsf_assert(x); if (!(x)) { status--; } }\r
+\r
+static void integrity_walker(void* ptr, size_t size, int used, void* user)\r
+{\r
+ block_header_t* block = block_from_ptr(ptr);\r
+ integrity_t* integ = tlsf_cast(integrity_t*, user);\r
+ const int this_prev_status = block_is_prev_free(block) ? 1 : 0;\r
+ const int this_status = block_is_free(block) ? 1 : 0;\r
+ const size_t this_block_size = block_size(block);\r
+\r
+ int status = 0;\r
+ (void)used;\r
+ tlsf_insist(integ->prev_status == this_prev_status && "prev status incorrect");\r
+ tlsf_insist(size == this_block_size && "block size incorrect");\r
+\r
+ integ->prev_status = this_status;\r
+ integ->status += status;\r
+}\r
+\r
+int tlsf_check(tlsf_t tlsf)\r
+{\r
+ int i, j;\r
+\r
+ control_t* control = tlsf_cast(control_t*, tlsf);\r
+ int status = 0;\r
+\r
+ /* Check that the free lists and bitmaps are accurate. */\r
+ for (i = 0; i < FL_INDEX_COUNT; ++i)\r
+ {\r
+ for (j = 0; j < SL_INDEX_COUNT; ++j)\r
+ {\r
+ const int fl_map = control->fl_bitmap & (1U << i);\r
+ const int sl_list = control->sl_bitmap[i];\r
+ const int sl_map = sl_list & (1U << j);\r
+ const block_header_t* block = control->blocks[i][j];\r
+\r
+ /* Check that first- and second-level lists agree. */\r
+ if (!fl_map)\r
+ {\r
+ tlsf_insist(!sl_map && "second-level map must be null");\r
+ }\r
+\r
+ if (!sl_map)\r
+ {\r
+ tlsf_insist(block == &control->block_null && "block list must be null");\r
+ continue;\r
+ }\r
+\r
+ /* Check that there is at least one free block. */\r
+ tlsf_insist(sl_list && "no free blocks in second-level map");\r
+ tlsf_insist(block != &control->block_null && "block should not be null");\r
+\r
+ while (block != &control->block_null)\r
+ {\r
+ int fli, sli;\r
+ tlsf_insist(block_is_free(block) && "block should be free");\r
+ tlsf_insist(!block_is_prev_free(block) && "blocks should have coalesced");\r
+ tlsf_insist(!block_is_free(block_next(block)) && "blocks should have coalesced");\r
+ tlsf_insist(block_is_prev_free(block_next(block)) && "block should be free");\r
+ tlsf_insist(block_size(block) >= block_size_min && "block not minimum size");\r
+\r
+ mapping_insert(block_size(block), &fli, &sli);\r
+ tlsf_insist(fli == i && sli == j && "block size indexed in wrong list");\r
+ block = block->next_free;\r
+ }\r
+ }\r
+ }\r
+\r
+ return status;\r
+}\r
+\r
+#undef tlsf_insist\r
+\r
+static void default_walker(void* ptr, size_t size, int used, void* user)\r
+{\r
+ (void)user;\r
+ printf("\t%p %s size: %x (%p)\n", ptr, used ? "used" : "free", (unsigned int)size, block_from_ptr(ptr));\r
+}\r
+\r
+void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user)\r
+{\r
+ tlsf_walker pool_walker = walker ? walker : default_walker;\r
+ block_header_t* block =\r
+ offset_to_block(pool, -(int)block_header_overhead);\r
+\r
+ while (block && !block_is_last(block))\r
+ {\r
+ pool_walker(\r
+ block_to_ptr(block),\r
+ block_size(block),\r
+ !block_is_free(block),\r
+ user);\r
+ block = block_next(block);\r
+ }\r
+}\r
+\r
+size_t tlsf_block_size(void* ptr)\r
+{\r
+ size_t size = 0;\r
+ if (ptr)\r
+ {\r
+ const block_header_t* block = block_from_ptr(ptr);\r
+ size = block_size(block);\r
+ }\r
+ return size;\r
+}\r
+\r
+int tlsf_check_pool(pool_t pool)\r
+{\r
+ /* Check that the blocks are physically correct. */\r
+ integrity_t integ = { 0, 0 };\r
+ tlsf_walk_pool(pool, integrity_walker, &integ);\r
+\r
+ return integ.status;\r
+}\r
+\r
+/*\r
+** Size of the TLSF structures in a given memory block passed to\r
+** tlsf_create, equal to the size of a control_t\r
+*/\r
+size_t tlsf_size(void)\r
+{\r
+ return sizeof(control_t);\r
+}\r
+\r
+size_t tlsf_align_size(void)\r
+{\r
+ return ALIGN_SIZE;\r
+}\r
+\r
+size_t tlsf_block_size_min(void)\r
+{\r
+ return block_size_min;\r
+}\r
+\r
+size_t tlsf_block_size_max(void)\r
+{\r
+ return block_size_max;\r
+}\r
+\r
+/*\r
+** Overhead of the TLSF structures in a given memory block passed to\r
+** tlsf_add_pool, equal to the overhead of a free block and the\r
+** sentinel block.\r
+*/\r
+size_t tlsf_pool_overhead(void)\r
+{\r
+ return 2 * block_header_overhead;\r
+}\r
+\r
+size_t tlsf_alloc_overhead(void)\r
+{\r
+ return block_header_overhead;\r
+}\r
+\r
+pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)\r
+{\r
+ block_header_t* block;\r
+ block_header_t* next;\r
+\r
+ const size_t pool_overhead = tlsf_pool_overhead();\r
+ const size_t pool_bytes = align_down(bytes - pool_overhead, ALIGN_SIZE);\r
+\r
+ if (((ptrdiff_t)mem % ALIGN_SIZE) != 0)\r
+ {\r
+ printf("tlsf_add_pool: Memory must be aligned by %u bytes.\n",\r
+ (unsigned int)ALIGN_SIZE);\r
+ return 0;\r
+ }\r
+\r
+ if (pool_bytes < block_size_min || pool_bytes > block_size_max)\r
+ {\r
+#if defined (TLSF_64BIT)\r
+ printf("tlsf_add_pool: Memory size must be between 0x%x and 0x%x00 bytes.\n", \r
+ (unsigned int)(pool_overhead + block_size_min),\r
+ (unsigned int)((pool_overhead + block_size_max) / 256));\r
+#else\r
+ printf("tlsf_add_pool: Memory size must be between %u and %u bytes.\n", \r
+ (unsigned int)(pool_overhead + block_size_min),\r
+ (unsigned int)(pool_overhead + block_size_max));\r
+#endif\r
+ return 0;\r
+ }\r
+\r
+ /*\r
+ ** Create the main free block. Offset the start of the block slightly\r
+ ** so that the prev_phys_block field falls outside of the pool -\r
+ ** it will never be used.\r
+ */\r
+ block = offset_to_block(mem, -(tlsfptr_t)block_header_overhead);\r
+ block_set_size(block, pool_bytes);\r
+ block_set_free(block);\r
+ block_set_prev_used(block);\r
+ block_insert(tlsf_cast(control_t*, tlsf), block);\r
+\r
+ /* Split the block to create a zero-size sentinel block. */\r
+ next = block_link_next(block);\r
+ block_set_size(next, 0);\r
+ block_set_used(next);\r
+ block_set_prev_free(next);\r
+\r
+ return mem;\r
+}\r
+\r
+void tlsf_remove_pool(tlsf_t tlsf, pool_t pool)\r
+{\r
+ control_t* control = tlsf_cast(control_t*, tlsf);\r
+ block_header_t* block = offset_to_block(pool, -(int)block_header_overhead);\r
+\r
+ int fl = 0, sl = 0;\r
+\r
+ tlsf_assert(block_is_free(block) && "block should be free");\r
+ tlsf_assert(!block_is_free(block_next(block)) && "next block should not be free");\r
+ tlsf_assert(block_size(block_next(block)) == 0 && "next block size should be zero");\r
+\r
+ mapping_insert(block_size(block), &fl, &sl);\r
+ remove_free_block(control, block, fl, sl);\r
+}\r
+\r
+/*\r
+** TLSF main interface.\r
+*/\r
+\r
+#if _DEBUG\r
+int test_ffs_fls()\r
+{\r
+ /* Verify ffs/fls work properly. */\r
+ int rv = 0;\r
+ rv += (tlsf_ffs(0) == -1) ? 0 : 0x1;\r
+ rv += (tlsf_fls(0) == -1) ? 0 : 0x2;\r
+ rv += (tlsf_ffs(1) == 0) ? 0 : 0x4;\r
+ rv += (tlsf_fls(1) == 0) ? 0 : 0x8;\r
+ rv += (tlsf_ffs(0x80000000) == 31) ? 0 : 0x10;\r
+ rv += (tlsf_ffs(0x80008000) == 15) ? 0 : 0x20;\r
+ rv += (tlsf_fls(0x80000008) == 31) ? 0 : 0x40;\r
+ rv += (tlsf_fls(0x7FFFFFFF) == 30) ? 0 : 0x80;\r
+\r
+#if defined (TLSF_64BIT)\r
+ rv += (tlsf_fls_sizet(0x80000000) == 31) ? 0 : 0x100;\r
+ rv += (tlsf_fls_sizet(0x100000000) == 32) ? 0 : 0x200;\r
+ rv += (tlsf_fls_sizet(0xffffffffffffffff) == 63) ? 0 : 0x400;\r
+#endif\r
+\r
+ if (rv)\r
+ {\r
+ printf("test_ffs_fls: %x ffs/fls tests failed.\n", rv);\r
+ }\r
+ return rv;\r
+}\r
+#endif\r
+\r
+tlsf_t tlsf_create(void* mem)\r
+{\r
+#if _DEBUG\r
+ if (test_ffs_fls())\r
+ {\r
+ return 0;\r
+ }\r
+#endif\r
+\r
+ if (((tlsfptr_t)mem % ALIGN_SIZE) != 0)\r
+ {\r
+ printf("tlsf_create: Memory must be aligned to %u bytes.\n",\r
+ (unsigned int)ALIGN_SIZE);\r
+ return 0;\r
+ }\r
+\r
+ control_construct(tlsf_cast(control_t*, mem));\r
+\r
+ return tlsf_cast(tlsf_t, mem);\r
+}\r
+\r
+tlsf_t tlsf_create_with_pool(void* mem, size_t bytes)\r
+{\r
+ tlsf_t tlsf = tlsf_create(mem);\r
+ tlsf_add_pool(tlsf, (char*)mem + tlsf_size(), bytes - tlsf_size());\r
+ return tlsf;\r
+}\r
+\r
+void tlsf_destroy(tlsf_t tlsf)\r
+{\r
+ /* Nothing to do. */\r
+ (void)tlsf;\r
+}\r
+\r
+pool_t tlsf_get_pool(tlsf_t tlsf)\r
+{\r
+ return tlsf_cast(pool_t, (char*)tlsf + tlsf_size());\r
+}\r
+\r
+void* tlsf_malloc(tlsf_t tlsf, size_t size)\r
+{\r
+ control_t* control = tlsf_cast(control_t*, tlsf);\r
+ const size_t adjust = adjust_request_size(size, ALIGN_SIZE);\r
+ block_header_t* block = block_locate_free(control, adjust);\r
+ return block_prepare_used(control, block, adjust);\r
+}\r
+\r
+void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size)\r
+{\r
+ control_t* control = tlsf_cast(control_t*, tlsf);\r
+ const size_t adjust = adjust_request_size(size, ALIGN_SIZE);\r
+\r
+ /*\r
+ ** We must allocate an additional minimum block size bytes so that if\r
+ ** our free block will leave an alignment gap which is smaller, we can\r
+ ** trim a leading free block and release it back to the pool. We must\r
+ ** do this because the previous physical block is in use, therefore\r
+ ** the prev_phys_block field is not valid, and we can't simply adjust\r
+ ** the size of that block.\r
+ */\r
+ const size_t gap_minimum = sizeof(block_header_t);\r
+ const size_t size_with_gap = adjust_request_size(adjust + align + gap_minimum, align);\r
+\r
+ /*\r
+ ** If alignment is less than or equals base alignment, we're done.\r
+ ** If we requested 0 bytes, return null, as tlsf_malloc(0) does.\r
+ */\r
+ const size_t aligned_size = (adjust && align > ALIGN_SIZE) ? size_with_gap : adjust;\r
+\r
+ block_header_t* block = block_locate_free(control, aligned_size);\r
+\r
+ /* This can't be a static assert. */\r
+ tlsf_assert(sizeof(block_header_t) == block_size_min + block_header_overhead);\r
+\r
+ if (block)\r
+ {\r
+ void* ptr = block_to_ptr(block);\r
+ void* aligned = align_ptr(ptr, align);\r
+ size_t gap = tlsf_cast(size_t,\r
+ tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr));\r
+\r
+ /* If gap size is too small, offset to next aligned boundary. */\r
+ if (gap && gap < gap_minimum)\r
+ {\r
+ const size_t gap_remain = gap_minimum - gap;\r
+ const size_t offset = tlsf_max(gap_remain, align);\r
+ const void* next_aligned = tlsf_cast(void*,\r
+ tlsf_cast(tlsfptr_t, aligned) + offset);\r
+\r
+ aligned = align_ptr(next_aligned, align);\r
+ gap = tlsf_cast(size_t,\r
+ tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr));\r
+ }\r
+\r
+ if (gap)\r
+ {\r
+ tlsf_assert(gap >= gap_minimum && "gap size too small");\r
+ block = block_trim_free_leading(control, block, gap);\r
+ }\r
+ }\r
+\r
+ return block_prepare_used(control, block, adjust);\r
+}\r
+\r
+void tlsf_free(tlsf_t tlsf, void* ptr)\r
+{\r
+ /* Don't attempt to free a NULL pointer. */\r
+ if (ptr)\r
+ {\r
+ control_t* control = tlsf_cast(control_t*, tlsf);\r
+ block_header_t* block = block_from_ptr(ptr);\r
+ tlsf_assert(!block_is_free(block) && "block already marked as free");\r
+ block_mark_as_free(block);\r
+ block = block_merge_prev(control, block);\r
+ block = block_merge_next(control, block);\r
+ block_insert(control, block);\r
+ }\r
+}\r
+\r
+/*\r
+** The TLSF block information provides us with enough information to\r
+** provide a reasonably intelligent implementation of realloc, growing or\r
+** shrinking the currently allocated block as required.\r
+**\r
+** This routine handles the somewhat esoteric edge cases of realloc:\r
+** - a non-zero size with a null pointer will behave like malloc\r
+** - a zero size with a non-null pointer will behave like free\r
+** - a request that cannot be satisfied will leave the original buffer\r
+** untouched\r
+** - an extended buffer size will leave the newly-allocated area with\r
+** contents undefined\r
+*/\r
+void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)\r
+{\r
+ control_t* control = tlsf_cast(control_t*, tlsf);\r
+ void* p = 0;\r
+\r
+ /* Zero-size requests are treated as free. */\r
+ if (ptr && size == 0)\r
+ {\r
+ tlsf_free(tlsf, ptr);\r
+ }\r
+ /* Requests with NULL pointers are treated as malloc. */\r
+ else if (!ptr)\r
+ {\r
+ p = tlsf_malloc(tlsf, size);\r
+ }\r
+ else\r
+ {\r
+ block_header_t* block = block_from_ptr(ptr);\r
+ block_header_t* next = block_next(block);\r
+\r
+ const size_t cursize = block_size(block);\r
+ const size_t combined = cursize + block_size(next) + block_header_overhead;\r
+ const size_t adjust = adjust_request_size(size, ALIGN_SIZE);\r
+\r
+ tlsf_assert(!block_is_free(block) && "block already marked as free");\r
+\r
+ /*\r
+ ** If the next block is used, or when combined with the current\r
+ ** block, does not offer enough space, we must reallocate and copy.\r
+ */\r
+ if (adjust > cursize && (!block_is_free(next) || adjust > combined))\r
+ {\r
+ p = tlsf_malloc(tlsf, size);\r
+ if (p)\r
+ {\r
+ const size_t minsize = tlsf_min(cursize, size);\r
+ memcpy(p, ptr, minsize);\r
+ tlsf_free(tlsf, ptr);\r
+ }\r
+ }\r
+ else\r
+ {\r
+ /* Do we need to expand to the next block? */\r
+ if (adjust > cursize)\r
+ {\r
+ block_merge_next(control, block);\r
+ block_mark_as_used(block);\r
+ }\r
+\r
+ /* Trim the resulting block and return the original pointer. */\r
+ block_trim_used(control, block, adjust);\r
+ p = ptr;\r
+ }\r
+ }\r
+\r
+ return p;\r
+}\r