1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-27 06:04:25 +01:00

Codechange: [Script] Add allocator that uses script memory allocation accounting

This commit is contained in:
Peter Nelson
2025-12-19 22:23:27 +00:00
committed by Peter Nelson
parent 33c967a558
commit d7942c3fd0

View File

@@ -510,4 +510,37 @@ public:
}
};
/**
* Allocator that uses script memory allocation accounting.
* @tparam T Type of allocator.
*/
template <typename T>
struct ScriptStdAllocator
{
using value_type = T;
ScriptStdAllocator() = default;
template <typename U>
constexpr ScriptStdAllocator(const ScriptStdAllocator<U> &) noexcept {}
T *allocate(std::size_t n)
{
Squirrel::IncreaseAllocatedSize(n * sizeof(T));
return std::allocator<T>{}.allocate(n);
}
void deallocate(T *mem, std::size_t n)
{
Squirrel::DecreaseAllocatedSize(n * sizeof(T));
std::allocator<T>{}.deallocate(mem, n);
}
};
template <typename T, typename U>
bool operator==(const ScriptStdAllocator<T> &, const ScriptStdAllocator<U> &) { return true; }
template <typename T, typename U>
bool operator!=(const ScriptStdAllocator<T> &, const ScriptStdAllocator<U> &) { return false; }
#endif /* SCRIPT_OBJECT_HPP */