summaryrefslogtreecommitdiff
path: root/src/shared.c
diff options
context:
space:
mode:
author@syxhe <https://t.me/syxhe>2025-03-23 22:11:15 -0500
committer@syxhe <https://t.me/syxhe>2025-03-23 22:11:15 -0500
commitb3bd3df103a5a75d267b2b79e85558768b1dc4bb (patch)
treeee3ad753b2db679295db846e7d80363e9b6552e7 /src/shared.c
parentc1b188af8c51e29c96a0422b79516d95696869e7 (diff)
Fix a whole bunch of shit, create an arena implementation
Diffstat (limited to 'src/shared.c')
-rw-r--r--src/shared.c115
1 files changed, 23 insertions, 92 deletions
diff --git a/src/shared.c b/src/shared.c
index d67e362..72ede56 100644
--- a/src/shared.c
+++ b/src/shared.c
@@ -1,45 +1,23 @@
1#include "shared.h" 1#include "shared.h"
2 2
3#include <stdarg.h>
4#include <stdlib.h> 3#include <stdlib.h>
5#include <string.h> 4#include <string.h>
6#include <unistd.h> 5#include <unistd.h>
7#include <errno.h> 6#include <errno.h>
8#include <error.h> 7#include <error.h>
9#include <stdio.h>
10 8
11void* xcalloc(size_t nmemb, size_t size) { 9void* xcalloc(size_t nmemb, size_t size) {
12 void *mem = calloc(nmemb, size); 10 void *mem = calloc(nmemb, size);
13 11 if(!mem)
14 if(!mem) { 12 XALLOC_EXIT("<xcalloc> Could not allocate memory");
15 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
16 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
17 error(1, errno, "<xcalloc> Could not allocate memory");
18 #else
19 exit(EXIT_FAILURE);
20 #endif
21 #endif
22
23 abort();
24 }
25 13
26
27 return mem; 14 return mem;
28} 15}
29 16
30void* xreallocarray(void *ptr, size_t nmemb, size_t size) { 17void* xreallocarray(void *ptr, size_t nmemb, size_t size) {
31 void *mem = reallocarray(ptr, nmemb, size); 18 void *mem = reallocarray(ptr, nmemb, size);
32 if(mem == NULL) { 19 if(mem == NULL)
33 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 20 XALLOC_EXIT("<xreallocarray> Could not allocate memory");
34 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
35 error(1, errno, "<xreallocarray> Could not allocate memory");
36 #else
37 exit(EXIT_FAILURE);
38 #endif
39 #endif
40
41 abort();
42 }
43 21
44 return mem; 22 return mem;
45} 23}
@@ -63,9 +41,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) {
63 41
64 tmp = realloc(lstr, csize * sizeof(char)); 42 tmp = realloc(lstr, csize * sizeof(char));
65 if(!tmp) { 43 if(!tmp) {
66 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 44 if(___VXGG___VERBOSE_ERRORS___)
67 error(0, errno, "Could not reallocate enough space for lstr"); 45 error(0, errno, "Could not reallocate enough space for lstr");
68 #endif 46
69 free(lstr); 47 free(lstr);
70 lstr = NULL; // Need to set this because of the break 48 lstr = NULL; // Need to set this because of the break
71 bytesread = -100; 49 bytesread = -100;
@@ -75,9 +53,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) {
75 } 53 }
76 } 54 }
77 if(bytesread < 0 && bytesread != -100) { 55 if(bytesread < 0 && bytesread != -100) {
78 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 56 if(___VXGG___VERBOSE_ERRORS___)
79 error(0, errno, "Ran into a read() error"); 57 error(0, errno, "Ran into a read() error");
80 #endif 58
81 free(lstr); 59 free(lstr);
82 lstr = NULL; 60 lstr = NULL;
83 } 61 }
@@ -85,9 +63,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) {
85 if(lstr) { 63 if(lstr) {
86 tmp = realloc(lstr, csize - ccap + 1); 64 tmp = realloc(lstr, csize - ccap + 1);
87 if(!tmp) { 65 if(!tmp) {
88 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 66 if(___VXGG___VERBOSE_ERRORS___)
89 error(0, errno, "Could not shrink lstr after reading buffer"); 67 error(0, errno, "Could not shrink lstr after reading buffer");
90 #endif 68
91 free(lstr); 69 free(lstr);
92 bytesread = -100; 70 bytesread = -100;
93 } 71 }
@@ -127,16 +105,8 @@ char *xdirname(const char * const path) {
127 char *tmp = NULL; 105 char *tmp = NULL;
128 if(!path) { // Path being null is a special case which should return super early, before anything else 106 if(!path) { // Path being null is a special case which should return super early, before anything else
129 tmp = strdup("."); 107 tmp = strdup(".");
130 if(!tmp) { 108 if(!tmp)
131 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 109 XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result \"NULL\"");
132 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
133 error(1, errno, "<xdirname> could not strdup \".\" for set path result \"NULL\"");
134 #else
135 exit(EXIT_FAILURE);
136 #endif
137 #endif
138 abort();
139 }
140 110
141 return tmp; 111 return tmp;
142 } 112 }
@@ -147,16 +117,9 @@ char *xdirname(const char * const path) {
147 if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;} 117 if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;}
148 118
149 if(flag) { 119 if(flag) {
150 if(!tmp) { 120 if(!tmp)
151 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 121 XALLOC_EXIT("<xdirname> could not strdup a set path result");
152 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 122
153 error(1, errno, "<xdirname> could not strdup a set path result");
154 #else
155 exit(EXIT_FAILURE);
156 #endif
157 #endif
158 abort();
159 }
160 return tmp; 123 return tmp;
161 } 124 }
162 125
@@ -173,16 +136,8 @@ char *xdirname(const char * const path) {
173 136
174 // Get a temp copy of the path for manipulation purposes 137 // Get a temp copy of the path for manipulation purposes
175 tmp = strdup(path); 138 tmp = strdup(path);
176 if(!tmp) { 139 if(!tmp)
177 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 140 XALLOC_EXIT("<xdirname> could not strdup the given path \"%s\" for internal manipulation", , path);
178 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
179 error(1, errno, "<xdirname> could not strdup the given path \"%s\" for internal manipulation", path);
180 #else
181 exit(EXIT_FAILURE);
182 #endif
183 #endif
184 abort();
185 }
186 141
187 // If there's a trailing '/', delete it 142 // If there's a trailing '/', delete it
188 size_t pathlen = strlen(path); 143 size_t pathlen = strlen(path);
@@ -204,31 +159,15 @@ char *xdirname(const char * const path) {
204 if(count == 0) { 159 if(count == 0) {
205 free(tmp); 160 free(tmp);
206 tmp = strdup("."); 161 tmp = strdup(".");
207 if(!tmp) { 162 if(!tmp)
208 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 163 XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result");
209 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
210 error(1, errno, "<xdirname> could not strdup \".\" for set path result");
211 #else
212 exit(EXIT_FAILURE);
213 #endif
214 #endif
215 abort();
216 }
217 return tmp; 164 return tmp;
218 } 165 }
219 if(count == 1) { 166 if(count == 1) {
220 free(tmp); 167 free(tmp);
221 tmp = strdup("/"); 168 tmp = strdup("/");
222 if(!tmp) { 169 if(!tmp)
223 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 170 XALLOC_EXIT("<xdirname> could not strdup \"/\" for set path result");
224 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
225 error(1, errno, "<xdirname> could not strdup \"/\" for set path result");
226 #else
227 exit(EXIT_FAILURE);
228 #endif
229 #endif
230 abort();
231 }
232 171
233 return tmp; 172 return tmp;
234 } 173 }
@@ -241,16 +180,8 @@ char *xdirname(const char * const path) {
241 } 180 }
242 181
243 char * const actual = strdup(tmp); 182 char * const actual = strdup(tmp);
244 if(!actual) { 183 if(!actual)
245 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 184 XALLOC_EXIT("<xdirname> could not strdup tmp string to make a shorter end string");
246 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
247 error(1, errno, "<xdirname> could not strdup tmp string to make a shorter end string");
248 #else
249 exit(EXIT_FAILURE);
250 #endif
251 #endif
252 abort();
253 }
254 free(tmp); 185 free(tmp);
255 186
256 return actual; 187 return actual;