GLdc/containers/aligned_vector.c

52 lines
1.6 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <string.h>
#include <math.h>
2019-03-06 08:31:06 +00:00
#include <assert.h>
2019-03-28 13:08:23 +00:00
#include <stdio.h>
2020-03-18 20:28:15 +00:00
#ifdef _arch_dreamcast
#include "../GL/private.h"
2020-03-18 20:28:15 +00:00
#else
#define FASTCPY memcpy
#endif
#include "aligned_vector.h"
2021-09-12 14:04:52 +00:00
extern inline void* aligned_vector_resize(AlignedVector* vector, const unsigned int element_count);
extern inline void* aligned_vector_extend(AlignedVector* vector, const unsigned int additional_count);
extern inline void* aligned_vector_reserve(AlignedVector* vector, unsigned int element_count);
extern inline void* aligned_vector_push_back(AlignedVector* vector, const void* objs, unsigned int count);
void aligned_vector_init(AlignedVector* vector, unsigned int element_size) {
vector->size = vector->capacity = 0;
vector->element_size = element_size;
vector->data = NULL;
/* Reserve some initial capacity */
2019-03-13 07:28:23 +00:00
aligned_vector_reserve(vector, ALIGNED_VECTOR_CHUNK_SIZE);
}
void aligned_vector_shrink_to_fit(AlignedVector* vector) {
if(vector->size == 0) {
free(vector->data);
vector->data = NULL;
vector->capacity = 0;
} else {
unsigned int new_byte_size = vector->size * vector->element_size;
unsigned char* original_data = vector->data;
vector->data = (unsigned char*) memalign(0x20, new_byte_size);
if(original_data) {
FASTCPY(vector->data, original_data, new_byte_size);
free(original_data);
}
vector->capacity = vector->size;
}
}
void aligned_vector_cleanup(AlignedVector* vector) {
aligned_vector_clear(vector);
aligned_vector_shrink_to_fit(vector);
}