Persisting structures using pmem.io C++

35 views
Skip to first unread message

Abhishek Kumar

unread,
Nov 12, 2016, 12:47:52 AM11/12/16
to pmem
Hello
I am new to pmem.io library, trying to persist a structure which has two std::strings as members. But am unable to do so. Can someone please tell me what might be the best way to do so? Most of the examples I have come across, do it the C way, persisting each member of structure manually using pmemobj_memcpy_persist. Presence of Char * pointers makes this ( http://pmem.io/2016/01/12/cpp-02.html ) tutorial not work for me. Please suggest some other alternative for persisting structure with strings in C++.


Thanks
Abhishek Kumar

Tomasz Kapela

unread,
Nov 14, 2016, 10:14:07 AM11/14/16
to Abhishek Kumar, pmem
Hi,
std::string is not that easy to persist properly, mainly because it is a standard library container which has its own allocator and many optimizations, which differ between implementations. As such it has been a thorn in our side, because we know how often it is used. We are working on enabling standard library containers to work with persistent memory and hopefully the std::string as well (in one form or another). As for a quick solution to your problem, I would suggest writing a simple string implementation based on this trivial example:

Depending on your usage I would also suggest some kind of short string optimization, something along the lines of:

class str {

public:

                str(const std::string &rhs) {

                                strncpy(sso, rhs.c_str(), 50);

                }

 

                str &operator=(const std::string &rhs) {

                                pmemobj_tx_add_range_direct(sso, 50);

                                strncpy(sso, rhs.c_str(), 50);

                                return *this;

                }

private:

                char sso[50];

};


Tom

--
You received this message because you are subscribed to the Google Groups "pmem" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pmem+uns...@googlegroups.com.
To post to this group, send email to pm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pmem/10a7ab2c-4224-400f-9197-3859e15d5321%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages