From dcc23765b49489bb7ab5242f36db15eaff309e72 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Tue, 8 Nov 2016 18:50:37 -0500 Subject: [PATCH] mempool: add [de]allocate_aligned Signed-off-by: Sage Weil --- src/include/mempool.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/include/mempool.h b/src/include/mempool.h index aee5b0f3cd7d3..7414ff2f08b4e 100644 --- a/src/include/mempool.h +++ b/src/include/mempool.h @@ -307,6 +307,33 @@ public: delete[] reinterpret_cast(p); } + T* allocate_aligned(size_t n, size_t align, void *p = nullptr) { + size_t total = sizeof(T) * n; + shard_t *shard = pool->pick_a_shard(); + shard->bytes += total; + shard->items += n; + if (type) { + type->items += n; + } + char *ptr; + int rc = ::posix_memalign((void**)(void*)&ptr, align, total); + if (rc) + throw std::bad_alloc(); + T* r = reinterpret_cast(ptr); + return r; + } + + void deallocate_aligned(T* p, size_t n) { + size_t total = sizeof(T) * n; + shard_t *shard = pool->pick_a_shard(); + shard->bytes -= total; + shard->items -= n; + if (type) { + type->items -= n; + } + ::free(p); + } + void destroy(T* p) { p->~T(); } -- 2.39.5