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__
12 typedef struct git_pool_page git_pool_page
;
17 * A `git_pool` can be used when you want to cheaply allocate
18 * multiple items of the same type and are willing to free them
19 * all together with a single call. The two most common cases
20 * are a set of fixed size items (such as lots of OIDs) or a
23 * Internally, a `git_pool` allocates pages of memory and then
24 * deals out blocks from the trailing unused portion of each page.
25 * The pages guarantee that the number of actual allocations done
26 * will be much smaller than the number of items needed.
28 * For examples of how to set up a `git_pool` see `git_pool_init`.
31 git_pool_page
*pages
; /* allocated pages */
32 uint32_t item_size
; /* size of single alloc unit in bytes */
33 uint32_t page_size
; /* size of page in bytes */
39 * To allocation strings, use like this:
41 * git_pool_init(&string_pool, 1);
42 * my_string = git_pool_strdup(&string_pool, your_string);
44 * To allocate items of fixed size, use like this:
46 * git_pool_init(&pool, sizeof(item));
47 * my_item = git_pool_malloc(&pool, 1);
49 * Of course, you can use this in other ways, but those are the
50 * two most common patterns.
52 extern void git_pool_init(git_pool
*pool
, uint32_t item_size
);
55 * Free all items in pool
57 extern void git_pool_clear(git_pool
*pool
);
60 * Swap two pools with one another
62 extern void git_pool_swap(git_pool
*a
, git_pool
*b
);
65 * Allocate space for one or more items from a pool.
67 extern void *git_pool_malloc(git_pool
*pool
, uint32_t items
);
68 extern void *git_pool_mallocz(git_pool
*pool
, uint32_t items
);
71 * Allocate space and duplicate string data into it.
73 * This is allowed only for pools with item_size == sizeof(char)
75 extern char *git_pool_strndup(git_pool
*pool
, const char *str
, size_t n
);
78 * Allocate space and duplicate a string into it.
80 * This is allowed only for pools with item_size == sizeof(char)
82 extern char *git_pool_strdup(git_pool
*pool
, const char *str
);
85 * Allocate space and duplicate a string into it, NULL is no error.
87 * This is allowed only for pools with item_size == sizeof(char)
89 extern char *git_pool_strdup_safe(git_pool
*pool
, const char *str
);
92 * Allocate space for the concatenation of two strings.
94 * This is allowed only for pools with item_size == sizeof(char)
96 extern char *git_pool_strcat(git_pool
*pool
, const char *a
, const char *b
);
101 extern uint32_t git_pool__open_pages(git_pool
*pool
);
102 extern bool git_pool__ptr_in_pool(git_pool
*pool
, void *ptr
);