#ifndef memory_hh_INCLUDED #define memory_hh_INCLUDED #include #include "assert.hh" namespace Kakoune { enum class MemoryDomain { Undefined, String, InternedString, BufferContent, BufferMeta, Options, Highlight, WordDB }; template struct UsedMemory { static size_t byte_count; }; template size_t UsedMemory::byte_count = 0; template struct Allocator { using value_type = T; // TODO: remove that once we have a c++11 compliant stdlib using pointer = T*; using const_pointer = const T*; using reference = T&; using const_reference = const T&; using size_type = std::size_t; using difference_type = std::ptrdiff_t; Allocator() = default; template Allocator(const Allocator&) {} template struct rebind { using other = Allocator; }; T* allocate(size_t n) { size_t size = sizeof(T) * n; UsedMemory::byte_count += size; return reinterpret_cast(malloc(size)); } void deallocate(T* ptr, size_t n) { size_t size = sizeof(T) * n; kak_assert(UsedMemory::byte_count >= size); UsedMemory::byte_count -= size; free(ptr); } }; template bool operator==(const Allocator& lhs, const Allocator& rhs) { return d1 == d2; } template bool operator!=(const Allocator& lhs, const Allocator& rhs) { return d1 != d2; } } #endif // memory_hh_INCLUDED