GCC Code Coverage Report


Directory: src/gate/
File: src/gate/structs.c
Date: 2026-10-19 17:06:30
Exec Total Coverage
Lines: 92 177 52.0%
Functions: 15 18 83.3%
Branches: 34 87 39.1%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28 #include "gate/structs.h"
29 #include "gate/results.h"
30 #include "gate/objects.h"
31 #include "gate/arrays.h"
32 #include "gate/guids.h"
33 #include "gate/times.h"
34 #include "gate/blobs.h"
35 #include "gate/utilities.h"
36 #include "gate/properties.h"
37
38
39 15 gate_result_t gate_struct_item_init(gate_uint16_t type, void* ptr)
40 {
41 15 gate_result_t ret = GATE_RESULT_OK;
42 15 gate_size_t type_length = gate_type_length(type);
43
44
3/29
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 2 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✓ Branch 24 taken 4 times.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✓ Branch 28 taken 9 times.
15 switch (type)
45 {
46 ✗ case GATE_TYPE_ARRAYLIST_BOOL: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_bool_t), NULL, 0, NULL, NULL); break;
47 ✗ case GATE_TYPE_ARRAYLIST_I8: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_int8_t), NULL, 0, NULL, NULL); break;
48 ✗ case GATE_TYPE_ARRAYLIST_UI8: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_uint8_t), NULL, 0, NULL, NULL); break;
49 ✗ case GATE_TYPE_ARRAYLIST_I16: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_int16_t), NULL, 0, NULL, NULL); break;
50 ✗ case GATE_TYPE_ARRAYLIST_UI16: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_uint16_t), NULL, 0, NULL, NULL); break;
51 ✗ case GATE_TYPE_ARRAYLIST_I32: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_int32_t), NULL, 0, NULL, NULL); break;
52 ✗ case GATE_TYPE_ARRAYLIST_UI32: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_uint32_t), NULL, 0, NULL, NULL); break;
53 ✗ case GATE_TYPE_ARRAYLIST_I64: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_int64_t), NULL, 0, NULL, NULL); break;
54 ✗ case GATE_TYPE_ARRAYLIST_UI64: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_uint64_t), NULL, 0, NULL, NULL); break;
55 ✗ case GATE_TYPE_ARRAYLIST_R32: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_real32_t), NULL, 0, NULL, NULL); break;
56 ✗ case GATE_TYPE_ARRAYLIST_R64: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_real64_t), NULL, 0, NULL, NULL); break;
57 ✗ case GATE_TYPE_ARRAYLIST_ADDRESS: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_uintptr_t), NULL, 0, NULL, NULL); break;
58
59 /* arraylist of basic gate structures (POD) */
60 ✗ case GATE_TYPE_ARRAYLIST_DATAPTR: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_dataptr_t), NULL, 0, NULL, NULL); break;
61 ✗ case GATE_TYPE_ARRAYLIST_FUNCPTR: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_funcptr_t), NULL, 0, NULL, NULL); break;
62 ✗ case GATE_TYPE_ARRAYLIST_CSTR: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(char const*), NULL, 0, NULL, NULL); break;
63 ✗ case GATE_TYPE_ARRAYLIST_WSTR: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(wchar_t const*), NULL, 0, NULL, NULL); break;
64 ✗ case GATE_TYPE_ARRAYLIST_GUID: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_guid_t), NULL, 0, NULL, NULL); break;
65 ✗ case GATE_TYPE_ARRAYLIST_DATE: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_date_t), NULL, 0, NULL, NULL); break;
66 ✗ case GATE_TYPE_ARRAYLIST_DAYTIME: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_daytime_t), NULL, 0, NULL, NULL); break;
67 ✗ case GATE_TYPE_ARRAYLIST_DATETIME: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_datetime_t), NULL, 0, NULL, NULL); break;
68 ✗ case GATE_TYPE_ARRAYLIST_TIME: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_time_t), NULL, 0, NULL, NULL); break;
69
70 /* arraylist of basic generic types */
71 2 case GATE_TYPE_ARRAYLIST_STRING: *((gate_arraylist_t*)(ptr)) = gate_util_stringarray_create(); break;
72 ✗ case GATE_TYPE_ARRAYLIST_ARRAY: ((gate_arraylist_t*)(ptr))->ptr = NULL; break; /* must be initialized individually */
73 ✗ case GATE_TYPE_ARRAYLIST_BLOB: *((gate_arraylist_t*)(ptr)) = gate_util_blobarray_create(); break;
74
75 /* arraylist of basic reference types */
76 4 case GATE_TYPE_ARRAYLIST_STRUCT: ((gate_arraylist_t*)(ptr))->ptr = NULL; break; /* must be initialized individually */
77 ✗ case GATE_TYPE_ARRAYLIST_OBJECT: *((gate_object_ptr_t*)(ptr)) = NULL; break; /* must be initialized individually */
78 ✗ case GATE_TYPE_ARRAYLIST_PROPERTY: gate_property_create_empty((gate_property_t*)(ptr)); break;
79 ✗ case GATE_TYPE_STRUCT: ((gate_struct_t*)(ptr))->struct_descriptor = NULL; break; /* must be initialized individually */
80 9 default:
81 {
82 9 gate_mem_clear(ptr, type_length);
83 9 break;
84 }
85 }
86 15 return ret;
87 }
88
89 2548 gate_result_t gate_struct_item_release(gate_uint16_t type, void* ptr)
90 {
91 2548 gate_result_t ret = GATE_RESULT_OK;
92 2548 gate_size_t type_length = gate_type_length(type);
93 gate_struct_ptr_t struct_ptr;
94
95
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 2115 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 433 times.
2548 switch (type)
96 {
97 ✗ case GATE_TYPE_STRUCT:
98 {
99 ✗ struct_ptr = *((gate_struct_ptr_t*)ptr);
100 ✗ ret = gate_struct_release(struct_ptr);
101 ✗ break;
102 }
103 2115 case GATE_TYPE_STRING:
104 {
105 2115 gate_string_release((gate_string_t*)ptr);
106 2115 break;
107 }
108 ✗ case GATE_TYPE_BLOB:
109 {
110 ✗ gate_blob_release((gate_blob_t*)ptr);
111 ✗ break;
112 }
113 ✗ case GATE_TYPE_OBJECT:
114 {
115 ✗ gate_object_release(*((gate_object_ptr_t*)ptr));
116 ✗ break;
117 }
118 ✗ case GATE_TYPE_PROPERTY:
119 {
120 ✗ gate_property_destroy((gate_property_t*)ptr);
121 ✗ break;
122 }
123 ✗ case GATE_TYPE_ARRAY:
124 {
125 ✗ gate_array_release((gate_array_t*)ptr);
126 ✗ break;
127 }
128 ✗ case GATE_TYPE_EMPTY:
129 {
130 ✗ break;
131 }
132 433 default:
133 {
134
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 416 times.
433 if (GATE_FLAG_ENABLED(type, GATE_TYPE_ARRAYLIST))
135 {
136 17 gate_arraylist_release((gate_arraylist_t*)ptr);
137 }
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 416 times.
416 else if (type_length == 0)
139 {
140 ✗ ret = GATE_RESULT_FAILED;
141 }
142 else
143 {
144 416 gate_mem_clear(ptr, type_length);
145 }
146 433 break;
147 }
148 }
149 2548 return ret;
150 }
151
152
153
154 3 gate_result_t gate_struct_init(gate_struct_t* obj, gate_struct_descriptor_t const* descriptor)
155 {
156 3 gate_result_t ret = GATE_RESULT_FAILED;
157 3 char* obj_ptr = (char*)obj;
158 char* data_ptr;
159 size_t index;
160
161 do
162 {
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (obj == NULL)
164 {
165 ✗ ret = GATE_RESULT_INVALIDARG;
166 ✗ break;
167 }
168
169
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (descriptor != NULL)
170 {
171 3 obj->struct_descriptor = descriptor;
172 }
173 else
174 {
175 ✗ descriptor = obj->struct_descriptor;
176 }
177
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (obj->struct_descriptor == NULL)
179 {
180 ✗ ret = GATE_RESULT_INVALIDARG;
181 ✗ break;
182 }
183
184 3 ret = GATE_RESULT_OK;
185
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
18 for (index = 0; index != descriptor->item_count; ++index)
186 {
187 15 data_ptr = obj_ptr + descriptor->items[index].offset;
188 15 ret = gate_struct_item_init(descriptor->items[index].type, data_ptr);
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (GATE_FAILED(ret))
190 {
191 /* release all previous created data */
192 ✗ while (index != 0)
193 {
194 ✗ --index;
195 ✗ data_ptr = obj_ptr + descriptor->items[index].offset;
196 ✗ gate_struct_item_release(descriptor->items[index].type, data_ptr);
197 }
198 ✗ break;
199 }
200 }
201 } while (0);
202
203 3 return ret;
204 }
205
206 244 gate_result_t gate_struct_copy(gate_struct_t* dst, gate_struct_t const* src)
207 {
208 244 gate_result_t ret = GATE_RESULT_FAILED;
209 244 char* ptr_dst = (char*)dst;
210 244 char const* ptr_src = (char const*)src;
211 gate_size_t index;
212
213
1/2
✓ Branch 0 taken 244 times.
✗ Branch 1 not taken.
244 if (dst->struct_descriptor == NULL)
214 {
215 244 dst->struct_descriptor = src->struct_descriptor;
216 }
217
218 do
219 {
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 244 times.
244 if (dst->struct_descriptor->item_count != src->struct_descriptor->item_count)
221 {
222 ✗ ret = GATE_RESULT_INCORRECTTYPE;
223 ✗ break;
224 }
225
226 244 ret = GATE_RESULT_OK;
227
228
2/2
✓ Branch 0 taken 1952 times.
✓ Branch 1 taken 244 times.
2196 for (index = 0; index != src->struct_descriptor->item_count; ++index)
229 {
230
1/2
✓ Branch 0 taken 1952 times.
✗ Branch 1 not taken.
1952 if (dst->struct_descriptor->items[index].type == src->struct_descriptor->items[index].type)
231 {
232 1952 ret = gate_type_copy(dst->struct_descriptor->items[index].type,
233 1952 ptr_dst + dst->struct_descriptor->items[index].offset,
234 1952 ptr_src + src->struct_descriptor->items[index].offset);
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1952 times.
1952 if (GATE_FAILED(ret))
236 {
237 ✗ while (index-- != 0)
238 {
239 ✗ gate_type_release(dst->struct_descriptor->items[index].type, ptr_dst + dst->struct_descriptor->items[index].offset);
240 }
241 ✗ break;
242 }
243 }
244 else
245 {
246 ✗ ret = GATE_RESULT_INCORRECTTYPE;
247 ✗ break;
248 }
249 }
250
251 } while (0);
252
253
254 244 return ret;
255 }
256
257 2 gate_size_t gate_struct_length(gate_struct_t const* src)
258 {
259 2 return src->struct_descriptor->struct_length;
260 }
261
262 3 char const* gate_struct_get_name(gate_struct_t const* src)
263 {
264 3 return src->struct_descriptor->name;
265 }
266
267 19 gate_size_t gate_struct_get_member_count(gate_struct_t const* src)
268 {
269 19 return src->struct_descriptor->item_count;
270 }
271
272 73 gate_uint16_t gate_struct_get_member_type(gate_struct_t const* src, gate_size_t index)
273 {
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (index >= src->struct_descriptor->item_count)
275 {
276 ✗ return 0;
277 }
278 73 return src->struct_descriptor->items[index].type;
279 }
280 1 gate_uint16_t gate_struct_get_member_type_by_string(gate_struct_t const* src, gate_string_t const* name)
281 {
282 gate_size_t index;
283 gate_size_t len;
284
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 for (index = 0; index != src->struct_descriptor->item_count; ++index)
285 {
286 2 len = gate_str_length(src->struct_descriptor->items[index].name);
287
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 if (0 == gate_str_compare(src->struct_descriptor->items[index].name, len, name->str, name->length))
288 {
289 1 return src->struct_descriptor->items[index].type;
290 }
291 }
292 ✗ return GATE_TYPE_EMPTY;
293 }
294 ✗ gate_uint16_t gate_struct_get_member_type_by_name(gate_struct_t const* src, char const* name)
295 {
296 ✗ gate_uint16_t type = GATE_TYPE_EMPTY;
297 ✗ gate_string_t str_name = GATE_STRING_INIT_EMPTY;
298 ✗ gate_string_create_static(&str_name, name);
299 ✗ type = gate_struct_get_member_type_by_string(src, &str_name);
300 ✗ gate_string_release(&str_name);
301 ✗ return type;
302 }
303
304
305 74 char const* gate_struct_get_member_name(gate_struct_t const* src, gate_size_t index)
306 {
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 if (index >= src->struct_descriptor->item_count)
308 {
309 ✗ return NULL;
310 }
311 74 return src->struct_descriptor->items[index].name;
312 }
313
314 79 void const* gate_struct_get_member(gate_struct_t const* src, gate_size_t index)
315 {
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 if (index >= src->struct_descriptor->item_count)
317 {
318 ✗ return NULL;
319 }
320 79 return (void const*)(((char const*)src) + src->struct_descriptor->items[index].offset);
321 }
322
323 6 void const* gate_struct_get_member_by_string(gate_struct_t const* src, gate_string_t const* name)
324 {
325 gate_size_t index;
326 gate_size_t len;
327
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 for (index = 0; index != src->struct_descriptor->item_count; ++index)
328 {
329 21 len = gate_str_length(src->struct_descriptor->items[index].name);
330
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 15 times.
21 if (0 == gate_str_compare(src->struct_descriptor->items[index].name, len, name->str, name->length))
331 {
332 6 return (void const*)(((char const*)src) + src->struct_descriptor->items[index].offset);
333 }
334 }
335 ✗ return NULL;
336 }
337
338 ✗ void const* gate_struct_get_member_by_name(gate_struct_t const* src, char const* name)
339 {
340 ✗ void const* ret = NULL;
341 ✗ gate_string_t str_name = GATE_STRING_INIT_EMPTY;
342 ✗ if (NULL != gate_string_create_static(&str_name, name))
343 {
344 ✗ ret = gate_struct_get_member_by_string(src, &str_name);
345 ✗ gate_string_release(&str_name);
346 }
347 ✗ return ret;
348 }
349
350 1 void* gate_struct_get_mutable_member(gate_struct_t* src, gate_size_t index)
351 {
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (index >= src->struct_descriptor->item_count)
353 {
354 ✗ return NULL;
355 }
356 1 return (void*)(((char*)src) + src->struct_descriptor->items[index].offset);
357 }
358
359
360 330 gate_result_t gate_struct_release(gate_struct_ptr_t obj)
361 {
362 330 gate_result_t ret = GATE_RESULT_OK;
363 330 char* obj_ptr = (char*)obj;
364
2/2
✓ Branch 0 taken 329 times.
✓ Branch 1 taken 1 times.
330 if (obj->struct_descriptor)
365 {
366 329 gate_size_t index = 0;
367
2/2
✓ Branch 0 taken 2548 times.
✓ Branch 1 taken 329 times.
2877 for (index = 0; index != obj->struct_descriptor->item_count; ++index)
368 {
369 2548 char* data_ptr = obj_ptr + obj->struct_descriptor->items[index].offset;
370 2548 gate_struct_item_release(obj->struct_descriptor->items[index].type, data_ptr);
371 }
372 }
373 330 return ret;
374 }
375
376 ✗ gate_result_t gate_struct_copy_constructor(void* dest, void const* src)
377 {
378 ✗ gate_result_t result = gate_struct_copy((gate_struct_t*)dest, (gate_struct_t const*)src);
379 ✗ return result;
380 }
381
382 102 void gate_struct_destructor(void* dest)
383 {
384 102 gate_struct_release((gate_struct_ptr_t)dest);
385 102 }
386