mirror of
https://github.com/hufrea/byedpi.git
synced 2024-12-22 22:35:39 +00:00
Custom AVL elem struct size
This commit is contained in:
parent
fdb91ecf17
commit
c86ba13937
4
extend.c
4
extend.c
@ -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
2
main.c
@ -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;
|
||||
}
|
||||
|
54
mpool.c
54
mpool.c
@ -15,68 +15,70 @@ 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) {
|
||||
e->data = malloc(len);
|
||||
if (!e->data) {
|
||||
free(e);
|
||||
return 0;
|
||||
while (1) {
|
||||
if (!hdr->static_data) {
|
||||
e->data = malloc(len);
|
||||
if (!e->data) {
|
||||
break;
|
||||
}
|
||||
memcpy(e->data, str, len);
|
||||
}
|
||||
memcpy(e->data, str, len);
|
||||
}
|
||||
else {
|
||||
e->data = str;
|
||||
}
|
||||
v = kavl_insert(my, &hdr->root, e, 0);
|
||||
if (e != v) {
|
||||
if (!hdr->stat) {
|
||||
free(e->data);
|
||||
else {
|
||||
e->data = str;
|
||||
}
|
||||
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 {
|
||||
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
18
mpool.h
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user