1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
#include "pool.h"
#include "posix.h"
#ifndef GIT_WIN32
#include <unistd.h>
#endif
struct git_pool_page {
git_pool_page *next;
uint32_t size;
uint32_t avail;
char data[GIT_FLEX_ARRAY];
};
static void *pool_alloc_page(git_pool *pool, uint32_t size);
uint32_t git_pool__system_page_size(void)
{
static uint32_t size = 0;
if (!size) {
size_t page_size;
if (git__page_size(&page_size) < 0)
page_size = 4096;
size = page_size - 2 * sizeof(void *); /* allow space for malloc overhead */
}
return size;
}
void git_pool_init(git_pool *pool, uint32_t item_size)
{
const uint32_t align_size = sizeof(void *) - 1;
assert(pool);
if (item_size > 1)
item_size = (item_size + align_size) & ~align_size;
memset(pool, 0, sizeof(git_pool));
pool->item_size = item_size;
pool->page_size = git_pool__system_page_size();
}
void git_pool_clear(git_pool *pool)
{
git_pool_page *scan, *next;
for (scan = pool->pages; scan != NULL; scan = next) {
next = scan->next;
git__free(scan);
}
pool->pages = NULL;
pool->items = 0;
}
void git_pool_swap(git_pool *a, git_pool *b)
{
git_pool temp;
if (a == b)
return;
memcpy(&temp, a, sizeof(temp));
memcpy(a, b, sizeof(temp));
memcpy(b, &temp, sizeof(temp));
}
static void *pool_alloc_page(git_pool *pool, uint32_t size)
{
git_pool_page *page;
const uint32_t new_page_size = (size <= pool->page_size) ? pool->page_size : size;
size_t alloc_size;
if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, new_page_size, sizeof(git_pool_page)) ||
!(page = git__calloc(1, alloc_size)))
return NULL;
page->size = new_page_size;
page->avail = new_page_size - size;
page->next = pool->pages;
pool->pages = page;
pool->items++;
return page->data;
}
void *git_pool_malloc(git_pool *pool, uint32_t items)
{
const uint32_t size = items * pool->item_size;
git_pool_page *page = pool->pages;
void *ptr = NULL;
if (!page || page->avail < size)
return pool_alloc_page(pool, size);
ptr = &page->data[page->size - page->avail];
page->avail -= size;
pool->items++;
return ptr;
}
char *git_pool_strndup(git_pool *pool, const char *str, size_t n)
{
char *ptr = NULL;
assert(pool && str && pool->item_size == sizeof(char));
if ((uint32_t)(n + 1) < n)
return NULL;
if ((ptr = git_pool_malloc(pool, (uint32_t)(n + 1))) != NULL) {
memcpy(ptr, str, n);
ptr[n] = '\0';
}
return ptr;
}
char *git_pool_strdup(git_pool *pool, const char *str)
{
assert(pool && str && pool->item_size == sizeof(char));
return git_pool_strndup(pool, str, strlen(str));
}
char *git_pool_strdup_safe(git_pool *pool, const char *str)
{
return str ? git_pool_strdup(pool, str) : NULL;
}
char *git_pool_strcat(git_pool *pool, const char *a, const char *b)
{
void *ptr;
size_t len_a, len_b;
assert(pool && pool->item_size == sizeof(char));
len_a = a ? strlen(a) : 0;
len_b = b ? strlen(b) : 0;
if ((ptr = git_pool_malloc(pool, (uint32_t)(len_a + len_b + 1))) != NULL) {
if (len_a)
memcpy(ptr, a, len_a);
if (len_b)
memcpy(((char *)ptr) + len_a, b, len_b);
*(((char *)ptr) + len_a + len_b) = '\0';
}
return ptr;
}
uint32_t git_pool__open_pages(git_pool *pool)
{
uint32_t ct = 0;
git_pool_page *scan;
for (scan = pool->pages; scan != NULL; scan = scan->next) ct++;
return ct;
}
bool git_pool__ptr_in_pool(git_pool *pool, void *ptr)
{
git_pool_page *scan;
for (scan = pool->pages; scan != NULL; scan = scan->next)
if ((void *)scan->data <= ptr &&
(void *)(((char *)scan->data) + scan->size) > ptr)
return true;
return false;
}