From: eny@ermes.enea.se (Erik Nykvist)
Newsgroups: comp.lang.c++
Subject: Re: Linked Lists in Shared Memory:
Date: 5 Nov 92 09:03:03 GMT
Organization: Project ERMES, Teli Telecom Sweden

ameesh@lsil.com (Ameesh Desai) writes:
> In article 8463@BofA.com, karl@BofA.COM (Karl Nicholas) writes:
> >
> >	Greetings Netters:
> >
> >	I would like to build a linked list in 
> >shared or global memory. 
> 
> 
> 
> *** BEWARE of the virtual table pointers ***
> 
> When sharing data, what you must take care of is that nothing in the shared memory
> points to a local processes address space. You can easily take care of this by
> allocating everything from shared memory using say an overloaded new operator.
> 
> What you do not control is the virtual table pointers generated by cfront
> that will point into the local address space of the process that allocates the
> object (calling the ctor).
> 
> Simple Solution:
> 
> Do not use virtual functions in objects that you want to share !
> 	This is very limiting. You can use some form of indirection to create
> 	your own version of virtual function implementation, but this will not 
> 	be as clean as the one provided by C++.
> 
> There could be other solutions such as, allow only one process to map the 
> pages and fix the vtable pointers for that process. This could be slow !
> 
> Ask some ODBMS vendors ... they seemed to have solved this problem in some
> manner. I suspect that they do not actually 'share' the *same* pages accross
> processes. They must map them to different swap pages and fix the vtable ptrs.
> 
> Ameesh
> 
> ---
> ______________________________     o__            
> | _   /|     Ameesh Desai     \    ,>/_                              
> | \`O.o'     LSI Logic Corp.   \__(_)`(_)_              email: ameesh@lsil.com
> | =(_|_)=    MS E192, 1501 McCarthy Blvd. \             fax  : (408) 433-6802
> |____U_______Milpitas, CA 95035____________\____________voice: (408) 433-4097 

I have used a technique based on the new-operator with placement-syntax.
If we have objects that can only be accessed by one process at the time it
is possible to adjust the this-pointer using this operator.

As long as the constructor used doesn't mutate the data members the affect of
using this operator is only adjustment of the vtbl-pointer.

Example:

enum eIN_SHARED_MEMORY { IN_SHARED_MEMORY };

// I am not sure if you can overload based on enum:s yet
// Assume there is a base class that provides a locking 
// mechanism.
class Shared : public Shared_Object
{
public:
  Shared(eIN_SHARED_MEMORY) {}
  Shared() : i(12) {}
  virtual void foo();
  void set(int);
private:
  int i;
};

void
Shared::foo()
{
   return i;
}

// We also use the placement syntax to allocate the shared object.
=========
PROCESS 1
=========
{
	Shared* sp = new (shared_mem) Shared;
	Lock lock(*sp);
	// Adjust vtbl
	new (shared_mem) Shared(IN_SHARED_MEMORY);
	sp->set(12);
	cout << sp->foo() ,, endl;
	....
}

I wonder if this technique can be of any practical use. I am not sure because
my expericene in this field is only limited.

There are many problems to solve, inheritance, ... etc

Erik Nyquist, ENEA DATA AB, 	erny@enea.se
				eny@ermes.enea.se
Newsgroups: comp.lang.c++