]> git.proxmox.com Git - libgit2.git/blobdiff - src/pool.h
New upstream version 1.4.3+dfsg.1
[libgit2.git] / src / pool.h
index 54a2861edcefa8b2dff58c497bd0fcb72566e91c..cecb84665fffb963c1481cc2fa0d4d6912d7ccee 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 the libgit2 contributors
+ * Copyright (C) the libgit2 contributors. All rights reserved.
  *
  * This file is part of libgit2, distributed under the GNU GPL v2 with
  * a Linking Exception. For full terms see the included COPYING file.
@@ -9,8 +9,11 @@
 
 #include "common.h"
 
+#include "vector.h"
+
 typedef struct git_pool_page git_pool_page;
 
+#ifndef GIT_DEBUG_POOL
 /**
  * Chunked allocator.
  *
@@ -28,37 +31,57 @@ typedef struct git_pool_page git_pool_page;
  * For examples of how to set up a `git_pool` see `git_pool_init`.
  */
 typedef struct {
-       git_pool_page *open; /* pages with space left */
-       git_pool_page *full; /* pages with no space left */
-       void *free_list;     /* optional: list of freed blocks */
-       uint32_t item_size;  /* size of single alloc unit in bytes */
-       uint32_t page_size;  /* size of page in bytes */
-       uint32_t items;
-       unsigned has_string_alloc : 1; /* was the strdup function used */
-       unsigned has_multi_item_alloc : 1; /* was items ever > 1 in malloc */
-       unsigned has_large_page_alloc : 1; /* are any pages > page_size */
+       git_pool_page *pages; /* allocated pages */
+       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_STRINGPOOL { 0, 0, 0, 1, 4000, 0, 0, 0, 0 }
+#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 int git_pool_init(
-       git_pool *pool, uint32_t item_size, uint32_t items_per_page);
+extern int git_pool_init(git_pool *pool, size_t item_size);
 
 /**
  * Free all items in pool
@@ -73,7 +96,8 @@ extern void git_pool_swap(git_pool *a, git_pool *b);
 /**
  * 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_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.
@@ -90,36 +114,33 @@ extern char *git_pool_strndup(git_pool *pool, const char *str, size_t n);
 extern char *git_pool_strdup(git_pool *pool, const char *str);
 
 /**
- * Allocate space for the concatenation of two strings.
+ * Allocate space and duplicate a string into it, NULL is no error.
  *
  * This is allowed only for pools with item_size == sizeof(char)
  */
-extern char *git_pool_strcat(git_pool *pool, const char *a, const char *b);
+extern char *git_pool_strdup_safe(git_pool *pool, const char *str);
 
 /**
- * Push a block back onto the free list for the pool.
- *
- * This is allowed only if the item_size is >= sizeof(void*).
+ * Allocate space for the concatenation of two strings.
  *
- * In some cases, it is helpful to "release" an allocated block
- * for reuse.  Pools don't support a general purpose free, but
- * they will keep a simple free blocks linked list provided the
- * native block size is large enough to hold a void pointer
+ * This is allowed only for pools with item_size == sizeof(char)
  */
-extern void git_pool_free(git_pool *pool, void *ptr);
+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);
-
-extern uint32_t git_pool__full_pages(git_pool *pool);
-
+#endif
 extern bool git_pool__ptr_in_pool(git_pool *pool, void *ptr);
 
-extern uint32_t git_pool__system_page_size(void);
-
-extern uint32_t git_pool__suggest_items_per_page(uint32_t item_size);
+/**
+ * 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