2023-10-31 20:26:31 +03:00
|
|
|
/**
|
|
|
|
* @file defines.h
|
|
|
|
*
|
|
|
|
* Macro definitions used throughout the prism library.
|
|
|
|
*
|
|
|
|
* This file should be included first by any *.h or *.c in prism for consistency
|
|
|
|
* and to ensure that the macros are defined before they are used.
|
|
|
|
*/
|
2023-09-27 19:24:48 +03:00
|
|
|
#ifndef PRISM_DEFINES_H
|
|
|
|
#define PRISM_DEFINES_H
|
2023-06-20 18:53:02 +03:00
|
|
|
|
2023-06-30 21:30:24 +03:00
|
|
|
#include <ctype.h>
|
2024-03-13 21:22:31 +03:00
|
|
|
#include <limits.h>
|
2024-02-23 05:22:56 +03:00
|
|
|
#include <math.h>
|
2023-06-30 21:30:24 +03:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2023-08-29 17:48:20 +03:00
|
|
|
#include <stdint.h>
|
2023-06-30 21:30:24 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-01-19 16:38:37 +03:00
|
|
|
/**
|
|
|
|
* We want to be able to use the PRI* macros for printing out integers, but on
|
|
|
|
* some platforms they aren't included unless this is already defined.
|
|
|
|
*/
|
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2023-10-31 22:40:50 +03:00
|
|
|
/**
|
2023-10-31 20:26:31 +03:00
|
|
|
* By default, we compile with -fvisibility=hidden. When this is enabled, we
|
|
|
|
* need to mark certain functions as being publically-visible. This macro does
|
|
|
|
* that in a compiler-agnostic way.
|
|
|
|
*/
|
2023-09-27 19:24:48 +03:00
|
|
|
#ifndef PRISM_EXPORTED_FUNCTION
|
|
|
|
# ifdef PRISM_EXPORT_SYMBOLS
|
2023-07-31 22:26:34 +03:00
|
|
|
# ifdef _WIN32
|
2023-09-27 19:24:48 +03:00
|
|
|
# define PRISM_EXPORTED_FUNCTION __declspec(dllexport) extern
|
2023-06-20 18:53:02 +03:00
|
|
|
# else
|
2023-09-27 19:24:48 +03:00
|
|
|
# define PRISM_EXPORTED_FUNCTION __attribute__((__visibility__("default"))) extern
|
2023-06-20 18:53:02 +03:00
|
|
|
# endif
|
2023-07-31 22:26:34 +03:00
|
|
|
# else
|
2023-09-27 19:24:48 +03:00
|
|
|
# define PRISM_EXPORTED_FUNCTION
|
2023-06-20 18:53:02 +03:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2023-10-31 20:26:31 +03:00
|
|
|
/**
|
|
|
|
* Certain compilers support specifying that a function accepts variadic
|
|
|
|
* parameters that look like printf format strings to provide a better developer
|
|
|
|
* experience when someone is using the function. This macro does that in a
|
|
|
|
* compiler-agnostic way.
|
|
|
|
*/
|
2023-10-27 05:04:54 +03:00
|
|
|
#if defined(__GNUC__)
|
2024-03-19 04:01:48 +03:00
|
|
|
# if defined(__MINGW_PRINTF_FORMAT)
|
|
|
|
# define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, argument_index)))
|
|
|
|
# else
|
|
|
|
# define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(printf, string_index, argument_index)))
|
|
|
|
# endif
|
2023-10-27 05:04:54 +03:00
|
|
|
#elif defined(__clang__)
|
|
|
|
# define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((__format__(__printf__, string_index, argument_index)))
|
|
|
|
#else
|
|
|
|
# define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index)
|
|
|
|
#endif
|
|
|
|
|
2023-10-31 20:26:31 +03:00
|
|
|
/**
|
|
|
|
* GCC will warn if you specify a function or parameter that is unused at
|
|
|
|
* runtime. This macro allows you to mark a function or parameter as unused in a
|
|
|
|
* compiler-agnostic way.
|
|
|
|
*/
|
2023-06-20 18:53:02 +03:00
|
|
|
#if defined(__GNUC__)
|
2023-09-27 19:24:48 +03:00
|
|
|
# define PRISM_ATTRIBUTE_UNUSED __attribute__((unused))
|
2023-06-20 18:53:02 +03:00
|
|
|
#else
|
2023-09-27 19:24:48 +03:00
|
|
|
# define PRISM_ATTRIBUTE_UNUSED
|
2023-06-20 18:53:02 +03:00
|
|
|
#endif
|
|
|
|
|
2023-10-31 20:26:31 +03:00
|
|
|
/**
|
|
|
|
* Old Visual Studio versions do not support the inline keyword, so we need to
|
|
|
|
* define it to be __inline.
|
|
|
|
*/
|
2023-06-20 18:53:02 +03:00
|
|
|
#if defined(_MSC_VER) && !defined(inline)
|
|
|
|
# define inline __inline
|
|
|
|
#endif
|
|
|
|
|
2023-10-31 20:26:31 +03:00
|
|
|
/**
|
|
|
|
* Old Visual Studio versions before 2015 do not implement sprintf, but instead
|
|
|
|
* implement _snprintf. We standard that here.
|
|
|
|
*/
|
2023-09-05 19:23:35 +03:00
|
|
|
#if !defined(snprintf) && defined(_MSC_VER) && (_MSC_VER < 1900)
|
2023-08-17 01:42:56 +03:00
|
|
|
# define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
2023-12-04 20:51:22 +03:00
|
|
|
/**
|
|
|
|
* A simple utility macro to concatenate two tokens together, necessary when one
|
|
|
|
* of the tokens is itself a macro.
|
|
|
|
*/
|
|
|
|
#define PM_CONCATENATE(left, right) left ## right
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We want to be able to use static assertions, but they weren't standardized
|
|
|
|
* until C11. As such, we polyfill it here by making a hacky typedef that will
|
|
|
|
* fail to compile due to a negative array size if the condition is false.
|
|
|
|
*/
|
|
|
|
#if defined(_Static_assert)
|
|
|
|
# define PM_STATIC_ASSERT(line, condition, message) _Static_assert(condition, message)
|
|
|
|
#else
|
|
|
|
# define PM_STATIC_ASSERT(line, condition, message) typedef char PM_CONCATENATE(static_assert_, line)[(condition) ? 1 : -1]
|
|
|
|
#endif
|
|
|
|
|
2024-01-22 09:17:40 +03:00
|
|
|
/**
|
|
|
|
* In general, libc for embedded systems does not support memory-mapped files.
|
|
|
|
* If the target platform is POSIX or Windows, we can map a file in memory and
|
|
|
|
* read it in a more efficient manner.
|
|
|
|
*/
|
2024-02-20 17:30:44 +03:00
|
|
|
#ifdef _WIN32
|
2024-01-22 09:17:40 +03:00
|
|
|
# define PRISM_HAS_MMAP
|
2024-02-20 17:30:44 +03:00
|
|
|
#else
|
|
|
|
# include <unistd.h>
|
|
|
|
# ifdef _POSIX_MAPPED_FILES
|
|
|
|
# define PRISM_HAS_MMAP
|
|
|
|
# endif
|
2024-01-22 09:17:40 +03:00
|
|
|
#endif
|
|
|
|
|
2024-02-23 05:22:56 +03:00
|
|
|
/**
|
|
|
|
* isinf on Windows is defined as accepting a float, but on POSIX systems it
|
|
|
|
* accepts a float, a double, or a long double. We want to mirror this behavior
|
|
|
|
* on windows.
|
|
|
|
*/
|
|
|
|
#ifdef _WIN32
|
|
|
|
# include <float.h>
|
|
|
|
# undef isinf
|
|
|
|
# define isinf(x) (sizeof(x) == sizeof(float) ? !_finitef(x) : !_finite(x))
|
|
|
|
#endif
|
|
|
|
|
2024-02-27 08:25:22 +03:00
|
|
|
/**
|
2024-03-04 19:34:53 +03:00
|
|
|
* If you build prism with a custom allocator, configure it with
|
|
|
|
* "-D PRISM_XALLOCATOR" to use your own allocator that defines xmalloc,
|
|
|
|
* xrealloc, xcalloc, and xfree.
|
|
|
|
*
|
|
|
|
* For example, your `prism_xallocator.h` file could look like this:
|
|
|
|
*
|
2024-02-27 08:25:22 +03:00
|
|
|
* ```
|
2024-03-03 03:52:36 +03:00
|
|
|
* #ifndef PRISM_XALLOCATOR_H
|
|
|
|
* #define PRISM_XALLOCATOR_H
|
2024-02-27 08:25:22 +03:00
|
|
|
* #define xmalloc my_malloc
|
|
|
|
* #define xrealloc my_realloc
|
|
|
|
* #define xcalloc my_calloc
|
|
|
|
* #define xfree my_free
|
|
|
|
* #endif
|
|
|
|
* ```
|
|
|
|
*/
|
2024-03-03 03:52:36 +03:00
|
|
|
#ifdef PRISM_XALLOCATOR
|
2024-03-04 19:34:53 +03:00
|
|
|
#include "prism_xallocator.h"
|
2024-02-27 08:25:22 +03:00
|
|
|
#else
|
2024-03-04 19:49:09 +03:00
|
|
|
#ifndef xmalloc
|
|
|
|
/**
|
2024-03-24 14:06:35 +03:00
|
|
|
* The malloc function that should be used. This can be overridden with
|
2024-03-04 19:49:09 +03:00
|
|
|
* the PRISM_XALLOCATOR define.
|
|
|
|
*/
|
|
|
|
#define xmalloc malloc
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef xrealloc
|
|
|
|
/**
|
2024-03-24 14:06:35 +03:00
|
|
|
* The realloc function that should be used. This can be overridden with
|
2024-03-04 19:49:09 +03:00
|
|
|
* the PRISM_XALLOCATOR define.
|
|
|
|
*/
|
|
|
|
#define xrealloc realloc
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef xcalloc
|
|
|
|
/**
|
2024-03-24 14:06:35 +03:00
|
|
|
* The calloc function that should be used. This can be overridden with
|
2024-03-04 19:49:09 +03:00
|
|
|
* the PRISM_XALLOCATOR define.
|
|
|
|
*/
|
|
|
|
#define xcalloc calloc
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef xfree
|
|
|
|
/**
|
2024-03-24 14:06:35 +03:00
|
|
|
* The free function that should be used. This can be overridden with the
|
2024-03-04 19:49:09 +03:00
|
|
|
* PRISM_XALLOCATOR define.
|
|
|
|
*/
|
|
|
|
#define xfree free
|
|
|
|
#endif
|
2024-02-27 08:25:22 +03:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 17:08:21 +03:00
|
|
|
/**
|
|
|
|
* If PRISM_BUILD_MINIMAL is defined, then we're going to define every possible
|
|
|
|
* switch that will turn off certain features of prism.
|
|
|
|
*/
|
|
|
|
#ifdef PRISM_BUILD_MINIMAL
|
|
|
|
/** Exclude the serialization API. */
|
|
|
|
#define PRISM_EXCLUDE_SERIALIZATION
|
|
|
|
|
|
|
|
/** Exclude the JSON serialization API. */
|
|
|
|
#define PRISM_EXCLUDE_JSON
|
|
|
|
|
|
|
|
/** Exclude the Array#pack parser API. */
|
|
|
|
#define PRISM_EXCLUDE_PACK
|
|
|
|
|
|
|
|
/** Exclude the prettyprint API. */
|
|
|
|
#define PRISM_EXCLUDE_PRETTYPRINT
|
|
|
|
|
|
|
|
/** Exclude the full set of encodings, using the minimal only. */
|
|
|
|
#define PRISM_ENCODING_EXCLUDE_FULL
|
|
|
|
#endif
|
|
|
|
|
2023-06-20 18:53:02 +03:00
|
|
|
#endif
|