X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/f14479c796fbf00c41ceefdcb4bec04df46f53ce..6e6bbac7a5ebc24ff7bf5913b4b8b49433b8e303:/src/ae.c diff --git a/src/ae.c b/src/ae.c index 05802890..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; @@ -119,7 +130,7 @@ void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask) } int aeGetFileEvents(aeEventLoop *eventLoop, int fd) { - if (fd >= AE_SETSIZE) return 0; + if (fd >= eventLoop->setsize) return 0; aeFileEvent *fe = &eventLoop->events[fd]; return fe->mask;