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:
parent
db868d0807
commit
67b499bbf8
1 changed files with 71 additions and 51 deletions
56
main/io.c
56
main/io.c
|
@ -73,15 +73,19 @@ 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))))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
tmp->needshrink = 0;
|
tmp->needshrink = 0;
|
||||||
tmp->fdcnt = 0;
|
tmp->fdcnt = 0;
|
||||||
tmp->maxfdcnt = GROW_SHRINK_SIZE/2;
|
tmp->maxfdcnt = GROW_SHRINK_SIZE/2;
|
||||||
tmp->current_ioc = -1;
|
tmp->current_ioc = -1;
|
||||||
|
|
||||||
if (!(tmp->fds = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->fds)))) {
|
if (!(tmp->fds = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->fds)))) {
|
||||||
free(tmp);
|
free(tmp);
|
||||||
tmp = NULL;
|
tmp = NULL;
|
||||||
|
@ -92,7 +96,7 @@ struct io_context *io_context_create(void)
|
||||||
tmp = NULL;
|
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,19 +180,25 @@ 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 (*id > ioc->fdcnt)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (fd > -1)
|
if (fd > -1)
|
||||||
ioc->fds[*id].fd = fd;
|
ioc->fds[*id].fd = fd;
|
||||||
if (callback)
|
if (callback)
|
||||||
|
@ -190,15 +207,14 @@ int *ast_io_change(struct io_context *ioc, int *id, int fd, ast_io_cb callback,
|
||||||
ioc->fds[*id].events = events;
|
ioc->fds[*id].events = events;
|
||||||
if (data)
|
if (data)
|
||||||
ioc->ior[*id].data = data;
|
ioc->ior[*id].data = data;
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
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,15 +273,14 @@ 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
|
||||||
|
@ -279,9 +297,10 @@ int ast_io_wait(struct io_context *ioc, int howlong)
|
||||||
ioc->current_ioc = -1;
|
ioc->current_ioc = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioc->needshrink)
|
if (ioc->needshrink)
|
||||||
io_shrink(ioc);
|
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");
|
||||||
|
|
Loading…
Reference in a new issue