diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2015-01-07 19:29:31 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2015-01-07 19:29:31 +0000 |
| commit | 9b30e7469a5832166b244a85065b3e1efb30cc90 (patch) | |
| tree | 0ae0332a35b790bd58d07073517c358ab00daf47 /src/memory.hh | |
| parent | de12fe1cc67cb7b61c8a824c2bffbfbd75ba503f (diff) | |
Add initial memory domain allocation tracking support
Diffstat (limited to 'src/memory.hh')
| -rw-r--r-- | src/memory.hh | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/memory.hh b/src/memory.hh new file mode 100644 index 00000000..36b5c31c --- /dev/null +++ b/src/memory.hh @@ -0,0 +1,72 @@ +#ifndef memory_hh_INCLUDED +#define memory_hh_INCLUDED + +#include <cstdlib> + +#include "assert.hh" + +namespace Kakoune +{ + +enum class MemoryDomain +{ + Undefined, + String, + InternedString, + BufferContent, + BufferMeta, + WordDB +}; + +template<MemoryDomain domain> +struct UsedMemory +{ + static size_t byte_count; +}; + +template<MemoryDomain domain> +size_t UsedMemory<domain>::byte_count = 0; + +template<typename T, MemoryDomain domain> +struct Allocator +{ + using value_type = T; + + Allocator() = default; + template<typename U> + Allocator(const Allocator<U, domain>&) {} + + template<typename U> + struct rebind { using other = Allocator<U, domain>; }; + + T* allocate(size_t n) + { + size_t size = sizeof(T) * n; + UsedMemory<domain>::byte_count += size; + return reinterpret_cast<T*>(malloc(size)); + } + + void deallocate(T* ptr, size_t n) + { + size_t size = sizeof(T) * n; + kak_assert(UsedMemory<domain>::byte_count >= size); + UsedMemory<domain>::byte_count -= size; + free(ptr); + } +}; + +template<typename T1, MemoryDomain d1, typename T2, MemoryDomain d2> +bool operator==(const Allocator<T1, d1>& lhs, const Allocator<T2, d2>& rhs) +{ + return d1 == d2; +} + +template<typename T1, MemoryDomain d1, typename T2, MemoryDomain d2> +bool operator!=(const Allocator<T1, d1>& lhs, const Allocator<T2, d2>& rhs) +{ + return d1 != d2; +} + +} + +#endif // memory_hh_INCLUDED |
