title: Unix: sharing already-mapped memory between processes
body: <p>I have a pre-built userspace library that has an API along the lines of</p>
  
  <pre><code>void getBuffer (void **ppBuf, unsigned long *pSize);
  void bufferFilled (void *pBuf, unsigned long size);
  </code></pre>
  
  <p>The idea being that my code requests a buffer from the lib, fills it with stuff, then hands it back to the lib.</p>
  
  <p>I want another process to be able to fill this buffer. I can do this by creating some new shared buffer via shm*/shm_* APIs, have the other process fill that, then copy it to the lib's buffer in the lib's local process, but this has the overhead of an extra (potentially large) copy.</p>
  
  <p>Is there a way to share memory that has ALREADY been mapped for a process? eg something like:</p>
  
  <pre><code>[local lib process]
  getBuffer (&amp;myLocalBuf, &amp;mySize);
  shmName = shareThisMemory (myLocalBuf, mySize);
  
  [other process]
  myLocalBuf = openTheSharedMemory (shmName);
  </code></pre>
  
  <p>That way the other process could write directly into the lib's buffer.
  (Synchronization between the processes is already taken care of so no problems there).</p>