2015-01-07 22:29:31 +03:00
|
|
|
#ifndef memory_hh_INCLUDED
|
|
|
|
#define memory_hh_INCLUDED
|
|
|
|
|
2015-01-12 23:03:52 +03:00
|
|
|
#include <cstddef>
|
2015-01-14 02:54:58 +03:00
|
|
|
#include <new>
|
2015-01-14 01:56:51 +03:00
|
|
|
#include <utility>
|
2015-01-07 22:29:31 +03:00
|
|
|
|
|
|
|
#include "assert.hh"
|
2017-05-26 09:59:18 +03:00
|
|
|
#include "meta.hh"
|
2015-01-07 22:29:31 +03:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
enum class MemoryDomain
|
|
|
|
{
|
|
|
|
Undefined,
|
|
|
|
String,
|
2015-01-15 16:54:38 +03:00
|
|
|
SharedString,
|
2015-01-07 22:29:31 +03:00
|
|
|
BufferContent,
|
|
|
|
BufferMeta,
|
2015-01-12 16:24:30 +03:00
|
|
|
Options,
|
|
|
|
Highlight,
|
2015-01-21 16:35:46 +03:00
|
|
|
Display,
|
2015-01-12 16:45:44 +03:00
|
|
|
Mapping,
|
|
|
|
Commands,
|
|
|
|
Hooks,
|
2015-01-14 22:16:32 +03:00
|
|
|
Aliases,
|
|
|
|
EnvVars,
|
|
|
|
Faces,
|
|
|
|
Values,
|
|
|
|
Registers,
|
|
|
|
Client,
|
2015-01-12 22:46:40 +03:00
|
|
|
WordDB,
|
2015-01-16 16:58:21 +03:00
|
|
|
Selections,
|
2015-01-29 01:33:29 +03:00
|
|
|
History,
|
2016-08-31 21:33:02 +03:00
|
|
|
Remote,
|
2016-11-28 16:59:55 +03:00
|
|
|
Events,
|
|
|
|
Completion,
|
2015-01-12 22:46:40 +03:00
|
|
|
Count
|
2015-01-07 22:29:31 +03:00
|
|
|
};
|
|
|
|
|
2015-01-12 22:46:40 +03:00
|
|
|
inline const char* domain_name(MemoryDomain domain)
|
2015-01-07 22:29:31 +03:00
|
|
|
{
|
2015-01-12 22:46:40 +03:00
|
|
|
switch (domain)
|
|
|
|
{
|
|
|
|
case MemoryDomain::Undefined: return "Undefined";
|
|
|
|
case MemoryDomain::String: return "String";
|
2015-01-15 16:54:38 +03:00
|
|
|
case MemoryDomain::SharedString: return "SharedString";
|
2015-01-12 22:46:40 +03:00
|
|
|
case MemoryDomain::BufferContent: return "BufferContent";
|
|
|
|
case MemoryDomain::BufferMeta: return "BufferMeta";
|
|
|
|
case MemoryDomain::Options: return "Options";
|
|
|
|
case MemoryDomain::Highlight: return "Highlight";
|
2015-01-21 16:35:46 +03:00
|
|
|
case MemoryDomain::Display: return "Display";
|
2015-01-12 22:46:40 +03:00
|
|
|
case MemoryDomain::Mapping: return "Mapping";
|
|
|
|
case MemoryDomain::Commands: return "Commands";
|
|
|
|
case MemoryDomain::Hooks: return "Hooks";
|
|
|
|
case MemoryDomain::WordDB: return "WordDB";
|
2015-01-14 22:16:32 +03:00
|
|
|
case MemoryDomain::Aliases: return "Aliases";
|
|
|
|
case MemoryDomain::EnvVars: return "EnvVars";
|
|
|
|
case MemoryDomain::Faces: return "Faces";
|
|
|
|
case MemoryDomain::Values: return "Values";
|
|
|
|
case MemoryDomain::Registers: return "Registers";
|
|
|
|
case MemoryDomain::Client: return "Client";
|
2015-01-16 16:58:21 +03:00
|
|
|
case MemoryDomain::Selections: return "Selections";
|
2015-01-29 01:33:29 +03:00
|
|
|
case MemoryDomain::History: return "History";
|
2016-08-31 21:33:02 +03:00
|
|
|
case MemoryDomain::Remote: return "Remote";
|
2016-11-28 16:59:55 +03:00
|
|
|
case MemoryDomain::Events: return "Events";
|
|
|
|
case MemoryDomain::Completion: return "Completion";
|
2015-01-12 22:46:40 +03:00
|
|
|
case MemoryDomain::Count: break;
|
|
|
|
}
|
|
|
|
kak_assert(false);
|
|
|
|
return "";
|
|
|
|
}
|
2015-01-07 22:29:31 +03:00
|
|
|
|
2015-01-12 22:46:40 +03:00
|
|
|
extern size_t domain_allocated_bytes[(size_t)MemoryDomain::Count];
|
2015-01-07 22:29:31 +03:00
|
|
|
|
2015-01-18 21:49:32 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-07 22:29:31 +03:00
|
|
|
template<typename T, MemoryDomain domain>
|
|
|
|
struct Allocator
|
|
|
|
{
|
|
|
|
using value_type = T;
|
2015-01-09 16:57:21 +03:00
|
|
|
// 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;
|
2015-01-07 22:29:31 +03:00
|
|
|
|
|
|
|
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;
|
2015-01-18 21:49:32 +03:00
|
|
|
on_alloc(domain, size);
|
|
|
|
return reinterpret_cast<T*>(::operator new(size));
|
2015-01-07 22:29:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(T* ptr, size_t n)
|
|
|
|
{
|
|
|
|
size_t size = sizeof(T) * n;
|
2015-01-18 21:49:32 +03:00
|
|
|
on_dealloc(domain, size);
|
|
|
|
::operator delete(ptr);
|
2015-01-07 22:29:31 +03:00
|
|
|
}
|
2015-01-14 01:56:51 +03:00
|
|
|
|
|
|
|
template<class U, class... Args>
|
2015-06-26 23:32:37 +03:00
|
|
|
[[gnu::always_inline]]
|
2015-01-14 01:56:51 +03:00
|
|
|
void construct(U* p, Args&&... args)
|
|
|
|
{
|
2015-01-14 03:01:20 +03:00
|
|
|
new ((void*)p) U(std::forward<Args>(args)...);
|
2015-01-14 01:56:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class U>
|
2015-06-26 23:32:37 +03:00
|
|
|
[[gnu::always_inline]]
|
2015-01-14 01:56:51 +03:00
|
|
|
void destroy(U* p) { p->~U(); }
|
2015-01-07 22:29:31 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:59:18 +03:00
|
|
|
|
|
|
|
constexpr MemoryDomain memory_domain(Meta::AnyType) { return MemoryDomain::Undefined; }
|
|
|
|
|
2015-01-16 16:58:21 +03:00
|
|
|
template<typename T>
|
2017-05-26 09:59:18 +03:00
|
|
|
constexpr decltype(T::Domain) memory_domain(Meta::Type<T>) { return T::Domain; }
|
2015-01-16 16:58:21 +03:00
|
|
|
|
2015-01-18 21:49:32 +03:00
|
|
|
template<MemoryDomain d>
|
|
|
|
struct UseMemoryDomain
|
|
|
|
{
|
2015-01-21 16:35:23 +03:00
|
|
|
static constexpr MemoryDomain Domain = d;
|
2015-01-18 21:49:32 +03:00
|
|
|
static void* operator new(size_t size)
|
|
|
|
{
|
2015-01-21 16:35:23 +03:00
|
|
|
on_alloc(Domain, size);
|
2015-01-18 21:49:32 +03:00
|
|
|
return ::operator new(size);
|
|
|
|
}
|
|
|
|
|
2015-01-21 16:35:23 +03:00
|
|
|
static void* operator new(size_t size, void* ptr)
|
|
|
|
{
|
|
|
|
return ::operator new(size, ptr);
|
|
|
|
}
|
|
|
|
|
2015-01-18 21:49:32 +03:00
|
|
|
static void operator delete(void* ptr, size_t size)
|
|
|
|
{
|
2015-01-21 16:35:23 +03:00
|
|
|
on_dealloc(Domain, size);
|
2015-01-18 21:49:32 +03:00
|
|
|
::operator delete(ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-07 22:29:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // memory_hh_INCLUDED
|