@@ -46,24 +46,23 @@ namespace {
46
46
void *
47
47
SharedMemoryHandleCreate (
48
48
std::string triton_shm_name, void * shm_addr, std::string shm_key,
49
- void * shm_file, size_t offset, size_t byte_size)
49
+ ShmFile * shm_file, size_t offset, size_t byte_size)
50
50
{
51
51
SharedMemoryHandle* handle = new SharedMemoryHandle ();
52
52
handle->triton_shm_name_ = triton_shm_name;
53
53
handle->base_addr_ = shm_addr;
54
54
handle->shm_key_ = shm_key;
55
- handle->platform_handle_ = std::make_unique<ShmFile> (shm_file);
55
+ handle->platform_handle_ . reset (shm_file);
56
56
handle->offset_ = offset;
57
57
handle->byte_size_ = byte_size;
58
58
return static_cast <void *>(handle);
59
59
}
60
60
61
61
int
62
62
SharedMemoryRegionMap (
63
- void * shm_file, size_t offset, size_t byte_size, void ** shm_addr)
63
+ ShmFile * shm_file, size_t offset, size_t byte_size, void ** shm_addr)
64
64
{
65
65
#ifdef _WIN32
66
- HANDLE file_handle = static_cast <HANDLE>(shm_file);
67
66
// The MapViewOfFile function takes a high-order and low-order DWORD (4 bytes
68
67
// each) for offset. 'size_t' can either be 4 or 8 bytes depending on the
69
68
// operating system. To handle both cases agnostically, we cast 'offset' to
@@ -74,14 +73,14 @@ SharedMemoryRegionMap(
74
73
DWORD low_order_offset = upperbound_offset & 0xFFFFFFFF ;
75
74
// map shared memory to process address space
76
75
*shm_addr = MapViewOfFile (
77
- file_handle, // handle to map object
78
- FILE_MAP_ALL_ACCESS, // read/write permission
79
- high_order_offset, // offset (high-order DWORD)
80
- low_order_offset, // offset (low-order DWORD)
76
+ shm_file-> shm_handle_ , // handle to map object
77
+ FILE_MAP_ALL_ACCESS, // read/write permission
78
+ high_order_offset, // offset (high-order DWORD)
79
+ low_order_offset, // offset (low-order DWORD)
81
80
byte_size);
82
81
83
82
if (*shm_addr == NULL ) {
84
- CloseHandle (file_handle );
83
+ CloseHandle (shm_file-> shm_handle_ );
85
84
return -1 ;
86
85
}
87
86
// For Windows, we cannot close the shared memory handle here. When all
@@ -90,9 +89,9 @@ SharedMemoryRegionMap(
90
89
// we are destroying the shared memory object.
91
90
return 0 ;
92
91
#else
93
- int fd = *static_cast <int *>(shm_file);
94
92
// map shared memory to process address space
95
- *shm_addr = mmap (NULL , byte_size, PROT_WRITE, MAP_SHARED, fd, offset);
93
+ *shm_addr =
94
+ mmap (NULL , byte_size, PROT_WRITE, MAP_SHARED, shm_file->shm_fd_ , offset);
96
95
if (*shm_addr == MAP_FAILED) {
97
96
return -1 ;
98
97
}
@@ -118,29 +117,25 @@ SharedMemoryRegionCreate(
118
117
DWORD high_order_size = (upperbound_size >> 32 ) & 0xFFFFFFFF ;
119
118
DWORD low_order_size = upperbound_size & 0xFFFFFFFF ;
120
119
121
- HANDLE shm_file = CreateFileMapping (
120
+ HANDLE win_handle = CreateFileMapping (
122
121
INVALID_HANDLE_VALUE, // use paging file
123
122
NULL , // default security
124
123
PAGE_READWRITE, // read/write access
125
124
high_order_size, // maximum object size (high-order DWORD)
126
125
low_order_size, // maximum object size (low-order DWORD)
127
126
shm_key); // name of mapping object
128
127
129
- if (shm_file == NULL ) {
128
+ if (win_handle == NULL ) {
130
129
return -7 ;
131
130
}
132
131
132
+ ShmFile* shm_file = new ShmFile (win_handle);
133
133
// get base address of shared memory region
134
134
void * shm_addr = nullptr ;
135
- int err = SharedMemoryRegionMap (( void *) shm_file, 0 , byte_size, &shm_addr);
135
+ int err = SharedMemoryRegionMap (shm_file, 0 , byte_size, &shm_addr);
136
136
if (err == -1 ) {
137
137
return -4 ;
138
138
}
139
-
140
- // create a handle for the shared memory region
141
- *shm_handle = SharedMemoryHandleCreate (
142
- std::string (triton_shm_name), shm_addr, std::string (shm_key),
143
- (void *)shm_file, 0 , byte_size);
144
139
#else
145
140
// get shared memory region descriptor
146
141
int shm_fd = shm_open (shm_key, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
@@ -154,18 +149,18 @@ SharedMemoryRegionCreate(
154
149
return -3 ;
155
150
}
156
151
152
+ ShmFile* shm_file = new ShmFile (shm_fd);
157
153
// get base address of shared memory region
158
154
void * shm_addr = nullptr ;
159
- int err = SharedMemoryRegionMap (( void *)&shm_fd , 0 , byte_size, &shm_addr);
155
+ int err = SharedMemoryRegionMap (shm_file , 0 , byte_size, &shm_addr);
160
156
if (err == -1 ) {
161
157
return -4 ;
162
158
}
163
-
159
+ # endif
164
160
// create a handle for the shared memory region
165
161
*shm_handle = SharedMemoryHandleCreate (
166
- std::string (triton_shm_name), shm_addr, std::string (shm_key),
167
- (void *)&shm_fd, 0 , byte_size);
168
- #endif
162
+ std::string (triton_shm_name), shm_addr, std::string (shm_key), shm_file, 0 ,
163
+ byte_size);
169
164
return 0 ;
170
165
}
171
166
@@ -181,20 +176,20 @@ SharedMemoryRegionSet(
181
176
182
177
TRITONCLIENT_DECLSPEC int
183
178
GetSharedMemoryHandleInfo (
184
- void * shm_handle, char ** shm_addr, const char ** shm_key, void ** shm_file,
179
+ void * shm_handle, char ** shm_addr, const char ** shm_key, void * shm_file,
185
180
size_t * offset, size_t * byte_size)
186
181
{
187
- #ifdef _WIN32
188
- HANDLE* file = static_cast <HANDLE*>(shm_file);
189
- #else
190
- int * file = *reinterpret_cast <int **>(shm_file);
191
- #endif // _WIN32
192
182
SharedMemoryHandle* handle = static_cast <SharedMemoryHandle*>(shm_handle);
183
+ ShmFile* file = static_cast <ShmFile*>(shm_file);
193
184
*shm_addr = static_cast <char *>(handle->base_addr_ );
194
185
*shm_key = handle->shm_key_ .c_str ();
195
- *file = *(handle->platform_handle_ ->GetShmFile ());
196
186
*offset = handle->offset_ ;
197
187
*byte_size = handle->byte_size_ ;
188
+ #ifdef _WIN32
189
+ file->shm_handle_ = handle->platform_handle_ ->shm_handle_ ;
190
+ #else
191
+ file->shm_fd_ = handle->platform_handle_ ->shm_fd_ ;
192
+ #endif
198
193
return 0 ;
199
194
}
200
195
@@ -212,7 +207,7 @@ SharedMemoryRegionDestroy(void* shm_handle)
212
207
// We keep Windows shared memory handles open until we are done
213
208
// using them. When all handles are closed, the system will free
214
209
// the section of the paging file that the object uses.
215
- CloseHandle (*( handle->platform_handle_ ->GetShmFile ()) );
210
+ CloseHandle (handle->platform_handle_ ->shm_handle_ );
216
211
#else
217
212
int status = munmap (shm_addr, handle->byte_size_ );
218
213
if (status == -1 ) {
@@ -223,7 +218,7 @@ SharedMemoryRegionDestroy(void* shm_handle)
223
218
if (shm_fd == -1 ) {
224
219
return -5 ;
225
220
}
226
- close (*( handle->platform_handle_ ->GetShmFile ()) );
221
+ close (handle->platform_handle_ ->shm_fd_ );
227
222
#endif // _WIN32
228
223
229
224
// FIXME: Investigate use of smart pointers for this
0 commit comments