shm_mmap.h

Go to the documentation of this file.
00001 
00004 #ifndef SHM_MMAP_H
00005 #define SHM_MMAP_H 1
00006 
00007 #include <sys/types.h>
00008 #include <sys/stat.h>
00009 #include <fcntl.h>
00010 #include <stdio.h>
00011 #include <errno.h>
00012 #include <unistd.h>
00013 #include <sys/mman.h>
00014 #include <pthread.h>
00015 
00020 #define MAPSIZE (size_t)(1024 * 1024)
00021 #define FORKS 20
00022 #define PARENT_FILE "/dev/zero"
00023 #define FORKED_FILE "/opt/local/data/fork%d"
00024 #define FORKED_SIZE 32
00025 
00052 class shm_mmap
00053 {
00054  public:
00055   shm_mmap( char * file ) :
00056     size(MAPSIZE),
00057     rights(PROT_WRITE | PROT_READ),
00058     publication(MAP_SHARED)    
00059     {
00060       if ( 0 == prepareFile( file )  ) {
00061     base = mmapIntArray( file );
00062       }
00063     };
00064 
00067   int * mmapIntArray( char * file );
00070   int prepareFile( char * file );
00073   int * initMap( void );
00074 
00075 
00076  private:
00077   long long id;
00078   char * file;
00081   void * base;
00084   size_t size;
00087   int rights;
00090   int publication;
00093   int last_errno;
00094 
00095 
00096 };
00097 
00105 int * 
00106 shm_mmap::mmapIntArray( char * file ) 
00107 {
00108   int *mmap_mp;
00109   int mmap_fd;
00110   /* open file */
00111   if (( mmap_fd = open( file, O_RDWR, 0644 ) ) == -1 ) {
00112     perror( "data file open fails" );
00113     return (int *) MAP_FAILED;
00114   } else {
00115     fprintf( stdout, "%d : file opened %d. \n", id, mmap_fd ) ;
00116     fflush( stdout );
00117   }
00118   /* mmap in file memory */
00119   if ((mmap_mp = (int *) mmap( (void *) 0, (size_t) MAPSIZE,
00120                 PROT_WRITE | PROT_READ, 
00121                 MAP_SHARED,
00122                 mmap_fd, 0 ) ) == MAP_FAILED) {
00123     perror("mmap failed");
00124   }else {
00125     fprintf( stdout, "%d : memory mapped %p. \n", id, mmap_mp ) ;
00126     fflush( stdout );
00127   }
00128   return mmap_mp;
00129 }
00130 
00131 
00139 int 
00140 shm_mmap::prepareFile( char * file ) 
00141 {
00142   char zero = '\0';
00143   int count = 0;
00144   int fd;
00145   FILE *stream;
00146   /* open file */
00147   if (( fd = open( file, O_RDWR|O_CREAT, 0644 ) ) == -1 ) {
00148     perror( "data file open fails" );
00149     return count;
00150   } else {
00151     fprintf( stdout, "%d : file opened %d. \n", id, fd ) ;
00152     fflush( stdout );
00153   }
00154   if (( stream = fdopen( fd, "w" ) ) == NULL ) {
00155     perror( "data stream open fails" );
00156     return count;
00157   } else {
00158     fprintf( stdout, "%d : stream opened %d. \n", id, fd ) ;
00159     fflush( stdout );
00160   }
00161   count = (int) fwrite( &zero, (size_t) 1, (size_t) MAPSIZE, stream);
00162   close( fd );
00163   return count;
00164 }
00165 
00166 
00167 
00178 int *
00179 shm_mmap::initMap( void ) 
00180 {
00181   char fileNameBuffer[FORKED_SIZE];
00182   snprintf( fileNameBuffer, (size_t) FORKED_SIZE, FORKED_FILE, id );
00183   fprintf( stdout, "%d : file name : %s constructed.\n", id, fileNameBuffer );
00184   fflush( stdout );
00185   if ( 0 == prepareFile( fileNameBuffer ) ) {
00186     fprintf( stdout, "%d : file name : %s not prepared.\n", id, fileNameBuffer );
00187     fflush( stdout );
00188   } else {
00189     fprintf( stdout, "%d : file name : %s prepared.\n", id, fileNameBuffer );
00190     fflush( stdout );
00191   }
00192   return mmapIntArray( fileNameBuffer );
00193 }
00194 
00195 
00196 #endif
00197 
00198 /*
00199  * $Log: shm_mmap.h,v $
00200  * Revision 1.1.1.1  2006/08/30 12:14:48  oommoo
00201  * Initial checkin of project library
00202  *
00203  * Revision 1.1  2004/11/19 20:54:15  vic
00204  * initial version of memory mapped file backing for shared memory segments
00205  *
00206  */

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