shm_segment.h

Go to the documentation of this file.
00001 
00004 #ifndef SHM_SEGMENT_H
00005 #define SHM_SEGMENT_H 1
00006 
00007 #include <sys/types.h> 
00008 #include <sys/ipc.h>
00009 #include <sys/shm.h>
00010 #include <exception>
00011 #include <cstdlib>
00012 
00013 namespace std { using ::malloc; using ::free; }
00014 
00027 class shm_segment
00028 {
00029  public:
00032   shm_segment( int i, int flg ) ;
00035   shm_segment( int key, int size, int perm );
00038   ~shm_segment();
00041   void* get_base() const { return attach ; } ;
00044   int get_flag() const { return shmflg ; } ;
00047   int get_key() const { return key ; } ;
00050   int get_id() const { return shmid ; } ;
00053   int get_size() const { return size ; } ;
00056   bool persist( bool p ) { return persistant = p ; } ;
00057  
00058   void destroy();
00059  private:
00062   key_t  key;  
00065   int  shmflg;  
00068   int  shmid;  
00071   int  size;    
00074   void* attach;  
00077   bool persistant;
00080   struct shmid_ds * shmctlds;
00081 };
00082  
00083 shm_segment::shm_segment( int i, int flg ) :
00084   shmid( i ), shmflg( flg ), persistant( 0 )
00085 {
00086   int j = 1;
00087   struct shmid_ds * shmidds;
00088   shmidds = (shmid_ds *)malloc( sizeof( struct shmid_ds ) );
00089   if ( 0 < shmid ) {
00090     attach = shmat( shmid, 0,0 );
00091     shmctl( shmid, IPC_STAT, shmidds );
00092 //    size = shmidds->shm_segsz;
00093 //    key = shmidds->shm_perm.__key;
00094     shmctlds = shmidds;
00095   } else {
00096     shmctlds = 0x0;
00097   }
00098 }
00099 
00100 shm_segment::shm_segment( int k, int sz, int flg ) :
00101   key( k ), size( sz ), shmflg( flg ), persistant( 0 )
00102 {
00103   shmid = shmget( key, size, flg ); 
00104   if ( 0 < shmid ) {
00105     attach = shmat( shmid, 0,0 );
00106   }
00107 }
00108 
00109 shm_segment::~shm_segment() {
00110   int result;
00111   if ( ! persistant ) {
00112     shmctl( shmid, IPC_RMID, NULL);
00113   }
00114   result = shmdt( attach );
00115 }
00116 
00118 class shm_create_exception : public std::exception
00119 {
00120  public:
00121   const char* what() const throw()
00122   { return "error creating shared memory segment"; }
00123 };
00124 
00125 #endif
00126 
00127 /*
00128  * $Log: shm_segment.h,v $
00129  * Revision 1.3  2008/05/25 15:32:02  oommoo
00130  * modifications to make shared object and install headers (may require additional adds to get ltmain.sh for example
00131  *
00132  * Revision 1.2  2006/09/02 18:47:28  oommoo
00133  * documentation enhancement
00134  *
00135  * Revision 1.1.1.1  2006/08/30 12:14:59  oommoo
00136  * Initial checkin of project library
00137  *
00138  *
00139  * SharedMemory notes from :
00140  *
00141  * http://home.earthlink.net/~joshwalker1/writing/SharedMemory.html
00142  *
00143  */

Project shmq hosted by   SourceForge.net
Documentation generated on Sat Sep 2 10:07:40 2006 for shmq by   doxygen.org 1.4.6