Skip to content

Commit 79bd54d

Browse files
committed
remove indirection
1 parent e4a2c73 commit 79bd54d

File tree

4 files changed

+11
-23
lines changed

4 files changed

+11
-23
lines changed

bench.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <time.h>
1616
#include "tlsf.h"
1717

18-
static tlsf t;
18+
static tlsf t = TLSF_INIT;
1919

2020
static void usage(const char *name) {
2121
printf("run a malloc benchmark.\n"
@@ -94,7 +94,7 @@ static void run_alloc_benchmark(size_t loops, size_t blk_min, size_t blk_max,
9494
static size_t max_size;
9595
static void* mem = 0;
9696

97-
static void* resize(tlsf* _t, size_t req_size) {
97+
void* tlsf_resize(tlsf* _t, size_t req_size) {
9898
(void)_t;
9999
return req_size <= max_size ? mem : 0;
100100
}
@@ -130,7 +130,6 @@ int main(int argc, char **argv) {
130130

131131
max_size = blk_max * num_blks;
132132
mem = malloc(max_size);
133-
tlsf_init(&t, resize);
134133

135134
void** blk_array = (void**)calloc(num_blks, sizeof(void*));
136135
assert(blk_array);

test.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static size_t MAX_PAGES;
1818
static size_t curr_pages = 0;
1919
static void* start_addr = 0;
2020

21-
static void* resize(tlsf* t, size_t req_size) {
21+
void* tlsf_resize(tlsf* t, size_t req_size) {
2222
(void)t;
2323

2424
if (!start_addr)
@@ -158,10 +158,7 @@ static void large_size_test(tlsf* t) {
158158
int main(void) {
159159
PAGE = (size_t)sysconf(_SC_PAGESIZE);
160160
MAX_PAGES = 20 * TLSF_MAX_SIZE / PAGE;
161-
162-
tlsf t;
163-
tlsf_init(&t, resize);
164-
161+
tlsf t = TLSF_INIT;
165162
srand((unsigned int)time(0));
166163
large_size_test(&t);
167164
random_sizes_test(&t);

tlsf.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ TLSF_INL void check_sentinel(tlsf_block* block) {
350350

351351
static bool arena_grow(tlsf* t, size_t size) {
352352
size_t req_size = (t->size ? t->size + BLOCK_OVERHEAD : 2*BLOCK_OVERHEAD) + size;
353-
void* addr = t->resize(t, req_size);
353+
void* addr = tlsf_resize(t, req_size);
354354
if (!addr)
355355
return false;
356356
TLSF_ASSERT((size_t)addr % ALIGN_SIZE == 0, "wrong heap alignment address");
@@ -375,7 +375,7 @@ static void arena_shrink(tlsf* t, tlsf_block* block) {
375375
t->size = t->size - size - BLOCK_OVERHEAD;
376376
if (t->size == BLOCK_OVERHEAD)
377377
t->size = 0;
378-
t->resize(t, t->size);
378+
tlsf_resize(t, t->size);
379379
if (t->size) {
380380
block->header = 0;
381381
check_sentinel(block);
@@ -398,11 +398,6 @@ TLSF_INL tlsf_block* block_find_free(tlsf* t, size_t size) {
398398
return block;
399399
}
400400

401-
TLSF_API void tlsf_init(tlsf* t, tlsf_resize resize) {
402-
memset(t, 0, sizeof (tlsf));
403-
t->resize = resize;
404-
}
405-
406401
TLSF_API void* tlsf_malloc(tlsf* t, size_t size) {
407402
size = adjust_size(size, ALIGN_SIZE);
408403
if (UNLIKELY(size > TLSF_MAX_SIZE))

tlsf.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@
1111
#define _TLSF_FL_COUNT (sizeof (size_t) == 8 ? 32 : 25)
1212
#define _TLSF_FL_MAX (sizeof (size_t) == 8 ? 38 : 30)
1313
#define TLSF_MAX_SIZE (((size_t)1 << (_TLSF_FL_MAX - 1)) - sizeof (size_t))
14+
#define TLSF_INIT (tlsf){.size=0}
1415

15-
typedef struct tlsf_ tlsf;
16-
typedef void* (*tlsf_resize)(tlsf*, size_t);
17-
18-
struct tlsf_ {
16+
typedef struct {
1917
uint32_t fl, sl[_TLSF_FL_COUNT];
2018
struct tlsf_block_* block[_TLSF_FL_COUNT][_TLSF_SL_COUNT];
21-
tlsf_resize resize;
22-
size_t size;
23-
};
19+
size_t size;
20+
} tlsf;
2421

25-
TLSF_API void tlsf_init(tlsf*, tlsf_resize);
22+
TLSF_API void* tlsf_resize(tlsf*, size_t);
2623
TLSF_API void* tlsf_aalloc(tlsf*, size_t, size_t);
2724
TLSF_API void* tlsf_malloc(tlsf*, size_t);
2825
TLSF_API void* tlsf_realloc(tlsf*, void*, size_t);

0 commit comments

Comments
 (0)