2018-05-26 18:17:42 +00:00
|
|
|
#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>
|
2018-05-26 18:17:42 +00:00
|
|
|
|
2020-03-18 20:28:15 +00:00
|
|
|
#ifdef _arch_dreamcast
|
2020-03-05 19:44:09 +00:00
|
|
|
#include "../GL/private.h"
|
2020-03-18 20:28:15 +00:00
|
|
|
#else
|
|
|
|
#define FASTCPY memcpy
|
|
|
|
#endif
|
|
|
|
|
2018-05-26 18:17:42 +00:00
|
|
|
#include "aligned_vector.h"
|
|
|
|
|
2023-05-12 19:51:36 +00:00
|
|
|
extern inline void* aligned_vector_resize(AlignedVector* vector, const uint32_t element_count);
|
|
|
|
extern inline void* aligned_vector_extend(AlignedVector* vector, const uint32_t additional_count);
|
|
|
|
extern inline void* aligned_vector_reserve(AlignedVector* vector, uint32_t element_count);
|
|
|
|
extern inline void* aligned_vector_push_back(AlignedVector* vector, const void* objs, uint32_t count);
|
|
|
|
|
|
|
|
void aligned_vector_init(AlignedVector* vector, uint32_t element_size) {
|
|
|
|
/* Now initialize the header*/
|
|
|
|
AlignedVectorHeader* const hdr = &vector->hdr;
|
|
|
|
hdr->size = 0;
|
|
|
|
hdr->capacity = ALIGNED_VECTOR_CHUNK_SIZE;
|
|
|
|
hdr->element_size = element_size;
|
|
|
|
|
|
|
|
/* Reserve some initial capacity. This will do the allocation but not set up the header */
|
|
|
|
void* ptr = aligned_vector_reserve(vector, ALIGNED_VECTOR_CHUNK_SIZE);
|
|
|
|
assert(ptr);
|
|
|
|
(void) ptr;
|
2018-05-26 18:17:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void aligned_vector_shrink_to_fit(AlignedVector* vector) {
|
2023-05-12 19:51:36 +00:00
|
|
|
AlignedVectorHeader* const hdr = &vector->hdr;
|
|
|
|
if(hdr->size == 0) {
|
|
|
|
uint32_t element_size = hdr->element_size;
|
2018-05-26 18:17:42 +00:00
|
|
|
free(vector->data);
|
2023-05-12 19:51:36 +00:00
|
|
|
|
|
|
|
/* Reallocate the header */
|
|
|
|
vector->data = memalign(0x20, sizeof(AlignedVectorHeader));
|
|
|
|
hdr->size = hdr->capacity = 0;
|
|
|
|
hdr->element_size = element_size;
|
2018-05-26 18:17:42 +00:00
|
|
|
} else {
|
2023-05-12 19:51:36 +00:00
|
|
|
uint32_t new_byte_size = (hdr->size * hdr->element_size);
|
|
|
|
uint8_t* original_data = vector->data;
|
2018-05-26 18:17:42 +00:00
|
|
|
vector->data = (unsigned char*) memalign(0x20, new_byte_size);
|
|
|
|
|
|
|
|
if(original_data) {
|
2020-03-05 19:44:09 +00:00
|
|
|
FASTCPY(vector->data, original_data, new_byte_size);
|
2018-05-26 18:17:42 +00:00
|
|
|
free(original_data);
|
|
|
|
}
|
2023-05-12 19:51:36 +00:00
|
|
|
hdr->capacity = hdr->size;
|
2018-05-26 18:17:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void aligned_vector_cleanup(AlignedVector* vector) {
|
|
|
|
aligned_vector_clear(vector);
|
|
|
|
aligned_vector_shrink_to_fit(vector);
|
|
|
|
}
|