@@ -307,25 +307,22 @@ unsigned long hashTypeLength(const robj *o) {
307
307
return length ;
308
308
}
309
309
310
- hashTypeIterator * hashTypeInitIterator (robj * subject ) {
311
- hashTypeIterator * hi = zmalloc (sizeof (hashTypeIterator ));
310
+ void hashTypeInitIterator (robj * subject , hashTypeIterator * hi ) {
312
311
hi -> subject = subject ;
313
312
hi -> encoding = subject -> encoding ;
314
313
315
314
if (hi -> encoding == OBJ_ENCODING_LISTPACK ) {
316
315
hi -> fptr = NULL ;
317
316
hi -> vptr = NULL ;
318
317
} else if (hi -> encoding == OBJ_ENCODING_HT ) {
319
- hi -> di = dictGetIterator ( subject -> ptr );
318
+ dictInitIterator ( & hi -> di , subject -> ptr );
320
319
} else {
321
320
serverPanic ("Unknown hash encoding" );
322
321
}
323
- return hi ;
324
322
}
325
323
326
- void hashTypeReleaseIterator (hashTypeIterator * hi ) {
327
- if (hi -> encoding == OBJ_ENCODING_HT ) dictReleaseIterator (hi -> di );
328
- zfree (hi );
324
+ void hashTypeResetIterator (hashTypeIterator * hi ) {
325
+ if (hi -> encoding == OBJ_ENCODING_HT ) dictResetIterator (& hi -> di );
329
326
}
330
327
331
328
/* Move to the next entry in the hash. Return C_OK when the next entry
@@ -358,7 +355,7 @@ int hashTypeNext(hashTypeIterator *hi) {
358
355
hi -> fptr = fptr ;
359
356
hi -> vptr = vptr ;
360
357
} else if (hi -> encoding == OBJ_ENCODING_HT ) {
361
- if ((hi -> de = dictNext (hi -> di )) == NULL ) return C_ERR ;
358
+ if ((hi -> de = dictNext (& hi -> di )) == NULL ) return C_ERR ;
362
359
} else {
363
360
serverPanic ("Unknown hash encoding" );
364
361
}
@@ -448,31 +445,31 @@ void hashTypeConvertListpack(robj *o, int enc) {
448
445
/* Nothing to do... */
449
446
450
447
} else if (enc == OBJ_ENCODING_HT ) {
451
- hashTypeIterator * hi ;
448
+ hashTypeIterator hi ;
452
449
dict * dict ;
453
450
int ret ;
454
451
455
- hi = hashTypeInitIterator (o );
452
+ hashTypeInitIterator (o , & hi );
456
453
dict = dictCreate (& hashDictType );
457
454
458
455
/* Presize the dict to avoid rehashing */
459
456
dictExpand (dict , hashTypeLength (o ));
460
457
461
- while (hashTypeNext (hi ) != C_ERR ) {
458
+ while (hashTypeNext (& hi ) != C_ERR ) {
462
459
sds key , value ;
463
460
464
- key = hashTypeCurrentObjectNewSds (hi , OBJ_HASH_KEY );
465
- value = hashTypeCurrentObjectNewSds (hi , OBJ_HASH_VALUE );
461
+ key = hashTypeCurrentObjectNewSds (& hi , OBJ_HASH_KEY );
462
+ value = hashTypeCurrentObjectNewSds (& hi , OBJ_HASH_VALUE );
466
463
ret = dictAdd (dict , key , value );
467
464
if (ret != DICT_OK ) {
468
465
sdsfree (key );
469
- sdsfree (value ); /* Needed for gcc ASAN */
470
- hashTypeReleaseIterator ( hi ); /* Needed for gcc ASAN */
466
+ sdsfree (value ); /* Needed for gcc ASAN */
467
+ hashTypeResetIterator ( & hi ); /* Needed for gcc ASAN */
471
468
serverLogHexDump (LL_WARNING , "listpack with dup elements dump" , o -> ptr , lpBytes (o -> ptr ));
472
469
serverPanic ("Listpack corruption detected" );
473
470
}
474
471
}
475
- hashTypeReleaseIterator ( hi );
472
+ hashTypeResetIterator ( & hi );
476
473
zfree (o -> ptr );
477
474
o -> encoding = OBJ_ENCODING_HT ;
478
475
o -> ptr = dict ;
@@ -498,7 +495,7 @@ void hashTypeConvert(robj *o, int enc) {
498
495
* The resulting object always has refcount set to 1 */
499
496
robj * hashTypeDup (robj * o ) {
500
497
robj * hobj ;
501
- hashTypeIterator * hi ;
498
+ hashTypeIterator hi ;
502
499
503
500
serverAssert (o -> type == OBJ_HASH );
504
501
@@ -513,20 +510,20 @@ robj *hashTypeDup(robj *o) {
513
510
dict * d = dictCreate (& hashDictType );
514
511
dictExpand (d , dictSize ((const dict * )o -> ptr ));
515
512
516
- hi = hashTypeInitIterator (o );
517
- while (hashTypeNext (hi ) != C_ERR ) {
513
+ hashTypeInitIterator (o , & hi );
514
+ while (hashTypeNext (& hi ) != C_ERR ) {
518
515
sds field , value ;
519
516
sds newfield , newvalue ;
520
517
/* Extract a field-value pair from an original hash object.*/
521
- field = hashTypeCurrentFromHashTable (hi , OBJ_HASH_KEY );
522
- value = hashTypeCurrentFromHashTable (hi , OBJ_HASH_VALUE );
518
+ field = hashTypeCurrentFromHashTable (& hi , OBJ_HASH_KEY );
519
+ value = hashTypeCurrentFromHashTable (& hi , OBJ_HASH_VALUE );
523
520
newfield = sdsdup (field );
524
521
newvalue = sdsdup (value );
525
522
526
523
/* Add a field-value pair to a new hash object. */
527
524
dictAdd (d , newfield , newvalue );
528
525
}
529
- hashTypeReleaseIterator ( hi );
526
+ hashTypeResetIterator ( & hi );
530
527
531
528
hobj = createObject (OBJ_HASH , d );
532
529
hobj -> encoding = OBJ_ENCODING_HT ;
@@ -812,7 +809,7 @@ static void addHashIteratorCursorToReply(client *c, hashTypeIterator *hi, int wh
812
809
813
810
void genericHgetallCommand (client * c , int flags ) {
814
811
robj * o ;
815
- hashTypeIterator * hi ;
812
+ hashTypeIterator hi ;
816
813
int length , count = 0 ;
817
814
818
815
robj * emptyResp = (flags & OBJ_HASH_KEY && flags & OBJ_HASH_VALUE ) ? shared .emptymap [c -> resp ] : shared .emptyarray ;
@@ -827,19 +824,19 @@ void genericHgetallCommand(client *c, int flags) {
827
824
addReplyArrayLen (c , length );
828
825
}
829
826
830
- hi = hashTypeInitIterator (o );
831
- while (hashTypeNext (hi ) != C_ERR ) {
827
+ hashTypeInitIterator (o , & hi );
828
+ while (hashTypeNext (& hi ) != C_ERR ) {
832
829
if (flags & OBJ_HASH_KEY ) {
833
- addHashIteratorCursorToReply (c , hi , OBJ_HASH_KEY );
830
+ addHashIteratorCursorToReply (c , & hi , OBJ_HASH_KEY );
834
831
count ++ ;
835
832
}
836
833
if (flags & OBJ_HASH_VALUE ) {
837
- addHashIteratorCursorToReply (c , hi , OBJ_HASH_VALUE );
834
+ addHashIteratorCursorToReply (c , & hi , OBJ_HASH_VALUE );
838
835
count ++ ;
839
836
}
840
837
}
841
838
842
- hashTypeReleaseIterator ( hi );
839
+ hashTypeResetIterator ( & hi );
843
840
844
841
/* Make sure we returned the right number of elements. */
845
842
if (flags & OBJ_HASH_KEY && flags & OBJ_HASH_VALUE ) count /= 2 ;
@@ -973,13 +970,14 @@ void hrandfieldWithCountCommand(client *c, long l, int withvalues) {
973
970
* The number of requested elements is greater than the number of
974
971
* elements inside the hash: simply return the whole hash. */
975
972
if (count >= size ) {
976
- hashTypeIterator * hi = hashTypeInitIterator (hash );
977
- while (hashTypeNext (hi ) != C_ERR ) {
973
+ hashTypeIterator hi ;
974
+ hashTypeInitIterator (hash , & hi );
975
+ while (hashTypeNext (& hi ) != C_ERR ) {
978
976
if (withvalues && c -> resp > 2 ) addReplyArrayLen (c , 2 );
979
- addHashIteratorCursorToReply (c , hi , OBJ_HASH_KEY );
980
- if (withvalues ) addHashIteratorCursorToReply (c , hi , OBJ_HASH_VALUE );
977
+ addHashIteratorCursorToReply (c , & hi , OBJ_HASH_KEY );
978
+ if (withvalues ) addHashIteratorCursorToReply (c , & hi , OBJ_HASH_VALUE );
981
979
}
982
- hashTypeReleaseIterator ( hi );
980
+ hashTypeResetIterator ( & hi );
983
981
return ;
984
982
}
985
983
@@ -1015,21 +1013,22 @@ void hrandfieldWithCountCommand(client *c, long l, int withvalues) {
1015
1013
/* Hashtable encoding (generic implementation) */
1016
1014
dict * d = dictCreate (& sdsReplyDictType );
1017
1015
dictExpand (d , size );
1018
- hashTypeIterator * hi = hashTypeInitIterator (hash );
1016
+ hashTypeIterator hi ;
1017
+ hashTypeInitIterator (hash , & hi );
1019
1018
1020
1019
/* Add all the elements into the temporary dictionary. */
1021
- while ((hashTypeNext (hi )) != C_ERR ) {
1020
+ while ((hashTypeNext (& hi )) != C_ERR ) {
1022
1021
int ret = DICT_ERR ;
1023
1022
sds key , value = NULL ;
1024
1023
1025
- key = hashTypeCurrentObjectNewSds (hi , OBJ_HASH_KEY );
1026
- if (withvalues ) value = hashTypeCurrentObjectNewSds (hi , OBJ_HASH_VALUE );
1024
+ key = hashTypeCurrentObjectNewSds (& hi , OBJ_HASH_KEY );
1025
+ if (withvalues ) value = hashTypeCurrentObjectNewSds (& hi , OBJ_HASH_VALUE );
1027
1026
ret = dictAdd (d , key , value );
1028
1027
1029
1028
serverAssert (ret == DICT_OK );
1030
1029
}
1031
1030
serverAssert (dictSize (d ) == size );
1032
- hashTypeReleaseIterator ( hi );
1031
+ hashTypeResetIterator ( & hi );
1033
1032
1034
1033
/* Remove random elements to reach the right count. */
1035
1034
while (size > count ) {
0 commit comments