Skip to content

Commit 9e08368

Browse files
committed
clang-format
1 parent 85f5d65 commit 9e08368

File tree

4 files changed

+83
-96
lines changed

4 files changed

+83
-96
lines changed

bench.c

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22
* All rights reserved.
33
* Use of this source code is governed by a BSD-style license.
44
*/
5-
#include <unistd.h>
5+
#include "tlsf.h"
66
#include <assert.h>
77
#include <errno.h>
88
#include <sched.h>
99
#include <stdbool.h>
1010
#include <stdio.h>
1111
#include <stdlib.h>
1212
#include <string.h>
13-
#include <time.h>
1413
#include <sys/resource.h>
1514
#include <time.h>
16-
#include "tlsf.h"
15+
#include <unistd.h>
1716

1817
static tlsf t = TLSF_INIT;
1918

20-
static void usage(const char *name) {
19+
static void usage(const char* name) {
2120
printf("run a malloc benchmark.\n"
2221
"usage: %s [-s blk-size|blk-min:blk-max] [-l loop-count] "
2322
"[-n num-blocks] [-c]\n",
@@ -26,19 +25,19 @@ static void usage(const char *name) {
2625
}
2726

2827
/* Parse an integer argument. */
29-
static size_t parse_int_arg(const char *arg, const char *exe_name) {
28+
static size_t parse_int_arg(const char* arg, const char* exe_name) {
3029
long ret = strtol(arg, NULL, 0);
3130
if (errno)
3231
usage(exe_name);
3332

34-
return (size_t) ret;
33+
return (size_t)ret;
3534
}
3635

3736
/* Parse a size argument, which is either an integer or two integers
3837
separated by a colon, denoting a range. */
39-
static void parse_size_arg(const char *arg, const char *exe_name,
40-
size_t *blk_min, size_t *blk_max) {
41-
char *endptr;
38+
static void parse_size_arg(const char* arg, const char* exe_name, size_t* blk_min,
39+
size_t* blk_max) {
40+
char* endptr;
4241
*blk_min = (size_t)strtol(arg, &endptr, 0);
4342

4443
if (errno)
@@ -61,8 +60,8 @@ static size_t get_random_block_size(size_t blk_min, size_t blk_max) {
6160
return blk_min;
6261
}
6362

64-
static void run_alloc_benchmark(size_t loops, size_t blk_min, size_t blk_max,
65-
void **blk_array, size_t num_blks, bool clear) {
63+
static void run_alloc_benchmark(size_t loops, size_t blk_min, size_t blk_max, void** blk_array,
64+
size_t num_blks, bool clear) {
6665
while (loops--) {
6766
size_t next_idx = (size_t)rand() % num_blks;
6867
size_t blk_size = get_random_block_size(blk_min, blk_max);
@@ -99,32 +98,20 @@ void* tlsf_resize(tlsf* _t, size_t req_size) {
9998
return req_size <= max_size ? mem : 0;
10099
}
101100

102-
int main(int argc, char **argv) {
101+
int main(int argc, char** argv) {
103102
size_t blk_min = 512, blk_max = 512, num_blks = 10000;
104103
size_t loops = 10000000;
105104
bool clear = false;
106105
int opt;
107106

108107
while ((opt = getopt(argc, argv, "s:l:r:t:n:b:ch")) > 0) {
109108
switch (opt) {
110-
case 's':
111-
parse_size_arg(optarg, argv[0], &blk_min, &blk_max);
112-
break;
113-
case 'l':
114-
loops = parse_int_arg(optarg, argv[0]);
115-
break;
116-
case 'n':
117-
num_blks = parse_int_arg(optarg, argv[0]);
118-
break;
119-
case 'c':
120-
clear = true;
121-
break;
122-
case 'h':
123-
usage(argv[0]);
124-
break;
125-
default:
126-
usage(argv[0]);
127-
break;
109+
case 's': parse_size_arg(optarg, argv[0], &blk_min, &blk_max); break;
110+
case 'l': loops = parse_int_arg(optarg, argv[0]); break;
111+
case 'n': num_blks = parse_int_arg(optarg, argv[0]); break;
112+
case 'c': clear = true; break;
113+
case 'h': usage(argv[0]); break;
114+
default: usage(argv[0]); break;
128115
}
129116
}
130117

@@ -141,23 +128,23 @@ int main(int argc, char **argv) {
141128

142129
printf("blk_min=%zu to blk_max=%zu\n", blk_min, blk_max);
143130

144-
run_alloc_benchmark(loops, blk_min, blk_max,
145-
blk_array, num_blks, clear);
131+
run_alloc_benchmark(loops, blk_min, blk_max, blk_array, num_blks, clear);
146132

147133
err = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
148134
assert(err == 0);
149135
free(blk_array);
150136

151-
double elapsed = (double)(end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) * 1e-9;
137+
double elapsed =
138+
(double)(end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) * 1e-9;
152139

153140
struct rusage usage;
154141
err = getrusage(RUSAGE_SELF, &usage);
155142
assert(err == 0);
156143

157144
/* Dump both machine and human readable versions */
158-
printf("%zu:%zu:%zu:%u:%lu:%.6f: took %.6f s for %zu malloc/free\nbenchmark loops of %zu-%zu bytes. ~%.3f us per loop\n",
159-
blk_min, blk_max, loops,
160-
clear, usage.ru_maxrss, elapsed, elapsed, loops, blk_min,
145+
printf("%zu:%zu:%zu:%u:%lu:%.6f: took %.6f s for %zu malloc/free\nbenchmark loops of %zu-%zu "
146+
"bytes. ~%.3f us per loop\n",
147+
blk_min, blk_max, loops, clear, usage.ru_maxrss, elapsed, elapsed, loops, blk_min,
161148
blk_max, elapsed / (double)loops * 1e6);
162149

163150
return 0;

test.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
* All rights reserved.
33
* Use of this source code is governed by a BSD-style license.
44
*/
5-
#include <stdint.h>
5+
#include "tlsf.h"
6+
#include <assert.h>
67
#include <stdbool.h>
8+
#include <stdint.h>
79
#include <stdio.h>
810
#include <stdlib.h>
911
#include <string.h>
10-
#include <unistd.h>
11-
#include <time.h>
12-
#include <assert.h>
1312
#include <sys/mman.h>
14-
#include "tlsf.h"
13+
#include <time.h>
14+
#include <unistd.h>
1515

1616
static size_t PAGE;
1717
static size_t MAX_PAGES;
@@ -22,17 +22,17 @@ void* tlsf_resize(tlsf* t, size_t req_size) {
2222
(void)t;
2323

2424
if (!start_addr)
25-
start_addr = mmap(0, MAX_PAGES * PAGE,
26-
PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE,
27-
-1, 0);
25+
start_addr = mmap(0, MAX_PAGES * PAGE, PROT_READ | PROT_WRITE,
26+
MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
2827

2928
size_t req_pages = (req_size + PAGE - 1) / PAGE;
3029
if (req_pages > MAX_PAGES)
3130
return 0;
3231

3332
if (req_pages != curr_pages) {
3433
if (req_pages < curr_pages)
35-
madvise((char*)start_addr + PAGE * req_pages, (size_t)(curr_pages - req_pages) * PAGE, MADV_DONTNEED);
34+
madvise((char*)start_addr + PAGE * req_pages, (size_t)(curr_pages - req_pages) * PAGE,
35+
MADV_DONTNEED);
3636
curr_pages = req_pages;
3737
}
3838

@@ -42,7 +42,7 @@ void* tlsf_resize(tlsf* t, size_t req_size) {
4242
static void random_test(tlsf* t, size_t spacelen, const size_t cap) {
4343
const size_t maxitems = 2 * spacelen;
4444

45-
void** p = (void**)malloc(maxitems * sizeof(void *));
45+
void** p = (void**)malloc(maxitems * sizeof(void*));
4646
assert(p);
4747

4848
/*
@@ -106,10 +106,10 @@ static void random_test(tlsf* t, size_t spacelen, const size_t cap) {
106106
free(p);
107107
}
108108

109-
#define__arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
109+
#define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
110110

111111
static void random_sizes_test(tlsf* t) {
112-
const size_t sizes[] = {16, 32, 64, 128, 256, 512, 1024, 1024 * 1024};//, 128 * 1024 * 1024};
112+
const size_t sizes[] = { 16, 32, 64, 128, 256, 512, 1024, 1024 * 1024 }; //, 128 * 1024 * 1024};
113113

114114
for (unsigned i = 0; i < __arraycount(sizes); i++) {
115115
unsigned n = 1024;

tlsf.c

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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
@@ -21,22 +21,22 @@
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

4242
typedef 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

7373
TLSF_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

8079
TLSF_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

9796
TLSF_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

101101
TLSF_INL size_t align_up(size_t x, size_t align) {
@@ -113,7 +113,8 @@ TLSF_INL char* block_payload(tlsf_block* block) {
113113

114114
TLSF_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

144145
TLSF_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

148149
TLSF_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

351352
static 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) {
411413
TLSF_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+
})
501501
TLSF_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

Comments
 (0)