1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and / or modify it
7 // under the terms of the GNU Lesser General Public version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
16 #if (!defined (__hpux )) && (!defined (HPUX))
18 #include <Standard_ProgramError.hxx>
19 #include <Standard_NullObject.hxx>
20 #include <Standard_ConstructionError.hxx>
21 #include <OSD_SharedMemory.ixx>
22 #include <OSD_WhoAmI.hxx>
26 const OSD_WhoAmI Iam = OSD_WSharedMemory;
29 extern "C" int create_sharedmemory(int **,char *,int);
30 extern "C" int open_sharedmemory(int **,char *,int);
31 extern "C" int remove_sharedmemory(int *, char *);
34 OSD_SharedMemory::OSD_SharedMemory(){
39 // ======================================================================
40 OSD_SharedMemory::OSD_SharedMemory(const TCollection_AsciiString& Name,
41 const Standard_Integer Size)
42 // CREATE a mapping memory object ==================================
43 // ======================================================================
50 Standard_ConstructionError::Raise("OSD_SharedMemory::OSD_SharedMemory: Name");
55 Standard_ProgramError::Raise("OSD_SharedMemory::OSD_SharedMemory : invalid size");
65 // ======================================================================
66 void OSD_SharedMemory::Build()
67 // ======================================================================
69 Standard_PCharacter pStr;
71 pStr=(Standard_PCharacter)myName.ToCString();
72 myId = create_sharedmemory((int **)&myAddress, pStr, (int)mySize);
75 myError.SetValue (errno, Iam, "OSD_SharedMemory::Build");
81 // ======================================================================
82 void OSD_SharedMemory::Open(const TCollection_AsciiString& Name,
83 const Standard_Integer Size)
84 // ======================================================================
85 // OPEN a mapping memory section
86 // We suppose that the shared memory segment is already
90 if (!Name.IsAscii()) {
91 Standard_ConstructionError::Raise("OSD_SharedMemory::Open : Name");
96 Standard_ProgramError::Raise("OSD_SharedMemory::Open : invalid size");
100 Standard_PCharacter pStr;
102 pStr=(Standard_PCharacter)myName.ToCString();
103 myId = open_sharedmemory((int **)&myAddress, pStr, (int)mySize);
106 myError.SetValue (errno, Iam, "OSD_SharedMemory::Open");
110 // ======================================================================
111 void OSD_SharedMemory::Delete()
112 // ======================================================================
113 // CLOSE a mapping memory section
116 if (myError.Failed()) {
120 Standard_ProgramError::Raise("OSD_SharedMemory::Delete : shared memory not opened/created");
123 Standard_PCharacter pStr;
125 pStr=(Standard_PCharacter)myName.ToCString();
126 if (remove_sharedmemory((int *)&myId, pStr) == 0) {
127 myError.SetValue(errno, Iam, "OSD_SharedMemory::Delete");
132 // ======================================================================
133 Standard_Address OSD_SharedMemory::GiveAddress()const{
134 // ======================================================================
135 if (myAddress == NULL)
136 Standard_NullObject::Raise("OSD_SharedMemory::Address : shared memory not opened/created");
142 // ======================================================================
143 void OSD_SharedMemory::Reset(){
144 // ======================================================================
148 // ======================================================================
149 Standard_Boolean OSD_SharedMemory::Failed()const{
150 // ======================================================================
151 return( myError.Failed());
154 // ======================================================================
155 void OSD_SharedMemory::Perror() {
156 // ======================================================================
161 // ======================================================================
162 Standard_Integer OSD_SharedMemory::Error()const{
163 // ======================================================================
164 return( myError.Error());
170 //------------------------------------------------------------------------
171 //------------------- Windows NT sources for OSD_SharedMemory- ----------
172 //------------------------------------------------------------------------
177 #include <OSD_SharedMemory.ixx>
179 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
181 OSD_SharedMemory :: OSD_SharedMemory () {
185 } // end constructor ( 1 )
187 OSD_SharedMemory :: OSD_SharedMemory (
188 const TCollection_AsciiString& Name,
189 const Standard_Integer size
196 Standard_ConstructionError :: Raise (
197 "OSD_SharedMemory :: OSD_SharedMemory : invalid size"
202 } // end constructor ( 2 )
204 void OSD_SharedMemory :: Build () {
206 HANDLE hFileMapping = CreateFileMapping (
207 ( HANDLE )0xFFFFFFFF, NULL, PAGE_READWRITE, 0, mySize,
211 if ( hFileMapping == NULL || GetLastError () == ERROR_ALREADY_EXISTS )
213 _osd_wnt_set_error ( myError, OSD_WSharedMemory );
217 myAddress = MapViewOfFile ( hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
219 if ( myAddress == NULL )
221 _osd_wnt_set_error ( myError, OSD_WSharedMemory );
225 myId = ( Standard_Integer )hFileMapping;
229 } // end OSD_SharedMemory :: Build
231 void OSD_SharedMemory :: Open (
232 const TCollection_AsciiString& Name,
233 const Standard_Integer size
240 Standard_ProgramError :: Raise ( "OSD_SharedMemory :: Open : invalid size" );
244 HANDLE hFileMapping = OpenFileMapping (
245 FILE_MAP_ALL_ACCESS, FALSE, myName.ToCString ()
248 if ( hFileMapping == NULL )
250 _osd_wnt_set_error ( myError, OSD_WSharedMemory );
254 myAddress = MapViewOfFile ( hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, mySize );
256 if ( myAddress == NULL )
258 _osd_wnt_set_error ( myError, OSD_WSharedMemory );
262 myId = ( Standard_Integer )hFileMapping;
264 CloseHandle ( hFileMapping );
268 } // end OSD_SharedMemory :: Open
270 void OSD_SharedMemory :: Delete () {
272 if ( Failed () ) Perror ();
276 Standard_ProgramError :: Raise (
277 "OSD_SharedMemory :: Delete : shared memory not opened/created"
280 UnmapViewOfFile ( myAddress );
281 CloseHandle ( ( HANDLE )myId );
283 } // end OSD_SharedMemory :: Delete
285 Standard_Address OSD_SharedMemory :: GiveAddress () const {
287 if ( myAddress == NULL )
289 Standard_NullObject :: Raise (
290 "OSD_SharedMemory :: Address : shared memory not opened/created"
295 } // end OSD_SharedMemory :: GiveAddress
297 Standard_Boolean OSD_SharedMemory :: Failed () const {
299 return myError.Failed ();
301 } // end OSD_SharedMemory :: Failed
303 void OSD_SharedMemory :: Reset () {
307 } // end OSD_SharedMemory :: Reset
309 void OSD_SharedMemory :: Perror () {
313 } // end OSD_SharedMemory :: Perror
315 Standard_Integer OSD_SharedMemory :: Error () const{
317 return myError.Error ();
319 } // end OSD_SharedMemory :: Error