Dealloc to Free & More Hashing Functions
This commit is contained in:
parent
7c971f24fb
commit
fdf6d07472
|
@ -5,7 +5,7 @@ gb single-file public domain libraries for C & C++
|
|||
library | latest version | category | languages | description
|
||||
----------------|----------------|----------|-----------|-------------
|
||||
**gb_string.h** | 0.93 | strings | C, C++ | A better string library for C & C++
|
||||
**gb.hpp** | 0.26a | misc | C++11 | (Experimental) A C++11 helper library without STL geared towards game development
|
||||
**gb.hpp** | 0.27 | misc | C++11 | (Experimental) A C++11 helper library without STL geared towards game development
|
||||
**gb_math.hpp** | 0.02b | math | C++11 | A C++11 math library geared towards game development
|
||||
**gb_ini.h** | 0.91a | misc | C, C++ | A simple ini file loader library for C & C++
|
||||
|
||||
|
|
112
gb.hpp
112
gb.hpp
|
@ -1,4 +1,4 @@
|
|||
// gb.hpp - v0.26a - public domain C++11 helper library - no warranty implied; use at your own risk
|
||||
// gb.hpp - v0.27 - 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
|
||||
|
||||
/*
|
||||
|
@ -847,7 +847,6 @@ bool is_running(const Thread& t);
|
|||
u32 current_id();
|
||||
} // namespace thread
|
||||
|
||||
|
||||
/// Default alignment for memory allocations
|
||||
#ifndef GB_DEFAULT_ALIGNMENT
|
||||
#define GB_DEFAULT_ALIGNMENT 8
|
||||
|
@ -858,8 +857,8 @@ struct Allocator
|
|||
{
|
||||
/// Allocates the specified amount of memory aligned to the specified alignment
|
||||
void* (*alloc)(Allocator* a, usize size, usize align);
|
||||
/// Deallocates/frees an allocation made with alloc()
|
||||
void (*dealloc)(Allocator* a, void* ptr);
|
||||
/// Frees an allocation made with alloc()
|
||||
void (*free)(Allocator* a, void* ptr);
|
||||
/// Returns the amount of usuable memory allocated at `ptr`.
|
||||
///
|
||||
/// If the allocator does not support tracking of the allocation size,
|
||||
|
@ -960,7 +959,7 @@ void swap(T (& a)[N], T (& b)[N]);
|
|||
} // namespace memory
|
||||
|
||||
void* alloc(Allocator* a, usize size, usize align = GB_DEFAULT_ALIGNMENT);
|
||||
void dealloc(Allocator* a, void* ptr);
|
||||
void free(Allocator* a, void* ptr);
|
||||
s64 allocated_size(Allocator* a, const void* ptr);
|
||||
s64 total_allocated(Allocator* a);
|
||||
|
||||
|
@ -1210,11 +1209,10 @@ u32 adler32(const void* key, u32 num_bytes);
|
|||
u32 crc32(const void* key, u32 num_bytes);
|
||||
u64 crc64(const void* key, usize num_bytes);
|
||||
|
||||
// TODO(bill): Complete hashing functions
|
||||
// u32 fnv32(const void* key, usize num_bytes);
|
||||
// u64 fnv64(const void* key, usize num_bytes);
|
||||
// u32 fnv32a(const void* key, usize num_bytes);
|
||||
// u64 fnv64a(const void* key, usize num_bytes);
|
||||
u32 fnv32(const void* key, usize num_bytes);
|
||||
u64 fnv64(const void* key, usize num_bytes);
|
||||
u32 fnv32a(const void* key, usize num_bytes);
|
||||
u64 fnv64a(const void* key, usize num_bytes);
|
||||
|
||||
u32 murmur32(const void* key, u32 num_bytes, u32 seed = 0x9747b28c);
|
||||
u64 murmur64(const void* key, usize num_bytes, u64 seed = 0x9747b28c);
|
||||
|
@ -1411,7 +1409,7 @@ inline
|
|||
Array<T>::~Array()
|
||||
{
|
||||
if (allocator && capacity > 0)
|
||||
dealloc(allocator, data);
|
||||
free(allocator, data);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1435,7 +1433,7 @@ Array<T>::operator=(Array<T>&& other)
|
|||
if (this != &other)
|
||||
{
|
||||
if (allocator && capacity > 0)
|
||||
dealloc(allocator, data);
|
||||
free(allocator, data);
|
||||
|
||||
allocator = other.allocator;
|
||||
count = other.count;
|
||||
|
@ -1497,7 +1495,7 @@ inline void
|
|||
free(Array<T>* a)
|
||||
{
|
||||
if (a->allocator)
|
||||
dealloc(a->allocator, a->data);
|
||||
free(a->allocator, a->data);
|
||||
a->count = 0;
|
||||
a->capacity = 0;
|
||||
a->data = nullptr;
|
||||
|
@ -1581,7 +1579,7 @@ set_capacity(Array<T>* a, usize capacity)
|
|||
data = alloc_array<T>(a->allocator, capacity);
|
||||
memory::copy_array(a->data, a->count, data);
|
||||
}
|
||||
dealloc(a->allocator, a->data);
|
||||
free(a->allocator, a->data);
|
||||
a->data = data;
|
||||
a->capacity = capacity;
|
||||
}
|
||||
|
@ -2658,7 +2656,7 @@ alloc(Allocator* a, usize size, usize align)
|
|||
}
|
||||
|
||||
internal_linkage void
|
||||
dealloc(Allocator* a, void* ptr)
|
||||
free(Allocator* a, void* ptr)
|
||||
{
|
||||
if (!ptr)
|
||||
return;
|
||||
|
@ -2702,7 +2700,7 @@ allocated_size(Allocator* a, const void* ptr)
|
|||
return static_cast<usize>(malloc_size(ptr));
|
||||
|
||||
#elif defined(GB_SYSTEM_LINUX)
|
||||
return static_cast<usize>(malloc_usable_size(const_cast<void*>(ptr)));
|
||||
return static_cast<usize>(malloc_usable_size(ptr));
|
||||
|
||||
#else
|
||||
#error Implement heap_allocator::allocated_size
|
||||
|
@ -2739,7 +2737,7 @@ make(bool use_mutex)
|
|||
#endif
|
||||
|
||||
heap.alloc = functions::alloc;
|
||||
heap.dealloc = functions::dealloc;
|
||||
heap.free = functions::free;
|
||||
heap.allocated_size = functions::allocated_size;
|
||||
heap.total_allocated = functions::total_allocated;
|
||||
|
||||
|
@ -2781,7 +2779,7 @@ alloc(Allocator* a, usize size, usize align)
|
|||
}
|
||||
|
||||
inline void
|
||||
dealloc(Allocator* a, void*) {}
|
||||
free(Allocator* a, void*) {}
|
||||
|
||||
inline s64 allocated_size(Allocator*, const void*) { return -1; }
|
||||
|
||||
|
@ -2806,7 +2804,7 @@ make(Allocator* backing, usize size)
|
|||
arena.physical_start = alloc(arena.backing, size);
|
||||
|
||||
arena.alloc = functions::alloc;
|
||||
arena.dealloc = functions::dealloc;
|
||||
arena.free = functions::free;
|
||||
arena.allocated_size = functions::allocated_size;
|
||||
arena.total_allocated = functions::total_allocated;
|
||||
|
||||
|
@ -2825,7 +2823,7 @@ make(void* start, usize size)
|
|||
arena.total_allocated_count = 0;
|
||||
|
||||
arena.alloc = functions::alloc;
|
||||
arena.dealloc = functions::dealloc;
|
||||
arena.free = functions::free;
|
||||
arena.allocated_size = functions::allocated_size;
|
||||
arena.total_allocated = functions::total_allocated;
|
||||
|
||||
|
@ -2836,7 +2834,7 @@ void
|
|||
destroy(Arena_Allocator* arena)
|
||||
{
|
||||
if (arena->backing)
|
||||
dealloc(arena->backing, arena->physical_start);
|
||||
free(arena->backing, arena->physical_start);
|
||||
|
||||
GB_ASSERT(arena->temp_count == 0,
|
||||
"%ld Temporary_Arena_Memory have not be cleared", arena->temp_count);
|
||||
|
@ -2963,11 +2961,11 @@ alloc(Allocator* a, usize size, usize align)
|
|||
}
|
||||
|
||||
inline void
|
||||
dealloc(Allocator* a, void* ptr)
|
||||
free(Allocator* a, void* ptr)
|
||||
{
|
||||
GB_ASSERT(a != nullptr);
|
||||
if (ptr)
|
||||
a->dealloc(a, ptr);
|
||||
a->free(a, ptr);
|
||||
}
|
||||
|
||||
inline s64
|
||||
|
@ -3032,7 +3030,7 @@ free(String str)
|
|||
string::Header* h = string::header(str);
|
||||
|
||||
if (h->allocator)
|
||||
dealloc(h->allocator, h);
|
||||
free(h->allocator, h);
|
||||
}
|
||||
|
||||
inline String
|
||||
|
@ -3130,7 +3128,7 @@ string_realloc(Allocator* a, void* ptr, usize old_size, usize new_size)
|
|||
|
||||
memory::copy(ptr, old_size, new_ptr);
|
||||
|
||||
dealloc(a, ptr);
|
||||
free(a, ptr);
|
||||
|
||||
return new_ptr;
|
||||
}
|
||||
|
@ -3609,25 +3607,61 @@ crc64(const void* key, usize num_bytes)
|
|||
return ~result;
|
||||
}
|
||||
|
||||
// u32 fnv32(const void* key, usize num_bytes)
|
||||
// {
|
||||
inline u32
|
||||
fnv32(const void* key, usize num_bytes)
|
||||
{
|
||||
u32 h = 0x811c9dc5;
|
||||
const u8* buffer = static_cast<const u8*>(key);
|
||||
|
||||
// }
|
||||
for (usize i = 0; i < num_bytes; i++)
|
||||
{
|
||||
h = (h * 0x01000193) ^ buffer[i];
|
||||
}
|
||||
|
||||
// u64 fnv64(const void* key, usize num_bytes)
|
||||
// {
|
||||
return h;
|
||||
}
|
||||
|
||||
// }
|
||||
inline u64
|
||||
fnv64(const void* key, usize num_bytes)
|
||||
{
|
||||
u64 h = 0xcbf29ce484222325ull;
|
||||
const u8* buffer = static_cast<const u8*>(key);
|
||||
|
||||
// u32 fnv32a(const void* key, usize num_bytes)
|
||||
// {
|
||||
for (usize i = 0; i < num_bytes; i++)
|
||||
{
|
||||
h = (h * 0x100000001B3ll) ^ buffer[i];
|
||||
}
|
||||
|
||||
// }
|
||||
return h;
|
||||
}
|
||||
|
||||
// u64 fnv64a(const void* key, usize num_bytes)
|
||||
// {
|
||||
inline u32
|
||||
fnv32a(const void* key, usize num_bytes)
|
||||
{
|
||||
u32 h = 0x811c9dc5;
|
||||
const u8* buffer = static_cast<const u8*>(key);
|
||||
|
||||
// }
|
||||
for (usize i = 0; i < num_bytes; i++)
|
||||
{
|
||||
h = (h ^ buffer[i]) * 0x01000193;
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
inline u64
|
||||
fnv64a(const void* key, usize num_bytes)
|
||||
{
|
||||
u64 h = 0xcbf29ce484222325ull;
|
||||
const u8* buffer = static_cast<const u8*>(key);
|
||||
|
||||
for (usize i = 0; i < num_bytes; i++)
|
||||
{
|
||||
h = (h ^ buffer[i]) * 0x100000001B3ll;
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
u32
|
||||
murmur32(const void* key, u32 num_bytes, u32 seed)
|
||||
|
@ -3805,7 +3839,6 @@ const Time TIME_ZERO = time::seconds(0);
|
|||
namespace time
|
||||
{
|
||||
#if defined(GB_SYSTEM_WINDOWS)
|
||||
|
||||
internal_linkage LARGE_INTEGER
|
||||
win32_get_frequency()
|
||||
{
|
||||
|
@ -3896,6 +3929,7 @@ f32 as_seconds(Time t) { return static_cast<f32>(t.microseconds / 1000000.0
|
|||
s32 as_milliseconds(Time t) { return static_cast<s32>(t.microseconds / 1000l); }
|
||||
s64 as_microseconds(Time t) { return t.microseconds; }
|
||||
} // namespace time
|
||||
|
||||
bool operator==(Time left, Time right) { return left.microseconds == right.microseconds; }
|
||||
bool operator!=(Time left, Time right) { return !operator==(left, right); }
|
||||
|
||||
|
@ -3929,7 +3963,6 @@ f32 operator/(Time left, Time right) { return time::as_seconds(left) / time::as_
|
|||
Time& operator/=(Time& left, f32 right) { return (left = left / right); }
|
||||
Time& operator/=(Time& left, s64 right) { return (left = left / right); }
|
||||
|
||||
|
||||
Time operator%(Time left, Time right) { return time::microseconds(time::as_microseconds(left) % time::as_microseconds(right)); }
|
||||
Time& operator%=(Time& left, Time right) { return (left = left % right); }
|
||||
|
||||
|
@ -4131,6 +4164,7 @@ __GB_NAMESPACE_END
|
|||
|
||||
/*
|
||||
Version History:
|
||||
0.27 - Dealloc to Free & More Hashing Functions
|
||||
0.26a - Heap_Allocator Fix
|
||||
0.26 - Better Allocation system
|
||||
0.25a - Array bug fix
|
||||
|
|
Loading…
Reference in New Issue