X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/96ffb2fe97c3e77879e7a4f6f7457397a18bf233..bb0fbc840d80d30a513cc85cf40848f1b006275c:/src/ae.c?ds=sidebyside diff --git a/src/ae.c b/src/ae.c index c7918ee1..4099b125 100644 --- a/src/ae.c +++ b/src/ae.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "ae.h" #include "zmalloc.h" @@ -52,30 +53,40 @@ #endif #endif -aeEventLoop *aeCreateEventLoop(void) { +aeEventLoop *aeCreateEventLoop(int setsize) { aeEventLoop *eventLoop; int i; - eventLoop = zmalloc(sizeof(*eventLoop)); - if (!eventLoop) return NULL; + if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err; + eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize); + eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize); + if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err; + eventLoop->setsize = setsize; eventLoop->timeEventHead = NULL; eventLoop->timeEventNextId = 0; eventLoop->stop = 0; eventLoop->maxfd = -1; eventLoop->beforesleep = NULL; - if (aeApiCreate(eventLoop) == -1) { - zfree(eventLoop); - return NULL; - } + if (aeApiCreate(eventLoop) == -1) goto err; /* Events with mask == AE_NONE are not set. So let's initialize the * vector with it. */ - for (i = 0; i < AE_SETSIZE; i++) + for (i = 0; i < setsize; i++) eventLoop->events[i].mask = AE_NONE; return eventLoop; + +err: + if (eventLoop) { + zfree(eventLoop->events); + zfree(eventLoop->fired); + zfree(eventLoop); + } + return NULL; } void aeDeleteEventLoop(aeEventLoop *eventLoop) { aeApiFree(eventLoop); + zfree(eventLoop->events); + zfree(eventLoop->fired); zfree(eventLoop); } @@ -86,7 +97,7 @@ void aeStop(aeEventLoop *eventLoop) { int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask, aeFileProc *proc, void *clientData) { - if (fd >= AE_SETSIZE) return AE_ERR; + if (fd >= eventLoop->setsize) return AE_ERR; aeFileEvent *fe = &eventLoop->events[fd]; if (aeApiAddEvent(eventLoop, fd, mask) == -1) @@ -102,7 +113,7 @@ int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask, void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask) { - if (fd >= AE_SETSIZE) return; + if (fd >= eventLoop->setsize) return; aeFileEvent *fe = &eventLoop->events[fd]; if (fe->mask == AE_NONE) return; @@ -118,6 +129,13 @@ void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask) aeApiDelEvent(eventLoop, fd, mask); } +int aeGetFileEvents(aeEventLoop *eventLoop, int fd) { + if (fd >= eventLoop->setsize) return 0; + aeFileEvent *fe = &eventLoop->events[fd]; + + return fe->mask; +} + static void aeGetTime(long *seconds, long *milliseconds) { struct timeval tv;