File tree Expand file tree Collapse file tree 3 files changed +36
-6
lines changed Expand file tree Collapse file tree 3 files changed +36
-6
lines changed Original file line number Diff line number Diff line change @@ -1340,6 +1340,7 @@ THIRD_PARTY_SOURCES += sha1collisiondetection/%
1340
1340
THIRD_PARTY_SOURCES += sha1dc/%
1341
1341
1342
1342
UNIT_TEST_PROGRAMS += t-basic
1343
+ UNIT_TEST_PROGRAMS += t-mem-pool
1343
1344
UNIT_TEST_PROGRAMS += t-strbuf
1344
1345
UNIT_TEST_PROGS = $(patsubst % ,$(UNIT_TEST_BIN ) /% $X,$(UNIT_TEST_PROGRAMS ) )
1345
1346
UNIT_TEST_OBJS = $(patsubst % ,$(UNIT_TEST_DIR ) /% .o,$(UNIT_TEST_PROGRAMS ) )
Original file line number Diff line number Diff line change @@ -89,19 +89,17 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len)
89
89
struct mp_block * p = NULL ;
90
90
void * r ;
91
91
92
- /* round up to a 'GIT_MAX_ALIGNMENT' alignment */
93
- if (len & (GIT_MAX_ALIGNMENT - 1 ))
94
- len += GIT_MAX_ALIGNMENT - (len & (GIT_MAX_ALIGNMENT - 1 ));
92
+ len = DIV_ROUND_UP (len , GIT_MAX_ALIGNMENT ) * GIT_MAX_ALIGNMENT ;
95
93
96
94
if (pool -> mp_block &&
97
95
pool -> mp_block -> end - pool -> mp_block -> next_free >= len )
98
96
p = pool -> mp_block ;
99
97
100
98
if (!p ) {
101
99
if (len >= (pool -> block_alloc / 2 ))
102
- return mem_pool_alloc_block (pool , len , pool -> mp_block );
103
-
104
- p = mem_pool_alloc_block (pool , pool -> block_alloc , NULL );
100
+ p = mem_pool_alloc_block (pool , len , pool -> mp_block );
101
+ else
102
+ p = mem_pool_alloc_block (pool , pool -> block_alloc , NULL );
105
103
}
106
104
107
105
r = p -> next_free ;
Original file line number Diff line number Diff line change
1
+ #include "test-lib.h"
2
+ #include "mem-pool.h"
3
+
4
+ static void setup_static (void (* f )(struct mem_pool * ), size_t block_alloc )
5
+ {
6
+ struct mem_pool pool = { .block_alloc = block_alloc };
7
+ f (& pool );
8
+ mem_pool_discard (& pool , 0 );
9
+ }
10
+
11
+ static void t_calloc_100 (struct mem_pool * pool )
12
+ {
13
+ size_t size = 100 ;
14
+ char * buffer = mem_pool_calloc (pool , 1 , size );
15
+ for (size_t i = 0 ; i < size ; i ++ )
16
+ check_int (buffer [i ], = = , 0 );
17
+ if (!check (pool -> mp_block != NULL ))
18
+ return ;
19
+ check (pool -> mp_block -> next_free != NULL );
20
+ check (pool -> mp_block -> end != NULL );
21
+ }
22
+
23
+ int cmd_main (int argc , const char * * argv )
24
+ {
25
+ TEST (setup_static (t_calloc_100 , 1024 * 1024 ),
26
+ "mem_pool_calloc returns 100 zeroed bytes with big block" );
27
+ TEST (setup_static (t_calloc_100 , 1 ),
28
+ "mem_pool_calloc returns 100 zeroed bytes with tiny block" );
29
+
30
+ return test_done ();
31
+ }
You can’t perform that action at this time.
0 commit comments