How to keep a pointer on a shared data in a boost shared memory segment

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to keep a pointer on a shared data in a boost shared memory segment

Boost - Users mailing list
Hi everyone !
How to keep a pointer on a shared data in a boost shared memory segment ? I have a function which returns shm.construct<SharedData>(_nameSeg.c_str())(innerDataAllocator); But outside of the function, the object pointed by SharedData is inaccessible
My general problem is that I want to have way to get, and set data in shared memory, but without, each time, having to :
-get shm
-find object in shm based on its name
-construct allocators.
So I would like to store 1) allocators and 2)a pointer to the shared object
How could I do this ?
I made a SO question about my problem, can you have a look ? It should be clearer :) https://stackoverflow.com/questions/55195149/how-to-keep-a-pointer-on-a-shared-data-in-a-boost-shared-memory-segment


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: How to keep a pointer on a shared data in a boost shared memory segment

Boost - Users mailing list
On 21/03/2019 01:03, jucin loic wrote:

> How to keep a pointer on a shared data in a boost shared memory segment
> ? I have a function which returns
> shm.construct<SharedData>(_nameSeg.c_str())(innerDataAllocator); But
> outside of the function, the object pointed by SharedData is inaccessible
> My general problem is that I want to have way to get, and set data in
> shared memory, but without, each time, having to :
> -get shm
> -find object in shm based on its name
> -construct allocators.
> So I would like to store 1) allocators and 2)a pointer to the shared object
> How could I do this ?

In general it makes no sense to store pointers inside shared memory,
because it is very likely that each process maps the shared memory to
different addresses, so a pointer would point to invalid memory in all
but one process.

To solve this, you store indexes or offsets relative to the base of the
shared memory itself (or some other known field within the shared
memory) instead of a "real" pointer.

Some compilers support a so-called "based pointer", which let you use
offsets as if they were pointers, but this is not portable.  You can
accomplish something similar in portable code by using a smart pointer
wrapper type.
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: How to keep a pointer on a shared data in a boost shared memory segment

Boost - Users mailing list
Hi,

> To solve this, you store indexes or offsets relative to the base of the shared memory itself (or some other known field within the shared memory) instead of a "real" pointer.

I once saw a system, where pointers in shared memory were self-relative. It should be possible to wrap them in a pointer-like class.

73, Mario

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: How to keep a pointer on a shared data in a boost shared memory segment

Boost - Users mailing list
Yes, this is a good idea, but you got wrong on my real problem:
I agree, and but I don't want to share the pointers between different process. I want to get the pointer to the shared data using managed_shared_memory::construct() or managed_shared_memory::find() and then store this pointer. And do this for of course all different processes.
Please, read the SO question, I described there more precisely my problem, and even provided a READY-TO-USE example (https://stackoverflow.com/questions/55195149/how-to-keep-a-pointer-on-a-shared-data-in-a-boost-shared-memory-segment
 Thank you in advance !

Le jeu. 21 mars 2019 à 14:10, Klebsch, Mario via Boost-users <[hidden email]> a écrit :
Hi,

> To solve this, you store indexes or offsets relative to the base of the shared memory itself (or some other known field within the shared memory) instead of a "real" pointer.

I once saw a system, where pointers in shared memory were self-relative. It should be possible to wrap them in a pointer-like class.

73, Mario

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: How to keep a pointer on a shared data in a boost shared memory segment

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
On 20/03/2019 13:03, jucin loic via Boost-users wrote:

> Hi everyone !
> How to keep a pointer on a shared data in a boost shared memory segment
> ? I have a function which returns
> shm.construct<SharedData>(_nameSeg.c_str())(innerDataAllocator); But
> outside of the function, the object pointed by SharedData is inaccessible
> My general problem is that I want to have way to get, and set data in
> shared memory, but without, each time, having to :
> -get shm
> -find object in shm based on its name
> -construct allocators.
> So I would like to store 1) allocators and 2)a pointer to the shared object
> How could I do this ?
> I made a SO question about my problem, can you have a look ? It should
> be clearer :)
> https://stackoverflow.com/questions/55195149/how-to-keep-a-pointer-on-a-shared-data-in-a-boost-shared-memory-segment

I'm afraid I don't understand the problem, and your minimal example
needs password. Could you send a single cpp file with the minimal code
to understand your question?

Best,

Ion
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: How to keep a pointer on a shared data in a boost shared memory segment

Boost - Users mailing list
Yes, forgot to give the password, it's simply: stackoverflow
In the minimal example is only a signle cpp, that you can also find attached. 

Thanks :)

Jucin Loic

Le dim. 24 mars 2019 à 01:49, Ion Gaztañaga via Boost-users <[hidden email]> a écrit :
On 20/03/2019 13:03, jucin loic via Boost-users wrote:
> Hi everyone !
> How to keep a pointer on a shared data in a boost shared memory segment
> ? I have a function which returns
> shm.construct<SharedData>(_nameSeg.c_str())(innerDataAllocator); But
> outside of the function, the object pointed by SharedData is inaccessible
> My general problem is that I want to have way to get, and set data in
> shared memory, but without, each time, having to :
> -get shm
> -find object in shm based on its name
> -construct allocators.
> So I would like to store 1) allocators and 2)a pointer to the shared object
> How could I do this ?
> I made a SO question about my problem, can you have a look ? It should
> be clearer :)
> https://stackoverflow.com/questions/55195149/how-to-keep-a-pointer-on-a-shared-data-in-a-boost-shared-memory-segment

I'm afraid I don't understand the problem, and your minimal example
needs password. Could you send a single cpp file with the minimal code
to understand your question?

Best,

Ion
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users

main.cpp (4K) Download Attachment