comparison dyncall/dyncall_struct.c @ 338:ee2b6e54b074

- minor refactoring for clarity
author Tassilo Philipp
date Fri, 03 Jan 2020 22:48:20 +0100
parents 3e629dc19168
children
comparison
equal deleted inserted replaced
337:168092290cae 338:ee2b6e54b074
52 if (type == DC_SIGCHAR_STRING) { 52 if (type == DC_SIGCHAR_STRING) {
53 assert(!"Use dcSubStruct instead !!!"); 53 assert(!"Use dcSubStruct instead !!!");
54 return; 54 return;
55 } 55 }
56 assert(s && s->pCurrentStruct); 56 assert(s && s->pCurrentStruct);
57 assert(s->pCurrentStruct->nextField <= (DCint)s->pCurrentStruct->fieldCount - 1); 57 assert(s->pCurrentStruct->nextField < (DCint)s->pCurrentStruct->fieldCount);
58 f = s->pCurrentStruct->pFields + (s->pCurrentStruct->nextField++); 58 f = s->pCurrentStruct->pFields + (s->pCurrentStruct->nextField++);
59 f->type = type; 59 f->type = type;
60 f->alignment = alignment; 60 f->alignment = alignment;
61 f->arrayLength = arrayLength; 61 f->arrayLength = arrayLength;
62 f->pSubStruct = NULL; 62 f->pSubStruct = NULL;
105 105
106 static void dcComputeStructSize(DCstruct* s) 106 static void dcComputeStructSize(DCstruct* s)
107 { 107 {
108 DCsize i; 108 DCsize i;
109 assert(s); 109 assert(s);
110
111 /* compute field sizes and alignments, recurse if needed */
110 for (i = 0; i < s->fieldCount; i++) { 112 for (i = 0; i < s->fieldCount; i++) {
111 DCfield *f = s->pFields + i; 113 DCfield *f = s->pFields + i;
112 DCsize fieldAlignment; 114 DCsize fieldAlignment;
113 if (f->type == DC_SIGCHAR_STRUCT) { 115 if (f->type == DC_SIGCHAR_STRUCT) {
114 dcComputeStructSize(f->pSubStruct); 116 dcComputeStructSize(f->pSubStruct);
115 f->size = f->pSubStruct->size; 117 f->size = f->pSubStruct->size;
116 fieldAlignment = f->pSubStruct->alignment; 118 fieldAlignment = f->pSubStruct->alignment;
117 } else { 119 } else
118 fieldAlignment = f->size; 120 fieldAlignment = f->size;
119 } 121
120 if (!f->alignment) 122 if (!f->alignment)
121 f->alignment = fieldAlignment; 123 f->alignment = fieldAlignment;
122 124
125 /* if field alignment > struct alignment, choose former */
123 if (f->alignment > s->alignment) 126 if (f->alignment > s->alignment)
124 s->alignment = f->alignment; 127 s->alignment = f->alignment;
125 128
129 /* if array, it's x times the size */
126 f->size *= f->arrayLength; 130 f->size *= f->arrayLength;
127 131
128 /*printf("FIELD %d, size = %d, alignment = %d\n", (int)i, (int)f->size, (int)f->alignment);@@@*/ 132 /*printf("FIELD %d, size = %d, alignment = %d\n", (int)i, (int)f->size, (int)f->alignment);@@@*/
129 } 133 }
134
135 /* compute overall struct size */
130 for (i = 0; i < s->fieldCount; i++) { 136 for (i = 0; i < s->fieldCount; i++) {
131 DCfield *f = s->pFields + i; 137 DCfield *f = s->pFields + i;
132 dcAlign(&s->size, f->alignment); 138 dcAlign(&s->size, f->alignment);
133 s->size += f->size; 139 s->size += f->size;
134 } 140 }