summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author@syxhe <https://t.me/syxhe>2025-04-21 21:51:54 -0500
committer@syxhe <https://t.me/syxhe>2025-04-21 21:51:54 -0500
commitc704a8a382c231e066a7fbf0402a33455c40b8f5 (patch)
tree0c4000906060013de91ea7fc1888cedcbc34159e
parent521e9a2fc8e0ef1dffe4e590d0d4f65292f0d1df (diff)
Rework dlinkedlist functions to use VALLOC macro instead of only xalloc
-rw-r--r--src/arena.h6
-rw-r--r--src/ll-internal.c7
-rw-r--r--src/ll.c18
-rw-r--r--src/ll.h18
-rw-r--r--src/main.c13
-rw-r--r--src/shared.h5
-rw-r--r--src/threadpool.c1
7 files changed, 28 insertions, 40 deletions
diff --git a/src/arena.h b/src/arena.h
index e65b041..1c1c576 100644
--- a/src/arena.h
+++ b/src/arena.h
@@ -3,12 +3,6 @@
3 3
4#include <stddef.h> 4#include <stddef.h>
5 5
6// Whether to use `XALLOC` or normal `malloc` functions for acquiring new memory in an arena. `> 0`
7// enables XALLOC functionality
8#define ___VXGG___USE_XALLOC_FOR_ARENAS___ 1
9
10#define VALLOC(nmemb, size) ((___VXGG___USE_XALLOC_FOR_ARENAS___ > 0) ? xmalloc((nmemb) * (size)) : malloc((nmemb) * (size)))
11
12// Normal arena. Can expand in size beyond original allocation 6// Normal arena. Can expand in size beyond original allocation
13typedef struct arena arena; 7typedef struct arena arena;
14 8
diff --git a/src/ll-internal.c b/src/ll-internal.c
index 1a4fe3c..e56302e 100644
--- a/src/ll-internal.c
+++ b/src/ll-internal.c
@@ -1,8 +1,13 @@
1#include "ll-internal.h" 1#include "ll-internal.h"
2#include "shared.h" 2#include "shared.h"
3 3
4#include <stdlib.h>
5
4dllnode * dllnode_init(void *data, dll_freecb fcb) { 6dllnode * dllnode_init(void *data, dll_freecb fcb) {
5 dllnode *n = xcalloc(1, sizeof(*n)); 7 dllnode *n = VALLOC(1, sizeof(*n));
8 if(!n)
9 return NULL;
10
6 n->data = data; 11 n->data = data;
7 n->freecb = fcb; 12 n->freecb = fcb;
8 n->prev = NULL; 13 n->prev = NULL;
diff --git a/src/ll.c b/src/ll.c
index 285203d..76a79f9 100644
--- a/src/ll.c
+++ b/src/ll.c
@@ -8,7 +8,10 @@
8#include <error.h> 8#include <error.h>
9 9
10dlinkedlist * dlinkedlist_init(void) { 10dlinkedlist * dlinkedlist_init(void) {
11 dlinkedlist *ll = xcalloc(1, sizeof(*ll)); 11 dlinkedlist *ll = VALLOC(1, sizeof(*ll));
12 if(!ll)
13 return NULL;
14
12 ll->end = NULL; 15 ll->end = NULL;
13 ll->start = NULL; 16 ll->start = NULL;
14 ll->size = 0; 17 ll->size = 0;
@@ -50,6 +53,8 @@ int dlinkedlist_handlefirstnode(dlinkedlist * const ll, void *data, dll_freecb f
50 53
51 // Need to handle adding the first element differently than other opers, so do it here 54 // Need to handle adding the first element differently than other opers, so do it here
52 dllnode *n = dllnode_init(data, fcb); 55 dllnode *n = dllnode_init(data, fcb);
56 if(!n)
57 return -1;
53 // dllnode_init already sets n.prev and n.next to null, so no need to do so again 58 // dllnode_init already sets n.prev and n.next to null, so no need to do so again
54 59
55 ll->end = n; 60 ll->end = n;
@@ -64,10 +69,15 @@ int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char
64 RETURNWERR(EINVAL, 1); 69 RETURNWERR(EINVAL, 1);
65 if(op != 'a' && op != 'p') 70 if(op != 'a' && op != 'p')
66 RETURNWERR(EINVAL, 1); 71 RETURNWERR(EINVAL, 1);
67 if(dlinkedlist_handlefirstnode(ll, data, fcb) == 0) 72
73 int handleret;
74 if((handleret = dlinkedlist_handlefirstnode(ll, data, fcb)) == 0)
68 return 0; 75 return 0;
69 76
70 dllnode *n = dllnode_init(data, fcb); 77 dllnode *n = dllnode_init(data, fcb);
78 if(!n || handleret < 0)
79 return -1;
80
71 switch (op) { 81 switch (op) {
72 case 'a': 82 case 'a':
73 n->prev = (ll->end); 83 n->prev = (ll->end);
@@ -135,6 +145,8 @@ int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int i
135 // If you're inserting at index 2, the new node becomes index 2, and the previous node at index 2 moves to index 3 145 // If you're inserting at index 2, the new node becomes index 2, and the previous node at index 2 moves to index 3
136 146
137 dllnode *new = dllnode_init(data, fcb); 147 dllnode *new = dllnode_init(data, fcb);
148 if(!new)
149 return -1;
138 dllnode *current = dlinkedlist_getnode(ll, index); 150 dllnode *current = dlinkedlist_getnode(ll, index);
139 if(!current) 151 if(!current)
140 XALLOC_EXIT("<dlinkedlist_insert> somehow managed to pull a null node from dlinkedlist_getnode(... , ... , ... , %d)", , (index)); 152 XALLOC_EXIT("<dlinkedlist_insert> somehow managed to pull a null node from dlinkedlist_getnode(... , ... , ... , %d)", , (index));
@@ -156,7 +168,7 @@ int dlinkedlist_remove(dlinkedlist * const ll, int index) {
156 168
157 dllnode *current = dlinkedlist_getnode(ll, index); 169 dllnode *current = dlinkedlist_getnode(ll, index);
158 if(!current) 170 if(!current)
159 abort(); 171 return -1;
160 172
161 if(index == 0) { 173 if(index == 0) {
162 ll->start = current->next; 174 ll->start = current->next;
diff --git a/src/ll.h b/src/ll.h
index 4b5a2df..154cfe2 100644
--- a/src/ll.h
+++ b/src/ll.h
@@ -1,25 +1,11 @@
1#ifndef __VXGG_REWRITE___LL_H___305861098005___ 1#ifndef __VXGG_REWRITE___LL_H___305861098005___
2#define __VXGG_REWRITE___LL_H___305861098005___ 2#define __VXGG_REWRITE___LL_H___305861098005___
3 3
4/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 4// Notice: dlinked functions are no longer necessarily xalloc, but MAY be IF the `___VXGG___USE_XALLOC_FOR_VALLOC___` is greater than 0
5// BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WA //
6// RNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING //
7// BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WA //
8////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*//*
9// //
10// dlinkedlist functions should be considered X_ALLOC functions, as they call xcalloc to initialize new nodes. This is not //
11// reflected in their naming, which could change later, but as of now it is something to keep in mind. This means ABSOLUETLY NO //
12// USE OF DLINKEDLIST FUNCTIONS INSIDE ENCRYPTION FUNCTIONS, OR ANYTHING THAT SHOULD BE CONSIDERED ATOMIC OR SEMI-ATOMIC. IF //
13// XCALLOC ERRORS FOR ANY REASON, AND YOU'RE DOING SOMETHING WITH THESE IN AN (SEMI)ATOMIC FUNCTION, YOU WILL BREAK SHIT //
14// //
15////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*//*
16// BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WA //
17// RNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING //
18// BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WA //
19//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
20 5
21/* TODO: Implement a way to register a set of alloc functions to a linked list so I can give it arenas for memory allocation 6/* TODO: Implement a way to register a set of alloc functions to a linked list so I can give it arenas for memory allocation
22// instead of just xcalloc */ 7// instead of just xcalloc */
8 // I don't know if I care to do this right now. I might be able to hack it in but it would be a fugly hack, something as bad if not worse than that weird callback thing I was doing with the ALWAYS_CHECK_LIBSODIUM macro
23 9
24typedef void (*dll_freecb)(void*); 10typedef void (*dll_freecb)(void*);
25typedef struct dlinked dlinkedlist; 11typedef struct dlinked dlinkedlist;
diff --git a/src/main.c b/src/main.c
index 3779206..2fcf8ab 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,5 +1,3 @@
1#define _GNU_SOURCE
2
3#include "arena.h" 1#include "arena.h"
4#include "ll.h" 2#include "ll.h"
5#include "encryption.h" 3#include "encryption.h"
@@ -9,18 +7,7 @@
9 7
10#include <errno.h> 8#include <errno.h>
11#include <error.h> 9#include <error.h>
12#include <stdio.h>
13
14#include <string.h>
15#include <stdlib.h>
16 10
17
18#include <dirent.h>
19int lol(const struct dirent *node) {return 1;}
20int printnames(void *data) {
21 printf("%s\n", (data) ? ((struct dirent *)data)->d_name : "null");
22 return 0;
23}
24int main() { 11int main() {
25 error(1, ENOTSUP, "No main file lol"); 12 error(1, ENOTSUP, "No main file lol");
26 return 0; 13 return 0;
diff --git a/src/shared.h b/src/shared.h
index 3b0a5f7..7034255 100644
--- a/src/shared.h
+++ b/src/shared.h
@@ -29,6 +29,11 @@
29 exit(EXIT_FAILURE); /* Makes gcc happy */\ 29 exit(EXIT_FAILURE); /* Makes gcc happy */\
30} while (0) 30} while (0)
31 31
32// Whether to use `XALLOC` or normal `malloc` functions for acquiring new memory via the `VALLOC()` macro. `> 0`
33// enables XALLOC functionality
34#define ___VXGG___USE_XALLOC_FOR_VALLOC___ 0
35#define VALLOC(nmemb, size) ((___VXGG___USE_XALLOC_FOR_VALLOC___ > 0) ? xmalloc((nmemb) * (size)) : malloc((nmemb) * (size)))
36
32// Error macro that gcc will not complain about 37// Error macro that gcc will not complain about
33#define ERROR(status, errnum, format, ...) do {error((status), (errnum), (format)__VA_ARGS__); exit((status));} while (0) 38#define ERROR(status, errnum, format, ...) do {error((status), (errnum), (format)__VA_ARGS__); exit((status));} while (0)
34 39
diff --git a/src/threadpool.c b/src/threadpool.c
index 808dd5d..56dcd6b 100644
--- a/src/threadpool.c
+++ b/src/threadpool.c
@@ -4,7 +4,6 @@
4 4
5#include "ll.h" 5#include "ll.h"
6 6
7#include <asm-generic/errno-base.h>
8#include <threads.h> 7#include <threads.h>
9#include <stdlib.h> 8#include <stdlib.h>
10#include <errno.h> 9#include <errno.h>