Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating std::scoped_memory_adapter aliases #38

Closed
adriansmares opened this issue Sep 25, 2018 · 1 comment
Closed

Creating std::scoped_memory_adapter aliases #38

adriansmares opened this issue Sep 25, 2018 · 1 comment

Comments

@adriansmares
Copy link

Currently the allocator aliases such as memory::unordered_map use directly memory::std_allocator<>.

However, if one wants to nest unordered_maps for example, using the rather intuitive memory::unordered_map<K1, memory::unordered_map<K2, V2>> leads to headaches since the [] operator requires the default constructor for the K2, V2 map to be called, which can't happen since we need to provide the allocator during creation.

Inside my code I've fixed this ('I've duplicated too much code' in this case) using the following std::scoped_allocator_adapter:

using memory_pool_collection = memory::memory_pool_collection<
		memory::node_pool,
		memory::log2_buckets>;

using unordered_set_int = std::unordered_set<int, std::hash<int>, std::equal_to<>,
		memory::std_allocator<int, memory_pool_collection, memory::default_mutex>>;

using unordered_map_int_si = std::unordered_map<int, unordered_set_int, std::hash<int>, std::equal_to<int>,
		std::scoped_allocator_adaptor<
				memory::std_allocator<std::pair<const int, unordered_set_int>, memory_pool_collection, memory::default_mutex>>>;

My proposal is that inside the container aliases header we could add something like

template <typename Key, typename Value, class RawAllocator, class Mutex = default_mutex>
FOONATHAN_ALIAS_TEMPLATE(
    unordered_map_scoped,
    std::unordered_map<Key, Value, std::hash<Key>, std::equal_to<Key>,
                       std::scoped_allocator_adapter<std_allocator<std::pair<const Key, Value>, RawAllocator, Mutex>>>);

This way the outermost allocator could be a scoped one, while the inside ones would still keep on being the normal versions. What I'm not entirely sure for now is if nesting 3 containers (so, map, in a map, in a map), requires the second container to be scoped or not.

@foonathan
Copy link
Owner

This is a good idea, yes. I've added them.

What I'm not entirely sure for now is if nesting 3 containers (so, map, in a map, in a map), requires the second container to be scoped or not.

They don't need to be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants