diff options
author | Jeffrey Czyz <jkczyz@gmail.com> | 2019-06-05 15:54:11 -0700 |
---|---|---|
committer | Jeffrey Czyz <jkczyz@gmail.com> | 2019-11-20 18:19:13 -0800 |
commit | f36d1d5b8934aac60d3097047ecedeb58bae2185 (patch) | |
tree | 695a8cf90b7c655717b0f7f572285d643793ae27 /src/support/lockedpool.cpp | |
parent | b7bc9b8330096d1f4f1fa563b855b88da425226e (diff) |
Use void* throughout support/lockedpool.h
Replace uses of char* with void* in Arena's member variables. Instead,
cast to char* where needed in the implementation.
Certain compiler environments disallow std::hash<char*> specializations
to prevent hashing the pointer's value instead of the string contents.
Thus, compilation fails when std::unordered_map is keyed by char*.
Explicitly using void* is a workaround in such environments. For
consistency, void* is used throughout all member variables similarly to
the public interface.
Diffstat (limited to 'src/support/lockedpool.cpp')
-rw-r--r-- | src/support/lockedpool.cpp | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/src/support/lockedpool.cpp b/src/support/lockedpool.cpp index 85e3351e72..6d767ca210 100644 --- a/src/support/lockedpool.cpp +++ b/src/support/lockedpool.cpp @@ -27,6 +27,7 @@ #include <iomanip> #include <iostream> #endif +#include <utility> LockedPoolManager* LockedPoolManager::_instance = nullptr; std::once_flag LockedPoolManager::init_flag; @@ -44,12 +45,12 @@ static inline size_t align_up(size_t x, size_t align) // Implementation: Arena Arena::Arena(void *base_in, size_t size_in, size_t alignment_in): - base(static_cast<char*>(base_in)), end(static_cast<char*>(base_in) + size_in), alignment(alignment_in) + base(base_in), end(static_cast<char*>(base_in) + size_in), alignment(alignment_in) { // Start with one free chunk that covers the entire arena auto it = size_to_free_chunk.emplace(size_in, base); chunks_free.emplace(base, it); - chunks_free_end.emplace(base + size_in, it); + chunks_free_end.emplace(static_cast<char*>(base) + size_in, it); } Arena::~Arena() @@ -75,8 +76,9 @@ void* Arena::alloc(size_t size) // Create the used-chunk, taking its space from the end of the free-chunk const size_t size_remaining = size_ptr_it->first - size; - auto allocated = chunks_used.emplace(size_ptr_it->second + size_remaining, size).first; - chunks_free_end.erase(size_ptr_it->second + size_ptr_it->first); + char* const free_chunk = static_cast<char*>(size_ptr_it->second); + auto allocated = chunks_used.emplace(free_chunk + size_remaining, size).first; + chunks_free_end.erase(free_chunk + size_ptr_it->first); if (size_ptr_it->first == size) { // whole chunk is used up chunks_free.erase(size_ptr_it->second); @@ -84,11 +86,11 @@ void* Arena::alloc(size_t size) // still some memory left in the chunk auto it_remaining = size_to_free_chunk.emplace(size_remaining, size_ptr_it->second); chunks_free[size_ptr_it->second] = it_remaining; - chunks_free_end.emplace(size_ptr_it->second + size_remaining, it_remaining); + chunks_free_end.emplace(free_chunk + size_remaining, it_remaining); } size_to_free_chunk.erase(size_ptr_it); - return reinterpret_cast<void*>(allocated->first); + return allocated->first; } void Arena::free(void *ptr) @@ -99,11 +101,11 @@ void Arena::free(void *ptr) } // Remove chunk from used map - auto i = chunks_used.find(static_cast<char*>(ptr)); + auto i = chunks_used.find(ptr); if (i == chunks_used.end()) { throw std::runtime_error("Arena: invalid or double free"); } - std::pair<char*, size_t> freed = *i; + auto freed = std::make_pair(static_cast<char*>(i->first), i->second); chunks_used.erase(i); // coalesce freed with previous chunk |