13#ifdef HAVE_MALLOC_SIZE
14# include <stdatomic.h>
16size_t malloc_size(
void *);
19#ifdef HAVE_MALLOC_USABLE_SIZE
20# include <stdatomic.h>
22size_t malloc_usable_size(
void *);
25#include "nyx_node_internal.h"
31#if defined(HAVE_MALLOC_SIZE) || defined(HAVE_MALLOC_USABLE_SIZE)
32static unsigned long used_mem = 0UL;
41 #if defined(HAVE_MALLOC_SIZE) || defined(HAVE_MALLOC_USABLE_SIZE)
42 atomic_store_explicit(&used_mem, 0UL, memory_order_relaxed);
54 #if defined(HAVE_MALLOC_SIZE) || defined(HAVE_MALLOC_USABLE_SIZE)
55 unsigned long leaks = atomic_exchange_explicit(&used_mem, 0UL, memory_order_relaxed);
81 #ifdef HAVE_MALLOC_SIZE
82 size_t result = malloc_size(buff);
84 atomic_fetch_sub_explicit(&used_mem, result, memory_order_relaxed);
89 #ifdef HAVE_MALLOC_USABLE_SIZE
90 size_t result = malloc_usable_size(buff);
92 atomic_fetch_sub_explicit(&used_mem, result, memory_order_relaxed);
101 #if defined(HAVE_MALLOC_SIZE) || defined(HAVE_MALLOC_USABLE_SIZE)
119 buff_t result = malloc(size);
128 #ifdef HAVE_MALLOC_SIZE
129 atomic_fetch_add_explicit(&used_mem, malloc_size(result), memory_order_relaxed);
134 #ifdef HAVE_MALLOC_USABLE_SIZE
135 atomic_fetch_add_explicit(&used_mem, malloc_usable_size(result), memory_order_relaxed);
157 #ifdef HAVE_MALLOC_SIZE
158 atomic_fetch_sub_explicit(&used_mem, malloc_size(buff), memory_order_relaxed);
163 #ifdef HAVE_MALLOC_USABLE_SIZE
164 atomic_fetch_sub_explicit(&used_mem, malloc_usable_size(buff), memory_order_relaxed);
169 buff_t result = realloc(buff, size);
178 #ifdef HAVE_MALLOC_SIZE
179 atomic_fetch_add_explicit(&used_mem, malloc_size(result), memory_order_relaxed);
184 #ifdef HAVE_MALLOC_USABLE_SIZE
185 atomic_fetch_add_explicit(&used_mem, malloc_usable_size(result), memory_order_relaxed);
197str_t nyx_bool_dup(
bool b)
213str_t nyx_double_dup(
double d)
219 snprintf(str, 32 + 1,
"%f", d);
233 size_t len = strlen(s);
253 size_t len = strnlen(s, n);
286nyx_str_t nyx_str_s(
STR_t s)
288 nyx_str_t str = {(
str_t) s, s == NULL ? 0x0000000 : strlen(s)};
341 if(!NYX_OBJECT_CHECK_MAGIC(
object))
375 if(!NYX_OBJECT_CHECK_MAGIC(
object))
386 _object_free(
object);
object = NULL;
402 return object != NULL ?
object->type : 0x00;
409 for(;
object != NULL;
object =
object->parent)
411 if(internal_notify(
object))
424 if(object1 == NULL || object2 == NULL)
429 if(!NYX_OBJECT_CHECK_MAGIC(object1) || !NYX_OBJECT_CHECK_MAGIC(object2))
436 if(object1 == object2)
441 if(object1->type != object2->type)
448 switch(object1->type)
465 str_t s1 = nyx_object_to_string(object1);
466 str_t s2 = nyx_object_to_string(object2);
468 bool equal = strcmp(s1, s2) == 0;
492 if(!NYX_OBJECT_CHECK_MAGIC(
object))
502 return nyx_null_to_string((
const nyx_null_t *)
object);
505 return nyx_number_to_string((
const nyx_number_t *)
object);
508 return nyx_boolean_to_string((
const nyx_boolean_t *)
object);
511 return nyx_string_to_string((
const nyx_string_t *)
object);
514 return nyx_list_to_string((
const nyx_list_t *)
object);
517 return nyx_dict_to_string((
const nyx_dict_t *)
object);
535 if(!NYX_OBJECT_CHECK_MAGIC(
object))
545 return nyx_null_to_string((
const nyx_null_t *)
object);
548 return nyx_number_to_string((
const nyx_number_t *)
object);
551 return nyx_boolean_to_string((
const nyx_boolean_t *)
object);
554 return nyx_string_to_cstring((
const nyx_string_t *)
object);
557 return nyx_list_to_string((
const nyx_list_t *)
object);
560 return nyx_dict_to_string((
const nyx_dict_t *)
object);
Struct describing a JSON boolean object.
Struct describing a JSON dict object.
Struct describing a JSON list object.
#define NYX_LOG_FATAL(fmt,...)
Logs a fatal message.
#define NYX_LOG_ERROR(fmt,...)
Logs an error message.
#define STR_t
Alias for const char *.
__NYX_NULLABLE__ buff_t nyx_memory_alloc(__NYX_ZEROABLE__ size_t size)
Similar to libc malloc except that a memory overflow causes the node to stop.
__NYX_ZEROABLE__ size_t nyx_memory_free(__NYX_NULLABLE__ buff_t buff)
Similar to libc free except that it returns the amount of memory freed.
#define buff_t
Alias for void *.
#define BUFF_t
Alias for const void *.
void nyx_memory_initialize(void)
Initializes the memory subsystem.
bool nyx_memory_finalize(void)
Finalizes the memory subsystem.
__NYX_NULLABLE__ str_t nyx_string_dup(__NYX_NULLABLE__ STR_t s)
Similar to libc strdup.
__NYX_NULLABLE__ str_t nyx_string_ndup(__NYX_NULLABLE__ STR_t s, __NYX_ZEROABLE__ size_t n)
Similar to libc strndup.
__NYX_NULLABLE__ buff_t nyx_memory_realloc(__NYX_NULLABLE__ buff_t buff, __NYX_ZEROABLE__ size_t size)
Similar to libc realloc except that a memory overflow causes the node to stop.
#define str_t
Alias for char *.
Struct describing a JSON null object.
Struct describing a JSON number object.
nyx_type_t
JSON object types.
@ NYX_TYPE_DICT
Dict object.
@ NYX_TYPE_LIST
List object.
@ NYX_TYPE_BOOLEAN
Boolean object.
@ NYX_TYPE_NUMBER
Number object.
@ NYX_TYPE_NULL
Null object.
@ NYX_TYPE_STRING
String object.
Struct describing a JSON object.
Struct describing a JSON string object.