Update gb.h - v0.02

Minor fixes to work with older versions of MSVC
This commit is contained in:
gingerBill 2016-03-03 16:15:25 +00:00
parent de04cbcee7
commit b6021f11c2
1 changed files with 58 additions and 37 deletions

79
gb.h
View File

@ -1,4 +1,4 @@
// gb.h - v0.01 - public domain C helper library - no warranty implied; use at your own risk // gb.h - v0.02 - public domain C helper library - no warranty implied; use at your own risk
// (Experimental) A C helper library geared towards game development // (Experimental) A C helper library geared towards game development
/* /*
@ -33,6 +33,7 @@ TODO
/* /*
Version History: Version History:
0.02 - Minor fixes
0.01 - Initial Version 0.01 - Initial Version
*/ */
@ -72,13 +73,14 @@ extern "C" {
#ifndef GB_ASSERT #ifndef GB_ASSERT
#include <assert.h> #include <assert.h>
#define GB_ASSERT(cond) assert(cond) #define GB_ASSERT(cond) assert(cond)
#define GB_ASSERT_MSG(cond, msg) assert((cond) && (msg))
#endif #endif
#define GB_STATIC_ASSERT(cond, msg) typedef char gb__static_assertion_##msg[(!!(cond))*2-1] #define GB_STATIC_ASSERT3(cond, msg) typedef char gb__static_assertion_##msg[(!!(cond))*2-1]
// NOTE(bill): Token pasting madness // NOTE(bill): Token pasting madness
#define GB_COMPILE_TIME_ASSERT3(cond, line) GB_STATIC_ASSERT(cond, static_assertion_at_line_##line) #define GB_STATIC_ASSERT2(cond, line) GB_STATIC_ASSERT3(cond, static_assertion_at_line_##line)
#define GB_COMPILE_TIME_ASSERT2(cond, line) GB_COMPILE_TIME_ASSERT3(cond, line) #define GB_STATIC_ASSERT1(cond, line) GB_STATIC_ASSERT2(cond, line)
#define GB_COMPILE_TIME_ASSERT(cond) GB_COMPILE_TIME_ASSERT2(cond, __LINE__) #define GB_STATIC_ASSERT(cond) GB_STATIC_ASSERT1(cond, __LINE__)
#if !defined(GB_NO_STDIO) && defined(_MSC_VER) #if !defined(GB_NO_STDIO) && defined(_MSC_VER)
@ -136,26 +138,37 @@ extern "C" {
typedef int64_t s64; typedef int64_t s64;
#endif #endif
GB_COMPILE_TIME_ASSERT(sizeof(s8) == 1); GB_STATIC_ASSERT(sizeof(s8) == 1);
GB_COMPILE_TIME_ASSERT(sizeof(s16) == 2); GB_STATIC_ASSERT(sizeof(s16) == 2);
GB_COMPILE_TIME_ASSERT(sizeof(s32) == 4); GB_STATIC_ASSERT(sizeof(s32) == 4);
GB_COMPILE_TIME_ASSERT(sizeof(s64) == 8); GB_STATIC_ASSERT(sizeof(s64) == 8);
typedef size_t usize; typedef size_t usize;
typedef uintptr_t uintptr; typedef uintptr_t uintptr;
typedef intptr_t intptr;
typedef float f32; typedef float f32;
typedef double f64; typedef double f64;
#include <stdbool.h> // NOTE(bill): To get false/true #if defined(_MSC_VER) && _MSC_VER < 1900
#ifndef false
#define false 0
#endif
#ifndef true
#define true 1
#endif
#else
#include <stdbool.h> // NOTE(bill): To get false/true
#endif
// Boolean Types // Boolean Types
typedef s8 b8; typedef s8 b8;
typedef s32 b32; typedef s32 b32;
#ifndef U8_MIN
#define U8_MIN 0u #define U8_MIN 0u
#define U8_MAX 0xffu #define U8_MAX 0xffu
#define S8_MIN (-0x7f - 1) #define S8_MIN (-0x7f - 1)
@ -175,7 +188,7 @@ typedef s32 b32;
#define U64_MAX 0xffffffffffffffffull #define U64_MAX 0xffffffffffffffffull
#define S64_MIN (-0x7fffffffffffffffll - 1) #define S64_MIN (-0x7fffffffffffffffll - 1)
#define S64_MAX 0x7fffffffffffffffll #define S64_MAX 0x7fffffffffffffffll
#endif
@ -185,7 +198,7 @@ typedef s32 b32;
#endif #endif
// NOTE(bill): Allows for easy grep of casts // NOTE(bill): Allows for easy grep of casts
// NOTE(bill): Still not as type safe as C++ static_cast, reinterpret_cast, const_cast // NOTE(bill): Still not as type safe as C++ static_cast, reinterpret_cast, and const_cast, but I don't need them
#ifndef cast #ifndef cast
#define cast(x) (x) #define cast(x) (x)
#endif #endif
@ -271,7 +284,10 @@ typedef enum gb_Allocation_Type
GB_ALLOCATION_TYPE_RESIZE, GB_ALLOCATION_TYPE_RESIZE,
} gb_Allocation_Type; } gb_Allocation_Type;
#ifndef GB_ALLOCATOR_PROCEDURE
#define GB_ALLOCATOR_PROCEDURE(name) void *name(void *allocator_data, gb_Allocation_Type type, usize size, usize alignment, void *old_memory, usize old_size, u32 options) #define GB_ALLOCATOR_PROCEDURE(name) void *name(void *allocator_data, gb_Allocation_Type type, usize size, usize alignment, void *old_memory, usize old_size, u32 options)
#endif
typedef GB_ALLOCATOR_PROCEDURE(gb_Allocator_Procedure); typedef GB_ALLOCATOR_PROCEDURE(gb_Allocator_Procedure);
typedef struct gb_Allocator typedef struct gb_Allocator
@ -471,27 +487,30 @@ gb_init_pool(gb_Pool *pool, gb_Allocator backing, usize num_blocks, usize block_
void void
gb_init_pool_align(gb_Pool *pool, gb_Allocator backing, usize num_blocks, usize block_size, usize block_align) gb_init_pool_align(gb_Pool *pool, gb_Allocator backing, usize num_blocks, usize block_size, usize block_align)
{ {
memset(pool, 0, sizeof(gb_Pool)); usize actual_block_size, pool_size, block_index;
u8 *data, *curr;
pool->backing = backing; pool->backing = backing;
pool->block_size = block_size; pool->block_size = block_size;
pool->block_align = block_align; pool->block_align = block_align;
usize actual_block_size = block_size + block_align; actual_block_size = block_size + block_align;
usize pool_size = num_blocks * actual_block_size; pool_size = num_blocks * actual_block_size;
u8 *data = cast(u8 *)gb_alloc_align(backing, pool_size, block_align); data = cast(u8 *)gb_alloc_align(backing, pool_size, block_align);
// Init intrusive freelist // Init intrusive freelist
u8 *curr = data; curr = data;
for (usize block_index = 0; block_index < num_blocks-1; block_index++) { for (block_index = 0; block_index < num_blocks-1; block_index++) {
uintptr *next = cast(uintptr *)curr; uintptr *next = cast(uintptr *)curr;
*next = cast(uintptr)curr + actual_block_size; *next = cast(uintptr)curr + actual_block_size;
curr += actual_block_size; curr += actual_block_size;
} }
{
uintptr *end = cast(uintptr*)curr; uintptr *end = cast(uintptr*)curr;
*end = cast(uintptr)NULL; *end = cast(uintptr)NULL;
}
pool->physical_start = data; pool->physical_start = data;
pool->free_list = data; pool->free_list = data;
@ -646,22 +665,23 @@ gb_string_make_length(gb_Allocator a, void const *init_str, gb_String_Size num_b
gb_String_Size header_size = sizeof(gb_String_Header); gb_String_Size header_size = sizeof(gb_String_Header);
void *ptr = gb_alloc(a, header_size + num_bytes + 1); void *ptr = gb_alloc(a, header_size + num_bytes + 1);
gb_String str; if (ptr == NULL) {
gb_String_Header *header; return NULL;
} else {
gb_String str = cast(char *)ptr + header_size;
gb_String_Header *header = GB_STRING_HEADER(str);
// Zero all data first
if (!init_str) gb_zero_size(ptr, header_size + num_bytes + 1); if (!init_str) gb_zero_size(ptr, header_size + num_bytes + 1);
if (ptr == NULL) return NULL;
str = cast(char *)ptr + header_size;
header = GB_STRING_HEADER(str);
header->allocator = a; header->allocator = a;
header->length = num_bytes; header->length = num_bytes;
header->capacity = num_bytes; header->capacity = num_bytes;
if (num_bytes && init_str) if (num_bytes && init_str)
memcpy(str, init_str, num_bytes); memcpy(str, init_str, num_bytes);
str[num_bytes] = '\0'; str[num_bytes] = '\0'; // Just in case
return str; return str;
}
} }
gb_inline void gb_inline void
@ -746,6 +766,7 @@ gb__string_realloc(gb_Allocator a, void *ptr, gb_String_Size old_size, gb_String
if (old_size == new_size) { if (old_size == new_size) {
return ptr; return ptr;
} else { } else {
// TODO(bill): Use gb_resize here??
void *new_ptr = gb_alloc(a, new_size); void *new_ptr = gb_alloc(a, new_size);
if (!new_ptr) if (!new_ptr)
return NULL; return NULL;
@ -785,8 +806,8 @@ gb_string_make_space_for(gb_String str, gb_String_Size add_len)
gb_inline gb_String_Size gb_inline gb_String_Size
gb_string_allocation_size(gb_String const str) gb_string_allocation_size(gb_String const str)
{ {
gb_String_Size cap = gb_string_capacity(str); gb_String_Size result = gb_string_capacity(str) + cast(gb_String_Size)sizeof(gb_String_Header);
return sizeof(gb_String_Header) + cap; return result;
} }