Skip to content

Commit 6f0056e

Browse files
committed
WSHUB-458: cborparser: Move ops outside of union
In its place, put an arbitrary `void *` pointer for reader context. The reader needs to store some context information which is specific to the `CborParser` instance it is serving. Right now, `CborValue::source::token` serves this purpose, but the problem is that we also need a per-`CborValue` context and have nowhere to put it. Better to spend an extra pointer (4 bytes on 32-bit platforms) in the `CborParser` (which there'll be just one of), then to do it in the `CborValue` (which there may be several of) or to use a `CborReader` object that itself carries two pointers (`ops` and the context, thus we'd need an extra 3 pointers).
1 parent 9829c2a commit 6f0056e

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

src/cbor.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,9 @@ struct CborParser
390390
{
391391
union {
392392
const uint8_t *end;
393-
const struct CborParserOperations *ops;
394-
} source;
393+
void *ctx;
394+
} data;
395+
const struct CborParserOperations *ops;
395396
enum CborParserGlobalFlags flags;
396397
};
397398
typedef struct CborParser CborParser;

src/cborinternal_p.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,15 @@ static inline bool can_read_bytes(const CborValue *it, size_t n)
178178
#ifdef CBOR_PARSER_CAN_READ_BYTES_FUNCTION
179179
return CBOR_PARSER_CAN_READ_BYTES_FUNCTION(it, n);
180180
#else
181-
return it->parser->source.ops->can_read_bytes(it, n);
181+
return it->parser->ops->can_read_bytes(it, n);
182182
#endif
183183
}
184184
}
185185

186186
/* Convert the pointer subtraction to size_t since end >= ptr
187187
* (this prevents issues with (ptrdiff_t)n becoming negative).
188188
*/
189-
return (size_t)(it->parser->source.end - it->source.ptr) >= n;
189+
return (size_t)(it->parser->data.end - it->source.ptr) >= n;
190190
}
191191

192192
static inline void advance_bytes(CborValue *it, size_t n)
@@ -196,7 +196,7 @@ static inline void advance_bytes(CborValue *it, size_t n)
196196
#ifdef CBOR_PARSER_ADVANCE_BYTES_FUNCTION
197197
CBOR_PARSER_ADVANCE_BYTES_FUNCTION(it, n);
198198
#else
199-
it->parser->source.ops->advance_bytes(it, n);
199+
it->parser->ops->advance_bytes(it, n);
200200
#endif
201201
return;
202202
}
@@ -212,7 +212,7 @@ static inline CborError transfer_string(CborValue *it, const void **ptr, size_t
212212
#ifdef CBOR_PARSER_TRANSFER_STRING_FUNCTION
213213
return CBOR_PARSER_TRANSFER_STRING_FUNCTION(it, ptr, offset, len);
214214
#else
215-
return it->parser->source.ops->transfer_string(it, ptr, offset, len);
215+
return it->parser->ops->transfer_string(it, ptr, offset, len);
216216
#endif
217217
}
218218
}
@@ -233,7 +233,7 @@ static inline void *read_bytes_unchecked(const CborValue *it, void *dst, size_t
233233
#ifdef CBOR_PARSER_READ_BYTES_FUNCTION
234234
return CBOR_PARSER_READ_BYTES_FUNCTION(it, dst, offset, n);
235235
#else
236-
return it->parser->source.ops->read_bytes(it, dst, offset, n);
236+
return it->parser->ops->read_bytes(it, dst, offset, n);
237237
#endif
238238
}
239239
}

src/cborparser.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ static void cbor_parser_init_common(CborParser *parser, CborValue *it)
354354
CborError cbor_parser_init(const uint8_t *buffer, size_t size, uint32_t flags, CborParser *parser, CborValue *it)
355355
{
356356
cbor_parser_init_common(parser, it);
357-
parser->source.end = buffer + size;
357+
parser->data.end = buffer + size;
358358
parser->flags = (enum CborParserGlobalFlags)flags;
359359
it->source.ptr = buffer;
360360
return preparse_value(it);
@@ -380,7 +380,7 @@ CborError cbor_parser_init(const uint8_t *buffer, size_t size, uint32_t flags, C
380380
CborError cbor_parser_init_reader(const struct CborParserOperations *ops, CborParser *parser, CborValue *it, void *token)
381381
{
382382
cbor_parser_init_common(parser, it);
383-
parser->source.ops = ops;
383+
parser->ops = ops;
384384
parser->flags = CborParserFlag_ExternalSource;
385385
it->source.token = token;
386386
return preparse_value(it);

0 commit comments

Comments
 (0)