MXS-2302: Use unique_ptr for hint storage

This removes the need to explicitly free them.
This commit is contained in:
Markus Mäkelä
2019-03-03 00:05:02 +02:00
parent d52f685ee2
commit 6c05fa7d54
2 changed files with 21 additions and 25 deletions

View File

@ -307,14 +307,13 @@ HINT* HintParser::parse_one(InputIter it, InputIter end)
{ {
if ((rval = process_definition())) if ((rval = process_definition()))
{ {
m_stack.push_back(hint_dup(rval)); m_stack.emplace_back(hint_dup(rval));
} }
} }
else if (t == TOK_STOP) else if (t == TOK_STOP)
{ {
if (!m_stack.empty()) if (!m_stack.empty())
{ {
hint_free(m_stack.back());
m_stack.pop_back(); m_stack.pop_back();
} }
} }
@ -339,7 +338,7 @@ HINT* HintParser::parse_one(InputIter it, InputIter end)
if (hint) if (hint)
{ {
// Preparation of a named hint // Preparation of a named hint
m_named_hints[key] = hint_dup(hint); m_named_hints[key] = std::unique_ptr<HINT>(hint_dup(hint));
} }
} }
else if (t == TOK_START) else if (t == TOK_START)
@ -349,8 +348,8 @@ HINT* HintParser::parse_one(InputIter it, InputIter end)
if (m_named_hints.count(key) == 0) if (m_named_hints.count(key) == 0)
{ {
// New hint defined, push it on to the stack // New hint defined, push it on to the stack
m_named_hints[key] = hint_dup(rval); m_named_hints[key] = std::unique_ptr<HINT>(hint_dup(rval));
m_stack.push_back(hint_dup(rval)); m_stack.emplace_back(hint_dup(rval));
} }
} }
else if (next_token() == TOK_END) else if (next_token() == TOK_END)
@ -360,8 +359,8 @@ HINT* HintParser::parse_one(InputIter it, InputIter end)
if (it != m_named_hints.end()) if (it != m_named_hints.end())
{ {
// We're starting an already define named hint // We're starting an already define named hint
m_stack.push_back(hint_dup(it->second)); m_stack.emplace_back(hint_dup(it->second.get()));
rval = hint_dup(it->second); rval = hint_dup(it->second.get());
} }
} }
} }
@ -393,25 +392,12 @@ HINT* HintParser::parse(InputIter it, InputIter end)
if (!rval && !m_stack.empty()) if (!rval && !m_stack.empty())
{ {
rval = hint_dup(m_stack.back()); rval = hint_dup(m_stack.back().get());
} }
return rval; return rval;
} }
HintParser::~HintParser()
{
for (auto& a : m_named_hints)
{
hint_free(a.second);
}
for (auto& a : m_stack)
{
hint_free(a);
}
}
void HintSession::process_hints(GWBUF* buffer) void HintSession::process_hints(GWBUF* buffer)
{ {
mxs::Buffer buf(buffer); mxs::Buffer buf(buffer);

View File

@ -16,6 +16,18 @@
#include <maxscale/hint.h> #include <maxscale/hint.h>
#include <maxscale/filter.hh> #include <maxscale/filter.hh>
namespace std
{
template<>
struct default_delete<HINT>
{
void operator()(HINT* pHint)
{
hint_free(pHint);
}
};
}
class HintSession; class HintSession;
class HintInstance : public mxs::Filter<HintInstance, HintSession> class HintInstance : public mxs::Filter<HintInstance, HintSession>
@ -62,8 +74,6 @@ public:
*/ */
HINT* parse(InputIter begin, InputIter end); HINT* parse(InputIter begin, InputIter end);
~HintParser();
private: private:
InputIter m_it; InputIter m_it;
@ -71,8 +81,8 @@ private:
InputIter m_tok_begin; InputIter m_tok_begin;
InputIter m_tok_end; InputIter m_tok_end;
std::vector<HINT*> m_stack; std::vector<std::unique_ptr<HINT>> m_stack;
std::unordered_map<std::string, HINT*> m_named_hints; std::unordered_map<std::string, std::unique_ptr<HINT>> m_named_hints;
TOKEN_VALUE next_token(); TOKEN_VALUE next_token();
HINT* process_definition(); HINT* process_definition();