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>
1615#include <time.h>
1716#include <unistd.h>
1817
18+ #include "tlsf.h"
19+
1920static 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
95112static 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}
0 commit comments