Commit 1aa08fe0 authored by Marek Vavruša's avatar Marek Vavruša

lib/utils: made mm_reserve generic in library

parent 96b345c6
......@@ -23,6 +23,7 @@
#include "lib/defines.h"
#include "lib/utils.h"
#include "lib/generic/array.h"
/*
* Macros.
......@@ -112,4 +113,23 @@ int kr_randseed(char *buf, size_t buflen)
gettimeofday(&tv, NULL);
memcpy(buf, &tv, buflen < sizeof(tv) ? buflen : sizeof(tv));
return 0;
}
\ No newline at end of file
}
int mm_reserve(void *baton, char **mem, size_t elm_size, size_t want, size_t *have)
{
if (*have >= want) {
return 0;
} else {
mm_ctx_t *pool = baton;
size_t next_size = array_next_count(want);
void *mem_new = mm_alloc(pool, next_size * elm_size);
if (mem_new != NULL) {
memcpy(mem_new, *mem, (*have)*(elm_size));
mm_free(pool, *mem);
*mem = mem_new;
*have = next_size;
return 0;
}
}
return -1;
}
......@@ -38,4 +38,7 @@ extern void _cleanup_fclose(FILE **p);
char* kr_strcatdup(unsigned n, ...);
/** Fill buffer with random seed. */
int kr_randseed(char *buf, size_t buflen);
\ No newline at end of file
int kr_randseed(char *buf, size_t buflen);
/** Memory reservation routine for mm_ctx_t */
int mm_reserve(void *baton, char **mem, size_t elm_size, size_t want, size_t *have);
......@@ -52,25 +52,6 @@ static const struct hint_info SBELT[HINT_COUNT] = {
{ U8("\x01""m""\x0c""root-servers""\x03""net"), U8("\xca\x0c\x1b!") }, /* 202.12.27.33 */
};
static inline int nsset_reserve(void *baton, char **mem, size_t elm_size, size_t want, size_t *have)
{
if (*have >= want) {
return 0;
} else {
mm_ctx_t *pool = baton;
size_t next_size = (want + 3);
void *mem_new = mm_alloc(pool, next_size * elm_size);
if (mem_new != NULL) {
memcpy(mem_new, *mem, (*have)*(elm_size));
mm_free(pool, *mem);
*mem = mem_new;
*have = next_size;
return 0;
}
}
return -1;
}
static void update_cut_name(struct kr_zonecut *cut, const knot_dname_t *name)
{
if (knot_dname_is_equal(name, cut->name)) {
......@@ -145,7 +126,7 @@ int kr_zonecut_add(struct kr_zonecut *cut, const knot_dname_t *ns, const knot_rd
return kr_ok();
}
uint16_t rdlen = knot_rdata_rdlen(rdata);
int ret = pack_reserve_mm(*pack, 1, rdlen, nsset_reserve, cut->pool);
int ret = pack_reserve_mm(*pack, 1, rdlen, mm_reserve, cut->pool);
if (ret != 0) {
return kr_error(ENOMEM);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment