英文:
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(&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<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
...
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]);
}
...
Sure, the standard way to make this scenario works is
- to get the integer array
- allocate a piece of memory
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论