17 #define _XOPEN_SOURCE 700
27 #define sion_assert(expr, args) (assert(expr))
29 #define SION_FDDATA_INITIALIZER {0,0,-1,NULL}
32 static struct _sion_fddata {
37 } sion_fd_data = SION_FDDATA_INITIALIZER;
90 new_size = fdd->size + SION_FD_CHUNK;
93 fdd->list = realloc(fdd->list, new_size *
sizeof(
struct _sion_fd));
96 fdd->list = malloc(new_size *
sizeof(
struct _sion_fd));
98 sion_assert(fdd->list, (
"out of memory"));
100 for (i = fdd->size; i < new_size; i++) {
101 fdd->list[i].state = 0;
102 fdd->list[i].type = 0;
103 fdd->list[i].data = NULL;
106 fdd->size = new_size;
107 fdd->nfree += SION_FD_CHUNK;
111 for (i = 0; i < fdd->size; i++) {
112 if (fdd->list[i].state == 0) {
113 fdd->list[i].data = data;
114 fdd->list[i].type = type;
115 fdd->list[i].state = 1;
126 for (i = fdd->last_freed + 1; i < fdd->last_freed + 1 + fdd->size; i++) {
127 int imod = i % fdd->size;
128 if (fdd->list[imod].state == -1) {
129 fdd->list[imod].data = data;
130 fdd->list[imod].type = type;
131 fdd->list[imod].state = 1;
137 sion_assert(0, (
"oops - we should never get here"));
148 int _sion_set_fd(sion_fddata *fdd,
int fd,
void *data,
int type,
char *text)
151 if (text && (!*text))
152 text =
"_sion_set_fd";
156 fprintf(stderr,
"%s: trying to set an invalid descriptor fd=%d.\n", text, fd);
163 if (fd >= fdd->size) {
170 new_size = fdd->size + SION_FD_CHUNK;
171 while (fd >= new_size) {
172 new_size += SION_FD_CHUNK;
176 fdd->list = realloc(fdd->list, new_size *
sizeof(
struct _sion_fd));
179 fdd->list = malloc(new_size *
sizeof(
struct _sion_fd));
181 sion_assert(fdd->list, (
"out of memory"));
183 for (i = fdd->size; i < new_size; i++) {
184 fdd->list[i].state = 0;
185 fdd->list[i].type = 0;
186 fdd->list[i].data = NULL;
189 fdd->nfree += new_size - fdd->size;
190 fdd->size = new_size;
192 else if (fdd->list[fd].state == 1) {
194 fprintf(stderr,
"%s: trying to set a descriptor that is in use fd=%d.\n", text, fd);
200 fdd->list[fd].data = data;
201 fdd->list[fd].type = type;
202 fdd->list[fd].state = 1;
217 if ((fd < fdd->size) && (fd >= 0) && (fdd->list) && (fdd->list[fd].state == 1)) {
218 fdd->list[fd].state = -1;
220 fdd->last_freed = fd;
222 if (fdd->nfree == fdd->size) {
235 text =
"_sion_free_fd";
236 fprintf(stderr,
"%s: trying to free an invalid descriptor fd=%d.\n", text, fd);
250 if ((fd < fdd->size) && (fd >= 0) && (fdd->list[fd].state == 1)) {
251 void *ptr = fdd->list[fd].data;
268 if ((fd < fdd->size) && (fd >= 0) && (fdd->list[fd].state == 1)) {
269 int type = fdd->list[fd].type;
276 text =
"_sion_fd2type";
277 fprintf(stderr,
"%s: invalid descriptor fd=%d.\n", text, fd);
294 for (i = 0; i < fdd->size; i++) {
295 if ((fdd->list[i].state == 1) && (fdd->list[i].data == ptr)) {
302 text =
"_sion_ptr2fd";
303 fprintf(stderr,
"%s: invalid pointer ptr=%x.\n", text, (
unsigned) (
size_t) ptr);
318 if ((fd < fdd->size) && (fd >= 0) && (fdd->list[fd].state == 1)) {
319 fdd->list[fd].data = data;
320 fdd->list[fd].type = type;
327 text =
"_sion_reassign_fd";
328 fprintf(stderr,
"%s: invalid descriptor fd=%d.\n", text, fd);
callback wrappers for OMP thread safe fd manipulation
int _sion_reassignvcd(int sid, void *data, int type)
int _sion_free_fd(sion_fddata *fdd, int fd, char *text)
int _sion_ptr2fd(sion_fddata *fdd, void *ptr, char *text)
void * _sion_vcdtovcon(int sid)
int _sion_vcdtype(int sid)
int _sion_newvcd(void *data, int type)
int _sion_reassign_fd(sion_fddata *fdd, int fd, void *data, int type, char *text)
void * _sion_fd2ptr(sion_fddata *fdd, int fd)
int _sion_new_fd(sion_fddata *fdd, void *data, int type)
int _sion_fd_size(sion_fddata *fdd)
int _sion_freevcd(int sid)
int _sion_set_fd(sion_fddata *fdd, int fd, void *data, int type, char *text)
int _sion_fd2type(sion_fddata *fdd, int fd, char *text)