2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
7 #ifndef INCLUDE_pool_h__
8 #define INCLUDE_pool_h__
13 typedef struct git_pool_page git_pool_page
;
15 #ifndef GIT_DEBUG_POOL
19 * A `git_pool` can be used when you want to cheaply allocate
20 * multiple items of the same type and are willing to free them
21 * all together with a single call. The two most common cases
22 * are a set of fixed size items (such as lots of OIDs) or a
25 * Internally, a `git_pool` allocates pages of memory and then
26 * deals out blocks from the trailing unused portion of each page.
27 * The pages guarantee that the number of actual allocations done
28 * will be much smaller than the number of items needed.
30 * For examples of how to set up a `git_pool` see `git_pool_init`.
33 git_pool_page
*pages
; /* allocated pages */
34 uint32_t item_size
; /* size of single alloc unit in bytes */
35 uint32_t page_size
; /* size of page in bytes */
41 * Debug chunked allocator.
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.
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
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`.
56 git_vector allocations
;
65 * To allocation strings, use like this:
67 * git_pool_init(&string_pool, 1);
68 * my_string = git_pool_strdup(&string_pool, your_string);
70 * To allocate items of fixed size, use like this:
72 * git_pool_init(&pool, sizeof(item));
73 * my_item = git_pool_malloc(&pool, 1);
75 * Of course, you can use this in other ways, but those are the
76 * two most common patterns.
78 extern void git_pool_init(git_pool
*pool
, uint32_t item_size
);
81 * Free all items in pool
83 extern void git_pool_clear(git_pool
*pool
);
86 * Swap two pools with one another
88 extern void git_pool_swap(git_pool
*a
, git_pool
*b
);
91 * Allocate space for one or more items from a pool.
93 extern void *git_pool_malloc(git_pool
*pool
, uint32_t items
);
94 extern void *git_pool_mallocz(git_pool
*pool
, uint32_t items
);
97 * Allocate space and duplicate string data into it.
99 * This is allowed only for pools with item_size == sizeof(char)
101 extern char *git_pool_strndup(git_pool
*pool
, const char *str
, size_t n
);
104 * Allocate space and duplicate a string into it.
106 * This is allowed only for pools with item_size == sizeof(char)
108 extern char *git_pool_strdup(git_pool
*pool
, const char *str
);
111 * Allocate space and duplicate a string into it, NULL is no error.
113 * This is allowed only for pools with item_size == sizeof(char)
115 extern char *git_pool_strdup_safe(git_pool
*pool
, const char *str
);
118 * Allocate space for the concatenation of two strings.
120 * This is allowed only for pools with item_size == sizeof(char)
122 extern char *git_pool_strcat(git_pool
*pool
, const char *a
, const char *b
);
127 #ifndef GIT_DEBUG_POOL
128 extern uint32_t git_pool__open_pages(git_pool
*pool
);
130 extern bool git_pool__ptr_in_pool(git_pool
*pool
, void *ptr
);