-
Notifications
You must be signed in to change notification settings - Fork 1
/
inshm_fortran.c
352 lines (274 loc) · 7.56 KB
/
inshm_fortran.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/******************************************************************************
Copyright (c) 2018, Ioannis Nompelis
All rights reserved.
******************************************************************************/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include "inshm.h"
static struct inSHM_segment_s * global_inshm_handles = NULL;
static int global_inshm_handles_num = 0;
//
// Function to retrieve a handle from the global array of handles
// It enlarges the global array of handles if needed
//
int inSHM_Fortran_GetSegment(
int *handle,
int iverb )
{
int ichunk = 10;
int nhandles;
struct inSHM_segment_s *s;
int n;
if( global_inshm_handles == NULL ) {
global_inshm_handles_num = 0;
} else {
for( n=0; n<global_inshm_handles_num; ++n ) {
struct inSHM_segment_s *p = &( global_inshm_handles[n] );
if( p->shmid == -1 ) {
if( iverb != 0 )
printf("Reusing old (unsued) handle %d \n", n);
*handle = n;
return(0);
}
}
}
nhandles = global_inshm_handles_num + ichunk;
s = (struct inSHM_segment_s *) realloc( (void *) global_inshm_handles,
((size_t) nhandles) * sizeof(struct inSHM_segment_s) );
if( s == NULL ) {
if( iverb != 0 )
printf("Could not reallocate global inshm_s handles array!\n");
return(-1);
} else {
if( iverb != 0 )
printf("(Re)allocated global handles array \n");
}
if( iverb != 0 )
printf("Initializing newly added elements (%d) \n",ichunk);
for( n=global_inshm_handles_num; n<nhandles;++n ) {
struct inSHM_segment_s *p = &( s[ n ] );
p->shmid = -1;
p->key = 0;
p->size = 0;
p->shm = (void *) NULL;
}
*handle = global_inshm_handles_num;
global_inshm_handles = s;
global_inshm_handles_num = nhandles;
return(0);
}
//
// Function to potentially discard the global array of handles
//
int inSHM_Fortran_DropSegment(
int handle,
int iverb )
{
int n,idrop=0;
struct inSHM_segment_s *p;
// sanity check
if( global_inshm_handles == NULL ) {
if( iverb != 0 )
printf("Error: there should be no handles floating around! \n");
return(1);
}
if( handle >= global_inshm_handles_num ) {
if( iverb != 0 )
printf("Error: Invalid handle to discard! (%d) \n", handle );
return(2);
}
if( global_inshm_handles[ handle ].shmid >= 0 ) {
if( iverb != 0 )
printf("Error: Handle seems to be un-clean!\n");
return(3);
}
// drop the array if there are not segments in use
p = global_inshm_handles;
idrop = global_inshm_handles_num;
for(n=0;n<global_inshm_handles_num;++n) {
if( p[n].shmid == -1 ) --idrop;
}
if( idrop == 0 ) {
free( global_inshm_handles );
global_inshm_handles = NULL;
global_inshm_handles_num = 0;
}
return(0);
}
//
// Function to create a shared memory segment
//
int inSHM_Fortran_CreateSegment(
long *size,
int *handle,
int *shmid,
int iverb )
{
size_t isize = (size_t ) (*size);
struct inSHM_segment_s *s;
int ier,ihandle;
ier = inSHM_Fortran_GetSegment( &ihandle, iverb );
if( ier != 0 ) {
if( iverb != 0 )
printf("Could not get an unused or a new handle!\n");
return(1);
}
s = &( global_inshm_handles[ ihandle ]);
ier = inSHM_CreateSegmentPrivate( s, isize, iverb );
if( ier != 0 ) {
if( iverb != 0 )
printf("Could not create shared memory segment \n");
if( ier == 4 ) {
if( inSHM_DetachSegment( s, iverb ) != 0 ) {
if( iverb != 0 )
printf("Also had trouble detaching! \n");
}
s->shmid = -1;
s->size = 0;
// s->shm may be a number... No hope of doing anything about it here
}
(void) inSHM_Fortran_DropSegment( ihandle, iverb );
return(2);
}
*handle = ihandle;
*shmid = s->shmid;
return(0);
}
void inshm_fortran_createsegment_(
long *size,
int *handle,
int *shmid,
int *iverb, int *ier )
{
*ier = inSHM_Fortran_CreateSegment( size, handle, shmid, *iverb );
}
//
// Detach from a shared memory segment
//
int inSHM_Fortran_DetachSegment(
int handle,
int iverb )
{
struct inSHM_segment_s *s;
// sanity check
if( global_inshm_handles == NULL ) {
if( iverb != 0 )
printf("Error: there should be no handles floating around! \n");
return(1);
}
if( handle >= global_inshm_handles_num ) {
if( iverb != 0 )
printf("Error: Invalid handle to detach from (%d) \n", handle );
return(2);
}
if( global_inshm_handles[ handle ].shmid < 0 ) {
if( iverb != 0 )
printf("Error: Handle seems to have been cleaned \n");
// we will drop the segment even though we return an error code
(void) inSHM_Fortran_DropSegment( handle, iverb );
return(3);
}
s = &( global_inshm_handles[ handle ] );
if( inSHM_DetachSegment( s, iverb ) != 0 ) {
if( iverb != 0 )
printf("Error: Could not cleanly detach from segment \n");
return(4);
}
(void) inSHM_Fortran_DropSegment( handle, iverb );
return(0);
}
void inshm_fortran_detachsegment_(
int *handle,
int *iverb, int *ier )
{
*ier = inSHM_Fortran_DetachSegment( *handle, *iverb );
}
//
// Attach to a shared memory segment
//
int inSHM_Fortran_AttachSegment(
int shmid,
int *handle,
int iverb )
{
struct inSHM_segment_s *s;
int ier;
ier = inSHM_Fortran_GetSegment( handle, iverb );
if( ier != 0 ) {
if( iverb != 0 )
printf("Could not get an unused or a new handle!\n");
return(1);
}
s = &( global_inshm_handles[ *handle ]);
ier = inSHM_AttachSegment( s, shmid, iverb );
if( ier != 0 ) {
if( iverb != 0 )
printf("Error: Could not attach to shared memory segment %d \n",
shmid );
return(2);
}
return(0);
}
void inshm_fortran_attachsegment_(
int *shmid,
int *handle,
int *iverb,
int *ier )
{
*ier = inSHM_Fortran_AttachSegment( *shmid, handle, *iverb );
}
//
// Get the identifier of a shared memory segment
//
int inSHM_Fortran_SegmentID(
int handle,
int iverb )
{
struct inSHM_segment_s *s;
// sanity check
if( global_inshm_handles == NULL ) {
if( iverb != 0 )
printf("Error: there should be no handles floating around! \n");
return(-1);
}
if( handle >= global_inshm_handles_num ) {
if( iverb != 0 )
printf("Error: Invalid handle to attach to (%d) \n", handle );
return(-1);
}
s = &( global_inshm_handles[ handle ] );
return( s->shmid );
}
int inshm_fortran_segmentid_( int *handle, int *iverb )
{
return( inSHM_Fortran_SegmentID( *handle, *iverb ) );
}
//
// Get the pointer to the allocated memory of a shared memory segment
//
void * inSHM_Fortran_SegmentPointer(
int handle,
int iverb )
{
struct inSHM_segment_s *s;
// sanity check
if( global_inshm_handles == NULL ) {
if( iverb != 0 )
printf("Error: there should be no handles floating around! \n");
return( NULL );
}
if( handle >= global_inshm_handles_num ) {
if( iverb != 0 )
printf("Error: Invalid handle to attach to (%d) \n", handle );
return( NULL );
}
s = &( global_inshm_handles[ handle ] );
return( s->shm );
}
void * inshm_fortran_segmentpointer_( int *handle, int *iverb )
{
return( inSHM_Fortran_SegmentPointer( *handle, *iverb ) );
}