mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-17 11:35:08 -04:00
use libnx code for aligned alloc
This commit is contained in:
parent
5fdad50316
commit
ed3c0ff23c
60
src/Audio.c
60
src/Audio.c
@ -942,62 +942,6 @@ static struct AudioMemPools audioPools[64];
|
||||
AudioDriver drv;
|
||||
bool switchAudio = false;
|
||||
|
||||
|
||||
// implementations of malloc_aligned and free_aligned
|
||||
// https://stackoverflow.com/questions/6563120/what-does-posix-memalign-memalign-do
|
||||
static void *malloc_aligned(size_t alignment, size_t bytes)
|
||||
{
|
||||
// we need to allocate enough storage for the requested bytes, some
|
||||
// book-keeping (to store the location returned by malloc) and some extra
|
||||
// padding to allow us to find an aligned byte. im not entirely sure if
|
||||
// 2 * alignment is enough here, its just a guess.
|
||||
const size_t total_size = bytes + (2 * alignment) + sizeof(size_t);
|
||||
|
||||
char *data = malloc(sizeof(char) * total_size);
|
||||
|
||||
if (data)
|
||||
{
|
||||
// store the original start of the malloc'd data.
|
||||
const void * const data_start = data;
|
||||
|
||||
// dedicate enough space to the book-keeping.
|
||||
data += sizeof(size_t);
|
||||
|
||||
// find a memory location with correct alignment. the alignment minus
|
||||
// the remainder of this mod operation is how many bytes forward we need
|
||||
// to move to find an aligned byte.
|
||||
const size_t offset = alignment - (((size_t)data) % alignment);
|
||||
|
||||
// set data to the aligned memory.
|
||||
data += offset;
|
||||
|
||||
// write the book-keeping.
|
||||
size_t *book_keeping = (size_t*)(data - sizeof(size_t));
|
||||
*book_keeping = (size_t)data_start;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
static void free_aligned(void *raw_data)
|
||||
{
|
||||
if (raw_data)
|
||||
{
|
||||
char *data = raw_data;
|
||||
|
||||
// we have to assume this memory was allocated with malloc_aligned.
|
||||
// this means the sizeof(size_t) bytes before data are the book-keeping
|
||||
// which points to the location we need to pass to free.
|
||||
data -= sizeof(size_t);
|
||||
|
||||
// set data to the location stored in book-keeping.
|
||||
data = (char*)(*((size_t*)data));
|
||||
|
||||
// free the memory.
|
||||
free(data);
|
||||
}
|
||||
}
|
||||
|
||||
static cc_bool AudioBackend_Init(void) {
|
||||
if (switchAudio) return true;
|
||||
switchAudio = true;
|
||||
@ -1154,7 +1098,7 @@ cc_bool Audio_DescribeError(cc_result res, cc_string* dst) {
|
||||
|
||||
void Audio_AllocChunks(cc_uint32 size, void** chunks, int numChunks) {
|
||||
size = (size + 0xFFF) & ~0xFFF; // round up to nearest multiple of 0x1000
|
||||
void* dst = malloc_aligned(0x1000, size * numChunks);
|
||||
void* dst = aligned_alloc(0x1000, size * numChunks);
|
||||
armDCacheFlush(dst, size * numChunks);
|
||||
|
||||
for (int i = 0; i < numChunks; i++) {
|
||||
@ -1185,7 +1129,7 @@ void Audio_FreeChunks(void** chunks, int numChunks) {
|
||||
}
|
||||
}
|
||||
|
||||
free_aligned(chunks[0]);
|
||||
free(chunks[0]);
|
||||
}
|
||||
#elif defined CC_BUILD_DREAMCAST
|
||||
/*########################################################################################################################*
|
||||
|
Loading…
x
Reference in New Issue
Block a user