Re: Performer and the STL

New Message Reply Date view Thread view Subject view Author view

From: Christian Skluzacek (c.skluzacek++at++fokkerspace.nl)
Date: 01/18/2000 09:26:06


Xavier Decoret wrote:

> Christian Skluzacek wrote:
> > Xavier,
> >
> > Here's what I developed. I basically got the starting point from Musser
> > & Saini's STL Tutorial and Reference Guide for writing my own Allocator classes.
> >
>
> Thanks Christian,
>
> You can probably answer to a few quesion I was wondering about STL:
>
> If I want to have a vector<myObj> in my shared data structure like:
> struct SharedData
> {
> vector<MyObj> v;
> }
> I guess that:
> SharedData *shared = (SharedData *) pfMalloc(sizeof(SharedData),arena);
>
> won't work because vector new operator doesn't allocate in shared
> memory.
> So I probably need to do:
> struct SharedData
> {
> vector<MyObj> *v;
> }
> SharedData *shared = (SharedData *) pfMalloc(sizeof(SharedData),arena);
> shared->v = (vector<MyObj> *) pfMalloc(sizeof(vector<MyObj>),arena);
>
> I am right or is it unecessary to use this pointer "indirection".
>
> Then I still may have problem with MyObj because this classe's operator
> new does'nt
> allocate in shared memory either.
> As far as I understand, I just need to make MyObj derives from pfObject
> whose operator new does what's necessary.
>
> As you can see, I am not completely sure about this and if you have
> already spend sometime understanding it, I would appreciate that you
> help me save some hours of debugging!
>

Here's my quick answer:
Basically since this is a nested structure (MyObj nested in vector nested in
SharedData), you have to make sure you're allocating from shared memory at every
nesting level (i.e. deep copy) . If you have this:

struct SharedData{ vector<MyObj> v; }

and you pfMalloc it, the vector v is a value type and will be allocated from shared
memory also. You could also do it your way by explicitly allocating the pointer from
shared memory.

The next level is to make sure that the contents of v is put into shared memory by
using the pfShMemAlloc:
vector<MyObj,pfShMemAlloc>. But the vector is still maintaining MyObjs by value, so
every time you add a new object, it is calling the copy constructor on MyObj (a la
Meyer's Effective C++) which may be fine if you're copy constructor is written ok.
You probably want the vector to maintain pointers (or references) to MyObj:
vector<MyObj *,pfShMemAlloc> v;
You just have to be careful when deleting objects.

Then you must allocate the MyObjs from shared memory also which can be done like you
said by deriving from pfObject (or pfMemory).

Hope this helps. I make no guarantees about the above answer, but the basic moral of
the story is that you have to be careful and consistent when using shared memory.
Chris

--
------------------------------------------------------------------------
Christian Skluzacek                 |                   FokkerSpace B.V.
Software Engineer                   |                        Newtonweg 1
Program Operations - Simulation     |                     2333 CP Leiden
+31 71 524 5718                     |                    The Netherlands
mailto:c.skluzacek++at++fokkerspace.nl   |          http://www.fokkerspace.nl
------------------------------------------------------------------------


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Tue Jan 18 2000 - 09:27:07 PST

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