4 * Mark Spencer <markster@marko.net>
6 * Copyright(C) 1999, Adtran, Inc.
8 * Distributed under the terms of the GNU General Public License (GPL) Version 2
10 * I/O Managment (Derived from Cheops-NG)
18 #include <asterisk/io.h>
19 #include <asterisk/logger.h>
28 * Kept for each file descriptor
31 ast_io_cb callback; /* What is to be called */
32 void *data; /* Data to be passed */
33 int *id; /* ID number */
36 /* These two arrays are keyed with
37 the same index. it's too bad that
38 pollfd doesn't have a callback field
39 or something like that. They grow as
40 needed, by GROW_SHRINK_AMOUNT structures
43 #define GROW_SHRINK_SIZE 512
45 /* Global variables are now in a struct in order to be
50 /* Associated I/O records */
52 /* First available fd */
54 /* Maximum available fd */
55 unsigned int maxfdcnt;
56 /* Currently used io callback */
61 struct io_context *io_context_create()
63 /* Create an I/O context */
64 struct io_context *tmp;
65 tmp = malloc(sizeof(struct io_context));
71 tmp->current_ioc = -1;
76 void io_context_destroy(struct io_context *ioc)
78 /* Free associated memory with an I/O context */
86 static int io_grow(struct io_context *ioc)
89 * Grow the size of our arrays. Return 0 on success or
93 DEBUG(ast_log(LOG_DEBUG, "io_grow()\n"));
94 ioc->maxfdcnt += GROW_SHRINK_SIZE;
95 tmp = realloc(ioc->ior, (ioc->maxfdcnt + 1) * sizeof(struct io_rec));
97 ioc->ior = (struct io_rec *)tmp;
98 tmp = realloc(ioc->fds, (ioc->maxfdcnt + 1) * sizeof(struct pollfd));
103 * Not enough memory for the pollfd. Not really any need
104 * to shrink back the iorec's as we'll probably want to
105 * grow them again soon when more memory is available, and
106 * then they'll already be the right size
108 ioc->maxfdcnt -= GROW_SHRINK_SIZE;
114 * Out of memory. We return to the old size, and return a failure
116 ioc->maxfdcnt -= GROW_SHRINK_SIZE;
122 int *ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data)
125 * Add a new I/O entry for this file descriptor
126 * with the given event mask, to call callback with
127 * data as an argument. Returns NULL on failure.
129 DEBUG(ast_log(LOG_DEBUG, "ast_io_add()\n"));
130 if (ioc->fdcnt < ioc->maxfdcnt) {
132 * We don't have enough space for this entry. We need to
133 * reallocate maxfdcnt poll fd's and io_rec's, or back out now.
140 * At this point, we've got sufficiently large arrays going
141 * and we can make an entry for it in the pollfd and io_r
144 ioc->fds[ioc->fdcnt].fd = fd;
145 ioc->fds[ioc->fdcnt].events = events;
146 ioc->ior[ioc->fdcnt].callback = callback;
147 ioc->ior[ioc->fdcnt].data = data;
148 ioc->ior[ioc->fdcnt].id = (int *)malloc(sizeof(int));
149 /* Bonk if we couldn't allocate an int */
150 if (!ioc->ior[ioc->fdcnt].id)
152 *ioc->ior[ioc->fdcnt].id = ioc->fdcnt;
153 return ioc->ior[ioc->fdcnt++].id;
156 int *ast_io_change(struct io_context *ioc, int *id, int fd, ast_io_cb callback, short events, void *data)
158 if (*id < ioc->fdcnt) {
160 ioc->fds[*id].fd = fd;
162 ioc->ior[*id].callback = callback;
164 ioc->fds[*id].events = events;
166 ioc->ior[*id].data = data;
171 static int io_shrink(struct io_context *ioc, int which)
174 * Bring the fields from the very last entry to cover over
175 * the entry we are removing, then decrease the size of the
181 free(ioc->ior[which].id);
183 /* If we're not deleting the last one, move the last one to
184 the current position */
185 if (which != ioc->fdcnt) {
186 ioc->fds[which] = ioc->fds[ioc->fdcnt];
187 ioc->ior[which] = ioc->ior[ioc->fdcnt];
188 *ioc->ior[which].id = which;
190 /* FIXME: We should free some memory if we have lots of unused
195 int ast_io_remove(struct io_context *ioc, int *id)
197 if (ioc->current_ioc == *id) {
198 ast_log(LOG_NOTICE, "Callback for %d tried to remove itself\n", *id);
201 if (*id < ioc->fdcnt) {
202 return io_shrink(ioc, *id);
204 ast_log(LOG_NOTICE, "Unable to remove unknown id %d\n", *id);
209 int ast_io_wait(struct io_context *ioc, int howlong)
212 * Make the poll call, and call
213 * the callbacks for anything that needs
218 DEBUG(ast_log(LOG_DEBUG, "ast_io_wait()\n"));
219 res = poll(ioc->fds, ioc->fdcnt, howlong);
224 for(x=0;x<ioc->fdcnt;x++) {
225 if (ioc->fds[x].revents) {
226 /* There's an event waiting */
227 ioc->current_ioc = *ioc->ior[x].id;
228 if (!ioc->ior[x].callback(ioc->ior[x].id, ioc->fds[x].fd, ioc->fds[x].revents, ioc->ior[x].data)) {
229 /* Time to delete them since they returned a 0 */
232 ioc->current_ioc = -1;
239 void ast_io_dump(struct io_context *ioc)
242 * Print some debugging information via
243 * the logger interface
246 ast_log(LOG_DEBUG, "Asterisk IO Dump: %d entries, %d max entries\n", ioc->fdcnt, ioc->maxfdcnt);
247 ast_log(LOG_DEBUG, "================================================\n");
248 ast_log(LOG_DEBUG, "| ID FD Callback Data Events |\n");
249 ast_log(LOG_DEBUG, "+------+------+-----------+-----------+--------+\n");
250 for (x=0;x<ioc->fdcnt;x++) {
251 ast_log(LOG_DEBUG, "| %.4d | %.4d | %p | %p | %.6x |\n",
254 ioc->ior[x].callback,
258 ast_log(LOG_DEBUG, "================================================\n");