From c9c41491395752e983f0e37926d6a2e197f2c5ed Mon Sep 17 00:00:00 2001 From: David Carlier Date: Fri, 7 Feb 2020 18:07:07 +0000 Subject: [PATCH] slighty less C code proposal --- src/audio/sfx_buffer.cpp | 14 +++----------- src/audio/sfx_buffer.hpp | 1 + src/graphics/stk_texture.cpp | 5 ++--- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/audio/sfx_buffer.cpp b/src/audio/sfx_buffer.cpp index 1c7bb196b..636718d2f 100644 --- a/src/audio/sfx_buffer.cpp +++ b/src/audio/sfx_buffer.cpp @@ -186,17 +186,11 @@ bool SFXBuffer::loadVorbisBuffer(const std::string &name, ALuint buffer) // always 16 bit data long len = (long)ov_pcm_total(&oggFile, -1) * info->channels * 2; - char *data = (char *) malloc(len); - if(!data) - { - ov_clear(&oggFile); - Log::error("SFXBuffer", "[SFXBuffer] Could not allocate decode buffer."); - return false; - } + std::unique_ptr data = std::unique_ptr(new char[len]); int bs = -1; long todo = len; - char *bufpt = data; + char *bufpt = data.get(); while (todo) { @@ -207,11 +201,9 @@ bool SFXBuffer::loadVorbisBuffer(const std::string &name, ALuint buffer) alBufferData(buffer, (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, - data, len, info->rate); + data.get(), len, info->rate); success = true; - free(data); - ov_clear(&oggFile); fclose(file); diff --git a/src/audio/sfx_buffer.hpp b/src/audio/sfx_buffer.hpp index 46507761f..dc6121253 100644 --- a/src/audio/sfx_buffer.hpp +++ b/src/audio/sfx_buffer.hpp @@ -37,6 +37,7 @@ typedef unsigned int ALuint; #include "utils/leak_check.hpp" #include +#include class SFXBase; class XMLNode; diff --git a/src/graphics/stk_texture.cpp b/src/graphics/stk_texture.cpp index 831cf7ee1..f9047c4e5 100644 --- a/src/graphics/stk_texture.cpp +++ b/src/graphics/stk_texture.cpp @@ -35,8 +35,7 @@ STKTexture::STKTexture(const std::string& path, TexConfig* tc, bool no_upload) { if (tc != NULL) { - m_tex_config = (TexConfig*)malloc(sizeof(TexConfig)); - memcpy(m_tex_config, tc, sizeof(TexConfig)); + m_tex_config = new TexConfig(*tc); } #ifndef SERVER_ONLY if (m_tex_config) @@ -86,7 +85,7 @@ STKTexture::~STKTexture() #endif // !SERVER_ONLY if (m_texture_image != NULL) m_texture_image->drop(); - free(m_tex_config); + delete m_tex_config; } // ~STKTexture // ----------------------------------------------------------------------------