#include "common.h"
+#include "vector.h"
+
typedef struct git_pool_page git_pool_page;
+#ifndef GIT_DEBUG_POOL
/**
* Chunked allocator.
*
*/
typedef struct {
git_pool_page *pages; /* allocated pages */
- uint32_t item_size; /* size of single alloc unit in bytes */
- uint32_t page_size; /* size of page in bytes */
+ size_t item_size; /* size of single alloc unit in bytes */
+ size_t page_size; /* size of page in bytes */
} git_pool;
+#define GIT_POOL_INIT { NULL, 0, 0 }
+
+#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;
+ size_t item_size;
+ size_t page_size;
+} git_pool;
+
+#define GIT_POOL_INIT { GIT_VECTOR_INIT, 0, 0 }
+
+#endif
+
/**
* Initialize a pool.
*
* To allocation strings, use like this:
*
- * git_pool_init(&string_pool, 1, 0);
+ * git_pool_init(&string_pool, 1);
* my_string = git_pool_strdup(&string_pool, your_string);
*
* To allocate items of fixed size, use like this:
*
- * git_pool_init(&pool, sizeof(item), 0);
+ * git_pool_init(&pool, sizeof(item));
* my_item = git_pool_malloc(&pool, 1);
*
* Of course, you can use this in other ways, but those are the
* two most common patterns.
*/
-extern void git_pool_init(git_pool *pool, uint32_t item_size);
+extern int git_pool_init(git_pool *pool, size_t item_size);
/**
* Free all items in pool
/**
* Allocate space for one or more items from a pool.
*/
-extern void *git_pool_malloc(git_pool *pool, uint32_t items);
-extern void *git_pool_mallocz(git_pool *pool, uint32_t items);
+extern void *git_pool_malloc(git_pool *pool, size_t items);
+extern void *git_pool_mallocz(git_pool *pool, size_t items);
/**
* Allocate space and duplicate string data into it.
/*
* 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);
+/**
+ * This function is being called by our global setup routines to
+ * initialize the system pool size.
+ *
+ * @return 0 on success, <0 on failure
+ */
+extern int git_pool_global_init(void);
+
#endif