From: Sage Weil Date: Thu, 20 Oct 2016 15:10:08 +0000 (-0500) Subject: mempool: use function static for pool table X-Git-Tag: v11.1.0~442^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F11331%2Fhead;p=ceph.git mempool: use function static for pool table The compiler/linker guarantee this is initialized before any invocation of this function... even if it is by a ctor in another compilation unit that is initialized by the mempool.cc compilation unit. Suggested by Bartłomiej Święcki Signed-off-by: Allen Samuels --- diff --git a/src/global/mempool.cc b/src/global/mempool.cc index f8f9927111f..33ea7beb4d8 100644 --- a/src/global/mempool.cc +++ b/src/global/mempool.cc @@ -21,35 +21,29 @@ bool mempool::debug_mode = false; // -------------------------------------------------------------- -// We rely on this array of pointers being zeroed when the process -// starts *before* the ctors from *any* linked modules are executed. -// That way, regardless of link order, pool_t's are allocated and -// instantiated on demand. -static mempool::pool_t *pools[mempool::num_pools]; - mempool::pool_t& mempool::get_pool(mempool::pool_index_t ix) { - if (pools[ix]) { - return *pools[ix]; - } + // We rely on this array being initialized before any invocation of + // this function, even if it is called by ctors in other compilation + // units that are being initialized before this compilation unit. + static mempool::pool_t table[num_pools]; + return table[ix]; +} - switch (ix) { -#define P(x) \ - case mempool_##x: pools[ix] = new mempool::pool_t(#x); break; - DEFINE_MEMORY_POOLS_HELPER(P); +const char *mempool::get_pool_name(mempool::pool_index_t ix) { +#define P(x) #x, + static const char *names[num_pools] = { + DEFINE_MEMORY_POOLS_HELPER(P) + }; #undef P - default: assert(0); - } - return *pools[ix]; + return names[ix]; } void mempool::dump(ceph::Formatter *f, size_t skip) { for (size_t i = skip; i < num_pools; ++i) { - if (!pools[i]) - continue; const pool_t &pool = mempool::get_pool((pool_index_t)i); - f->open_object_section(pool.get_name().c_str()); + f->open_object_section(get_pool_name((pool_index_t)i)); pool.dump(f); f->close_section(); } diff --git a/src/include/mempool.h b/src/include/mempool.h index d801801bb5a..da017bb9143 100644 --- a/src/include/mempool.h +++ b/src/include/mempool.h @@ -182,6 +182,7 @@ struct stats_t { }; pool_t& get_pool(pool_index_t ix); +const char *get_pool_name(pool_index_t ix); struct type_t { const char *type_name; @@ -196,27 +197,18 @@ struct type_info_hash { }; class pool_t { - std::string name; shard_t shard[num_shards]; mutable std::mutex lock; // only used for types list std::unordered_map type_map; public: - pool_t(const std::string& n) - : name(n) { - } - // // How much this pool consumes. O() // size_t allocated_bytes() const; size_t allocated_items() const; - const std::string& get_name() const { - return name; - } - shard_t* pick_a_shard() { // Dirt cheap, see: // http://fossies.org/dox/glibc-2.24/pthread__self_8c_source.html