Re: pfMalloc & object container

New Message Reply Date view Thread view Subject view Author view

Steve Baker (sbaker++at++link.com)
Tue, 28 Apr 1998 12:12:56 -0500 (CDT)


On Tue, 28 Apr 1998, David Chan wrote:

> HI,
> I have a problem with pfMalloc when I am using pfMalloc to create
> a shared memory. Here is my simple code:
>
> shared=(SharedData *)pfMalloc(sizeof(SharedData),
> pfGetSharedArena());
>
> while SharedData is a typedef structure, and inside SharedData, there is a
> Class container:
>
> typedef structure {
> ....
> Console *console[10];
> ....
> } SharedData;
  
When you are using C++, it is inadvisable to use any variety of malloc/amalloc/pfMalloc
since those functions only allocate memory - they know nothing of calling the
all-important C++ constructor functions.

One solution (the one I like best) is for structures/classes that ALWAYS need to
be in shared memory, I declare:

  class SharedMemory
  {
  public:

    void *operator new ( size_t x ) ;
    {
      return pfCalloc ( 1, x, pfGetSharedArena () ) ;
    }

    void operator delete ( void *p ) ;
    {
      if ( p != NULL )
        pfFree ( p ) ;
    }
  } ;

...then all classes that need to be in shared memory can be derived from
this base class and you can forget about pfMalloc and pfFree and just
use 'new' and 'delete' as nature intended.

  class MyClass : public SharedMemory
  {
    ...stuff...
  } ;

  MyClass *my_object = new MyClass ; /* pfCalloc called automagically! */

There are other ways to do it - but this is my favorite.

Steve Baker (817)619-8776 (Vox/Vox-Mail)
Raytheon Systems Inc. (817)619-4028 (Fax)
Work: SBaker++at++link.com http://www.hti.com
Home: SJBaker1++at++airmail.net http://web2.airmail.net/sjbaker1

=======================================================================
List Archives, FAQ, FTP: http://www.sgi.com/Technology/Performer/
            Submissions: info-performer++at++sgi.com
        Admin. requests: info-performer-request++at++sgi.com


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2.0b2 on Mon Aug 10 1998 - 17:57:18 PDT

This message has been cleansed for anti-spam protection. Replace '++at++' in any mail addresses with the '@' symbol.