Custom AVL elem struct size

This commit is contained in:
ruti 2024-12-16 19:32:07 +03:00
parent fdb91ecf17
commit c86ba13937
4 changed files with 42 additions and 36 deletions

View File

@ -77,7 +77,7 @@ static int cache_get(const struct sockaddr_ina *dst)
uint8_t key[KEY_SIZE] = { 0 };
int len = serialize_addr(dst, key, sizeof(key));
struct elem *val = mem_get(params.mempool, (char *)key, len);
struct elem_i *val = (struct elem_i *)mem_get(params.mempool, (char *)key, len);
if (!val) {
return -1;
}
@ -106,7 +106,7 @@ static int cache_add(const struct sockaddr_ina *dst, int m)
LOG(LOG_S, "save ip: %s, m=%d\n", ADDR_STR, m);
time_t t = time(0);
struct elem *val = mem_add(params.mempool, (char *)key, len);
struct elem_i *val = (struct elem_i *)mem_add(params.mempool, (char *)key, len, sizeof(struct elem_i));
if (!val) {
uniperror("mem_add");
return -1;

2
main.c
View File

@ -306,7 +306,7 @@ struct mphdr *parse_hosts(char *buffer, size_t size)
s++;
continue;
}
if (mem_add(hdr, s, e - s) == 0) {
if (mem_add(hdr, s, e - s, sizeof(struct elem)) == 0) {
free(hdr);
return 0;
}

34
mpool.c
View File

@ -15,39 +15,39 @@ static inline int scmp(const struct elem *p, const struct elem *q)
KAVL_INIT(my, struct elem, head, scmp)
struct mphdr *mem_pool(bool cst)
struct mphdr *mem_pool(bool is_static)
{
struct mphdr *hdr = calloc(sizeof(struct mphdr), 1);
if (hdr) {
hdr->stat = cst;
hdr->static_data = is_static;
}
return hdr;
}
struct elem *mem_get(struct mphdr *hdr, char *str, int len)
struct elem *mem_get(const struct mphdr *hdr, const char *str, int len)
{
struct {
int len;
char *data;
const char *data;
} temp = { .len = len, .data = str };
return kavl_find(my, hdr->root, (struct elem *)&temp, 0);
}
struct elem *mem_add(struct mphdr *hdr, char *str, int len)
struct elem *mem_add(struct mphdr *hdr, char *str, int len, size_t struct_size)
{
struct elem *v, *e = calloc(sizeof(struct elem), 1);
struct elem *v, *e = calloc(struct_size, 1);
if (!e) {
return 0;
}
e->len = len;
if (!hdr->stat) {
while (1) {
if (!hdr->static_data) {
e->data = malloc(len);
if (!e->data) {
free(e);
return 0;
break;
}
memcpy(e->data, str, len);
}
@ -56,27 +56,29 @@ struct elem *mem_add(struct mphdr *hdr, char *str, int len)
}
v = kavl_insert(my, &hdr->root, e, 0);
if (e != v) {
if (!hdr->stat) {
if (!hdr->static_data)
free(e->data);
}
free(e);
break;
}
return v;
}
free(e);
return 0;
}
void mem_delete(struct mphdr *hdr, char *str, int len)
void mem_delete(struct mphdr *hdr, const char *str, int len)
{
struct {
int len;
char *data;
const char *data;
} temp = { .len = len, .data = str };
struct elem *e = kavl_erase(my, &hdr->root, (struct elem *)&temp, 0);
if (!e) {
return;
}
if (!hdr->stat) {
if (!hdr->static_data) {
free(e->data);
e->data = 0;
}
@ -91,7 +93,7 @@ void mem_destroy(struct mphdr *hdr)
if (!e) {
break;
}
if (!hdr->stat && e->data) {
if (!hdr->static_data) {
free(e->data);
}
e->data = 0;

18
mpool.h
View File

@ -8,23 +8,27 @@
struct elem {
int len;
char *data;
int m;
time_t time;
KAVL_HEAD(struct elem) head;
};
struct elem_i {
struct elem i;
int m;
time_t time;
};
struct mphdr {
bool stat;
bool static_data;
struct elem *root;
};
struct mphdr *mem_pool(bool cst);
struct mphdr *mem_pool(bool is_static);
struct elem *mem_get(struct mphdr *hdr, char *str, int len);
struct elem *mem_get(const struct mphdr *hdr, const char *str, int len);
struct elem *mem_add(struct mphdr *hdr, char *str, int len);
struct elem *mem_add(struct mphdr *hdr, char *str, int len, size_t ssize);
void mem_delete(struct mphdr *hdr, char *str, int len);
void mem_delete(struct mphdr *hdr, const char *str, int len);
void mem_destroy(struct mphdr *hdr);