libsheepy
libsheepyCSmallContainer.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"
26 
27 #define internal static
28 
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <stdio.h>
33 
38 void finalizeSmallContainer(void);
44 internal void freeSmallContainer(smallContainert *self);
45 internal void terminateSmallContainer(smallContainert **self);
46 #if (NFreeStackCheck)
47 internal void finishSmallContainer(smallContainert **self);
48 #else
49 internal void finishSmallContainer(smallContainert **self);
50 #endif
51 internal const char* helpSmallContainer(smallContainert UNUSED *self);
52 internal char* toStringSmallContainer(smallContainert *self);
60 internal void smashSmallContainer(smallContainert **self);
61 internal void* getSmallContainer(smallContainert *self);
64 void* getSmallContainerG(smallContainert *self, void* retType UNUSED, int64_t index UNUSED);
66 
68 
69  self->type = "smallContainer";
70  if (!smallContainerF) {
71  isError(smallContainerF, malloc(sizeof(smallContainerFunctionst))) {
72  self->f = NULL;
73  return;
74  }
75  registerMethodsSmallContainer(smallContainerF);
76  }
77  self->f = smallContainerF;;
78 
79  self->data = NULL;
80  self->dataFree = NULL;
81  self->dataToString = NULL;
82  self->dataDuplicate = NULL;
83 }
84 
86 
92  f->help = helpSmallContainer;
93  f->dataFree = NULL;
94  f->setClassDataFree = setClassDataFreeSmallContainer;
95  f->setObjectDataFree = setObjectDataFreeSmallContainer;
96  f->dataToString = NULL;
97  f->setClassDataToString = setClassDataToStringSmallContainer;
98  f->setObjectDataToString = setObjectDataToStringSmallContainer;
99  f->dataDuplicate = NULL;
100  f->setClassDataDuplicate = setClassDataDuplicateSmallContainer;
101  f->setObjectDataDuplicate = setObjectDataDuplicateSmallContainer;
103  f->get = getSmallContainer;
104  f->set = setSmallContainer;
105 }
106 
108 
109  if (self) {
110  #if (recycleContainers)
111  initAllocateRecycle(smallContainert);
112  #else
113  isError(*self, malloc(sizeof(smallContainert)))
114  return;
115  #endif
116  if (*self) {
117  initiateSmallContainer(*self);
118  if (!(*self)->f) {
119  finishSmallContainer(self);
120 }
121  }
122  }
123  }
124 
126 
127  #if (recycleContainers)
128  finalizeRecycle
129  #endif
130  // recycleContainers
131 }
132 
134 
135  if (smallContainerF) {
136  free(smallContainerF);
137  smallContainerF = NULL;
138  }
140 }
141 
143  smallContainert *r = NULL;
144 
146  if (!r) {
147  return(NULL);
148  }
149  r->data = allocSContainer(data);
150  return(r);
151 }
152 
154 
155  terminateO(*val);
156 }
157 
159 
160  freeO(val);
161 }
162 
164 
165  freeO(*val);
166 }
167 
169 
170  finishO(*val);
171 }
172 
173 internal void freeSmallContainer(smallContainert *self) {
174 
175  if (self->data) {
176  // free with sContainert free function
177  if (self->data->free) {
178  self->data->free(self->data->data);
179  }
180  else if (self->dataFree) {
181  self->dataFree(self->data->data);
182  }
183  else if (self->f->dataFree) {
184  self->f->dataFree(self->data->data);
185  }
186  free(self->data);
187  self->data = NULL;
188  }
189  return;
190 }
191 
193 
194  freeSmallContainer(*self);
195  finishSmallContainer(self);
196 }
197 
198 #if (NFreeStackCheck)
199 internal void finishSmallContainer(smallContainert **self) {
200 
201  register u64 rsp asm("rsp");
202  if ((u64)*self > rsp) {
203  logW("Probably trying to free a smallContainer on stack: "BLD PRIx64 RST" sp: "BLD PRIx64 RST, *self, rsp);
204  logBtrace;
205  }
206  else {
207  #if (recycleContainers)
208  finishRecycle
209  #else
210  free(*self);
211  #endif
212  // recycleContainers
213  *self = NULL;
214 }
215  }
216 
217 #else
218 // #if NFreeStackCheck
219 internal void finishSmallContainer(smallContainert **self) {
220 
221  #if (recycleContainers)
222  finishRecycle
223  #else
224  free(*self);
225  #endif
226  // recycleContainers
227  *self = NULL;
228 }
229 
230 #endif
231 // #if NFreeStackCheck
232 
233 internal const char* helpSmallContainer(smallContainert UNUSED *self) {
234 
235  return(helpTextSmallContainer);
236 }
237 
238 
240 
241  if (!self->data) {
242  return(NULL);
243  }
244  if (self->dataToString) {
245  return(self->dataToString(self->data->data));
246  }
247  if (self->f->dataToString) {
248  return(self->f->dataToString(self->data->data));
249  }
250  return(strdup("<data smallContainer>"));
251 }
252 
254 
256  if (!dup) {
257  return(NULL);
258  }
259  if (self->data) {
260  isError(dup->data, allocSContainer(NULL)) return(NULL);
261  if (self->dataDuplicate) {
262  dup->data->data = self->dataDuplicate(self->data->data);
263  }
264  else if (self->f->dataDuplicate) {
265  dup->data->data = self->f->dataDuplicate(self->data->data);
266  }
267  dup->data->free = self->data->free;
268  }
269  dup->dataFree = self->dataFree;
270  dup->dataToString = self->dataToString;
271  dup->dataDuplicate = self->dataDuplicate;
272  return(dup);
273 }
274 
276 
277  self->f->dataFree = free;
278  if (self->data) {
279  self->data->free = (freeSContainerFt)free;;
280  }
281  return(self);
282 }
283 
285 
286  self->dataFree = free;
287  if (self->data) {
288  self->data->free = (freeSContainerFt)free;;
289  }
290  return(self);
291 }
292 
294 
295  self->f->dataToString = toString;
296  return(self);
297 }
298 
300 
301  self->dataToString = toString;
302  return(self);
303 }
304 
306 
307  self->f->dataDuplicate = duplicate;
308  return(self);
309 }
310 
312 
313  self->dataDuplicate = duplicate;
314  return(self);
315 }
316 
317 internal void smashSmallContainer(smallContainert **self) {
318 
319  if ((*self)->data) {
320  free((*self)->data);
321  }
322  finishSmallContainer(self);
323 }
324 
325 internal void* getSmallContainer(smallContainert *self) {
326 
327  if (!self->data) {
328  return(NULL);
329  }
330  return(self->data->data);
331 }
332 
334 
335  if (!self->data) {
336  isError(self->data, allocSContainer(data)) return(NULL);
337  if (self->f->dataFree) {
338  // set free function from class
339  self->data->free = (freeSContainerFt)self->f->dataFree;
340  }
341  }
342  else {
343  self->data->data = data;
344  }
345  return(self);
346 }
347 
349 
350  return(self->f->duplicate(self));
351 }
352 
353 void freeSmallContainerG(smallContainert *self) {self->f->free(self);}
354 
355 void* getSmallContainerG(smallContainert *self, void* retType UNUSED, int64_t index UNUSED) {
356 
357  return(self->f->get(self));
358 }
359 
361 
362  return(self->f->set(self, data));
363 }
void * getSmallContainerG(smallContainert *self, void *retType UNUSED, int64_t index UNUSED)
smallContainert * duplicateSmallContainerG(smallContainert *self)
internal smallContainert * setClassDataToStringSmallContainer(smallContainert *self, dataToStringSmallContainerFt toString)
internal smallContainert * setSmallContainer(smallContainert *self, void *data)
internal smallContainert * setClassDataDuplicateSmallContainer(smallContainert *self, dataDuplicateSmallContainerFt duplicate)
smallContainert * setSmallContainerG(smallContainert *self, void *data)
free(s)
void(* dataFreeSmallContainerFt)(void *data)
prototype for user provided free function
#define RST
reset for color function
Definition: libsheepy.h:763
char *(* dataToStringSmallContainerFt)(void *data)
prototype for user provided toString function
smallContainert * allocSmallContainer(void *data)
toStringSmallContainerFt toString
#define u64
Definition: libsheepy.h:448
internal smallContainert * duplicateSmallContainer(smallContainert *self)
#define helpTextSmallContainer
help text for this class It is public declaration so that child classes can add their help text easil...
internal smallContainert * setClassDataFreeSmallContainer(smallContainert *self, dataFreeSmallContainerFt free)
char data[7]
Definition: libsheepy.c:19690
internal void * getSmallContainer(smallContainert *self)
#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
void(* freeSContainerFt)(void *data)
small container holding a pointer to a buffer
internal smallContainert * setObjectDataToStringSmallContainer(smallContainert *self, dataToStringSmallContainerFt toString)
dataFreeSmallContainerFt dataFree
internal void terminateSmallContainer(smallContainert **self)
void cleanUpSmallContainerFinishG(smallContainert **val)
internal char * toStringSmallContainer(smallContainert *self)
internal void freeSmallContainer(smallContainert *self)
#define terminateO(obj)
free buffers and obj itself
void cleanUpSmallContainerTerminateG(smallContainert **val)
sContainert * allocSContainer(void *data)
allocate a small container The container holds a pointer to a user buffer
#define logBtrace
Definition: libsheepy.h:3249
void *(* dataDuplicateSmallContainerFt)(void *data)
prototype for user provided duplicate function
void finalizeRecycleSmallContainer(void *arg UNUSED)
sContainert * data
pointer to data in smallContainer
void initiateSmallContainer(smallContainert *self)
dataToStringSmallContainerFt dataToString
smallContainerFunctionst * f
void finalizeSmallContainer(void)
void freeSmallContainerG(smallContainert *self)
#define BLD
bold for color function
Definition: libsheepy.h:765
internal void smashSmallContainer(smallContainert **self)
void registerMethodsSmallContainer(smallContainerFunctionst *f)
internal const char * helpSmallContainer(smallContainert UNUSED *self)
duplicateSmallContainerFt duplicate
void initiateAllocateSmallContainer(smallContainert **self)
createAllocateSmallContainer(jcontainer)
#define UNUSED
Definition: libsheepy.h:8195
void cleanUpSmallContainerFreeLocalG(smallContainert *val)
internal void finishSmallContainer(smallContainert **self)
bool r
#define logW(...)
Definition: libsheepy.h:1061
void cleanUpSmallContainerFreeG(smallContainert **val)
#define freeO(obj)
free buffers in obj
dataDuplicateSmallContainerFt dataDuplicate
internal smallContainert * setObjectDataFreeSmallContainer(smallContainert *self, dataFreeSmallContainerFt free)
internal smallContainert * setObjectDataDuplicateSmallContainer(smallContainert *self, dataDuplicateSmallContainerFt duplicate)
terminateSmallContainerFt terminate
#define finishO(obj)
free container only