forked from indilib/indi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharedblob.c
275 lines (226 loc) · 6.55 KB
/
sharedblob.c
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#if 0
INDI Driver Functions
Copyright (C) 2020 by Pawel Soja <[email protected]>
Copyright (C) 2003 - 2020 Jasem Mutlaq
Copyright (C) 2003 - 2006 Elwood C. Downey
This library is free software;
you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY;
without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library;
if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA
#endif
#ifdef __linux__
#define _GNU_SOURCE
#include <linux/unistd.h>
#endif
#include "indidriver.h"
#include "base64.h"
#include "indicom.h"
#include "indidevapi.h"
#include "locale_compat.h"
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <assert.h>
// A shared buffer will be allocated by chunk of at least 1M (must be ^ 2)
#define BLOB_SIZE_UNIT 0x100000
static pthread_mutex_t shared_buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
typedef struct shared_buffer {
void * mapstart;
size_t size;
size_t allocated;
int fd;
int sealed;
struct shared_buffer * prev, *next;
} shared_buffer;
/* Return the buffer size required for storage (rounded to next BLOB_SIZE_UNIT) */
static size_t allocation(size_t storage) {
if (storage == 0) {
return BLOB_SIZE_UNIT;
}
return (storage + BLOB_SIZE_UNIT - 1) & ~(BLOB_SIZE_UNIT - 1);
}
static void sharedBufferAdd(shared_buffer * sb);
static shared_buffer * sharedBufferRemove(void * mapstart);
static shared_buffer * sharedBufferFind(void * mapstart);
void * IDSharedBlobAlloc(size_t size) {
shared_buffer * sb = (shared_buffer*)malloc(sizeof(shared_buffer));
if (sb == NULL) goto ERROR;
sb->size = size;
sb->allocated = allocation(size);
sb->sealed = 0;
sb->fd = memfd_create("indiblob", MFD_ALLOW_SEALING|MFD_CLOEXEC);
if (sb->fd == -1) goto ERROR;
int ret = ftruncate(sb->fd, size);
if (ret == -1) goto ERROR;
// FIXME: try to map far more than sb->allocated, to allow efficient mremap
sb->mapstart = mmap(0, sb->allocated, PROT_READ|PROT_WRITE, MAP_SHARED, sb->fd, 0);
if (sb->mapstart == MAP_FAILED) goto ERROR;
sharedBufferAdd(sb);
return sb->mapstart;
ERROR:
if (sb) {
int e = errno;
if (sb->fd != -1) close(sb->fd);
free(sb);
errno = e;
}
return NULL;
}
void * IDSharedBlobAttach(int fd, size_t size) {
shared_buffer * sb = (shared_buffer*)malloc(sizeof(shared_buffer));
if (sb == NULL) goto ERROR;
sb->fd = fd;
sb->size = size;
sb->allocated = size;
sb->sealed = 1;
sb->mapstart = mmap(0, sb->allocated, PROT_READ|PROT_WRITE, MAP_PRIVATE, sb->fd, 0);
if (sb->mapstart == MAP_FAILED) goto ERROR;
sharedBufferAdd(sb);
return sb->mapstart;
ERROR:
if (sb) {
int e = errno;
free(sb);
errno = e;
}
return NULL;
}
void IDSharedBlobFree(void * ptr) {
shared_buffer * sb = sharedBufferRemove(ptr);
if (sb == NULL) {
// Error ?
free(ptr);
return;
}
if (munmap(sb->mapstart, sb->allocated) == -1) {
perror("shared buffer munmap");
_exit(1);
}
if (close(sb->fd) == -1) {
perror("shared buffer close");
}
free(sb);
}
void IDSharedBlobDettach(void * ptr) {
shared_buffer * sb = sharedBufferRemove(ptr);
if (sb == NULL) {
// Error ?
free(ptr);
return;
}
if (munmap(sb->mapstart, sb->allocated) == -1) {
perror("shared buffer munmap");
_exit(1);
}
free(sb);
}
void * IDSharedBlobRealloc(void * ptr, size_t size) {
shared_buffer * sb;
sb = sharedBufferFind(ptr);
if (sb == NULL) {
return realloc(ptr, size);
}
if (sb->size == size) {
return ptr;
}
int ret = ftruncate(sb->fd, size);
if (ret == -1) return NULL;
sb->size = size;
size_t reallocated = allocation(size);
if (reallocated == sb->allocated) {
return ptr;
}
// FIXME: compatibility path for MACOS ?
void * remaped = mremap(sb->mapstart, sb->allocated, reallocated, MREMAP_MAYMOVE);
if (remaped == MAP_FAILED) return NULL;
sb->allocated = reallocated;
sb->mapstart = remaped;
return remaped;
}
int IDSharedBlobGetFd(void * ptr) {
shared_buffer * sb;
sb = sharedBufferFind(ptr);
if (sb == NULL) {
errno = EINVAL;
return -1;
}
return sb->fd;
}
void IDSharedBlobSeal(void * ptr) {
shared_buffer * sb;
sb = sharedBufferFind(ptr);
if (sb->sealed) return;
void * ret = mmap(sb->mapstart, sb->allocated, PROT_READ, MAP_SHARED|MAP_FIXED, sb->fd, 0);
if (ret == MAP_FAILED) {
perror("remap readonly failed");
} else {
sb->sealed = 1;
}
}
static shared_buffer * first = NULL, *last = NULL;
static void sharedBufferAdd(shared_buffer * sb) {
pthread_mutex_lock(&shared_buffer_mutex);
// Chained insert at start
sb->prev = NULL;
sb->next = first;
if (first) {
first->prev = sb;
} else {
last = sb;
}
first = sb;
pthread_mutex_unlock(&shared_buffer_mutex);
}
static shared_buffer * sharedBufferFindUnlocked(void * mapstart) {
shared_buffer * sb = first;
while(sb) {
if (sb->mapstart == mapstart) {
return sb;
}
sb = sb->next;
}
return NULL;
}
static shared_buffer * sharedBufferRemove(void * mapstart) {
pthread_mutex_lock(&shared_buffer_mutex);
shared_buffer * sb = sharedBufferFindUnlocked(mapstart);
if (sb != NULL) {
if (sb->prev) {
sb->prev->next = sb->next;
} else {
first = sb->next;
}
if (sb->next) {
sb->next->prev = sb->prev;
} else {
last = sb->prev;
}
}
pthread_mutex_unlock(&shared_buffer_mutex);
return sb;
}
static shared_buffer * sharedBufferFind(void * mapstart) {
pthread_mutex_lock(&shared_buffer_mutex);
shared_buffer * sb = sharedBufferFindUnlocked(mapstart);
pthread_mutex_unlock(&shared_buffer_mutex);
return sb;
}