From 0ea5fc66924303d1bf73ba283a383e2aadee02f2 Mon Sep 17 00:00:00 2001 From: neodarz Date: Sat, 11 Aug 2018 20:21:34 +0200 Subject: Initial commit --- docs/doxygen/nel/shared__memory_8cpp-source.html | 253 +++++++++++++++++++++++ 1 file changed, 253 insertions(+) create mode 100644 docs/doxygen/nel/shared__memory_8cpp-source.html (limited to 'docs/doxygen/nel/shared__memory_8cpp-source.html') diff --git a/docs/doxygen/nel/shared__memory_8cpp-source.html b/docs/doxygen/nel/shared__memory_8cpp-source.html new file mode 100644 index 00000000..b5dbf9bd --- /dev/null +++ b/docs/doxygen/nel/shared__memory_8cpp-source.html @@ -0,0 +1,253 @@ + + + + nevrax.org : docs + + + + + + + + + + + + + + +
# Home   # nevrax.com   
+ + + + +
Nevrax
+ + + + + + + + + + +
+ + +
+ Nevrax.org
+ + + + + + + +
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
+
+ + +
+ + +
+Docs + +
+  + + + + + +
Documentation 
+ +
+Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  
+

shared_memory.cpp

Go to the documentation of this file.
00001 
+00007 /* Copyright, 2000-2002 Nevrax Ltd.
+00008  *
+00009  * This file is part of NEVRAX NEL.
+00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
+00011  * it under the terms of the GNU General Public License as published by
+00012  * the Free Software Foundation; either version 2, or (at your option)
+00013  * any later version.
+00014 
+00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
+00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
+00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+00018  * General Public License for more details.
+00019 
+00020  * You should have received a copy of the GNU General Public License
+00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
+00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+00023  * MA 02111-1307, USA.
+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         // Storage for file handles, necessary to close the handles
+00045         map<void*,HANDLE>       AccessAddressesToHandles;
+00046 #else
+00047   // Storage for shmid, necessary to destroy the segments
+00048   map<TSharedMemId, int>   SharedMemIdsToShmids;
+00049 #endif
+00050 
+00051 
+00052 /*
+00053  * Create a shared memory segment
+00054  */
+00055 void                    *CSharedMemory::createSharedMemory( TSharedMemId sharedMemId, uint32 size )
+00056 {
+00057 #ifdef NL_OS_WINDOWS
+00058 
+00059         // Create a file mapping backed by the virtual memory swap file (not a data file)
+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         // Map the file into memory address space
+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         // Create a shared memory segment
+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         // Map the segment into memory address space
+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  * Get access to an existing shared memory segment
+00089  */
+00090 void                    *CSharedMemory::accessSharedMemory( TSharedMemId sharedMemId )
+00091 {
+00092 #ifdef NL_OS_WINDOWS
+00093 
+00094         // Open the existing file mapping by name
+00095         HANDLE hMapFile = OpenFileMapping( FILE_MAP_ALL_ACCESS, false, sharedMemId );
+00096         if ( hMapFile == NULL )
+00097                 return NULL;
+00098 
+00099         // Map the file into memory address space
+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         // Open an existing shared memory segment
+00107         int shmid = shmget( sharedMemId, 0, 0666 );
+00108         if ( shmid == -1 )
+00109                 return NULL;
+00110 
+00111         // Map the segment into memory address space
+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  * Close a shared memory segment
+00124  */
+00125 bool                    CSharedMemory::closeSharedMemory( void *accessAddress )
+00126 {
+00127 #ifdef NL_OS_WINDOWS
+00128 
+00129         // Unmap the file from memory address space
+00130         if ( UnmapViewOfFile( accessAddress ) == 0 )
+00131                 return false;
+00132 
+00133         // Close the corresponding handle
+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         // Detach the shared memory segment
+00149         return ( shmdt( accessAddress ) != -1 );
+00150         
+00151 #endif
+00152 }
+00153 
+00154 
+00155 /*
+00156  * Destroy a shared memory segment (to call only by the process that created the segment)
+00157  * Note: does nothing under Windows, it is automatic.
+00158  */
+00159 void        CSharedMemory::destroySharedMemory( TSharedMemId sharedMemId, bool force )
+00160 {
+00161 #ifndef NL_OS_WINDOWS
+00162   // Set the segment to auto-destroying (when the last process detaches)
+00163   map<TSharedMemId,int>::iterator im = SharedMemIdsToShmids.find( sharedMemId );
+00164   if ( im != SharedMemIdsToShmids.end() )
+00165         {
+00166           // Destroy the segment created before
+00167           shmctl( (*im).second, IPC_RMID, 0 );
+00168           SharedMemIdsToShmids.erase( im );
+00169         }
+00170   else if ( force )
+00171         {
+00172           // Open and destroy the segment
+00173           int shmid = shmget( sharedMemId, 0, 0666 );
+00174           if ( shmid != -1 )
+00175                 {
+00176                   // Destroy the segment
+00177                   shmctl( shmid, IPC_RMID, 0 );
+00178                 }
+00179         }
+00180 #endif
+00181 }
+00182 
+00183 
+00184 } // NLMISC
+
+ + +
                                                                                                                                                                    +
+ + -- cgit v1.2.1