Merge pull request #37362 from reduz/audioserver-memory-cleanup

Remove the audio memory allocator, use regular one instead.
This commit is contained in:
Rémi Verschelde 2020-03-28 15:49:48 +01:00 committed by GitHub
commit 3f7fa93179
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 65 deletions

View File

@ -122,7 +122,7 @@ void AudioStreamPlaybackOGGVorbis::seek(float p_time) {
AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() {
if (ogg_alloc.alloc_buffer) {
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->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->frames_mixed = 0;
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);
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;
ERR_FAIL_COND_V(!ovs->ogg_stream, Ref<AudioStreamPlaybackOGGVorbis>());
}
@ -158,7 +158,7 @@ String AudioStreamOGGVorbis::get_stream_name() const {
void AudioStreamOGGVorbis::clear_data() {
if (data) {
AudioServer::get_singleton()->audio_data_free(data);
memfree(data);
data = NULL;
data_len = 0;
}
@ -210,7 +210,8 @@ void AudioStreamOGGVorbis::set_data(const Vector<uint8_t> &p_data) {
// free any existing 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;
break;

View File

@ -481,7 +481,7 @@ void AudioStreamSample::set_data(const Vector<uint8_t> &p_data) {
AudioServer::get_singleton()->lock();
if (data) {
AudioServer::get_singleton()->audio_data_free(data);
memfree(data);
data = NULL;
data_bytes = 0;
}
@ -491,7 +491,7 @@ void AudioStreamSample::set_data(const Vector<uint8_t> &p_data) {
const uint8_t *r = p_data.ptr();
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);
uint8_t *dataptr = (uint8_t *)data;
copymem(dataptr + DATA_PAD, r, datalen);
@ -660,7 +660,7 @@ AudioStreamSample::AudioStreamSample() {
AudioStreamSample::~AudioStreamSample() {
if (data) {
AudioServer::get_singleton()->audio_data_free(data);
memfree(data);
data = NULL;
data_bytes = 0;
}

View File

@ -1131,47 +1131,6 @@ double AudioServer::get_time_since_last_mix() const {
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) {
lock();
CallbackItem ci;
@ -1400,8 +1359,6 @@ void AudioServer::_bind_methods() {
AudioServer::AudioServer() {
singleton = this;
audio_data_total_mem = 0;
audio_data_max_mem = 0;
mix_frames = 0;
channel_count = 0;
to_mix = 0;

View File

@ -232,14 +232,6 @@ private:
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 _mix_step();
@ -350,12 +342,6 @@ public:
virtual double get_time_to_next_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 remove_callback(AudioCallback p_callback, void *p_userdata);