Skip to content

Commit

Permalink
Merge pull request #4742 from lioncash/dspasm
Browse files Browse the repository at this point in the history
DSPAssembler: Replace malloced output buffer with std::vector
  • Loading branch information
degasus committed Jan 25, 2017
2 parents 18968ab + ed627a8 commit 3c184dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
41 changes: 12 additions & 29 deletions Source/Core/Core/DSP/DSPAssembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <iostream>
#include <map>
#include <string>
#include <utility>

#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
Expand Down Expand Up @@ -47,16 +48,12 @@ static const char* err_string[] = {"",
"Number out of range"};

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()
{
if (gdg_buffer)
free(gdg_buffer);
}
DSPAssembler::~DSPAssembler() = default;

bool DSPAssembler::Assemble(const std::string& text, std::vector<u16>& code,
std::vector<int>* line_numbers)
Expand All @@ -71,32 +68,18 @@ bool DSPAssembler::Assemble(const std::string& text, std::vector<u16>& code,
return false;

// We now have the size of the output buffer
if (m_totalSize > 0)
{
gdg_buffer = (char*)malloc(m_totalSize * sizeof(u16) + 4);
if (!gdg_buffer)
return false;

memset(gdg_buffer, 0, m_totalSize * sizeof(u16));
}
else
if (m_totalSize <= 0)
return false;

m_output_buffer.resize(m_totalSize);

InitPass(2);
if (!AssembleFile(file_name, 2))
return false;

code.resize(m_totalSize);
for (int i = 0; i < m_totalSize; i++)
{
code[i] = *(u16*)(gdg_buffer + i * 2);
}

if (gdg_buffer)
{
free(gdg_buffer);
gdg_buffer = nullptr;
}
code = std::move(m_output_buffer);
m_output_buffer.clear();
m_output_buffer.shrink_to_fit();

last_error_str = "(no errors)";
last_error = ERR_OK;
Expand Down Expand Up @@ -1017,10 +1000,10 @@ bool DSPAssembler::AssembleFile(const std::string& file_path, int pass)
if (pass == 2)
{
// generate binary
((u16*)gdg_buffer)[m_cur_addr] = 0x0000;
BuildCode(opc, params, params_count, (u16*)gdg_buffer);
m_output_buffer[m_cur_addr] = 0x0000;
BuildCode(opc, params, params_count, m_output_buffer.data());
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;
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/Core/DSP/DSPAssembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstddef>
#include <map>
#include <string>
#include <vector>

#include "Common/CommonTypes.h"

Expand Down Expand Up @@ -95,7 +96,7 @@ class DSPAssembler
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);

char* gdg_buffer;
std::vector<u16> m_output_buffer;

std::string include_dir;
std::string cur_line;
Expand Down

0 comments on commit 3c184dc

Please sign in to comment.