aboutsummaryrefslogtreecommitdiffstats
path: root/src/node.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/node.c')
-rw-r--r--src/node.c145
1 files changed, 90 insertions, 55 deletions
diff --git a/src/node.c b/src/node.c
index a80a38b2..135ccc22 100644
--- a/src/node.c
+++ b/src/node.c
@@ -3,9 +3,9 @@
#include "node.h"
+#include "memory.h"
#include "namespaces.h"
#include "string_utils.h"
-#include "system.h"
#include "exess/exess.h"
#include "serd/buffer.h"
@@ -110,35 +110,46 @@ serd_node_total_size(const SerdNode* const node)
}
SerdNode*
-serd_node_malloc(const size_t length,
- const SerdNodeFlags flags,
- const SerdNodeType type)
+serd_node_malloc(SerdAllocator* const allocator,
+ const size_t length,
+ const SerdNodeFlags flags,
+ const SerdNodeType type)
{
const size_t size = sizeof(SerdNode) + serd_node_pad_length(length);
- SerdNode* node = (SerdNode*)serd_calloc_aligned(serd_node_align, size);
- node->length = 0;
- node->flags = flags;
- node->type = type;
+ SerdNode* const node =
+ (SerdNode*)serd_aaligned_calloc(allocator, serd_node_align, size);
+
+ if (node) {
+ node->length = 0;
+ node->flags = flags;
+ node->type = type;
+ }
assert((uintptr_t)node % serd_node_align == 0U);
return node;
}
-void
-serd_node_set(SerdNode** const dst, const SerdNode* const src)
+SerdStatus
+serd_node_set(SerdAllocator* const allocator,
+ SerdNode** const dst,
+ const SerdNode* const src)
{
assert(dst);
assert(src);
const size_t size = serd_node_total_size(src);
if (!*dst || serd_node_total_size(*dst) < size) {
- serd_free_aligned(*dst);
- *dst = (SerdNode*)serd_calloc_aligned(serd_node_align, size);
+ serd_aaligned_free(allocator, *dst);
+ if (!(*dst = (SerdNode*)serd_aaligned_calloc(
+ allocator, serd_node_align, size))) {
+ return SERD_BAD_ALLOC;
+ }
}
assert(*dst);
memcpy(*dst, src, size);
+ return SERD_SUCCESS;
}
/**
@@ -169,11 +180,13 @@ result(const SerdStatus status, const size_t count)
}
SerdNode*
-serd_new_token(const SerdNodeType type, const SerdStringView str)
+serd_new_token(SerdAllocator* const allocator,
+ const SerdNodeType type,
+ const SerdStringView str)
{
SerdNodeFlags flags = 0U;
const size_t length = str.data ? str.length : 0U;
- SerdNode* node = serd_node_malloc(length, flags, type);
+ SerdNode* node = serd_node_malloc(allocator, length, flags, type);
if (node) {
if (str.data) {
@@ -189,10 +202,10 @@ serd_new_token(const SerdNodeType type, const SerdStringView str)
}
SerdNode*
-serd_new_string(const SerdStringView str)
+serd_new_string(SerdAllocator* const allocator, const SerdStringView str)
{
SerdNodeFlags flags = 0U;
- SerdNode* node = serd_node_malloc(str.length, flags, SERD_LITERAL);
+ SerdNode* node = serd_node_malloc(allocator, str.length, flags, SERD_LITERAL);
if (node) {
if (str.data && str.length) {
@@ -236,12 +249,14 @@ is_langtag(const SerdStringView string)
}
SerdNode*
-serd_new_literal(const SerdStringView string,
+serd_new_literal(SerdAllocator* const allocator,
+ const SerdStringView string,
const SerdNodeFlags flags,
const SerdStringView meta)
{
if (!(flags & (SERD_HAS_DATATYPE | SERD_HAS_LANGUAGE))) {
- SerdNode* node = serd_node_malloc(string.length, flags, SERD_LITERAL);
+ SerdNode* node =
+ serd_node_malloc(allocator, string.length, flags, SERD_LITERAL);
memcpy(serd_node_buffer(node), string.data, string.length);
node->length = string.length;
@@ -268,7 +283,8 @@ serd_new_literal(const SerdStringView string,
const size_t meta_len = serd_node_pad_length(meta.length);
const size_t meta_size = sizeof(SerdNode) + meta_len;
- SerdNode* node = serd_node_malloc(len + meta_size, flags, SERD_LITERAL);
+ SerdNode* node =
+ serd_node_malloc(allocator, len + meta_size, flags, SERD_LITERAL);
memcpy(serd_node_buffer(node), string.data, string.length);
node->length = string.length;
@@ -283,9 +299,9 @@ serd_new_literal(const SerdStringView string,
}
SerdNode*
-serd_new_blank(const SerdStringView str)
+serd_new_blank(SerdAllocator* const allocator, const SerdStringView str)
{
- return serd_new_token(SERD_BLANK, str);
+ return serd_new_token(allocator, SERD_BLANK, str);
}
ExessResult
@@ -396,16 +412,20 @@ serd_get_base64(const SerdNode* const node,
}
SerdNode*
-serd_node_copy(const SerdNode* node)
+serd_node_copy(SerdAllocator* const allocator, const SerdNode* node)
{
if (!node) {
return NULL;
}
const size_t size = serd_node_total_size(node);
- SerdNode* copy = (SerdNode*)serd_calloc_aligned(serd_node_align, size);
+ SerdNode* copy =
+ (SerdNode*)serd_aaligned_alloc(allocator, serd_node_align, size);
+
+ if (copy) {
+ memcpy(copy, node, size);
+ }
- memcpy(copy, node, size);
return copy;
}
@@ -466,39 +486,45 @@ serd_node_compare(const SerdNode* const a, const SerdNode* const b)
}
SerdNode*
-serd_new_uri(const SerdStringView string)
+serd_new_uri(SerdAllocator* const allocator, const SerdStringView string)
{
- return serd_new_token(SERD_URI, string);
+ return serd_new_token(allocator, SERD_URI, string);
}
SerdNode*
-serd_new_parsed_uri(const SerdURIView uri)
+serd_new_parsed_uri(SerdAllocator* const allocator, const SerdURIView uri)
{
- const size_t len = serd_uri_string_length(uri);
- SerdNode* const node = serd_node_malloc(len, 0, SERD_URI);
- char* ptr = serd_node_buffer(node);
- const size_t actual_len = serd_write_uri(uri, string_sink, &ptr);
+ const size_t len = serd_uri_string_length(uri);
+ SerdNode* const node = serd_node_malloc(allocator, len, 0, SERD_URI);
- assert(actual_len == len);
+ if (node) {
+ char* ptr = serd_node_buffer(node);
+ const size_t actual_len = serd_write_uri(uri, string_sink, &ptr);
+
+ assert(actual_len == len);
- serd_node_buffer(node)[actual_len] = '\0';
- node->length = actual_len;
+ serd_node_buffer(node)[actual_len] = '\0';
+ node->length = actual_len;
+ }
serd_node_check_padding(node);
return node;
}
SerdNode*
-serd_new_file_uri(const SerdStringView path, const SerdStringView hostname)
+serd_new_file_uri(SerdAllocator* const allocator,
+ const SerdStringView path,
+ const SerdStringView hostname)
{
- SerdBuffer buffer = {NULL, 0U};
+ SerdBuffer buffer = {NULL, NULL, 0U};
serd_write_file_uri(path, hostname, serd_buffer_write, &buffer);
serd_buffer_close(&buffer);
const size_t length = buffer.len;
const char* const string = (char*)buffer.buf;
- SerdNode* const node = serd_new_string(serd_substring(string, length));
+ SerdNode* const node =
+ serd_new_string(allocator, serd_substring(string, length));
free(buffer.buf);
serd_node_check_padding(node);
@@ -510,7 +536,8 @@ typedef size_t (*SerdWriteLiteralFunc)(const void* user_data,
char* buf);
static SerdNode*
-serd_new_custom_literal(const void* const user_data,
+serd_new_custom_literal(SerdAllocator* const allocator,
+ const void* const user_data,
const size_t len,
const SerdWriteLiteralFunc write,
const SerdNode* const datatype)
@@ -523,7 +550,7 @@ serd_new_custom_literal(const void* const user_data,
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);
+ allocator, total_size, datatype ? SERD_HAS_DATATYPE : 0U, SERD_LITERAL);
node->length = write(user_data, len + 1, serd_node_buffer(node));
@@ -531,46 +558,50 @@ serd_new_custom_literal(const void* const user_data,
memcpy(serd_node_meta(node), datatype, datatype_size);
}
- serd_node_check_padding(node);
return node;
}
SerdNode*
-serd_new_double(const double d)
+serd_new_double(SerdAllocator* const allocator, const double d)
{
char buf[EXESS_MAX_DOUBLE_LENGTH + 1] = {0};
const ExessResult r = exess_write_double(d, sizeof(buf), buf);
return r.status ? NULL
- : serd_new_literal(serd_substring(buf, r.count),
+ : serd_new_literal(allocator,
+ serd_substring(buf, r.count),
SERD_HAS_DATATYPE,
serd_string(EXESS_XSD_URI "double"));
}
SerdNode*
-serd_new_float(const float f)
+serd_new_float(SerdAllocator* const allocator, const float f)
{
char buf[EXESS_MAX_FLOAT_LENGTH + 1] = {0};
const ExessResult r = exess_write_float(f, sizeof(buf), buf);
return r.status ? NULL
- : serd_new_literal(serd_substring(buf, r.count),
+ : serd_new_literal(allocator,
+ serd_substring(buf, r.count),
SERD_HAS_DATATYPE,
serd_string(EXESS_XSD_URI "float"));
}
SerdNode*
-serd_new_boolean(bool b)
+serd_new_boolean(SerdAllocator* const allocator, bool b)
{
- return serd_new_literal(b ? serd_string("true") : serd_string("false"),
+ return serd_new_literal(allocator,
+ b ? serd_string("true") : serd_string("false"),
SERD_HAS_DATATYPE,
serd_node_string_view(&serd_xsd_boolean.node));
}
SerdNode*
-serd_new_decimal(const double d, const SerdNode* const datatype)
+serd_new_decimal(SerdAllocator* const allocator,
+ const double d,
+ const SerdNode* const datatype)
{
// Use given datatype, or xsd:decimal as a default if it is null
const SerdNode* type = datatype ? datatype : &serd_xsd_decimal.node;
@@ -581,8 +612,11 @@ 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_length(r.count) + type_size, SERD_HAS_DATATYPE, SERD_LITERAL);
+ SerdNode* const node =
+ serd_node_malloc(allocator,
+ 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));
@@ -595,7 +629,7 @@ serd_new_decimal(const double d, const SerdNode* const datatype)
}
SerdNode*
-serd_new_integer(const int64_t i)
+serd_new_integer(SerdAllocator* const allocator, const int64_t i)
{
// Use given datatype, or xsd:integer as a default if it is null
const SerdNode* datatype = &serd_xsd_integer.node;
@@ -607,7 +641,8 @@ serd_new_integer(const int64_t i)
// Allocate node with enough space for value and datatype URI
SerdNode* const node =
- serd_node_malloc(serd_node_pad_length(r.count) + datatype_size,
+ serd_node_malloc(allocator,
+ serd_node_pad_length(r.count) + datatype_size,
SERD_HAS_DATATYPE,
SERD_LITERAL);
@@ -634,13 +669,13 @@ write_base64_literal(const void* const user_data,
}
SerdNode*
-serd_new_base64(const void* buf, size_t size)
+serd_new_base64(SerdAllocator* const allocator, const void* buf, size_t size)
{
const size_t len = exess_write_base64(size, buf, 0, NULL).count;
SerdConstBuffer blob = {buf, size};
return serd_new_custom_literal(
- &blob, len, write_base64_literal, &serd_xsd_base64Binary.node);
+ allocator, &blob, len, write_base64_literal, &serd_xsd_base64Binary.node);
}
SerdNodeType
@@ -721,7 +756,7 @@ serd_node_flags(const SerdNode* const node)
}
void
-serd_node_free(SerdNode* const node)
+serd_node_free(SerdAllocator* const allocator, SerdNode* const node)
{
- serd_free_aligned(node);
+ serd_aaligned_free(allocator, node);
}