How can I use unique_ptr with container and string in boost::interprocess?

Hi, I have a complex data structure that wants to put into shared memory. In STL way it should be: list<tuple<string, string, map<string, string>>> As you can see, there are a lot of strings which will be put into shared memory. My current way to a string into a container is (I use list<string> as a example): typedef allocator<char, managed_windows_shared_memory::segment_manager> CharAllocator; typedef basic_string<char, std::char_traits<char>, CharAllocator> SharedMemoryString; typedef allocator<SharedMemoryString, managed_windows_shared_memory::segment_manager> PushValueAllocator; typedef list<PushValue, PushValueAllocator> PushQueue; In this way, I always must construct a SharedMemoryString first then put it into container: SharedMemoryString mStr(charAlloc); mStr = "some stirng..."; queue->push_back(mStr); In my understanding, this will cost twice memory allocations to store this string into shared memory. First time is mStr = "some string...", second time is that mStr is put into list container, then mStr's memory will be destructed after leaving the scope. This means if the string length is 1KB, my shared memory size at least must be 2 * 1KB + container overhead. This seems stupid because twice memory allocation and copy operation. So, I want to use unique_ptr to solve this problem. I try: typedef managed_unique_ptr<SharedMemoryString, managed_windows_shared_memory>::type StringUniquePtr; typedef allocator<StringUniquePtr, managed_windows_shared_memory::segment_manager> PushValueAllocator; typedef list<PushValue, PushValueAllocator> PushQueue; SharedMemoryString* mStr = new SharedMemoryString(charAlloc); *mStr = "some stirng..."; queue->push_back(StringUniquePtr(make_managed_unique_ptr(mStr, segment))); But it can't compile... :( I have no idea to make unique_ptr to work with string in container in boost::interprocess. How do I make this work? Thank you very much. -- View this message in context: http://www.nabble.com/How-can-I-use-unique_ptr-with-container-and-string-in-... Sent from the Boost - Users mailing list archive at Nabble.com.

blp330 wrote:
Hi,
I have a complex data structure that wants to put into shared memory. In STL way it should be:
... In this way, I always must construct a SharedMemoryString first then put it into container: SharedMemoryString mStr(charAlloc); mStr = "some stirng..."; queue->push_back(mStr);
In my understanding, this will cost twice memory allocations to store this string into shared memory. First time is mStr = "some string...", second time is that mStr is put into list container, then mStr's memory will be destructed after leaving the scope.
I have no compiler at hand to test the unique_ptr issue, but you can use move semantics to avoid memory allocations: //transfer resources from mStr to the newly //created string queue->push_back(move(mStr)); should work. Also: SharedMemoryString mStr2(charAlloc); queue->push_back(mStr2); queue.back().swap(mStr); Regards, Ion
participants (2)
-
blp330
-
Ion Gaztañaga