Introduce solidity_free in libsolc

This commit is contained in:
Alex Beregszaszi 2019-01-22 11:41:21 +00:00
parent e83e9a81e5
commit 554511b68e
5 changed files with 17 additions and 0 deletions

View File

@ -10,6 +10,7 @@ Language Features:
Compiler Features: Compiler Features:
* C API (``libsolc`` / raw ``soljson.js``): Introduce ``solidity_free`` method which releases all internal buffers to save memory.
Bugfixes: Bugfixes:

View File

@ -98,4 +98,8 @@ extern char const* solidity_compile(char const* _input, CStyleReadFileCallback _
s_outputBuffer = compile(_input, _readCallback); s_outputBuffer = compile(_input, _readCallback);
return s_outputBuffer.c_str(); return s_outputBuffer.c_str();
} }
extern void solidity_free() noexcept
{
s_outputBuffer.clear();
}
} }

View File

@ -55,6 +55,11 @@ char const* solidity_version() SOLC_NOEXCEPT;
/// The pointer returned must not be freed by the caller. /// The pointer returned must not be freed by the caller.
char const* solidity_compile(char const* _input, CStyleReadFileCallback _readCallback) SOLC_NOEXCEPT; char const* solidity_compile(char const* _input, CStyleReadFileCallback _readCallback) SOLC_NOEXCEPT;
/// Frees up any allocated memory.
///
/// NOTE: the pointer returned by solidity_compile is invalid after calling this!
void solidity_free() SOLC_NOEXCEPT;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -45,6 +45,7 @@ Json::Value compile(string const& _input)
string output(solidity_compile(_input.c_str(), nullptr)); string output(solidity_compile(_input.c_str(), nullptr));
Json::Value ret; Json::Value ret;
BOOST_REQUIRE(jsonParseStrict(output, ret)); BOOST_REQUIRE(jsonParseStrict(output, ret));
solidity_free();
return ret; return ret;
} }
@ -56,12 +57,14 @@ BOOST_AUTO_TEST_CASE(read_version)
{ {
string output(solidity_version()); string output(solidity_version());
BOOST_CHECK(output.find(VersionString) == 0); BOOST_CHECK(output.find(VersionString) == 0);
solidity_free();
} }
BOOST_AUTO_TEST_CASE(read_license) BOOST_AUTO_TEST_CASE(read_license)
{ {
string output(solidity_license()); string output(solidity_license());
BOOST_CHECK(output.find("GNU GENERAL PUBLIC LICENSE") != string::npos); BOOST_CHECK(output.find("GNU GENERAL PUBLIC LICENSE") != string::npos);
solidity_free();
} }
BOOST_AUTO_TEST_CASE(standard_compilation) BOOST_AUTO_TEST_CASE(standard_compilation)

View File

@ -35,6 +35,10 @@ void FuzzerUtil::runCompiler(string const& _input, bool _quiet)
string outputString(solidity_compile(_input.c_str(), nullptr)); string outputString(solidity_compile(_input.c_str(), nullptr));
if (!_quiet) if (!_quiet)
cout << "Output JSON: " << outputString << endl; cout << "Output JSON: " << outputString << endl;
// This should be safe given the above copies the output.
solidity_free();
Json::Value output; Json::Value output;
if (!jsonParseStrict(outputString, output)) if (!jsonParseStrict(outputString, output))
{ {