Rotate quicklist by moving the tail element to the head. */
| 1305 | |
| 1306 | /* Rotate quicklist by moving the tail element to the head. */ |
| 1307 | void quicklistRotate(quicklist *quicklist) { |
| 1308 | if (quicklist->count <= 1) |
| 1309 | return; |
| 1310 | |
| 1311 | /* First, get the tail entry */ |
| 1312 | unsigned char *p = ziplistIndex(quicklist->tail->zl, -1); |
| 1313 | unsigned char *value, *tmp; |
| 1314 | long long longval; |
| 1315 | unsigned int sz; |
| 1316 | char longstr[32] = {0}; |
| 1317 | ziplistGet(p, &tmp, &sz, &longval); |
| 1318 | |
| 1319 | /* If value found is NULL, then ziplistGet populated longval instead */ |
| 1320 | if (!tmp) { |
| 1321 | /* Write the longval as a string so we can re-add it */ |
| 1322 | sz = ll2string(longstr, sizeof(longstr), longval); |
| 1323 | value = (unsigned char *)longstr; |
| 1324 | } else if (quicklist->len == 1) { |
| 1325 | /* Copy buffer since there could be a memory overlap when move |
| 1326 | * entity from tail to head in the same ziplist. */ |
| 1327 | value = zmalloc(sz); |
| 1328 | memcpy(value, tmp, sz); |
| 1329 | } else { |
| 1330 | value = tmp; |
| 1331 | } |
| 1332 | |
| 1333 | /* Add tail entry to head (must happen before tail is deleted). */ |
| 1334 | quicklistPushHead(quicklist, value, sz); |
| 1335 | |
| 1336 | /* If quicklist has only one node, the head ziplist is also the |
| 1337 | * tail ziplist and PushHead() could have reallocated our single ziplist, |
| 1338 | * which would make our pre-existing 'p' unusable. */ |
| 1339 | if (quicklist->len == 1) { |
| 1340 | p = ziplistIndex(quicklist->tail->zl, -1); |
| 1341 | } |
| 1342 | |
| 1343 | /* Remove tail entry. */ |
| 1344 | quicklistDelIndex(quicklist, quicklist->tail, &p); |
| 1345 | if (value != (unsigned char*)longstr && value != tmp) |
| 1346 | zfree(value); |
| 1347 | } |
| 1348 | |
| 1349 | /* pop from quicklist and return result in 'data' ptr. Value of 'data' |
| 1350 | * is the return value of 'saver' function pointer if the data is NOT a number. |
no test coverage detected