diff --git a/build/ipc.pro b/build/ipc.pro index cad56ce..a11bccc 100644 --- a/build/ipc.pro +++ b/build/ipc.pro @@ -27,6 +27,7 @@ HEADERS += \ ../include/tls_pointer.h \ ../include/pool_alloc.h \ ../include/buffer.h \ + ../src/memory/detail.h \ ../src/memory/alloc.hpp \ ../src/memory/wrapper.hpp \ ../src/memory/resource.hpp \ diff --git a/src/ipc.cpp b/src/ipc.cpp index 0f18be1..8405414 100644 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -53,9 +53,9 @@ constexpr static queue_t* queue_of(ipc::handle_t h) { } static buff_t make_cache(void const * data, std::size_t size) { - auto ptr = mem::detail::pool_alloc::alloc(size); + auto ptr = mem::sync_pool_alloc::alloc(size); std::memcpy(ptr, data, size); - return { ptr, size, mem::detail::pool_alloc::free }; + return { ptr, size, mem::sync_pool_alloc::free }; } struct cache_t { diff --git a/src/memory/alloc.hpp b/src/memory/alloc.hpp index 957f341..b7ebdd0 100644 --- a/src/memory/alloc.hpp +++ b/src/memory/alloc.hpp @@ -264,5 +264,8 @@ using page_alloc = fixed_alloc<4096>; template using page_fixed_alloc = fixed_alloc; +template +using locked_fixed_alloc = fixed_alloc; + } // namespace mem } // namespace ipc diff --git a/src/memory/detail.h b/src/memory/detail.h new file mode 100644 index 0000000..c756b11 --- /dev/null +++ b/src/memory/detail.h @@ -0,0 +1,81 @@ +#pragma once + +#include + +#include "memory/alloc.hpp" +#include "platform/detail.h" + +namespace ipc { +namespace mem { +namespace detail { + +enum : std::size_t { + base_size = sizeof(void*) +}; + +template class FixedAlloc> +struct fixed { + static auto& pool() { + static FixedAlloc pool; + return pool; + } +}; + +#if __cplusplus >= 201703L +constexpr std::size_t classify(std::size_t size) { + constexpr std::size_t mapping[] = { +#else /*__cplusplus < 201703L*/ +inline std::size_t classify(std::size_t size) { + static const std::size_t mapping[] = { +#endif/*__cplusplus < 201703L*/ + /* 1 */ + 0 , 1 , 2 , 3 , + /* 2 */ + 5 , 5 , 7 , 7 , + 9 , 9 , 11, 11, + 13, 13, 15, 15, + /* 4 */ + 19, 19, 19, 19, + 23, 23, 23, 23, + 27, 27, 27, 27, + 31, 31, 31, 31 + }; + size = (size - 1) / base_size; +#if __cplusplus >= 201703L + return (size < std::size(mapping)) ? mapping[size] : 32; +#else /*__cplusplus < 201703L*/ + return (size < (sizeof(mapping) / sizeof(mapping[0]))) ? mapping[size] : 32; +#endif/*__cplusplus < 201703L*/ +} + +template