Add dynamic resizing of returned events array if we keep getting too many events

This commit is contained in:
Laurence Withers 2007-11-05 10:42:39 +00:00
parent 9046c9b878
commit 3fc440edab
1 changed files with 13 additions and 2 deletions

View File

@ -129,6 +129,7 @@ int lwevent_wait(int timeout_ms)
{ {
int ret, i, revents; int ret, i, revents;
struct lwevent* ev; struct lwevent* ev;
struct epoll_event* new_revents;
_lwevent_invalid_head = 0; _lwevent_invalid_head = 0;
@ -150,14 +151,24 @@ int lwevent_wait(int timeout_ms)
ev->callback(ev, revents); ev->callback(ev, revents);
} }
/* possibly resize returned events array */
++_lw_revents_count;
if(i == ret) ++_lw_revents_overcount;
if(_lw_revents_overcount >= 4 && _lw_revents_overcount >= (_lw_revents_count >> 2)) {
new_revents = malloc(sizeof(struct epoll_event) * (_lw_revents_size << 1));
if(new_revents) {
free(_lw_revents);
_lw_revents = new_revents;
_lw_revents_size <<= 1;
}
}
/* free any invalid struct lwevent objects */ /* free any invalid struct lwevent objects */
for(ev = _lwevent_invalid_head; ev; ev = _lwevent_invalid_head) { for(ev = _lwevent_invalid_head; ev; ev = _lwevent_invalid_head) {
_lwevent_invalid_head = _lwevent_invalid_head->invalid_next; _lwevent_invalid_head = _lwevent_invalid_head->invalid_next;
free(ev); free(ev);
} }
/* TODO: possibly resize returned events array */
return ret; return ret;
} }