Remove the audio memory allocator, use regular one instead.

This commit is contained in:
Juan Linietsky 2020-03-27 16:14:19 -03:00
parent fcfffd7297
commit 16245f2c29
4 changed files with 9 additions and 65 deletions

View file

@ -122,7 +122,7 @@ void AudioStreamPlaybackOGGVorbis::seek(float p_time) {
AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() { AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() {
if (ogg_alloc.alloc_buffer) { if (ogg_alloc.alloc_buffer) {
stb_vorbis_close(ogg_stream); stb_vorbis_close(ogg_stream);
AudioServer::get_singleton()->audio_data_free(ogg_alloc.alloc_buffer); memfree(ogg_alloc.alloc_buffer);
} }
} }
@ -134,7 +134,7 @@ Ref<AudioStreamPlayback> AudioStreamOGGVorbis::instance_playback() {
ovs.instance(); ovs.instance();
ovs->vorbis_stream = Ref<AudioStreamOGGVorbis>(this); ovs->vorbis_stream = Ref<AudioStreamOGGVorbis>(this);
ovs->ogg_alloc.alloc_buffer = (char *)AudioServer::get_singleton()->audio_data_alloc(decode_mem_size); ovs->ogg_alloc.alloc_buffer = (char *)memalloc(decode_mem_size);
ovs->ogg_alloc.alloc_buffer_length_in_bytes = decode_mem_size; ovs->ogg_alloc.alloc_buffer_length_in_bytes = decode_mem_size;
ovs->frames_mixed = 0; ovs->frames_mixed = 0;
ovs->active = false; ovs->active = false;
@ -143,7 +143,7 @@ Ref<AudioStreamPlayback> AudioStreamOGGVorbis::instance_playback() {
ovs->ogg_stream = stb_vorbis_open_memory((const unsigned char *)data, data_len, &error, &ovs->ogg_alloc); ovs->ogg_stream = stb_vorbis_open_memory((const unsigned char *)data, data_len, &error, &ovs->ogg_alloc);
if (!ovs->ogg_stream) { if (!ovs->ogg_stream) {
AudioServer::get_singleton()->audio_data_free(ovs->ogg_alloc.alloc_buffer); memfree(ovs->ogg_alloc.alloc_buffer);
ovs->ogg_alloc.alloc_buffer = NULL; ovs->ogg_alloc.alloc_buffer = NULL;
ERR_FAIL_COND_V(!ovs->ogg_stream, Ref<AudioStreamPlaybackOGGVorbis>()); ERR_FAIL_COND_V(!ovs->ogg_stream, Ref<AudioStreamPlaybackOGGVorbis>());
} }
@ -158,7 +158,7 @@ String AudioStreamOGGVorbis::get_stream_name() const {
void AudioStreamOGGVorbis::clear_data() { void AudioStreamOGGVorbis::clear_data() {
if (data) { if (data) {
AudioServer::get_singleton()->audio_data_free(data); memfree(data);
data = NULL; data = NULL;
data_len = 0; data_len = 0;
} }
@ -210,7 +210,8 @@ void AudioStreamOGGVorbis::set_data(const Vector<uint8_t> &p_data) {
// free any existing data // free any existing data
clear_data(); clear_data();
data = AudioServer::get_singleton()->audio_data_alloc(src_data_len, src_datar); data = memalloc(src_data_len);
copymem(data, src_datar, src_data_len);
data_len = src_data_len; data_len = src_data_len;
break; break;

View file

@ -481,7 +481,7 @@ void AudioStreamSample::set_data(const Vector<uint8_t> &p_data) {
AudioServer::get_singleton()->lock(); AudioServer::get_singleton()->lock();
if (data) { if (data) {
AudioServer::get_singleton()->audio_data_free(data); memfree(data);
data = NULL; data = NULL;
data_bytes = 0; data_bytes = 0;
} }
@ -491,7 +491,7 @@ void AudioStreamSample::set_data(const Vector<uint8_t> &p_data) {
const uint8_t *r = p_data.ptr(); const uint8_t *r = p_data.ptr();
int alloc_len = datalen + DATA_PAD * 2; int alloc_len = datalen + DATA_PAD * 2;
data = AudioServer::get_singleton()->audio_data_alloc(alloc_len); //alloc with some padding for interpolation data = memalloc(alloc_len); //alloc with some padding for interpolation
zeromem(data, alloc_len); zeromem(data, alloc_len);
uint8_t *dataptr = (uint8_t *)data; uint8_t *dataptr = (uint8_t *)data;
copymem(dataptr + DATA_PAD, r, datalen); copymem(dataptr + DATA_PAD, r, datalen);
@ -660,7 +660,7 @@ AudioStreamSample::AudioStreamSample() {
AudioStreamSample::~AudioStreamSample() { AudioStreamSample::~AudioStreamSample() {
if (data) { if (data) {
AudioServer::get_singleton()->audio_data_free(data); memfree(data);
data = NULL; data = NULL;
data_bytes = 0; data_bytes = 0;
} }

View file

@ -1131,47 +1131,6 @@ double AudioServer::get_time_since_last_mix() const {
AudioServer *AudioServer::singleton = NULL; AudioServer *AudioServer::singleton = NULL;
void *AudioServer::audio_data_alloc(uint32_t p_data_len, const uint8_t *p_from_data) {
void *ad = memalloc(p_data_len);
ERR_FAIL_COND_V(!ad, NULL);
if (p_from_data) {
copymem(ad, p_from_data, p_data_len);
}
{
MutexLock lock(audio_data_lock);
audio_data[ad] = p_data_len;
audio_data_total_mem += p_data_len;
audio_data_max_mem = MAX(audio_data_total_mem, audio_data_max_mem);
}
return ad;
}
void AudioServer::audio_data_free(void *p_data) {
MutexLock lock(audio_data_lock);
if (!audio_data.has(p_data)) {
ERR_FAIL();
}
audio_data_total_mem -= audio_data[p_data];
audio_data.erase(p_data);
memfree(p_data);
}
size_t AudioServer::audio_data_get_total_memory_usage() const {
return audio_data_total_mem;
}
size_t AudioServer::audio_data_get_max_memory_usage() const {
return audio_data_max_mem;
}
void AudioServer::add_callback(AudioCallback p_callback, void *p_userdata) { void AudioServer::add_callback(AudioCallback p_callback, void *p_userdata) {
lock(); lock();
CallbackItem ci; CallbackItem ci;
@ -1400,8 +1359,6 @@ void AudioServer::_bind_methods() {
AudioServer::AudioServer() { AudioServer::AudioServer() {
singleton = this; singleton = this;
audio_data_total_mem = 0;
audio_data_max_mem = 0;
mix_frames = 0; mix_frames = 0;
channel_count = 0; channel_count = 0;
to_mix = 0; to_mix = 0;

View file

@ -232,14 +232,6 @@ private:
static AudioServer *singleton; static AudioServer *singleton;
// TODO create an audiodata pool to optimize memory
Map<void *, uint32_t> audio_data;
size_t audio_data_total_mem;
size_t audio_data_max_mem;
Mutex audio_data_lock;
void init_channels_and_buffers(); void init_channels_and_buffers();
void _mix_step(); void _mix_step();
@ -350,12 +342,6 @@ public:
virtual double get_time_to_next_mix() const; virtual double get_time_to_next_mix() const;
virtual double get_time_since_last_mix() const; virtual double get_time_since_last_mix() const;
void *audio_data_alloc(uint32_t p_data_len, const uint8_t *p_from_data = NULL);
void audio_data_free(void *p_data);
size_t audio_data_get_total_memory_usage() const;
size_t audio_data_get_max_memory_usage() const;
void add_callback(AudioCallback p_callback, void *p_userdata); void add_callback(AudioCallback p_callback, void *p_userdata);
void remove_callback(AudioCallback p_callback, void *p_userdata); void remove_callback(AudioCallback p_callback, void *p_userdata);