From b1b4973e1e8163a24f0560c1fb8c83b901d05b66 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Tue, 19 Mar 2019 23:49:02 +0100 Subject: Add cppc4group-wrapper for custom tmp memory management --- src/cppc4group.cpp | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'src/cppc4group.cpp') diff --git a/src/cppc4group.cpp b/src/cppc4group.cpp index 464dc88..1b4022e 100644 --- a/src/cppc4group.cpp +++ b/src/cppc4group.cpp @@ -12,6 +12,11 @@ CppC4Group::Data::Data(const void* const data, const size_t size) : data{data}, } +CppC4Group::TmpMemory::TmpMemory(void* const memory, const TmpMemoryCleanupCallback& cleanup, void *const arg) : memory(memory), cleanup(cleanup), arg(arg) +{ + +} + struct CppC4Group::Private { CC4Group* g; @@ -145,11 +150,38 @@ std::string CppC4Group::getErrorCauser() return cc4group.getErrorCauser(p->g); } -void CppC4Group::setTmpMemoryStrategy(const CppC4Group::TmpMemoryStrategy strategy) +void CppC4Group::setTmpMemoryStrategy(const TmpMemoryStrategy strategy) { cc4group.setTmpMemoryStrategy(convertTmpMemoryStrategy(strategy)); } +namespace { + static CppC4Group::TmpMemoryCallback tmpMemoryCallback = nullptr; + + void* customTmpMemoryStrategy(CC4Group* const, size_t size, CC4Group_CleanupJob* cleanup) + { + if(const auto& tmpMemory = tmpMemoryCallback(size)) + { + auto tmpMemorySettings = new CppC4Group::TmpMemory(*tmpMemory); + *cleanup = CC4Group_CleanupJob{[](void* arg){ + auto tmpMemory = reinterpret_cast(arg); + tmpMemory->cleanup(tmpMemory->memory, tmpMemory->arg); + delete tmpMemory; + }, static_cast(tmpMemorySettings)}; + + return tmpMemorySettings->memory; + } + + return nullptr; + } +} + +void CppC4Group::setTmpMemoryStrategy(const TmpMemoryCallback callback) +{ + tmpMemoryCallback = callback; + cc4group.setTmpMemoryStrategy(customTmpMemoryStrategy); +} + bool CppC4Group::setMaker(const std::string& maker, const std::string& path, const bool recursive) { return cc4group.setMaker(p->g, maker.c_str(), path.c_str(), recursive); -- cgit v1.2.3-54-g00ecf