diff options
author | Pieter Noordhuis <pcnoordhuis@gmail.com> | 2010-11-01 10:10:03 +0100 |
---|---|---|
committer | Pieter Noordhuis <pcnoordhuis@gmail.com> | 2010-11-01 10:10:03 +0100 |
commit | a66ec18e80bdd400ce331efb9ae537a2d9189614 (patch) | |
tree | e6f243df6c0aaa479459fb297ee762753d083f54 | |
parent | e25db30f38c3721c7e17c3a84078a4adde059c53 (diff) |
Make push/shift functions for callbacks responsible for malloc/free
-rw-r--r-- | async.c | 51 |
1 files changed, 32 insertions, 19 deletions
@@ -69,22 +69,38 @@ int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallba } /* Helper functions to push/shift callbacks */ -static void __redisPushCallback(redisCallbackList *list, redisCallback *cb) { +static int __redisPushCallback(redisCallbackList *list, redisCallback *source) { + redisCallback *cb; + + /* Copy callback from stack to heap */ + cb = calloc(1,sizeof(*cb)); + if (!cb) redisOOM(); + if (source != NULL) + memcpy(cb,source,sizeof(*cb)); + + /* Store callback in list */ if (list->head == NULL) list->head = cb; if (list->tail != NULL) list->tail->next = cb; list->tail = cb; + return REDIS_OK; } -static redisCallback *__redisShiftCallback(redisCallbackList *list) { +static int __redisShiftCallback(redisCallbackList *list, redisCallback *target) { redisCallback *cb = list->head; if (cb != NULL) { list->head = cb->next; if (cb == list->tail) list->tail = NULL; + + /* Copy callback from heap to stack */ + if (target != NULL) + memcpy(target,cb,sizeof(*cb)); + free(cb); + return REDIS_OK; } - return cb; + return REDIS_ERR; } /* Tries to do a clean disconnect from Redis, meaning it stops new commands @@ -102,7 +118,7 @@ void redisAsyncDisconnect(redisAsyncContext *ac) { /* Helper function to make the disconnect happen and clean up. */ static void __redisAsyncDisconnect(redisAsyncContext *ac) { redisContext *c = &(ac->c); - redisCallback *cb; + redisCallback cb; int status; /* Make sure error is accessible if there is any */ @@ -112,15 +128,15 @@ static void __redisAsyncDisconnect(redisAsyncContext *ac) { if (status == REDIS_OK) { /* When the connection is cleanly disconnected, there should not * be pending callbacks. */ - assert((cb = __redisShiftCallback(&ac->replies)) == NULL); + assert(__redisShiftCallback(&ac->replies,NULL) == REDIS_ERR); } else { /* Callbacks should not be able to issue new commands. */ c->flags |= REDIS_DISCONNECTING; /* Execute pending callbacks with NULL reply. */ - while ((cb = __redisShiftCallback(&ac->replies)) != NULL) { - if (cb->fn != NULL) - cb->fn(ac,NULL,cb->privdata); + while (__redisShiftCallback(&ac->replies,&cb) == REDIS_OK) { + if (cb.fn != NULL) + cb.fn(ac,NULL,cb.privdata); } } @@ -136,7 +152,7 @@ static void __redisAsyncDisconnect(redisAsyncContext *ac) { void redisProcessCallbacks(redisAsyncContext *ac) { redisContext *c = &(ac->c); - redisCallback *cb; + redisCallback cb; void *reply = NULL; int status; @@ -155,10 +171,9 @@ void redisProcessCallbacks(redisAsyncContext *ac) { } /* Shift callback and execute it */ - cb = __redisShiftCallback(&ac->replies); - assert(cb != NULL); - if (cb->fn != NULL) { - cb->fn(ac,reply,cb->privdata); + assert(__redisShiftCallback(&ac->replies,&cb) == REDIS_OK); + if (cb.fn != NULL) { + cb.fn(ac,reply,cb.privdata); } else { c->fn->freeObject(reply); } @@ -210,18 +225,16 @@ void redisAsyncHandleWrite(redisAsyncContext *ac) { */ static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, char *cmd, size_t len) { redisContext *c = &(ac->c); - redisCallback *cb; + redisCallback cb; /* Don't accept new commands when the connection is lazily closed. */ if (c->flags & REDIS_DISCONNECTING) return REDIS_ERR; c->obuf = sdscatlen(c->obuf,cmd,len); /* Store callback */ - cb = calloc(1,sizeof(redisCallback)); - if (!cb) redisOOM(); - cb->fn = fn; - cb->privdata = privdata; - __redisPushCallback(&(ac->replies),cb); + cb.fn = fn; + cb.privdata = privdata; + __redisPushCallback(&ac->replies,&cb); /* Always schedule a write when the write buffer is non-empty */ if (ac->evAddWrite) ac->evAddWrite(ac->data); |