use nix build & meson & zlog
This commit is contained in:
parent
0123623742
commit
346768a144
60 changed files with 13739 additions and 0 deletions
99
src/lib/xlist.c
Normal file
99
src/lib/xlist.c
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "xlist.h"
|
||||
|
||||
int xlist_init(xlist_t* l,
|
||||
int delta)
|
||||
{
|
||||
if ((l == NULL) || (delta <= 0)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
l->delta = delta;
|
||||
l->data = NULL;
|
||||
l->count = 0;
|
||||
l->caps = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int xlist_ensure(xlist_t* l)
|
||||
{
|
||||
void* p;
|
||||
int nc, i;
|
||||
|
||||
if (l->count < l->caps) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
nc = l->caps + l->delta;
|
||||
p = (void*)realloc(l->data, nc * sizeof(void*));
|
||||
if (p == NULL) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
l->data = p;
|
||||
for(i = l->caps; i < nc; i ++) {
|
||||
l->data[i] = NULL;
|
||||
}
|
||||
|
||||
l->caps = nc;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int xlist_store(xlist_t* l,
|
||||
void* data)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (l == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
rc = xlist_ensure(l);
|
||||
if (rc < 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
l->data[l->count] = data;
|
||||
l->count++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void xlist_clear(xlist_t *l) {
|
||||
if (l == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
l->count = 0;
|
||||
}
|
||||
|
||||
int xlist_iter(xlist_t *l,
|
||||
xlist_iter_fn_t fn,
|
||||
void *data) {
|
||||
int i;
|
||||
|
||||
if ((l == NULL) || (fn == NULL)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
for(i = 0; i < l->count; i ++) {
|
||||
(*fn)((void *)l->data[i], data);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void xlist_fini(xlist_t* l)
|
||||
{
|
||||
if (l == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
FREEUP(l->data);
|
||||
return;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue