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
|
/* Copyright (C) CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include "contrib/mempattern.h"
#include "contrib/ucw/mempool.h"
static void mm_nofree(void *p)
{
/* nop */
}
static void *mm_malloc(void *ctx, size_t n)
{
(void)ctx;
return malloc(n);
}
void *mm_alloc(knot_mm_t *mm, size_t size)
{
if (mm) {
return mm->alloc(mm->ctx, size);
} else {
return malloc(size);
}
}
void *mm_calloc(knot_mm_t *mm, size_t nmemb, size_t size)
{
if (nmemb == 0 || size == 0) {
return NULL;
}
if (mm) {
size_t total_size = nmemb * size;
if (total_size / nmemb != size) { // Overflow check
return NULL;
}
void *mem = mm_alloc(mm, total_size);
if (mem == NULL) {
return NULL;
}
return memset(mem, 0, total_size);
} else {
return calloc(nmemb, size);
}
}
void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size)
{
if (mm) {
void *p = mm->alloc(mm->ctx, size);
if (p == NULL) {
return NULL;
} else {
if (what) {
memcpy(p, what,
prev_size < size ? prev_size : size);
}
mm_free(mm, what);
return p;
}
} else {
return realloc(what, size);
}
}
char *mm_strdup(knot_mm_t *mm, const char *s)
{
if (s == NULL) {
return NULL;
}
if (mm) {
size_t len = strlen(s) + 1;
void *mem = mm_alloc(mm, len);
if (mem == NULL) {
return NULL;
}
return memcpy(mem, s, len);
} else {
return strdup(s);
}
}
void mm_free(knot_mm_t *mm, void *what)
{
if (mm) {
if (mm->free) {
mm->free(what);
}
} else {
free(what);
}
}
void mm_ctx_init(knot_mm_t *mm)
{
mm->ctx = NULL;
mm->alloc = mm_malloc;
mm->free = free;
}
void mm_ctx_mempool(knot_mm_t *mm, size_t chunk_size)
{
mm->ctx = mp_new(chunk_size);
mm->alloc = (knot_mm_alloc_t)mp_alloc;
mm->free = mm_nofree;
}
/* Code in addition to Knot's mempattern. */
void *mm_malloc_aligned(void *ctx, size_t n)
{
size_t alignment = (size_t)ctx;
void *res;
int err = posix_memalign(&res, alignment, n);
if (err == 0) {
return res;
} else {
assert(err == -1 && errno == ENOMEM);
return NULL;
}
}
knot_mm_t * mm_ctx_mempool2(size_t chunk_size)
{
knot_mm_t pool_tmp;
mm_ctx_mempool(&pool_tmp, chunk_size);
knot_mm_t *pool = mm_alloc(&pool_tmp, sizeof(*pool));
if (!pool) {
mp_delete(pool_tmp.ctx);
return NULL;
}
memcpy(pool, &pool_tmp, sizeof(*pool));
return pool;
}
|