25 #define sion_assert(expr, args) (assert(expr)) 27 #define SION_FDDATA_INITIALIZER {0,0,-1,NULL} 30 static struct _sion_fddata {
35 } sion_fd_data = SION_FDDATA_INITIALIZER;
88 new_size = fdd->size + SION_FD_CHUNK;
91 fdd->list = realloc(fdd->list, new_size *
sizeof(
struct _sion_fd));
94 fdd->list = malloc(new_size *
sizeof(
struct _sion_fd));
96 sion_assert(fdd->list, (
"out of memory"));
98 for (i = fdd->size; i < new_size; i++) {
99 fdd->list[i].state = 0;
100 fdd->list[i].type = 0;
101 fdd->list[i].data = NULL;
104 fdd->size = new_size;
105 fdd->nfree += SION_FD_CHUNK;
109 for (i = 0; i < fdd->size; i++) {
110 if (fdd->list[i].state == 0) {
111 fdd->list[i].data = data;
112 fdd->list[i].type = type;
113 fdd->list[i].state = 1;
124 for (i = fdd->last_freed + 1; i < fdd->last_freed + 1 + fdd->size; i++) {
125 int imod = i % fdd->size;
126 if (fdd->list[imod].state == -1) {
127 fdd->list[imod].data = data;
128 fdd->list[imod].type = type;
129 fdd->list[imod].state = 1;
135 sion_assert(0, (
"oops - we should never get here"));
146 int _sion_set_fd(sion_fddata *fdd,
int fd,
void *data,
int type,
char *text)
149 if (text && (!*text))
150 text =
"_sion_set_fd";
154 fprintf(stderr,
"%s: trying to set an invalid descriptor fd=%d.\n", text, fd);
161 if (fd >= fdd->size) {
168 new_size = fdd->size + SION_FD_CHUNK;
169 while (fd >= new_size) {
170 new_size += SION_FD_CHUNK;
174 fdd->list = realloc(fdd->list, new_size *
sizeof(
struct _sion_fd));
177 fdd->list = malloc(new_size *
sizeof(
struct _sion_fd));
179 sion_assert(fdd->list, (
"out of memory"));
181 for (i = fdd->size; i < new_size; i++) {
182 fdd->list[i].state = 0;
183 fdd->list[i].type = 0;
184 fdd->list[i].data = NULL;
187 fdd->nfree += new_size - fdd->size;
188 fdd->size = new_size;
190 else if (fdd->list[fd].state == 1) {
192 fprintf(stderr,
"%s: trying to set a descriptor that is in use fd=%d.\n", text, fd);
198 fdd->list[fd].data = data;
199 fdd->list[fd].type = type;
200 fdd->list[fd].state = 1;
215 if ((fd < fdd->size) && (fd >= 0) && (fdd->list) && (fdd->list[fd].state == 1)) {
216 fdd->list[fd].state = -1;
218 fdd->last_freed = fd;
220 if (fdd->nfree == fdd->size) {
233 text =
"_sion_free_fd";
234 fprintf(stderr,
"%s: trying to free an invalid descriptor fd=%d.\n", text, fd);
248 if ((fd < fdd->size) && (fd >= 0) && (fdd->list[fd].state == 1)) {
249 void *ptr = fdd->list[fd].data;
266 if ((fd < fdd->size) && (fd >= 0) && (fdd->list[fd].state == 1)) {
267 int type = fdd->list[fd].type;
274 text =
"_sion_fd2type";
275 fprintf(stderr,
"%s: invalid descriptor fd=%d.\n", text, fd);
292 for (i = 0; i < fdd->size; i++) {
293 if ((fdd->list[i].state == 1) && (fdd->list[i].data == ptr)) {
300 text =
"_sion_ptr2fd";
301 fprintf(stderr,
"%s: invalid pointer ptr=%x.\n", text, (
unsigned) (
size_t) ptr);
316 if ((fd < fdd->size) && (fd >= 0) && (fdd->list[fd].state == 1)) {
317 fdd->list[fd].data = data;
318 fdd->list[fd].type = type;
325 text =
"_sion_reassign_fd";
326 fprintf(stderr,
"%s: invalid descriptor fd=%d.\n", text, fd);
int _sion_reassignvcd(int sid, void *data, int type)
int _sion_new_fd(sion_fddata *fdd, void *data, int type)
int _sion_vcdtype(int sid)
void * _sion_fd2ptr(sion_fddata *fdd, int fd)
void * _sion_vcdtovcon(int sid)
int _sion_reassign_fd(sion_fddata *fdd, int fd, void *data, int type, char *text)
int _sion_newvcd(void *data, int type)
int _sion_fd2type(sion_fddata *fdd, int fd, char *text)
int _sion_ptr2fd(sion_fddata *fdd, void *ptr, char *text)
int _sion_free_fd(sion_fddata *fdd, int fd, char *text)
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)