11#include "tlsf.h"
2- #include <string.h>
32#include <stdbool.h>
3+ #include <string.h>
44
55#define UNLIKELY (x ) __builtin_expect(!!(x), false)
66
77// All allocation sizes and addresses are aligned.
88#define ALIGN_SIZE ((size_t)1 << ALIGN_SHIFT)
9- #define ALIGN_SHIFT (sizeof (size_t) == 8 ? 3 : 2)
9+ #define ALIGN_SHIFT (sizeof(size_t) == 8 ? 3 : 2)
1010
1111// First and second level counts
1212#define SL_SHIFT 4
2121#define BLOCK_BITS (BLOCK_BIT_FREE | BLOCK_BIT_PREV_FREE)
2222
2323// A free block must be large enough to store its header minus the size of the prev field.
24- #define BLOCK_OVERHEAD (sizeof (size_t))
25- #define BLOCK_SIZE_MIN (sizeof (tlsf_block) - sizeof (tlsf_block*))
24+ #define BLOCK_OVERHEAD (sizeof(size_t))
25+ #define BLOCK_SIZE_MIN (sizeof(tlsf_block) - sizeof(tlsf_block*))
2626#define BLOCK_SIZE_MAX ((size_t)1 << (FL_MAX - 1))
2727#define BLOCK_SIZE_SMALL ((size_t)1 << FL_SHIFT)
2828
2929#ifndef TLSF_ASSERT
30- # ifdef TLSF_ENABLE_ASSERT
31- # include <assert.h>
32- # define TLSF_ASSERT (cond , msg ) assert((cond) && msg)
33- # else
34- # define TLSF_ASSERT (cond , msg )
35- # endif
30+ # ifdef TLSF_ENABLE_ASSERT
31+ # include <assert.h>
32+ # define TLSF_ASSERT (cond , msg ) assert((cond) && msg)
33+ # else
34+ # define TLSF_ASSERT (cond , msg )
35+ # endif
3636#endif
3737
3838#ifndef TLSF_INL
39- # define TLSF_INL static inline __attribute__ ((always_inline))
39+ # define TLSF_INL static inline __attribute__((always_inline))
4040#endif
4141
4242typedef struct tlsf_block_ tlsf_block ;
@@ -54,8 +54,8 @@ struct tlsf_block_ {
5454 tlsf_block * next_free , * prev_free ;
5555};
5656
57- _Static_assert (sizeof (size_t ) == 4 || sizeof (size_t ) == 8 , "size_t must be 32 or 64 bit" );
58- _Static_assert (sizeof (size_t ) == sizeof (void * ), "size_t must equal pointer size" );
57+ _Static_assert (sizeof (size_t ) == 4 || sizeof (size_t ) == 8 , "size_t must be 32 or 64 bit" );
58+ _Static_assert (sizeof (size_t ) == sizeof (void * ), "size_t must equal pointer size" );
5959_Static_assert (ALIGN_SIZE == BLOCK_SIZE_SMALL / SL_COUNT , "sizes are not properly set" );
6060_Static_assert (BLOCK_SIZE_MIN < BLOCK_SIZE_SMALL , "min allocation size is wrong" );
6161_Static_assert (BLOCK_SIZE_MAX == TLSF_MAX_SIZE + BLOCK_OVERHEAD , "max allocation size is wrong" );
@@ -72,9 +72,8 @@ TLSF_INL uint32_t bitmap_ffs(uint32_t x) {
7272
7373TLSF_INL uint32_t log2floor (size_t x ) {
7474 TLSF_ASSERT (x > 0 , "log2 of zero" );
75- return sizeof (size_t ) == 8
76- ? (uint32_t )(63 - (uint32_t )__builtin_clzll ((unsigned long long )x ))
77- : (uint32_t )(31 - (uint32_t )__builtin_clzl ((unsigned long )x ));
75+ return sizeof (size_t ) == 8 ? (uint32_t )(63 - (uint32_t )__builtin_clzll ((unsigned long long )x ))
76+ : (uint32_t )(31 - (uint32_t )__builtin_clzl ((unsigned long )x ));
7877}
7978
8079TLSF_INL size_t block_size (const tlsf_block * block ) {
@@ -95,7 +94,8 @@ TLSF_INL bool block_is_prev_free(const tlsf_block* block) {
9594}
9695
9796TLSF_INL void block_set_prev_free (tlsf_block * block , bool free ) {
98- block -> header = free ? block -> header | BLOCK_BIT_PREV_FREE : block -> header & ~BLOCK_BIT_PREV_FREE ;
97+ block -> header =
98+ free ? block -> header | BLOCK_BIT_PREV_FREE : block -> header & ~BLOCK_BIT_PREV_FREE ;
9999}
100100
101101TLSF_INL size_t align_up (size_t x , size_t align ) {
@@ -113,7 +113,8 @@ TLSF_INL char* block_payload(tlsf_block* block) {
113113
114114TLSF_INL tlsf_block * to_block (void * ptr ) {
115115 tlsf_block * block = (tlsf_block * )ptr ;
116- TLSF_ASSERT (block_payload (block ) == align_ptr (block_payload (block ), ALIGN_SIZE ), "block not aligned properly" );
116+ TLSF_ASSERT (block_payload (block ) == align_ptr (block_payload (block ), ALIGN_SIZE ),
117+ "block not aligned properly" );
117118 return block ;
118119}
119120
@@ -142,7 +143,7 @@ TLSF_INL tlsf_block* block_link_next(tlsf_block* block) {
142143}
143144
144145TLSF_INL bool block_can_split (tlsf_block * block , size_t size ) {
145- return block_size (block ) >= sizeof (tlsf_block ) + size ;
146+ return block_size (block ) >= sizeof (tlsf_block ) + size ;
146147}
147148
148149TLSF_INL void block_set_free (tlsf_block * block , bool free ) {
@@ -163,7 +164,7 @@ TLSF_INL size_t round_block_size(size_t size) {
163164 return size >= BLOCK_SIZE_SMALL ? (size + t ) & ~t : size ;
164165}
165166
166- TLSF_INL void mapping (size_t size , uint32_t * fl , uint32_t * sl ) {
167+ TLSF_INL void mapping (size_t size , uint32_t * fl , uint32_t * sl ) {
167168 if (size < BLOCK_SIZE_SMALL ) {
168169 // Store small blocks in first list.
169170 * fl = 0 ;
@@ -177,7 +178,7 @@ TLSF_INL void mapping(size_t size, uint32_t *fl, uint32_t *sl) {
177178 TLSF_ASSERT (* sl < SL_COUNT , "wrong second level" );
178179}
179180
180- TLSF_INL tlsf_block * block_find_suitable (tlsf * t , uint32_t * fl , uint32_t * sl ) {
181+ TLSF_INL tlsf_block * block_find_suitable (tlsf * t , uint32_t * fl , uint32_t * sl ) {
181182 TLSF_ASSERT (* fl < FL_COUNT , "wrong first level" );
182183 TLSF_ASSERT (* sl < SL_COUNT , "wrong second level" );
183184
@@ -349,12 +350,13 @@ TLSF_INL void check_sentinel(tlsf_block* block) {
349350}
350351
351352static bool arena_grow (tlsf * t , size_t size ) {
352- size_t req_size = (t -> size ? t -> size + BLOCK_OVERHEAD : 2 * BLOCK_OVERHEAD ) + size ;
353+ size_t req_size = (t -> size ? t -> size + BLOCK_OVERHEAD : 2 * BLOCK_OVERHEAD ) + size ;
353354 void * addr = tlsf_resize (t , req_size );
354355 if (!addr )
355356 return false;
356357 TLSF_ASSERT ((size_t )addr % ALIGN_SIZE == 0 , "wrong heap alignment address" );
357- tlsf_block * block = to_block (t -> size ? (char * )addr + t -> size - 2 * BLOCK_OVERHEAD : (char * )addr - BLOCK_OVERHEAD );
358+ tlsf_block * block = to_block (t -> size ? (char * )addr + t -> size - 2 * BLOCK_OVERHEAD
359+ : (char * )addr - BLOCK_OVERHEAD );
358360 if (!t -> size )
359361 block -> header = 0 ;
360362 check_sentinel (block );
@@ -411,20 +413,19 @@ TLSF_API void* tlsf_malloc(tlsf* t, size_t size) {
411413TLSF_API void * tlsf_aalloc (tlsf * t , size_t align , size_t size ) {
412414 size_t adjust = adjust_size (size , ALIGN_SIZE );
413415
414- if (UNLIKELY (!size ||
415- ((align | size ) & (align - 1 )) || // align!=2**x, size!=n*align
416- adjust > TLSF_MAX_SIZE - align - sizeof (tlsf_block ))) // size is too large
416+ if (UNLIKELY (!size || ((align | size ) & (align - 1 )) || // align!=2**x, size!=n*align
417+ adjust > TLSF_MAX_SIZE - align - sizeof (tlsf_block ))) // size is too large
417418 return 0 ;
418419
419420 if (align <= ALIGN_SIZE )
420421 return tlsf_malloc (t , size );
421422
422- size_t asize = adjust_size (adjust + align - 1 + sizeof (tlsf_block ), align );
423+ size_t asize = adjust_size (adjust + align - 1 + sizeof (tlsf_block ), align );
423424 tlsf_block * block = block_find_free (t , asize );
424425 if (UNLIKELY (!block ))
425426 return 0 ;
426427
427- char * mem = align_ptr (block_payload (block ) + sizeof (tlsf_block ), align );
428+ char * mem = align_ptr (block_payload (block ) + sizeof (tlsf_block ), align );
428429 block = block_ltrim_free (t , block , (size_t )(mem - block_payload (block )));
429430 return block_use (t , block , adjust );
430431}
@@ -488,22 +489,19 @@ TLSF_API void* tlsf_realloc(tlsf* t, void* mem, size_t size) {
488489}
489490
490491#ifdef TLSF_ENABLE_CHECK
491- #include <stdio.h>
492- #include <stdlib.h>
493- #define CHECK (cond , msg ) \
494- ({ \
495- if (!(cond)) { \
496- fprintf(stderr, "TLSF CHECK: %s - %s\n", msg, #cond); \
497- abort(); \
498- } \
499- })
500-
492+ #include <stdio.h>
493+ #include <stdlib.h>
494+ #define CHECK (cond , msg ) \
495+ ({ \
496+ if (!(cond)) { \
497+ fprintf(stderr, "TLSF CHECK: %s - %s\n", msg, #cond); \
498+ abort(); \
499+ } \
500+ })
501501TLSF_API void tlsf_check (tlsf * t ) {
502502 for (uint32_t i = 0 ; i < FL_COUNT ; ++ i ) {
503503 for (uint32_t j = 0 ; j < SL_COUNT ; ++ j ) {
504- size_t fl_map = t -> fl & (1U << i ),
505- sl_list = t -> sl [i ],
506- sl_map = sl_list & (1U << j );
504+ size_t fl_map = t -> fl & (1U << i ), sl_list = t -> sl [i ], sl_map = sl_list & (1U << j );
507505 tlsf_block * block = t -> block [i ][j ];
508506
509507 // Check that first- and second-level lists agree.
0 commit comments