Re: shared memory malloc library



In article <1177644689.425916.288180@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
llothar <llothar@xxxxxx> wrote:
Sorry i mean of course base address not base directory.

On most systems you can't map virtual address to the same address
range as in another process - this will not work realiable. Thats why
you need to do all pointer operations in the heap code relative to the
base adresse which you get when you map in the shared memory.

[OT]
I don't know about Windows, but Unix systems (POSIX, including
MacOS), you can use mmap(), the first parameter to which is the
virtual address in the current process that you wish the memory to
(if you specify MAP_FIXED.) Since mmap() is system specific, one
usually knows enough about the standard memory layout to be able to
choose addresses that will not conflict with other mappings. This
is the same semantics as shmat() with a non-NULL address.


For the
client side of the library all this is invisible, you just need to
pass an additional parameter to malloc/realloc/free and it will be
slower due to the interprocess mutex.

I thought something like this is a common in the C world.

The memory so allocated cannot be used to store pointers that are
meaningful to all shared address spaces. In order to communicate the
location of the allocated objects between processes, you would have to
have the C code find the offset of the allocated object relative to the
beginning of the shared heap -- but in C, taking pointer differences
between different C objects is undefined behaviour. The implementation
would therefore not only need to provide malloc() and free()
equivilents in the heap: it would also need to provide a call that
returned a heap-relative offset given a pointer and a heap identifier,
and another call that converted a heap-relative offset and a heap
identifier into a local pointer.

Things are so much easier when you just map into the same virtual
address spaces using POSIX semantics -- changes to the shared segment
are automatically reflected in all attachments, without anything
equivilent to "shared memory flush" needed to update caches. If the
system is multiprocessor (or multi-core) but -not- cache-coherent
between the processors (or cores), it isn't clear to me that the system
would meet the POSIX semantic requirements.

But you still have the same difficulty about C having essentially no
atomic operations, so your program pretty much has to add in reliance
upon synchronization primitives -- and if this hypothetical
malloc-in-a-heap library does not want to require dependancy upon any
particular OS's synchronization primitives, then the library
is effectively going to have to provide synchronization primitives
itself.

Clearly effective operations with such a library isn't as simple as
just providing an additional parameter to *malloc() and kin.

Anyhow, the trials and tribulations of shared memory are not
considered appropriate topics for comp.lang.c, since proper operation
of shared memory inherently relies upon system operations
which C knows nothing about (and does not want to know anything
about, because they are not portable to a number of the systems
that C is expected to be able to run on.)
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
.



Relevant Pages

  • Re: data structures on shared memory?
    ... still can't get nested data structures to work. ... If you're allocating stuff from the heap, ... IN SHARED MEMORY*, therefore *IT'S NOT SHARED*. ... just use one pointer and increment the "used up to" mark, ...
    (comp.unix.programmer)
  • Re: shared memory malloc library
    ... llothar wrote: ... i need to manage a heap in shared memory. ... Simply use malloc. ...
    (comp.lang.c)
  • Re: static, global variable memory allocation
    ... for which the term "heap" may or may not be applicable ... freed is the memory that was allocated by malloc. ... As long as we're being precise, malloc does not return a memory area, ... and that pointer is assigned to m. ...
    (comp.lang.c)
  • Re: stack and heap
    ... mallocalways takes memory in the heap. ... machine, say, could allocate them in the heap and garbage-collect them at ... can call the memory managed by the malloc family "the heap" if you like. ... The function `malloc` returns either null or a pointer to the ...
    (comp.programming)
  • Re: malloc realloc and pointers
    ... When we perform malloc(), the memory allocated dynamically comes from ... Technically, C doesn't define "heap". ... reallocreturns the same pointer, in which case ptr is still ...
    (comp.lang.c)