From 928238bdcbc78c4196eb4a3508808c79e31f7c84 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Wed, 14 May 2025 12:19:34 -0500 Subject: Update signatures of free-type functions to match the freecallback signature --- src/Makefile | 41 ++++++++- src/arena.c | 50 ++++------- src/arena.h | 10 +-- src/ll.c | 5 +- src/ll.h | 2 +- src/main.c | 202 +++--------------------------------------- src/threadpool.c | 264 ++++++++++++++++++++++++++----------------------------- src/~Makefile | 39 -------- 8 files changed, 197 insertions(+), 416 deletions(-) delete mode 100644 src/~Makefile diff --git a/src/Makefile b/src/Makefile index f89fd4d..b045807 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,2 +1,39 @@ -all: - @./main.c +CC = gcc +SHELL := /usr/bin/env +.SHELLFLAGS := -S bash -c + +# I need to get better at makefiles so I can write this in a way that isn't absolutely insane/stupid +# RELEASE_CFLAGS := -O3 -fipa-pta -fipa-cp -fuse-linker-plugin -flto=auto +# RELEASE_LDFLAGS := -fuse-linker-plugin -flto=auto + +CFLAGS = -std=c2x -Wall -Wextra -Wpedantic -pedantic-errors -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -O0 $$(pkg-config --cflags libsodium) +LDLIBS += $$(pkg-config --libs-only-l libsodium) +LDFLAGS += $$(pkg-config --libs-only-L libsodium) +DEPFLAGS = -MT $@ -MMD -MP -MF $*.d + +SOURCES := $(wildcard *.c) +OBJECTS := $(patsubst %.c,%.o,$(SOURCES)) +DEPS := $(patsubst %.c,%.d,$(SOURCES)) + +COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) -c + +.PHONY: all c clean val +.DELETE_ON_ERROR: +.ONESHELL: + +all: main +main: $(OBJECTS) + +%.o: %.c %.d + $(COMPILE.c) $< + +$(DEPS): +include $(wildcard $(DEPS)) +# Adopted from https://make.mad-scientist.net/papers/advanced-auto-dependency-generation/ + +c clean: + @-rm -rv main $(OBJECTS) $(DEPS) $(wildcard *.test*) $(wildcard *.enc) + +val: + $(MAKE) all + valgrind --leak-check=yes ./main \ No newline at end of file diff --git a/src/arena.c b/src/arena.c index f944661..40c72d5 100644 --- a/src/arena.c +++ b/src/arena.c @@ -103,42 +103,25 @@ void * arena_alloc(arena * const arena, size_t bytes) { // doesn't, that sucks and blows everything up } -int arena_free(arena **arena) { - if(!arena) - RETURNWERR(EINVAL, -1); - if(!(*arena)) - RETURNWERR(EINVAL, -1); +void arena_free(void *a) { + if(!a) + return; - (*arena)->current = (*arena)->start; - for(arenanode *n; (*arena)->current != NULL;) { - n = (*arena)->current->next; + arena *real = (arena *)a; + real->current = real->start; + for(arenanode *n; real->current != NULL;) { + n = real->current->next; - free((*arena)->current->membase); - free((*arena)->current); + free(real->current->membase); + free(real->current); - (*arena)->current = n; + real->current = n; } - free((*arena)); - (*arena) = NULL; + free(real); - return 0; + return; } -int arena_clear(arena **arena) { - if(!arena) - RETURNWERR(EINVAL, -1); - if(!(*arena)) - RETURNWERR(EINVAL, -1); - - size_t bytes = (*arena)->node_memspace; - - int ret = 0; - if((ret = arena_free(arena)) < 0) - return ret; - (*arena) = arena_init(bytes); - - return (!(*arena)) ? -1 : 0; -} @@ -158,10 +141,7 @@ void * simplearena_alloc(simplearena * const a, size_t bytes) { return arena_alloc(a, bytes); } -int simplearena_free(simplearena **a) { - return arena_free(a); -} - -int simplearena_clear(simplearena **a) { - return arena_clear(a); +void simplearena_free(simplearena *a) { + arena_free(a); + return; } \ No newline at end of file diff --git a/src/arena.h b/src/arena.h index 1c1c576..907a2fa 100644 --- a/src/arena.h +++ b/src/arena.h @@ -26,10 +26,7 @@ arena * arena_init(size_t bytes); void * arena_alloc(arena * const arena, size_t bytes); // Free an arena its memory pool(s). Returns -1 and sets `errno` if the given arena is NULL -int arena_free(arena **arena); - -// Clear an arena of its contents (frees and inits an arena in 1). Returns -1 and sets `errno` if the given arena is NULL -int arena_clear(arena **arena); +void arena_free(void *a); // Initializes a simple arena. @@ -39,9 +36,6 @@ simplearena * simplearena_init(size_t bytes); void * simplearena_alloc(simplearena * const a, size_t bytes); // Free a simple arena. Exits / returns -1 on error depending on the value of `___VXGG___USE_XALLOC_FOR_ARENAS___` -int simplearena_free(simplearena **a); - -// Clear a simple arena and its memory pool (frees and inits a simple arena in 1). Exits / returns -1 on error depending on the value of `___VXGG___USE_XALLOC_FOR_ARENAS___` -int simplearena_clear(simplearena **a); +void simplearena_free(simplearena *a); #endif \ No newline at end of file diff --git a/src/ll.c b/src/ll.c index 76a79f9..6cc2afe 100644 --- a/src/ll.c +++ b/src/ll.c @@ -19,10 +19,11 @@ dlinkedlist * dlinkedlist_init(void) { return ll; } -void dlinkedlist_free(dlinkedlist *ll) { - if(!ll) +void dlinkedlist_free(void *dll) { + if(!dll) return; + dlinkedlist *ll = (dlinkedlist *)dll; for(dllnode *current = ll->start, *next; current != NULL; ) { next = current->next; diff --git a/src/ll.h b/src/ll.h index 154cfe2..f5801e0 100644 --- a/src/ll.h +++ b/src/ll.h @@ -13,7 +13,7 @@ typedef struct dlinked dlinkedlist; #ifndef __VXGG_REWRITE___LL_INTERNAL___ dlinkedlist * dlinkedlist_init(void); -void dlinkedlist_free(dlinkedlist *ll); +void dlinkedlist_free(void *dll); int dlinkedlist_append(dlinkedlist * const ll, void *data, dll_freecb fcb); int dlinkedlist_prepend(dlinkedlist * const ll, void *data, dll_freecb fcb); int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int index); diff --git a/src/main.c b/src/main.c index 9d1c5ab..97310fc 100755 --- a/src/main.c +++ b/src/main.c @@ -1,192 +1,14 @@ -///usr/bin/true; gcc -std=c2x -Wall -Wextra -Wpedantic -pedantic-errors -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -O0 main.c -o main && ./main; exit $? - -#include -#include -#include -#include -#include - -#define RETURNWERR(eval, rval) do { \ - errno = (eval); \ - return (rval); \ -} while (0) - -#define ERROR(exitval, errval, msg, __VA_ARGS__) do { \ - error((exitval), (errval), (msg)__VA_ARGS__); \ - abort(); \ -} while (0) - -void * xcalloc(size_t nmemb, size_t size) { - void *mem = calloc(nmemb, size); - if(!mem) - ERROR(-1, errno, " failed to allocate memory ", ); - - return mem; -} - - - -typedef int (*gcallback)(void*); -typedef void (*fcallback)(void*); -typedef struct gdata { - void *data; - fcallback fcb; -} gdata; - -gdata * gdata_init(void *data, fcallback fcb) { - gdata *gd = xcalloc(1, sizeof(*gd)); - gd->data = data; - gd->fcb = fcb; - - return gd; -} - -void gdata_free(void *gd) { - if(!gd) - return; - - gdata *real = (gdata*)gd; - if(real->fcb != NULL) - real->fcb(real->data); - free(real); - - return; -} - -void * gdata_getdata(gdata * const gd) { - if(!gd) - RETURNWERR(EINVAL, NULL); - - return gd->data; -} - -void * gdata_destruct(gdata *gd) { - if(!gd) - RETURNWERR(EINVAL, NULL); - - void *data = gdata_getdata(gd); - free(gd); - return data; -} - - - -typedef struct stack { - gdata **arr; - int size; - int used; -} stack; - -stack * stack_init(int size) { - if(size < 1) - RETURNWERR(EINVAL, NULL); - - stack *st = xcalloc(1, sizeof(*st)); - st->size = size; - st->used = 0; - st->arr = xcalloc(st->size, sizeof(gdata*)); - - return st; -} - -void stack_free(void *st) { - if(!st) - return; - - stack *real = (stack*)st; - for(int i = 0; i < real->used; i++) - gdata_free(real->arr[i]); - free(real->arr); - free(real); - - return; -} - -int stack_push(stack *st, gdata *gd) { - if(!st || !gd) - RETURNWERR(EINVAL, -1); - if(!(st->used < st->size)) - RETURNWERR(ENOMEM, 0); - - st->arr[st->used++] = gd; - return st->used; -} - -gdata * stack_pop(stack *st) { - if(!st) - RETURNWERR(EINVAL, NULL); - if(st->used <= 0) - RETURNWERR(ENODATA, 0); - - return st->arr[--st->used]; -} - -int stack_pushd(stack *st, void *data, fcallback fcb) { - if(!st) - RETURNWERR(EINVAL, -1); - - gdata *gd = gdata_init(data, fcb); - int retval = stack_push(st, gd); - if(retval <= 0) - gdata_free(gd); - - return retval; -} - -void * stack_popd(stack *st) { - if(!st) - RETURNWERR(EINVAL, NULL); - - gdata *gd = stack_pop(st); - if(!gd) - return NULL; - - return gdata_destruct(gd); -} - - - -typedef struct cstack { - stack *st; - mtx_t mutex; - cnd_t cond; - unsigned char canceled; - -} cstack; - -cstack * cstack_init(int size) { - if(size < 1) - RETURNWERR(EINVAL, NULL); - - cstack *cst = xcalloc(1, sizeof(*cst)); - cst->st = stack_init(size); - cst->canceled = 0; - mtx_init(&cst->mutex, mtx_plain); - cnd_init(&cst->cond); - - return cst; -} - -int cstack_cancel(cstack * const cst) { - if(!cst) - RETURNWERR(EINVAL, -1); - - mtx_lock(&cst->mutex); - cst->canceled = 1; - mtx_unlock(&cst->mutex); - cnd_broadcast(&cst->cond); - +#include "arena.h" +#include "encryption.h" +#include "ll.h" +#include "scanner.h" +#include "shared.h" +#include "threadpool.h" + +#include "errno.h" +#include "error.h" + +int main() { + error(-1, ENOTSUP, "lol"); return 0; -} - -void cstack_free(void *cst) { - if(!cst) - return; - - cstack *real = (cstack*)cst; - - cstack_cancel(real); - // Ok I don't think there's a good way to wait for all threads to die without registering the threads - - return; } \ No newline at end of file diff --git a/src/threadpool.c b/src/threadpool.c index 9d00030..e1c11aa 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -10,7 +10,7 @@ // Pair some data with a mutex. Specifically a way to deal with mutices easier, not for data storage (mtxpair_free does not free the `(void*)data` member) typedef struct mtxp { void *data; - mtx_t *mtx; + mtx_t mtx; } mtxpair; mtxpair * mtxpair_init(void * const data, int type) { @@ -18,16 +18,9 @@ mtxpair * mtxpair_init(void * const data, int type) { if(!mtxp) return NULL; - // Make room for the mutex - mtxp->mtx = VALLOC(1, sizeof(*mtxp->mtx)); - if(!mtxp->mtx) { - free(mtxp); - return NULL; - } - // Init the mutex - if(mtx_init(mtxp->mtx, type) == thrd_error) { - free(mtxp->mtx); free(mtxp); + if(mtx_init(&mtxp->mtx, type) == thrd_error) { + free(mtxp); RETURNWERR(errno, NULL); } @@ -39,8 +32,7 @@ void mtxpair_free(mtxpair *mp) { if(!mp) return; - mtx_destroy(mp->mtx); - free(mp->mtx); + mtx_destroy(&mp->mtx); free(mp); return; @@ -64,9 +56,9 @@ int thrd_createwmx(thrd_t * const thr, thrd_start_t func, mtxpair * const mtxd) if(!mtxd) RETURNWERR(EINVAL, thrd_error); - if(mtx_lock(mtxd->mtx) == thrd_error) {RETURNWERR(errno, thrd_error);} + if(mtx_lock(&mtxd->mtx) == thrd_error) {RETURNWERR(errno, thrd_error);} int retval = thrd_create(thr, func, mtxd->data); - if(mtx_unlock(mtxd->mtx) == thrd_error) {RETURNWERR(errno, thrd_error);} + if(mtx_unlock(&mtxd->mtx) == thrd_error) {RETURNWERR(errno, thrd_error);} return retval; } @@ -99,21 +91,6 @@ typedef struct task { void *arg; } task; -typedef struct cq { - dlinkedlist *list; - mtx_t *mtx; - cnd_t *cnd; - unsigned char canceled; -} cqueue; - -typedef struct tp { - thrd_t **threads; - int nthreads; - - cqueue *taskqueue; -} threadpool; - - task * task_init(task_callback cb, void *arg) { if(cb == NULL) RETURNWERR(EINVAL, NULL); @@ -138,149 +115,158 @@ void task_free(task *ts) { int task_fire(task *ts) { if(!ts) RETURNWERR(EINVAL, -1); + if(ts->cb == NULL) + RETURNWERR(EINVAL, -1); return ts->cb(ts->arg); } -/* Mutex: Lock a shared resource. Used to prevent race conditions when accessing / modifying some shared resource. A lock must -// always be followed by an unlock -// Semaphore: Send / wait on a signal; solves the consumer/producer problem. A function that sends should never wait, and a -// function that waits should never send */ +typedef struct cq { + dlinkedlist *taskqueue; + dlinkedlist *rthreads; -static void ___ucleanup_dfree(void *dll) { - if(!dll) - return; + mtx_t mtx; + cnd_t cnd; + + unsigned char canceled; - dlinkedlist_free((dlinkedlist *)dll); - return; -} +} cqueue; -static void ___ucleanup_cndd(void *cnd) { - if(!cnd) - return; - cnd_destroy((cnd_t *)cnd); - return; -} -static void ___ucleanup_mtxd(void *mtx) { - if(!mtx) - return; +// static void ___ucleanup_dfree(void *dll) { +// if(!dll) +// return; - mtx_destroy((mtx_t*)mtx); - return; -} +// dlinkedlist_free((dlinkedlist *)dll); +// return; +// } + +// static void ___ucleanup_cndd(void *cnd) { +// if(!cnd) +// return; + +// cnd_destroy((cnd_t *)cnd); +// return; +// } + +// static void ___ucleanup_mtxd(void *mtx) { +// if(!mtx) +// return; -cqueue * cqueue_init(int mtx_type) { - cleanup_CREATE(10); +// mtx_destroy((mtx_t*)mtx); +// return; +// } + +// cqueue * cqueue_init(int mtx_type) { +// cleanup_CREATE(10); - cqueue *cq = VALLOC(1, sizeof(*cq)); - if(!cq) - return NULL; - cleanup_REGISTER(free, cq); - - cq->canceled = FALSE; - cq->list = dlinkedlist_init(); - if(!cq->list) - cleanup_MARK(); - cleanup_CNDREGISTER(___ucleanup_dfree, cq->list); - - if(!cleanup_ERRORFLAGGED) - if(!(cq->cnd = VALLOC(1, sizeof(*cq->cnd)))) - cleanup_MARK(); - cleanup_CNDREGISTER(free, cq->cnd); +// cqueue *cq = VALLOC(1, sizeof(*cq)); +// if(!cq) +// return NULL; +// cleanup_REGISTER(free, cq); + +// cq->canceled = FALSE; +// cq->list = dlinkedlist_init(); +// if(!cq->list) +// cleanup_MARK(); +// cleanup_CNDREGISTER(___ucleanup_dfree, cq->list); - if(!cleanup_ERRORFLAGGED) - if(cnd_init(cq->cnd) == thrd_error) - cleanup_MARK(); - cleanup_CNDREGISTER(___ucleanup_cndd, cq->cnd); +// if(!cleanup_ERRORFLAGGED) +// if(cnd_init(&cq->cnd) == thrd_error) +// cleanup_MARK(); +// cleanup_CNDREGISTER(___ucleanup_cndd, &cq->cnd); - if(!cleanup_ERRORFLAGGED) - if(!(cq->mtx = VALLOC(1, sizeof(*cq->mtx)))) - cleanup_MARK(); - cleanup_CNDREGISTER(free, cq->mtx); - - if(!cleanup_ERRORFLAGGED) - if(mtx_init(cq->mtx, mtx_type) != thrd_success) - cleanup_MARK(); - cleanup_CNDREGISTER(___ucleanup_mtxd, cq->mtx); +// if(!cleanup_ERRORFLAGGED) +// if(mtx_init(&cq->mtx, mtx_type) != thrd_success) +// cleanup_MARK(); +// cleanup_CNDREGISTER(___ucleanup_mtxd, &cq->mtx); - if(cleanup_ERRORFLAGGED) - cleanup_FIRE(); +// if(cleanup_ERRORFLAGGED) +// cleanup_FIRE(); - return cq; -} +// return cq; +// } -void cqueue_cancel(cqueue *cq) { - if(!cq) - return; +// void cqueue_cancel(cqueue *cq) { +// if(!cq) +// return; - mtx_lock(cq->mtx); - if(cq->canceled) { - mtx_unlock(cq->mtx); - thrd_exit(-1); - } +// mtx_lock(cq->mtx); +// if(cq->canceled) { +// mtx_unlock(cq->mtx); +// thrd_exit(-1); +// } - cq->canceled++; - mtx_unlock(cq->mtx); - cnd_broadcast(cq->cnd); +// cq->canceled++; +// mtx_unlock(cq->mtx); +// cnd_broadcast(cq->cnd); - return; -} +// return; +// } -void cqueue_free(cqueue *cq) { - if(!cq) - return; +// void cqueue_free(cqueue *cq) { +// if(!cq) +// return; - cqueue_cancel(cq); - mtx_destroy(cq->mtx); - cnd_destroy(cq->cnd); - free(cq->mtx); - free(cq->cnd); - dlinkedlist_free(cq->list); +// cqueue_cancel(cq); +// mtx_destroy(cq->mtx); +// cnd_destroy(cq->cnd); +// free(cq->mtx); +// free(cq->cnd); +// dlinkedlist_free(cq->list); - return; -} +// return; +// } -int cqueue_addtask(cqueue * const cq, task * const tsk) { - if(!cq || !tsk) - RETURNWERR(EINVAL, -1); +// int cqueue_addtask(cqueue * const cq, task * const tsk) { +// if(!cq || !tsk) +// RETURNWERR(EINVAL, -1); - mtx_lock(cq->mtx); +// mtx_lock(cq->mtx); - // TODO: Think about creating an "exception" via signal handling - if(cq->canceled) { - mtx_unlock(cq->mtx); - thrd_exit(-1); - } +// // TODO: Think about creating an "exception" via signal handling +// if(cq->canceled) { +// mtx_unlock(cq->mtx); +// thrd_exit(-1); +// } - dlinkedlist_prepend(cq->list, tsk, free); - mtx_unlock(cq->mtx); - cnd_signal(cq->cnd); +// dlinkedlist_prepend(cq->list, tsk, free); +// mtx_unlock(cq->mtx); +// cnd_signal(cq->cnd); - return 0; -} +// return 0; +// } -task * cqueue_waitpop(cqueue * const cq) { - if(!cq) - RETURNWERR(EINVAL, NULL); +// task * cqueue_waitpop(cqueue * const cq) { +// if(!cq) +// RETURNWERR(EINVAL, NULL); - task *retval = NULL; +// task *retval = NULL; - mtx_lock(cq->mtx); - while(dlinkedlist_isempty(cq->list) && !cq->canceled) - cnd_wait(cq->cnd, cq->mtx); +// mtx_lock(cq->mtx); +// while(dlinkedlist_isempty(cq->list) && !cq->canceled) +// cnd_wait(cq->cnd, cq->mtx); - if(cq->canceled) { - mtx_unlock(cq->mtx); - thrd_exit(-1); - } +// if(cq->canceled) { +// mtx_unlock(cq->mtx); +// thrd_exit(-1); +// } - retval = dlinkedlist_get(cq->list, dlinkedlist_size(cq->list) - 1); - dlinkedlist_remove(cq->list, dlinkedlist_size(cq->list) - 1); - mtx_unlock(cq->mtx); +// retval = dlinkedlist_get(cq->list, dlinkedlist_size(cq->list) - 1); +// dlinkedlist_remove(cq->list, dlinkedlist_size(cq->list) - 1); +// mtx_unlock(cq->mtx); - return retval; -} \ No newline at end of file +// return retval; +// } + + + +typedef struct tp { + thrd_t **threads; // thrd_t *threads[] + int nthreads; + + cqueue *taskqueue; +} threadpool; diff --git a/src/~Makefile b/src/~Makefile deleted file mode 100644 index b045807..0000000 --- a/src/~Makefile +++ /dev/null @@ -1,39 +0,0 @@ -CC = gcc -SHELL := /usr/bin/env -.SHELLFLAGS := -S bash -c - -# I need to get better at makefiles so I can write this in a way that isn't absolutely insane/stupid -# RELEASE_CFLAGS := -O3 -fipa-pta -fipa-cp -fuse-linker-plugin -flto=auto -# RELEASE_LDFLAGS := -fuse-linker-plugin -flto=auto - -CFLAGS = -std=c2x -Wall -Wextra -Wpedantic -pedantic-errors -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -O0 $$(pkg-config --cflags libsodium) -LDLIBS += $$(pkg-config --libs-only-l libsodium) -LDFLAGS += $$(pkg-config --libs-only-L libsodium) -DEPFLAGS = -MT $@ -MMD -MP -MF $*.d - -SOURCES := $(wildcard *.c) -OBJECTS := $(patsubst %.c,%.o,$(SOURCES)) -DEPS := $(patsubst %.c,%.d,$(SOURCES)) - -COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) -c - -.PHONY: all c clean val -.DELETE_ON_ERROR: -.ONESHELL: - -all: main -main: $(OBJECTS) - -%.o: %.c %.d - $(COMPILE.c) $< - -$(DEPS): -include $(wildcard $(DEPS)) -# Adopted from https://make.mad-scientist.net/papers/advanced-auto-dependency-generation/ - -c clean: - @-rm -rv main $(OBJECTS) $(DEPS) $(wildcard *.test*) $(wildcard *.enc) - -val: - $(MAKE) all - valgrind --leak-check=yes ./main \ No newline at end of file -- cgit v1.2.3