Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6cc4bac

Browse files
author
Edward Thomson
committed
Merge pull request libgit2#3577 from rossdylan/rossdylan/pooldebug
Add a new build flag to disable the pool allocator
2 parents 9f4e7c8 + 93e1664 commit 6cc4bac

File tree

4 files changed

+117
-30
lines changed

4 files changed

+117
-30
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ OPTION( USE_SSH "Link with libssh to enable SSH support" ON )
4141
OPTION( USE_GSSAPI "Link with libgssapi for SPNEGO auth" OFF )
4242
OPTION( VALGRIND "Configure build for valgrind" OFF )
4343
OPTION( CURL "User curl for HTTP if available" ON)
44+
OPTION( DEBUG_POOL "Enable debug pool allocator" OFF )
45+
46+
IF(DEBUG_POOL)
47+
ADD_DEFINITIONS(-DGIT_DEBUG_POOL)
48+
ENDIF()
4449

4550
IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
4651
SET( USE_ICONV ON )

src/pool.c

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ uint32_t git_pool__system_page_size(void)
2828
return size;
2929
}
3030

31+
#ifndef GIT_DEBUG_POOL
3132
void git_pool_init(git_pool *pool, uint32_t item_size)
3233
{
3334
assert(pool);
@@ -50,18 +51,6 @@ void git_pool_clear(git_pool *pool)
5051
pool->pages = NULL;
5152
}
5253

53-
void git_pool_swap(git_pool *a, git_pool *b)
54-
{
55-
git_pool temp;
56-
57-
if (a == b)
58-
return;
59-
60-
memcpy(&temp, a, sizeof(temp));
61-
memcpy(a, b, sizeof(temp));
62-
memcpy(b, &temp, sizeof(temp));
63-
}
64-
6554
static void *pool_alloc_page(git_pool *pool, uint32_t size)
6655
{
6756
git_pool_page *page;
@@ -95,6 +84,83 @@ static void *pool_alloc(git_pool *pool, uint32_t size)
9584
return ptr;
9685
}
9786

87+
uint32_t git_pool__open_pages(git_pool *pool)
88+
{
89+
uint32_t ct = 0;
90+
git_pool_page *scan;
91+
for (scan = pool->pages; scan != NULL; scan = scan->next) ct++;
92+
return ct;
93+
}
94+
95+
bool git_pool__ptr_in_pool(git_pool *pool, void *ptr)
96+
{
97+
git_pool_page *scan;
98+
for (scan = pool->pages; scan != NULL; scan = scan->next)
99+
if ((void *)scan->data <= ptr &&
100+
(void *)(((char *)scan->data) + scan->size) > ptr)
101+
return true;
102+
return false;
103+
}
104+
105+
#else
106+
107+
static int git_pool__ptr_cmp(const void * a, const void * b)
108+
{
109+
if(a > b) {
110+
return 1;
111+
}
112+
if(a < b) {
113+
return -1;
114+
}
115+
else {
116+
return 0;
117+
}
118+
}
119+
120+
void git_pool_init(git_pool *pool, uint32_t item_size)
121+
{
122+
assert(pool);
123+
assert(item_size >= 1);
124+
125+
memset(pool, 0, sizeof(git_pool));
126+
pool->item_size = item_size;
127+
pool->page_size = git_pool__system_page_size();
128+
git_vector_init(&pool->allocations, 100, git_pool__ptr_cmp);
129+
}
130+
131+
void git_pool_clear(git_pool *pool)
132+
{
133+
git_vector_free_deep(&pool->allocations);
134+
}
135+
136+
static void *pool_alloc(git_pool *pool, uint32_t size) {
137+
void *ptr = NULL;
138+
if((ptr = git__malloc(size)) == NULL) {
139+
return NULL;
140+
}
141+
git_vector_insert_sorted(&pool->allocations, ptr, NULL);
142+
return ptr;
143+
}
144+
145+
bool git_pool__ptr_in_pool(git_pool *pool, void *ptr)
146+
{
147+
size_t pos;
148+
return git_vector_bsearch(&pos, &pool->allocations, ptr) != GIT_ENOTFOUND;
149+
}
150+
#endif
151+
152+
void git_pool_swap(git_pool *a, git_pool *b)
153+
{
154+
git_pool temp;
155+
156+
if (a == b)
157+
return;
158+
159+
memcpy(&temp, a, sizeof(temp));
160+
memcpy(a, b, sizeof(temp));
161+
memcpy(b, &temp, sizeof(temp));
162+
}
163+
98164
static uint32_t alloc_size(git_pool *pool, uint32_t count)
99165
{
100166
const uint32_t align = sizeof(void *) - 1;
@@ -168,21 +234,3 @@ char *git_pool_strcat(git_pool *pool, const char *a, const char *b)
168234
}
169235
return ptr;
170236
}
171-
172-
uint32_t git_pool__open_pages(git_pool *pool)
173-
{
174-
uint32_t ct = 0;
175-
git_pool_page *scan;
176-
for (scan = pool->pages; scan != NULL; scan = scan->next) ct++;
177-
return ct;
178-
}
179-
180-
bool git_pool__ptr_in_pool(git_pool *pool, void *ptr)
181-
{
182-
git_pool_page *scan;
183-
for (scan = pool->pages; scan != NULL; scan = scan->next)
184-
if ((void *)scan->data <= ptr &&
185-
(void *)(((char *)scan->data) + scan->size) > ptr)
186-
return true;
187-
return false;
188-
}

src/pool.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
#define INCLUDE_pool_h__
99

1010
#include "common.h"
11+
#include "vector.h"
1112

1213
typedef struct git_pool_page git_pool_page;
1314

15+
#ifndef GIT_DEBUG_POOL
1416
/**
1517
* Chunked allocator.
1618
*
@@ -33,6 +35,30 @@ typedef struct {
3335
uint32_t page_size; /* size of page in bytes */
3436
} git_pool;
3537

38+
#else
39+
40+
/**
41+
* Debug chunked allocator.
42+
*
43+
* Acts just like `git_pool` but instead of actually pooling allocations it
44+
* passes them through to `git__malloc`. This makes it possible to easily debug
45+
* systems that use `git_pool` using valgrind.
46+
*
47+
* In order to track allocations during the lifetime of the pool we use a
48+
* `git_vector`. When the pool is deallocated everything in the vector is
49+
* freed.
50+
*
51+
* `API is exactly the same as the standard `git_pool` with one exception.
52+
* Since we aren't allocating pages to hand out in chunks we can't easily
53+
* implement `git_pool__open_pages`.
54+
*/
55+
typedef struct {
56+
git_vector allocations;
57+
uint32_t item_size;
58+
uint32_t page_size;
59+
} git_pool;
60+
#endif
61+
3662
/**
3763
* Initialize a pool.
3864
*
@@ -98,7 +124,9 @@ extern char *git_pool_strcat(git_pool *pool, const char *a, const char *b);
98124
/*
99125
* Misc utilities
100126
*/
127+
#ifndef GIT_DEBUG_POOL
101128
extern uint32_t git_pool__open_pages(git_pool *pool);
129+
#endif
102130
extern bool git_pool__ptr_in_pool(git_pool *pool, void *ptr);
103131

104132
#endif

tests/core/pool.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ void test_core_pool__1(void)
3131
for (i = 2010; i > 0; i--)
3232
cl_assert(git_pool_malloc(&p, i) != NULL);
3333

34+
#ifndef GIT_DEBUG_POOL
3435
/* with fixed page size, allocation must end up with these values */
3536
cl_assert_equal_i(591, git_pool__open_pages(&p));
37+
#endif
3638
git_pool_clear(&p);
3739

3840
git_pool_init(&p, 1);
@@ -41,8 +43,10 @@ void test_core_pool__1(void)
4143
for (i = 2010; i > 0; i--)
4244
cl_assert(git_pool_malloc(&p, i) != NULL);
4345

46+
#ifndef GIT_DEBUG_POOL
4447
/* with fixed page size, allocation must end up with these values */
4548
cl_assert_equal_i(sizeof(void *) == 8 ? 575 : 573, git_pool__open_pages(&p));
49+
#endif
4650
git_pool_clear(&p);
4751
}
4852

@@ -69,8 +73,10 @@ void test_core_pool__2(void)
6973
cl_git_pass(git_oid_fromstr(oid, oid_hex));
7074
}
7175

76+
#ifndef GIT_DEBUG_POOL
7277
/* with fixed page size, allocation must end up with these values */
7378
cl_assert_equal_i(sizeof(void *) == 8 ? 55 : 45, git_pool__open_pages(&p));
79+
#endif
7480
git_pool_clear(&p);
7581
}
7682

0 commit comments

Comments
 (0)