summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
author@syxhe <https://t.me/syxhe>2025-05-14 12:19:34 -0500
committer@syxhe <https://t.me/syxhe>2025-05-14 12:19:34 -0500
commit928238bdcbc78c4196eb4a3508808c79e31f7c84 (patch)
tree6ce7c393be64ba26c54f9f5f8d3fa442c6f5833d /src/main.c
parent9be8b5f26a29dad04035386331461c4c320f9237 (diff)
Update signatures of free-type functions to match the freecallback signature
Diffstat (limited to 'src/main.c')
-rwxr-xr-xsrc/main.c202
1 files changed, 12 insertions, 190 deletions
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 @@
1///usr/bin/true; gcc -std=c2x -Wall -Wextra -Wpedantic -pedantic-errors -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -O0 main.c -o main && ./main; exit $? 1#include "arena.h"
2 2#include "encryption.h"
3#include <asm-generic/errno-base.h> 3#include "ll.h"
4#include <stdlib.h> 4#include "scanner.h"
5#include <errno.h> 5#include "shared.h"
6#include <error.h> 6#include "threadpool.h"
7#include <threads.h> 7
8 8#include "errno.h"
9#define RETURNWERR(eval, rval) do { \ 9#include "error.h"
10 errno = (eval); \ 10
11 return (rval); \ 11int main() {
12} while (0) 12 error(-1, ENOTSUP, "lol");
13
14#define ERROR(exitval, errval, msg, __VA_ARGS__) do { \
15 error((exitval), (errval), (msg)__VA_ARGS__); \
16 abort(); \
17} while (0)
18
19void * xcalloc(size_t nmemb, size_t size) {
20 void *mem = calloc(nmemb, size);
21 if(!mem)
22 ERROR(-1, errno, "<xcalloc> failed to allocate memory ", );
23
24 return mem;
25}
26
27
28
29typedef int (*gcallback)(void*);
30typedef void (*fcallback)(void*);
31typedef struct gdata {
32 void *data;
33 fcallback fcb;
34} gdata;
35
36gdata * gdata_init(void *data, fcallback fcb) {
37 gdata *gd = xcalloc(1, sizeof(*gd));
38 gd->data = data;
39 gd->fcb = fcb;
40
41 return gd;
42}
43
44void gdata_free(void *gd) {
45 if(!gd)
46 return;
47
48 gdata *real = (gdata*)gd;
49 if(real->fcb != NULL)
50 real->fcb(real->data);
51 free(real);
52
53 return;
54}
55
56void * gdata_getdata(gdata * const gd) {
57 if(!gd)
58 RETURNWERR(EINVAL, NULL);
59
60 return gd->data;
61}
62
63void * gdata_destruct(gdata *gd) {
64 if(!gd)
65 RETURNWERR(EINVAL, NULL);
66
67 void *data = gdata_getdata(gd);
68 free(gd);
69 return data;
70}
71
72
73
74typedef struct stack {
75 gdata **arr;
76 int size;
77 int used;
78} stack;
79
80stack * stack_init(int size) {
81 if(size < 1)
82 RETURNWERR(EINVAL, NULL);
83
84 stack *st = xcalloc(1, sizeof(*st));
85 st->size = size;
86 st->used = 0;
87 st->arr = xcalloc(st->size, sizeof(gdata*));
88
89 return st;
90}
91
92void stack_free(void *st) {
93 if(!st)
94 return;
95
96 stack *real = (stack*)st;
97 for(int i = 0; i < real->used; i++)
98 gdata_free(real->arr[i]);
99 free(real->arr);
100 free(real);
101
102 return;
103}
104
105int stack_push(stack *st, gdata *gd) {
106 if(!st || !gd)
107 RETURNWERR(EINVAL, -1);
108 if(!(st->used < st->size))
109 RETURNWERR(ENOMEM, 0);
110
111 st->arr[st->used++] = gd;
112 return st->used;
113}
114
115gdata * stack_pop(stack *st) {
116 if(!st)
117 RETURNWERR(EINVAL, NULL);
118 if(st->used <= 0)
119 RETURNWERR(ENODATA, 0);
120
121 return st->arr[--st->used];
122}
123
124int stack_pushd(stack *st, void *data, fcallback fcb) {
125 if(!st)
126 RETURNWERR(EINVAL, -1);
127
128 gdata *gd = gdata_init(data, fcb);
129 int retval = stack_push(st, gd);
130 if(retval <= 0)
131 gdata_free(gd);
132
133 return retval;
134}
135
136void * stack_popd(stack *st) {
137 if(!st)
138 RETURNWERR(EINVAL, NULL);
139
140 gdata *gd = stack_pop(st);
141 if(!gd)
142 return NULL;
143
144 return gdata_destruct(gd);
145}
146
147
148
149typedef struct cstack {
150 stack *st;
151 mtx_t mutex;
152 cnd_t cond;
153 unsigned char canceled;
154
155} cstack;
156
157cstack * cstack_init(int size) {
158 if(size < 1)
159 RETURNWERR(EINVAL, NULL);
160
161 cstack *cst = xcalloc(1, sizeof(*cst));
162 cst->st = stack_init(size);
163 cst->canceled = 0;
164 mtx_init(&cst->mutex, mtx_plain);
165 cnd_init(&cst->cond);
166
167 return cst;
168}
169
170int cstack_cancel(cstack * const cst) {
171 if(!cst)
172 RETURNWERR(EINVAL, -1);
173
174 mtx_lock(&cst->mutex);
175 cst->canceled = 1;
176 mtx_unlock(&cst->mutex);
177 cnd_broadcast(&cst->cond);
178
179 return 0; 13 return 0;
180}
181
182void cstack_free(void *cst) {
183 if(!cst)
184 return;
185
186 cstack *real = (cstack*)cst;
187
188 cstack_cancel(real);
189 // Ok I don't think there's a good way to wait for all threads to die without registering the threads
190
191 return;
192} \ No newline at end of file 14} \ No newline at end of file