“unmanaged” shared memory pointer using boost interprocess是否可以共享?

huangapple go评论71阅读模式
英文:

is it possible to share an "unmanaged" shared memory pointer using boost interprocess?

问题

My case is very rare.
我这个情况非常罕见。

I have a library called libtest.so. It has a function called void test1(int **p);
我有一个名为libtest.so的库。它有一个名为void test1(int **p)的函数。

I can get the integer array by this way
我可以通过这种方式获取整数数组

int p = NULL;
test0(&p);
int p = NULL;
test0(&p);

For some reason, I don't want to dlopen() it in process A.
由于某种原因,我不想在进程A中使用dlopen()加载它。

Instead, I dlopen("libtest.so") in process B and create a managed_shared_memory.
相反,我在进程B中使用dlopen("libtest.so")并创建了一个managed_shared_memory。

The process A connects to the managed_shared_memory.
进程A连接到managed_shared_memory。

The process B calls the test0(&p) to obtain an integer array.
进程B调用test0(&p)来获取整数数组。

It is possible to share this pointer p(unmanaged shared memory) from process B to process A ?
是否可能从进程B将这个指针p(非托管共享内存)共享给进程A?

I tried to pass the addr of p by pointer of pointer of the managed_shared_memory from process B to process A. But It didn't work.
我尝试通过在进程B中使用managed_shared_memory的指针传递指针p的地址到进程A,但没有成功。

// process B
// 进程B
struct shm_object
{
offset_ptr<int *> pp;
shm_object() :{};
};
...
shm = managed_shared_memory(open_or_create, INTERPROCESS_MEMORY_NAME,MANAGED_MEMORY_INITIAL_SIZE);
s_mem_object = shm.find_or_construct<shm_object>(SHARED_MEMORY_OBJECT_1_NAME)

int p;
test0(&p);

s_mem_object->pp = static_cast<int **>(shm.allocate(sizeof(int *)));
int **pp = static_cast<int **>(s_mem_object->p.get());

pp[0] = p; // save address of the p to pp
...
// process A
// 进程A
...
int pp = static_cast<int>(s_mem_object->pp.get());
for(int i=0; i<4; i++)
{
printf("from process-A %d\n", (*pp)[i]);
}
...
但是,在实际情况下,内存结构复杂。需要在深拷贝的实现上投入大量工作。

Is there any workaround ?
有没有任何解决方法?

英文:

My case is very rare.
I have a library called libtest.so. It has a function called void test1(int **p);

void test1(int **p)
{
   *p = (int *) malloc(4 * sizeof(int));
}

I can get the integer array by this way

int p = NULL;
test0(&amp;p);

For some reason, I don't want to dlopen() it in process A.
Instead, I dlopen("libtest.so") in process B and create a managed_shared_memory.
The process A connects to the managed_shared_memory.

The process B calls the test0(&p) to obtain an integer array.
It is possible to share this pointer p(unmanaged shared memory) from process B to process A ?

I tried to pass the addr of p by pointer of pointer of the managed_shared_memory from process B to process A. But It didn't work.

// process B
struct shm_object
{
    offset_ptr&lt;int *&gt; pp;
	shm_object() :{};
};
...
shm = managed_shared_memory(open_or_create, INTERPROCESS_MEMORY_NAME,MANAGED_MEMORY_INITIAL_SIZE);
s_mem_object = shm.find_or_construct&lt;shm_object&gt;(SHARED_MEMORY_OBJECT_1_NAME)

int p;
test0(&amp;p);

s_mem_object-&gt;pp = static_cast&lt;int **&gt;(shm.allocate(sizeof(int *)));
int **pp = static_cast&lt;int **&gt;(s_mem_object-&gt;p.get());

pp[0] = p;   // save address of the p to pp
...
// process A
...
 int **pp = static_cast&lt;int**&gt;(s_mem_object-&gt;pp.get());
 for(int i=0; i&lt;4; i++)
 {
    printf(&quot;from process-A %d\n&quot;, (*pp)[i]);
 }
...

Sure, the standard way to make this scenario works is

  1. to get the integer array
  2. allocate a piece of memory
  3. copy the integer array to the managed_shared_memory

But, it real case scenario, the memory structs are complicated. A lot of efforts have to be spent on the implementation of deep-copy.

Is there any workaround ?

答案1

得分: 0

如果test1无法成为分配器感知的对象,您不能可靠/有用地与另一个进程共享指针。

请注意,您可以将其交给另一个进程,并有用地接收回来。我建议将其发送为ptrdiff_t,而不是原始指针,以避免意外将其用作指针。

英文:

If test1 cannot be (made) allocator-aware you cannot reliably/usefully share a pointer with another process.

Note that you can hand it off to another process and receive it back usefully. I'd suggest to send it as ptrdiff_t instead of the raw pointer to avoid accidental use as a pointer.

huangapple
  • 本文由 发表于 2023年7月5日 00:23:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定