Skip to content

Commit

Permalink
Stack allocate dict iterators
Browse files Browse the repository at this point in the history
Replacing the get & release functions with an initiation
function. Simplifies the code and will make sure we
run subscription callbacks in OOM scenarios.
  • Loading branch information
bjosv committed Jan 25, 2021
1 parent 297ecbe commit 920128a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 25 deletions.
20 changes: 7 additions & 13 deletions async.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ static void __redisRunPushCallback(redisAsyncContext *ac, redisReply *reply) {
static void __redisAsyncFree(redisAsyncContext *ac) {
redisContext *c = &(ac->c);
redisCallback cb;
dictIterator *it;
dictIterator it;
dictEntry *de;

/* Execute pending callbacks with NULL reply. */
Expand All @@ -319,23 +319,17 @@ static void __redisAsyncFree(redisAsyncContext *ac) {

/* Run subscription callbacks with NULL reply */
if (ac->sub.channels) {
it = dictGetIterator(ac->sub.channels);
if (it != NULL) {
while ((de = dictNext(it)) != NULL)
__redisRunCallback(ac,dictGetEntryVal(de),NULL);
dictReleaseIterator(it);
}
dictInitIterator(&it,ac->sub.channels);
while ((de = dictNext(&it)) != NULL)
__redisRunCallback(ac,dictGetEntryVal(de),NULL);

dictRelease(ac->sub.channels);
}

if (ac->sub.patterns) {
it = dictGetIterator(ac->sub.patterns);
if (it != NULL) {
while ((de = dictNext(it)) != NULL)
__redisRunCallback(ac,dictGetEntryVal(de),NULL);
dictReleaseIterator(it);
}
dictInitIterator(&it,ac->sub.patterns);
while ((de = dictNext(&it)) != NULL)
__redisRunCallback(ac,dictGetEntryVal(de),NULL);

dictRelease(ac->sub.patterns);
}
Expand Down
11 changes: 1 addition & 10 deletions dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,11 @@ static dictEntry *dictFind(dict *ht, const void *key) {
return NULL;
}

static dictIterator *dictGetIterator(dict *ht) {
dictIterator *iter = hi_malloc(sizeof(*iter));
if (iter == NULL)
return NULL;

static void dictInitIterator(dictIterator *iter, dict *ht) {
iter->ht = ht;
iter->index = -1;
iter->entry = NULL;
iter->nextEntry = NULL;
return iter;
}

static dictEntry *dictNext(dictIterator *iter) {
Expand All @@ -299,10 +294,6 @@ static dictEntry *dictNext(dictIterator *iter) {
return NULL;
}

static void dictReleaseIterator(dictIterator *iter) {
hi_free(iter);
}

/* ------------------------- private functions ------------------------------ */

/* Expand the hash table if needed */
Expand Down
3 changes: 1 addition & 2 deletions dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ static int dictReplace(dict *ht, void *key, void *val);
static int dictDelete(dict *ht, const void *key);
static void dictRelease(dict *ht);
static dictEntry * dictFind(dict *ht, const void *key);
static dictIterator *dictGetIterator(dict *ht);
static void dictInitIterator(dictIterator *iter, dict *ht);
static dictEntry *dictNext(dictIterator *iter);
static void dictReleaseIterator(dictIterator *iter);

#endif /* __DICT_H */

0 comments on commit 920128a

Please sign in to comment.