Use cl_ namespace and fix internal error naming

This commit is contained in:
Alex D. 2021-08-27 23:36:09 +00:00
parent 4848259901
commit 1036867979
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
2 changed files with 131 additions and 131 deletions

View File

@ -23,36 +23,36 @@
#include <string.h> // memcpy()
// Exporter
static dynarray_err corelibs_dynarray_export_slice(const dynarray_t*, uintmax_t, uintmax_t, void*);
static cl_dynarray_err corelibs_dynarray_export_slice(const cl_dynarray_t*, uintmax_t, uintmax_t, void*);
// Lifetime
static dynarray_err corelibs_dynarray_make_new(size_t, dynarray_t**);
static dynarray_err corelibs_dynarray_make_slice(const dynarray_t*, uintmax_t, uintmax_t, dynarray_t**);
static dynarray_err corelibs_dynarray_free(dynarray_t*);
static cl_dynarray_err corelibs_dynarray_make_new(size_t, cl_dynarray_t**);
static cl_dynarray_err corelibs_dynarray_make_slice(const cl_dynarray_t*, uintmax_t, uintmax_t, cl_dynarray_t**);
static cl_dynarray_err corelibs_dynarray_free(cl_dynarray_t*);
// Array storage
static dynarray_err corelibs_dynarray_mod_arr_cap(dynarray_t*, uintmax_t);
static dynarray_err corelibs_dynarray_mod_arr_lock(dynarray_t*, bool);
static cl_dynarray_err corelibs_dynarray_mod_arr_cap(cl_dynarray_t*, uintmax_t);
static cl_dynarray_err corelibs_dynarray_mod_arr_lock(cl_dynarray_t*, bool);
// Array contents
static dynarray_err corelibs_dynarray_mod_ct_app(dynarray_t*, uintmax_t, const void*);
static dynarray_err corelibs_dynarray_mod_ct_ins(dynarray_t*, uintmax_t, uintmax_t, const void*);
static dynarray_err corelibs_dynarray_mod_ct_rep(dynarray_t*, uintmax_t, uintmax_t, const void*);
static dynarray_err corelibs_dynarray_mod_ct_rm(dynarray_t*, uintmax_t, uintmax_t);
static cl_dynarray_err corelibs_dynarray_mod_ct_app(cl_dynarray_t*, uintmax_t, const void*);
static cl_dynarray_err corelibs_dynarray_mod_ct_ins(cl_dynarray_t*, uintmax_t, uintmax_t, const void*);
static cl_dynarray_err corelibs_dynarray_mod_ct_rep(cl_dynarray_t*, uintmax_t, uintmax_t, const void*);
static cl_dynarray_err corelibs_dynarray_mod_ct_rm(cl_dynarray_t*, uintmax_t, uintmax_t);
// Get array properties
static dynarray_err corelibs_dynarray_get_len(const dynarray_t*, uintmax_t*);
static dynarray_err corelibs_dynarray_get_size(const dynarray_t*, size_t*);
static dynarray_err corelibs_dynarray_get_cap_len(const dynarray_t*, uintmax_t*);
static dynarray_err corelibs_dynarray_get_cap_lock(const dynarray_t*, bool*);
static cl_dynarray_err corelibs_dynarray_get_len(const cl_dynarray_t*, uintmax_t*);
static cl_dynarray_err corelibs_dynarray_get_size(const cl_dynarray_t*, size_t*);
static cl_dynarray_err corelibs_dynarray_get_cap_len(const cl_dynarray_t*, uintmax_t*);
static cl_dynarray_err corelibs_dynarray_get_cap_lock(const cl_dynarray_t*, bool*);
// Compare arrays
static dynarray_err corelibs_dynarray_cmp_data(const dynarray_t* a, const dynarray_t* b, bool* eq);
static cl_dynarray_err corelibs_dynarray_cmp_data(const cl_dynarray_t* a, const cl_dynarray_t* b, bool* eq);
// Private functions
static dynarray_err corelibs_dynarray_bcheck(const dynarray_t*, uintmax_t, uintmax_t);
static cl_dynarray_err corelibs_dynarray_bcheck(const cl_dynarray_t*, uintmax_t, uintmax_t);
struct dynarray_t {
struct cl_dynarray_t {
void* addr; // Location of element
uintmax_t len; // Element count
struct {
@ -63,15 +63,15 @@ struct dynarray_t {
};
enum { // Avoid collision in error numbers
CORELIBS_DYNARRAY_ERR_NCOMPAT = -7,
CORELIBS_DYNARRAY_ERR_INVAL,
CORELIBS_DYNARRAY_ERR_IMMUT,
CORELIBS_DYNARRAY_ERR_UNDEF,
CORELIBS_DYNARRAY_ERR_OOB,
CORELIBS_DYNARRAY_ERR_NULL,
CORELIBS_DYNARRAY_ERR_ALLOC,
CORELIBS_DYNARRAY_ERR_UNK = 0,
CORELIBS_DYNARRAY_ERR_OK = 1,
CORELIBS_DYNARRAY_ERR_VAR_NCOMPAT = -7,
CORELIBS_DYNARRAY_ERR_VAR_INVAL,
CORELIBS_DYNARRAY_ERR_VAR_IMMUT,
CORELIBS_DYNARRAY_ERR_VAR_UNDEF,
CORELIBS_DYNARRAY_ERR_MEM_OOB,
CORELIBS_DYNARRAY_ERR_MEM_NULL,
CORELIBS_DYNARRAY_ERR_MEM_ALLOC,
CORELIBS_DYNARRAY_ERR_UNKOWN = 0,
CORELIBS_DYNARRAY_ERR_OK = 1,
};
// Present a unified structure that may receive changes, deprecations and renames without hassle for external users
@ -109,34 +109,34 @@ const struct corelibs_dynarray_interface dynarray = {
},
.err = {
.ok = CORELIBS_DYNARRAY_ERR_OK,
.unknown = CORELIBS_DYNARRAY_ERR_UNK,
.unknown = CORELIBS_DYNARRAY_ERR_UNKOWN,
.mem = {
.alloc = CORELIBS_DYNARRAY_ERR_ALLOC,
.null = CORELIBS_DYNARRAY_ERR_NULL,
.oob = CORELIBS_DYNARRAY_ERR_OOB,
.alloc = CORELIBS_DYNARRAY_ERR_MEM_ALLOC,
.null = CORELIBS_DYNARRAY_ERR_MEM_NULL,
.oob = CORELIBS_DYNARRAY_ERR_MEM_OOB,
},
.var = {
.undef = CORELIBS_DYNARRAY_ERR_UNDEF,
.immut = CORELIBS_DYNARRAY_ERR_IMMUT,
.inval = CORELIBS_DYNARRAY_ERR_INVAL,
.ncompat = CORELIBS_DYNARRAY_ERR_NCOMPAT,
.undef = CORELIBS_DYNARRAY_ERR_VAR_UNDEF,
.immut = CORELIBS_DYNARRAY_ERR_VAR_IMMUT,
.inval = CORELIBS_DYNARRAY_ERR_VAR_INVAL,
.ncompat = CORELIBS_DYNARRAY_ERR_VAR_NCOMPAT,
},
},
};
static dynarray_err
corelibs_dynarray_make_new(size_t sbyte, dynarray_t** ptr)
static cl_dynarray_err
corelibs_dynarray_make_new(size_t sbyte, cl_dynarray_t** ptr)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (ptr == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
dynarray_t* new = malloc(sizeof(*new));
cl_dynarray_t* new = malloc(sizeof(*new));
if (new == NULL) {
err = CORELIBS_DYNARRAY_ERR_ALLOC;
err = CORELIBS_DYNARRAY_ERR_MEM_ALLOC;
goto ret;
}
@ -151,13 +151,13 @@ ret:
return err;
}
static dynarray_err
corelibs_dynarray_make_slice(const dynarray_t* arr, uintmax_t pos, uintmax_t cnt, dynarray_t** save)
static cl_dynarray_err
corelibs_dynarray_make_slice(const cl_dynarray_t* arr, uintmax_t pos, uintmax_t cnt, cl_dynarray_t** save)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (save == NULL || arr == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
@ -170,7 +170,7 @@ corelibs_dynarray_make_slice(const dynarray_t* arr, uintmax_t pos, uintmax_t cnt
// Can we slice these elements?
if ((err = corelibs_dynarray_bcheck(arr, pos, cnt)) != CORELIBS_DYNARRAY_ERR_OK) goto ret;
dynarray_t* new;
cl_dynarray_t* new;
// Create new array
if ((err = corelibs_dynarray_make_new(arr->es, &new)) != CORELIBS_DYNARRAY_ERR_OK) goto ret;
@ -193,13 +193,13 @@ ret:
return err;
}
static dynarray_err
corelibs_dynarray_free(dynarray_t* arr)
static cl_dynarray_err
corelibs_dynarray_free(cl_dynarray_t* arr)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (arr == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
@ -210,13 +210,13 @@ ret:
return err;
}
static dynarray_err
corelibs_dynarray_mod_arr_cap(dynarray_t* arr, uintmax_t len)
static cl_dynarray_err
corelibs_dynarray_mod_arr_cap(cl_dynarray_t* arr, uintmax_t len)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (arr == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
@ -227,7 +227,7 @@ corelibs_dynarray_mod_arr_cap(dynarray_t* arr, uintmax_t len)
}
if (arr->cap.lock) {
err = CORELIBS_DYNARRAY_ERR_IMMUT;
err = CORELIBS_DYNARRAY_ERR_VAR_IMMUT;
goto ret;
}
@ -236,12 +236,12 @@ corelibs_dynarray_mod_arr_cap(dynarray_t* arr, uintmax_t len)
if (arr->addr == NULL) {
if ((reg = malloc(nl)) == NULL) {
err = CORELIBS_DYNARRAY_ERR_ALLOC;
err = CORELIBS_DYNARRAY_ERR_MEM_ALLOC;
goto ret;
}
} else if (arr->cap.len != len) {
if ((reg = realloc(arr->addr, nl)) == NULL) {
err = CORELIBS_DYNARRAY_ERR_ALLOC;
err = CORELIBS_DYNARRAY_ERR_MEM_ALLOC;
goto ret;
}
}
@ -253,13 +253,13 @@ ret:
return err;
}
static dynarray_err
corelibs_dynarray_mod_arr_lock(dynarray_t* arr, bool lock)
static cl_dynarray_err
corelibs_dynarray_mod_arr_lock(cl_dynarray_t* arr, bool lock)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (arr == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
@ -268,19 +268,19 @@ ret:
return err;
}
static dynarray_err
corelibs_dynarray_mod_ct_app(dynarray_t* arr, uintmax_t cnt, const void* elem)
static cl_dynarray_err
corelibs_dynarray_mod_ct_app(cl_dynarray_t* arr, uintmax_t cnt, const void* elem)
{
return corelibs_dynarray_mod_ct_ins(arr, arr->len, cnt, elem);
}
static dynarray_err
corelibs_dynarray_mod_ct_ins(dynarray_t* arr, uintmax_t pos, uintmax_t cnt, const void* elem)
static cl_dynarray_err
corelibs_dynarray_mod_ct_ins(cl_dynarray_t* arr, uintmax_t pos, uintmax_t cnt, const void* elem)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (arr == NULL || elem == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
@ -315,13 +315,13 @@ ret:
return err;
}
static dynarray_err
corelibs_dynarray_mod_ct_rep(dynarray_t* arr, uintmax_t pos, uintmax_t cnt, const void* elem)
static cl_dynarray_err
corelibs_dynarray_mod_ct_rep(cl_dynarray_t* arr, uintmax_t pos, uintmax_t cnt, const void* elem)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (arr == NULL || elem == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
@ -346,13 +346,13 @@ ret:
return err;
}
static dynarray_err
corelibs_dynarray_mod_ct_rm(dynarray_t* arr, uintmax_t pos, uintmax_t cnt)
static cl_dynarray_err
corelibs_dynarray_mod_ct_rm(cl_dynarray_t* arr, uintmax_t pos, uintmax_t cnt)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (arr == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
@ -362,7 +362,7 @@ corelibs_dynarray_mod_ct_rm(dynarray_t* arr, uintmax_t pos, uintmax_t cnt)
// Allocate intermediate buffer
void* tbuf = malloc(arr->es * arr->len);
if (tbuf == NULL) {
err = CORELIBS_DYNARRAY_ERR_ALLOC;
err = CORELIBS_DYNARRAY_ERR_MEM_ALLOC;
goto ret;
}
@ -395,13 +395,13 @@ ret:
return err;
}
static dynarray_err
corelibs_dynarray_export_slice(const dynarray_t* arr, uintmax_t pos, uintmax_t cnt, void* save)
static cl_dynarray_err
corelibs_dynarray_export_slice(const cl_dynarray_t* arr, uintmax_t pos, uintmax_t cnt, void* save)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (arr == NULL || save == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
if (cnt == 0) goto ret;
@ -421,13 +421,13 @@ ret:
return err;
}
static dynarray_err
corelibs_dynarray_get_len(const dynarray_t* arr, uintmax_t* save)
static cl_dynarray_err
corelibs_dynarray_get_len(const cl_dynarray_t* arr, uintmax_t* save)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (arr == NULL || save == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
@ -436,13 +436,13 @@ ret:
return err;
}
static dynarray_err
corelibs_dynarray_get_cap_len(const dynarray_t* arr, uintmax_t* save)
static cl_dynarray_err
corelibs_dynarray_get_cap_len(const cl_dynarray_t* arr, uintmax_t* save)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (arr == NULL || save == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
@ -451,13 +451,13 @@ ret:
return err;
}
static dynarray_err
corelibs_dynarray_get_cap_lock(const dynarray_t* arr, bool* save)
static cl_dynarray_err
corelibs_dynarray_get_cap_lock(const cl_dynarray_t* arr, bool* save)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (arr == NULL || save == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
@ -466,13 +466,13 @@ ret:
return err;
}
static dynarray_err
corelibs_dynarray_get_size(const dynarray_t* arr, size_t* save)
static cl_dynarray_err
corelibs_dynarray_get_size(const cl_dynarray_t* arr, size_t* save)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (arr == NULL || save == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
@ -481,18 +481,18 @@ ret:
return err;
}
static dynarray_err
corelibs_dynarray_cmp_data(const dynarray_t* a, const dynarray_t* b, bool* eq)
static cl_dynarray_err
corelibs_dynarray_cmp_data(const cl_dynarray_t* a, const cl_dynarray_t* b, bool* eq)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (a == NULL || b == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
if (a->es != b->es) {
err = CORELIBS_DYNARRAY_ERR_NCOMPAT;
err = CORELIBS_DYNARRAY_ERR_VAR_NCOMPAT;
goto ret;
}
@ -507,18 +507,18 @@ ret:
}
// Private functions
static dynarray_err
corelibs_dynarray_bcheck(const dynarray_t* arr, uintmax_t pos, uintmax_t len)
static cl_dynarray_err
corelibs_dynarray_bcheck(const cl_dynarray_t* arr, uintmax_t pos, uintmax_t len)
{
dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
cl_dynarray_err err = CORELIBS_DYNARRAY_ERR_OK;
if (arr == NULL) {
err = CORELIBS_DYNARRAY_ERR_NULL;
err = CORELIBS_DYNARRAY_ERR_MEM_NULL;
goto ret;
}
if (arr->cap.len < pos + len) {
err = CORELIBS_DYNARRAY_ERR_OOB;
err = CORELIBS_DYNARRAY_ERR_MEM_OOB;
goto ret;
}
ret:

View File

@ -23,82 +23,82 @@
#ifndef CORELIBS_GUARD_DYNARRAY
#define CORELIBS_GUARD_DYNARRAY
typedef signed short int dynarray_err;
typedef struct dynarray_t dynarray_t;
typedef signed short int cl_dynarray_err;
typedef struct cl_dynarray_t cl_dynarray_t;
struct corelibs_dynarray_interface {
// Makers
const struct {
// New - allocate new array
dynarray_err (*const new)(size_t sbyte, dynarray_t** ptr);
cl_dynarray_err (*const new)(size_t sbyte, cl_dynarray_t** ptr);
// Slicer - gets a slice of a array
dynarray_err (*const slice)(const dynarray_t* arr, uintmax_t pos, uintmax_t len, dynarray_t** ssave);
cl_dynarray_err (*const slice)(const cl_dynarray_t* arr, uintmax_t pos, uintmax_t len, cl_dynarray_t** ssave);
} make;
// Free
dynarray_err (*const free)(dynarray_t* arr);
cl_dynarray_err (*const free)(cl_dynarray_t* arr);
// Modifiers
const struct {
const struct {
// Capacity
dynarray_err (*const cap)(dynarray_t* ptr, uintmax_t len);
cl_dynarray_err (*const cap)(cl_dynarray_t* ptr, uintmax_t len);
// Capacity lock (dynamic / static capacity)
dynarray_err (*const lock)(dynarray_t* ptr, bool cap);
cl_dynarray_err (*const lock)(cl_dynarray_t* ptr, bool cap);
} arr;
const struct {
// Appender - Adds element at end of array
dynarray_err (*const app)(dynarray_t* arr, uintmax_t cnt, const void* elem);
cl_dynarray_err (*const app)(cl_dynarray_t* arr, uintmax_t cnt, const void* elem);
// Inserter - Adds element at position, pushing next element 1 position further
dynarray_err (*const ins)(dynarray_t* arr, uintmax_t pos, uintmax_t cnt, const void* elem);
cl_dynarray_err (*const ins)(cl_dynarray_t* arr, uintmax_t pos, uintmax_t cnt, const void* elem);
// Replacer - Replaces array item or creates it if not existent
dynarray_err (*const rep)(dynarray_t* arr, uintmax_t pos, uintmax_t cnt, const void* elem);
cl_dynarray_err (*const rep)(cl_dynarray_t* arr, uintmax_t pos, uintmax_t cnt, const void* elem);
// Remover - removes element from array
dynarray_err (*const rm)(dynarray_t* arr, uintmax_t pos, uintmax_t cnt);
cl_dynarray_err (*const rm)(cl_dynarray_t* arr, uintmax_t pos, uintmax_t cnt);
} ct;
} mod;
// Fetchers
const struct {
dynarray_err (*const len)(const dynarray_t* arr, uintmax_t* save);
dynarray_err (*const size)(const dynarray_t* arr, size_t* save);
cl_dynarray_err (*const len)(const cl_dynarray_t* arr, uintmax_t* save);
cl_dynarray_err (*const size)(const cl_dynarray_t* arr, size_t* save);
const struct {
dynarray_err (*const len)(const dynarray_t* arr, uintmax_t* save);
dynarray_err (*const lock)(const dynarray_t* arr, bool* save);
cl_dynarray_err (*const len)(const cl_dynarray_t* arr, uintmax_t* save);
cl_dynarray_err (*const lock)(const cl_dynarray_t* arr, bool* save);
} cap;
} get;
// Comparers
const struct {
dynarray_err (*const data)(const dynarray_t* a, const dynarray_t* b, bool* eq);
cl_dynarray_err (*const data)(const cl_dynarray_t* a, const cl_dynarray_t* b, bool* eq);
} cmp;
// Exporter
const struct {
dynarray_err (*const slice)(const dynarray_t* arr, uintmax_t pos, uintmax_t cnt, void* save);
cl_dynarray_err (*const slice)(const cl_dynarray_t* arr, uintmax_t pos, uintmax_t cnt, void* save);
} export;
// Errors - functions return them on run-time problems
const struct {
const dynarray_err ok, // No error
unknown; // Unknown error
const cl_dynarray_err ok, // No error
unknown; // Unknown error
const struct {
const dynarray_err alloc, // Memory allocation failed
null, // Passed a null pointer
oob; // Out of bounds modification or fetch
const cl_dynarray_err alloc, // Memory allocation failed
null, // Passed a null pointer
oob; // Out of bounds modification or fetch
} mem;
const struct {
const dynarray_err undef, // Undefined variable
immut, // Immutable (constant) variable
inval, // Invalid value passed
ncompat; // Incompatible comparasion
const cl_dynarray_err undef, // Undefined variable
immut, // Immutable (constant) variable
inval, // Invalid value passed
ncompat; // Incompatible comparasion
} var;
} err;