Skip to content

Commit 8f12140

Browse files
committed
Enforce consistent style
1 parent f24aee1 commit 8f12140

File tree

5 files changed

+344
-210
lines changed

5 files changed

+344
-210
lines changed

.clang-format

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
BasedOnStyle: Chromium
2+
Language: Cpp
3+
MaxEmptyLinesToKeep: 3
4+
IndentCaseLabels: false
5+
AllowShortIfStatementsOnASingleLine: false
6+
AllowShortCaseLabelsOnASingleLine: false
7+
AllowShortLoopsOnASingleLine: false
8+
DerivePointerAlignment: false
9+
PointerAlignment: Right
10+
SpaceAfterCStyleCast: true
11+
TabWidth: 4
12+
UseTab: Never
13+
IndentWidth: 4
14+
BreakBeforeBraces: Linux
15+
AccessModifierOffset: -4

bench.c

Lines changed: 72 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* Use of this source code is governed by a BSD-style license.
55
*/
66

7-
#include "tlsf.h"
87
#include <assert.h>
98
#include <errno.h>
109
#include <sched.h>
@@ -16,37 +15,45 @@
1615
#include <time.h>
1716
#include <unistd.h>
1817

18+
#include "tlsf.h"
19+
1920
static tlsf t = TLSF_INIT;
2021

21-
static void usage(const char* name) {
22-
printf("run a malloc benchmark.\n"
23-
"usage: %s [-s blk-size|blk-min:blk-max] [-l loop-count] "
24-
"[-n num-blocks] [-c]\n",
25-
name);
22+
static void usage(const char *name)
23+
{
24+
printf(
25+
"run a malloc benchmark.\n"
26+
"usage: %s [-s blk-size|blk-min:blk-max] [-l loop-count] "
27+
"[-n num-blocks] [-c]\n",
28+
name);
2629
exit(-1);
2730
}
2831

2932
/* Parse an integer argument. */
30-
static size_t parse_int_arg(const char* arg, const char* exe_name) {
33+
static size_t parse_int_arg(const char *arg, const char *exe_name)
34+
{
3135
long ret = strtol(arg, NULL, 0);
3236
if (errno)
3337
usage(exe_name);
3438

35-
return (size_t)ret;
39+
return (size_t) ret;
3640
}
3741

3842
/* Parse a size argument, which is either an integer or two integers
3943
separated by a colon, denoting a range. */
40-
static void parse_size_arg(const char* arg, const char* exe_name, size_t* blk_min,
41-
size_t* blk_max) {
42-
char* endptr;
43-
*blk_min = (size_t)strtol(arg, &endptr, 0);
44+
static void parse_size_arg(const char *arg,
45+
const char *exe_name,
46+
size_t *blk_min,
47+
size_t *blk_max)
48+
{
49+
char *endptr;
50+
*blk_min = (size_t) strtol(arg, &endptr, 0);
4451

4552
if (errno)
4653
usage(exe_name);
4754

4855
if (endptr && *endptr == ':') {
49-
*blk_max = (size_t)strtol(endptr + 1, NULL, 0);
56+
*blk_max = (size_t) strtol(endptr + 1, NULL, 0);
5057
if (errno)
5158
usage(exe_name);
5259
}
@@ -56,29 +63,39 @@ static void parse_size_arg(const char* arg, const char* exe_name, size_t* blk_mi
5663
}
5764

5865
/* Get a random block size between blk_min and blk_max. */
59-
static size_t get_random_block_size(size_t blk_min, size_t blk_max) {
66+
static size_t get_random_block_size(size_t blk_min, size_t blk_max)
67+
{
6068
if (blk_max > blk_min)
61-
return blk_min + ((size_t)rand() % (blk_max - blk_min));
69+
return blk_min + ((size_t) rand() % (blk_max - blk_min));
6270
return blk_min;
6371
}
6472

65-
static void run_alloc_benchmark(size_t loops, size_t blk_min, size_t blk_max, void** blk_array,
66-
size_t num_blks, bool clear) {
73+
static void run_alloc_benchmark(size_t loops,
74+
size_t blk_min,
75+
size_t blk_max,
76+
void **blk_array,
77+
size_t num_blks,
78+
bool clear)
79+
{
6780
while (loops--) {
68-
size_t next_idx = (size_t)rand() % num_blks;
81+
size_t next_idx = (size_t) rand() % num_blks;
6982
size_t blk_size = get_random_block_size(blk_min, blk_max);
7083

7184
if (blk_array[next_idx]) {
7285
if (rand() % 10 == 0) {
73-
/* Insert the newly alloced block into the array at a random point. */
74-
blk_array[next_idx] = tlsf_realloc(&t, blk_array[next_idx], blk_size);
86+
/* Insert the newly alloced block into the array at a random
87+
* point. */
88+
blk_array[next_idx] =
89+
tlsf_realloc(&t, blk_array[next_idx], blk_size);
7590
} else {
7691
tlsf_free(&t, blk_array[next_idx]);
77-
/* Insert the newly alloced block into the array at a random point. */
92+
/* Insert the newly alloced block into the array at a random
93+
* point. */
7894
blk_array[next_idx] = tlsf_malloc(&t, blk_size);
7995
}
8096
} else {
81-
/* Insert the newly alloced block into the array at a random point. */
97+
/* Insert the newly alloced block into the array at a random point.
98+
*/
8299
blk_array[next_idx] = tlsf_malloc(&t, blk_size);
83100
}
84101
if (clear)
@@ -93,34 +110,48 @@ static void run_alloc_benchmark(size_t loops, size_t blk_min, size_t blk_max, vo
93110
}
94111

95112
static size_t max_size;
96-
static void* mem = 0;
113+
static void *mem = 0;
97114

98-
void* tlsf_resize(tlsf* _t, size_t req_size) {
99-
(void)_t;
115+
void *tlsf_resize(tlsf *_t, size_t req_size)
116+
{
117+
(void) _t;
100118
return req_size <= max_size ? mem : 0;
101119
}
102120

103-
int main(int argc, char** argv) {
121+
int main(int argc, char **argv)
122+
{
104123
size_t blk_min = 512, blk_max = 512, num_blks = 10000;
105124
size_t loops = 10000000;
106125
bool clear = false;
107126
int opt;
108127

109128
while ((opt = getopt(argc, argv, "s:l:r:t:n:b:ch")) > 0) {
110129
switch (opt) {
111-
case 's': parse_size_arg(optarg, argv[0], &blk_min, &blk_max); break;
112-
case 'l': loops = parse_int_arg(optarg, argv[0]); break;
113-
case 'n': num_blks = parse_int_arg(optarg, argv[0]); break;
114-
case 'c': clear = true; break;
115-
case 'h': usage(argv[0]); break;
116-
default: usage(argv[0]); break;
130+
case 's':
131+
parse_size_arg(optarg, argv[0], &blk_min, &blk_max);
132+
break;
133+
case 'l':
134+
loops = parse_int_arg(optarg, argv[0]);
135+
break;
136+
case 'n':
137+
num_blks = parse_int_arg(optarg, argv[0]);
138+
break;
139+
case 'c':
140+
clear = true;
141+
break;
142+
case 'h':
143+
usage(argv[0]);
144+
break;
145+
default:
146+
usage(argv[0]);
147+
break;
117148
}
118149
}
119150

120151
max_size = blk_max * num_blks;
121152
mem = malloc(max_size);
122153

123-
void** blk_array = (void**)calloc(num_blks, sizeof(void*));
154+
void **blk_array = (void **) calloc(num_blks, sizeof(void *));
124155
assert(blk_array);
125156

126157
struct timespec start, end;
@@ -136,18 +167,20 @@ int main(int argc, char** argv) {
136167
assert(err == 0);
137168
free(blk_array);
138169

139-
double elapsed =
140-
(double)(end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) * 1e-9;
170+
double elapsed = (double) (end.tv_sec - start.tv_sec) +
171+
(double) (end.tv_nsec - start.tv_nsec) * 1e-9;
141172

142173
struct rusage usage;
143174
err = getrusage(RUSAGE_SELF, &usage);
144175
assert(err == 0);
145176

146177
/* Dump both machine and human readable versions */
147-
printf("%zu:%zu:%zu:%u:%lu:%.6f: took %.6f s for %zu malloc/free\nbenchmark loops of %zu-%zu "
148-
"bytes. ~%.3f us per loop\n",
149-
blk_min, blk_max, loops, clear, usage.ru_maxrss, elapsed, elapsed, loops, blk_min,
150-
blk_max, elapsed / (double)loops * 1e6);
178+
printf(
179+
"%zu:%zu:%zu:%u:%lu:%.6f: took %.6f s for %zu malloc/free\nbenchmark "
180+
"loops of %zu-%zu "
181+
"bytes. ~%.3f us per loop\n",
182+
blk_min, blk_max, loops, clear, usage.ru_maxrss, elapsed, elapsed,
183+
loops, blk_min, blk_max, elapsed / (double) loops * 1e6);
151184

152185
return 0;
153186
}

test.c

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* Use of this source code is governed by a BSD-style license.
55
*/
66

7-
#include "tlsf.h"
87
#include <assert.h>
98
#include <stdbool.h>
109
#include <stdint.h>
@@ -15,13 +14,16 @@
1514
#include <time.h>
1615
#include <unistd.h>
1716

17+
#include "tlsf.h"
18+
1819
static size_t PAGE;
1920
static size_t MAX_PAGES;
2021
static size_t curr_pages = 0;
21-
static void* start_addr = 0;
22+
static void *start_addr = 0;
2223

23-
void* tlsf_resize(tlsf* t, size_t req_size) {
24-
(void)t;
24+
void *tlsf_resize(tlsf *t, size_t req_size)
25+
{
26+
(void) t;
2527

2628
if (!start_addr)
2729
start_addr = mmap(0, MAX_PAGES * PAGE, PROT_READ | PROT_WRITE,
@@ -33,53 +35,55 @@ void* tlsf_resize(tlsf* t, size_t req_size) {
3335

3436
if (req_pages != curr_pages) {
3537
if (req_pages < curr_pages)
36-
madvise((char*)start_addr + PAGE * req_pages, (size_t)(curr_pages - req_pages) * PAGE,
37-
MADV_DONTNEED);
38+
madvise((char *) start_addr + PAGE * req_pages,
39+
(size_t) (curr_pages - req_pages) * PAGE, MADV_DONTNEED);
3840
curr_pages = req_pages;
3941
}
4042

4143
return start_addr;
4244
}
4345

44-
static void random_test(tlsf* t, size_t spacelen, const size_t cap) {
46+
static void random_test(tlsf *t, size_t spacelen, const size_t cap)
47+
{
4548
const size_t maxitems = 2 * spacelen;
4649

47-
void** p = (void**)malloc(maxitems * sizeof(void*));
50+
void **p = (void **) malloc(maxitems * sizeof(void *));
4851
assert(p);
4952

5053
/*
5154
* Allocate random sizes up to the cap threshold.
5255
* Track them in an array.
5356
*/
54-
int64_t rest = (int64_t)spacelen * (rand() % 6 + 1);
57+
int64_t rest = (int64_t) spacelen * (rand() % 6 + 1);
5558
unsigned i = 0;
5659
while (rest > 0) {
57-
size_t len = ((size_t)rand() % cap) + 1;
60+
size_t len = ((size_t) rand() % cap) + 1;
5861
if (rand() % 2 == 0) {
5962
p[i] = tlsf_malloc(t, len);
6063
} else {
6164
size_t align = 1U << (rand() % 20);
6265
if (cap < align)
6366
align = 0;
6467
else
65-
len = align * (((size_t)rand() % (cap / align)) + 1);
66-
p[i] = !align || !len ? tlsf_malloc(t, len) : tlsf_aalloc(t, align, len);
68+
len = align * (((size_t) rand() % (cap / align)) + 1);
69+
p[i] = !align || !len ? tlsf_malloc(t, len)
70+
: tlsf_aalloc(t, align, len);
6771
if (align)
68-
assert(!((size_t)p[i] % align));
72+
assert(!((size_t) p[i] % align));
6973
}
7074
assert(p[i]);
71-
rest -= (int64_t)len;
75+
rest -= (int64_t) len;
7276

7377
if (rand() % 10 == 0) {
74-
len = ((size_t)rand() % cap) + 1;
78+
len = ((size_t) rand() % cap) + 1;
7579
p[i] = tlsf_realloc(t, p[i], len);
7680
assert(p[i]);
7781
}
7882

7983
tlsf_check(t);
8084

8185
/* Fill with magic (only when testing up to 1MB). */
82-
uint8_t* data = (uint8_t*)p[i];
86+
uint8_t *data = (uint8_t *) p[i];
8387
if (spacelen <= 1024 * 1024)
8488
memset(data, 0, len);
8589
data[0] = 0xa5;
@@ -93,10 +97,10 @@ static void random_test(tlsf* t, size_t spacelen, const size_t cap) {
9397
* The free space should match the free space after initialisation.
9498
*/
9599
for (unsigned n = i; n;) {
96-
size_t target = (size_t)rand() % i;
100+
size_t target = (size_t) rand() % i;
97101
if (p[target] == NULL)
98102
continue;
99-
uint8_t* data = (uint8_t*)p[target];
103+
uint8_t *data = (uint8_t *) p[target];
100104
assert(data[0] == 0xa5);
101105
tlsf_free(t, p[target]);
102106
p[target] = NULL;
@@ -110,27 +114,30 @@ static void random_test(tlsf* t, size_t spacelen, const size_t cap) {
110114

111115
#define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
112116

113-
static void random_sizes_test(tlsf* t) {
114-
const size_t sizes[] = { 16, 32, 64, 128, 256, 512, 1024, 1024 * 1024 }; //, 128 * 1024 * 1024};
117+
static void random_sizes_test(tlsf *t)
118+
{
119+
const size_t sizes[] = {16, 32, 64, 128, 256,
120+
512, 1024, 1024 * 1024}; //, 128 * 1024 * 1024};
115121

116122
for (unsigned i = 0; i < __arraycount(sizes); i++) {
117123
unsigned n = 1024;
118124

119125
while (n--) {
120-
size_t cap = (size_t)rand() % sizes[i] + 1;
126+
size_t cap = (size_t) rand() % sizes[i] + 1;
121127
printf("sizes = %zu, cap = %zu\n", sizes[i], cap);
122128
random_test(t, sizes[i], cap);
123129
}
124130
}
125131
}
126132

127-
static void large_alloc(tlsf* t, size_t s) {
133+
static void large_alloc(tlsf *t, size_t s)
134+
{
128135
printf("large alloc %zu\n", s);
129136
for (size_t d = 0; d < 100 && d < s; ++d) {
130-
void* p = tlsf_malloc(t, s - d);
137+
void *p = tlsf_malloc(t, s - d);
131138
assert(p);
132139

133-
void* q = tlsf_malloc(t, s - d);
140+
void *q = tlsf_malloc(t, s - d);
134141
assert(q);
135142
tlsf_free(t, q);
136143

@@ -143,7 +150,8 @@ static void large_alloc(tlsf* t, size_t s) {
143150
}
144151
}
145152

146-
static void large_size_test(tlsf* t) {
153+
static void large_size_test(tlsf *t)
154+
{
147155
size_t s = 1;
148156
while (s <= TLSF_MAX_SIZE) {
149157
large_alloc(t, s);
@@ -157,11 +165,12 @@ static void large_size_test(tlsf* t) {
157165
}
158166
}
159167

160-
int main(void) {
161-
PAGE = (size_t)sysconf(_SC_PAGESIZE);
168+
int main(void)
169+
{
170+
PAGE = (size_t) sysconf(_SC_PAGESIZE);
162171
MAX_PAGES = 20 * TLSF_MAX_SIZE / PAGE;
163172
tlsf t = TLSF_INIT;
164-
srand((unsigned int)time(0));
173+
srand((unsigned int) time(0));
165174
large_size_test(&t);
166175
random_sizes_test(&t);
167176
puts("OK!");

0 commit comments

Comments
 (0)