Comments (3)
I need to look into that.
For me it also seems that there is no hashing for strings with custom allocators.
from memory.
Okay, I think I found the issue.
The operator[]
of map first needs to create a default constructed key-value pair. However, my::string
uses a stateful allocator and stateful allocators aren't default constructible, so it fails. You just can't use operator[]
, just like you can't use it if you have a "normal" non-default constructible type.
However, the error message could be nicer.
from memory.
First of all, thanks for looking into this! Following posting the issue yesterday, I started looking into allocators in a bit more depth. I found this talk from CPPCon2014 on the subject:
Making Allocators Work - Part 1
Making Allocators Work - Part 2
And there is a bit about building container of non trivial objects and how one should go about and do it. He mentions std::scoped_allocator_adaptor<...>
which are precisely there to inject the allocator of the container in the constructor of the contained item by highjacking the construct
method of the allocator. His example is a vector<string>
allocated using a shared memory allocator which is should both be used to allocate the vector
memory as well as the string
memory.
So this morning, I thought I'd just use that and it should work. So I rewrote the type system as follows:
template<typename T>
using allocator = std::scoped_allocator_adaptor<foonathan::memory::std_allocator<T, my::raw_stat_mallocator>>;
template<typename C>
using basic_string = std::basic_string<C, std::char_traits<C>, allocator<C>>;
using string = std::basic_string<char>;
template<typename K, typename V>
using hash_map = std::unordered_map<K, V, std::hash<K>, std::equal_to<K>, allocator<std::pair<const K, V>>>;
But that does not compile either (something about alloc_reference
not being able to access a private base class or something). However, trying some more random stuff, I simply removed the scoped_allocator_adaptor
from the above type system. And hey, it works. I'm still puzzled, because it looks like I'm just redefining what's done in Container.hpp
, but obviously there is subtle difference that eludes me at the moment. I'll continue digging.
Anywho, here's what I've got so far:
// ... same as above
template<typename T>
using allocator = foonathan::memory::std_allocator<T, my::raw_stat_mallocator>;
template<typename C>
using basic_string = std::basic_string<C, std::char_traits<C>, allocator<C>>;
using string = std::basic_string<char>;
template<typename K, typename V>
using hash_map = std::unordered_map<K, V, std::hash<K>, std::equal_to<K>, allocator<std::pair<const K, V>>>;
}
int main()
{
auto ral = my::raw_stat_mallocator();
auto m1 = my::hash_map<int, my::string>(ral);
auto m2 = my::hash_map<my::string, int>(ral);
// both of these work fine and (added bonus) I don't have
// to explicitly instantiate the strings with ral :-)
m1[2] = "hello";
m2["hello"] = 2;
return EXIT_SUCCESS;
}
which produced the following output, which seems reasonable to me:
[ 0x7fff5c438430 ] Allocating 48 bytes (0 total, 0 current)
[ 0x7fff5c438430 ] Allocating 16 bytes (48 total, 48 current)
[ 0x7fff5c438430 ] Allocating 48 bytes (64 total, 64 current)
[ 0x7fff5c438430 ] Allocating 16 bytes (112 total, 112 current)
[ 0x7fff5c438430 ] Deallocating 48 bytes (128 total, 128 current)
[ 0x7fff5c438430 ] Deallocating 16 bytes (128 total, 80 current)
[ 0x7fff5c438430 ] Deallocating 48 bytes (128 total, 64 current)
[ 0x7fff5c438430 ] Deallocating 16 bytes (128 total, 16 current)
from memory.
Related Issues (20)
- `memory_pool_collection` carves too little memory from block if there are many buckets HOT 4
- [docs/examples] associative containers HOT 4
- memory_block_stack::owns() seems wrong? HOT 1
- Time for a new release? HOT 1
- problem with std::is_abstract for unique_ptr with allocator inside struct definition HOT 1
- give allocator_polymorphic_deleter a default constructor HOT 1
- Thread safety
- Build issues on MacOS while building tools with static flag HOT 3
- MinGW build fails due to "#warning" HOT 1
- Can a vector use a memory pool with a small_node_pool ?
- Unable to determine node size of C++ container HOT 4
- ARM64 crash when using static block allocator with small types HOT 2
- memory_pool_collection fails to allocate memory HOT 4
- Test fails on Android arm64-v8a
- memory pool allocate
- Wrap a memory_pool in a thread_safe_allocator? HOT 1
- Calling pure virtual function. HOT 1
- Page (4KiB) alignment for memory pool
- a new release/tag ?
- compiler error ( from warning) when compiled with FOONATHAN_MEMORY_DEBUG_ASSERT and FOONATHAN_MEMORY_ASSERT not defined HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from memory.