annotate dyncall/dyncall_aggregate.c @ 663:127b569978cc default tip

- another tweak handling clang trying to be too smart (see last commit)
author Tassilo Philipp
date Sun, 24 Mar 2024 13:52:44 +0100
parents 1539ebfabebb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
533
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
1 /*
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
2
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
3 Package: dyncall
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
4 Library: dyncall
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
5 File: dyncall/dyncall_aggregate.c
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
6 Description: C interface to compute struct size
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
7 License:
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
8
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
9 Copyright (c) 2021-2022 Tassilo Philipp <tphilipp@potion-studios.com>
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
10
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
11 Permission to use, copy, modify, and distribute this software for any
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
12 purpose with or without fee is hereby granted, provided that the above
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
13 copyright notice and this permission notice appear in all copies.
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
14
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
15 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
16 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
17 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
18 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
22
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
23 */
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
24
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
25
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
26
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
27 #include "dyncall.h"
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
28 #include "dyncall_signature.h"
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
29 #include "dyncall_aggregate.h"
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
30 #include "dyncall_alloc.h"
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
31 #include <stdio.h>
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
32 #include <assert.h>
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
33 #include <stdarg.h>
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
34
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
35
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
36
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
37 #if defined(DC__Arch_AMD64) && defined(DC_UNIX)
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
38 # include "dyncall_aggregate_x64.c"
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
39 #else
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
40 static void dcFinishAggr(DCaggr *ag)
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
41 {
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
42 }
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
43 #endif
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
44
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
45
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
46 DCaggr* dcNewAggr(DCsize maxFieldCount, DCsize size)
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
47 {
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
48 DCaggr* ag = (DCaggr*)dcAllocMem(sizeof(DCaggr) + maxFieldCount * sizeof(DCfield));
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
49 ag->n_fields = 0;
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
50 ag->size = size;
546
Tassilo Philipp
parents: 533
diff changeset
51 ag->alignment = 0;
533
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
52 return ag;
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
53 }
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
54
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
55
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
56 void dcAggrField(DCaggr* ag, DCsigchar type, DCint offset, DCsize array_len, ...)
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
57 {
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
58 DCfield *f = ag->fields + (ag->n_fields++);
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
59 f->type = type;
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
60 f->offset = offset;
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
61 f->array_len = array_len;
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
62 f->sub_aggr = NULL;
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
63 switch(type) {
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
64 case DC_SIGCHAR_BOOL: f->size = sizeof(DCbool); break;
601
1539ebfabebb cosmetics
Tassilo Philipp
parents: 551
diff changeset
65 case DC_SIGCHAR_CHAR:
533
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
66 case DC_SIGCHAR_UCHAR: f->size = sizeof(DCchar); break;
601
1539ebfabebb cosmetics
Tassilo Philipp
parents: 551
diff changeset
67 case DC_SIGCHAR_SHORT:
533
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
68 case DC_SIGCHAR_USHORT: f->size = sizeof(DCshort); break;
601
1539ebfabebb cosmetics
Tassilo Philipp
parents: 551
diff changeset
69 case DC_SIGCHAR_INT:
533
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
70 case DC_SIGCHAR_UINT: f->size = sizeof(DCint); break;
601
1539ebfabebb cosmetics
Tassilo Philipp
parents: 551
diff changeset
71 case DC_SIGCHAR_LONG:
533
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
72 case DC_SIGCHAR_ULONG: f->size = sizeof(DClong); break;
601
1539ebfabebb cosmetics
Tassilo Philipp
parents: 551
diff changeset
73 case DC_SIGCHAR_LONGLONG:
533
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
74 case DC_SIGCHAR_ULONGLONG: f->size = sizeof(DClonglong); break;
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
75 case DC_SIGCHAR_FLOAT: f->size = sizeof(DCfloat); break;
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
76 case DC_SIGCHAR_DOUBLE: f->size = sizeof(DCdouble); break;
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
77 case DC_SIGCHAR_POINTER:
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
78 case DC_SIGCHAR_STRING: f->size = sizeof(DCpointer); break;
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
79 case DC_SIGCHAR_AGGREGATE:
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
80 {
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
81 va_list ap;
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
82 va_start(ap, array_len);
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
83 f->sub_aggr = va_arg(ap, const DCaggr*);
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
84 va_end(ap);
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
85
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
86 f->size = f->sub_aggr->size;
551
eef302b7a58d - amendment fix for buffer overflow (see commit 0455834d29a1), to also handle non-standard struct packing, + better asserts
Tassilo Philipp
parents: 546
diff changeset
87 f->alignment = f->sub_aggr->alignment; /* field inherit's sub aggrs alignment*/
533
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
88 break;
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
89 }
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
90 default:
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
91 assert(0);
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
92 }
546
Tassilo Philipp
parents: 533
diff changeset
93
Tassilo Philipp
parents: 533
diff changeset
94 if(type != DC_SIGCHAR_AGGREGATE)
Tassilo Philipp
parents: 533
diff changeset
95 f->alignment = f->size;
Tassilo Philipp
parents: 533
diff changeset
96
551
eef302b7a58d - amendment fix for buffer overflow (see commit 0455834d29a1), to also handle non-standard struct packing, + better asserts
Tassilo Philipp
parents: 546
diff changeset
97 /* aggr's field alignment is relative to largest field size */
546
Tassilo Philipp
parents: 533
diff changeset
98 if(ag->alignment < f->alignment)
Tassilo Philipp
parents: 533
diff changeset
99 ag->alignment = f->alignment;
533
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
100 }
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
101
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
102
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
103 void dcCloseAggr(DCaggr* ag)
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
104 {
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
105 dcFinishAggr(ag);
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
106 }
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
107
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
108
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
109 void dcFreeAggr(DCaggr* ag)
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
110 {
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
111 dcFreeMem(ag);
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
112 }
71c884e610f0 - integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
diff changeset
113