Initial commit

This commit is contained in:
2026-06-25 18:46:24 -05:00
commit c3dc1beb91
25 changed files with 1828 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
/* - | Copyright / About | ----------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
ci2.h is the next layer of abstraction after the base and platform layer. Base
layer detects os,compiler, etc - the platform abstracts those differences away.
You get to decide which parts of that platform layer you'd like to use.
* --------------------------------------------------------------------------*/
#ifndef CI2_H
#define CI2_H
/* Platform layer is where os differences are abstracted away. */
#include "./platform/ci2_platform.h"
/* Then you import which parts of the platform layer, you'd like. */
#include "./platform/error/ci2_exception.h"
#endif // ci2.h
+89
View File
@@ -0,0 +1,89 @@
/* - | Copyright / About |
------------------------------------------------------------ Copyright (c) 2026
Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* --------------------------------------------------------------------------*/
#ifndef CI2_ASSERT_H
#define CI2_ASSERT_H
#include "../base/ci2_debug_trap.h"
#include "../base/ci2_func.h"
#include "../base/ci2_inline.h"
#include <stdio.h>
/* Assert Fail */
static CI2_INLINE void
ci2_assert_fail(const char* expr,
const char* msg, /* may be NULL */
const char* file,
int line,
const char* func)
{
if (msg)
fprintf(stderr,
"\n[ASSERT FAILED]\n"
" Expression : %s\n"
" Message : %s\n"
" Location : %s:%d\n"
" Function : %s\n\n",
expr,
msg,
file,
line,
func);
else
fprintf(stderr,
"\n[ASSERT FAILED]\n"
" Expression : %s\n"
" Location : %s:%d\n"
" Function : %s\n\n",
expr,
file,
line,
func);
/* Flush so the message is visible before we halt */
fflush(stderr);
CI2_DEBUG_TRAP();
}
#ifndef NDEBUG
#define CI2_ASSERT(expr) \
do { \
if (!(expr)) { \
ci2_assert_fail(#expr, NULL, __FILE__, __LINE__, CI2_FUNC); \
} \
} while (0)
#define CI2_ASSERT_MSG(expr, msg) \
do { \
if (!(expr)) { \
ci2_assert_fail(#expr, (msg), __FILE__, __LINE__, CI2_FUNC); \
} \
} while (0)
#else /* NDEBUG — strip all runtime checks */
#define CI2_ASSERT(expr) ((void)(expr))
#define CI2_ASSERT_MSG(expr, msg) ((void)(expr))
#endif /* NDEBUG */
#endif // ci2_assert.h
+56
View File
@@ -0,0 +1,56 @@
/* - | Copyright / About | ----------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
ci2_base layer is the layer in which most of the OS specific differences are
abstracted away. It contains various os, environments, built-ins, and
compiler detections to provide a simple interface.
* --------------------------------------------------------------------------*/
#ifndef CI2_BASE_H
#define CI2_BASE_H
#include "ci2_compiler.h"
#include "ci2_os.h"
#include "ci2_thread.h"
#include "ci2_win_env.h"
#include "ci2_debug_trap.h"
#include "ci2_func.h"
#include "ci2_inline.h"
#include "ci2_static_assert.h"
#include "ci2_macros.h"
#include "ci2_syntax.h"
#if defined CI2_UNIX
#define CI2_INVALID_FD -1
typedef int ci2_fd;
typedef char ci2_sys_char;
#else // Windows:
#define CI2_INVALID_FD NULL
typedef HANDLE ci2_fd;
typedef wchar_t ci2_sys_char;
#endif
#endif // ci2_base.h
+39
View File
@@ -0,0 +1,39 @@
/* - | Copyright / About | ----------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
ci2_compiler detects the compiler despite clang doing what it wants.
* --------------------------------------------------------------------------*/
#ifndef CI2_COMPILER_H
#define CI2_COMPILER_H
#if defined(__INTEL_LLVM_COMPILER)
#define CI2_INTEL_ICX
#elif defined(__GNUC__) && !defined(__clang__)
#define CI2_GCC
#elif defined(__clang__)
#define CI2_CLANG
#elif define(_MSC_VER) && !defined(__clang__)
#define CI2_MSVC
#else
#error "Compiler is unkown and unsupported."
#endif
#endif // ci2_compiler.h
+45
View File
@@ -0,0 +1,45 @@
/* - | Copyright / About | ----------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
ci2_debug_trap is a portable way to break out a program without before anything
can disturb state.
* --------------------------------------------------------------------------*/
#ifndef CI2_DEBUG_TRAP_H
#define CI2_DEBUG_TRAP_H
/* Use __has_builtin if available (Clang and newer GCC). */
#if defined(__has_builtin)
/* Check builtins in preferred order */
#if __has_builtin(__builtin_debugtrap)
#define CI2_DEBUG_TRAP() __builtin_debugtrap()
#elif __has_builtin(__builtin_trap)
#define CI2_DEBUG_TRAP() __builtin_trap()
#elif __has_builtin(__builtin_break)
#define CI2_DEBUG_TRAP() __builtin_break()
#endif
#elif defined(_MSC_VER) || defined(__INTEL__LLVM_COMPILER)
#define CI2_DEBUG_TRAP() __debugbreak()
#else
#define CI2_DEBUG_TRAP() (*(volatile int*)0 = 0)
#endif
#endif // ci2_debug_trap.h
+35
View File
@@ -0,0 +1,35 @@
/* - | Copyright / About | ----------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
ci2_func.h provides a standard __func__ macro.
* --------------------------------------------------------------------------*/
#ifndef CI2_FUNC_H
#define CI2_FUNC_H
/* Current __func__ */
#if defined(__FUNC__)
#define CI2_FUNC __FUNCTION__
#else
#define CI2_FUNC __func__
#endif
#endif // ci2_func.h
+38
View File
@@ -0,0 +1,38 @@
/* - | Copyright / About | ----------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
ci2_inline is a portable way to try to get the compiler not to perform magic
on your stuff.
* --------------------------------------------------------------------------*/
#ifndef CI2_INLINE_H
#define CI2_INLINE_H
/* Force Inline */
#if defined(_MSC_VER) || defined(__INTEL__LLVM_COMPILER)
#define CI2_INLINE __forceinline
#elif defined(__GNUC__) || defined(__clang__)
#define CI2_INLINE __attribute__((always_inline)) inline
#else
#define CI2_INLINE inline
#endif
#endif // ci2_inline.h
+104
View File
@@ -0,0 +1,104 @@
/* - | Copyright / About | ----------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
ci2_macros contains general purpose macros that are not apart of the platform
api, or library. You may want to exclude them.
* --------------------------------------------------------------------------*/
#ifndef CI2_MACROS_H
#define CI2_MACROS_H
#define UNUSED(x) (void)(x)
#define STR2(s) #s
#define STR(s) STR2(s)
#define U8_MIN 0u
#define U8_MAX 0xffu
#define I8_MIN (-0x7f - 1)
#define I8_MAX 0x7f
#define U16_MIN 0u
#define U16_MAX 0xffffu
#define I16_MIN (-0x7fff - 1)
#define I16_MAX 0x7fff
#define U32_MIN 0u
#define U32_MAX 0xffffffffu
#define I32_MIN (-0x7fffffff - 1)
#define I32_MAX 0x7fffffff
#define U64_MIN 0ull
#define U64_MAX 0xffffffffffffffffull
#define I64_MIN (-0x7fffffffffffffffll - 1)
#define I64_MAX 0x7fffffffffffffffll
#define F32_MIN 1.17549435e-38f
#define F32_MAX 3.40282347e+38f
#define F64_MIN 2.2250738585072014e-308
#define F64_MAX 1.7976931348623157e+308
#define PI 3.14159265
#define RAD2DEG(x) ((x) / PI * 180)
#define DEG2RAD(x) ((x) * PI / 180)
#define ALIGNB(x, align) (((x) + ((align) - 1)) & ~((align) - 1))
#define ALIGN(x, align) ((((x) + ((align) - 1)) / (align)) * (align))
#define FLOORB(x, align) ((x) & ~((align) - 1))
#define FLOOR(x, align) (((x) / (align)) * (align))
#define CEILB(x, align) ALIGNB(x, align)
#define CEIL(x, align) ALIGN(x, align)
#define CLIP(x, min, max) \
(((x) < (min)) ? (min) : (((x) > (max)) ? (max) : (x)))
#define UCLIP(x, max) (((x) > (max)) ? (max) : (x))
#define LCLIP(x, min) (((x) < (min)) ? (min) : (x))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define ABS(x) (((x) < 0) ? -(x) : (x))
#define DIFF(a, b) ABS((a) - (b))
#define IS_NAN(x) ((x) != (x))
#define IMPLIES(x, y) (!(x) || (y))
#define COMPARE(x, y) (((x) > (y)) - ((x) < (y)))
#define SIGN(x) COMPARE(x, 0)
#define IS_ODD(num) ((num) & 1)
#define IS_EVEN(num) (!IS_ODD((num)))
#define IS_BETWEEN(n, L, H) ((unsigned char)((n) >= (L) && (n) <= (H)))
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#define SET(d, n, v) \
do { \
size_t i_, n_; \
for (n_ = (n), i_ = 0; n_ > 0; --n_, ++i_) \
(d)[i_] = (v); \
} while (0)
#define ZERO(d, n) SET(d, n, 0)
#define SWAP(a, b) \
do { \
a ^= b; \
b ^= a; \
a ^= b; \
} while (0)
#define SORT(a, b) \
do { \
if ((a) > (b)) \
SWAP((a), (b)); \
} while (0)
#endif // ci2_macros.h
+84
View File
@@ -0,0 +1,84 @@
/* - | Copyright / About | ----------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
ci2_os.h check the system operating system and defines any macros and settings
required to that specific operating system. I.E Windows version target etc.
* --------------------------------------------------------------------------*/
#ifndef CI2_OS_H
#define CI2_OS_H
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
/* Entire windows platform */
#define CI2_WIN_APIVER 0x0600 // Vista
#define NOMINMAX // Don't define min/max macros
#define _CRT_SECURE_NO_WARNINGS // No unsafe C warnings
#define _CRT_RAND_S // TODO
#define OEMRESOURCE // GUI must be before windows.h
#include <windows.h> // haha
#define CI2_WINDOWS
#ifdef _WIN64
/* Windows (64-bit only) */
#define CI2_WINDOWS_64
#else
/* Windows (32-bit only) */
#define CI2_WINDOWS_32
#endif
#elif __APPLE__
#define CI2_UNIX
#define CI2_APPLE
#include <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR
// iOS, tvOS, or watchOS Simulator
#define CI2_IOS
#elif TARGET_OS_MACCATALYST
// Mac's Catalyst (ports iOS API into Mac, like UIKit).
#define CI2_IOS
#elif TARGET_OS_IPHONE
// iOS, tvOS, or watchOS device
#define CI2_IOS
#elif TARGET_OS_MAC
// Other kinds of Apple platforms
#define CI2_OSX
#else
#error "Unknown Apple platform"
#endif
#elif __ANDROID__
#define CI2_UNIX
#define CI2_ANDROID
#elif __linux__
#define CI2_UNIX
#elif __unix__ // TODO BSD Variants
#define CI2_UNIX
#elif defined(_POSIX_VERSION)
#define CI2_POSIX
#else
#error "Unknown and unsupported operating system."
#endif
#endif // ci2_os.h
+38
View File
@@ -0,0 +1,38 @@
/* - | Copyright / About | ----------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
ci2_static_assert check for c11 static asserts, otherwise use abuse of a
* --------------------------------------------------------------------------*/
#ifndef CI2_STATIC_ASSERT_H
#define CI2_STATIC_ASSERT_H
/* Static Asserts */
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
/* C11 */
#define CI2_STATIC_ASSERT(expr) _Static_assert((expr), #expr)
#else
#define CI2_STATIC_ASSERT(expr, msg) \
extern int(*__Static_assert_function( \
void))[!!sizeof(struct { int __error_if_negative : (expr) ? 2 : -1; })]
#endif
#endif // ci2_static_assert.h
+72
View File
@@ -0,0 +1,72 @@
/* - | Copyright / About | ----------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
ci2_syntax contains short hands for various primitive / basic C types. If C99
stdint.h is available, contains short hands for all standard int sizes. You may
want to exclude these.
* --------------------------------------------------------------------------*/
#ifndef CI2_SYNTAX_H
#define CI2_SYNTAX_H
/* Floating Points */
typedef float ci2_f32; // IEEE 754
typedef double ci2_f64; // IEE 754
/* Characters */
typedef signed char ci2_char; // [127, +127]
typedef unsigned char ci2_uchar; // [0, 255]
typedef unsigned char ci2_byte; // [0, 255]
typedef unsigned char* ci2_bytes; // Pointer
/* Integers */
typedef signed short ci2_short; // [32,767, 32,767]
typedef unsigned short ci2_ushort; // [0, 65,535]
typedef signed int ci2_int; // [32,767, 32,767]
typedef unsigned int ci2_uint; // [0, 65,535]
typedef signed long int ci2_long; // [2,147,483,647, 2,147,483,647]
typedef unsigned long int ci2_ulong; // [0, 4,294,967,295]
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
// 9223372036854775807
typedef unsigned long long int ci2_ull; // +9223372036854775807 range.
typedef signed long long int ci2_ll; //[0, 18446744073709551615] range.
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef uintptr_t uptr;
typedef ptrdiff_t size;
typedef size_t usize;
#endif // C99
#endif // ci2_syntax.h
+37
View File
@@ -0,0 +1,37 @@
/* - | Copyright / About | ----------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
ci2_thread is compiler portable way to declare a thread local variable.
* --------------------------------------------------------------------------*/
#ifndef CI2_THREAD_H
#define CI2_THREAD_H
#if !defined(ci2_thread_local)
#if defined(_MSC_VER) && _MSC_VER >= 1300
#define ci2_thread_local __declspec(thread)
#elif defined(__GNUC__)
#define ci2_thread_local __thread
#else
#define ci2_thread_local thread_local
#endif
#endif
#endif // ci2_thread.h
+39
View File
@@ -0,0 +1,39 @@
/* - | Copyright / About |
------------------------------------------------------------ Copyright (c) 2026
Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
ci2_win_env detects mingw cygwin, because windows sucks.
* --------------------------------------------------------------------------*/
#ifndef CI2_WIN_ENV_H
#define CI2_WIN_ENV_H
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#if defined __MINGW32__ || defined __MINGW64__
#define CI2_WIN_ENV
#define CI2_MINGW
#elif defined __CYGWIN__
#define CI2_WIN_ENV
#define CI2_CYGWIN
#endif
#endif
#endif // ci2_win_env.h
+86
View File
@@ -0,0 +1,86 @@
/* - | Copyright | ------------------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* --------------------------------------------------------------------------*/
#ifndef CI2_PLATFORM_H
#define CI2_PLATFORM_H
#include "./assert/ci2_assert.h"
#include "./base/ci2_base.h"
/* CI2_EXTERN: plain extern with C++ aware linkage token for macros if needed */
#if defined(__cplusplus)
#define CI2_EXTERN extern "C"
#else
#define CI2_EXTERN extern
#endif
/* CI2_API: platform/compiler-aware import/export/visibility */
#if defined(_WIN32) || defined(__CYGWIN__)
#if defined(CI2_BUILD_DLL)
#define CI2_API CI2_EXTERN __declspec(dllexport)
#elif defined(CI2_USE_DLL)
#define CI2_API CI2_EXTERN __declspec(dllimport)
#else
#define CI2_API CI2_EXTERN
#endif
#else
/* Non-Windows: use visibility if supported */
#if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
#if defined(CI2_BUILD_DLL)
#define CI2_API CI2_EXTERN __attribute__((visibility("default")))
#else
#define CI2_API CI2_EXTERN
#endif
#else
#define CI2_API CI2_EXTERN
#endif
#endif
/* CI2_DEF: internal vs external linkage */
#ifndef CI2_DEF
#ifdef CI2_STATIC
#define CI2_DEF static
#else
#define CI2_DEF CI2_API
#endif
#endif
#define CI2_SUCCESS 0
#define CI2_FAILURE -1
#define CI2_TRUE 1
#define CI2_FALSE 0
#define CI2_SIZE(x) (ptrdiff_t)sizeof(x)
#define CI2_COUNT(a) (sizeof(a) / sizeof(*(a)))
#define CI2_LEN(s) (countof(s) - 1)
#define CI2_KB(x) ((x) * (size_t)(1024))
#define CI2_MB(x) (CI2_KB(x) * (size_t)(1024))
#define CI2_GB(x) (CI2_MB(x) * (size_t)(1024))
#define CI2_TB(x) (CI2_GB(x) * (size_t)(1024))
CI2_API ci2_thread_local int ci2_errno;
CI2_API int*
ci2_errno_location(void);
#endif // ci2_platform.h
+188
View File
@@ -0,0 +1,188 @@
/* - | Copyright | ------------------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* --------------------------------------------------------------------------*/
#ifndef CI2_EXCEPTION_H
#define CI2_EXCEPTION_H
#include "../ci2_platform.h"
#include <setjmp.h>
struct Exception
{
const char* msg;
};
typedef struct Exception Exception;
struct Exception_Frame
{
struct Exception_Frame* prev;
jmp_buf env;
const char* file;
const char* func;
int line;
const struct Exception* exception;
};
typedef struct Exception_Frame Exception_Frame;
enum
{
EXCEPTION_ENTERED = 0,
EXCEPTION_RAISED,
EXCEPTION_HANDLED,
EXCEPTION_FINALIZED
};
CI2_DEF const struct Exception assertion_failed;
CI2_DEF void
ci2_rt_assert(int cond);
CI2_DEF void
ci2_raise_exception(const struct Exception* e,
const char* file,
const char* func,
int line);
#if defined(CI2_WINDOWS)
CI2_DEF DWORD exception_stack;
CI2_DEF void
ci2_exception_init(void);
CI2_DEF void
ci2_exception_push(struct Exception_Frame* fp);
CI2_DEF void
ci2_exception_pop(void);
#define CI2_RAISE(e) ci2_raise_exception(&(e), __FILE__, CI2_FUNC, __LINE__)
#define CI2_RERAISE \
ci2_raise_exception(exception_frame.exception, \
exception_frame.file, \
exception_frame.func, \
exception_frame.line)
#define CI2_RETURN \
switch (ci2_exception_pop(), 0) \
default: \
return
#define CI2_TRY \
do { \
volatile int exception_flag; \
Exception_Frame exception_frame; \
if (exception_stack == TLS_OUT_OF_INDEXES) \
ci2_exception_init(); \
ci2_exception_push(&exception_frame); \
exception_flag = setjmp(exception_frame.env); \
if (exception_flag == EXCEPTION_ENTERED) {
#define CI2_EXCEPT(e) \
if (exception_flag == EXCEPTION_ENTERED) \
ci2_exception_pop(); \
} \
else if (exception_frame.exception == &(e)) \
{ \
exception_flag = EXCEPTION_HANDLED;
#define CI2_ELSE \
if (exception_flag == EXCEPTION_ENTERED) \
ci2_exception_pop(); \
} \
else \
{ \
exception_flag = EXCEPTION_HANDLED;
#define CI2_FINALLY \
if (exception_flag == EXCEPTION_ENTERED) \
ci2_exception_pop(); \
} \
{ \
if (exception_flag == EXCEPTION_ENTERED) \
exception_flag = EXCEPTION_FINALIZED;
#define CI2_END_TRY \
if (exception_flag == EXCEPTION_ENTERED) \
ci2_exception_pop(); \
} \
if (exception_flag == EXCEPTION_RAISED) \
CI2_RERAISE; \
} \
while (0)
#else // UNIX
CI2_DEF struct Exception_Frame* exception_stack;
#define CI2_RAISE(e) ci2_raise_exception(&(e), __FILE__, CI2_FUNC, __LINE__)
#define CI2_RERAISE \
ci2_raise_exception(exception_frame.exception, \
exception_frame.file, \
exception_frame.func, \
exception_frame.line)
#define CI2_RETURN \
switch (exception_stack = exception_stack->prev, 0) \
default: \
return
#define CI2_TRY \
do { \
volatile int exception_flag; \
Exception_Frame exception_frame; \
exception_frame.prev = exception_stack; \
exception_stack = &exception_frame; \
exception_flag = setjmp(exception_frame.env); \
if (exception_flag == EXCEPTION_ENTERED) {
#define CI2_EXCEPT(e) \
if (exception_flag == EXCEPTION_ENTERED) \
exception_stack = exception_stack->prev; \
} \
else if (exception_frame.exception == &(e)) \
{ \
exception_flag = EXCEPTION_HANDLED;
#define CI2_ELSE \
if (exception_flag == EXCEPTION_ENTERED) \
exception_stack = exception_stack->prev; \
} \
else \
{ \
exception_flag = EXCEPTION_HANDLED;
#define CI2_FINALLY \
if (exception_flag == EXCEPTION_ENTERED) \
exception_stack = exception_stack->prev; \
} \
{ \
if (exception_flag == EXCEPTION_ENTERED) \
exception_flag = EXCEPTION_FINALIZED;
#define CI2_END_TRY \
if (exception_flag == EXCEPTION_ENTERED) \
exception_stack = exception_stack->prev; \
} \
if (exception_flag == EXCEPTION_RAISED) \
CI2_RERAISE; \
} \
while (0)
#endif
#endif // ci2_exception.h