diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile | 2 | ||||
| -rw-r--r-- | src/arena.c | 5 | ||||
| -rw-r--r-- | src/arena.h | 2 | ||||
| -rw-r--r-- | src/encryption.c | 2 | ||||
| -rw-r--r-- | src/ll.c | 2 | ||||
| -rw-r--r-- | src/shared.c | 66 | ||||
| -rw-r--r-- | src/shared.h | 7 |
7 files changed, 62 insertions, 24 deletions
diff --git a/src/Makefile b/src/Makefile index 4bddadf..4450727 100644 --- a/src/Makefile +++ b/src/Makefile | |||
| @@ -5,7 +5,7 @@ SHELL = /usr/bin/env -S bash | |||
| 5 | # RELEASE_CFLAGS := -O3 -fipa-pta -fipa-cp -fuse-linker-plugin -flto=auto | 5 | # RELEASE_CFLAGS := -O3 -fipa-pta -fipa-cp -fuse-linker-plugin -flto=auto |
| 6 | # RELEASE_LDFLAGS := -fuse-linker-plugin -flto=auto | 6 | # RELEASE_LDFLAGS := -fuse-linker-plugin -flto=auto |
| 7 | 7 | ||
| 8 | CFLAGS = -Wall -Wextra -Wpedantic -pedantic-errors -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -O0 $$(pkg-config --cflags libsodium) | 8 | CFLAGS = -std=c2x -Wall -Wextra -Wpedantic -pedantic-errors -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -O0 $$(pkg-config --cflags libsodium) |
| 9 | LDLIBS += $$(pkg-config --libs-only-l libsodium) | 9 | LDLIBS += $$(pkg-config --libs-only-l libsodium) |
| 10 | LDFLAGS += $$(pkg-config --libs-only-L libsodium) | 10 | LDFLAGS += $$(pkg-config --libs-only-L libsodium) |
| 11 | 11 | ||
diff --git a/src/arena.c b/src/arena.c index 36a9ce7..62dbbf8 100644 --- a/src/arena.c +++ b/src/arena.c | |||
| @@ -23,6 +23,7 @@ typedef struct arena { | |||
| 23 | arenanode *current; | 23 | arenanode *current; |
| 24 | 24 | ||
| 25 | // For keeping track of the largest possible thing that can be allocated to one node | 25 | // For keeping track of the largest possible thing that can be allocated to one node |
| 26 | // I don't know if I care to keep this or remove it. I'll see if it becomes a problem / is useful | ||
| 26 | size_t node_memspace; | 27 | size_t node_memspace; |
| 27 | } arena; | 28 | } arena; |
| 28 | 29 | ||
| @@ -49,7 +50,7 @@ arenanode * arenanode_init(size_t bytes) { | |||
| 49 | 50 | ||
| 50 | arena * arena_init(size_t bytes) { | 51 | arena * arena_init(size_t bytes) { |
| 51 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) | 52 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) |
| 52 | XALLOC_EXIT("<arena_init> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena"); | 53 | XALLOC_EXIT("<arena_init> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena", ); |
| 53 | 54 | ||
| 54 | arena *a = VALLOC(1, sizeof(arena)); | 55 | arena *a = VALLOC(1, sizeof(arena)); |
| 55 | if(!a) | 56 | if(!a) |
| @@ -73,7 +74,7 @@ arena * arena_init(size_t bytes) { | |||
| 73 | 74 | ||
| 74 | void * arena_alloc(arena * const arena, size_t bytes) { | 75 | void * arena_alloc(arena * const arena, size_t bytes) { |
| 75 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) | 76 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) |
| 76 | XALLOC_EXIT("<arena_alloc> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to allocate any memory"); | 77 | XALLOC_EXIT("<arena_alloc> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to allocate any memory", ); |
| 77 | if(!arena) | 78 | if(!arena) |
| 78 | RETURNWERR(EINVAL, NULL); | 79 | RETURNWERR(EINVAL, NULL); |
| 79 | if(bytes > arena->node_memspace) | 80 | if(bytes > arena->node_memspace) |
diff --git a/src/arena.h b/src/arena.h index 1f57f01..e5be79a 100644 --- a/src/arena.h +++ b/src/arena.h | |||
| @@ -15,7 +15,7 @@ static const size_t MEM_ALIGN_BYTES = (2 * sizeof(void*)); // Hey, the static ke | |||
| 15 | #define MEM_ALIGN(x) ((x) + (((x) & (MEM_ALIGN_BYTES - 1)) != 0) * (MEM_ALIGN_BYTES - ((x) & (MEM_ALIGN_BYTES - 1)))) | 15 | #define MEM_ALIGN(x) ((x) + (((x) & (MEM_ALIGN_BYTES - 1)) != 0) * (MEM_ALIGN_BYTES - ((x) & (MEM_ALIGN_BYTES - 1)))) |
| 16 | 16 | ||
| 17 | arena * arena_init(size_t bytes); | 17 | arena * arena_init(size_t bytes); |
| 18 | void* arena_alloc(arena * const arena, size_t bytes); | 18 | void * arena_alloc(arena * const arena, size_t bytes); |
| 19 | int arena_free(arena **arena); | 19 | int arena_free(arena **arena); |
| 20 | int arena_clear(arena **arena); | 20 | int arena_clear(arena **arena); |
| 21 | 21 | ||
diff --git a/src/encryption.c b/src/encryption.c index 9b8715e..b75a119 100644 --- a/src/encryption.c +++ b/src/encryption.c | |||
| @@ -206,7 +206,7 @@ void* xsodium_malloc(size_t size) { | |||
| 206 | 206 | ||
| 207 | void *mem = sodium_malloc(size); | 207 | void *mem = sodium_malloc(size); |
| 208 | if(mem == NULL) | 208 | if(mem == NULL) |
| 209 | XALLOC_EXIT("<xsodium_malloc> could not allocate memory... Quitting"); | 209 | XALLOC_EXIT("<xsodium_malloc> could not allocate memory... Quitting", ); |
| 210 | 210 | ||
| 211 | return mem; | 211 | return mem; |
| 212 | } | 212 | } |
| @@ -119,7 +119,7 @@ int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char | |||
| 119 | break; | 119 | break; |
| 120 | 120 | ||
| 121 | default: | 121 | default: |
| 122 | XALLOC_EXIT("<dlinkedlist_xxxend> got an invalid op token when it shouldn't be possible to recieve one"); | 122 | XALLOC_EXIT("<dlinkedlist_xxxend> got an invalid op token when it shouldn't be possible to recieve one", ); |
| 123 | // Technically not an alloc, but also there's no reason I can't reuse a perfectly good macro | 123 | // Technically not an alloc, but also there's no reason I can't reuse a perfectly good macro |
| 124 | } | 124 | } |
| 125 | 125 | ||
diff --git a/src/shared.c b/src/shared.c index cc11fe1..771c44f 100644 --- a/src/shared.c +++ b/src/shared.c | |||
| @@ -6,20 +6,54 @@ | |||
| 6 | #include <errno.h> | 6 | #include <errno.h> |
| 7 | #include <error.h> | 7 | #include <error.h> |
| 8 | 8 | ||
| 9 | void* xcalloc(size_t nmemb, size_t size) { | 9 | static enum XALLOC_TYPE { |
| 10 | void *mem = calloc(nmemb, size); | 10 | XALLOC_INVAL, // Default when unset |
| 11 | if(!mem) | 11 | |
| 12 | XALLOC_EXIT("<xcalloc> Could not allocate memory"); | 12 | XALLOC_MALLOC, |
| 13 | 13 | XALLOC_CALLOC, | |
| 14 | return mem; | 14 | XALLOC_REALLOC, |
| 15 | |||
| 16 | XALLOC_2BIG // Out of range | ||
| 17 | }; | ||
| 18 | |||
| 19 | void * xalloc(size_t nmemb, size_t size, enum XALLOC_TYPE actype, void *ptr) { | ||
| 20 | if(actype <= XALLOC_INVAL || actype >= XALLOC_2BIG) | ||
| 21 | RETURNWERR(EINVAL, NULL); | ||
| 22 | |||
| 23 | void *mem = NULL; | ||
| 24 | switch(actype) { | ||
| 25 | case XALLOC_MALLOC: | ||
| 26 | mem = malloc(nmemb * size); | ||
| 27 | break; | ||
| 28 | |||
| 29 | case XALLOC_CALLOC: | ||
| 30 | mem = calloc(nmemb, size); | ||
| 31 | break; | ||
| 32 | |||
| 33 | case XALLOC_REALLOC: | ||
| 34 | mem = realloc(ptr, nmemb * size); | ||
| 35 | break; | ||
| 36 | |||
| 37 | default: | ||
| 38 | XALLOC_EXIT("<xalloc> An unknown alloc type was passed, which shouldn't be possible", ); | ||
| 39 | } | ||
| 40 | |||
| 41 | if(!mem) | ||
| 42 | XALLOC_EXIT("<xalloc> Could not allocate memory", ); | ||
| 43 | |||
| 44 | return mem; | ||
| 15 | } | 45 | } |
| 16 | 46 | ||
| 17 | void* xreallocarray(void *ptr, size_t nmemb, size_t size) { | 47 | void * xmalloc(size_t size) { |
| 18 | void *mem = reallocarray(ptr, nmemb, size); | 48 | return xalloc(size, 1, XALLOC_MALLOC, NULL); |
| 19 | if(mem == NULL) | 49 | } |
| 20 | XALLOC_EXIT("<xreallocarray> Could not allocate memory"); | 50 | |
| 51 | void * xcalloc(size_t nmemb, size_t size) { | ||
| 52 | return xalloc(nmemb, size, XALLOC_CALLOC, NULL); | ||
| 53 | } | ||
| 21 | 54 | ||
| 22 | return mem; | 55 | void * xreallocarray(void *ptr, size_t nmemb, size_t size) { |
| 56 | return xalloc(nmemb, size, XALLOC_REALLOC, ptr); | ||
| 23 | } | 57 | } |
| 24 | 58 | ||
| 25 | int rwbuf(char **str, unsigned long int initsize, int fd) { | 59 | int rwbuf(char **str, unsigned long int initsize, int fd) { |
| @@ -108,7 +142,7 @@ char *xdirname(const char * const path) { | |||
| 108 | if(!path) { // Path being null is a special case which should return super early, before anything else | 142 | if(!path) { // Path being null is a special case which should return super early, before anything else |
| 109 | tmp = strdup("."); | 143 | tmp = strdup("."); |
| 110 | if(!tmp) | 144 | if(!tmp) |
| 111 | XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result \"NULL\""); | 145 | XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result \"NULL\"", ); |
| 112 | 146 | ||
| 113 | return tmp; | 147 | return tmp; |
| 114 | } | 148 | } |
| @@ -120,7 +154,7 @@ char *xdirname(const char * const path) { | |||
| 120 | 154 | ||
| 121 | if(flag) { | 155 | if(flag) { |
| 122 | if(!tmp) | 156 | if(!tmp) |
| 123 | XALLOC_EXIT("<xdirname> could not strdup a set path result"); | 157 | XALLOC_EXIT("<xdirname> could not strdup a set path result", ); |
| 124 | 158 | ||
| 125 | return tmp; | 159 | return tmp; |
| 126 | } | 160 | } |
| @@ -162,14 +196,14 @@ char *xdirname(const char * const path) { | |||
| 162 | free(tmp); | 196 | free(tmp); |
| 163 | tmp = strdup("."); | 197 | tmp = strdup("."); |
| 164 | if(!tmp) | 198 | if(!tmp) |
| 165 | XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result"); | 199 | XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result", ); |
| 166 | return tmp; | 200 | return tmp; |
| 167 | } | 201 | } |
| 168 | if(count == 1) { | 202 | if(count == 1) { |
| 169 | free(tmp); | 203 | free(tmp); |
| 170 | tmp = strdup("/"); | 204 | tmp = strdup("/"); |
| 171 | if(!tmp) | 205 | if(!tmp) |
| 172 | XALLOC_EXIT("<xdirname> could not strdup \"/\" for set path result"); | 206 | XALLOC_EXIT("<xdirname> could not strdup \"/\" for set path result", ); |
| 173 | 207 | ||
| 174 | return tmp; | 208 | return tmp; |
| 175 | } | 209 | } |
| @@ -183,7 +217,7 @@ char *xdirname(const char * const path) { | |||
| 183 | 217 | ||
| 184 | char * const actual = strdup(tmp); | 218 | char * const actual = strdup(tmp); |
| 185 | if(!actual) | 219 | if(!actual) |
| 186 | XALLOC_EXIT("<xdirname> could not strdup tmp string to make a shorter end string"); | 220 | XALLOC_EXIT("<xdirname> could not strdup tmp string to make a shorter end string", ); |
| 187 | free(tmp); | 221 | free(tmp); |
| 188 | 222 | ||
| 189 | return actual; | 223 | return actual; |
diff --git a/src/shared.h b/src/shared.h index b9e7bcd..718bbf2 100644 --- a/src/shared.h +++ b/src/shared.h | |||
| @@ -29,10 +29,13 @@ | |||
| 29 | exit(EXIT_FAILURE); /* Makes gcc happy */\ | 29 | exit(EXIT_FAILURE); /* Makes gcc happy */\ |
| 30 | } while (0) | 30 | } while (0) |
| 31 | 31 | ||
| 32 | // `calloc()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` | 32 | // `malloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` |
| 33 | void * xmalloc(size_t size); | ||
| 34 | |||
| 35 | // `calloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` | ||
| 33 | void* xcalloc(size_t nmemb, size_t size); | 36 | void* xcalloc(size_t nmemb, size_t size); |
| 34 | 37 | ||
| 35 | // `reallocarray()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` | 38 | // `reallocarray()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` |
| 36 | void* xreallocarray(void *ptr, size_t nmemb, size_t size); | 39 | void* xreallocarray(void *ptr, size_t nmemb, size_t size); |
| 37 | 40 | ||
| 38 | // Read the entire contents of a file descriptor into a malloc()'ed buffer | 41 | // Read the entire contents of a file descriptor into a malloc()'ed buffer |
