Initial commit after repository separation
This commit is contained in:
commit
a976b6c580
|
@ -0,0 +1,60 @@
|
|||
include config.mk
|
||||
|
||||
ifeq (${CC},clang)
|
||||
|
||||
CFLAGS :=\
|
||||
-std=c99\
|
||||
-Weverything\
|
||||
-Wno-padded\
|
||||
-Wno-disabled-macro-expansion\
|
||||
-pedantic\
|
||||
${CFLAGS}
|
||||
|
||||
else ifeq (${CC},gcc)
|
||||
|
||||
CFLAGS :=\
|
||||
-std=c99\
|
||||
-Wall\
|
||||
-Wextra\
|
||||
-Wformat-overflow=2\
|
||||
-Wformat-security\
|
||||
-Winit-self\
|
||||
-Wstrict-overflow=2\
|
||||
-Wstringop-overflow=2\
|
||||
-Walloc-zero\
|
||||
-Wduplicated-branches\
|
||||
-Wduplicated-cond\
|
||||
-Wtrampolines\
|
||||
-Wfloat-equal\
|
||||
-Wshadow\
|
||||
-Wunsafe-loop-optimizations\
|
||||
-Wparentheses\
|
||||
-pedantic\
|
||||
${CFLAGS}
|
||||
|
||||
endif
|
||||
|
||||
SRC = llist.c
|
||||
HDR = llist.h
|
||||
LIB = llist.a
|
||||
OBJ = ${SRC:.c=.o}
|
||||
|
||||
all: ${LIB}
|
||||
|
||||
.c.o:
|
||||
${CC} -c ${CFLAGS} $<
|
||||
|
||||
${LIB}: ${OBJ}
|
||||
${AR} rc $@ $?
|
||||
${RANLIB} $@
|
||||
|
||||
clean:
|
||||
rm -f ${LIB} ${OBJ}
|
||||
|
||||
install: all
|
||||
mkdir -p "${DESTDIR}${PREFIX}/lib/corelibs"
|
||||
cp -f ${LIB} "${DESTDIR}${PREFIX}/lib/corelibs"
|
||||
mkdir -p "${DESTDIR}${PREFIX}/include/corelibs"
|
||||
cp -f ${HDR} "${DESTDIR}${PREFIX}/include/corelibs"
|
||||
|
||||
.PHONY: all clean install
|
|
@ -0,0 +1,7 @@
|
|||
VERSION = 1.0
|
||||
|
||||
PREFIX = /usr/local
|
||||
|
||||
CC = clang
|
||||
AR = llvm-ar
|
||||
RANLIB = llvm-ranlib
|
|
@ -0,0 +1,215 @@
|
|||
/*
|
||||
* This file is part of corelibs. (https://git.redxen.eu/caskd/corelibs)
|
||||
* Copyright (c) 2021 Alex-David Denes
|
||||
*
|
||||
* corelibs is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* corelibs is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with corelibs. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
#include <stdint.h> // uintmax_t
|
||||
#include <stdlib.h> // malloc() free()
|
||||
|
||||
static llist_err corelibs_llist_create(void*, void (*)(void*), llist_t**);
|
||||
static llist_err corelibs_llist_link(llist_t*, llist_t*);
|
||||
static llist_err corelibs_llist_free(llist_t*);
|
||||
|
||||
static llist_err corelibs_llist_set_cont(llist_t*, void*);
|
||||
static llist_err corelibs_llist_set_free(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 void corelibs_llist_remove(llist_t*);
|
||||
|
||||
struct llist_t {
|
||||
struct llist_t *next, *prev;
|
||||
void* cont;
|
||||
void (*free)(void*);
|
||||
};
|
||||
|
||||
enum { // Avoid collision in error numbers
|
||||
CORELIBS_LLIST_ERR_UNDEF = -3,
|
||||
CORELIBS_LLIST_ERR_NULL,
|
||||
CORELIBS_LLIST_ERR_ALLOC,
|
||||
CORELIBS_LLIST_ERR_UNK = 0,
|
||||
CORELIBS_LLIST_ERR_OK = 1,
|
||||
};
|
||||
|
||||
struct corelibs_llist_interface const llist = {
|
||||
.create = corelibs_llist_create,
|
||||
.link = corelibs_llist_link,
|
||||
.free = corelibs_llist_free,
|
||||
.set = {
|
||||
.cont = corelibs_llist_set_cont,
|
||||
.free = corelibs_llist_set_free,
|
||||
},
|
||||
.get = {
|
||||
.prev = corelibs_llist_get_prev,
|
||||
.cont = corelibs_llist_get_cont,
|
||||
.next = corelibs_llist_get_next,
|
||||
},
|
||||
};
|
||||
|
||||
static llist_err
|
||||
corelibs_llist_create(void* c, void (*const f)(void*), llist_t** s)
|
||||
{
|
||||
llist_err err = CORELIBS_LLIST_ERR_OK;
|
||||
|
||||
llist_t* cur = malloc(sizeof(*cur));
|
||||
if (cur == NULL) {
|
||||
err = CORELIBS_LLIST_ERR_ALLOC;
|
||||
goto ret;
|
||||
}
|
||||
|
||||
cur->prev = NULL;
|
||||
cur->next = NULL;
|
||||
cur->cont = c;
|
||||
cur->free = f;
|
||||
|
||||
*s = cur;
|
||||
ret:
|
||||
return err;
|
||||
}
|
||||
|
||||
static llist_err
|
||||
corelibs_llist_free(llist_t* l)
|
||||
{
|
||||
llist_err err = CORELIBS_LLIST_ERR_OK;
|
||||
|
||||
if (l == NULL) {
|
||||
err = CORELIBS_LLIST_ERR_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;
|
||||
corelibs_llist_remove(c);
|
||||
}
|
||||
}
|
||||
|
||||
// Free current and next elements
|
||||
for (llist_t* p = l; p != NULL;) {
|
||||
llist_t* c = p;
|
||||
p = p->next;
|
||||
corelibs_llist_remove(c);
|
||||
}
|
||||
ret:
|
||||
return err;
|
||||
}
|
||||
|
||||
static llist_err
|
||||
corelibs_llist_link(llist_t* a, llist_t* b)
|
||||
{
|
||||
llist_err err = CORELIBS_LLIST_ERR_OK;
|
||||
|
||||
if (a == NULL && b == NULL) {
|
||||
err = CORELIBS_LLIST_ERR_NULL;
|
||||
goto ret;
|
||||
}
|
||||
|
||||
if (a != NULL) a->next = b;
|
||||
if (b != NULL) b->prev = a;
|
||||
ret:
|
||||
return err;
|
||||
}
|
||||
|
||||
static llist_err
|
||||
corelibs_llist_get_next(const llist_t* e, llist_t** save)
|
||||
{
|
||||
llist_err err = CORELIBS_LLIST_ERR_OK;
|
||||
|
||||
if (e == NULL || save == NULL) {
|
||||
err = CORELIBS_LLIST_ERR_NULL;
|
||||
goto ret;
|
||||
}
|
||||
|
||||
*save = e->next;
|
||||
ret:
|
||||
return err;
|
||||
}
|
||||
|
||||
static llist_err
|
||||
corelibs_llist_get_prev(const llist_t* e, llist_t** save)
|
||||
{
|
||||
llist_err err = CORELIBS_LLIST_ERR_OK;
|
||||
|
||||
if (e == NULL || save == NULL) {
|
||||
err = CORELIBS_LLIST_ERR_NULL;
|
||||
goto ret;
|
||||
}
|
||||
|
||||
*save = e->prev;
|
||||
ret:
|
||||
return err;
|
||||
}
|
||||
|
||||
static llist_err
|
||||
corelibs_llist_get_cont(const llist_t* e, void** save)
|
||||
{
|
||||
llist_err err = CORELIBS_LLIST_ERR_OK;
|
||||
|
||||
if (e == NULL || save == NULL) {
|
||||
err = CORELIBS_LLIST_ERR_NULL;
|
||||
goto ret;
|
||||
}
|
||||
|
||||
*save = e->cont;
|
||||
ret:
|
||||
return err;
|
||||
}
|
||||
|
||||
static llist_err
|
||||
corelibs_llist_set_cont(llist_t* e, void* i)
|
||||
{
|
||||
llist_err err = CORELIBS_LLIST_ERR_OK;
|
||||
|
||||
if (e == NULL) {
|
||||
err = CORELIBS_LLIST_ERR_NULL;
|
||||
goto ret;
|
||||
}
|
||||
|
||||
e->cont = i;
|
||||
ret:
|
||||
return err;
|
||||
}
|
||||
|
||||
static llist_err
|
||||
corelibs_llist_set_free(llist_t* e, void (*const f)(void*))
|
||||
{
|
||||
llist_err err = CORELIBS_LLIST_ERR_OK;
|
||||
|
||||
if (e == NULL) {
|
||||
err = CORELIBS_LLIST_ERR_NULL;
|
||||
goto ret;
|
||||
}
|
||||
|
||||
e->free = f;
|
||||
ret:
|
||||
return err;
|
||||
}
|
||||
|
||||
// Private function
|
||||
static void
|
||||
corelibs_llist_remove(llist_t* e)
|
||||
{
|
||||
corelibs_llist_link(e->prev, e->next);
|
||||
if (e->free != NULL) e->free(e->cont);
|
||||
free(e);
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* This file is part of corelibs. (https://git.redxen.eu/caskd/corelibs)
|
||||
* Copyright (c) 2021 Alex-David Denes
|
||||
*
|
||||
* corelibs is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* corelibs is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with corelibs. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef CORELIBS_GUARD_LLIST
|
||||
#define CORELIBS_GUARD_LLIST
|
||||
|
||||
typedef struct llist_t llist_t;
|
||||
typedef signed short int llist_err;
|
||||
|
||||
struct corelibs_llist_interface {
|
||||
llist_err (*const create)(void* content, void (*const free_function)(void*), llist_t** save);
|
||||
llist_err (*const link)(llist_t* element_a, llist_t* element_b);
|
||||
llist_err (*const free)(llist_t* element);
|
||||
const struct {
|
||||
llist_err (*const cont)(llist_t* element, void* content);
|
||||
llist_err (*const free)(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);
|
||||
} get;
|
||||
const struct {
|
||||
const 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
|
||||
} mem;
|
||||
} err;
|
||||
};
|
||||
extern const struct corelibs_llist_interface llist;
|
||||
|
||||
#endif /* CORELIBS_GUARD_LLIST */
|
||||
|
Reference in New Issue