kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

memory.hh (4483B)


      1 #ifndef memory_hh_INCLUDED
      2 #define memory_hh_INCLUDED
      3 
      4 #include <cstddef>
      5 #include <new>
      6 
      7 #include "assert.hh"
      8 #include "meta.hh"
      9 
     10 namespace Kakoune
     11 {
     12 
     13 enum class MemoryDomain
     14 {
     15     Undefined,
     16     String,
     17     SharedString,
     18     BufferContent,
     19     BufferMeta,
     20     Options,
     21     Highlight,
     22     Regions,
     23     Display,
     24     Mapping,
     25     Commands,
     26     Hooks,
     27     Aliases,
     28     EnvVars,
     29     Faces,
     30     Values,
     31     Registers,
     32     Client,
     33     WordDB,
     34     Selections,
     35     Remote,
     36     Events,
     37     Completion,
     38     Regex,
     39     Count
     40 };
     41 
     42 inline const char* domain_name(MemoryDomain domain)
     43 {
     44     switch (domain)
     45     {
     46         case MemoryDomain::Undefined: return "Undefined";
     47         case MemoryDomain::String: return "String";
     48         case MemoryDomain::SharedString: return "SharedString";
     49         case MemoryDomain::BufferContent: return "BufferContent";
     50         case MemoryDomain::BufferMeta: return "BufferMeta";
     51         case MemoryDomain::Options: return "Options";
     52         case MemoryDomain::Highlight: return "Highlight";
     53         case MemoryDomain::Regions: return "Regions";
     54         case MemoryDomain::Display: return "Display";
     55         case MemoryDomain::Mapping: return "Mapping";
     56         case MemoryDomain::Commands: return "Commands";
     57         case MemoryDomain::Hooks: return "Hooks";
     58         case MemoryDomain::WordDB: return "WordDB";
     59         case MemoryDomain::Aliases: return "Aliases";
     60         case MemoryDomain::EnvVars: return "EnvVars";
     61         case MemoryDomain::Faces: return "Faces";
     62         case MemoryDomain::Values: return "Values";
     63         case MemoryDomain::Registers: return "Registers";
     64         case MemoryDomain::Client: return "Client";
     65         case MemoryDomain::Selections: return "Selections";
     66         case MemoryDomain::Remote: return "Remote";
     67         case MemoryDomain::Events: return "Events";
     68         case MemoryDomain::Completion: return "Completion";
     69         case MemoryDomain::Regex: return "Regex";
     70         case MemoryDomain::Count: break;
     71     }
     72     kak_assert(false);
     73     return "";
     74 }
     75 
     76 struct MemoryStats
     77 {
     78     size_t allocated_bytes;
     79     size_t allocation_count;
     80     size_t total_allocation_count;
     81 };
     82 
     83 extern MemoryStats memory_stats[(size_t)MemoryDomain::Count];
     84 
     85 inline void on_alloc(MemoryDomain domain, size_t size)
     86 {
     87     auto& stats = memory_stats[(int)domain];
     88     stats.allocated_bytes += size;
     89     ++stats.allocation_count;
     90     ++stats.total_allocation_count;
     91 }
     92 
     93 inline void on_dealloc(MemoryDomain domain, size_t size)
     94 {
     95     auto& stats = memory_stats[(int)domain];
     96     kak_assert(stats.allocated_bytes >= size);
     97     stats.allocated_bytes -= size;
     98     --stats.allocation_count;
     99 }
    100 
    101 template<typename T, MemoryDomain domain>
    102 struct Allocator
    103 {
    104     using value_type = T;
    105 
    106     Allocator() = default;
    107     template<typename U>
    108     Allocator(const Allocator<U, domain>&) {}
    109 
    110     template<typename U>
    111     struct rebind { using other = Allocator<U, domain>; };
    112 
    113     T* allocate(size_t n)
    114     {
    115         size_t size = sizeof(T) * n;
    116         on_alloc(domain, size);
    117         return reinterpret_cast<T*>(::operator new(size));
    118     }
    119 
    120     void deallocate(T* ptr, size_t n)
    121     {
    122         size_t size = sizeof(T) * n;
    123         on_dealloc(domain, size);
    124         ::operator delete(ptr);
    125     }
    126 };
    127 
    128 template<typename T1, MemoryDomain d1, typename T2, MemoryDomain d2>
    129 constexpr bool operator==(const Allocator<T1, d1>&, const Allocator<T2, d2>&)
    130 {
    131     return d1 == d2;
    132 }
    133 
    134 constexpr MemoryDomain memory_domain(Meta::AnyType) { return MemoryDomain::Undefined; }
    135 
    136 template<typename T>
    137 constexpr decltype(T::Domain) memory_domain(Meta::Type<T>) { return T::Domain; }
    138 
    139 template<MemoryDomain d>
    140 struct UseMemoryDomain
    141 {
    142     static constexpr MemoryDomain Domain = d;
    143 
    144     [[gnu::always_inline]]
    145     static void* operator new(size_t size)
    146     {
    147         on_alloc(Domain, size);
    148         return ::operator new(size);
    149     }
    150 
    151     [[gnu::always_inline]]
    152     static void* operator new[](size_t size)
    153     {
    154         on_alloc(Domain, size);
    155         return ::operator new[](size);
    156     }
    157 
    158     [[gnu::always_inline]]
    159     static void* operator new(size_t size, void* ptr)
    160     {
    161         return ::operator new(size, ptr);
    162     }
    163 
    164     [[gnu::always_inline]]
    165     static void operator delete(void* ptr, size_t size)
    166     {
    167         on_dealloc(Domain, size);
    168         ::operator delete(ptr);
    169     }
    170 
    171     [[gnu::always_inline]]
    172     static void operator delete[](void* ptr, size_t size)
    173     {
    174         on_dealloc(Domain, size);
    175         ::operator delete[](ptr);
    176     }
    177 };
    178 
    179 }
    180 
    181 #endif // memory_hh_INCLUDED