Skip to content

Commit

Permalink
Merge pull request #3577 from rossdylan/rossdylan/pooldebug
Browse files Browse the repository at this point in the history
Add a new build flag to disable the pool allocator
  • Loading branch information
Edward Thomson committed Feb 28, 2016
2 parents 9f4e7c8 + 93e1664 commit 6cc4bac
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 30 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ OPTION( USE_SSH "Link with libssh to enable SSH support" ON )
OPTION( USE_GSSAPI "Link with libgssapi for SPNEGO auth" OFF )
OPTION( VALGRIND "Configure build for valgrind" OFF )
OPTION( CURL "User curl for HTTP if available" ON)
OPTION( DEBUG_POOL "Enable debug pool allocator" OFF )

IF(DEBUG_POOL)
ADD_DEFINITIONS(-DGIT_DEBUG_POOL)
ENDIF()

IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
SET( USE_ICONV ON )
Expand Down
108 changes: 78 additions & 30 deletions src/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ uint32_t git_pool__system_page_size(void)
return size;
}

#ifndef GIT_DEBUG_POOL
void git_pool_init(git_pool *pool, uint32_t item_size)
{
assert(pool);
Expand All @@ -50,18 +51,6 @@ void git_pool_clear(git_pool *pool)
pool->pages = NULL;
}

void git_pool_swap(git_pool *a, git_pool *b)
{
git_pool temp;

if (a == b)
return;

memcpy(&temp, a, sizeof(temp));
memcpy(a, b, sizeof(temp));
memcpy(b, &temp, sizeof(temp));
}

static void *pool_alloc_page(git_pool *pool, uint32_t size)
{
git_pool_page *page;
Expand Down Expand Up @@ -95,6 +84,83 @@ static void *pool_alloc(git_pool *pool, uint32_t size)
return ptr;
}

uint32_t git_pool__open_pages(git_pool *pool)
{
uint32_t ct = 0;
git_pool_page *scan;
for (scan = pool->pages; scan != NULL; scan = scan->next) ct++;
return ct;
}

bool git_pool__ptr_in_pool(git_pool *pool, void *ptr)
{
git_pool_page *scan;
for (scan = pool->pages; scan != NULL; scan = scan->next)
if ((void *)scan->data <= ptr &&
(void *)(((char *)scan->data) + scan->size) > ptr)
return true;
return false;
}

#else

static int git_pool__ptr_cmp(const void * a, const void * b)
{
if(a > b) {
return 1;
}
if(a < b) {
return -1;
}
else {
return 0;
}
}

void git_pool_init(git_pool *pool, uint32_t item_size)
{
assert(pool);
assert(item_size >= 1);

memset(pool, 0, sizeof(git_pool));
pool->item_size = item_size;
pool->page_size = git_pool__system_page_size();
git_vector_init(&pool->allocations, 100, git_pool__ptr_cmp);
}

void git_pool_clear(git_pool *pool)
{
git_vector_free_deep(&pool->allocations);
}

static void *pool_alloc(git_pool *pool, uint32_t size) {
void *ptr = NULL;
if((ptr = git__malloc(size)) == NULL) {
return NULL;
}
git_vector_insert_sorted(&pool->allocations, ptr, NULL);
return ptr;
}

bool git_pool__ptr_in_pool(git_pool *pool, void *ptr)
{
size_t pos;
return git_vector_bsearch(&pos, &pool->allocations, ptr) != GIT_ENOTFOUND;
}
#endif

void git_pool_swap(git_pool *a, git_pool *b)
{
git_pool temp;

if (a == b)
return;

memcpy(&temp, a, sizeof(temp));
memcpy(a, b, sizeof(temp));
memcpy(b, &temp, sizeof(temp));
}

static uint32_t alloc_size(git_pool *pool, uint32_t count)
{
const uint32_t align = sizeof(void *) - 1;
Expand Down Expand Up @@ -168,21 +234,3 @@ char *git_pool_strcat(git_pool *pool, const char *a, const char *b)
}
return ptr;
}

uint32_t git_pool__open_pages(git_pool *pool)
{
uint32_t ct = 0;
git_pool_page *scan;
for (scan = pool->pages; scan != NULL; scan = scan->next) ct++;
return ct;
}

bool git_pool__ptr_in_pool(git_pool *pool, void *ptr)
{
git_pool_page *scan;
for (scan = pool->pages; scan != NULL; scan = scan->next)
if ((void *)scan->data <= ptr &&
(void *)(((char *)scan->data) + scan->size) > ptr)
return true;
return false;
}
28 changes: 28 additions & 0 deletions src/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#define INCLUDE_pool_h__

#include "common.h"
#include "vector.h"

typedef struct git_pool_page git_pool_page;

#ifndef GIT_DEBUG_POOL
/**
* Chunked allocator.
*
Expand All @@ -33,6 +35,30 @@ typedef struct {
uint32_t page_size; /* size of page in bytes */
} git_pool;

#else

/**
* Debug chunked allocator.
*
* Acts just like `git_pool` but instead of actually pooling allocations it
* passes them through to `git__malloc`. This makes it possible to easily debug
* systems that use `git_pool` using valgrind.
*
* In order to track allocations during the lifetime of the pool we use a
* `git_vector`. When the pool is deallocated everything in the vector is
* freed.
*
* `API is exactly the same as the standard `git_pool` with one exception.
* Since we aren't allocating pages to hand out in chunks we can't easily
* implement `git_pool__open_pages`.
*/
typedef struct {
git_vector allocations;
uint32_t item_size;
uint32_t page_size;
} git_pool;
#endif

/**
* Initialize a pool.
*
Expand Down Expand Up @@ -98,7 +124,9 @@ extern char *git_pool_strcat(git_pool *pool, const char *a, const char *b);
/*
* Misc utilities
*/
#ifndef GIT_DEBUG_POOL
extern uint32_t git_pool__open_pages(git_pool *pool);
#endif
extern bool git_pool__ptr_in_pool(git_pool *pool, void *ptr);

#endif
6 changes: 6 additions & 0 deletions tests/core/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ void test_core_pool__1(void)
for (i = 2010; i > 0; i--)
cl_assert(git_pool_malloc(&p, i) != NULL);

#ifndef GIT_DEBUG_POOL
/* with fixed page size, allocation must end up with these values */
cl_assert_equal_i(591, git_pool__open_pages(&p));
#endif
git_pool_clear(&p);

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

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

Expand All @@ -69,8 +73,10 @@ void test_core_pool__2(void)
cl_git_pass(git_oid_fromstr(oid, oid_hex));
}

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

Expand Down

0 comments on commit 6cc4bac

Please sign in to comment.