gb.hpp - Atomics

This commit is contained in:
gingerBill 2015-10-03 15:35:16 +01:00
commit 2357bdd227
3 changed files with 48 additions and 36 deletions

View File

@ -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_string.h** | 0.93 | strings | C, C++ | A better string library for C & C++
**gb_ini.h** | 0.91 | misc | C, C++ | A simple ini file loader library for C & C++ **gb_ini.h** | 0.91 | misc | C, C++ | A simple ini file loader library for C & C++
**gb.hpp** | 0.08 | misc | C++11 | (Experimental) A C++11 helper library without STL geared towards game development **gb.hpp** | 0.09 | misc | C++11 | (Experimental) A C++11 helper library without STL geared towards game development
## FAQ ## FAQ

77
gb.hpp
View File

@ -1,7 +1,8 @@
// gb.hpp - v0.08 - public domain C++11 helper library - no warranty implied; use at your own risk // gb.hpp - v0.09 - public domain C++11 helper library - no warranty implied; use at your own risk
// (Experimental) A C++11 helper library without STL geared towards game development // (Experimental) A C++11 helper library without STL geared towards game development
// //
// Version History: // Version History:
// 0.09 - Bug Fixes
// 0.08 - Matrix(2,3) // 0.08 - Matrix(2,3)
// 0.07 - Bug Fixes // 0.07 - Bug Fixes
// 0.06 - Os spec ideas // 0.06 - Os spec ideas
@ -141,6 +142,7 @@
#include <intrin.h> #include <intrin.h>
#else #else
#include <pthread.h> #include <pthread.h>
#include <sys/time.h>
#endif #endif
#ifndef NDEBUG #ifndef NDEBUG
@ -157,7 +159,7 @@ gb__assert_handler(bool condition, const char* condition_str,
if (condition) if (condition)
return; return;
fprintf(stderr, "ASSERT! %s(%d): %s", filename, line, condition_str); fprintf(stderr, "ASSERT! %s(%d): %s", filename, (int)line, condition_str);
if (error_text) if (error_text)
{ {
fprintf(stderr, " - "); fprintf(stderr, " - ");
@ -415,7 +417,7 @@ inline void* alloc(Allocator& a, usize size, usize align = GB_DEFAULT_ALIGNMENT)
inline void dealloc(Allocator& a, void* ptr) { return a.dealloc(ptr); } inline void dealloc(Allocator& a, void* ptr) { return a.dealloc(ptr); }
template <typename T> template <typename T>
inline T* alloc_struct(Allocator& a) { return static_cast<T*>a.alloc(sizeof(T), alignof(T)); } inline T* alloc_struct(Allocator& a) { return static_cast<T*>(a.alloc(sizeof(T), alignof(T))); }
template <typename T> template <typename T>
@ -821,7 +823,7 @@ erase_from_hash_table(Hash_Table<T>& h, const Find_Result& fr)
if (fr.data_prev < 0) if (fr.data_prev < 0)
h.hashes[fr.hash_index] = h.data[fr.data_index].next; h.hashes[fr.hash_index] = h.data[fr.data_index].next;
else else
h.data[fr.data_prev].next = g.data[fr.data_index].next; h.data[fr.data_prev].next = h.data[fr.data_index].next;
pop_back_array(h.data); // updated array count pop_back_array(h.data); // updated array count
@ -869,22 +871,23 @@ Find_Result
find_result_in_hash_table(const Hash_Table<T>& h, const typename Hash_Table<T>::Entry* e) find_result_in_hash_table(const Hash_Table<T>& h, const typename Hash_Table<T>::Entry* e)
{ {
Find_Result fr; Find_Result fr;
fr.hash_index = -1; // TODO(bill):
fr.data_prev = -1; // fr.hash_index = -1;
fr.data_index = -1; // fr.data_prev = -1;
// fr.data_index = -1;
if (h.hashes.count == 0 || !e) // if (h.hashes.count == 0 || !e)
return fr; // return fr;
fr.hash_index = key % h.hashes.count; // fr.hash_index = key % h.hashes.count;
fr.data_index = h.hashes[fr.hash_index]; // fr.data_index = h.hashes[fr.hash_index];
while (fr.data_index >= 0) // while (fr.data_index >= 0)
{ // {
if (&h.data[fr.data_index] == e) // if (&h.data[fr.data_index] == e)
return fr; // return fr;
fr.data_prev = fr.data_index; // fr.data_prev = fr.data_index;
fr.data_index = h.data[fr.data_index].next; // fr.data_index = h.data[fr.data_index].next;
} // }
return fr; return fr;
} }
@ -985,7 +988,7 @@ template <typename T>
inline bool inline bool
hash_table_has(const Hash_Table<T>& h, u64 key) hash_table_has(const Hash_Table<T>& h, u64 key)
{ {
return imple::find_entry_or_fail_in_hash_table(h, key) >= 0; return impl::find_entry_or_fail_in_hash_table(h, key) >= 0;
} }
template <typename T> template <typename T>
@ -1070,7 +1073,7 @@ multiple_count_from_hash_table(const Hash_Table<T>& h, u64 key)
auto e = find_first_in_hash_table(h, key); auto e = find_first_in_hash_table(h, key);
while (e) while (e)
{ {
count++ count++;
e = find_next_in_hash_table(h, e); e = find_next_in_hash_table(h, e);
} }
@ -1082,7 +1085,7 @@ template <typename T>
inline const typename Hash_Table<T>::Entry* inline const typename Hash_Table<T>::Entry*
find_first_in_hash_table(const Hash_Table<T>& h, u64 key) find_first_in_hash_table(const Hash_Table<T>& h, u64 key)
{ {
const s64 index = impl::find_first_in_hash_table(h, key); const s64 index = find_first_in_hash_table(h, key);
if (index < 0) if (index < 0)
return nullptr; return nullptr;
return &h.data[index]; return &h.data[index];
@ -1125,7 +1128,7 @@ template <typename T>
inline void inline void
remove_entry_from_hash_table(Hash_Table<T>& h, const typename Hash_Table<T>::Entry* e) remove_entry_from_hash_table(Hash_Table<T>& h, const typename Hash_Table<T>::Entry* e)
{ {
const auto fr = impl:;find_result_in_hash_table(h, e); const auto fr = impl::find_result_in_hash_table(h, e);
if (fr.data_index >= 0) if (fr.data_index >= 0)
impl::erase_from_hash_table(h, fr); impl::erase_from_hash_table(h, fr);
} }
@ -2913,10 +2916,10 @@ Time time_now()
s64 t = (s64)mach_absolute_time(); s64 t = (s64)mach_absolute_time();
return microseconds(t); return microseconds(t);
#else #else
struct timespec spec; struct timeval t;
clock_gettime(CLOCK_REALTIME, &spec); gettimeofday(&t, nullptr);
return milliseconds((spec.tv_sec * 1000000ll) + (spec.tv_nsec * 1000ll)); return microseconds((t.tv_sec * 1000000ll) + (t.tv_usec * 1ll));
#endif #endif
} }
@ -3925,8 +3928,10 @@ inline f32 magnitude(const Vector2& a)
inline Vector2 normalize(const Vector2& a) inline Vector2 normalize(const Vector2& a)
{ {
f32 m = 1.0f / magnitude(a); f32 m = magnitude(a);
return a * m; if (m > 0)
return a * (1.0f / m);
return {};
} }
inline Vector2 hadamard(const Vector2& a, const Vector2& b) inline Vector2 hadamard(const Vector2& a, const Vector2& b)
@ -3956,8 +3961,10 @@ inline f32 magnitude(const Vector3& a)
inline Vector3 normalize(const Vector3& a) inline Vector3 normalize(const Vector3& a)
{ {
f32 m = 1.0f / magnitude(a); f32 m = magnitude(a);
return a * m; if (m > 0)
return a * (1.0f / m);
return {};
} }
inline Vector3 hadamard(const Vector3& a, const Vector3& b) inline Vector3 hadamard(const Vector3& a, const Vector3& b)
@ -3978,8 +3985,10 @@ inline f32 magnitude(const Vector4& a)
inline Vector4 normalize(const Vector4& a) inline Vector4 normalize(const Vector4& a)
{ {
f32 m = 1.0f / magnitude(a); f32 m = magnitude(a);
return a * m; if (m > 0)
return a * (1.0f / m);
return {};
} }
inline Vector4 hadamard(const Vector4& a, const Vector4& b) inline Vector4 hadamard(const Vector4& a, const Vector4& b)
@ -4008,8 +4017,10 @@ inline f32 magnitude(const Quaternion& a)
inline Quaternion normalize(const Quaternion& a) inline Quaternion normalize(const Quaternion& a)
{ {
f32 m = 1.0f / magnitude(a); f32 m = magnitude(a);
return a * m; if (m > 0)
return a * (1.0f / m);
return {};
} }
inline Quaternion conjugate(const Quaternion& a) inline Quaternion conjugate(const Quaternion& a)

View File

@ -224,6 +224,7 @@ enum
GB_INI_ERROR_COUNT, GB_INI_ERROR_COUNT,
}; };
typedef struct gb_Ini_Error gb_Ini_Error;
struct gb_Ini_Error struct gb_Ini_Error
{ {
int type; int type;
@ -242,8 +243,8 @@ typedef GB_INI_HANDLER(gb_Ini_Handler);
extern const char* GB_ERROR_STRINGS[GB_INI_ERROR_COUNT]; extern const char* GB_ERROR_STRINGS[GB_INI_ERROR_COUNT];
struct gb_Ini_Error gb_ini_parse(const char* filename, gb_Ini_Handler* handler_func, void* data); gb_Ini_Error gb_ini_parse(const char* filename, gb_Ini_Handler* handler_func, void* data);
struct gb_Ini_Error gb_ini_parse_file(FILE* file, gb_Ini_Handler* handler_func, void* data); gb_Ini_Error gb_ini_parse_file(FILE* file, gb_Ini_Handler* handler_func, void* data);
gb_inline const char* gb_inline const char*
gb_ini_error_string(const struct gb_Ini_Error err) gb_ini_error_string(const struct gb_Ini_Error err)