Merge pull request #279 from ethereum/release-helper

Improve evmc_release_result() helper
This commit is contained in:
Paweł Bylica 2019-05-09 22:33:29 +02:00 committed by GitHub
commit 9a4dcea648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 18 deletions

View File

@ -362,25 +362,23 @@ struct evmc_result
size_t output_size;
/**
* The pointer to a function releasing all resources associated with
* the result object.
* The method releasing all resources associated with the result object.
*
* This function pointer is optional (MAY be NULL) and MAY be set by
* the EVM implementation. If set it MUST be used by the user to
* release memory and other resources associated with the result object.
* After the result resources are released the result object
* MUST NOT be used any more.
* This method (function pointer) is optional (MAY be NULL) and MAY be set
* by the VM implementation. If set it MUST be called by the user once to
* release memory and other resources associated with the result object.
* Once the resources are released the result object MUST NOT be used again.
*
* The suggested code pattern for releasing EVM results:
* @code
* struct evmc_result result = ...;
* if (result.release)
* result.release(&result);
* @endcode
* The suggested code pattern for releasing execution results:
* @code
* struct evmc_result result = ...;
* if (result.release)
* result.release(&result);
* @endcode
*
* @note
* It works similarly to C++ virtual destructor. Attaching the release
* function to the result itself allows EVM composition.
* @note
* It works similarly to C++ virtual destructor. Attaching the release
* function to the result itself allows VM composition.
*/
evmc_release_result_fn release;

View File

@ -109,11 +109,14 @@ static inline struct evmc_result evmc_execute(struct evmc_instance* instance,
/**
* Releases the resources allocated to the execution result.
*
* @see evmc_release_result_fn
* @param result The result object to be released. MUST NOT be NULL.
*
* @see evmc_result::release() evmc_release_result_fn
*/
static inline void evmc_release_result(struct evmc_result* result)
{
result->release(result);
if (result->release)
result->release(result);
}

View File

@ -61,3 +61,19 @@ TEST(helpers, is_zero)
b.bytes[0] = 1;
EXPECT_FALSE(is_zero(b));
}
TEST(helpers, release_result)
{
auto r1 = evmc_result{};
evmc_release_result(&r1);
static evmc_result r2;
static bool e;
e = false;
r2 = evmc_result{};
r2.release = [](const evmc_result* r) { e = r == &r2; };
EXPECT_FALSE(e);
evmc_release_result(&r2);
EXPECT_TRUE(e);
}