From e8b80705363e736940b2487f7158b4a5d21ddd55 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 12 Jan 2015 19:46:40 +0000 Subject: [PATCH] refactor slighly memory domain handling --- src/commands.cc | 33 +++++++-------------------------- src/memory.cc | 8 ++++++++ src/memory.hh | 35 +++++++++++++++++++++++++---------- 3 files changed, 40 insertions(+), 36 deletions(-) create mode 100644 src/memory.cc diff --git a/src/commands.cc b/src/commands.cc index 71ecd221..5fc53657 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -826,33 +826,14 @@ const CommandDesc debug_cmd = { } else if (parser[0] == "memory") { - auto string = UsedMemory::byte_count; - auto interned_string = UsedMemory::byte_count; - auto buffer_content = UsedMemory::byte_count; - auto buffer_meta = UsedMemory::byte_count; - auto options = UsedMemory::byte_count; - auto highlight = UsedMemory::byte_count; - auto word_db = UsedMemory::byte_count; - auto mapping = UsedMemory::byte_count; - auto commands = UsedMemory::byte_count; - auto hooks = UsedMemory::byte_count; - auto undefined = UsedMemory::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)); } diff --git a/src/memory.cc b/src/memory.cc new file mode 100644 index 00000000..e2928412 --- /dev/null +++ b/src/memory.cc @@ -0,0 +1,8 @@ +#include "memory.hh" + +namespace Kakoune +{ + +size_t domain_allocated_bytes[(size_t)MemoryDomain::Count] = {}; + +} diff --git a/src/memory.hh b/src/memory.hh index 670da739..7da3486e 100644 --- a/src/memory.hh +++ b/src/memory.hh @@ -20,17 +20,32 @@ enum class MemoryDomain Mapping, Commands, Hooks, - WordDB + WordDB, + Count }; -template -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 -size_t UsedMemory::byte_count = 0; +extern size_t domain_allocated_bytes[(size_t)MemoryDomain::Count]; template struct Allocator @@ -54,15 +69,15 @@ struct Allocator T* allocate(size_t n) { size_t size = sizeof(T) * n; - UsedMemory::byte_count += size; + domain_allocated_bytes[(int)domain] += 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; + kak_assert(domain_allocated_bytes[(int)domain] >= size); + domain_allocated_bytes[(int)domain] -= size; free(ptr); } };