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 }; uint8_t key[KEY_SIZE] = { 0 };
int len = serialize_addr(dst, key, sizeof(key)); 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) { if (!val) {
return -1; 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); LOG(LOG_S, "save ip: %s, m=%d\n", ADDR_STR, m);
time_t t = time(0); 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) { if (!val) {
uniperror("mem_add"); uniperror("mem_add");
return -1; return -1;

2
main.c
View File

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

54
mpool.c
View File

@ -15,68 +15,70 @@ static inline int scmp(const struct elem *p, const struct elem *q)
KAVL_INIT(my, struct elem, head, scmp) 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); struct mphdr *hdr = calloc(sizeof(struct mphdr), 1);
if (hdr) { if (hdr) {
hdr->stat = cst; hdr->static_data = is_static;
} }
return hdr; 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 { struct {
int len; int len;
char *data; const char *data;
} temp = { .len = len, .data = str }; } temp = { .len = len, .data = str };
return kavl_find(my, hdr->root, (struct elem *)&temp, 0); 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) { if (!e) {
return 0; return 0;
} }
e->len = len; e->len = len;
if (!hdr->stat) { while (1) {
e->data = malloc(len); if (!hdr->static_data) {
if (!e->data) { e->data = malloc(len);
free(e); if (!e->data) {
return 0; break;
}
memcpy(e->data, str, len);
} }
memcpy(e->data, str, len); else {
} e->data = str;
else {
e->data = str;
}
v = kavl_insert(my, &hdr->root, e, 0);
if (e != v) {
if (!hdr->stat) {
free(e->data);
} }
free(e); v = kavl_insert(my, &hdr->root, e, 0);
if (e != v) {
if (!hdr->static_data)
free(e->data);
break;
}
return v;
} }
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 { struct {
int len; int len;
char *data; const char *data;
} temp = { .len = len, .data = str }; } temp = { .len = len, .data = str };
struct elem *e = kavl_erase(my, &hdr->root, (struct elem *)&temp, 0); struct elem *e = kavl_erase(my, &hdr->root, (struct elem *)&temp, 0);
if (!e) { if (!e) {
return; return;
} }
if (!hdr->stat) { if (!hdr->static_data) {
free(e->data); free(e->data);
e->data = 0; e->data = 0;
} }
@ -91,7 +93,7 @@ void mem_destroy(struct mphdr *hdr)
if (!e) { if (!e) {
break; break;
} }
if (!hdr->stat && e->data) { if (!hdr->static_data) {
free(e->data); free(e->data);
} }
e->data = 0; e->data = 0;

18
mpool.h
View File

@ -8,23 +8,27 @@
struct elem { struct elem {
int len; int len;
char *data; char *data;
int m;
time_t time;
KAVL_HEAD(struct elem) head; KAVL_HEAD(struct elem) head;
}; };
struct elem_i {
struct elem i;
int m;
time_t time;
};
struct mphdr { struct mphdr {
bool stat; bool static_data;
struct elem *root; 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); void mem_destroy(struct mphdr *hdr);