Use cl_ namespace and fix internal error naming

This commit is contained in:
Alex D. 2021-08-27 23:31:15 +00:00
parent 1aa5f88b9b
commit 14f6970a24
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
2 changed files with 88 additions and 88 deletions

140
llist.c
View File

@ -22,34 +22,34 @@
#include <stdint.h> // uintmax_t
#include <stdlib.h> // malloc() free()
static llist_err corelibs_llist_create(void*, void (*)(void*), llist_t**);
static cl_llist_err corelibs_llist_create(void*, void (*)(void*), cl_llist_t**);
static llist_err corelibs_llist_link(uintmax_t, ...);
static llist_err corelibs_llist_rep(llist_t*, llist_t*);
static cl_llist_err corelibs_llist_link(uintmax_t, ...);
static cl_llist_err corelibs_llist_rep(cl_llist_t*, cl_llist_t*);
static llist_err corelibs_llist_free_list(llist_t*);
static llist_err corelibs_llist_free_elem(llist_t*);
static cl_llist_err corelibs_llist_free_list(cl_llist_t*);
static cl_llist_err corelibs_llist_free_elem(cl_llist_t*);
static llist_err corelibs_llist_set_cont(llist_t*, void*);
static llist_err corelibs_llist_set_free(llist_t*, void (*)(void*));
static cl_llist_err corelibs_llist_set_cont(cl_llist_t*, void*);
static cl_llist_err corelibs_llist_set_free(cl_llist_t*, void (*)(void*));
static llist_err corelibs_llist_get_next(const llist_t*, llist_t**);
static llist_err corelibs_llist_get_prev(const llist_t*, llist_t**);
static llist_err corelibs_llist_get_cont(const llist_t*, void**);
static cl_llist_err corelibs_llist_get_next(const cl_llist_t*, cl_llist_t**);
static cl_llist_err corelibs_llist_get_prev(const cl_llist_t*, cl_llist_t**);
static cl_llist_err corelibs_llist_get_cont(const cl_llist_t*, void**);
static void corelibs_llist_remove(llist_t*);
static void corelibs_llist_remove(cl_llist_t*);
struct llist_t {
struct llist_t *next, *prev;
void* cont;
struct cl_llist_t {
struct cl_llist_t *next, *prev;
void* cont;
void (*free)(void*);
};
enum { // Avoid collision in error numbers
CORELIBS_LLIST_ERR_MARGS = -4,
CORELIBS_LLIST_ERR_ARGS_MIS = -4,
CORELIBS_LLIST_ERR_UNDEF,
CORELIBS_LLIST_ERR_NULL,
CORELIBS_LLIST_ERR_ALLOC,
CORELIBS_LLIST_ERR_MEM_NULL,
CORELIBS_LLIST_ERR_MEM_ALLOC,
CORELIBS_LLIST_ERR_UNK = 0,
CORELIBS_LLIST_ERR_OK = 1,
};
@ -76,23 +76,23 @@ struct corelibs_llist_interface const llist = {
.unknown = CORELIBS_LLIST_ERR_UNK,
.undef = CORELIBS_LLIST_ERR_UNDEF,
.mem = {
.null = CORELIBS_LLIST_ERR_NULL,
.alloc = CORELIBS_LLIST_ERR_ALLOC,
.null = CORELIBS_LLIST_ERR_MEM_NULL,
.alloc = CORELIBS_LLIST_ERR_MEM_ALLOC,
},
.args = {
.mis = CORELIBS_LLIST_ERR_MARGS,
.mis = CORELIBS_LLIST_ERR_ARGS_MIS,
},
},
};
static llist_err
corelibs_llist_create(void* c, void (*const f)(void*), llist_t** s)
static cl_llist_err
corelibs_llist_create(void* c, void (*const f)(void*), cl_llist_t** s)
{
llist_err err = CORELIBS_LLIST_ERR_OK;
cl_llist_err err = CORELIBS_LLIST_ERR_OK;
llist_t* cur = malloc(sizeof(*cur));
cl_llist_t* cur = malloc(sizeof(*cur));
if (cur == NULL) {
err = CORELIBS_LLIST_ERR_ALLOC;
err = CORELIBS_LLIST_ERR_MEM_ALLOC;
goto ret;
}
@ -106,42 +106,42 @@ ret:
return err;
}
static llist_err
corelibs_llist_free_list(llist_t* l)
static cl_llist_err
corelibs_llist_free_list(cl_llist_t* l)
{
llist_err err = CORELIBS_LLIST_ERR_OK;
cl_llist_err err = CORELIBS_LLIST_ERR_OK;
if (l == NULL) {
err = CORELIBS_LLIST_ERR_NULL;
err = CORELIBS_LLIST_ERR_MEM_NULL;
goto ret;
}
// Free previous elements if we have any
if (l->prev != NULL) {
for (llist_t* p = l->prev; p != NULL;) {
llist_t* c = p;
p = p->prev;
for (cl_llist_t* p = l->prev; p != NULL;) {
cl_llist_t* c = p;
p = p->prev;
corelibs_llist_remove(c);
}
}
// Free current and next elements
for (llist_t* p = l; p != NULL;) {
llist_t* c = p;
p = p->next;
for (cl_llist_t* p = l; p != NULL;) {
cl_llist_t* c = p;
p = p->next;
corelibs_llist_remove(c);
}
ret:
return err;
}
static llist_err
corelibs_llist_free_elem(llist_t* l)
static cl_llist_err
corelibs_llist_free_elem(cl_llist_t* l)
{
llist_err err = CORELIBS_LLIST_ERR_OK;
cl_llist_err err = CORELIBS_LLIST_ERR_OK;
if (l == NULL) {
err = CORELIBS_LLIST_ERR_NULL;
err = CORELIBS_LLIST_ERR_MEM_NULL;
goto ret;
}
corelibs_llist_remove(l);
@ -150,24 +150,24 @@ ret:
return err;
}
static llist_err
static cl_llist_err
corelibs_llist_link(uintmax_t cnt, ...)
{
llist_err err = CORELIBS_LLIST_ERR_OK;
cl_llist_err err = CORELIBS_LLIST_ERR_OK;
if (cnt <= 1) {
err = CORELIBS_LLIST_ERR_MARGS;
err = CORELIBS_LLIST_ERR_ARGS_MIS;
goto ret;
}
va_list ap;
va_start(ap, cnt);
uintmax_t ccnt = 0;
llist_t * a = NULL, *b = NULL;
uintmax_t ccnt = 0;
cl_llist_t *a = NULL, *b = NULL;
while (ccnt < cnt) {
a = b;
b = va_arg(ap, llist_t*);
b = va_arg(ap, cl_llist_t*);
if (ccnt < 1) continue; // Fill at least first 2 slots
if (a != NULL) a->next = b;
@ -181,13 +181,13 @@ ret:
return err;
}
static llist_err
corelibs_llist_rep(llist_t* dest, llist_t* src)
static cl_llist_err
corelibs_llist_rep(cl_llist_t* dest, cl_llist_t* src)
{
llist_err err = CORELIBS_LLIST_ERR_OK;
cl_llist_err err = CORELIBS_LLIST_ERR_OK;
if (src == NULL || dest == NULL) {
err = CORELIBS_LLIST_ERR_NULL;
err = CORELIBS_LLIST_ERR_MEM_NULL;
goto ret;
}
@ -199,13 +199,13 @@ ret:
return err;
}
static llist_err
corelibs_llist_get_next(const llist_t* e, llist_t** save)
static cl_llist_err
corelibs_llist_get_next(const cl_llist_t* e, cl_llist_t** save)
{
llist_err err = CORELIBS_LLIST_ERR_OK;
cl_llist_err err = CORELIBS_LLIST_ERR_OK;
if (e == NULL || save == NULL) {
err = CORELIBS_LLIST_ERR_NULL;
err = CORELIBS_LLIST_ERR_MEM_NULL;
goto ret;
}
@ -218,13 +218,13 @@ ret:
return err;
}
static llist_err
corelibs_llist_get_prev(const llist_t* e, llist_t** save)
static cl_llist_err
corelibs_llist_get_prev(const cl_llist_t* e, cl_llist_t** save)
{
llist_err err = CORELIBS_LLIST_ERR_OK;
cl_llist_err err = CORELIBS_LLIST_ERR_OK;
if (e == NULL || save == NULL) {
err = CORELIBS_LLIST_ERR_NULL;
err = CORELIBS_LLIST_ERR_MEM_NULL;
goto ret;
}
@ -237,13 +237,13 @@ ret:
return err;
}
static llist_err
corelibs_llist_get_cont(const llist_t* e, void** save)
static cl_llist_err
corelibs_llist_get_cont(const cl_llist_t* e, void** save)
{
llist_err err = CORELIBS_LLIST_ERR_OK;
cl_llist_err err = CORELIBS_LLIST_ERR_OK;
if (e == NULL || save == NULL) {
err = CORELIBS_LLIST_ERR_NULL;
err = CORELIBS_LLIST_ERR_MEM_NULL;
goto ret;
}
@ -252,13 +252,13 @@ ret:
return err;
}
static llist_err
corelibs_llist_set_cont(llist_t* e, void* i)
static cl_llist_err
corelibs_llist_set_cont(cl_llist_t* e, void* i)
{
llist_err err = CORELIBS_LLIST_ERR_OK;
cl_llist_err err = CORELIBS_LLIST_ERR_OK;
if (e == NULL) {
err = CORELIBS_LLIST_ERR_NULL;
err = CORELIBS_LLIST_ERR_MEM_NULL;
goto ret;
}
@ -267,13 +267,13 @@ ret:
return err;
}
static llist_err
corelibs_llist_set_free(llist_t* e, void (*const f)(void*))
static cl_llist_err
corelibs_llist_set_free(cl_llist_t* e, void (*const f)(void*))
{
llist_err err = CORELIBS_LLIST_ERR_OK;
cl_llist_err err = CORELIBS_LLIST_ERR_OK;
if (e == NULL) {
err = CORELIBS_LLIST_ERR_NULL;
err = CORELIBS_LLIST_ERR_MEM_NULL;
goto ret;
}
@ -284,7 +284,7 @@ ret:
// Private function
static void
corelibs_llist_remove(llist_t* e)
corelibs_llist_remove(cl_llist_t* e)
{
corelibs_llist_link(2, e->prev, e->next);
if (e->free != NULL) e->free(e->cont);

36
llist.h
View File

@ -21,36 +21,36 @@
#ifndef CORELIBS_GUARD_LLIST
#define CORELIBS_GUARD_LLIST
typedef struct llist_t llist_t;
typedef signed short int llist_err;
typedef struct cl_llist_t cl_llist_t;
typedef signed short int cl_llist_err;
struct corelibs_llist_interface {
llist_err (*const create)(void* content, void (*const free_function)(void*), llist_t** save);
llist_err (*const link)(uintmax_t elements, ...);
llist_err (*const rep)(llist_t* dest, llist_t* src);
cl_llist_err (*const create)(void* content, void (*const free_function)(void*), cl_llist_t** save);
cl_llist_err (*const link)(uintmax_t elements, ...);
cl_llist_err (*const rep)(cl_llist_t* dest, cl_llist_t* src);
const struct {
llist_err (*const list)(llist_t* element);
llist_err (*const elem)(llist_t* element);
cl_llist_err (*const list)(cl_llist_t* element);
cl_llist_err (*const elem)(cl_llist_t* element);
} free;
const struct {
llist_err (*const cont)(llist_t* element, void* content);
llist_err (*const free)(llist_t* element, void (*const free_function)(void*));
cl_llist_err (*const cont)(cl_llist_t* element, void* content);
cl_llist_err (*const free)(cl_llist_t* element, void (*const free_function)(void*));
} set;
const struct {
llist_err (*const prev)(const llist_t* element, llist_t** save);
llist_err (*const next)(const llist_t* element, llist_t** save);
llist_err (*const cont)(const llist_t* element, void** save);
cl_llist_err (*const prev)(const cl_llist_t* element, cl_llist_t** save);
cl_llist_err (*const next)(const cl_llist_t* element, cl_llist_t** save);
cl_llist_err (*const cont)(const cl_llist_t* element, void** save);
} get;
const struct {
const llist_err ok, // No error
unknown, // Unknown error
undef; // Fetched element is undefined
const cl_llist_err ok, // No error
unknown, // Unknown error
undef; // Fetched element is undefined
const struct {
const llist_err null, // NULL address was passed
alloc; // Allocation failed
const cl_llist_err null, // NULL address was passed
alloc; // Allocation failed
} mem;
const struct {
const llist_err mis; // Missing arguments / Not enough arguments
const cl_llist_err mis; // Missing arguments / Not enough arguments
} args;
} err;
};