DSPAssembler: Replace malloced output buffer with std::vector

Same thing, less manual memory management.
This commit is contained in:
Lioncash 2017-01-23 16:43:04 -05:00
parent bd2881ff86
commit ed627a8cff
2 changed files with 14 additions and 30 deletions

View File

@ -13,6 +13,7 @@
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <string> #include <string>
#include <utility>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
@ -47,16 +48,12 @@ static const char* err_string[] = {"",
"Number out of range"}; "Number out of range"};
DSPAssembler::DSPAssembler(const AssemblerSettings& settings) DSPAssembler::DSPAssembler(const AssemblerSettings& settings)
: gdg_buffer(nullptr), m_cur_addr(0), m_cur_pass(0), m_current_param(0), settings_(settings) : m_cur_addr(0), m_cur_pass(0), m_current_param(0), settings_(settings)
{ {
} }
DSPAssembler::~DSPAssembler() DSPAssembler::~DSPAssembler() = default;
{
if (gdg_buffer)
free(gdg_buffer);
}
bool DSPAssembler::Assemble(const std::string& text, std::vector<u16>& code, bool DSPAssembler::Assemble(const std::string& text, std::vector<u16>& code,
std::vector<int>* line_numbers) std::vector<int>* line_numbers)
@ -71,32 +68,18 @@ bool DSPAssembler::Assemble(const std::string& text, std::vector<u16>& code,
return false; return false;
// We now have the size of the output buffer // We now have the size of the output buffer
if (m_totalSize > 0) if (m_totalSize <= 0)
{
gdg_buffer = (char*)malloc(m_totalSize * sizeof(u16) + 4);
if (!gdg_buffer)
return false; return false;
memset(gdg_buffer, 0, m_totalSize * sizeof(u16)); m_output_buffer.resize(m_totalSize);
}
else
return false;
InitPass(2); InitPass(2);
if (!AssembleFile(file_name, 2)) if (!AssembleFile(file_name, 2))
return false; return false;
code.resize(m_totalSize); code = std::move(m_output_buffer);
for (int i = 0; i < m_totalSize; i++) m_output_buffer.clear();
{ m_output_buffer.shrink_to_fit();
code[i] = *(u16*)(gdg_buffer + i * 2);
}
if (gdg_buffer)
{
free(gdg_buffer);
gdg_buffer = nullptr;
}
last_error_str = "(no errors)"; last_error_str = "(no errors)";
last_error = ERR_OK; last_error = ERR_OK;
@ -1017,10 +1000,10 @@ bool DSPAssembler::AssembleFile(const std::string& file_path, int pass)
if (pass == 2) if (pass == 2)
{ {
// generate binary // generate binary
((u16*)gdg_buffer)[m_cur_addr] = 0x0000; m_output_buffer[m_cur_addr] = 0x0000;
BuildCode(opc, params, params_count, (u16*)gdg_buffer); BuildCode(opc, params, params_count, m_output_buffer.data());
if (opc_ext) if (opc_ext)
BuildCode(opc_ext, params_ext, params_count_ext, (u16*)gdg_buffer); BuildCode(opc_ext, params_ext, params_count_ext, m_output_buffer.data());
} }
m_cur_addr += opcode_size; m_cur_addr += opcode_size;

View File

@ -8,6 +8,7 @@
#include <cstddef> #include <cstddef>
#include <map> #include <map>
#include <string> #include <string>
#include <vector>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -95,7 +96,7 @@ private:
bool VerifyParams(const opc_t* opc, param_t* par, size_t count, bool ext = false); bool VerifyParams(const opc_t* opc, param_t* par, size_t count, bool ext = false);
void BuildCode(const opc_t* opc, param_t* par, u32 par_count, u16* outbuf); void BuildCode(const opc_t* opc, param_t* par, u32 par_count, u16* outbuf);
char* gdg_buffer; std::vector<u16> m_output_buffer;
std::string include_dir; std::string include_dir;
std::string cur_line; std::string cur_line;