00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "stdmisc.h"
00027 #include "nel/misc/shared_memory.h"
00028
00029 #ifdef NL_OS_WINDOWS
00030 #include <windows.h>
00031 #else
00032 # include <sys/types.h>
00033 # include <sys/ipc.h>
00034 # include <sys/shm.h>
00035 #endif
00036
00037 using namespace std;
00038
00039
00040 namespace NLMISC {
00041
00042
00043 #ifdef NL_OS_WINDOWS
00044
00045 map<void*,HANDLE> AccessAddressesToHandles;
00046 #else
00047
00048 map<TSharedMemId, int> SharedMemIdsToShmids;
00049 #endif
00050
00051
00052
00053
00054
00055 void *CSharedMemory::createSharedMemory( TSharedMemId sharedMemId, uint32 size )
00056 {
00057 #ifdef NL_OS_WINDOWS
00058
00059
00060 HANDLE hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, sharedMemId );
00061 if ( (hMapFile == NULL) || (GetLastError() == ERROR_ALREADY_EXISTS) )
00062 return NULL;
00063
00064
00065 void *accessAddress = MapViewOfFile( hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
00066 AccessAddressesToHandles.insert( make_pair( accessAddress, hMapFile ) );
00067 return accessAddress;
00068
00069 #else
00070
00071
00072 int shmid = shmget( sharedMemId, size, IPC_CREAT | IPC_EXCL | 0666 );
00073 if ( shmid == -1 )
00074 return NULL;
00075 SharedMemIdsToShmids.insert( make_pair( sharedMemId, shmid ) );
00076
00077
00078 void *accessAddress = (void*)shmat( shmid, 0, 0 );
00079 if ( accessAddress == (void*)-1 )
00080 return NULL;
00081 else
00082 return accessAddress;
00083 #endif
00084 }
00085
00086
00087
00088
00089
00090 void *CSharedMemory::accessSharedMemory( TSharedMemId sharedMemId )
00091 {
00092 #ifdef NL_OS_WINDOWS
00093
00094
00095 HANDLE hMapFile = OpenFileMapping( FILE_MAP_ALL_ACCESS, false, sharedMemId );
00096 if ( hMapFile == NULL )
00097 return NULL;
00098
00099
00100 void *accessAddress = MapViewOfFile( hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
00101 AccessAddressesToHandles.insert( make_pair( accessAddress, hMapFile ) );
00102 return accessAddress;
00103
00104 #else
00105
00106
00107 int shmid = shmget( sharedMemId, 0, 0666 );
00108 if ( shmid == -1 )
00109 return NULL;
00110
00111
00112 void *accessAddress = (void*)shmat( shmid, 0, 0 );
00113 if ( accessAddress == (void*)-1 )
00114 return NULL;
00115 else
00116 return accessAddress;
00117
00118 #endif
00119 }
00120
00121
00122
00123
00124
00125 bool CSharedMemory::closeSharedMemory( void *accessAddress )
00126 {
00127 #ifdef NL_OS_WINDOWS
00128
00129
00130 if ( UnmapViewOfFile( accessAddress ) == 0 )
00131 return false;
00132
00133
00134 map<void*,HANDLE>::iterator im = AccessAddressesToHandles.find( accessAddress );
00135 if ( im != AccessAddressesToHandles.end() )
00136 {
00137 CloseHandle( (*im).second );
00138 AccessAddressesToHandles.erase( im );
00139 return true;
00140 }
00141 else
00142 {
00143 return false;
00144 }
00145
00146 #else
00147
00148
00149 return ( shmdt( accessAddress ) != -1 );
00150
00151 #endif
00152 }
00153
00154
00155
00156
00157
00158
00159 void CSharedMemory::destroySharedMemory( TSharedMemId sharedMemId, bool force )
00160 {
00161 #ifndef NL_OS_WINDOWS
00162
00163 map<TSharedMemId,int>::iterator im = SharedMemIdsToShmids.find( sharedMemId );
00164 if ( im != SharedMemIdsToShmids.end() )
00165 {
00166
00167 shmctl( (*im).second, IPC_RMID, 0 );
00168 SharedMemIdsToShmids.erase( im );
00169 }
00170 else if ( force )
00171 {
00172
00173 int shmid = shmget( sharedMemId, 0, 0666 );
00174 if ( shmid != -1 )
00175 {
00176
00177 shmctl( shmid, IPC_RMID, 0 );
00178 }
00179 }
00180 #endif
00181 }
00182
00183
00184 }