rockbox/lib/rbcodec/codecs/libtremor/oggmalloc.c
Aidan MacDonald 7eeb4e4302 firmware: refactor CACHEALIGN_BITS/SIZE defines
Mostly motivated by PP needing CACHEALIGN_SIZE in linker
scripts, which can't include system.h, so move these to
cpu.h instead. Also gets rid of the default 32 byte line
size that was used if the target didn't define alignment
itself. RK24xx, DM320, and JZ4740 were missing this but
have been confirmed (from datasheets) to use 32-byte cache
lines.

Add checks to make sure the macros are appropriately
(un)defined based on the HAVE_CPU_CACHE_ALIGN define,
and make sure their values are consistent when they
are defined.

Disable HAVE_CPU_CACHE_ALIGN for hosted targets since it
arguably doesn't matter if there's a cache, if we aren't
responsible for cache maintenance.

A few files in rbcodec use CACHEALIGN_SIZE, but these
can be converted to MEM_ALIGN_SIZE, which is identical
to CACHEALIGN_SIZE if the latter is defined. On other
targets, it aligns to at least sizeof(intptr_t).

Change-Id: If8cf8f6ec327dc3732f4cd5022a858546b9e63d6
2026-03-06 14:49:00 +00:00

86 lines
1.5 KiB
C

#include "os_types.h"
#include <tlsf.h>
#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
#include <setjmp.h>
extern jmp_buf rb_jump_buf;
#define LONGJMP(x) longjmp(rb_jump_buf, x)
#elif defined(SIMULATOR)
#define LONGJMP(x) do { DEBUGF("Vorbis: allocation failed!\n"); return NULL; } while (false)
#else
#define LONGJMP(x) return NULL
#endif
void ogg_malloc_init(void)
{
size_t bufsize;
void* buf = ci->codec_get_buffer(&bufsize);
init_memory_pool(bufsize, buf);
}
void ogg_malloc_destroy()
{
size_t bufsize;
void* buf = ci->codec_get_buffer(&bufsize);
destroy_memory_pool(buf);
}
void *ogg_malloc(size_t size)
{
void* x = tlsf_malloc(size);
if (x == NULL)
LONGJMP(1);
return x;
}
void *ogg_calloc(size_t nmemb, size_t size)
{
void *x = tlsf_calloc(nmemb, size);
if (x == NULL)
LONGJMP(1);
return x;
}
void *ogg_realloc(void *ptr, size_t size)
{
void *x = tlsf_realloc(ptr, size);
if (x == NULL)
LONGJMP(1);
return x;
}
void ogg_free(void* ptr)
{
tlsf_free(ptr);
}
#ifdef TREMOR_USE_IRAM
/* Allocate IRAM buffer */
static unsigned char iram_buff[IRAM_IBSS_SIZE] IBSS_ATTR MEM_ALIGN_ATTR;
static size_t iram_remain;
void iram_malloc_init(void){
iram_remain=IRAM_IBSS_SIZE;
}
void *iram_malloc(size_t size){
void* x;
/* align for best performance */
size = MEM_ALIGN_UP(size);
if(size>iram_remain)
return NULL;
x = &iram_buff[IRAM_IBSS_SIZE-iram_remain];
iram_remain-=size;
return x;
}
#endif