placement new and further

Discussion in 'C++' started by Dima Stopel, Aug 8, 2007.

  1. Dima Stopel

    Dima Stopel Guest

    Dear comp.lang.c++ members,

    I need to write an application that shares some object via shared memory
    with other applications.

    I use Memory-Mapped Files technique in order to share memory.

    Thus, in each process I have the *same* region of memory that is shared.

    All I need now is to put my object into this region.

    I use 'placement new' technique in order to put the object and all objects
    it creates in its constructor into this region.

    However, let's say that the object creates a hash_map in the constuctor. The
    hash_map is placed inside the region as needed, however if hash_map also
    creates some objects in *it's* constructor they won't be placed into the
    region and I have no control over it. Thus, the other applications which
    will try to call methods of my object will fail because those objects that
    hash_map creates are not inside the shared region.

    How can I ensure that the *whole* object with all it's sub-objects will be
    inside the shared region? Is it possible?

    Thank you in advance,
    Dima Stopel, Aug 8, 2007
    #1
    1. Advertising

  2. * Dima Stopel:
    > Dear comp.lang.c++ members,
    >
    > I need to write an application that shares some object via shared memory
    > with other applications.
    >
    > I use Memory-Mapped Files technique in order to share memory.
    >
    > Thus, in each process I have the *same* region of memory that is shared.
    >
    > All I need now is to put my object into this region.
    >
    > I use 'placement new' technique in order to put the object and all
    > objects it creates in its constructor into this region.
    >
    > However, let's say that the object creates a hash_map in the constuctor.
    > The hash_map is placed inside the region as needed, however if hash_map
    > also creates some objects in *it's* constructor they won't be placed
    > into the region and I have no control over it. Thus, the other
    > applications which will try to call methods of my object will fail
    > because those objects that hash_map creates are not inside the shared
    > region.
    >
    > How can I ensure that the *whole* object with all it's sub-objects will
    > be inside the shared region? Is it possible?


    In general no.

    You can take control over dynamic allocation.

    However, you can't take control over static allocation.

    Instead of using shared memory directly, consider other means
    inter-process communication.

    In general those are abstractions on top of shared memory, but limited
    to more useful and safe functionality, such as Windows mailslots, files,
    RPC, Windows message queues, Windows DCOM/Automation, SOAP, etc.; it's
    very very rare that raw shared memory is really called for, unless
    you're implementing such an abstraction yourself.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 8, 2007
    #2
    1. Advertising

  3. Dima Stopel wrote:
    > I need to write an application that shares some object via shared
    > memory with other applications.
    >
    > I use Memory-Mapped Files technique in order to share memory.
    >
    > Thus, in each process I have the *same* region of memory that is
    > shared.
    > All I need now is to put my object into this region.
    >
    > I use 'placement new' technique in order to put the object and all
    > objects it creates in its constructor into this region.
    >
    > However, let's say that the object creates a hash_map in the
    > constuctor. The hash_map is placed inside the region as needed,
    > however if hash_map also creates some objects in *it's* constructor
    > they won't be placed into the region and I have no control over it.
    > Thus, the other applications which will try to call methods of my
    > object will fail because those objects that hash_map creates are not
    > inside the shared region.
    > How can I ensure that the *whole* object with all it's sub-objects
    > will be inside the shared region? Is it possible?


    I guess you're using the terms "whole object" and "sub-objects" too
    freely. Whatever a hash_map creates are not its "sub-objects". That
    said, I believe you need to look into the Allocator template argument
    any standard container has. It is unclear from your posting whether
    you already know about those.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 8, 2007
    #3
  4. Dima Stopel

    Dima Stopel Guest

    You are right.

    I'm not very familiar with the allocator specification.

    However, does the structure (as hash_map) requests the memory from the
    allocator for *any* object it creates (such as different helper objects it
    creates during the initialization), or does it requests memory *only* for
    inserting pairs into the structure ?

    If it is the latter then it is not very suitable for me.

    Thanks,

    "Victor Bazarov" <> wrote in message
    news:f9cpem$t89$...
    > Dima Stopel wrote:
    >> I need to write an application that shares some object via shared
    >> memory with other applications.
    >>
    >> I use Memory-Mapped Files technique in order to share memory.
    >>
    >> Thus, in each process I have the *same* region of memory that is
    >> shared.
    >> All I need now is to put my object into this region.
    >>
    >> I use 'placement new' technique in order to put the object and all
    >> objects it creates in its constructor into this region.
    >>
    >> However, let's say that the object creates a hash_map in the
    >> constuctor. The hash_map is placed inside the region as needed,
    >> however if hash_map also creates some objects in *it's* constructor
    >> they won't be placed into the region and I have no control over it.
    >> Thus, the other applications which will try to call methods of my
    >> object will fail because those objects that hash_map creates are not
    >> inside the shared region.
    >> How can I ensure that the *whole* object with all it's sub-objects
    >> will be inside the shared region? Is it possible?

    >
    > I guess you're using the terms "whole object" and "sub-objects" too
    > freely. Whatever a hash_map creates are not its "sub-objects". That
    > said, I believe you need to look into the Allocator template argument
    > any standard container has. It is unclear from your posting whether
    > you already know about those.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
    >
    Dima Stopel, Aug 9, 2007
    #4
  5. Dima Stopel

    Dima Stopel Guest

    Thank you,

    I'll read about the techniques you proposed.

    "Alf P. Steinbach" <> wrote in message
    news:...
    >* Dima Stopel:
    >> Dear comp.lang.c++ members,
    >>
    >> I need to write an application that shares some object via shared memory
    >> with other applications.
    >>
    >> I use Memory-Mapped Files technique in order to share memory.
    >>
    >> Thus, in each process I have the *same* region of memory that is shared.
    >>
    >> All I need now is to put my object into this region.
    >>
    >> I use 'placement new' technique in order to put the object and all
    >> objects it creates in its constructor into this region.
    >>
    >> However, let's say that the object creates a hash_map in the constuctor.
    >> The hash_map is placed inside the region as needed, however if hash_map
    >> also creates some objects in *it's* constructor they won't be placed into
    >> the region and I have no control over it. Thus, the other applications
    >> which will try to call methods of my object will fail because those
    >> objects that hash_map creates are not inside the shared region.
    >>
    >> How can I ensure that the *whole* object with all it's sub-objects will
    >> be inside the shared region? Is it possible?

    >
    > In general no.
    >
    > You can take control over dynamic allocation.
    >
    > However, you can't take control over static allocation.
    >
    > Instead of using shared memory directly, consider other means
    > inter-process communication.
    >
    > In general those are abstractions on top of shared memory, but limited to
    > more useful and safe functionality, such as Windows mailslots, files, RPC,
    > Windows message queues, Windows DCOM/Automation, SOAP, etc.; it's very
    > very rare that raw shared memory is really called for, unless you're
    > implementing such an abstraction yourself.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
    Dima Stopel, Aug 9, 2007
    #5
  6. Dima Stopel

    James Kanze Guest

    On Aug 9, 8:32 am, "Dima Stopel" <> wrote:
    [Top posting fixed...]
    > "Victor Bazarov" <> wrote in message
    > news:f9cpem$t89$...
    > > Dima Stopel wrote:
    > >> I need to write an application that shares some object via shared
    > >> memory with other applications.


    > >> I use Memory-Mapped Files technique in order to share memory.


    > >> Thus, in each process I have the *same* region of memory
    > >> that is shared. All I need now is to put my object into
    > >> this region.


    > >> I use 'placement new' technique in order to put the object
    > >> and all objects it creates in its constructor into this
    > >> region.


    Be very, very careful about this. In most cases, if you need a
    constructor, you can't put the object in shared memory.

    > >> However, let's say that the object creates a hash_map in
    > >> the constuctor. The hash_map is placed inside the region as
    > >> needed, however if hash_map also creates some objects in
    > >> *it's* constructor they won't be placed into the region and
    > >> I have no control over it. Thus, the other applications
    > >> which will try to call methods of my object will fail
    > >> because those objects that hash_map creates are not inside
    > >> the shared region. How can I ensure that the *whole*
    > >> object with all it's sub-objects will be inside the shared
    > >> region? Is it possible?


    > > I guess you're using the terms "whole object" and
    > > "sub-objects" too freely. Whatever a hash_map creates are
    > > not its "sub-objects". That said, I believe you need to
    > > look into the Allocator template argument any standard
    > > container has. It is unclear from your posting whether you
    > > already know about those.


    > I'm not very familiar with the allocator specification.


    > However, does the structure (as hash_map) requests the memory
    > from the allocator for *any* object it creates (such as
    > different helper objects it creates during the
    > initialization), or does it requests memory *only* for
    > inserting pairs into the structure ?


    The hash_map object requests all dynamic memory it uses using
    the allocator. Some of the objects it creates, however, may
    have their own constructors, which request memory using their
    allocators. Thus, a hash_map< std::string, ...,
    SharedMemAllocator > will not work, because the strings used as
    keys will not use the shared allocator.

    As a general rule, I'd go the other route, and try to design the
    objects in shared memory so that they are POD's.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Aug 9, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gcc
    Replies:
    11
    Views:
    15,334
    David Dorward
    Jun 10, 2004
  2. Mark P
    Replies:
    6
    Views:
    790
    James Dennett
    Apr 27, 2005
  3. John Benson
    Replies:
    4
    Views:
    702
    David M. Cooke
    Jan 9, 2004
  4. Girish Sahani
    Replies:
    11
    Views:
    605
    Roberto Bonvallet
    Jun 7, 2006
  5. Stuart Cullum
    Replies:
    4
    Views:
    92
    Jesús Gabriel y Galán
    Aug 14, 2009
Loading...

Share This Page