From 216b9a419f6829a80c38cf0bca68a74974f82fd7 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 15 Dec 2015 00:18:52 +0000 Subject: [PATCH] Allow for no --- README.md | 2 +- gb.h | 48 +++++++++++++++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index d7e0fb1..2381f68 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ library | latest version | category | languages | description **gb_string.h** | 0.93 | strings | C, C++ | A better string library for C & C++ **gb.hpp** | 0.31a | misc | C++11 | (Experimental) A C++11 helper library without STL geared towards game development **gb_math.hpp** | 0.03a | math | C++11 | A C++11 math library geared towards game development -**gb.h** | 0.03 | misc | C | (Experimental) A C helpher library geared towards game development (port of gb.hpp) +**gb.h** | 0.04 | misc | C | (Experimental) A C helpher library geared towards game development (port of gb.hpp) **gb_ini.h** | 0.91a | misc | C, C++ | A simple ini file loader library for C & C++ ## FAQ diff --git a/gb.h b/gb.h index e747792..fad1ee1 100644 --- a/gb.h +++ b/gb.h @@ -1,4 +1,4 @@ -/* gb.h - v0.03 - public domain C helper library - no warranty implied; use at your own risk */ +/* gb.h - v0.04 - public domain C helper library - no warranty implied; use at your own risk */ /* (Experimental) A C helper library geared towards game development */ /* @@ -16,6 +16,7 @@ /* Version History: + 0.04 - Allow for no 0.03 - Allocators can be passed to gb_alloc/free/etc. without cast using `typedef void *gb_Allocator_Ptr` 0.02 - Implement all functions (from gb.hpp) 0.01 - Initial Version (just prototypes) @@ -172,8 +173,9 @@ extern "C" { #endif #endif -#include +#if !defined(GB_NO_STDIO) #include +#endif #include #if defined(GB_SYSTEM_WINDOWS) @@ -190,7 +192,6 @@ extern "C" { #undef WIN32_EXTRA_LEAN #undef WIN32_LEAN_AND_MEAN - #include #else #include #include @@ -205,20 +206,23 @@ extern "C" { #define NULL ((void *)0) #endif -#if !defined(NDEBUG) +#if defined(NDEBUG) + #define GB_ASSERT(cond) ((void)(0)) +#else /* TODO(bill): Do a better assert */ #define GB_ASSERT(cond) assert(cond) -#else - #define GB_ASSERT(cond) ((void)(cond)) #endif -#define GB_STATIC_ASSERT(COND, MSG) typedef char gb__static_assertion_##MSG[(!!(COND))*2-1] +#define GB_STATIC_ASSERT(cond, msg) typedef char gb__static_assertion_##msg[(!!(cond))*2-1] /* token pasting madness: */ #define GB_COMPILE_TIME_ASSERT3(cond, line) GB_STATIC_ASSERT(cond, static_assertion_at_line_##line) #define GB_COMPILE_TIME_ASSERT2(cond, line) GB_COMPILE_TIME_ASSERT3(cond, line) #define GB_COMPILE_TIME_ASSERT(cond) GB_COMPILE_TIME_ASSERT2(cond, __LINE__) -/* snprintf_msvc */ -#if defined(_MSC_VER) + + + +#if !defined(GB_NO_STDIO) && defined(_MSC_VER) + /* snprintf_msvc */ int gb__vsnprintf_compatible(char *buffer, size_t size, const char *format, va_list args) { int result = -1; @@ -240,7 +244,7 @@ extern "C" { #define snprintf gb__snprintf_compatible #define vsnprintf gb__vsnprintf_compatible #endif /* GB_DO_NOT_USE_MSVC_SPRINTF_FIX */ -#endif +#endif /* !defined(GB_NO_STDIO) */ #if defined(_MSC_VER) typedef unsigned __int8 u8; @@ -252,6 +256,8 @@ extern "C" { typedef unsigned __int64 u64; typedef signed __int64 s64; #else + #include + typedef uint8_t u8; typedef int8_t s8; typedef uint16_t u16; @@ -331,7 +337,15 @@ typedef ptrdiff_t ptrdiff; /* NOTE(bill): Easier to grep/find for casts */ /* Still not as type safe as C++ static_cast, reinterpret_cast, const_cast */ -#define cast(Type, var) ((Type)(var)) +#define cast(Type, src) ((Type)(src)) + +#if defined(GB_COMPILER_GNU_GCC) + #define bit_cast(Type, src) ({ GB_ASSERT(sizeof(Type) <= sizeof(src)); Type dst; memcpy(&dst, &(src), sizeof(Type)); dst; }) +#endif + +#define pseudo_cast(Type, src) (*cast(Type *, &(src))) + +#define GB_UNUSED(x) cast(void, sizeof(x)) /**********************************/ @@ -1438,15 +1452,15 @@ gb__arena_alloc(gb_Allocator *a, usize size, usize align) internal_linkage void gb__arena_free(gb_Allocator *a, void *ptr) /* NOTE(bill): Arenas free all at once */ { - cast(void, a); - cast(void, ptr); + GB_UNUSED(a); + GB_UNUSED(ptr); } internal_linkage s64 gb__arena_allocated_size(gb_Allocator *a, const void* ptr) { - cast(void, a); - cast(void, ptr); + GB_UNUSED(a); + GB_UNUSED(ptr); return -1; } @@ -1579,8 +1593,8 @@ gb__pool_free(gb_Allocator *a, void *ptr) internal_linkage s64 gb__pool_allocated_size(gb_Allocator *a, const void *ptr) { - cast(void, a); - cast(void, ptr); + GB_UNUSED(a); + GB_UNUSED(ptr); return -1; }