21
21
#include <conio.h>
22
22
#include <tchar.h>
23
23
24
- struct sharedMemoryBuffer {
25
- char * sharedMemoryBuf ;
26
- char * sharedMemoryBufExport ;
24
+ /** Shared memory object */
25
+ typedef struct {
26
+ char * smBuf ;
27
+ char * smBufExport ;
27
28
HANDLE hMapFile ;
28
- };
29
+ } MDDSharedMemory ;
30
+
31
+ #define MDDSM_PLOCK ((long*)smb->smBuf)
32
+ #define MDDSM_PLEN (MDDSM_PLOCK + sizeof(long))
33
+ #define MDDSM_DATA (MDDSM_PLEN + sizeof(int))
34
+ #define MDDSM_LOCK () while (InterlockedExchange(MDDSM_PLOCK, 1L) != 0) Sleep(0)
35
+ #define MDDSM_UNLOCK () InterlockedExchange(MDDSM_PLOCK, 0)
29
36
30
37
DllExport void * MDD_SharedMemoryConstructor (const char * name , int bufSize ) {
31
- struct sharedMemoryBuffer * smb = (struct sharedMemoryBuffer * )malloc (sizeof (struct sharedMemoryBuffer ));
38
+ MDDSharedMemory * smb = (MDDSharedMemory * )malloc (sizeof (MDDSharedMemory ));
32
39
smb -> hMapFile = CreateFileMappingA (
33
40
INVALID_HANDLE_VALUE , /* use paging file */
34
41
NULL , /* default security */
35
42
PAGE_READWRITE , /* read/write access */
36
43
0 , /* maximum object size (high-order DWORD) */
37
- sizeof (long * ) + sizeof (int ) + bufSize , /* maximum object size (low-order DWORD) */
44
+ sizeof (long ) + sizeof (int ) + bufSize , /* maximum object size (low-order DWORD) */
38
45
name ); /* name of mapping object */
39
- if (GetLastError () == ERROR_ALREADY_EXISTS ) {
46
+ if (GetLastError () == ERROR_ALREADY_EXISTS ) {
40
47
smb -> hMapFile = OpenFileMappingA (
41
48
FILE_MAP_ALL_ACCESS , /* read/write access */
42
49
FALSE, /* do not inherit the name */
@@ -49,71 +56,65 @@ DllExport void* MDD_SharedMemoryConstructor(const char * name, int bufSize) {
49
56
ModelicaFormatError ("MDDSharedMemory.h: Could not create file mapping object (%d).\n" , GetLastError ());
50
57
return NULL ;
51
58
}
52
- smb -> sharedMemoryBuf = (char * ) MapViewOfFile (
59
+ smb -> smBuf = (char * ) MapViewOfFile (
53
60
smb -> hMapFile , /* handle to map object */
54
61
FILE_MAP_ALL_ACCESS , /* read/write permission */
55
62
0 ,
56
63
0 ,
57
- sizeof (long * ) + sizeof (int ) + bufSize );
64
+ sizeof (long ) + sizeof (int ) + bufSize );
58
65
59
- if (smb -> sharedMemoryBuf == NULL ) {
66
+ if (smb -> smBuf == NULL ) {
60
67
CloseHandle (smb -> hMapFile );
61
68
free (smb );
62
69
ModelicaFormatError ("MDDSharedMemory.h: Could not map view of file (%d).\n" , GetLastError ());
63
70
return NULL ;
64
71
}
65
- smb -> sharedMemoryBufExport = (char * ) calloc (bufSize , 1 );
72
+ smb -> smBufExport = (char * ) calloc (bufSize , 1 );
66
73
67
74
return smb ;
68
75
}
69
76
70
77
DllExport void MDD_SharedMemoryDestructor (void * p_smb ) {
71
- struct sharedMemoryBuffer * smb = (struct sharedMemoryBuffer * ) p_smb ;
78
+ MDDSharedMemory * smb = (MDDSharedMemory * ) p_smb ;
72
79
if (smb ) {
73
- if (smb -> sharedMemoryBufExport ) {
74
- free (smb -> sharedMemoryBufExport );
75
- }
76
- UnmapViewOfFile (smb -> sharedMemoryBuf );
80
+ if (smb -> smBufExport ) {
81
+ free (smb -> smBufExport );
82
+ }
83
+ UnmapViewOfFile (smb -> smBuf );
77
84
CloseHandle (smb -> hMapFile );
78
85
free (smb );
79
86
}
80
87
}
81
88
82
89
DllExport int MDD_SharedMemoryGetDataSize (void * p_smb ) {
83
90
int len = 0 ;
84
- struct sharedMemoryBuffer * smb = (struct sharedMemoryBuffer * ) p_smb ;
91
+ MDDSharedMemory * smb = (MDDSharedMemory * ) p_smb ;
85
92
if (smb ) {
86
- while (InterlockedExchange ((volatile long * )smb -> sharedMemoryBuf , 1L ) != 0 ) {
87
- Sleep (0 );
88
- }
89
- memcpy (& len , smb -> sharedMemoryBuf + sizeof (long * ), sizeof (int ));
90
- InterlockedExchange ((volatile long * )smb -> sharedMemoryBuf , 0 );
93
+ MDDSM_LOCK ();
94
+ memcpy (& len , MDDSM_PLEN , sizeof (int ));
95
+ MDDSM_UNLOCK ();
91
96
}
92
97
return len ;
93
98
}
94
99
95
100
DllExport const char * MDD_SharedMemoryRead (void * p_smb ) {
96
- struct sharedMemoryBuffer * smb = (struct sharedMemoryBuffer * ) p_smb ;
101
+ MDDSharedMemory * smb = (MDDSharedMemory * ) p_smb ;
97
102
if (smb ) {
98
103
int len ;
99
- while (InterlockedExchange ((volatile long * )smb -> sharedMemoryBuf , 1L ) != 0 ) {
100
- Sleep (0 );
101
- }
102
- memcpy (& len , smb -> sharedMemoryBuf + sizeof (long * ), sizeof (int ));
103
- memcpy (smb -> sharedMemoryBufExport , smb -> sharedMemoryBuf + sizeof (long * ) + sizeof (int ), len );
104
- InterlockedExchange ((volatile long * )smb -> sharedMemoryBuf , 0 );
104
+ MDDSM_LOCK ();
105
+ memcpy (& len , MDDSM_PLEN , sizeof (int ));
106
+ memcpy (smb -> smBufExport , MDDSM_DATA , len );
107
+ MDDSM_UNLOCK ();
105
108
}
106
- return (const char * ) smb -> sharedMemoryBufExport ;
109
+ return (const char * ) smb -> smBufExport ;
107
110
}
108
111
109
112
DllExport void MDD_SharedMemoryWrite (void * p_smb , const char * buffer , int len ) {
110
- struct sharedMemoryBuffer * smb = (struct sharedMemoryBuffer * ) p_smb ;
111
- while (InterlockedExchange ((volatile long * )smb -> sharedMemoryBuf , 1L ) != 0 ) {
112
- Sleep (0 );
113
- }
114
- memcpy (smb -> sharedMemoryBuf + sizeof (long * ) + sizeof (int ), buffer , len );
115
- memcpy (smb -> sharedMemoryBuf + sizeof (long * ), & len , sizeof (int ));
116
- InterlockedExchange ((volatile long * )smb -> sharedMemoryBuf , 0 );
113
+ MDDSharedMemory * smb = (MDDSharedMemory * ) p_smb ;
114
+ MDDSM_LOCK ();
115
+ memcpy (MDDSM_DATA , buffer , len );
116
+ memcpy (MDDSM_PLEN , & len , sizeof (int ));
117
+ MDDSM_UNLOCK ();
117
118
}
118
119
119
120
#elif defined(__linux__ )
0 commit comments