[test runner] Introduce AllocationIndex
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
add_executable(mbgl-render-test
|
||||
render-test/allocation_index.cpp
|
||||
render-test/main.cpp
|
||||
render-test/parser.cpp
|
||||
render-test/runner.cpp
|
||||
|
||||
93
render-test/allocation_index.cpp
Normal file
93
render-test/allocation_index.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
#include "allocation_index.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace {
|
||||
|
||||
std::atomic_size_t indexedMemorySize{0};
|
||||
std::atomic_size_t allocationsCount{0};
|
||||
std::unordered_map<void*, size_t> memoryIndex;
|
||||
std::atomic_bool suppresIndexing{false};
|
||||
std::atomic_bool active{false};
|
||||
std::mutex indexMutex;
|
||||
|
||||
class FlagGuard {
|
||||
public:
|
||||
explicit FlagGuard(std::atomic_bool& flag_)
|
||||
: flag(flag_) { flag = true; }
|
||||
~FlagGuard() { flag = false; }
|
||||
|
||||
private:
|
||||
std::atomic_bool& flag;
|
||||
};
|
||||
|
||||
void addToIndex(std::size_t sz, void* ptr) {
|
||||
std::lock_guard<std::mutex> mlk(indexMutex);
|
||||
FlagGuard flk(suppresIndexing);
|
||||
allocationsCount++;
|
||||
indexedMemorySize += sz;
|
||||
memoryIndex[ptr] = sz;
|
||||
}
|
||||
|
||||
void removeFromIndex(void* ptr) {
|
||||
std::lock_guard<std::mutex> mlk(indexMutex);
|
||||
FlagGuard flk(suppresIndexing);
|
||||
auto it = memoryIndex.find(ptr);
|
||||
if (it == memoryIndex.end()) return;
|
||||
|
||||
assert(indexedMemorySize >= it->second);
|
||||
indexedMemorySize -= it->second;
|
||||
memoryIndex.erase(it);
|
||||
}
|
||||
|
||||
inline bool canModifyIndex() {
|
||||
return active && !suppresIndexing;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// static
|
||||
void AllocationIndex::setActive(bool active_) {
|
||||
active = active_;
|
||||
}
|
||||
|
||||
// static
|
||||
bool AllocationIndex::isActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
// static
|
||||
void AllocationIndex::reset() {
|
||||
std::lock_guard<std::mutex> mlk(indexMutex);
|
||||
FlagGuard flk(suppresIndexing);
|
||||
memoryIndex.clear();
|
||||
indexedMemorySize = 0;
|
||||
allocationsCount = 0;
|
||||
}
|
||||
|
||||
// static
|
||||
void* AllocationIndex::allocate(size_t size) {
|
||||
void *ptr = std::malloc(size);
|
||||
if (ptr && canModifyIndex()) addToIndex(size, ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
// static
|
||||
void AllocationIndex::deallocate(void* ptr) noexcept {
|
||||
if (canModifyIndex()) removeFromIndex(ptr);
|
||||
std::free(ptr);
|
||||
}
|
||||
|
||||
// static
|
||||
size_t AllocationIndex::getAllocatedSize() {
|
||||
return indexedMemorySize;
|
||||
}
|
||||
|
||||
// static
|
||||
size_t AllocationIndex::getAllocationsCount() {
|
||||
return allocationsCount;
|
||||
}
|
||||
46
render-test/allocation_index.hpp
Normal file
46
render-test/allocation_index.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
class AllocationIndex {
|
||||
public:
|
||||
AllocationIndex() = delete;
|
||||
/**
|
||||
* @brief Starts/stops allocated memory indexing.
|
||||
*/
|
||||
static void setActive(bool);
|
||||
/**
|
||||
* @brief Returns memory indexing activity status.
|
||||
*/
|
||||
static bool isActive();
|
||||
/**
|
||||
* @brief Resets the allocated memory index and all statistics data.
|
||||
*/
|
||||
static void reset();
|
||||
/**
|
||||
* @brief Same as `malloc()` + indexes the allocated data,
|
||||
* if indexing is active.
|
||||
*
|
||||
* @return void*
|
||||
*/
|
||||
static void* allocate(size_t);
|
||||
/**
|
||||
* @brief Same as `free()` + removes the corresponding data from index,
|
||||
* if these data are present in the index and indexing is active.
|
||||
*
|
||||
* @return void*
|
||||
*/
|
||||
static void deallocate(void*) noexcept;
|
||||
/**
|
||||
* @brief Returns the size (in bytes) of allocated data, currently present in the index.
|
||||
*
|
||||
* @return size_t
|
||||
*/
|
||||
static size_t getAllocatedSize();
|
||||
/**
|
||||
* @brief Returns the total amount of allocations since indexing start.
|
||||
*
|
||||
* @return size_t
|
||||
*/
|
||||
static size_t getAllocationsCount();
|
||||
};
|
||||
Reference in New Issue
Block a user