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