From: Patrick J Melody (melody++at++cmf.nrl.navy.mil)
Date: 03/29/2001 16:07:41
Not sure how helpful this really is but I have been using the following
for my STL allocators. It allocates in the current Performer shared
memory arena not in pfDataPools. Generally objects that I want in shared
memory inherit from a SharedMemory class, which might not be the "best"
way but it has been working well for me for a while now. SharedAlloc.h
and SharedMemory.h are appended.
As for deques working and vectors not: are you taking the address of a
vector element, caching that address, adding more elements to the vector,
and then dereferencing the cached address to get at your data? This
won't generally work since adding more elements can cause reallocation of
vector memory which causes all your elements to be moved in memory,
invalidating your cached address. Deques are probably implemented with
linked lists, so you can probably cache the address of deque elements and
not have them move in memory when you push/pop the deque. Invalid
instruction core dumps can happen if you do a virtual member function
call on a garbage pointer.
#ifndef SHAREDALLOC_H
#define SHAREDALLOC_H
// SharedAlloc is an STL allocator allocating in Performer shared memory.
// PerformerStlAllocRoutines is not directly used by the programmer.
// eg: vector<int, SharedAlloc> v;
#include <alloc.h>
#include <Performer/pf.h>
class PerformerStlAllocRoutines {
public:
static void* allocate(size_t n)
{ return pfMalloc(n, pfGetSharedArena()); }
static void deallocate(void* p, size_t /* n */)
{ pfFree(p); }
static void* reallocate(void* p, size_t /* old_size */, size_t n)
{ return pfRealloc(p, n); }
};
typedef std::allocator<PerformerStlAllocRoutines> SharedAlloc;
#endif
-------------------------------
#ifndef SHAREDMEMORY_H
#define SHAREDMEMORY_H
// Classes derived from SharedMemory will be allocated in Performer
// shared memory. (This may be redundant given that we have pfMemory.)
#include <Performer/pf.h>
class SharedMemory {
public:
void* operator new(size_t n)
{ return pfMalloc(n, pfGetSharedArena()); }
void* operator new (size_t n, void* p)
{ return p; }
void* operator new [] (size_t n)
{ return pfMalloc(n, pfGetSharedArena()); }
void* operator new [] (size_t n, void* p)
{ return p; }
void operator delete (void* p)
{ if (p != NULL) pfFree(p); }
void operator delete [] (void* p)
{ if (p != NULL) pfFree(p); }
};
#endif
- Patrick Melody
This archive was generated by hypermail 2b29 : Thu Mar 29 2001 - 16:07:49 PST