Wednesday, 28 August 2013

Sharing small data using Windows shared memory

Sharing small data using Windows shared memory

I'm looking for advice and suggestions for storing small amounts of data
(only a few bytes) in a shared memory segment on Windows.
Right now, whenever we map shared memory, we round the mapping size up to
the nearest 4KB so we get a multiple of pages mapped.
mem_size = ((mem_size + 4095) / 4096) * 4096;
However, I want to map enough memory to share a named integer between
processes. Or more specifically, many differently-named integers between
processes. About a thousand integers, all with different names and each
one mapped by one or more processes is what I'm looking at.
I don't want to round 4 bytes up to 4KB because this would be a huge
waste. After creating a thousand or so of these, I would have used about a
thousand pages when I only needed one.
But I am worried about the overhead of creating a named memory segment for
just 4 bytes. Will the OS be "reasonable" enough to try and fit the
different mappings onto the same page where possible? (There are no
guarantees, I know). Or will this quickly blow out?
I had considered mapping one huge block of memory, but the individual
integers still need to be referred to by name. Maintaining a hash table
inside shared memory seems like I'd just be duplicating the work of the
OS.
Is there an alternative to the CreateFileMapping/OpenFileMapping and
MapViewOfFile technique that would be more suited to sharing very small
amounts of data between processes? Or am I worrying over nothing?

No comments:

Post a Comment