@@ -51,13 +51,12 @@ enum json_tokens {
51
51
struct json_obj_descr {
52
52
const char * field_name ;
53
53
54
- /* Alignment can never be 0 or more than 4. The macros to create a
55
- * struct json_obj_descr will subtract 1 from the result of
56
- * __alignof__() calls in order to keep this value in the 0-3 range
57
- * and thus use only 2 bits. 1 is added back when rounding it up to
58
- * calculate the struct size while parsing an array or object.
54
+ /* Alignment can be 1, 2, 4, or 8. The macros to create
55
+ * a struct json_obj_descr will store the alignment's
56
+ * power of 2 in order to keep this value in the 0-3 range
57
+ * and thus use only 2 bits.
59
58
*/
60
- u32_t alignment : 2 ;
59
+ u32_t align_shift : 2 ;
61
60
62
61
/* 127 characters is more than enough for a field name. */
63
62
u32_t field_name_len : 7 ;
@@ -100,6 +99,9 @@ struct json_obj_descr {
100
99
typedef int (* json_append_bytes_t )(const char * bytes , size_t len ,
101
100
void * data );
102
101
102
+ #define Z_ALIGN_SHIFT (type ) (__alignof__(type) == 1 ? 0 : \
103
+ __alignof__(type) == 2 ? 1 : \
104
+ __alignof__(type) == 4 ? 2 : 3)
103
105
104
106
/**
105
107
* @brief Helper macro to declare a descriptor for supported primitive
@@ -128,7 +130,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
128
130
.field_name = (#field_name_), \
129
131
.field_name_len = sizeof(#field_name_) - 1, \
130
132
.offset = offsetof(struct_, field_name_), \
131
- .alignment = __alignof__ (struct_) - 1 , \
133
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
132
134
.type = type_, \
133
135
}
134
136
@@ -162,7 +164,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
162
164
.field_name = (#field_name_), \
163
165
.field_name_len = (sizeof(#field_name_) - 1), \
164
166
.offset = offsetof(struct_, field_name_), \
165
- .alignment = __alignof__ (struct_) - 1 , \
167
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
166
168
.type = JSON_TOK_OBJECT_START, \
167
169
.object = { \
168
170
.sub_descr = sub_descr_, \
@@ -201,13 +203,13 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
201
203
.field_name = (#field_name_), \
202
204
.field_name_len = sizeof(#field_name_) - 1, \
203
205
.offset = offsetof(struct_, field_name_), \
204
- .alignment = __alignof__ (struct_) - 1 , \
206
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
205
207
.type = JSON_TOK_LIST_START, \
206
208
.array = { \
207
209
.element_descr = &(struct json_obj_descr) { \
208
210
.type = elem_type_, \
209
211
.offset = offsetof(struct_, len_field_), \
210
- .alignment = __alignof__ (struct_) - 1 , \
212
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
211
213
}, \
212
214
.n_elements = (max_len_), \
213
215
}, \
@@ -258,7 +260,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
258
260
.field_name = (#field_name_), \
259
261
.field_name_len = sizeof(#field_name_) - 1, \
260
262
.offset = offsetof(struct_, field_name_), \
261
- .alignment = __alignof__ (struct_) - 1 , \
263
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
262
264
.type = JSON_TOK_LIST_START, \
263
265
.array = { \
264
266
.element_descr = &(struct json_obj_descr) { \
@@ -268,7 +270,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
268
270
.sub_descr_len = elem_descr_len_, \
269
271
}, \
270
272
.offset = offsetof(struct_, len_field_), \
271
- .alignment = __alignof__ (struct_) - 1 , \
273
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
272
274
}, \
273
275
.n_elements = (max_len_), \
274
276
}, \
@@ -328,7 +330,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
328
330
.field_name = (#field_name_), \
329
331
.field_name_len = sizeof(#field_name_) - 1, \
330
332
.offset = offsetof(struct_, field_name_), \
331
- .alignment = __alignof__ (struct_) - 1 , \
333
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
332
334
.type = JSON_TOK_LIST_START, \
333
335
.array = { \
334
336
.element_descr = &(struct json_obj_descr) { \
@@ -338,7 +340,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
338
340
.sub_descr_len = elem_descr_len_, \
339
341
}, \
340
342
.offset = offsetof(struct_, len_field_), \
341
- .alignment = __alignof__ (struct_) - 1 , \
343
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
342
344
}, \
343
345
.n_elements = (max_len_), \
344
346
}, \
@@ -367,7 +369,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
367
369
.field_name = (json_field_name_), \
368
370
.field_name_len = sizeof(json_field_name_) - 1, \
369
371
.offset = offsetof(struct_, struct_field_name_), \
370
- .alignment = __alignof__ (struct_) - 1 , \
372
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
371
373
.type = type_, \
372
374
}
373
375
@@ -393,7 +395,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
393
395
.field_name = (json_field_name_), \
394
396
.field_name_len = (sizeof(json_field_name_) - 1), \
395
397
.offset = offsetof(struct_, struct_field_name_), \
396
- .alignment = __alignof__ (struct_) - 1 , \
398
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
397
399
.type = JSON_TOK_OBJECT_START, \
398
400
.object = { \
399
401
.sub_descr = sub_descr_, \
@@ -429,13 +431,13 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
429
431
.field_name = (json_field_name_), \
430
432
.field_name_len = sizeof(json_field_name_) - 1, \
431
433
.offset = offsetof(struct_, struct_field_name_), \
432
- .alignment = __alignof__ (struct_) - 1 , \
434
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
433
435
.type = JSON_TOK_LIST_START, \
434
436
.array = { \
435
437
.element_descr = &(struct json_obj_descr) { \
436
438
.type = elem_type_, \
437
439
.offset = offsetof(struct_, len_field_), \
438
- .alignment = __alignof__ (struct_) - 1 , \
440
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
439
441
}, \
440
442
.n_elements = (max_len_), \
441
443
}, \
@@ -495,7 +497,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
495
497
.field_name = json_field_name_, \
496
498
.field_name_len = sizeof(json_field_name_) - 1, \
497
499
.offset = offsetof(struct_, struct_field_name_), \
498
- .alignment = __alignof__ (struct_) - 1 , \
500
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
499
501
.type = JSON_TOK_LIST_START, \
500
502
.element_descr = &(struct json_obj_descr) { \
501
503
.type = JSON_TOK_OBJECT_START, \
@@ -504,7 +506,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
504
506
.sub_descr_len = elem_descr_len_, \
505
507
}, \
506
508
.offset = offsetof(struct_, len_field_), \
507
- .alignment = __alignof__ (struct_) - 1 , \
509
+ .align_shift = Z_ALIGN_SHIFT (struct_), \
508
510
}, \
509
511
.n_elements = (max_len_), \
510
512
}
0 commit comments