roman
roman

Reputation: 5210

Pass allocated memory using memory map

I'm writing memory manager for Windows in c++. I've created a class that allocates memory and passes it back to the client as void*. I've overriden new and delete operators to use my allocator.

__forceinline void * operator new(size_t n)
{ 
    EnterCriticalSection(&CriticalSection);
    void *ret = Heap.Alloc(n); 
    LeaveCriticalSection(&CriticalSection);
    return ret;
}

There are several threads that turn to allocator process asking for some amount of memory. Is there any possibility to pass this allocated memory using memory map or something like that to be able to pass memory between processes? Is there any way to just pass void* to another process to use allocated memory there?

Upvotes: 0

Views: 218

Answers (2)

Felice Pollano
Felice Pollano

Reputation: 33252

You can't just pass the void* since each process has its own address space and an address in one has no meaning in the other. Have a look at this question, it seems to fulfill your needings

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 596632

Using a memory map is the correct way to share a memory block across processes. Look at the CreateFileMapping() and MapViewOfFile() functions. Give your memory map a unique Name that both processes specify when calling CreateFileMapping().

You cannot pass a void* pointer itself between processes. A pointer is local to the calling process's address space. Different processes have their own individual address spaces. However, if the void* is backed by a memory map then both processes can access the shared memory by mapping their own local void* pointers to the same named memory map. You can then pass offsets within that memory back and forth between your processes as needed, and they can apply those offsets to their local pointers to access the same data.

Upvotes: 1

Related Questions