diff --git a/src/memory.hh b/src/memory.hh index 7aaff5fe..163dd76c 100644 --- a/src/memory.hh +++ b/src/memory.hh @@ -2,7 +2,6 @@ #define memory_hh_INCLUDED #include -#include #include #include @@ -64,6 +63,17 @@ inline const char* domain_name(MemoryDomain domain) extern size_t domain_allocated_bytes[(size_t)MemoryDomain::Count]; +inline void on_alloc(MemoryDomain domain, size_t size) +{ + domain_allocated_bytes[(int)domain] += size; +} + +inline void on_dealloc(MemoryDomain domain, size_t size) +{ + kak_assert(domain_allocated_bytes[(int)domain] >= size); + domain_allocated_bytes[(int)domain] -= size; +} + template struct Allocator { @@ -86,16 +96,15 @@ struct Allocator T* allocate(size_t n) { size_t size = sizeof(T) * n; - domain_allocated_bytes[(int)domain] += size; - return reinterpret_cast(malloc(size)); + on_alloc(domain, size); + return reinterpret_cast(::operator new(size)); } void deallocate(T* ptr, size_t n) { size_t size = sizeof(T) * n; - kak_assert(domain_allocated_bytes[(int)domain] >= size); - domain_allocated_bytes[(int)domain] -= size; - free(ptr); + on_dealloc(domain, size); + ::operator delete(ptr); } template @@ -129,6 +138,23 @@ private: static constexpr MemoryDomain helper(...) { return MemoryDomain::Undefined; } }; +template +struct UseMemoryDomain +{ + static constexpr MemoryDomain domain = d; + static void* operator new(size_t size) + { + on_alloc(domain, size); + return ::operator new(size); + } + + static void operator delete(void* ptr, size_t size) + { + on_dealloc(domain, size); + ::operator delete(ptr); + } +}; + } #endif // memory_hh_INCLUDED diff --git a/src/shared_string.hh b/src/shared_string.hh index e2240cc9..8a84bf48 100644 --- a/src/shared_string.hh +++ b/src/shared_string.hh @@ -12,7 +12,7 @@ namespace Kakoune class SharedString : public StringView { public: - struct Storage + struct Storage : UseMemoryDomain { int refcount = 0; Vector content; diff --git a/src/value.hh b/src/value.hh index 541da3d8..51318c2d 100644 --- a/src/value.hh +++ b/src/value.hh @@ -54,16 +54,12 @@ private: }; template - struct Model : public Concept + struct Model : public Concept, public UseMemoryDomain { Model(T&& val) : m_content(std::move(val)) {} const std::type_info& type() const override { return typeid(T); } T m_content; - - using Alloc = Allocator, MemoryDomain::Values>; - static void* operator new (std::size_t sz) { return Alloc{}.allocate(1); } - static void operator delete (void* ptr) { Alloc{}.deallocate((Model*)ptr, 1); } }; std::unique_ptr m_value;