Skip to content

Commit 5f00f7e

Browse files
committed
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 67140d8 commit 5f00f7e

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
@@ -393,8 +393,9 @@ struct CborParser
393393
{
394394
union {
395395
const uint8_t *end;
396-
const struct CborParserOperations *ops;
397-
} source;
396+
void *ctx;
397+
} data;
398+
const struct CborParserOperations *ops;
398399
enum CborParserGlobalFlags flags;
399400
};
400401
typedef struct CborParser CborParser;

src/cborinternal_p.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,15 @@ static inline bool can_read_bytes(const CborValue *it, size_t n)
205205
#ifdef CBOR_PARSER_CAN_READ_BYTES_FUNCTION
206206
return CBOR_PARSER_CAN_READ_BYTES_FUNCTION(it, n);
207207
#else
208-
return it->parser->source.ops->can_read_bytes(it, n);
208+
return it->parser->ops->can_read_bytes(it, n);
209209
#endif
210210
}
211211
}
212212

213213
/* Convert the pointer subtraction to size_t since end >= ptr
214214
* (this prevents issues with (ptrdiff_t)n becoming negative).
215215
*/
216-
return (size_t)(it->parser->source.end - it->source.ptr) >= n;
216+
return (size_t)(it->parser->data.end - it->source.ptr) >= n;
217217
}
218218

219219
static inline void advance_bytes(CborValue *it, size_t n)
@@ -223,7 +223,7 @@ static inline void advance_bytes(CborValue *it, size_t n)
223223
#ifdef CBOR_PARSER_ADVANCE_BYTES_FUNCTION
224224
CBOR_PARSER_ADVANCE_BYTES_FUNCTION(it, n);
225225
#else
226-
it->parser->source.ops->advance_bytes(it, n);
226+
it->parser->ops->advance_bytes(it, n);
227227
#endif
228228
return;
229229
}
@@ -239,7 +239,7 @@ static inline CborError transfer_string(CborValue *it, const void **ptr, size_t
239239
#ifdef CBOR_PARSER_TRANSFER_STRING_FUNCTION
240240
return CBOR_PARSER_TRANSFER_STRING_FUNCTION(it, ptr, offset, len);
241241
#else
242-
return it->parser->source.ops->transfer_string(it, ptr, offset, len);
242+
return it->parser->ops->transfer_string(it, ptr, offset, len);
243243
#endif
244244
}
245245
}
@@ -260,7 +260,7 @@ static inline void *read_bytes_unchecked(const CborValue *it, void *dst, size_t
260260
#ifdef CBOR_PARSER_READ_BYTES_FUNCTION
261261
return CBOR_PARSER_READ_BYTES_FUNCTION(it, dst, offset, n);
262262
#else
263-
return it->parser->source.ops->read_bytes(it, dst, offset, n);
263+
return it->parser->ops->read_bytes(it, dst, offset, n);
264264
#endif
265265
}
266266
}

src/cborparser.c

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

0 commit comments

Comments
 (0)