Support custom memory management macros

Users can define their custom memory management macros.

NOTE: Most external libraries support custom macros in the same way, raylib should redefine those macros to raylib ones, to unify custom memory loading. That redefinition is only implemented as example for stb_image.h in [textures] module.
This commit is contained in:
Ray 2019-04-23 14:55:35 +02:00
parent 8ed71b9d5a
commit e67ebabb02
9 changed files with 364 additions and 338 deletions

View file

@ -567,7 +567,7 @@ void SetMasterVolume(float volume)
// Create a new audio buffer. Initially filled with silence
AudioBuffer *CreateAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 bufferSizeInFrames, AudioBufferUsage usage)
{
AudioBuffer *audioBuffer = (AudioBuffer *)calloc(sizeof(*audioBuffer) + (bufferSizeInFrames*channels*ma_get_bytes_per_sample(format)), 1);
AudioBuffer *audioBuffer = (AudioBuffer *)RL_CALLOC(sizeof(*audioBuffer) + (bufferSizeInFrames*channels*ma_get_bytes_per_sample(format)), 1);
if (audioBuffer == NULL)
{
TraceLog(LOG_ERROR, "CreateAudioBuffer() : Failed to allocate memory for audio buffer");
@ -591,7 +591,7 @@ AudioBuffer *CreateAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 s
if (result != MA_SUCCESS)
{
TraceLog(LOG_ERROR, "CreateAudioBuffer() : Failed to create data conversion pipeline");
free(audioBuffer);
RL_FREE(audioBuffer);
return NULL;
}
@ -623,7 +623,7 @@ void DeleteAudioBuffer(AudioBuffer *audioBuffer)
}
UntrackAudioBuffer(audioBuffer);
free(audioBuffer);
RL_FREE(audioBuffer);
}
// Check if an audio buffer is playing
@ -863,7 +863,7 @@ Sound LoadSoundFromWave(Wave wave)
// Unload wave data
void UnloadWave(Wave wave)
{
if (wave.data != NULL) free(wave.data);
if (wave.data != NULL) RL_FREE(wave.data);
TraceLog(LOG_INFO, "Unloaded wave data from RAM");
}
@ -1017,7 +1017,7 @@ void WaveFormat(Wave *wave, int sampleRate, int sampleSize, int channels)
return;
}
void *data = malloc(frameCount*channels*(sampleSize/8));
void *data = RL_MALLOC(frameCount*channels*(sampleSize/8));
frameCount = (ma_uint32)ma_convert_frames(data, formatOut, channels, sampleRate, wave->data, formatIn, wave->channels, wave->sampleRate, frameCountIn);
if (frameCount == 0)
@ -1030,7 +1030,7 @@ void WaveFormat(Wave *wave, int sampleRate, int sampleSize, int channels)
wave->sampleSize = sampleSize;
wave->sampleRate = sampleRate;
wave->channels = channels;
free(wave->data);
RL_FREE(wave->data);
wave->data = data;
}
@ -1039,7 +1039,7 @@ Wave WaveCopy(Wave wave)
{
Wave newWave = { 0 };
newWave.data = malloc(wave.sampleCount*wave.sampleSize/8*wave.channels);
newWave.data = RL_MALLOC(wave.sampleCount*wave.sampleSize/8*wave.channels);
if (newWave.data != NULL)
{
@ -1064,11 +1064,11 @@ void WaveCrop(Wave *wave, int initSample, int finalSample)
{
int sampleCount = finalSample - initSample;
void *data = malloc(sampleCount*wave->sampleSize/8*wave->channels);
void *data = RL_MALLOC(sampleCount*wave->sampleSize/8*wave->channels);
memcpy(data, (unsigned char *)wave->data + (initSample*wave->channels*wave->sampleSize/8), sampleCount*wave->channels*wave->sampleSize/8);
free(wave->data);
RL_FREE(wave->data);
wave->data = data;
}
else TraceLog(LOG_WARNING, "Wave crop range out of bounds");
@ -1078,7 +1078,7 @@ void WaveCrop(Wave *wave, int initSample, int finalSample)
// NOTE: Returned sample values are normalized to range [-1..1]
float *GetWaveData(Wave wave)
{
float *samples = (float *)malloc(wave.sampleCount*wave.channels*sizeof(float));
float *samples = (float *)RL_MALLOC(wave.sampleCount*wave.channels*sizeof(float));
for (unsigned int i = 0; i < wave.sampleCount; i++)
{
@ -1100,7 +1100,7 @@ float *GetWaveData(Wave wave)
// Load music stream from file
Music LoadMusicStream(const char *fileName)
{
Music music = (MusicData *)malloc(sizeof(MusicData));
Music music = (MusicData *)RL_MALLOC(sizeof(MusicData));
bool musicLoaded = true;
#if defined(SUPPORT_FILEFORMAT_OGG)
@ -1228,7 +1228,7 @@ Music LoadMusicStream(const char *fileName)
if (false) {}
#endif
#if defined(SUPPORT_FILEFORMAT_FLAC)
else if (music->ctxType == MUSIC_AUDIO_FLAC) drflac_free(music->ctxFlac);
else if (music->ctxType == MUSIC_AUDIO_FLAC) drflac_RL_FREE(music->ctxFlac);
#endif
#if defined(SUPPORT_FILEFORMAT_MP3)
else if (music->ctxType == MUSIC_AUDIO_MP3) drmp3_uninit(&music->ctxMp3);
@ -1240,7 +1240,7 @@ Music LoadMusicStream(const char *fileName)
else if (music->ctxType == MUSIC_MODULE_MOD) jar_mod_unload(&music->ctxMod);
#endif
free(music);
RL_FREE(music);
music = NULL;
TraceLog(LOG_WARNING, "[%s] Music file could not be opened", fileName);
@ -1262,7 +1262,7 @@ void UnloadMusicStream(Music music)
if (false) {}
#endif
#if defined(SUPPORT_FILEFORMAT_FLAC)
else if (music->ctxType == MUSIC_AUDIO_FLAC) drflac_free(music->ctxFlac);
else if (music->ctxType == MUSIC_AUDIO_FLAC) drflac_RL_FREE(music->ctxFlac);
#endif
#if defined(SUPPORT_FILEFORMAT_MP3)
else if (music->ctxType == MUSIC_AUDIO_MP3) drmp3_uninit(&music->ctxMp3);
@ -1274,7 +1274,7 @@ void UnloadMusicStream(Music music)
else if (music->ctxType == MUSIC_MODULE_MOD) jar_mod_unload(&music->ctxMod);
#endif
free(music);
RL_FREE(music);
}
// Start music playing (open stream)
@ -1357,7 +1357,7 @@ void UpdateMusicStream(Music music)
unsigned int subBufferSizeInFrames = ((AudioBuffer *)music->stream.audioBuffer)->bufferSizeInFrames/2;
// NOTE: Using dynamic allocation because it could require more than 16KB
void *pcm = calloc(subBufferSizeInFrames*music->stream.channels*music->stream.sampleSize/8, 1);
void *pcm = RL_CALLOC(subBufferSizeInFrames*music->stream.channels*music->stream.sampleSize/8, 1);
int samplesCount = 0; // Total size of data steamed in L+R samples for xm floats, individual L or R for ogg shorts
@ -1427,7 +1427,7 @@ void UpdateMusicStream(Music music)
}
// Free allocated pcm data
free(pcm);
RL_FREE(pcm);
// Reset audio stream for looping
if (streamEnding)
@ -1750,7 +1750,7 @@ static Wave LoadWAV(const char *fileName)
else
{
// Allocate memory for data
wave.data = malloc(wavData.subChunkSize);
wave.data = RL_MALLOC(wavData.subChunkSize);
// Read in the sound data into the soundData variable
fread(wave.data, wavData.subChunkSize, 1, wavFile);
@ -1891,7 +1891,7 @@ static Wave LoadOGG(const char *fileName)
float totalSeconds = stb_vorbis_stream_length_in_seconds(oggFile);
if (totalSeconds > 10) TraceLog(LOG_WARNING, "[%s] Ogg audio length is larger than 10 seconds (%f), that's a big file in memory, consider music streaming", fileName, totalSeconds);
wave.data = (short *)malloc(wave.sampleCount*wave.channels*sizeof(short));
wave.data = (short *)RL_MALLOC(wave.sampleCount*wave.channels*sizeof(short));
// NOTE: Returns the number of samples to process (be careful! we ask for number of shorts!)
int numSamplesOgg = stb_vorbis_get_samples_short_interleaved(oggFile, info.channels, (short *)wave.data, wave.sampleCount*wave.channels);