File: StructTest.c

package info (click to toggle)
jnr-ffi 2.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,304 kB
  • sloc: java: 23,671; ansic: 590; xml: 255; sh: 137; makefile: 12
file content (239 lines) | stat: -rwxr-xr-x 13,414 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright (C) 2007-2010 Wayne Meissner
 *
 * This file is part of the JNR project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stddef.h>
#include <stdint.h>

typedef char Signed8;
typedef short Signed16;
typedef int Signed32;
typedef long long Signed64;
typedef float Float32;
typedef double Float64;
typedef long SignedLong;

struct test1 {
    char b;
    short s;
    int i;
    long long j;
    SignedLong l;
    float f;
    double d;
    char string[32];
};

#define T(x, type) \
    type struct_field_##type(struct test1* t) { return t->x; } \
    struct type##Align { char first; type value; }; \
    type struct_align_##type(struct type##Align* a) { return a->value; }

T(b, Signed8);
T(s, Signed16);
T(i, Signed32);
T(j, Signed64);
T(f, Float32);
T(d, Float64);

long struct_field_SignedLong(struct test1* t) { return t->l; }
struct SignedLongAlign { char first; SignedLong value; };
long struct_align_SignedLong(struct SignedLongAlign* a) { return a->value; }

void 
struct_set_string(struct test1* t, char* s) 
{
    strcpy(t->string, s);
}

struct test1*
struct_make_struct(char b, short s, int i, long long ll, float f, double d) 
{
    static struct test1 t;
    memset(&t, 0, sizeof(t));
    t.b = b;
    t.s = s;
    t.i = i;
    t.j = ll;
    t.f = f;
    t.d = d;
    return &t;
}

struct foo {
  unsigned long l1,l2, l3;
};

int
fill_struct_from_longs(unsigned long l1, unsigned long l2, struct foo* s, unsigned long l3)
{
    s->l1 = l1;
    s->l2 = l2;
    s->l3 = l3;

  return 0;
}

#define STRUCT_ALIGNMENT(alignment)                                                   \
    struct StructAlignment##alignment {                                               \
        uint8_t  f0;                                                                  \
        uint16_t f1;                                                                  \
        uint32_t f2;                                                                  \
        uint64_t f3;                                                                  \
        void    *f4;                                                                  \
        };                                                                            \
                                                                                      \
    int struct_alignment_size_##alignment() {                                         \
        return sizeof(struct StructAlignment##alignment);                             \
    }                                                                                 \
                                                                                      \
    int struct_alignment_field_offset_##alignment(int field) {                        \
        switch(field) {                                                               \
        case 0:                                                                       \
            return offsetof(struct StructAlignment##alignment, f0);                   \
        case 1:                                                                       \
            return offsetof(struct StructAlignment##alignment, f1);                   \
        case 2:                                                                       \
            return offsetof(struct StructAlignment##alignment, f2);                   \
        case 3:                                                                       \
            return offsetof(struct StructAlignment##alignment, f3);                   \
        case 4:                                                                       \
            return offsetof(struct StructAlignment##alignment, f4);                   \
        default:                                                                      \
            return -1;                                                                \
        }                                                                             \
    }                                                                                 \
                                                                                      \
    struct InnerStructAlignment1_##alignment {                                        \
        uint8_t  f0;                                                                  \
        uint16_t f1;                                                                  \
        uint32_t f2;                                                                  \
        uint64_t f3;                                                                  \
        void    *f4;                                                                  \
        struct InnerStructAlignment2_##alignment {                                    \
            uint8_t  f0;                                                              \
            uint16_t f1;                                                              \
            uint32_t f2;                                                              \
            uint64_t f3;                                                              \
            void    *f4;                                                              \
            struct InnerStructAlignment3_##alignment {                                \
                uint8_t  f0;                                                          \
                uint16_t f1;                                                          \
                uint32_t f2;                                                          \
                uint64_t f3;                                                          \
                void    *f4;                                                          \
            } f5;                                                                     \
        } f5;                                                                         \
    };                                                                                \
                                                                                      \
    int inner_struct_alignment_size_##alignment() {                                   \
        return sizeof(struct InnerStructAlignment1_##alignment);                      \
    }                                                                                 \
                                                                                      \
    int inner_struct_alignment_field_offset_##alignment(int level, int field) {       \
        switch(field) {                                                               \
        case 0:                                                                       \
            switch(level) {                                                           \
            case 0:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f0);        \
            case 1:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f5.f0);     \
            case 2:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f5.f5.f0);  \
            default:                                                                  \
                return -1;                                                            \
            }                                                                         \
        case 1:                                                                       \
            switch(level) {                                                           \
            case 0:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f1);        \
            case 1:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f5.f1);     \
            case 2:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f5.f5.f1);  \
            default:                                                                  \
                return -1;                                                            \
            }                                                                         \
        case 2:                                                                       \
            switch(level) {                                                           \
            case 0:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f2);        \
            case 1:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f5.f2);     \
            case 2:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f5.f5.f2);  \
            default:                                                                  \
                return -1;                                                            \
            }                                                                         \
        case 3:                                                                       \
            switch(level) {                                                           \
            case 0:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f3);        \
            case 1:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f5.f3);     \
            case 2:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f5.f5.f3);  \
            default:                                                                  \
                return -1;                                                            \
            }                                                                         \
        case 4:                                                                       \
            switch(level) {                                                           \
            case 0:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f4);        \
            case 1:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f5.f4);     \
            case 2:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f5.f5.f4);  \
            default:                                                                  \
                return -1;                                                            \
            }                                                                         \
        case 5:                                                                       \
            switch(level) {                                                           \
            case 0:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f5);        \
            case 1:                                                                   \
                return offsetof(struct InnerStructAlignment1_##alignment, f5.f5);     \
            default:                                                                  \
                return -1;                                                            \
            }                                                                         \
        default:                                                                      \
            return -1;                                                                \
        }                                                                             \
    }

#pragma pack(push, 1)
STRUCT_ALIGNMENT(1)
#pragma pack(pop)

#pragma pack(push, 2)
STRUCT_ALIGNMENT(2)
#pragma pack(pop)

#pragma pack(push, 4)
STRUCT_ALIGNMENT(4)
#pragma pack(pop)

#pragma pack(push, 8)
STRUCT_ALIGNMENT(8)
#pragma pack(pop)

#pragma pack(push, 16)
STRUCT_ALIGNMENT(16)
#pragma pack(pop)