PLearn 0.1
Public Member Functions | Protected Attributes
PLearn::SharedMemory< T > Class Template Reference

#include <Semaphores.h>

Collaboration diagram for PLearn::SharedMemory< T >:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 SharedMemory (int n_items=1)
 allocate shared memory
 SharedMemory (SemId semid)
 access an existing shared memory area
T * data () const
 convert to address of beginning of shared memory segment
int size () const
 ~SharedMemory ()
 release id and memory

Protected Attributes

SemId id
 shared memory id provided by the operating system at construction
bool owner
 true if this process is the owner of the shared memory, i.e.
int size_
 the shared memory will be released with this object is deleted
T * segment
 allocated segment

Detailed Description

template<class T>
class PLearn::SharedMemory< T >

Definition at line 167 of file Semaphores.h.


Constructor & Destructor Documentation

template<class T >
PLearn::SharedMemory< T >::SharedMemory ( int  n_items = 1) [inline]

allocate shared memory

Definition at line 176 of file Semaphores.h.

References PLERROR.

                                : owner(true) {
        size_ = n_items*sizeof(T);
        int rv=shmget(IPC_PRIVATE, size_, 0666 | IPC_CREAT);
        if (rv == -1) PLERROR("SharedMemory::SharedMemory, shmget failed:%s",strerror(errno));
        else id.id=rv;
        segment = (T*)shmat(id.id,0,0);
        if (segment == 0) 
            PLERROR("SharedMemory::SharedMemory, shmat failed trying to allocate %d bytes: err=%s",
                    size_,strerror(errno));

    }
template<class T >
PLearn::SharedMemory< T >::SharedMemory ( SemId  semid) [inline]

access an existing shared memory area

Definition at line 189 of file Semaphores.h.

References PLERROR.

                              : id(semid), owner(false) {
        struct shmid_ds buf;
        int r = shmctl(id.id,IPC_STAT,&buf);
        if (r == -1)
            PLERROR("SharedMemory:: slave SharedMemory(%d) shmctl returns -1, %s",
                    id.id,strerror(errno));
        size_ = buf.shm_segsz;
        segment = (T*)shmat(id.id,0,0);
    }
template<class T >
PLearn::SharedMemory< T >::~SharedMemory ( ) [inline]

release id and memory

Definition at line 205 of file Semaphores.h.

References PLearn::endl(), and PLERROR.

                    {
        int rv=shmdt((char*)segment);
        if (rv == -1)
            PLERROR("SharedMemory::~SharedMemory (id=%d) shmdt failed, %s", 
                    id.id,strerror(errno));
        if (owner)
        {
            rv=shmctl(id.id,IPC_RMID,0);
            if (rv == -1)
                PLERROR("SharedMemory::~SharedMemory (id=%d) shmctl failed, %s", 
                        id.id,strerror(errno));
            cout << "released shared memory segment ID = " << id.id << endl;
        }
    }

Here is the call graph for this function:


Member Function Documentation

template<class T >
T* PLearn::SharedMemory< T >::data ( ) const [inline]

convert to address of beginning of shared memory segment

Definition at line 200 of file Semaphores.h.

{ return segment; }
template<class T >
int PLearn::SharedMemory< T >::size ( ) const [inline]

Definition at line 202 of file Semaphores.h.

{ return size_ / sizeof(T); }

Member Data Documentation

template<class T >
SemId PLearn::SharedMemory< T >::id [protected]

shared memory id provided by the operating system at construction

Definition at line 169 of file Semaphores.h.

template<class T >
bool PLearn::SharedMemory< T >::owner [protected]

true if this process is the owner of the shared memory, i.e.

Definition at line 170 of file Semaphores.h.

template<class T >
T* PLearn::SharedMemory< T >::segment [protected]

allocated segment

Definition at line 173 of file Semaphores.h.

template<class T >
int PLearn::SharedMemory< T >::size_ [protected]

the shared memory will be released with this object is deleted

in number of bytes

Definition at line 172 of file Semaphores.h.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines