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__
14 typedef struct git_pool_page git_pool_page
;
16 #ifndef GIT_DEBUG_POOL
20 * A `git_pool` can be used when you want to cheaply allocate
21 * multiple items of the same type and are willing to free them
22 * all together with a single call. The two most common cases
23 * are a set of fixed size items (such as lots of OIDs) or a
26 * Internally, a `git_pool` allocates pages of memory and then
27 * deals out blocks from the trailing unused portion of each page.
28 * The pages guarantee that the number of actual allocations done
29 * will be much smaller than the number of items needed.
31 * For examples of how to set up a `git_pool` see `git_pool_init`.
34 git_pool_page
*pages
; /* allocated pages */
35 size_t item_size
; /* size of single alloc unit in bytes */
36 size_t page_size
; /* size of page in bytes */
39 #define GIT_POOL_INIT { NULL, 0, 0 }
44 * Debug chunked allocator.
46 * Acts just like `git_pool` but instead of actually pooling allocations it
47 * passes them through to `git__malloc`. This makes it possible to easily debug
48 * systems that use `git_pool` using valgrind.
50 * In order to track allocations during the lifetime of the pool we use a
51 * `git_vector`. When the pool is deallocated everything in the vector is
54 * `API is exactly the same as the standard `git_pool` with one exception.
55 * Since we aren't allocating pages to hand out in chunks we can't easily
56 * implement `git_pool__open_pages`.
59 git_vector allocations
;
64 #define GIT_POOL_INIT { GIT_VECTOR_INIT, 0, 0 }
71 * To allocation strings, use like this:
73 * git_pool_init(&string_pool, 1);
74 * my_string = git_pool_strdup(&string_pool, your_string);
76 * To allocate items of fixed size, use like this:
78 * git_pool_init(&pool, sizeof(item));
79 * my_item = git_pool_malloc(&pool, 1);
81 * Of course, you can use this in other ways, but those are the
82 * two most common patterns.
84 extern int git_pool_init(git_pool
*pool
, size_t item_size
);
87 * Free all items in pool
89 extern void git_pool_clear(git_pool
*pool
);
92 * Swap two pools with one another
94 extern void git_pool_swap(git_pool
*a
, git_pool
*b
);
97 * Allocate space for one or more items from a pool.
99 extern void *git_pool_malloc(git_pool
*pool
, size_t items
);
100 extern void *git_pool_mallocz(git_pool
*pool
, size_t items
);
103 * Allocate space and duplicate string data into it.
105 * This is allowed only for pools with item_size == sizeof(char)
107 extern char *git_pool_strndup(git_pool
*pool
, const char *str
, size_t n
);
110 * Allocate space and duplicate a string into it.
112 * This is allowed only for pools with item_size == sizeof(char)
114 extern char *git_pool_strdup(git_pool
*pool
, const char *str
);
117 * Allocate space and duplicate a string into it, NULL is no error.
119 * This is allowed only for pools with item_size == sizeof(char)
121 extern char *git_pool_strdup_safe(git_pool
*pool
, const char *str
);
124 * Allocate space for the concatenation of two strings.
126 * This is allowed only for pools with item_size == sizeof(char)
128 extern char *git_pool_strcat(git_pool
*pool
, const char *a
, const char *b
);
133 #ifndef GIT_DEBUG_POOL
134 extern uint32_t git_pool__open_pages(git_pool
*pool
);
136 extern bool git_pool__ptr_in_pool(git_pool
*pool
, void *ptr
);
139 * This function is being called by our global setup routines to
140 * initialize the system pool size.
142 * @return 0 on success, <0 on failure
144 extern int git_pool_global_init(void);