Clean up the I/O context handler.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@54552 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Joshua Colp 2007-02-15 02:11:34 +00:00
parent db868d0807
commit 67b499bbf8
1 changed files with 71 additions and 51 deletions

122
main/io.c
View File

@ -73,26 +73,30 @@ struct io_context {
int needshrink; /*!< Whether something has been deleted */ int needshrink; /*!< Whether something has been deleted */
}; };
/* Create an I/O context */
struct io_context *io_context_create(void) struct io_context *io_context_create(void)
{ {
/* Create an I/O context */ struct io_context *tmp = NULL;
struct io_context *tmp;
if ((tmp = ast_malloc(sizeof(*tmp)))) { if (!(tmp = ast_malloc(sizeof(*tmp))))
tmp->needshrink = 0; return NULL;
tmp->fdcnt = 0;
tmp->maxfdcnt = GROW_SHRINK_SIZE/2; tmp->needshrink = 0;
tmp->current_ioc = -1; tmp->fdcnt = 0;
if (!(tmp->fds = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->fds)))) { tmp->maxfdcnt = GROW_SHRINK_SIZE/2;
tmp->current_ioc = -1;
if (!(tmp->fds = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->fds)))) {
free(tmp);
tmp = NULL;
} else {
if (!(tmp->ior = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->ior)))) {
free(tmp->fds);
free(tmp); free(tmp);
tmp = NULL; tmp = NULL;
} else {
if (!(tmp->ior = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->ior)))) {
free(tmp->fds);
free(tmp);
tmp = NULL;
}
} }
} }
return tmp; return tmp;
} }
@ -103,6 +107,7 @@ void io_context_destroy(struct io_context *ioc)
free(ioc->fds); free(ioc->fds);
if (ioc->ior) if (ioc->ior)
free(ioc->ior); free(ioc->ior);
free(ioc); free(ioc);
} }
@ -113,8 +118,11 @@ void io_context_destroy(struct io_context *ioc)
static int io_grow(struct io_context *ioc) static int io_grow(struct io_context *ioc)
{ {
void *tmp; void *tmp;
DEBUG(ast_log(LOG_DEBUG, "io_grow()\n")); DEBUG(ast_log(LOG_DEBUG, "io_grow()\n"));
ioc->maxfdcnt += GROW_SHRINK_SIZE; ioc->maxfdcnt += GROW_SHRINK_SIZE;
if ((tmp = ast_realloc(ioc->ior, (ioc->maxfdcnt + 1) * sizeof(*ioc->ior)))) { if ((tmp = ast_realloc(ioc->ior, (ioc->maxfdcnt + 1) * sizeof(*ioc->ior)))) {
ioc->ior = tmp; ioc->ior = tmp;
if ((tmp = ast_realloc(ioc->fds, (ioc->maxfdcnt + 1) * sizeof(*ioc->fds)))) { if ((tmp = ast_realloc(ioc->fds, (ioc->maxfdcnt + 1) * sizeof(*ioc->fds)))) {
@ -137,6 +145,7 @@ static int io_grow(struct io_context *ioc)
ioc->maxfdcnt -= GROW_SHRINK_SIZE; ioc->maxfdcnt -= GROW_SHRINK_SIZE;
return -1; return -1;
} }
return 0; return 0;
} }
@ -149,7 +158,9 @@ static int io_grow(struct io_context *ioc)
int *ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data) int *ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data)
{ {
int *ret; int *ret;
DEBUG(ast_log(LOG_DEBUG, "ast_io_add()\n")); DEBUG(ast_log(LOG_DEBUG, "ast_io_add()\n"));
if (ioc->fdcnt >= ioc->maxfdcnt) { if (ioc->fdcnt >= ioc->maxfdcnt) {
/* /*
* We don't have enough space for this entry. We need to * We don't have enough space for this entry. We need to
@ -169,36 +180,41 @@ int *ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events
ioc->fds[ioc->fdcnt].revents = 0; ioc->fds[ioc->fdcnt].revents = 0;
ioc->ior[ioc->fdcnt].callback = callback; ioc->ior[ioc->fdcnt].callback = callback;
ioc->ior[ioc->fdcnt].data = data; ioc->ior[ioc->fdcnt].data = data;
if (!(ioc->ior[ioc->fdcnt].id = ast_malloc(sizeof(*ioc->ior[ioc->fdcnt].id)))) { if (!(ioc->ior[ioc->fdcnt].id = ast_malloc(sizeof(*ioc->ior[ioc->fdcnt].id)))) {
/* Bonk if we couldn't allocate an int */ /* Bonk if we couldn't allocate an int */
return NULL; return NULL;
} }
*(ioc->ior[ioc->fdcnt].id) = ioc->fdcnt; *(ioc->ior[ioc->fdcnt].id) = ioc->fdcnt;
ret = ioc->ior[ioc->fdcnt].id; ret = ioc->ior[ioc->fdcnt].id;
ioc->fdcnt++; ioc->fdcnt++;
return ret; return ret;
} }
int *ast_io_change(struct io_context *ioc, int *id, int fd, ast_io_cb callback, short events, void *data) int *ast_io_change(struct io_context *ioc, int *id, int fd, ast_io_cb callback, short events, void *data)
{ {
if (*id < ioc->fdcnt) { /* If this id exceeds our file descriptor count it doesn't exist here */
if (fd > -1) if (*id > ioc->fdcnt)
ioc->fds[*id].fd = fd; return NULL;
if (callback)
ioc->ior[*id].callback = callback; if (fd > -1)
if (events) ioc->fds[*id].fd = fd;
ioc->fds[*id].events = events; if (callback)
if (data) ioc->ior[*id].callback = callback;
ioc->ior[*id].data = data; if (events)
return id; ioc->fds[*id].events = events;
} if (data)
return NULL; ioc->ior[*id].data = data;
return id;
} }
static int io_shrink(struct io_context *ioc) static int io_shrink(struct io_context *ioc)
{ {
int getfrom; int getfrom, putto = 0;
int putto = 0;
/* /*
* Bring the fields from the very last entry to cover over * Bring the fields from the very last entry to cover over
* the entry we are removing, then decrease the size of the * the entry we are removing, then decrease the size of the
@ -225,10 +241,12 @@ static int io_shrink(struct io_context *ioc)
int ast_io_remove(struct io_context *ioc, int *_id) int ast_io_remove(struct io_context *ioc, int *_id)
{ {
int x; int x;
if (!_id) { if (!_id) {
ast_log(LOG_WARNING, "Asked to remove NULL?\n"); ast_log(LOG_WARNING, "Asked to remove NULL?\n");
return -1; return -1;
} }
for (x = 0; x < ioc->fdcnt; x++) { for (x = 0; x < ioc->fdcnt; x++) {
if (ioc->ior[x].id == _id) { if (ioc->ior[x].id == _id) {
/* Free the int immediately and set to NULL so we know it's unused now */ /* Free the int immediately and set to NULL so we know it's unused now */
@ -244,6 +262,7 @@ int ast_io_remove(struct io_context *ioc, int *_id)
} }
ast_log(LOG_NOTICE, "Unable to remove unknown id %p\n", _id); ast_log(LOG_NOTICE, "Unable to remove unknown id %p\n", _id);
return -1; return -1;
} }
@ -254,34 +273,34 @@ int ast_io_remove(struct io_context *ioc, int *_id)
*/ */
int ast_io_wait(struct io_context *ioc, int howlong) int ast_io_wait(struct io_context *ioc, int howlong)
{ {
int res; int res, x, origcnt;
int x;
int origcnt;
DEBUG(ast_log(LOG_DEBUG, "ast_io_wait()\n")); DEBUG(ast_log(LOG_DEBUG, "ast_io_wait()\n"));
res = poll(ioc->fds, ioc->fdcnt, howlong);
if (res > 0) { if ((res = poll(ioc->fds, ioc->fdcnt, howlong)) <= 0)
/* return res;
* At least one event
*/ /* At least one event tripped */
origcnt = ioc->fdcnt; origcnt = ioc->fdcnt;
for (x = 0; x < origcnt; x++) { for (x = 0; x < origcnt; x++) {
/* Yes, it is possible for an entry to be deleted and still have an /* Yes, it is possible for an entry to be deleted and still have an
event waiting if it occurs after the original calling id */ event waiting if it occurs after the original calling id */
if (ioc->fds[x].revents && ioc->ior[x].id) { if (ioc->fds[x].revents && ioc->ior[x].id) {
/* There's an event waiting */ /* There's an event waiting */
ioc->current_ioc = *ioc->ior[x].id; ioc->current_ioc = *ioc->ior[x].id;
if (ioc->ior[x].callback) { if (ioc->ior[x].callback) {
if (!ioc->ior[x].callback(ioc->ior[x].id, ioc->fds[x].fd, ioc->fds[x].revents, ioc->ior[x].data)) { if (!ioc->ior[x].callback(ioc->ior[x].id, ioc->fds[x].fd, ioc->fds[x].revents, ioc->ior[x].data)) {
/* Time to delete them since they returned a 0 */ /* Time to delete them since they returned a 0 */
ast_io_remove(ioc, ioc->ior[x].id); ast_io_remove(ioc, ioc->ior[x].id);
}
} }
ioc->current_ioc = -1;
} }
ioc->current_ioc = -1;
} }
if (ioc->needshrink)
io_shrink(ioc);
} }
if (ioc->needshrink)
io_shrink(ioc);
return res; return res;
} }
@ -292,6 +311,7 @@ void ast_io_dump(struct io_context *ioc)
* the logger interface * the logger interface
*/ */
int x; int x;
ast_log(LOG_DEBUG, "Asterisk IO Dump: %d entries, %d max entries\n", ioc->fdcnt, ioc->maxfdcnt); ast_log(LOG_DEBUG, "Asterisk IO Dump: %d entries, %d max entries\n", ioc->fdcnt, ioc->maxfdcnt);
ast_log(LOG_DEBUG, "================================================\n"); ast_log(LOG_DEBUG, "================================================\n");
ast_log(LOG_DEBUG, "| ID FD Callback Data Events |\n"); ast_log(LOG_DEBUG, "| ID FD Callback Data Events |\n");