Support custom memory allocators
This commit is contained in:
parent
bec467705e
commit
d797bb3e1c
30
src/external/jar_mod.h
vendored
30
src/external/jar_mod.h
vendored
@ -81,15 +81,13 @@
|
||||
#ifndef INCLUDE_JAR_MOD_H
|
||||
#define INCLUDE_JAR_MOD_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
//#include <stdbool.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
// Allow custom memory allocators
|
||||
#ifndef JARMOD_MALLOC
|
||||
#define JARMOD_MALLOC(sz) malloc(sz)
|
||||
#endif
|
||||
#ifndef JARMOD_FREE
|
||||
#define JARMOD_FREE(p) free(p)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// Basic type
|
||||
@ -240,7 +238,9 @@ typedef struct jar_mod_tracker_buffer_state_
|
||||
tracker_state * track_state_buf;
|
||||
}jar_mod_tracker_buffer_state;
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
bool jar_mod_init(jar_mod_context_t * modctx);
|
||||
bool jar_mod_setcfg(jar_mod_context_t * modctx, int samplerate, int bits, int stereo, int stereo_separation, int filter);
|
||||
@ -261,6 +261,10 @@ void jar_mod_seek_start(jar_mod_context_t * ctx);
|
||||
//-------------------------------------------------------------------------------
|
||||
#ifdef JAR_MOD_IMPLEMENTATION
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
//#include <stdbool.h>
|
||||
|
||||
// Effects list
|
||||
#define EFFECT_ARPEGGIO 0x0 // Supported
|
||||
#define EFFECT_PORTAMENTO_UP 0x1 // Supported
|
||||
@ -1504,7 +1508,7 @@ void jar_mod_unload( jar_mod_context_t * modctx)
|
||||
{
|
||||
if(modctx->modfile)
|
||||
{
|
||||
free(modctx->modfile);
|
||||
JARMOD_FREE(modctx->modfile);
|
||||
modctx->modfile = 0;
|
||||
modctx->modfilesize = 0;
|
||||
modctx->loopcount = 0;
|
||||
@ -1513,14 +1517,12 @@ void jar_mod_unload( jar_mod_context_t * modctx)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
mulong jar_mod_load_file(jar_mod_context_t * modctx, const char* filename)
|
||||
{
|
||||
mulong fsize = 0;
|
||||
if(modctx->modfile)
|
||||
{
|
||||
free(modctx->modfile);
|
||||
JARMOD_FREE(modctx->modfile);
|
||||
modctx->modfile = 0;
|
||||
}
|
||||
|
||||
@ -1533,7 +1535,7 @@ mulong jar_mod_load_file(jar_mod_context_t * modctx, const char* filename)
|
||||
|
||||
if(fsize && fsize < 32*1024*1024)
|
||||
{
|
||||
modctx->modfile = malloc(fsize);
|
||||
modctx->modfile = JARMOD_MALLOC(fsize);
|
||||
modctx->modfilesize = fsize;
|
||||
memset(modctx->modfile, 0, fsize);
|
||||
fread(modctx->modfile, fsize, 1, f);
|
||||
|
50
src/external/jar_xm.h
vendored
50
src/external/jar_xm.h
vendored
@ -56,22 +56,22 @@
|
||||
#define JAR_XM_DEFENSIVE 1
|
||||
#define JAR_XM_RAMPING 1
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// Allow custom memory allocators
|
||||
#ifndef JARXM_MALLOC
|
||||
#define JARXM_MALLOC(sz) malloc(sz)
|
||||
#endif
|
||||
#ifndef JARXM_FREE
|
||||
#define JARXM_FREE(p) free(p)
|
||||
#endif
|
||||
|
||||
//-------------------------------------------------------------------------------
|
||||
struct jar_xm_context_s;
|
||||
typedef struct jar_xm_context_s jar_xm_context_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct jar_xm_context_s;
|
||||
typedef struct jar_xm_context_s jar_xm_context_t;
|
||||
|
||||
/** Create a XM context.
|
||||
*
|
||||
* @param moddata the contents of the module
|
||||
@ -133,7 +133,7 @@ void jar_xm_generate_samples(jar_xm_context_t* ctx, float* output, size_t numsam
|
||||
*/
|
||||
void jar_xm_generate_samples_16bit(jar_xm_context_t* ctx, short* output, size_t numsamples)
|
||||
{
|
||||
float* musicBuffer = malloc((2*numsamples)*sizeof(float));
|
||||
float* musicBuffer = JARXM_MALLOC((2*numsamples)*sizeof(float));
|
||||
jar_xm_generate_samples(ctx, musicBuffer, numsamples);
|
||||
|
||||
if(output){
|
||||
@ -142,7 +142,7 @@ void jar_xm_generate_samples_16bit(jar_xm_context_t* ctx, short* output, size_t
|
||||
output[x] = musicBuffer[x] * SHRT_MAX;
|
||||
}
|
||||
|
||||
free(musicBuffer);
|
||||
JARXM_FREE(musicBuffer);
|
||||
}
|
||||
|
||||
/** Play the module, resample from 32 bit to 8 bit, and put the sound samples in an output buffer.
|
||||
@ -152,7 +152,7 @@ void jar_xm_generate_samples_16bit(jar_xm_context_t* ctx, short* output, size_t
|
||||
*/
|
||||
void jar_xm_generate_samples_8bit(jar_xm_context_t* ctx, char* output, size_t numsamples)
|
||||
{
|
||||
float* musicBuffer = malloc((2*numsamples)*sizeof(float));
|
||||
float* musicBuffer = JARXM_MALLOC((2*numsamples)*sizeof(float));
|
||||
jar_xm_generate_samples(ctx, musicBuffer, numsamples);
|
||||
|
||||
if(output){
|
||||
@ -161,7 +161,7 @@ void jar_xm_generate_samples_8bit(jar_xm_context_t* ctx, char* output, size_t nu
|
||||
output[x] = musicBuffer[x] * CHAR_MAX;
|
||||
}
|
||||
|
||||
free(musicBuffer);
|
||||
JARXM_FREE(musicBuffer);
|
||||
}
|
||||
|
||||
|
||||
@ -306,6 +306,10 @@ uint64_t jar_xm_get_remaining_samples(jar_xm_context_t* ctx);
|
||||
#ifdef JAR_XM_IMPLEMENTATION
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#if JAR_XM_DEBUG //JAR_XM_DEBUG defined as 0
|
||||
@ -630,10 +634,10 @@ int jar_xm_create_context_safe(jar_xm_context_t** ctxp, const char* moddata, siz
|
||||
#endif
|
||||
|
||||
bytes_needed = jar_xm_get_memory_needed_for_context(moddata, moddata_length);
|
||||
mempool = malloc(bytes_needed);
|
||||
mempool = JARXM_MALLOC(bytes_needed);
|
||||
if(mempool == NULL && bytes_needed > 0) {
|
||||
/* malloc() failed, trouble ahead */
|
||||
DEBUG("call to malloc() failed, returned %p", (void*)mempool);
|
||||
/* JARXM_MALLOC() failed, trouble ahead */
|
||||
DEBUG("call to JARXM_MALLOC() failed, returned %p", (void*)mempool);
|
||||
return 2;
|
||||
}
|
||||
|
||||
@ -641,7 +645,7 @@ int jar_xm_create_context_safe(jar_xm_context_t** ctxp, const char* moddata, siz
|
||||
memset(mempool, 0, bytes_needed);
|
||||
|
||||
ctx = (*ctxp = (jar_xm_context_t *)mempool);
|
||||
ctx->allocated_memory = mempool; /* Keep original pointer for free() */
|
||||
ctx->allocated_memory = mempool; /* Keep original pointer for JARXM_FREE() */
|
||||
mempool += sizeof(jar_xm_context_t);
|
||||
|
||||
ctx->rate = rate;
|
||||
@ -691,7 +695,7 @@ int jar_xm_create_context_safe(jar_xm_context_t** ctxp, const char* moddata, siz
|
||||
}
|
||||
|
||||
void jar_xm_free_context(jar_xm_context_t* ctx) {
|
||||
free(ctx->allocated_memory);
|
||||
JARXM_FREE(ctx->allocated_memory);
|
||||
}
|
||||
|
||||
void jar_xm_set_max_loop_count(jar_xm_context_t* ctx, uint8_t loopcnt) {
|
||||
@ -2620,11 +2624,11 @@ int jar_xm_create_context_from_file(jar_xm_context_t** ctx, uint32_t rate, const
|
||||
return 4;
|
||||
}
|
||||
|
||||
char* data = malloc(size + 1);
|
||||
char* data = JARXM_MALLOC(size + 1);
|
||||
if(!data || fread(data, 1, size, xmf) < size) {
|
||||
fclose(xmf);
|
||||
DEBUG_ERR(data ? "fread() failed" : "malloc() failed");
|
||||
free(data);
|
||||
DEBUG_ERR(data ? "fread() failed" : "JARXM_MALLOC() failed");
|
||||
JARXM_FREE(data);
|
||||
*ctx = NULL;
|
||||
return 5;
|
||||
}
|
||||
@ -2632,7 +2636,7 @@ int jar_xm_create_context_from_file(jar_xm_context_t** ctx, uint32_t rate, const
|
||||
fclose(xmf);
|
||||
|
||||
ret = jar_xm_create_context_safe(ctx, data, size, rate);
|
||||
free(data);
|
||||
JARXM_FREE(data);
|
||||
|
||||
switch(ret) {
|
||||
case 0:
|
||||
|
Loading…
Reference in New Issue
Block a user