libsheepyCUndefined.c (4988B)
1 // MIT License 2 // 3 // Copyright (c) 2026 Remy Noulin 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 #include "../libsheepyObject.h" 24 #include "libsheepyCUndefined.h" 25 #include "libsheepyCUndefinedInternal.h" 26 27 #define internal static 28 29 #include <stdlib.h> 30 #include <string.h> 31 #include <stdio.h> 32 33 void initiateUndefined(undefinedt *self); 34 void initiateAllocateUndefined(undefinedt **self); 35 void finalizeRecycleUndefined(void *arg UNUSED); 36 void finalizeUndefined(void); 37 undefinedt* allocUndefined(void); 38 void cleanUpUndefinedTerminateG(undefinedt **val); 39 void cleanUpUndefinedFreeLocalG(undefinedt *val); 40 void cleanUpUndefinedFreeG(undefinedt **val); 41 void cleanUpUndefinedFinishG(undefinedt **val); 42 void cleanUpUndefinedSmashG(undefinedt **val); 43 internal void freeUndefined(undefinedt *self UNUSED); 44 internal void terminateUndefined(undefinedt **self); 45 internal char* toStringUndefined(undefinedt *self UNUSED); 46 internal undefinedt* duplicateUndefined(undefinedt *self UNUSED); 47 #if (NFreeStackCheck) 48 internal void finishUndefined(undefinedt **self); 49 #else 50 internal void finishUndefined(undefinedt **self); 51 #endif 52 undefinedt* duplicateUndefinedG (undefinedt *self); 53 54 void initiateUndefined(undefinedt *self) { 55 56 self->type = "undefined"; 57 if (!undefinedF) { 58 isError(undefinedF, malloc(sizeof(undefinedFunctionst))) { 59 self->f = NULL; 60 return; 61 } 62 undefinedF->free = freeUndefined; 63 undefinedF->terminate = terminateUndefined; 64 undefinedF->toString = toStringUndefined; 65 undefinedF->duplicate = duplicateUndefined; 66 undefinedF->smash = terminateUndefined; 67 undefinedF->finish = finishUndefined; 68 } 69 self->f = undefinedF; 70 } 71 72 void initiateAllocateUndefined(undefinedt **self) { 73 74 if (self) { 75 #if (recycleContainers) 76 initAllocateRecycle(undefinedt); 77 #else 78 isError(*self, malloc(sizeof(undefinedt))) 79 return; 80 #endif 81 if (*self) { 82 initiateUndefined(*self); 83 if (!(*self)->f) { 84 finishUndefined(self); 85 } 86 } 87 } 88 } 89 90 void finalizeRecycleUndefined(void *arg UNUSED) { 91 92 #if (recycleContainers) 93 finalizeRecycle 94 #endif 95 // recycleContainers 96 } 97 98 void finalizeUndefined(void) { 99 100 if (undefinedF) { 101 free(undefinedF); 102 undefinedF = NULL; 103 } 104 finalizeRecycleUndefined(NULL); 105 } 106 107 undefinedt* allocUndefined(void) { 108 undefinedt *r = NULL; 109 110 initiateAllocateUndefined(&r); 111 if (!r) { 112 return(NULL); 113 } 114 return(r); 115 } 116 117 void cleanUpUndefinedTerminateG(undefinedt **val) { 118 119 terminateO(*val); 120 } 121 122 void cleanUpUndefinedFreeLocalG(undefinedt *val) { 123 124 freeO(val); 125 } 126 127 void cleanUpUndefinedFreeG(undefinedt **val) { 128 129 freeO(*val); 130 } 131 132 void cleanUpUndefinedFinishG(undefinedt **val) { 133 134 finishO(*val); 135 } 136 137 void cleanUpUndefinedSmashG(undefinedt **val) { 138 139 smashO(*val); 140 } 141 142 internal void freeUndefined(undefinedt *self UNUSED) { 143 144 return; 145 } 146 147 internal void terminateUndefined(undefinedt **self) { 148 149 finishUndefined(self); 150 } 151 152 153 internal char* toStringUndefined(undefinedt *self UNUSED) { 154 155 return(strdup("null")); 156 } 157 158 internal undefinedt* duplicateUndefined(undefinedt *self UNUSED) { 159 160 createAllocateUndefined(dup); 161 if (!dup) { 162 return(NULL); 163 } 164 return(dup); 165 } 166 167 #if (NFreeStackCheck) 168 internal void finishUndefined(undefinedt **self) { 169 170 register u64 rsp asm("rsp"); 171 if ((u64)*self > rsp) { 172 logW("Probably trying to free a undefined on stack: "BLD PRIx64 RST" sp: "BLD PRIx64 RST, *self, rsp); 173 logBtrace; 174 } 175 else { 176 #if (recycleContainers) 177 finishRecycle 178 #else 179 free(*self); 180 #endif 181 // recycleContainers 182 *self = NULL; 183 } 184 } 185 186 #else 187 // #if NFreeStackCheck 188 internal void finishUndefined(undefinedt **self) { 189 190 #if (recycleContainers) 191 finishRecycle 192 #else 193 free(*self); 194 #endif 195 // recycleContainers 196 *self = NULL; 197 } 198 199 #endif 200 // #if NFreeStackCheck 201 202 undefinedt* duplicateUndefinedG (undefinedt *self) { 203 204 return(self->f->duplicate(self)); 205 } 206 207 void freeUndefinedG (undefinedt *self) {self->f->free(self);}