-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathshm_win.h
159 lines (142 loc) · 4.27 KB
/
shm_win.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
typedef struct {
HANDLE h; /* creator handle */
void* addr;
size_t len;
} ipc_shm_handle;
static void* ipc_shm_addr( ipc_shm_handle* h ) {
return h->addr;
}
static size_t ipc_shm_size( ipc_shm_handle* h ) {
return h->len;
}
static void ipc_shm_error( char* buf, size_t len, int code ) {
if( len > 0 ) {
if( 0 == FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
code,
0,
buf,
len,
NULL ) ) {
strncpy( buf, "unknown error", len-1 );
buf[ len-1 ] = '\0';
} else { /* Windows puts an extra newline in there! */
size_t n = strlen( buf );
while( n > 0 && isspace( (unsigned char)buf[ --n ] ) )
buf[ n ] = '\0';
}
}
}
static int ipc_shm_make_name_( char const* s, char** name ) {
size_t len = strlen( s );
if( len == 0 || strcspn( s, "/\\" ) != len )
return IPC_ERR( ERROR_INVALID_NAME );
/* FIXME: should we try a "Global\\" name first? */
#define P "Local\\"
*name = malloc( len + sizeof( P ) );
if( *name == NULL )
return IPC_ERR( ERROR_OUTOFMEMORY ); /* or ERROR_NOT_ENOUGH_MEMORY?! */
memcpy( *name, P, sizeof( P )-1 );
memcpy( *name+sizeof( P )-1, s, len+1 );
#undef P
return 0;
}
static int ipc_shm_create( ipc_shm_handle* h, char const* name,
size_t req ) {
char* rname = NULL;
int rv = ipc_shm_make_name_( name, &rname );
if( rv != 0 )
return IPC_ERR( rv );
if( req == 0 ) {
free( rname );
return IPC_ERR( ERROR_INVALID_PARAMETER );
}
h->h = CreateFileMappingA( INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
(DWORD)((req >> 16) >> 16),
(DWORD)req,
rname );
if( h->h == NULL ) {
int saved_errno = GetLastError();
free( rname );
return IPC_ERR( saved_errno );
} else if( GetLastError() == ERROR_ALREADY_EXISTS ) {
/* It seems we have no way of actually removing the shared memory
* object -- should we fail with EEXIST anyway?! */
CloseHandle( h->h );
free( rname );
return IPC_ERR( ERROR_ALREADY_EXISTS );
}
/* Windows automatically handles the lifetime of the shared memory
* object, so we don't need to keep track of the name! */
free( rname );
h->addr = MapViewOfFile( h->h,
FILE_MAP_ALL_ACCESS,
0,
0,
0 );
if( h->addr == NULL ) {
int saved_errno = GetLastError();
CloseHandle( h->h );
return IPC_ERR( saved_errno );
}
h->len = req;
return 0;
}
static int ipc_shm_attach( ipc_shm_handle* h, char const* name ) {
HANDLE hmap;
MEMORY_BASIC_INFORMATION meminfo;
char* rname = NULL;
int rv = ipc_shm_make_name_( name, &rname );
if( rv != 0 )
return IPC_ERR( rv );
hmap = OpenFileMappingA( FILE_MAP_ALL_ACCESS,
FALSE,
rname );
if( hmap == NULL ) {
int saved_errno = GetLastError();
free( rname );
return IPC_ERR( saved_errno );
}
free( rname );
h->addr = MapViewOfFile( hmap,
FILE_MAP_ALL_ACCESS,
0,
0,
0 );
if( h->addr == NULL ) {
int saved_errno = GetLastError();
CloseHandle( hmap );
return IPC_ERR( saved_errno );
}
CloseHandle( hmap );
if( VirtualQuery( h->addr, &meminfo, sizeof( meminfo ) ) == 0 ) {
int saved_errno = GetLastError();
UnmapViewOfFile( h->addr );
return IPC_ERR( saved_errno );
}
h->len = meminfo.RegionSize;
return 0;
}
static int ipc_shm_detach( ipc_shm_handle* h ) {
if( !UnmapViewOfFile( h->addr ) )
return IPC_ERR( GetLastError() );
return 0;
}
static int ipc_shm_remove( ipc_shm_handle* h ) {
if( !UnmapViewOfFile( h->addr ) ) {
int saved_errno = GetLastError();
CloseHandle( h->h );
return IPC_ERR( saved_errno );
}
CloseHandle( h->h );
return 0;
}