refactor slighly memory domain handling

This commit is contained in:
Maxime Coste 2015-01-12 19:46:40 +00:00
parent 19797ae8d6
commit e8b8070536
3 changed files with 40 additions and 36 deletions

View File

@ -826,33 +826,14 @@ const CommandDesc debug_cmd = {
}
else if (parser[0] == "memory")
{
auto string = UsedMemory<MemoryDomain::String>::byte_count;
auto interned_string = UsedMemory<MemoryDomain::InternedString>::byte_count;
auto buffer_content = UsedMemory<MemoryDomain::BufferContent>::byte_count;
auto buffer_meta = UsedMemory<MemoryDomain::BufferMeta>::byte_count;
auto options = UsedMemory<MemoryDomain::Options>::byte_count;
auto highlight = UsedMemory<MemoryDomain::Highlight>::byte_count;
auto word_db = UsedMemory<MemoryDomain::WordDB>::byte_count;
auto mapping = UsedMemory<MemoryDomain::Mapping>::byte_count;
auto commands = UsedMemory<MemoryDomain::Commands>::byte_count;
auto hooks = UsedMemory<MemoryDomain::Hooks>::byte_count;
auto undefined = UsedMemory<MemoryDomain::Undefined>::byte_count;
auto total = string + interned_string + buffer_content + buffer_meta +
options + highlight + word_db + mapping + commands + hooks + undefined;
auto total = 0;
write_debug("Memory usage:");
write_debug("String: " + to_string(string));
write_debug("InternedString: " + to_string(interned_string));
write_debug("BufferContent: " + to_string(buffer_content));
write_debug("BufferMeta: " + to_string(buffer_meta));
write_debug("Options: " + to_string(options));
write_debug("Highlight: " + to_string(highlight));
write_debug("WordDB: " + to_string(word_db));
write_debug("Mapping: " + to_string(mapping));
write_debug("Commands: " + to_string(commands));
write_debug("Hooks: " + to_string(hooks));
write_debug("Undefined: " + to_string(undefined));
for (int domain = 0; domain < (int)MemoryDomain::Count; ++domain)
{
size_t count = domain_allocated_bytes[domain];
total += count;
write_debug(domain_name((MemoryDomain)domain) + (": " + to_string(count)));
}
write_debug("Total: " + to_string(total));
write_debug("Malloced: " + to_string(mallinfo().uordblks));
}

8
src/memory.cc Normal file
View File

@ -0,0 +1,8 @@
#include "memory.hh"
namespace Kakoune
{
size_t domain_allocated_bytes[(size_t)MemoryDomain::Count] = {};
}

View File

@ -20,17 +20,32 @@ enum class MemoryDomain
Mapping,
Commands,
Hooks,
WordDB
WordDB,
Count
};
template<MemoryDomain domain>
struct UsedMemory
inline const char* domain_name(MemoryDomain domain)
{
static size_t byte_count;
};
switch (domain)
{
case MemoryDomain::Undefined: return "Undefined";
case MemoryDomain::String: return "String";
case MemoryDomain::InternedString: return "InternedString";
case MemoryDomain::BufferContent: return "BufferContent";
case MemoryDomain::BufferMeta: return "BufferMeta";
case MemoryDomain::Options: return "Options";
case MemoryDomain::Highlight: return "Highlight";
case MemoryDomain::Mapping: return "Mapping";
case MemoryDomain::Commands: return "Commands";
case MemoryDomain::Hooks: return "Hooks";
case MemoryDomain::WordDB: return "WordDB";
case MemoryDomain::Count: break;
}
kak_assert(false);
return "";
}
template<MemoryDomain domain>
size_t UsedMemory<domain>::byte_count = 0;
extern size_t domain_allocated_bytes[(size_t)MemoryDomain::Count];
template<typename T, MemoryDomain domain>
struct Allocator
@ -54,15 +69,15 @@ struct Allocator
T* allocate(size_t n)
{
size_t size = sizeof(T) * n;
UsedMemory<domain>::byte_count += size;
domain_allocated_bytes[(int)domain] += 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;
kak_assert(domain_allocated_bytes[(int)domain] >= size);
domain_allocated_bytes[(int)domain] -= size;
free(ptr);
}
};