diff options
| author | Markus Mittendrein <git@maxmitti.tk> | 2019-03-19 23:49:02 +0100 |
|---|---|---|
| committer | Markus Mittendrein <git@maxmitti.tk> | 2019-03-19 23:49:02 +0100 |
| commit | b1b4973e1e8163a24f0560c1fb8c83b901d05b66 (patch) | |
| tree | f98ae83d9a3b890e46a0f8dfe57154e01aa238fc /src/cppc4group.cpp | |
| parent | 540d28f433c658f843528f498271ca722db055b4 (diff) | |
| download | cc4group-b1b4973e1e8163a24f0560c1fb8c83b901d05b66.tar.gz cc4group-b1b4973e1e8163a24f0560c1fb8c83b901d05b66.zip | |
Add cppc4group-wrapper for custom tmp memory management
Diffstat (limited to 'src/cppc4group.cpp')
| -rw-r--r-- | src/cppc4group.cpp | 34 |
1 files changed, 33 insertions, 1 deletions
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<CppC4Group::TmpMemory*>(arg); + tmpMemory->cleanup(tmpMemory->memory, tmpMemory->arg); + delete tmpMemory; + }, static_cast<void*>(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); |
