Coverage Report

Created: 2025-12-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/mem-pool.c
Line
Count
Source
1
/*
2
 * Memory Pool implementation logic.
3
 */
4
5
#define DISABLE_SIGN_COMPARE_WARNINGS
6
7
#include "git-compat-util.h"
8
#include "mem-pool.h"
9
#include "gettext.h"
10
11
0
#define BLOCK_GROWTH_SIZE (1024 * 1024 - sizeof(struct mp_block))
12
13
/*
14
 * The inner union is an approximation for C11's max_align_t, and the
15
 * struct + offsetof computes _Alignof. This can all just be replaced
16
 * with _Alignof(max_align_t) if/when C11 is part of the baseline.
17
 * Note that _Alignof(X) need not be the same as sizeof(X); it's only
18
 * required to be a (possibly trivial) factor. They are the same for
19
 * most architectures, but m68k for example has only 2-byte alignment
20
 * for its 4-byte and 8-byte types, so using sizeof would waste space.
21
 *
22
 * Add more types to the union if the current set is insufficient.
23
 */
24
struct git_max_alignment {
25
  char unalign;
26
  union {
27
    uintmax_t max_align_uintmax;
28
    void *max_align_pointer;
29
  } aligned;
30
};
31
0
#define GIT_MAX_ALIGNMENT offsetof(struct git_max_alignment, aligned)
32
33
/*
34
 * Allocate a new mp_block and insert it after the block specified in
35
 * `insert_after`. If `insert_after` is NULL, then insert block at the
36
 * head of the linked list.
37
 */
38
static struct mp_block *mem_pool_alloc_block(struct mem_pool *pool,
39
               size_t block_alloc,
40
               struct mp_block *insert_after)
41
0
{
42
0
  struct mp_block *p;
43
44
0
  pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
45
0
  p = xmalloc(st_add(sizeof(struct mp_block), block_alloc));
46
47
0
  p->next_free = (char *)p->space;
48
0
  p->end = p->next_free + block_alloc;
49
50
0
  if (insert_after) {
51
0
    p->next_block = insert_after->next_block;
52
0
    insert_after->next_block = p;
53
0
  } else {
54
0
    p->next_block = pool->mp_block;
55
0
    pool->mp_block = p;
56
0
  }
57
58
0
  return p;
59
0
}
60
61
void mem_pool_init(struct mem_pool *pool, size_t initial_size)
62
0
{
63
0
  memset(pool, 0, sizeof(*pool));
64
0
  pool->block_alloc = BLOCK_GROWTH_SIZE;
65
66
0
  if (initial_size > 0)
67
0
    mem_pool_alloc_block(pool, initial_size, NULL);
68
0
}
69
70
void mem_pool_discard(struct mem_pool *pool, int invalidate_memory)
71
0
{
72
0
  struct mp_block *block, *block_to_free;
73
74
0
  block = pool->mp_block;
75
0
  while (block)
76
0
  {
77
0
    block_to_free = block;
78
0
    block = block->next_block;
79
80
0
    if (invalidate_memory)
81
0
      memset(block_to_free->space, 0xDD, ((char *)block_to_free->end) - ((char *)block_to_free->space));
82
83
0
    free(block_to_free);
84
0
  }
85
86
0
  pool->mp_block = NULL;
87
0
  pool->pool_alloc = 0;
88
0
}
89
90
void *mem_pool_alloc(struct mem_pool *pool, size_t len)
91
0
{
92
0
  struct mp_block *p = NULL;
93
0
  void *r;
94
95
0
  len = DIV_ROUND_UP(len, GIT_MAX_ALIGNMENT) * GIT_MAX_ALIGNMENT;
96
97
0
  if (pool->mp_block &&
98
0
      pool->mp_block->end - pool->mp_block->next_free >= len)
99
0
    p = pool->mp_block;
100
101
0
  if (!p) {
102
0
    if (len >= (pool->block_alloc / 2))
103
0
      p = mem_pool_alloc_block(pool, len, pool->mp_block);
104
0
    else
105
0
      p = mem_pool_alloc_block(pool, pool->block_alloc, NULL);
106
0
  }
107
108
0
  r = p->next_free;
109
0
  p->next_free += len;
110
0
  return r;
111
0
}
112
113
static char *mem_pool_strvfmt(struct mem_pool *pool, const char *fmt,
114
            va_list ap)
115
0
{
116
0
  struct mp_block *block = pool->mp_block;
117
0
  char *next_free = block ? block->next_free : NULL;
118
0
  size_t available = block ? block->end - block->next_free : 0;
119
0
  va_list cp;
120
0
  int len, len2;
121
0
  size_t size;
122
0
  char *ret;
123
124
0
  va_copy(cp, ap);
125
0
  len = vsnprintf(next_free, available, fmt, cp);
126
0
  va_end(cp);
127
0
  if (len < 0)
128
0
    die(_("unable to format message: %s"), fmt);
129
130
0
  size = st_add(len, 1); /* 1 for NUL */
131
0
  ret = mem_pool_alloc(pool, size);
132
133
  /* Shortcut; relies on mem_pool_alloc() not touching buffer contents. */
134
0
  if (ret == next_free)
135
0
    return ret;
136
137
0
  len2 = vsnprintf(ret, size, fmt, ap);
138
0
  if (len2 != len)
139
0
    BUG("your vsnprintf is broken (returns inconsistent lengths)");
140
0
  return ret;
141
0
}
142
143
char *mem_pool_strfmt(struct mem_pool *pool, const char *fmt, ...)
144
0
{
145
0
  va_list ap;
146
0
  char *ret;
147
148
0
  va_start(ap, fmt);
149
0
  ret = mem_pool_strvfmt(pool, fmt, ap);
150
0
  va_end(ap);
151
0
  return ret;
152
0
}
153
154
void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size)
155
0
{
156
0
  size_t len = st_mult(count, size);
157
0
  void *r = mem_pool_alloc(pool, len);
158
0
  memset(r, 0, len);
159
0
  return r;
160
0
}
161
162
char *mem_pool_strdup(struct mem_pool *pool, const char *str)
163
0
{
164
0
  size_t len = strlen(str) + 1;
165
0
  char *ret = mem_pool_alloc(pool, len);
166
167
0
  return memcpy(ret, str, len);
168
0
}
169
170
char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len)
171
0
{
172
0
  char *p = memchr(str, '\0', len);
173
0
  size_t actual_len = (p ? p - str : len);
174
0
  char *ret = mem_pool_alloc(pool, actual_len+1);
175
176
0
  ret[actual_len] = '\0';
177
0
  return memcpy(ret, str, actual_len);
178
0
}
179
180
int mem_pool_contains(struct mem_pool *pool, void *mem)
181
0
{
182
0
  struct mp_block *p;
183
184
  /* Check if memory is allocated in a block */
185
0
  for (p = pool->mp_block; p; p = p->next_block)
186
0
    if ((mem >= ((void *)p->space)) &&
187
0
        (mem < ((void *)p->end)))
188
0
      return 1;
189
190
0
  return 0;
191
0
}
192
193
void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src)
194
0
{
195
0
  struct mp_block *p;
196
197
  /* Append the blocks from src to dst */
198
0
  if (dst->mp_block && src->mp_block) {
199
    /*
200
     * src and dst have blocks, append
201
     * blocks from src to dst.
202
     */
203
0
    p = dst->mp_block;
204
0
    while (p->next_block)
205
0
      p = p->next_block;
206
207
0
    p->next_block = src->mp_block;
208
0
  } else if (src->mp_block) {
209
    /*
210
     * src has blocks, dst is empty.
211
     */
212
0
    dst->mp_block = src->mp_block;
213
0
  } else {
214
    /* src is empty, nothing to do. */
215
0
  }
216
217
0
  dst->pool_alloc += src->pool_alloc;
218
0
  src->pool_alloc = 0;
219
  src->mp_block = NULL;
220
0
}