diff options
author | David Robillard <d@drobilla.net> | 2021-07-24 21:08:10 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:07 -0500 |
commit | 6f87e81df367d93f327cebbc6e848ffd09742761 (patch) | |
tree | a7343cb271a1caeebae31a73f50bcdecb2854414 /src | |
parent | 2d43bf2b5c301ca1e4766b5048650ca1e29e38c0 (diff) | |
download | serd-6f87e81df367d93f327cebbc6e848ffd09742761.tar.gz serd-6f87e81df367d93f327cebbc6e848ffd09742761.tar.bz2 serd-6f87e81df367d93f327cebbc6e848ffd09742761.zip |
Be precise about the meaning of node "length" and "size"
Diffstat (limited to 'src')
-rw-r--r-- | src/n3.c | 5 | ||||
-rw-r--r-- | src/node.c | 50 | ||||
-rw-r--r-- | src/reader.c | 16 | ||||
-rw-r--r-- | src/reader.h | 4 |
4 files changed, 36 insertions, 39 deletions
@@ -1331,7 +1331,8 @@ read_collection(SerdReader* const reader, /* The order of node allocation here is necessarily not in stack order, so we create two nodes and recycle them throughout. */ SerdNode* n1 = - push_node_padded(reader, genid_size(reader), SERD_BLANK, "", 0); + push_node_padded(reader, genid_length(reader), SERD_BLANK, "", 0); + SerdNode* node = n1; SerdNode* rest = 0; @@ -1355,7 +1356,7 @@ read_collection(SerdReader* const reader, rest = blank_id(reader); // First pass, push assert(rest); // Can't overflow since read_object() popped } else { - set_blank_id(reader, rest, genid_size(reader)); + set_blank_id(reader, rest, genid_length(reader) + 1); } } @@ -83,7 +83,7 @@ string_sink(const void* const buf, } SERD_PURE_FUNC static size_t -serd_node_pad_size(const size_t n_bytes) +serd_node_pad_length(const size_t n_bytes) { const size_t pad = sizeof(SerdNode) - (n_bytes + 2) % sizeof(SerdNode); const size_t size = n_bytes + 2 + pad; @@ -91,16 +91,16 @@ serd_node_pad_size(const size_t n_bytes) return size; } -SERD_PURE_FUNC static SerdNode* -serd_node_meta(SerdNode* const node) +static const SerdNode* +serd_node_meta_c(const SerdNode* const node) { - return node + 1 + (serd_node_pad_size(node->length) / sizeof(SerdNode)); + return node + 1 + (serd_node_pad_length(node->length) / sizeof(SerdNode)); } -SERD_PURE_FUNC static const SerdNode* -serd_node_meta_c(const SerdNode* const node) +static SerdNode* +serd_node_meta(SerdNode* const node) { - return node + 1 + (serd_node_pad_size(node->length) / sizeof(SerdNode)); + return node + 1 + (serd_node_pad_length(node->length) / sizeof(SerdNode)); } SERD_PURE_FUNC static const SerdNode* @@ -115,10 +115,9 @@ serd_node_check_padding(const SerdNode* node) (void)node; #ifndef NDEBUG if (node) { - const size_t unpadded_size = node->length; - const size_t padded_size = serd_node_pad_size(unpadded_size); - for (size_t i = 0; i < padded_size - unpadded_size; ++i) { - assert(serd_node_buffer_c(node)[unpadded_size + i] == '\0'); + const size_t padded_length = serd_node_pad_length(node->length); + for (size_t i = 0; i < padded_length - node->length; ++i) { + assert(serd_node_buffer_c(node)[node->length + i] == '\0'); } serd_node_check_padding(serd_node_maybe_get_meta_c(node)); @@ -129,7 +128,7 @@ serd_node_check_padding(const SerdNode* node) static SERD_PURE_FUNC size_t serd_node_total_size(const SerdNode* const node) { - return node ? (sizeof(SerdNode) + serd_node_pad_size(node->length) + + return node ? (sizeof(SerdNode) + serd_node_pad_length(node->length) + serd_node_total_size(serd_node_maybe_get_meta_c(node))) : 0; } @@ -139,7 +138,7 @@ serd_node_malloc(const size_t length, const SerdNodeFlags flags, const SerdNodeType type) { - const size_t size = sizeof(SerdNode) + serd_node_pad_size(length); + const size_t size = sizeof(SerdNode) + serd_node_pad_length(length); SerdNode* node = (SerdNode*)serd_calloc_aligned(serd_node_align, size); node->length = 0; @@ -176,11 +175,10 @@ serd_node_set(SerdNode** const dst, const SerdNode* const src) void serd_node_zero_pad(SerdNode* node) { - char* buf = serd_node_buffer(node); - const size_t size = node->length; - const size_t padded_size = serd_node_pad_size(size); + char* buf = serd_node_buffer(node); + const size_t padded_length = serd_node_pad_length(node->length); - memset(buf + size, 0, padded_size - size); + memset(buf + node->length, 0, padded_length - node->length); if (node->flags & (SERD_HAS_DATATYPE | SERD_HAS_LANGUAGE)) { serd_node_zero_pad(serd_node_meta(node)); @@ -239,7 +237,7 @@ serd_new_plain_literal_i(const SerdStringView str, flags |= SERD_HAS_LANGUAGE; - const size_t len = serd_node_pad_size(str.length); + const size_t len = serd_node_pad_length(str.length); const size_t total_len = len + sizeof(SerdNode) + lang.length; SerdNode* node = serd_node_malloc(total_len, flags, SERD_LITERAL); @@ -286,7 +284,7 @@ serd_new_typed_literal(const SerdStringView str, flags |= SERD_HAS_DATATYPE; - const size_t len = serd_node_pad_size(str.length); + const size_t len = serd_node_pad_length(str.length); const size_t total_len = len + sizeof(SerdNode) + datatype_uri.length; SerdNode* node = serd_node_malloc(total_len, flags, SERD_LITERAL); @@ -654,7 +652,7 @@ serd_new_custom_literal(const void* const user_data, } const size_t datatype_size = serd_node_total_size(datatype); - const size_t total_size = serd_node_pad_size(len + 1) + datatype_size; + const size_t total_size = serd_node_pad_length(len) + datatype_size; SerdNode* const node = serd_node_malloc( total_size, datatype ? SERD_HAS_DATATYPE : 0U, SERD_LITERAL); @@ -705,10 +703,8 @@ serd_new_decimal(const double d, const SerdNode* const datatype) assert(!r.status); // Allocate node with enough space for value and datatype URI - SerdNode* const node = - serd_node_malloc(serd_node_pad_size(r.count + 1) + type_size, - SERD_HAS_DATATYPE, - SERD_LITERAL); + SerdNode* const node = serd_node_malloc( + serd_node_pad_length(r.count) + type_size, SERD_HAS_DATATYPE, SERD_LITERAL); // Write string directly into node r = exess_write_decimal(d, r.count + 1, serd_node_buffer(node)); @@ -732,10 +728,8 @@ serd_new_integer(const int64_t i, const SerdNode* const datatype) assert(!r.status); // Allocate node with enough space for value and datatype URI - SerdNode* const node = - serd_node_malloc(serd_node_pad_size(r.count + 1) + type_size, - SERD_HAS_DATATYPE, - SERD_LITERAL); + SerdNode* const node = serd_node_malloc( + serd_node_pad_length(r.count) + type_size, SERD_HAS_DATATYPE, SERD_LITERAL); // Write string directly into node r = exess_write_long(i, r.count + 1U, serd_node_buffer(node)); diff --git a/src/reader.c b/src/reader.c index 5ff2cbd9..00af2095 100644 --- a/src/reader.c +++ b/src/reader.c @@ -48,9 +48,9 @@ set_blank_id(SerdReader* const reader, } size_t -genid_size(const SerdReader* const reader) +genid_length(const SerdReader* const reader) { - return reader->bprefix_len + 1 + 10 + 1; // + "b" + UINT32_MAX + \0 + return reader->bprefix_len + 10; // + "b" + UINT32_MAX } bool @@ -71,17 +71,19 @@ tolerate_status(const SerdReader* const reader, const SerdStatus status) SerdNode* blank_id(SerdReader* const reader) { - SerdNode* ref = - push_node_padded(reader, genid_size(reader), SERD_BLANK, "", 0); + SerdNode* const ref = + push_node_padded(reader, genid_length(reader), SERD_BLANK, "", 0); + if (ref) { - set_blank_id(reader, ref, genid_size(reader)); + set_blank_id(reader, ref, genid_length(reader) + 1); } + return ref; } SerdNode* push_node_padded(SerdReader* const reader, - const size_t maxlen, + const size_t max_length, const SerdNodeType type, const char* const str, const size_t length) @@ -94,7 +96,7 @@ push_node_padded(SerdReader* const reader, *terminator = 0; void* mem = serd_stack_push_aligned( - &reader->stack, sizeof(SerdNode) + maxlen + 1, sizeof(SerdNode)); + &reader->stack, sizeof(SerdNode) + max_length + 1, sizeof(SerdNode)); if (!mem) { return NULL; diff --git a/src/reader.h b/src/reader.h index fcbcb110..5441d8b6 100644 --- a/src/reader.h +++ b/src/reader.h @@ -57,7 +57,7 @@ r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...); SerdNode* push_node_padded(SerdReader* reader, - size_t maxlen, + size_t max_length, SerdNodeType type, const char* str, size_t length); @@ -69,7 +69,7 @@ push_node(SerdReader* reader, size_t length); SERD_PURE_FUNC size_t -genid_size(const SerdReader* reader); +genid_length(const SerdReader* reader); SERD_PURE_FUNC bool tolerate_status(const SerdReader* reader, SerdStatus status); |