aboutsummaryrefslogtreecommitdiff
path: root/asm/preprocesser.cpp
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2024-04-15 05:34:02 +0630
committerAryadev Chavali <aryadev@aryadevchavali.com>2024-04-15 05:34:02 +0630
commit7a6275c0a1ff61216b0dc7f2f3a78e5e958b5ead (patch)
tree11052957fca35b9bd8babd7c84dbbfe43fbf943e /asm/preprocesser.cpp
parent8d3951a871c1442649c33bd3c4af7cc699b9ce64 (diff)
downloadovm-7a6275c0a1ff61216b0dc7f2f3a78e5e958b5ead.tar.gz
ovm-7a6275c0a1ff61216b0dc7f2f3a78e5e958b5ead.tar.bz2
ovm-7a6275c0a1ff61216b0dc7f2f3a78e5e958b5ead.zip
fix memory leak through vec.clear
vec.clear() doesn't delete pointers (unless they're smart) so I need to do it myself.
Diffstat (limited to 'asm/preprocesser.cpp')
-rw-r--r--asm/preprocesser.cpp18
1 files changed, 12 insertions, 6 deletions
diff --git a/asm/preprocesser.cpp b/asm/preprocesser.cpp
index 6971d2b..0976396 100644
--- a/asm/preprocesser.cpp
+++ b/asm/preprocesser.cpp
@@ -20,6 +20,12 @@ using std::pair, std::vector, std::make_pair, std::string, std::string_view;
#define ERR(E) std::make_pair(tokens, (E))
#define VAL(E) std::make_pair(E, pp_err_t{pp_err_type_t::OK})
+#define VCLEAR(V) \
+ std::for_each((V).begin(), (V).end(), \
+ [](token_t *t) \
+ { \
+ delete t; \
+ });
pair<vector<token_t *>, pp_err_t> preprocesser(vector<token_t *> &tokens)
{
@@ -33,12 +39,12 @@ pair<vector<token_t *>, pp_err_t> preprocesser(vector<token_t *> &tokens)
std::tie(const_block_tokens, pperr) = preprocess_const_blocks(tokens);
if (pperr.type != pp_err_type_t::OK)
{
- tokens.clear();
+ VCLEAR(tokens);
tokens = use_block_tokens;
return ERR(pperr);
}
- use_block_tokens.clear();
+ VCLEAR(use_block_tokens);
return VAL(const_block_tokens);
}
@@ -54,7 +60,7 @@ preprocess_use_blocks(const vector<token_t *> &tokens)
if (i + 1 >= tokens.size() ||
tokens[i + 1]->type != token_type_t::LITERAL_STRING)
{
- new_tokens.clear();
+ VCLEAR(new_tokens);
return ERR(pp_err_t(pp_err_type_t::EXPECTED_STRING, t));
}
@@ -62,7 +68,7 @@ preprocess_use_blocks(const vector<token_t *> &tokens)
auto source = read_file(name->content.c_str());
if (!source)
{
- new_tokens.clear();
+ VCLEAR(new_tokens);
return ERR(pp_err_t(pp_err_type_t::FILE_NONEXISTENT, name));
}
@@ -70,7 +76,7 @@ preprocess_use_blocks(const vector<token_t *> &tokens)
lerr_t lerr = tokenise_buffer(source.value(), ftokens);
if (lerr != lerr_t::OK)
{
- new_tokens.clear();
+ VCLEAR(new_tokens);
return ERR(pp_err_t(pp_err_type_t::FILE_PARSE_ERROR, name, lerr));
}
@@ -150,7 +156,7 @@ preprocess_const_blocks(vector<token_t *> const &tokens)
auto it = blocks.find(token->content);
if (it == blocks.end())
{
- new_tokens.clear();
+ VCLEAR(new_tokens);
return ERR(pp_err_t(pp_err_type_t::UNKNOWN_NAME, token));
}