Coverage Report

Created: 2025-07-23 07:00

/src/libgit2/src/util/pool.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
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.
6
 */
7
#ifndef INCLUDE_pool_h__
8
#define INCLUDE_pool_h__
9
10
#include "git2_util.h"
11
12
#include "vector.h"
13
14
typedef struct git_pool_page git_pool_page;
15
16
#ifndef GIT_DEBUG_POOL
17
/**
18
 * Chunked allocator.
19
 *
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
24
 * bunch of strings.
25
 *
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.
30
 *
31
 * For examples of how to set up a `git_pool` see `git_pool_init`.
32
 */
33
typedef struct {
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 */
37
} git_pool;
38
39
#define GIT_POOL_INIT { NULL, 0, 0 }
40
41
#else
42
43
/**
44
 * Debug chunked allocator.
45
 *
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.
49
 *
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
52
 * freed.
53
 *
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`.
57
 */
58
typedef struct {
59
  git_vector allocations;
60
  size_t item_size;
61
  size_t page_size;
62
} git_pool;
63
64
#define GIT_POOL_INIT { GIT_VECTOR_INIT, 0, 0 }
65
66
#endif
67
68
/**
69
 * Initialize a pool.
70
 *
71
 * To allocation strings, use like this:
72
 *
73
 *     git_pool_init(&string_pool, 1);
74
 *     my_string = git_pool_strdup(&string_pool, your_string);
75
 *
76
 * To allocate items of fixed size, use like this:
77
 *
78
 *     git_pool_init(&pool, sizeof(item));
79
 *     my_item = git_pool_malloc(&pool, 1);
80
 *
81
 * Of course, you can use this in other ways, but those are the
82
 * two most common patterns.
83
 */
84
extern int git_pool_init(git_pool *pool, size_t item_size);
85
86
GIT_INLINE(bool) git_pool_is_initialized(git_pool *pool)
87
0
{
88
0
  return (pool->item_size > 0);
89
0
}
Unexecuted instantiation: commit_graph_fuzzer.c:git_pool_is_initialized
Unexecuted instantiation: fuzzer_utils.c:git_pool_is_initialized
Unexecuted instantiation: commit_graph.c:git_pool_is_initialized
Unexecuted instantiation: libgit2.c:git_pool_is_initialized
Unexecuted instantiation: merge_driver.c:git_pool_is_initialized
Unexecuted instantiation: merge_file.c:git_pool_is_initialized
Unexecuted instantiation: mwindow.c:git_pool_is_initialized
Unexecuted instantiation: object_api.c:git_pool_is_initialized
Unexecuted instantiation: odb.c:git_pool_is_initialized
Unexecuted instantiation: odb_loose.c:git_pool_is_initialized
Unexecuted instantiation: odb_pack.c:git_pool_is_initialized
Unexecuted instantiation: oid.c:git_pool_is_initialized
Unexecuted instantiation: pack.c:git_pool_is_initialized
Unexecuted instantiation: repository.c:git_pool_is_initialized
Unexecuted instantiation: revparse.c:git_pool_is_initialized
Unexecuted instantiation: revwalk.c:git_pool_is_initialized
Unexecuted instantiation: settings.c:git_pool_is_initialized
Unexecuted instantiation: submodule.c:git_pool_is_initialized
Unexecuted instantiation: tag.c:git_pool_is_initialized
Unexecuted instantiation: smart_protocol.c:git_pool_is_initialized
Unexecuted instantiation: tree.c:git_pool_is_initialized
Unexecuted instantiation: worktree.c:git_pool_is_initialized
Unexecuted instantiation: filebuf.c:git_pool_is_initialized
Unexecuted instantiation: fs_path.c:git_pool_is_initialized
Unexecuted instantiation: futils.c:git_pool_is_initialized
Unexecuted instantiation: pool.c:git_pool_is_initialized
Unexecuted instantiation: attr.c:git_pool_is_initialized
Unexecuted instantiation: attr_file.c:git_pool_is_initialized
Unexecuted instantiation: attrcache.c:git_pool_is_initialized
Unexecuted instantiation: blob.c:git_pool_is_initialized
Unexecuted instantiation: branch.c:git_pool_is_initialized
Unexecuted instantiation: cache.c:git_pool_is_initialized
Unexecuted instantiation: checkout.c:git_pool_is_initialized
Unexecuted instantiation: clone.c:git_pool_is_initialized
Unexecuted instantiation: commit.c:git_pool_is_initialized
Unexecuted instantiation: commit_list.c:git_pool_is_initialized
Unexecuted instantiation: config.c:git_pool_is_initialized
Unexecuted instantiation: config_cache.c:git_pool_is_initialized
Unexecuted instantiation: config_file.c:git_pool_is_initialized
Unexecuted instantiation: config_list.c:git_pool_is_initialized
Unexecuted instantiation: config_parse.c:git_pool_is_initialized
Unexecuted instantiation: config_snapshot.c:git_pool_is_initialized
Unexecuted instantiation: delta.c:git_pool_is_initialized
Unexecuted instantiation: diff.c:git_pool_is_initialized
Unexecuted instantiation: diff_driver.c:git_pool_is_initialized
Unexecuted instantiation: diff_generate.c:git_pool_is_initialized
Unexecuted instantiation: diff_print.c:git_pool_is_initialized
Unexecuted instantiation: diff_tform.c:git_pool_is_initialized
Unexecuted instantiation: email.c:git_pool_is_initialized
Unexecuted instantiation: filter.c:git_pool_is_initialized
Unexecuted instantiation: grafts.c:git_pool_is_initialized
Unexecuted instantiation: hashsig.c:git_pool_is_initialized
Unexecuted instantiation: ident.c:git_pool_is_initialized
Unexecuted instantiation: index.c:git_pool_is_initialized
Unexecuted instantiation: indexer.c:git_pool_is_initialized
Unexecuted instantiation: iterator.c:git_pool_is_initialized
Unexecuted instantiation: mailmap.c:git_pool_is_initialized
Unexecuted instantiation: merge.c:git_pool_is_initialized
Unexecuted instantiation: midx.c:git_pool_is_initialized
Unexecuted instantiation: object.c:git_pool_is_initialized
Unexecuted instantiation: pack-objects.c:git_pool_is_initialized
Unexecuted instantiation: patch.c:git_pool_is_initialized
Unexecuted instantiation: patch_generate.c:git_pool_is_initialized
Unexecuted instantiation: path.c:git_pool_is_initialized
Unexecuted instantiation: pathspec.c:git_pool_is_initialized
Unexecuted instantiation: push.c:git_pool_is_initialized
Unexecuted instantiation: refdb.c:git_pool_is_initialized
Unexecuted instantiation: refdb_fs.c:git_pool_is_initialized
Unexecuted instantiation: reflog.c:git_pool_is_initialized
Unexecuted instantiation: refs.c:git_pool_is_initialized
Unexecuted instantiation: remote.c:git_pool_is_initialized
Unexecuted instantiation: signature.c:git_pool_is_initialized
Unexecuted instantiation: transaction.c:git_pool_is_initialized
Unexecuted instantiation: local.c:git_pool_is_initialized
Unexecuted instantiation: tree-cache.c:git_pool_is_initialized
Unexecuted instantiation: sortedcache.c:git_pool_is_initialized
Unexecuted instantiation: crlf.c:git_pool_is_initialized
Unexecuted instantiation: diff_file.c:git_pool_is_initialized
Unexecuted instantiation: diff_stats.c:git_pool_is_initialized
Unexecuted instantiation: diff_xdiff.c:git_pool_is_initialized
Unexecuted instantiation: fetch.c:git_pool_is_initialized
Unexecuted instantiation: fetchhead.c:git_pool_is_initialized
Unexecuted instantiation: graph.c:git_pool_is_initialized
Unexecuted instantiation: ignore.c:git_pool_is_initialized
Unexecuted instantiation: patch_parse.c:git_pool_is_initialized
Unexecuted instantiation: objects_fuzzer.c:git_pool_is_initialized
Unexecuted instantiation: config_mem.c:git_pool_is_initialized
Unexecuted instantiation: midx_fuzzer.c:git_pool_is_initialized
Unexecuted instantiation: download_refs_fuzzer.c:git_pool_is_initialized
Unexecuted instantiation: odb_mempack.c:git_pool_is_initialized
90
91
/**
92
 * Free all items in pool
93
 */
94
extern void git_pool_clear(git_pool *pool);
95
96
/**
97
 * Swap two pools with one another
98
 */
99
extern void git_pool_swap(git_pool *a, git_pool *b);
100
101
/**
102
 * Allocate space for one or more items from a pool.
103
 */
104
extern void *git_pool_malloc(git_pool *pool, size_t items);
105
extern void *git_pool_mallocz(git_pool *pool, size_t items);
106
107
/**
108
 * Allocate space and duplicate string data into it.
109
 *
110
 * This is allowed only for pools with item_size == sizeof(char)
111
 */
112
extern char *git_pool_strndup(git_pool *pool, const char *str, size_t n);
113
114
/**
115
 * Allocate space and duplicate a string into it.
116
 *
117
 * This is allowed only for pools with item_size == sizeof(char)
118
 */
119
extern char *git_pool_strdup(git_pool *pool, const char *str);
120
121
/**
122
 * Allocate space and duplicate a string into it, NULL is no error.
123
 *
124
 * This is allowed only for pools with item_size == sizeof(char)
125
 */
126
extern char *git_pool_strdup_safe(git_pool *pool, const char *str);
127
128
/**
129
 * Allocate space for the concatenation of two strings.
130
 *
131
 * This is allowed only for pools with item_size == sizeof(char)
132
 */
133
extern char *git_pool_strcat(git_pool *pool, const char *a, const char *b);
134
135
/*
136
 * Misc utilities
137
 */
138
#ifndef GIT_DEBUG_POOL
139
extern uint32_t git_pool__open_pages(git_pool *pool);
140
#endif
141
extern bool git_pool__ptr_in_pool(git_pool *pool, void *ptr);
142
143
/**
144
 * This function is being called by our global setup routines to
145
 * initialize the system pool size.
146
 *
147
 * @return 0 on success, <0 on failure
148
 */
149
extern int git_pool_global_init(void);
150
151
#endif