0023427: Unused C-sources in OSD package
[occt.git] / src / OSD / OSD_SharedMemory.cxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef WNT
16 #if (!defined (__hpux )) && (!defined (HPUX))
17
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>
23
24 #include <errno.h>
25
26 const OSD_WhoAmI Iam = OSD_WSharedMemory;
27
28
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 *);
32
33
34 OSD_SharedMemory::OSD_SharedMemory(){
35  myId = -1;
36 }
37
38
39 // ======================================================================
40 OSD_SharedMemory::OSD_SharedMemory(const TCollection_AsciiString& Name,
41                                    const Standard_Integer Size) 
42 //      CREATE a mapping memory object ==================================
43 // ======================================================================
44
45
46 {
47  myId = -1;
48  
49  if (!Name.IsAscii())
50   Standard_ConstructionError::Raise("OSD_SharedMemory::OSD_SharedMemory: Name");
51
52  myName = Name;
53
54  if (Size <= 0)
55   Standard_ProgramError::Raise("OSD_SharedMemory::OSD_SharedMemory : invalid size");
56
57  mySize = Size;
58 }
59
60
61
62
63
64
65 // ======================================================================
66 void OSD_SharedMemory::Build()
67 // ======================================================================
68 {
69   Standard_PCharacter pStr;
70   //
71   pStr=(Standard_PCharacter)myName.ToCString();
72   myId = create_sharedmemory((int **)&myAddress, pStr, (int)mySize);
73   
74   if (myId == 0)
75     myError.SetValue (errno, Iam, "OSD_SharedMemory::Build");
76 }
77
78
79
80
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
87 //      created(allocated)
88
89 {
90   if (!Name.IsAscii()) {
91     Standard_ConstructionError::Raise("OSD_SharedMemory::Open : Name");
92   }
93   myName = Name;
94
95   if (Size <= 0) {
96     Standard_ProgramError::Raise("OSD_SharedMemory::Open : invalid size");
97   }
98   mySize = Size;
99   //
100   Standard_PCharacter pStr;
101   //
102   pStr=(Standard_PCharacter)myName.ToCString();
103   myId = open_sharedmemory((int **)&myAddress, pStr, (int)mySize);
104   //
105   if (myId == 0) {
106     myError.SetValue (errno, Iam, "OSD_SharedMemory::Open");
107   }
108 }
109
110 // ======================================================================
111 void OSD_SharedMemory::Delete() 
112 // ======================================================================
113 //      CLOSE a mapping memory section
114
115 {
116   if (myError.Failed()) {
117     myError.Perror();
118   }
119   if (myId == -1) {
120     Standard_ProgramError::Raise("OSD_SharedMemory::Delete : shared memory not opened/created");
121   }
122   //
123   Standard_PCharacter pStr;
124   //
125   pStr=(Standard_PCharacter)myName.ToCString();
126   if (remove_sharedmemory((int *)&myId, pStr) == 0) {
127     myError.SetValue(errno, Iam, "OSD_SharedMemory::Delete");
128   }
129 }
130
131
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");
137  return(myAddress);
138 }
139
140
141
142 // ======================================================================
143 void OSD_SharedMemory::Reset(){
144 // ======================================================================
145  myError.Reset();
146 }
147
148 // ======================================================================
149 Standard_Boolean OSD_SharedMemory::Failed()const{
150 // ======================================================================
151  return( myError.Failed());
152 }
153
154 // ======================================================================
155 void OSD_SharedMemory::Perror() {
156 // ======================================================================
157  myError.Perror();
158 }
159
160
161 // ======================================================================
162 Standard_Integer OSD_SharedMemory::Error()const{
163 // ======================================================================
164  return( myError.Error());
165 }
166
167 #endif
168 #else
169
170 //------------------------------------------------------------------------
171 //-------------------  Windows NT sources for OSD_SharedMemory- ----------
172 //------------------------------------------------------------------------
173
174 #define STRICT
175 #include <windows.h>
176
177 #include <OSD_SharedMemory.ixx>
178
179 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
180
181 OSD_SharedMemory :: OSD_SharedMemory () {
182
183  myId = 0;
184
185 }  // end constructor ( 1 )
186
187 OSD_SharedMemory :: OSD_SharedMemory (
188                      const TCollection_AsciiString& Name,
189                      const Standard_Integer         size
190                     ) {
191
192  myName = Name;
193
194  if ( size <= 0 )
195
196   Standard_ConstructionError :: Raise (
197                                  "OSD_SharedMemory :: OSD_SharedMemory : invalid size"
198                                 );
199
200  mySize = size;
201
202 }  // end constructor ( 2 )
203
204 void OSD_SharedMemory :: Build () {
205
206  HANDLE hFileMapping = CreateFileMapping (
207                         ( HANDLE )0xFFFFFFFF, NULL, PAGE_READWRITE, 0, mySize,
208                         myName.ToCString ()
209                        );
210
211  if (  hFileMapping == NULL || GetLastError () == ERROR_ALREADY_EXISTS  )
212  
213   _osd_wnt_set_error ( myError, OSD_WSharedMemory ); 
214  
215  else {
216  
217   myAddress = MapViewOfFile ( hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
218
219   if ( myAddress == NULL )
220
221    _osd_wnt_set_error ( myError, OSD_WSharedMemory );
222
223   else
224
225    myId = ( Standard_Integer )hFileMapping;
226
227  }  // end else
228
229 }  // end OSD_SharedMemory :: Build
230
231 void OSD_SharedMemory :: Open (
232                           const TCollection_AsciiString& Name,
233                           const Standard_Integer         size
234                          ) {
235
236  myName = Name;
237
238  if ( size <= 0 )
239
240   Standard_ProgramError :: Raise ( "OSD_SharedMemory :: Open : invalid size" );
241
242  mySize = size;
243
244  HANDLE hFileMapping = OpenFileMapping (
245                         FILE_MAP_ALL_ACCESS, FALSE, myName.ToCString ()
246                        );
247
248  if ( hFileMapping == NULL )
249
250   _osd_wnt_set_error ( myError, OSD_WSharedMemory );
251
252  else {
253  
254   myAddress = MapViewOfFile ( hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, mySize );
255
256   if ( myAddress == NULL )
257  
258    _osd_wnt_set_error ( myError, OSD_WSharedMemory );
259
260   else
261
262    myId = ( Standard_Integer )hFileMapping;
263
264   CloseHandle ( hFileMapping );
265
266  }  // end else
267
268 }  // end OSD_SharedMemory :: Open
269
270 void OSD_SharedMemory :: Delete () {
271
272  if (  Failed ()  ) Perror ();
273
274  if ( myId == 0 )
275
276   Standard_ProgramError :: Raise (
277                             "OSD_SharedMemory :: Delete : shared memory not opened/created"
278                            );
279
280  UnmapViewOfFile ( myAddress );
281  CloseHandle (  ( HANDLE )myId  );
282
283 }  // end OSD_SharedMemory :: Delete
284
285 Standard_Address OSD_SharedMemory :: GiveAddress () const {
286
287  if ( myAddress == NULL )
288
289   Standard_NullObject :: Raise (
290                           "OSD_SharedMemory :: Address : shared memory not opened/created"
291                          );
292
293  return myAddress;
294
295 }  // end OSD_SharedMemory :: GiveAddress
296
297 Standard_Boolean OSD_SharedMemory :: Failed () const {
298
299  return myError.Failed ();
300
301 }  // end OSD_SharedMemory :: Failed
302
303 void OSD_SharedMemory :: Reset () {
304
305  myError.Reset ();
306
307 }  // end OSD_SharedMemory :: Reset
308
309 void OSD_SharedMemory :: Perror () {
310
311  myError.Perror ();
312
313 }  // end OSD_SharedMemory :: Perror
314
315 Standard_Integer OSD_SharedMemory :: Error () const{
316
317  return myError.Error ();
318
319 }  // end OSD_SharedMemory :: Error
320
321 #endif