From 19374bc1a41a5b4c5e8970a36c38a1af6574048b Mon Sep 17 00:00:00 2001 From: Hayden Kowalchuk <819028+mrneo240@users.noreply.github.com> Date: Thu, 5 Mar 2020 14:44:09 -0500 Subject: [PATCH] chore: change containers - move some math to integer - inline some smaller functions - whitespace fixes --- containers/aligned_vector.c | 22 ++++------------------ containers/aligned_vector.h | 17 ++++++++++++++--- containers/named_array.c | 15 ++------------- containers/named_array.h | 12 +++++++++++- 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/containers/aligned_vector.c b/containers/aligned_vector.c index 55ddc7e..1bac8a5 100644 --- a/containers/aligned_vector.c +++ b/containers/aligned_vector.c @@ -13,6 +13,7 @@ static inline void* memalign(size_t alignment, size_t size) { #include #endif +#include "../GL/private.h" #include "aligned_vector.h" void aligned_vector_init(AlignedVector* vector, unsigned int element_size) { @@ -24,7 +25,6 @@ void aligned_vector_init(AlignedVector* vector, unsigned int element_size) { aligned_vector_reserve(vector, ALIGNED_VECTOR_CHUNK_SIZE); } - static inline unsigned int round_to_chunk_size(unsigned int val) { const unsigned int n = val; const unsigned int m = ALIGNED_VECTOR_CHUNK_SIZE; @@ -32,7 +32,6 @@ static inline unsigned int round_to_chunk_size(unsigned int val) { return ((n + m - 1) / m) * m; } - void aligned_vector_reserve(AlignedVector* vector, unsigned int element_count) { if(element_count == 0) { return; @@ -54,7 +53,7 @@ void aligned_vector_reserve(AlignedVector* vector, unsigned int element_count) { assert(vector->data); if(original_data) { - memcpy(vector->data, original_data, original_byte_size); + FASTCPY(vector->data, original_data, original_byte_size); free(original_data); } @@ -74,7 +73,7 @@ void* aligned_vector_push_back(AlignedVector* vector, const void* objs, unsigned unsigned char* dest = vector->data + (vector->element_size * initial_size); /* Copy the objects in */ - memcpy(dest, objs, vector->element_size * count); + FASTCPY(dest, objs, vector->element_size * count); return dest; } @@ -101,25 +100,12 @@ void* aligned_vector_resize(AlignedVector* vector, const unsigned int element_co } } -void* aligned_vector_at(const AlignedVector* vector, const unsigned int index) { - assert(index < vector->size); - return &vector->data[index * vector->element_size]; -} - -void* aligned_vector_back(AlignedVector* vector) { - return aligned_vector_at(vector, vector->size - 1); -} - void* aligned_vector_extend(AlignedVector* vector, const unsigned int additional_count) { const unsigned int current = vector->size; aligned_vector_resize(vector, vector->size + additional_count); return aligned_vector_at(vector, current); } -void aligned_vector_clear(AlignedVector* vector) { - vector->size = 0; -} - void aligned_vector_shrink_to_fit(AlignedVector* vector) { if(vector->size == 0) { free(vector->data); @@ -131,7 +117,7 @@ void aligned_vector_shrink_to_fit(AlignedVector* vector) { vector->data = (unsigned char*) memalign(0x20, new_byte_size); if(original_data) { - memcpy(vector->data, original_data, new_byte_size); + FASTCPY(vector->data, original_data, new_byte_size); free(original_data); } diff --git a/containers/aligned_vector.h b/containers/aligned_vector.h index a002ece..dbbcda4 100644 --- a/containers/aligned_vector.h +++ b/containers/aligned_vector.h @@ -1,3 +1,5 @@ +#pragma once + #ifndef ALIGNED_VECTOR_H #define ALIGNED_VECTOR_H @@ -5,6 +7,8 @@ extern "C" { #endif +#include + typedef struct { unsigned int size; unsigned int capacity; @@ -18,12 +22,19 @@ void aligned_vector_init(AlignedVector* vector, unsigned int element_size); void aligned_vector_reserve(AlignedVector* vector, unsigned int element_count); void* aligned_vector_push_back(AlignedVector* vector, const void* objs, unsigned int count); void* aligned_vector_resize(AlignedVector* vector, const unsigned int element_count); -void* aligned_vector_at(const AlignedVector* vector, const unsigned int index); +static inline void* aligned_vector_at(const AlignedVector* vector, const unsigned int index) { + assert(index < vector->size); + return &vector->data[index * vector->element_size]; +} void* aligned_vector_extend(AlignedVector* vector, const unsigned int additional_count); -void aligned_vector_clear(AlignedVector* vector); +static inline void aligned_vector_clear(AlignedVector* vector){ + vector->size = 0; +} void aligned_vector_shrink_to_fit(AlignedVector* vector); void aligned_vector_cleanup(AlignedVector* vector); -void* aligned_vector_back(AlignedVector* vector); +static inline void* aligned_vector_back(AlignedVector* vector){ + return aligned_vector_at(vector, vector->size - 1); +} #ifdef __cplusplus } diff --git a/containers/named_array.c b/containers/named_array.c index a0714bf..1bf06c9 100644 --- a/containers/named_array.c +++ b/containers/named_array.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -16,8 +17,7 @@ void named_array_init(NamedArray* array, unsigned int element_size, unsigned int array->element_size = element_size; array->max_element_count = max_elements; - float c = (float) max_elements / 8.0f; - array->marker_count = (unsigned char) ceil(c); + array->marker_count = (unsigned char)((max_elements+8-1)/8); #ifdef _arch_dreamcast // Use 32-bit aligned memory on the Dreamcast @@ -30,17 +30,6 @@ void named_array_init(NamedArray* array, unsigned int element_size, unsigned int memset(array->used_markers, 0, sizeof(unsigned char) * array->marker_count); } -char named_array_used(NamedArray* array, unsigned int id) { - unsigned int i = id / 8; - unsigned int j = id % 8; - - assert(i < array->max_element_count); - assert(array->used_markers); - - unsigned char v = array->used_markers[i] & (unsigned char) (1 << j); - return !!(v); -} - void* named_array_alloc(NamedArray* array, unsigned int* new_id) { unsigned int i = 0, j = 0; for(i = 0; i < array->marker_count; ++i) { diff --git a/containers/named_array.h b/containers/named_array.h index a0f6c97..9bd6c50 100644 --- a/containers/named_array.h +++ b/containers/named_array.h @@ -1,3 +1,5 @@ +#pragma once + #ifndef NAMED_ARRAY_H #define NAMED_ARRAY_H @@ -5,6 +7,8 @@ extern "C" { #endif +#include "../GL/private.h" + typedef struct { unsigned int element_size; unsigned int max_element_count; @@ -14,7 +18,13 @@ typedef struct { } NamedArray; void named_array_init(NamedArray* array, unsigned int element_size, unsigned int max_elements); -char named_array_used(NamedArray* array, unsigned int id); +static inline char named_array_used(NamedArray* array, unsigned int id) { + const unsigned int i = id / 8; + const unsigned int j = id % 8; + + unsigned char v = array->used_markers[i] & (unsigned char) (1 << j); + return !!(v); +} void* named_array_alloc(NamedArray* array, unsigned int* new_id); void* named_array_reserve(NamedArray* array, unsigned int id);