Duplicate the quicklist. * On success a copy of the original quicklist is returned. * * The original quicklist both on success or error is never modified. * * Returns newly allocated quicklist. */
| 1208 | * |
| 1209 | * Returns newly allocated quicklist. */ |
| 1210 | quicklist *quicklistDup(quicklist *orig) { |
| 1211 | quicklist *copy; |
| 1212 | |
| 1213 | copy = quicklistNew(orig->fill, orig->compress); |
| 1214 | |
| 1215 | for (quicklistNode *current = orig->head; current; |
| 1216 | current = current->next) { |
| 1217 | quicklistNode *node = quicklistCreateNode(); |
| 1218 | |
| 1219 | if (current->encoding == QUICKLIST_NODE_ENCODING_LZF) { |
| 1220 | quicklistLZF *lzf = (quicklistLZF *)current->zl; |
| 1221 | size_t lzf_sz = sizeof(*lzf) + lzf->sz; |
| 1222 | node->zl = zmalloc(lzf_sz, MALLOC_SHARED); |
| 1223 | memcpy(node->zl, current->zl, lzf_sz); |
| 1224 | } else if (current->encoding == QUICKLIST_NODE_ENCODING_RAW) { |
| 1225 | node->zl = zmalloc(current->sz, MALLOC_SHARED); |
| 1226 | memcpy(node->zl, current->zl, current->sz); |
| 1227 | } |
| 1228 | |
| 1229 | node->count = current->count; |
| 1230 | copy->count += node->count; |
| 1231 | node->sz = current->sz; |
| 1232 | node->encoding = current->encoding; |
| 1233 | |
| 1234 | _quicklistInsertNodeAfter(copy, copy->tail, node); |
| 1235 | } |
| 1236 | |
| 1237 | /* copy->count must equal orig->count here */ |
| 1238 | return copy; |
| 1239 | } |
| 1240 | |
| 1241 | /* Populate 'entry' with the element at the specified zero-based index |
| 1242 | * where 0 is the head, 1 is the element next to head |
no test coverage detected