diff --git a/libpsn00b/include/smd/smd.h b/libpsn00b/include/smd/smd.h index cf2920f9..658f7faf 100644 --- a/libpsn00b/include/smd/smd.h +++ b/libpsn00b/include/smd/smd.h @@ -43,17 +43,6 @@ typedef struct { uint8_t len; } SMD_PRI_TYPE; -#define SMD_PRI_TYPE_LINE 0 -#define SMD_PRI_TYPE_TRIANGLE 1 -#define SMD_PRI_TYPE_QUAD 2 - -#define SMD_PRI_TYPE_LIGHTING_NONE 0 // No shading (no normals) -#define SMD_PRI_TYPE_LIGHTING_FLAT 1 // Flat shading (1 normal) -#define SMD_PRI_TYPE_LIGHTING_SMOOTH 2 // Smooth shading (3 normals per vertex) - -#define SMD_PRI_TYPE_COLORING_SOLID 0 -#define SMD_PRI_TYPE_COLORING_GOURAUD 1 - typedef struct { SMD_PRI_TYPE prim_id; diff --git a/libpsn00b/libc/tlsf.c b/libpsn00b/libc/tlsf.c index 441eab54..579e81d2 100644 --- a/libpsn00b/libc/tlsf.c +++ b/libpsn00b/libc/tlsf.c @@ -132,8 +132,7 @@ int tlsf_check_pool(pool_t pool); #if defined (__SNC__) /* SNC for Playstation 3. */ -tlsf_decl int tlsf_ffs(unsigned int word) -{ +tlsf_decl int tlsf_ffs(unsigned int word) { const unsigned int reverse = word & (~word + 1); const int bit = 32 - __builtin_clz(reverse); return bit - 1; @@ -141,15 +140,13 @@ tlsf_decl int tlsf_ffs(unsigned int word) #else -tlsf_decl int tlsf_ffs(unsigned int word) -{ +tlsf_decl int tlsf_ffs(unsigned int word) { return __builtin_ffs(word) - 1; } #endif -tlsf_decl int tlsf_fls(unsigned int word) -{ +tlsf_decl int tlsf_fls(unsigned int word) { const int bit = word ? 32 - __builtin_clz(word) : 0; return bit - 1; } @@ -162,14 +159,12 @@ tlsf_decl int tlsf_fls(unsigned int word) #pragma intrinsic(_BitScanReverse) #pragma intrinsic(_BitScanForward) -tlsf_decl int tlsf_fls(unsigned int word) -{ +tlsf_decl int tlsf_fls(unsigned int word) { unsigned long index; return _BitScanReverse(&index, word) ? index : -1; } -tlsf_decl int tlsf_ffs(unsigned int word) -{ +tlsf_decl int tlsf_ffs(unsigned int word) { unsigned long index; return _BitScanForward(&index, word) ? index : -1; } @@ -179,14 +174,12 @@ tlsf_decl int tlsf_ffs(unsigned int word) #include -tlsf_decl int tlsf_fls(unsigned int word) -{ +tlsf_decl int tlsf_fls(unsigned int word) { const int bit = 32 - _CountLeadingZeros(word); return bit - 1; } -tlsf_decl int tlsf_ffs(unsigned int word) -{ +tlsf_decl int tlsf_ffs(unsigned int word) { const unsigned int reverse = word & (~word + 1); const int bit = 32 - _CountLeadingZeros(reverse); return bit - 1; @@ -195,15 +188,13 @@ tlsf_decl int tlsf_ffs(unsigned int word) #elif defined (__ARMCC_VERSION) /* RealView Compilation Tools for ARM */ -tlsf_decl int tlsf_ffs(unsigned int word) -{ +tlsf_decl int tlsf_ffs(unsigned int word) { const unsigned int reverse = word & (~word + 1); const int bit = 32 - __clz(reverse); return bit - 1; } -tlsf_decl int tlsf_fls(unsigned int word) -{ +tlsf_decl int tlsf_fls(unsigned int word) { const int bit = word ? 32 - __clz(word) : 0; return bit - 1; } @@ -213,15 +204,13 @@ tlsf_decl int tlsf_fls(unsigned int word) #include -tlsf_decl int tlsf_ffs(unsigned int word) -{ +tlsf_decl int tlsf_ffs(unsigned int word) { const unsigned int reverse = word & (~word + 1); const int bit = 32 - __CLZ32(reverse); return bit - 1; } -tlsf_decl int tlsf_fls(unsigned int word) -{ +tlsf_decl int tlsf_fls(unsigned int word) { const int bit = word ? 32 - __CLZ32(word) : 0; return bit - 1; } @@ -229,8 +218,7 @@ tlsf_decl int tlsf_fls(unsigned int word) #else /* Fall back to generic implementation. */ -tlsf_decl int tlsf_fls_generic(unsigned int word) -{ +tlsf_decl int tlsf_fls_generic(unsigned int word) { int bit = 32; if (!word) bit -= 1; @@ -244,13 +232,11 @@ tlsf_decl int tlsf_fls_generic(unsigned int word) } /* Implement ffs in terms of fls. */ -tlsf_decl int tlsf_ffs(unsigned int word) -{ +tlsf_decl int tlsf_ffs(unsigned int word) { return tlsf_fls_generic(word & (~word + 1)) - 1; } -tlsf_decl int tlsf_fls(unsigned int word) -{ +tlsf_decl int tlsf_fls(unsigned int word) { return tlsf_fls_generic(word) - 1; } @@ -258,16 +244,13 @@ tlsf_decl int tlsf_fls(unsigned int word) /* Possibly 64-bit version of tlsf_fls. */ #if defined (TLSF_64BIT) -tlsf_decl int tlsf_fls_sizet(size_t size) -{ +tlsf_decl int tlsf_fls_sizet(size_t size) { int high = (int)(size >> 32); int bits = 0; - if (high) - { + if (high) { bits = 32 + tlsf_fls(high); } - else - { + else { bits = tlsf_fls((int)size & 0xffffffff); } @@ -284,8 +267,7 @@ tlsf_decl int tlsf_fls_sizet(size_t size) */ /* Public constants: may be modified. */ -enum tlsf_public -{ +enum tlsf_public { /* log2 of number of linear subdivisions of block sizes. Larger ** values require more memory in the control structure. Values of ** 4 or 5 are typical. @@ -294,8 +276,7 @@ enum tlsf_public }; /* Private constants: do not modify. */ -enum tlsf_private -{ +enum tlsf_private { #if defined (TLSF_64BIT) /* All allocation sizes and addresses are aligned to 8 bytes. */ ALIGN_SIZE_LOG2 = 3, @@ -381,8 +362,7 @@ tlsf_static_assert(ALIGN_SIZE == SMALL_BLOCK_SIZE / SL_INDEX_COUNT); ** simplify the implementation. ** - The next_free / prev_free fields are only valid if the block is free. */ -typedef struct block_header_t -{ +typedef struct block_header_t { /* Points to the previous physical block. */ struct block_header_t* prev_phys_block; @@ -424,8 +404,7 @@ static const size_t block_size_max = tlsf_cast(size_t, 1) << FL_INDEX_MAX; /* The TLSF control structure. */ -typedef struct control_t -{ +typedef struct control_t { /* Empty lists point at this block to indicate they are free. */ block_header_t block_null; @@ -444,80 +423,66 @@ typedef ptrdiff_t tlsfptr_t; ** block_header_t member functions. */ -static size_t block_size(const block_header_t* block) -{ +static size_t block_size(const block_header_t* block) { return block->size & ~(block_header_free_bit | block_header_prev_free_bit); } -static void block_set_size(block_header_t* block, size_t size) -{ +static void block_set_size(block_header_t* block, size_t size) { const size_t oldsize = block->size; block->size = size | (oldsize & (block_header_free_bit | block_header_prev_free_bit)); } -static int block_is_last(const block_header_t* block) -{ +static int block_is_last(const block_header_t* block) { return block_size(block) == 0; } -static int block_is_free(const block_header_t* block) -{ +static int block_is_free(const block_header_t* block) { return tlsf_cast(int, block->size & block_header_free_bit); } -static void block_set_free(block_header_t* block) -{ +static void block_set_free(block_header_t* block) { block->size |= block_header_free_bit; } -static void block_set_used(block_header_t* block) -{ +static void block_set_used(block_header_t* block) { block->size &= ~block_header_free_bit; } -static int block_is_prev_free(const block_header_t* block) -{ +static int block_is_prev_free(const block_header_t* block) { return tlsf_cast(int, block->size & block_header_prev_free_bit); } -static void block_set_prev_free(block_header_t* block) -{ +static void block_set_prev_free(block_header_t* block) { block->size |= block_header_prev_free_bit; } -static void block_set_prev_used(block_header_t* block) -{ +static void block_set_prev_used(block_header_t* block) { block->size &= ~block_header_prev_free_bit; } -static block_header_t* block_from_ptr(const void* ptr) -{ +static block_header_t* block_from_ptr(const void* ptr) { return tlsf_cast(block_header_t*, tlsf_cast(unsigned char*, ptr) - block_start_offset); } -static void* block_to_ptr(const block_header_t* block) -{ +static void* block_to_ptr(const block_header_t* block) { return tlsf_cast(void*, tlsf_cast(unsigned char*, block) + block_start_offset); } /* Return location of next block after block of given size. */ -static block_header_t* offset_to_block(const void* ptr, size_t size) -{ +static block_header_t* offset_to_block(const void* ptr, size_t size) { return tlsf_cast(block_header_t*, tlsf_cast(tlsfptr_t, ptr) + size); } /* Return location of previous block. */ -static block_header_t* block_prev(const block_header_t* block) -{ +static block_header_t* block_prev(const block_header_t* block) { tlsf_assert(block_is_prev_free(block) && "previous block must be free"); return block->prev_phys_block; } /* Return location of next existing block. */ -static block_header_t* block_next(const block_header_t* block) -{ +static block_header_t* block_next(const block_header_t* block) { block_header_t* next = offset_to_block(block_to_ptr(block), block_size(block) - block_header_overhead); tlsf_assert(!block_is_last(block)); @@ -525,42 +490,36 @@ static block_header_t* block_next(const block_header_t* block) } /* Link a new block with its physical neighbor, return the neighbor. */ -static block_header_t* block_link_next(block_header_t* block) -{ +static block_header_t* block_link_next(block_header_t* block) { block_header_t* next = block_next(block); next->prev_phys_block = block; return next; } -static void block_mark_as_free(block_header_t* block) -{ +static void block_mark_as_free(block_header_t* block) { /* Link the block to the next block, first. */ block_header_t* next = block_link_next(block); block_set_prev_free(next); block_set_free(block); } -static void block_mark_as_used(block_header_t* block) -{ +static void block_mark_as_used(block_header_t* block) { block_header_t* next = block_next(block); block_set_prev_used(next); block_set_used(block); } -static size_t align_up(size_t x, size_t align) -{ +static size_t align_up(size_t x, size_t align) { tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two"); return (x + (align - 1)) & ~(align - 1); } -static size_t align_down(size_t x, size_t align) -{ +static size_t align_down(size_t x, size_t align) { tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two"); return x - (x & (align - 1)); } -static void* align_ptr(const void* ptr, size_t align) -{ +static void* align_ptr(const void* ptr, size_t align) { const tlsfptr_t aligned = (tlsf_cast(tlsfptr_t, ptr) + (align - 1)) & ~(align - 1); tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two"); @@ -571,16 +530,13 @@ static void* align_ptr(const void* ptr, size_t align) ** Adjust an allocation size to be aligned to word size, and no smaller ** than internal minimum. */ -static size_t adjust_request_size(size_t size, size_t align) -{ +static size_t adjust_request_size(size_t size, size_t align) { size_t adjust = 0; - if (size) - { + if (size) { const size_t aligned = align_up(size, align); /* aligned sized must not exceed block_size_max or we'll go out of bounds on sl_bitmap */ - if (aligned < block_size_max) - { + if (aligned < block_size_max) { adjust = tlsf_max(aligned, block_size_min); } } @@ -592,17 +548,14 @@ static size_t adjust_request_size(size_t size, size_t align) ** the documentation found in the white paper. */ -static void mapping_insert(size_t size, int* fli, int* sli) -{ +static void mapping_insert(size_t size, int* fli, int* sli) { int fl, sl; - if (size < SMALL_BLOCK_SIZE) - { + if (size < SMALL_BLOCK_SIZE) { /* Store small blocks in first list. */ fl = 0; sl = tlsf_cast(int, size) / (SMALL_BLOCK_SIZE / SL_INDEX_COUNT); } - else - { + else { fl = tlsf_fls_sizet(size); sl = tlsf_cast(int, size >> (fl - SL_INDEX_COUNT_LOG2)) ^ (1 << SL_INDEX_COUNT_LOG2); fl -= (FL_INDEX_SHIFT - 1); @@ -612,18 +565,15 @@ static void mapping_insert(size_t size, int* fli, int* sli) } /* This version rounds up to the next block size (for allocations) */ -static void mapping_search(size_t size, int* fli, int* sli) -{ - if (size >= SMALL_BLOCK_SIZE) - { +static void mapping_search(size_t size, int* fli, int* sli) { + if (size >= SMALL_BLOCK_SIZE) { const size_t round = (1 << (tlsf_fls_sizet(size) - SL_INDEX_COUNT_LOG2)) - 1; size += round; } mapping_insert(size, fli, sli); } -static block_header_t* search_suitable_block(control_t* control, int* fli, int* sli) -{ +static block_header_t* search_suitable_block(control_t* control, int* fli, int* sli) { int fl = *fli; int sl = *sli; @@ -632,12 +582,10 @@ static block_header_t* search_suitable_block(control_t* control, int* fli, int* ** fl/sl index. */ unsigned int sl_map = control->sl_bitmap[fl] & (~0U << sl); - if (!sl_map) - { + if (!sl_map) { /* No block exists. Search in the next largest first-level list. */ const unsigned int fl_map = control->fl_bitmap & (~0U << (fl + 1)); - if (!fl_map) - { + if (!fl_map) { /* No free blocks available, memory has been exhausted. */ return 0; } @@ -655,8 +603,7 @@ static block_header_t* search_suitable_block(control_t* control, int* fli, int* } /* Remove a free block from the free list.*/ -static void remove_free_block(control_t* control, block_header_t* block, int fl, int sl) -{ +static void remove_free_block(control_t* control, block_header_t* block, int fl, int sl) { block_header_t* prev = block->prev_free; block_header_t* next = block->next_free; tlsf_assert(prev && "prev_free field can not be null"); @@ -665,18 +612,15 @@ static void remove_free_block(control_t* control, block_header_t* block, int fl, prev->next_free = next; /* If this block is the head of the free list, set new head. */ - if (control->blocks[fl][sl] == block) - { + if (control->blocks[fl][sl] == block) { control->blocks[fl][sl] = next; /* If the new head is null, clear the bitmap. */ - if (next == &control->block_null) - { + if (next == &control->block_null) { control->sl_bitmap[fl] &= ~(1U << sl); /* If the second bitmap is now empty, clear the fl bitmap. */ - if (!control->sl_bitmap[fl]) - { + if (!control->sl_bitmap[fl]) { control->fl_bitmap &= ~(1U << fl); } } @@ -684,8 +628,7 @@ static void remove_free_block(control_t* control, block_header_t* block, int fl, } /* Insert a free block into the free block list. */ -static void insert_free_block(control_t* control, block_header_t* block, int fl, int sl) -{ +static void insert_free_block(control_t* control, block_header_t* block, int fl, int sl) { block_header_t* current = control->blocks[fl][sl]; tlsf_assert(current && "free list cannot have a null entry"); tlsf_assert(block && "cannot insert a null entry into the free list"); @@ -705,32 +648,27 @@ static void insert_free_block(control_t* control, block_header_t* block, int fl, } /* Remove a given block from the free list. */ -static void block_remove(control_t* control, block_header_t* block) -{ +static void block_remove(control_t* control, block_header_t* block) { int fl, sl; mapping_insert(block_size(block), &fl, &sl); remove_free_block(control, block, fl, sl); } /* Insert a given block into the free list. */ -static void block_insert(control_t* control, block_header_t* block) -{ +static void block_insert(control_t* control, block_header_t* block) { int fl, sl; mapping_insert(block_size(block), &fl, &sl); insert_free_block(control, block, fl, sl); } -static int block_can_split(block_header_t* block, size_t size) -{ +static int block_can_split(block_header_t* block, size_t size) { return block_size(block) >= sizeof(block_header_t) + size; } /* Split a block into two, the second of which is free. */ -static block_header_t* block_split(block_header_t* block, size_t size) -{ +static block_header_t* block_split(block_header_t* block, size_t size) { /* Calculate the amount of space left in the remaining block. */ - block_header_t* remaining = - offset_to_block(block_to_ptr(block), size - block_header_overhead); + block_header_t* remaining = offset_to_block(block_to_ptr(block), size - block_header_overhead); const size_t remain_size = block_size(block) - (size + block_header_overhead); @@ -748,8 +686,7 @@ static block_header_t* block_split(block_header_t* block, size_t size) } /* Absorb a free block's storage into an adjacent previous free block. */ -static block_header_t* block_absorb(block_header_t* prev, block_header_t* block) -{ +static block_header_t* block_absorb(block_header_t* prev, block_header_t* block) { tlsf_assert(!block_is_last(prev) && "previous block can't be last"); /* Note: Leaves flags untouched. */ prev->size += block_size(block) + block_header_overhead; @@ -758,10 +695,8 @@ static block_header_t* block_absorb(block_header_t* prev, block_header_t* block) } /* Merge a just-freed block with an adjacent previous free block. */ -static block_header_t* block_merge_prev(control_t* control, block_header_t* block) -{ - if (block_is_prev_free(block)) - { +static block_header_t* block_merge_prev(control_t* control, block_header_t* block) { + if (block_is_prev_free(block)) { block_header_t* prev = block_prev(block); tlsf_assert(prev && "prev physical block can't be null"); tlsf_assert(block_is_free(prev) && "prev block is not free though marked as such"); @@ -773,13 +708,11 @@ static block_header_t* block_merge_prev(control_t* control, block_header_t* bloc } /* Merge a just-freed block with an adjacent free block. */ -static block_header_t* block_merge_next(control_t* control, block_header_t* block) -{ +static block_header_t* block_merge_next(control_t* control, block_header_t* block) { block_header_t* next = block_next(block); tlsf_assert(next && "next physical block can't be null"); - if (block_is_free(next)) - { + if (block_is_free(next)) { tlsf_assert(!block_is_last(block) && "previous block can't be last"); block_remove(control, next); block = block_absorb(block, next); @@ -789,11 +722,9 @@ static block_header_t* block_merge_next(control_t* control, block_header_t* bloc } /* Trim any trailing block space off the end of a block, return to pool. */ -static void block_trim_free(control_t* control, block_header_t* block, size_t size) -{ +static void block_trim_free(control_t* control, block_header_t* block, size_t size) { tlsf_assert(block_is_free(block) && "block must be free"); - if (block_can_split(block, size)) - { + if (block_can_split(block, size)) { block_header_t* remaining_block = block_split(block, size); block_link_next(block); block_set_prev_free(remaining_block); @@ -802,11 +733,9 @@ static void block_trim_free(control_t* control, block_header_t* block, size_t si } /* Trim any trailing block space off the end of a used block, return to pool. */ -static void block_trim_used(control_t* control, block_header_t* block, size_t size) -{ +static void block_trim_used(control_t* control, block_header_t* block, size_t size) { tlsf_assert(!block_is_free(block) && "block must be used"); - if (block_can_split(block, size)) - { + if (block_can_split(block, size)) { /* If the next block is free, we must coalesce. */ block_header_t* remaining_block = block_split(block, size); block_set_prev_used(remaining_block); @@ -816,11 +745,9 @@ static void block_trim_used(control_t* control, block_header_t* block, size_t si } } -static block_header_t* block_trim_free_leading(control_t* control, block_header_t* block, size_t size) -{ +static block_header_t* block_trim_free_leading(control_t* control, block_header_t* block, size_t size) { block_header_t* remaining_block = block; - if (block_can_split(block, size)) - { + if (block_can_split(block, size)) { /* We want the 2nd block. */ remaining_block = block_split(block, size - block_header_overhead); block_set_prev_free(remaining_block); @@ -832,29 +759,24 @@ static block_header_t* block_trim_free_leading(control_t* control, block_header_ return remaining_block; } -static block_header_t* block_locate_free(control_t* control, size_t size) -{ +static block_header_t* block_locate_free(control_t* control, size_t size) { int fl = 0, sl = 0; block_header_t* block = 0; - if (size) - { + if (size) { mapping_search(size, &fl, &sl); - /* ** mapping_search can futz with the size, so for excessively large sizes it can sometimes wind up ** with indices that are off the end of the block array. ** So, we protect against that here, since this is the only callsite of mapping_search. ** Note that we don't need to check sl, since it comes from a modulo operation that guarantees it's always in range. */ - if (fl < FL_INDEX_COUNT) - { + if (fl < FL_INDEX_COUNT) { block = search_suitable_block(control, &fl, &sl); } } - if (block) - { + if (block) { tlsf_assert(block_size(block) >= size); remove_free_block(control, block, fl, sl); } @@ -862,11 +784,9 @@ static block_header_t* block_locate_free(control_t* control, size_t size) return block; } -static void* block_prepare_used(control_t* control, block_header_t* block, size_t size) -{ +static void* block_prepare_used(control_t* control, block_header_t* block, size_t size) { void* p = 0; - if (block) - { + if (block) { tlsf_assert(size && "size must be non-zero"); block_trim_free(control, block, size); block_mark_as_used(block); @@ -876,19 +796,16 @@ static void* block_prepare_used(control_t* control, block_header_t* block, size_ } /* Clear structure and point all empty lists at the null block. */ -static void control_construct(control_t* control) -{ +static void control_construct(control_t* control) { int i, j; control->block_null.next_free = &control->block_null; control->block_null.prev_free = &control->block_null; control->fl_bitmap = 0; - for (i = 0; i < FL_INDEX_COUNT; ++i) - { + for (i = 0; i < FL_INDEX_COUNT; ++i) { control->sl_bitmap[i] = 0; - for (j = 0; j < SL_INDEX_COUNT; ++j) - { + for (j = 0; j < SL_INDEX_COUNT; ++j) { control->blocks[i][j] = &control->block_null; } } @@ -898,16 +815,14 @@ static void control_construct(control_t* control) ** Debugging utilities. */ -typedef struct integrity_t -{ +typedef struct integrity_t { int prev_status; int status; } integrity_t; #define tlsf_insist(x) { tlsf_assert(x); if (!(x)) { status--; } } -static void integrity_walker(void* ptr, size_t size, int used, void* user) -{ +static void integrity_walker(void* ptr, size_t size, int used, void* user) { block_header_t* block = block_from_ptr(ptr); integrity_t* integ = tlsf_cast(integrity_t*, user); const int this_prev_status = block_is_prev_free(block) ? 1 : 0; @@ -923,31 +838,26 @@ static void integrity_walker(void* ptr, size_t size, int used, void* user) integ->status += status; } -int tlsf_check(tlsf_t tlsf) -{ +int tlsf_check(tlsf_t tlsf) { int i, j; control_t* control = tlsf_cast(control_t*, tlsf); int status = 0; /* Check that the free lists and bitmaps are accurate. */ - for (i = 0; i < FL_INDEX_COUNT; ++i) - { - for (j = 0; j < SL_INDEX_COUNT; ++j) - { + for (i = 0; i < FL_INDEX_COUNT; ++i) { + for (j = 0; j < SL_INDEX_COUNT; ++j) { const int fl_map = control->fl_bitmap & (1U << i); const int sl_list = control->sl_bitmap[i]; const int sl_map = sl_list & (1U << j); const block_header_t* block = control->blocks[i][j]; /* Check that first- and second-level lists agree. */ - if (!fl_map) - { + if (!fl_map) { tlsf_insist(!sl_map && "second-level map must be null"); } - if (!sl_map) - { + if (!sl_map) { tlsf_insist(block == &control->block_null && "block list must be null"); continue; } @@ -956,8 +866,7 @@ int tlsf_check(tlsf_t tlsf) tlsf_insist(sl_list && "no free blocks in second-level map"); tlsf_insist(block != &control->block_null && "block should not be null"); - while (block != &control->block_null) - { + while (block != &control->block_null) { int fli, sli; tlsf_insist(block_is_free(block) && "block should be free"); tlsf_insist(!block_is_prev_free(block) && "blocks should have coalesced"); @@ -977,20 +886,16 @@ int tlsf_check(tlsf_t tlsf) #undef tlsf_insist -static void default_walker(void* ptr, size_t size, int used, void* user) -{ +static void default_walker(void* ptr, size_t size, int used, void* user) { (void)user; printf("\t%p %s size: %x (%p)\n", ptr, used ? "used" : "free", (unsigned int)size, block_from_ptr(ptr)); } -void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user) -{ +void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user) { tlsf_walker pool_walker = walker ? walker : default_walker; - block_header_t* block = - offset_to_block(pool, -(int)block_header_overhead); + block_header_t* block = offset_to_block(pool, -(int)block_header_overhead); - while (block && !block_is_last(block)) - { + while (block && !block_is_last(block)) { pool_walker( block_to_ptr(block), block_size(block), @@ -1000,23 +905,19 @@ void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user) } } -size_t tlsf_block_size(void* ptr) -{ +size_t tlsf_block_size(void* ptr) { size_t size = 0; - if (ptr) - { + if (ptr) { const block_header_t* block = block_from_ptr(ptr); size = block_size(block); } return size; } -int tlsf_check_pool(pool_t pool) -{ +int tlsf_check_pool(pool_t pool) { /* Check that the blocks are physically correct. */ integrity_t integ = { 0, 0 }; tlsf_walk_pool(pool, integrity_walker, &integ); - return integ.status; } @@ -1024,23 +925,19 @@ int tlsf_check_pool(pool_t pool) ** Size of the TLSF structures in a given memory block passed to ** tlsf_create, equal to the size of a control_t */ -size_t tlsf_size(void) -{ +size_t tlsf_size(void) { return sizeof(control_t); } -size_t tlsf_align_size(void) -{ +size_t tlsf_align_size(void) { return ALIGN_SIZE; } -size_t tlsf_block_size_min(void) -{ +size_t tlsf_block_size_min(void) { return block_size_min; } -size_t tlsf_block_size_max(void) -{ +size_t tlsf_block_size_max(void) { return block_size_max; } @@ -1049,33 +946,28 @@ size_t tlsf_block_size_max(void) ** tlsf_add_pool, equal to the overhead of a free block and the ** sentinel block. */ -size_t tlsf_pool_overhead(void) -{ +size_t tlsf_pool_overhead(void) { return 2 * block_header_overhead; } -size_t tlsf_alloc_overhead(void) -{ +size_t tlsf_alloc_overhead(void) { return block_header_overhead; } -pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes) -{ +pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes) { block_header_t* block; block_header_t* next; const size_t pool_overhead = tlsf_pool_overhead(); const size_t pool_bytes = align_down(bytes - pool_overhead, ALIGN_SIZE); - if (((ptrdiff_t)mem % ALIGN_SIZE) != 0) - { + if (((ptrdiff_t)mem % ALIGN_SIZE) != 0) { printf("tlsf_add_pool: Memory must be aligned by %u bytes.\n", (unsigned int)ALIGN_SIZE); return 0; } - if (pool_bytes < block_size_min || pool_bytes > block_size_max) - { + if (pool_bytes < block_size_min || pool_bytes > block_size_max) { #if defined (TLSF_64BIT) printf("tlsf_add_pool: Memory size must be between 0x%x and 0x%x00 bytes.\n", (unsigned int)(pool_overhead + block_size_min), @@ -1108,8 +1000,7 @@ pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes) return mem; } -void tlsf_remove_pool(tlsf_t tlsf, pool_t pool) -{ +void tlsf_remove_pool(tlsf_t tlsf, pool_t pool) { control_t* control = tlsf_cast(control_t*, tlsf); block_header_t* block = offset_to_block(pool, -(int)block_header_overhead); @@ -1128,8 +1019,7 @@ void tlsf_remove_pool(tlsf_t tlsf, pool_t pool) */ #if _DEBUG -int test_ffs_fls() -{ +int test_ffs_fls() { /* Verify ffs/fls work properly. */ int rv = 0; rv += (tlsf_ffs(0) == -1) ? 0 : 0x1; @@ -1147,8 +1037,7 @@ int test_ffs_fls() rv += (tlsf_fls_sizet(0xffffffffffffffff) == 63) ? 0 : 0x400; #endif - if (rv) - { + if (rv) { printf("test_ffs_fls: %x ffs/fls tests failed.\n", rv); } return rv; @@ -1191,19 +1080,15 @@ void tlsf_get_heap_usage(HeapUsage* usage) { usage->alloc_max = _heap_alloc_max; } -tlsf_t tlsf_create(void* mem) -{ +tlsf_t tlsf_create(void* mem) { #if _DEBUG - if (test_ffs_fls()) - { + if (test_ffs_fls()) { return 0; } #endif - if (((tlsfptr_t)mem % ALIGN_SIZE) != 0) - { - printf("tlsf_create: Memory must be aligned to %u bytes.\n", - (unsigned int)ALIGN_SIZE); + if (((tlsfptr_t)mem % ALIGN_SIZE) != 0) { + printf("tlsf_create: Memory must be aligned to %u bytes.\n", (unsigned int)ALIGN_SIZE); return 0; } @@ -1212,26 +1097,22 @@ tlsf_t tlsf_create(void* mem) return tlsf_cast(tlsf_t, mem); } -tlsf_t tlsf_create_with_pool(void* mem, size_t bytes) -{ +tlsf_t tlsf_create_with_pool(void* mem, size_t bytes) { tlsf_t tlsf = tlsf_create(mem); tlsf_add_pool(tlsf, (char*)mem + tlsf_size(), bytes - tlsf_size()); return tlsf; } -void tlsf_destroy(tlsf_t tlsf) -{ +void tlsf_destroy(tlsf_t tlsf) { /* Nothing to do. */ (void)tlsf; } -pool_t tlsf_get_pool(tlsf_t tlsf) -{ +pool_t tlsf_get_pool(tlsf_t tlsf) { return tlsf_cast(pool_t, (char*)tlsf + tlsf_size()); } -void* tlsf_malloc(tlsf_t tlsf, size_t size) -{ +void* tlsf_malloc(tlsf_t tlsf, size_t size) { control_t* control = tlsf_cast(control_t*, tlsf); const size_t adjust = adjust_request_size(size, ALIGN_SIZE); block_header_t* block = block_locate_free(control, adjust); @@ -1240,8 +1121,7 @@ void* tlsf_malloc(tlsf_t tlsf, size_t size) return mem; } -void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size) -{ +void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size) { control_t* control = tlsf_cast(control_t*, tlsf); const size_t adjust = adjust_request_size(size, ALIGN_SIZE); @@ -1267,28 +1147,20 @@ void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size) /* This can't be a static assert. */ tlsf_assert(sizeof(block_header_t) == block_size_min + block_header_overhead); - if (block) - { + if (block) { void* ptr = block_to_ptr(block); void* aligned = align_ptr(ptr, align); - size_t gap = tlsf_cast(size_t, - tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr)); - + size_t gap = tlsf_cast(size_t, tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr)); /* If gap size is too small, offset to next aligned boundary. */ - if (gap && gap < gap_minimum) - { + if (gap && gap < gap_minimum) { const size_t gap_remain = gap_minimum - gap; const size_t offset = tlsf_max(gap_remain, align); - const void* next_aligned = tlsf_cast(void*, - tlsf_cast(tlsfptr_t, aligned) + offset); + const void* next_aligned = tlsf_cast(void*, tlsf_cast(tlsfptr_t, aligned) + offset); aligned = align_ptr(next_aligned, align); - gap = tlsf_cast(size_t, - tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr)); + gap = tlsf_cast(size_t, tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr)); } - - if (gap) - { + if (gap) { tlsf_assert(gap >= gap_minimum && "gap size too small"); block = block_trim_free_leading(control, block, gap); } @@ -1299,20 +1171,19 @@ void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size) return mem; } -void tlsf_free(tlsf_t tlsf, void* ptr) -{ +void tlsf_free(tlsf_t tlsf, void* ptr) { /* Don't attempt to free a NULL pointer. */ - if (ptr) - { - control_t* control = tlsf_cast(control_t*, tlsf); - block_header_t* block = block_from_ptr(ptr); - tlsf_assert(!block_is_free(block) && "block already marked as free"); - tlsf_track_heap_usage(NULL, -block_size(block)); - block_mark_as_free(block); - block = block_merge_prev(control, block); - block = block_merge_next(control, block); - block_insert(control, block); + if (!ptr) { + return; } + control_t* control = tlsf_cast(control_t*, tlsf); + block_header_t* block = block_from_ptr(ptr); + tlsf_assert(!block_is_free(block) && "block already marked as free"); + tlsf_track_heap_usage(NULL, -block_size(block)); + block_mark_as_free(block); + block = block_merge_prev(control, block); + block = block_merge_next(control, block); + block_insert(control, block); } /* @@ -1328,23 +1199,17 @@ void tlsf_free(tlsf_t tlsf, void* ptr) ** - an extended buffer size will leave the newly-allocated area with ** contents undefined */ -void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size) -{ +void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size) { control_t* control = tlsf_cast(control_t*, tlsf); void* p = 0; - /* Zero-size requests are treated as free. */ - if (ptr && size == 0) - { + if (ptr && size == 0) { + /* Zero-size requests are treated as free. */ tlsf_free(tlsf, ptr); - } - /* Requests with NULL pointers are treated as malloc. */ - else if (!ptr) - { + } else if (!ptr) { + /* Requests with NULL pointers are treated as malloc. */ p = tlsf_malloc(tlsf, size); - } - else - { + } else { block_header_t* block = block_from_ptr(ptr); block_header_t* next = block_next(block); @@ -1358,21 +1223,16 @@ void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size) ** If the next block is used, or when combined with the current ** block, does not offer enough space, we must reallocate and copy. */ - if (adjust > cursize && (!block_is_free(next) || adjust > combined)) - { + if (adjust > cursize && (!block_is_free(next) || adjust > combined)) { p = tlsf_malloc(tlsf, size); - if (p) - { + if (p) { const size_t minsize = tlsf_min(cursize, size); memcpy(p, ptr, minsize); tlsf_free(tlsf, ptr); } - } - else - { + } else { /* Do we need to expand to the next block? */ - if (adjust > cursize) - { + if (adjust > cursize) { block_merge_next(control, block); block_mark_as_used(block); } @@ -1384,7 +1244,6 @@ void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size) p = ptr; } } - return p; }