libsheepy
libsheepyCUndefined.c
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2023 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"
26 
27 #define internal static
28 
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 
33 void initiateUndefined(undefinedt *self);
35 void finalizeRecycleUndefined(void *arg UNUSED);
36 void finalizeUndefined(void);
43 internal void freeUndefined(undefinedt *self UNUSED);
44 internal void terminateUndefined(undefinedt **self);
45 internal char* toStringUndefined(undefinedt *self UNUSED);
47 #if (NFreeStackCheck)
48 internal void finishUndefined(undefinedt **self);
49 #else
50 internal void finishUndefined(undefinedt **self);
51 #endif
53 
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 
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 
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  }
105 }
106 
108  undefinedt *r = NULL;
109 
111  if (!r) {
112  return(NULL);
113  }
114  return(r);
115 }
116 
118 
119  terminateO(*val);
120 }
121 
123 
124  freeO(val);
125 }
126 
128 
129  freeO(*val);
130 }
131 
133 
134  finishO(*val);
135 }
136 
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 
159 
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 
203 
204  return(self->f->duplicate(self));
205 }
206 
207 void freeUndefinedG (undefinedt *self) {self->f->free(self);}
void finalizeRecycleUndefined(void *arg UNUSED)
free(s)
#define RST
reset for color function
Definition: libsheepy.h:763
#define u64
Definition: libsheepy.h:448
internal undefinedt * duplicateUndefined(undefinedt *self UNUSED)
void cleanUpUndefinedSmashG(undefinedt **val)
#define isError(assigned, left)
is Assigment Error catch error when assigned is false, 0 or NULL after being assigned with left ...
Definition: libsheepy.h:386
#define terminateO(obj)
free buffers and obj itself
void cleanUpUndefinedFinishG(undefinedt **val)
#define logBtrace
Definition: libsheepy.h:3249
class functions allocated once for all objects
#define createAllocateUndefined(obj)
internal char * toStringUndefined(undefinedt *self UNUSED)
void cleanUpUndefinedFreeLocalG(undefinedt *val)
internal void freeUndefined(undefinedt *self UNUSED)
void freeUndefinedG(undefinedt *self)
internal void terminateUndefined(undefinedt **self)
undefinedt * duplicateUndefinedG(undefinedt *self)
#define BLD
bold for color function
Definition: libsheepy.h:765
undefinedt * allocUndefined(void)
void cleanUpUndefinedTerminateG(undefinedt **val)
#define UNUSED
Definition: libsheepy.h:8195
internal void finishUndefined(undefinedt **self)
bool r
#define logW(...)
Definition: libsheepy.h:1061
#define freeO(obj)
free buffers in obj
void cleanUpUndefinedFreeG(undefinedt **val)
void finalizeUndefined(void)
void initiateUndefined(undefinedt *self)
#define finishO(obj)
free container only
void initiateAllocateUndefined(undefinedt **self)
#define smashO(obj)
free object and keep data