chore: change containers

- move some math to integer
- inline some smaller functions
- whitespace fixes
This commit is contained in:
Hayden Kowalchuk 2020-03-05 14:44:09 -05:00
parent 1d1a980f16
commit 19374bc1a4
4 changed files with 31 additions and 35 deletions

View File

@ -13,6 +13,7 @@ static inline void* memalign(size_t alignment, size_t size) {
#include <malloc.h> #include <malloc.h>
#endif #endif
#include "../GL/private.h"
#include "aligned_vector.h" #include "aligned_vector.h"
void aligned_vector_init(AlignedVector* vector, unsigned int element_size) { 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); aligned_vector_reserve(vector, ALIGNED_VECTOR_CHUNK_SIZE);
} }
static inline unsigned int round_to_chunk_size(unsigned int val) { static inline unsigned int round_to_chunk_size(unsigned int val) {
const unsigned int n = val; const unsigned int n = val;
const unsigned int m = ALIGNED_VECTOR_CHUNK_SIZE; 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; return ((n + m - 1) / m) * m;
} }
void aligned_vector_reserve(AlignedVector* vector, unsigned int element_count) { void aligned_vector_reserve(AlignedVector* vector, unsigned int element_count) {
if(element_count == 0) { if(element_count == 0) {
return; return;
@ -54,7 +53,7 @@ void aligned_vector_reserve(AlignedVector* vector, unsigned int element_count) {
assert(vector->data); assert(vector->data);
if(original_data) { if(original_data) {
memcpy(vector->data, original_data, original_byte_size); FASTCPY(vector->data, original_data, original_byte_size);
free(original_data); 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); unsigned char* dest = vector->data + (vector->element_size * initial_size);
/* Copy the objects in */ /* Copy the objects in */
memcpy(dest, objs, vector->element_size * count); FASTCPY(dest, objs, vector->element_size * count);
return dest; 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) { void* aligned_vector_extend(AlignedVector* vector, const unsigned int additional_count) {
const unsigned int current = vector->size; const unsigned int current = vector->size;
aligned_vector_resize(vector, vector->size + additional_count); aligned_vector_resize(vector, vector->size + additional_count);
return aligned_vector_at(vector, current); return aligned_vector_at(vector, current);
} }
void aligned_vector_clear(AlignedVector* vector) {
vector->size = 0;
}
void aligned_vector_shrink_to_fit(AlignedVector* vector) { void aligned_vector_shrink_to_fit(AlignedVector* vector) {
if(vector->size == 0) { if(vector->size == 0) {
free(vector->data); free(vector->data);
@ -131,7 +117,7 @@ void aligned_vector_shrink_to_fit(AlignedVector* vector) {
vector->data = (unsigned char*) memalign(0x20, new_byte_size); vector->data = (unsigned char*) memalign(0x20, new_byte_size);
if(original_data) { if(original_data) {
memcpy(vector->data, original_data, new_byte_size); FASTCPY(vector->data, original_data, new_byte_size);
free(original_data); free(original_data);
} }

View File

@ -1,3 +1,5 @@
#pragma once
#ifndef ALIGNED_VECTOR_H #ifndef ALIGNED_VECTOR_H
#define ALIGNED_VECTOR_H #define ALIGNED_VECTOR_H
@ -5,6 +7,8 @@
extern "C" { extern "C" {
#endif #endif
#include <assert.h>
typedef struct { typedef struct {
unsigned int size; unsigned int size;
unsigned int capacity; 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_reserve(AlignedVector* vector, unsigned int element_count);
void* aligned_vector_push_back(AlignedVector* vector, const void* objs, unsigned int 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_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_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_shrink_to_fit(AlignedVector* vector);
void aligned_vector_cleanup(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 #ifdef __cplusplus
} }

View File

@ -1,4 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
@ -16,8 +17,7 @@ void named_array_init(NamedArray* array, unsigned int element_size, unsigned int
array->element_size = element_size; array->element_size = element_size;
array->max_element_count = max_elements; array->max_element_count = max_elements;
float c = (float) max_elements / 8.0f; array->marker_count = (unsigned char)((max_elements+8-1)/8);
array->marker_count = (unsigned char) ceil(c);
#ifdef _arch_dreamcast #ifdef _arch_dreamcast
// Use 32-bit aligned memory on the 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); 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) { void* named_array_alloc(NamedArray* array, unsigned int* new_id) {
unsigned int i = 0, j = 0; unsigned int i = 0, j = 0;
for(i = 0; i < array->marker_count; ++i) { for(i = 0; i < array->marker_count; ++i) {

View File

@ -1,3 +1,5 @@
#pragma once
#ifndef NAMED_ARRAY_H #ifndef NAMED_ARRAY_H
#define NAMED_ARRAY_H #define NAMED_ARRAY_H
@ -5,6 +7,8 @@
extern "C" { extern "C" {
#endif #endif
#include "../GL/private.h"
typedef struct { typedef struct {
unsigned int element_size; unsigned int element_size;
unsigned int max_element_count; unsigned int max_element_count;
@ -14,7 +18,13 @@ typedef struct {
} NamedArray; } NamedArray;
void named_array_init(NamedArray* array, unsigned int element_size, unsigned int max_elements); 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_alloc(NamedArray* array, unsigned int* new_id);
void* named_array_reserve(NamedArray* array, unsigned int id); void* named_array_reserve(NamedArray* array, unsigned int id);