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
00093
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
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143