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);
270nyx_str_t nyx_str_s(
STR_t s)
272 nyx_str_t str = {(
str_t) s, s == NULL ? 0x0000000 : strlen(s)};
328 if(object1 == NULL || object2 == NULL)
340 if(object1 == object2)
345 if(object1->type != object2->type)
352 switch(object1->type)
369 str_t s1 = nyx_object_to_string(object1);
370 str_t s2 = nyx_object_to_string(object2);
372 bool equal = strcmp(s1, s2) == 0;
406 return nyx_null_to_string((
const nyx_null_t *)
object);
409 return nyx_number_to_string((
const nyx_number_t *)
object);
412 return nyx_boolean_to_string((
const nyx_boolean_t *)
object);
415 return nyx_string_to_string((
const nyx_string_t *)
object);
418 return nyx_list_to_string((
const nyx_list_t *)
object);
421 return nyx_dict_to_string((
const nyx_dict_t *)
object);
449 return nyx_null_to_string((
const nyx_null_t *)
object);
452 return nyx_number_to_string((
const nyx_number_t *)
object);
455 return nyx_boolean_to_string((
const nyx_boolean_t *)
object);
461 return nyx_list_to_string((
const nyx_list_t *)
object);
464 return nyx_dict_to_string((
const nyx_dict_t *)
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 *.
void nyx_memory_initialize(void)
Initialize the memory subsystem.
bool nyx_memory_finalize(void)
Finalize 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 *.
#define NYX_OBJECT_MAGIC
Magic number for identifying JSON objects.
@ 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.
str_t nyx_string_to_cstring(const nyx_string_t *object)
Struct describing a JSON boolean object.
Struct describing a JSON dict object.
Struct describing a JSON list object.
Struct describing a JSON null object.
Struct describing a JSON number object.
Struct describing a JSON object.
Struct describing a JSON string object.