]>
git.saurik.com Git - redis.git/blob - src/ziplist.c
1 /* The ziplist is a specially encoded dually linked list that is designed
2 * to be very memory efficient. It stores both strings and integer values,
3 * where integers are encoded as actual integers instead of a series of
4 * characters. It allows push and pop operations on either side of the list
5 * in O(1) time. However, because every operation requires a reallocation of
6 * the memory used by the ziplist, the actual complexity is related to the
7 * amount of memory used by the ziplist.
9 * ----------------------------------------------------------------------------
11 * ZIPLIST OVERALL LAYOUT:
12 * The general layout of the ziplist is as follows:
13 * <zlbytes><zltail><zllen><entry><entry><zlend>
15 * <zlbytes> is an unsigned integer to hold the number of bytes that the
16 * ziplist occupies. This value needs to be stored to be able to resize the
17 * entire structure without the need to traverse it first.
19 * <zltail> is the offset to the last entry in the list. This allows a pop
20 * operation on the far side of the list without the need for full traversal.
22 * <zllen> is the number of entries.When this value is larger than 2**16-2,
23 * we need to traverse the entire list to know how many items it holds.
25 * <zlend> is a single byte special value, equal to 255, which indicates the
29 * Every entry in the ziplist is prefixed by a header that contains two pieces
30 * of information. First, the length of the previous entry is stored to be
31 * able to traverse the list from back to front. Second, the encoding with an
32 * optional string length of the entry itself is stored.
34 * The length of the previous entry is encoded in the following way:
35 * If this length is smaller than 254 bytes, it will only consume a single
36 * byte that takes the length as value. When the length is greater than or
37 * equal to 254, it will consume 5 bytes. The first byte is set to 254 to
38 * indicate a larger value is following. The remaining 4 bytes take the
39 * length of the previous entry as value.
41 * The other header field of the entry itself depends on the contents of the
42 * entry. When the entry is a string, the first 2 bits of this header will hold
43 * the type of encoding used to store the length of the string, followed by the
44 * actual length of the string. When the entry is an integer the first 2 bits
45 * are both set to 1. The following 2 bits are used to specify what kind of
46 * integer will be stored after this header. An overview of the different
47 * types and encodings is as follows:
50 * String value with length less than or equal to 63 bytes (6 bits).
51 * |01pppppp|qqqqqqqq| - 2 bytes
52 * String value with length less than or equal to 16383 bytes (14 bits).
53 * |10______|qqqqqqqq|rrrrrrrr|ssssssss|tttttttt| - 5 bytes
54 * String value with length greater than or equal to 16384 bytes.
56 * Integer encoded as int16_t (2 bytes).
58 * Integer encoded as int32_t (4 bytes).
60 * Integer encoded as int64_t (8 bytes).
72 #include "endianconv.h"
75 #define ZIP_BIGLEN 254
77 /* Different encoding/length possibilities */
78 #define ZIP_STR_MASK (0xc0)
79 #define ZIP_INT_MASK (0x30)
80 #define ZIP_STR_06B (0 << 6)
81 #define ZIP_STR_14B (1 << 6)
82 #define ZIP_STR_32B (2 << 6)
83 #define ZIP_INT_16B (0xc0 | 0<<4)
84 #define ZIP_INT_32B (0xc0 | 1<<4)
85 #define ZIP_INT_64B (0xc0 | 2<<4)
87 /* Macro to determine type */
88 #define ZIP_IS_STR(enc) (((enc) & ZIP_STR_MASK) < ZIP_STR_MASK)
91 #define ZIPLIST_BYTES(zl) (*((uint32_t*)(zl)))
92 #define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t))))
93 #define ZIPLIST_LENGTH(zl) (*((uint16_t*)((zl)+sizeof(uint32_t)*2)))
94 #define ZIPLIST_HEADER_SIZE (sizeof(uint32_t)*2+sizeof(uint16_t))
95 #define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE)
96 #define ZIPLIST_ENTRY_TAIL(zl) ((zl)+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)))
97 #define ZIPLIST_ENTRY_END(zl) ((zl)+intrev32ifbe(ZIPLIST_BYTES(zl))-1)
99 /* We know a positive increment can only be 1 because entries can only be
100 * pushed one at a time. */
101 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
102 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) \
103 ZIPLIST_LENGTH(zl) = intrev16ifbe(intrev16ifbe(ZIPLIST_LENGTH(zl))+incr); \
106 typedef struct zlentry
{
107 unsigned int prevrawlensize
, prevrawlen
;
108 unsigned int lensize
, len
;
109 unsigned int headersize
;
110 unsigned char encoding
;
114 #define ZIP_ENTRY_ENCODING(ptr, encoding) do { \
115 (encoding) = (ptr[0]) & (ZIP_STR_MASK | ZIP_INT_MASK); \
116 if (((encoding) & ZIP_STR_MASK) < ZIP_STR_MASK) { \
117 /* String encoding: 2 MSBs */ \
118 (encoding) &= ZIP_STR_MASK; \
122 /* Return bytes needed to store integer encoded by 'encoding' */
123 static unsigned int zipIntSize(unsigned char encoding
) {
125 case ZIP_INT_16B
: return sizeof(int16_t);
126 case ZIP_INT_32B
: return sizeof(int32_t);
127 case ZIP_INT_64B
: return sizeof(int64_t);
133 /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
134 * the amount of bytes required to encode such a length. */
135 static unsigned int zipEncodeLength(unsigned char *p
, unsigned char encoding
, unsigned int rawlen
) {
136 unsigned char len
= 1, buf
[5];
138 if (ZIP_IS_STR(encoding
)) {
139 /* Although encoding is given it may not be set for strings,
140 * so we determine it here using the raw length. */
141 if (rawlen
<= 0x3f) {
143 buf
[0] = ZIP_STR_06B
| rawlen
;
144 } else if (rawlen
<= 0x3fff) {
147 buf
[0] = ZIP_STR_14B
| ((rawlen
>> 8) & 0x3f);
148 buf
[1] = rawlen
& 0xff;
152 buf
[0] = ZIP_STR_32B
;
153 buf
[1] = (rawlen
>> 24) & 0xff;
154 buf
[2] = (rawlen
>> 16) & 0xff;
155 buf
[3] = (rawlen
>> 8) & 0xff;
156 buf
[4] = rawlen
& 0xff;
159 /* Implies integer encoding, so length is always 1. */
164 /* Store this length at p */
169 /* Decode the length encoded in 'ptr'. The 'encoding' variable will hold the
170 * entries encoding, the 'lensize' variable will hold the number of bytes
171 * required to encode the entries length, and the 'len' variable will hold the
173 #define ZIP_DECODE_LENGTH(ptr, encoding, lensize, len) do { \
174 ZIP_ENTRY_ENCODING((ptr), (encoding)); \
175 if ((encoding) < ZIP_STR_MASK) { \
176 if ((encoding) == ZIP_STR_06B) { \
178 (len) = (ptr)[0] & 0x3f; \
179 } else if ((encoding) == ZIP_STR_14B) { \
181 (len) = (((ptr)[0] & 0x3f) << 8) | (ptr)[1]; \
182 } else if (encoding == ZIP_STR_32B) { \
184 (len) = ((ptr)[1] << 24) | \
193 (len) = zipIntSize(encoding); \
197 /* Encode the length of the previous entry and write it to "p". Return the
198 * number of bytes needed to encode this length if "p" is NULL. */
199 static unsigned int zipPrevEncodeLength(unsigned char *p
, unsigned int len
) {
201 return (len
< ZIP_BIGLEN
) ? 1 : sizeof(len
)+1;
203 if (len
< ZIP_BIGLEN
) {
208 memcpy(p
+1,&len
,sizeof(len
));
210 return 1+sizeof(len
);
215 /* Encode the length of the previous entry and write it to "p". This only
216 * uses the larger encoding (required in __ziplistCascadeUpdate). */
217 static void zipPrevEncodeLengthForceLarge(unsigned char *p
, unsigned int len
) {
218 if (p
== NULL
) return;
220 memcpy(p
+1,&len
,sizeof(len
));
224 /* Decode the number of bytes required to store the length of the previous
225 * element, from the perspective of the entry pointed to by 'ptr'. */
226 #define ZIP_DECODE_PREVLENSIZE(ptr, prevlensize) do { \
227 if ((ptr)[0] < ZIP_BIGLEN) { \
234 /* Decode the length of the previous element, from the perspective of the entry
235 * pointed to by 'ptr'. */
236 #define ZIP_DECODE_PREVLEN(ptr, prevlensize, prevlen) do { \
237 ZIP_DECODE_PREVLENSIZE(ptr, prevlensize); \
238 if ((prevlensize) == 1) { \
239 (prevlen) = (ptr)[0]; \
240 } else if ((prevlensize) == 5) { \
241 assert(sizeof((prevlensize)) == 4); \
242 memcpy(&(prevlen), ((char*)(ptr)) + 1, 4); \
243 memrev32ifbe(&len); \
247 /* Return the difference in number of bytes needed to store the length of the
248 * previous element 'len', in the entry pointed to by 'p'. */
249 static int zipPrevLenByteDiff(unsigned char *p
, unsigned int len
) {
250 unsigned int prevlensize
;
251 ZIP_DECODE_PREVLENSIZE(p
, prevlensize
);
252 return zipPrevEncodeLength(NULL
, len
) - prevlensize
;
255 /* Return the total number of bytes used by the entry pointed to by 'p'. */
256 static unsigned int zipRawEntryLength(unsigned char *p
) {
257 unsigned int prevlensize
, encoding
, lensize
, len
;
258 ZIP_DECODE_PREVLENSIZE(p
, prevlensize
);
259 ZIP_DECODE_LENGTH(p
+ prevlensize
, encoding
, lensize
, len
);
260 return prevlensize
+ lensize
+ len
;
263 /* Check if string pointed to by 'entry' can be encoded as an integer.
264 * Stores the integer value in 'v' and its encoding in 'encoding'. */
265 static int zipTryEncoding(unsigned char *entry
, unsigned int entrylen
, long long *v
, unsigned char *encoding
) {
268 if (entrylen
>= 32 || entrylen
== 0) return 0;
269 if (string2ll((char*)entry
,entrylen
,&value
)) {
270 /* Great, the string can be encoded. Check what's the smallest
271 * of our encoding types that can hold this value. */
272 if (value
>= INT16_MIN
&& value
<= INT16_MAX
) {
273 *encoding
= ZIP_INT_16B
;
274 } else if (value
>= INT32_MIN
&& value
<= INT32_MAX
) {
275 *encoding
= ZIP_INT_32B
;
277 *encoding
= ZIP_INT_64B
;
285 /* Store integer 'value' at 'p', encoded as 'encoding' */
286 static void zipSaveInteger(unsigned char *p
, int64_t value
, unsigned char encoding
) {
290 if (encoding
== ZIP_INT_16B
) {
292 memcpy(p
,&i16
,sizeof(i16
));
294 } else if (encoding
== ZIP_INT_32B
) {
296 memcpy(p
,&i32
,sizeof(i32
));
298 } else if (encoding
== ZIP_INT_64B
) {
300 memcpy(p
,&i64
,sizeof(i64
));
307 /* Read integer encoded as 'encoding' from 'p' */
308 static int64_t zipLoadInteger(unsigned char *p
, unsigned char encoding
) {
311 int64_t i64
, ret
= 0;
312 if (encoding
== ZIP_INT_16B
) {
313 memcpy(&i16
,p
,sizeof(i16
));
316 } else if (encoding
== ZIP_INT_32B
) {
317 memcpy(&i32
,p
,sizeof(i32
));
320 } else if (encoding
== ZIP_INT_64B
) {
321 memcpy(&i64
,p
,sizeof(i64
));
330 /* Return a struct with all information about an entry. */
331 static zlentry
zipEntry(unsigned char *p
) {
334 ZIP_DECODE_PREVLEN(p
, e
.prevrawlensize
, e
.prevrawlen
);
335 ZIP_DECODE_LENGTH(p
+ e
.prevrawlensize
, e
.encoding
, e
.lensize
, e
.len
);
336 e
.headersize
= e
.prevrawlensize
+ e
.lensize
;
341 /* Create a new empty ziplist. */
342 unsigned char *ziplistNew(void) {
343 unsigned int bytes
= ZIPLIST_HEADER_SIZE
+1;
344 unsigned char *zl
= zmalloc(bytes
);
345 ZIPLIST_BYTES(zl
) = intrev32ifbe(bytes
);
346 ZIPLIST_TAIL_OFFSET(zl
) = intrev32ifbe(ZIPLIST_HEADER_SIZE
);
347 ZIPLIST_LENGTH(zl
) = 0;
348 zl
[bytes
-1] = ZIP_END
;
352 /* Resize the ziplist. */
353 static unsigned char *ziplistResize(unsigned char *zl
, unsigned int len
) {
354 zl
= zrealloc(zl
,len
);
355 ZIPLIST_BYTES(zl
) = intrev32ifbe(len
);
360 /* When an entry is inserted, we need to set the prevlen field of the next
361 * entry to equal the length of the inserted entry. It can occur that this
362 * length cannot be encoded in 1 byte and the next entry needs to be grow
363 * a bit larger to hold the 5-byte encoded prevlen. This can be done for free,
364 * because this only happens when an entry is already being inserted (which
365 * causes a realloc and memmove). However, encoding the prevlen may require
366 * that this entry is grown as well. This effect may cascade throughout
367 * the ziplist when there are consecutive entries with a size close to
368 * ZIP_BIGLEN, so we need to check that the prevlen can be encoded in every
371 * Note that this effect can also happen in reverse, where the bytes required
372 * to encode the prevlen field can shrink. This effect is deliberately ignored,
373 * because it can cause a "flapping" effect where a chain prevlen fields is
374 * first grown and then shrunk again after consecutive inserts. Rather, the
375 * field is allowed to stay larger than necessary, because a large prevlen
376 * field implies the ziplist is holding large entries anyway.
378 * The pointer "p" points to the first entry that does NOT need to be
379 * updated, i.e. consecutive fields MAY need an update. */
380 static unsigned char *__ziplistCascadeUpdate(unsigned char *zl
, unsigned char *p
) {
381 size_t curlen
= intrev32ifbe(ZIPLIST_BYTES(zl
)), rawlen
, rawlensize
;
382 size_t offset
, noffset
, extra
;
386 while (p
[0] != ZIP_END
) {
388 rawlen
= cur
.headersize
+ cur
.len
;
389 rawlensize
= zipPrevEncodeLength(NULL
,rawlen
);
391 /* Abort if there is no next entry. */
392 if (p
[rawlen
] == ZIP_END
) break;
393 next
= zipEntry(p
+rawlen
);
395 /* Abort when "prevlen" has not changed. */
396 if (next
.prevrawlen
== rawlen
) break;
398 if (next
.prevrawlensize
< rawlensize
) {
399 /* The "prevlen" field of "next" needs more bytes to hold
400 * the raw length of "cur". */
402 extra
= rawlensize
-next
.prevrawlensize
;
403 zl
= ziplistResize(zl
,curlen
+extra
);
406 /* Current pointer and offset for next element. */
410 /* Update tail offset when next element is not the tail element. */
411 if ((zl
+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))) != np
) {
412 ZIPLIST_TAIL_OFFSET(zl
) =
413 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))+extra
);
416 /* Move the tail to the back. */
417 memmove(np
+rawlensize
,
418 np
+next
.prevrawlensize
,
419 curlen
-noffset
-next
.prevrawlensize
-1);
420 zipPrevEncodeLength(np
,rawlen
);
422 /* Advance the cursor */
426 if (next
.prevrawlensize
> rawlensize
) {
427 /* This would result in shrinking, which we want to avoid.
428 * So, set "rawlen" in the available bytes. */
429 zipPrevEncodeLengthForceLarge(p
+rawlen
,rawlen
);
431 zipPrevEncodeLength(p
+rawlen
,rawlen
);
434 /* Stop here, as the raw length of "next" has not changed. */
441 /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
442 static unsigned char *__ziplistDelete(unsigned char *zl
, unsigned char *p
, unsigned int num
) {
443 unsigned int i
, totlen
, deleted
= 0;
449 for (i
= 0; p
[0] != ZIP_END
&& i
< num
; i
++) {
450 p
+= zipRawEntryLength(p
);
456 if (p
[0] != ZIP_END
) {
457 /* Tricky: storing the prevlen in this entry might reduce or
458 * increase the number of bytes needed, compared to the current
459 * prevlen. Note that we can always store this length because
460 * it was previously stored by an entry that is being deleted. */
461 nextdiff
= zipPrevLenByteDiff(p
,first
.prevrawlen
);
462 zipPrevEncodeLength(p
-nextdiff
,first
.prevrawlen
);
464 /* Update offset for tail */
465 ZIPLIST_TAIL_OFFSET(zl
) =
466 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))-totlen
);
468 /* When the tail contains more than one entry, we need to take
469 * "nextdiff" in account as well. Otherwise, a change in the
470 * size of prevlen doesn't have an effect on the *tail* offset. */
472 if (p
[tail
.headersize
+tail
.len
] != ZIP_END
) {
473 ZIPLIST_TAIL_OFFSET(zl
) =
474 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))+nextdiff
);
477 /* Move tail to the front of the ziplist */
478 memmove(first
.p
,p
-nextdiff
,
479 intrev32ifbe(ZIPLIST_BYTES(zl
))-(p
-zl
)-1+nextdiff
);
481 /* The entire tail was deleted. No need to move memory. */
482 ZIPLIST_TAIL_OFFSET(zl
) =
483 intrev32ifbe((first
.p
-zl
)-first
.prevrawlen
);
486 /* Resize and update length */
488 zl
= ziplistResize(zl
, intrev32ifbe(ZIPLIST_BYTES(zl
))-totlen
+nextdiff
);
489 ZIPLIST_INCR_LENGTH(zl
,-deleted
);
492 /* When nextdiff != 0, the raw length of the next entry has changed, so
493 * we need to cascade the update throughout the ziplist */
495 zl
= __ziplistCascadeUpdate(zl
,p
);
500 /* Insert item at "p". */
501 static unsigned char *__ziplistInsert(unsigned char *zl
, unsigned char *p
, unsigned char *s
, unsigned int slen
) {
502 size_t curlen
= intrev32ifbe(ZIPLIST_BYTES(zl
)), reqlen
, prevlen
= 0;
505 unsigned char encoding
= 0;
506 long long value
= 123456789; /* initialized to avoid warning. Using a value
507 that is easy to see if for some reason
508 we use it uninitialized. */
511 /* Find out prevlen for the entry that is inserted. */
512 if (p
[0] != ZIP_END
) {
514 prevlen
= entry
.prevrawlen
;
516 unsigned char *ptail
= ZIPLIST_ENTRY_TAIL(zl
);
517 if (ptail
[0] != ZIP_END
) {
518 prevlen
= zipRawEntryLength(ptail
);
522 /* See if the entry can be encoded */
523 if (zipTryEncoding(s
,slen
,&value
,&encoding
)) {
524 /* 'encoding' is set to the appropriate integer encoding */
525 reqlen
= zipIntSize(encoding
);
527 /* 'encoding' is untouched, however zipEncodeLength will use the
528 * string length to figure out how to encode it. */
531 /* We need space for both the length of the previous entry and
532 * the length of the payload. */
533 reqlen
+= zipPrevEncodeLength(NULL
,prevlen
);
534 reqlen
+= zipEncodeLength(NULL
,encoding
,slen
);
536 /* When the insert position is not equal to the tail, we need to
537 * make sure that the next entry can hold this entry's length in
538 * its prevlen field. */
539 nextdiff
= (p
[0] != ZIP_END
) ? zipPrevLenByteDiff(p
,reqlen
) : 0;
541 /* Store offset because a realloc may change the address of zl. */
543 zl
= ziplistResize(zl
,curlen
+reqlen
+nextdiff
);
546 /* Apply memory move when necessary and update tail offset. */
547 if (p
[0] != ZIP_END
) {
548 /* Subtract one because of the ZIP_END bytes */
549 memmove(p
+reqlen
,p
-nextdiff
,curlen
-offset
-1+nextdiff
);
551 /* Encode this entry's raw length in the next entry. */
552 zipPrevEncodeLength(p
+reqlen
,reqlen
);
554 /* Update offset for tail */
555 ZIPLIST_TAIL_OFFSET(zl
) =
556 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))+reqlen
);
558 /* When the tail contains more than one entry, we need to take
559 * "nextdiff" in account as well. Otherwise, a change in the
560 * size of prevlen doesn't have an effect on the *tail* offset. */
561 tail
= zipEntry(p
+reqlen
);
562 if (p
[reqlen
+tail
.headersize
+tail
.len
] != ZIP_END
) {
563 ZIPLIST_TAIL_OFFSET(zl
) =
564 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))+nextdiff
);
567 /* This element will be the new tail. */
568 ZIPLIST_TAIL_OFFSET(zl
) = intrev32ifbe(p
-zl
);
571 /* When nextdiff != 0, the raw length of the next entry has changed, so
572 * we need to cascade the update throughout the ziplist */
575 zl
= __ziplistCascadeUpdate(zl
,p
+reqlen
);
579 /* Write the entry */
580 p
+= zipPrevEncodeLength(p
,prevlen
);
581 p
+= zipEncodeLength(p
,encoding
,slen
);
582 if (ZIP_IS_STR(encoding
)) {
585 zipSaveInteger(p
,value
,encoding
);
587 ZIPLIST_INCR_LENGTH(zl
,1);
591 unsigned char *ziplistPush(unsigned char *zl
, unsigned char *s
, unsigned int slen
, int where
) {
593 p
= (where
== ZIPLIST_HEAD
) ? ZIPLIST_ENTRY_HEAD(zl
) : ZIPLIST_ENTRY_END(zl
);
594 return __ziplistInsert(zl
,p
,s
,slen
);
597 /* Returns an offset to use for iterating with ziplistNext. When the given
598 * index is negative, the list is traversed back to front. When the list
599 * doesn't contain an element at the provided index, NULL is returned. */
600 unsigned char *ziplistIndex(unsigned char *zl
, int index
) {
605 p
= ZIPLIST_ENTRY_TAIL(zl
);
606 if (p
[0] != ZIP_END
) {
608 while (entry
.prevrawlen
> 0 && index
--) {
609 p
-= entry
.prevrawlen
;
614 p
= ZIPLIST_ENTRY_HEAD(zl
);
615 while (p
[0] != ZIP_END
&& index
--) {
616 p
+= zipRawEntryLength(p
);
619 return (p
[0] == ZIP_END
|| index
> 0) ? NULL
: p
;
622 /* Return pointer to next entry in ziplist.
624 * zl is the pointer to the ziplist
625 * p is the pointer to the current element
627 * The element after 'p' is returned, otherwise NULL if we are at the end. */
628 unsigned char *ziplistNext(unsigned char *zl
, unsigned char *p
) {
631 /* "p" could be equal to ZIP_END, caused by ziplistDelete,
632 * and we should return NULL. Otherwise, we should return NULL
633 * when the *next* element is ZIP_END (there is no next entry). */
634 if (p
[0] == ZIP_END
) {
638 p
+= zipRawEntryLength(p
);
639 if (p
[0] == ZIP_END
) {
646 /* Return pointer to previous entry in ziplist. */
647 unsigned char *ziplistPrev(unsigned char *zl
, unsigned char *p
) {
650 /* Iterating backwards from ZIP_END should return the tail. When "p" is
651 * equal to the first element of the list, we're already at the head,
652 * and should return NULL. */
653 if (p
[0] == ZIP_END
) {
654 p
= ZIPLIST_ENTRY_TAIL(zl
);
655 return (p
[0] == ZIP_END
) ? NULL
: p
;
656 } else if (p
== ZIPLIST_ENTRY_HEAD(zl
)) {
660 assert(entry
.prevrawlen
> 0);
661 return p
-entry
.prevrawlen
;
665 /* Get entry pointer to by 'p' and store in either 'e' or 'v' depending
666 * on the encoding of the entry. 'e' is always set to NULL to be able
667 * to find out whether the string pointer or the integer value was set.
668 * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */
669 unsigned int ziplistGet(unsigned char *p
, unsigned char **sstr
, unsigned int *slen
, long long *sval
) {
671 if (p
== NULL
|| p
[0] == ZIP_END
) return 0;
672 if (sstr
) *sstr
= NULL
;
675 if (ZIP_IS_STR(entry
.encoding
)) {
678 *sstr
= p
+entry
.headersize
;
682 *sval
= zipLoadInteger(p
+entry
.headersize
,entry
.encoding
);
688 /* Insert an entry at "p". */
689 unsigned char *ziplistInsert(unsigned char *zl
, unsigned char *p
, unsigned char *s
, unsigned int slen
) {
690 return __ziplistInsert(zl
,p
,s
,slen
);
693 /* Delete a single entry from the ziplist, pointed to by *p.
694 * Also update *p in place, to be able to iterate over the
695 * ziplist, while deleting entries. */
696 unsigned char *ziplistDelete(unsigned char *zl
, unsigned char **p
) {
697 size_t offset
= *p
-zl
;
698 zl
= __ziplistDelete(zl
,*p
,1);
700 /* Store pointer to current element in p, because ziplistDelete will
701 * do a realloc which might result in a different "zl"-pointer.
702 * When the delete direction is back to front, we might delete the last
703 * entry and end up with "p" pointing to ZIP_END, so check this. */
708 /* Delete a range of entries from the ziplist. */
709 unsigned char *ziplistDeleteRange(unsigned char *zl
, unsigned int index
, unsigned int num
) {
710 unsigned char *p
= ziplistIndex(zl
,index
);
711 return (p
== NULL
) ? zl
: __ziplistDelete(zl
,p
,num
);
714 /* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
715 unsigned int ziplistCompare(unsigned char *p
, unsigned char *sstr
, unsigned int slen
) {
717 unsigned char sencoding
;
718 long long zval
, sval
;
719 if (p
[0] == ZIP_END
) return 0;
722 if (ZIP_IS_STR(entry
.encoding
)) {
724 if (entry
.len
== slen
) {
725 return memcmp(p
+entry
.headersize
,sstr
,slen
) == 0;
730 /* Try to compare encoded values */
731 if (zipTryEncoding(sstr
,slen
,&sval
,&sencoding
)) {
732 if (entry
.encoding
== sencoding
) {
733 zval
= zipLoadInteger(p
+entry
.headersize
,entry
.encoding
);
741 /* Find pointer to the entry equal to the specified entry. Skip 'skip' entries
742 * between every comparison. Returns NULL when the field could not be found. */
743 unsigned char *ziplistFind(unsigned char *p
, unsigned char *vstr
, unsigned int vlen
, unsigned int skip
) {
745 unsigned char vencoding
= 0;
748 while (p
[0] != ZIP_END
) {
749 unsigned int prevlensize
, encoding
, lensize
, len
;
752 ZIP_DECODE_PREVLENSIZE(p
, prevlensize
);
753 ZIP_DECODE_LENGTH(p
+ prevlensize
, encoding
, lensize
, len
);
754 q
= p
+ prevlensize
+ lensize
;
757 /* Compare current entry with specified entry */
758 if (ZIP_IS_STR(encoding
)) {
759 if (len
== vlen
&& memcmp(q
, vstr
, vlen
) == 0) {
763 /* Find out if the specified entry can be encoded */
764 if (vencoding
== 0) {
765 /* UINT_MAX when the entry CANNOT be encoded */
766 if (!zipTryEncoding(vstr
, vlen
, &vll
, &vencoding
)) {
767 vencoding
= UCHAR_MAX
;
770 /* Must be non-zero by now */
774 /* Compare current entry with specified entry */
775 if (encoding
== vencoding
) {
776 long long ll
= zipLoadInteger(q
, encoding
);
783 /* Reset skip count */
790 /* Move to next entry */
797 /* Return length of ziplist. */
798 unsigned int ziplistLen(unsigned char *zl
) {
799 unsigned int len
= 0;
800 if (intrev16ifbe(ZIPLIST_LENGTH(zl
)) < UINT16_MAX
) {
801 len
= intrev16ifbe(ZIPLIST_LENGTH(zl
));
803 unsigned char *p
= zl
+ZIPLIST_HEADER_SIZE
;
804 while (*p
!= ZIP_END
) {
805 p
+= zipRawEntryLength(p
);
809 /* Re-store length if small enough */
810 if (len
< UINT16_MAX
) ZIPLIST_LENGTH(zl
) = intrev16ifbe(len
);
815 /* Return ziplist blob size in bytes. */
816 size_t ziplistBlobLen(unsigned char *zl
) {
817 return intrev32ifbe(ZIPLIST_BYTES(zl
));
820 void ziplistRepr(unsigned char *zl
) {
828 "{tail offset %u}\n",
829 intrev32ifbe(ZIPLIST_BYTES(zl
)),
830 intrev16ifbe(ZIPLIST_LENGTH(zl
)),
831 intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
)));
832 p
= ZIPLIST_ENTRY_HEAD(zl
);
833 while(*p
!= ZIP_END
) {
848 (unsigned long) (p
-zl
),
849 entry
.headersize
+entry
.len
,
852 entry
.prevrawlensize
,
854 p
+= entry
.headersize
;
855 if (ZIP_IS_STR(entry
.encoding
)) {
856 if (entry
.len
> 40) {
857 if (fwrite(p
,40,1,stdout
) == 0) perror("fwrite");
861 fwrite(p
,entry
.len
,1,stdout
) == 0) perror("fwrite");
864 printf("%lld", (long long) zipLoadInteger(p
,entry
.encoding
));
873 #ifdef ZIPLIST_TEST_MAIN
874 #include <sys/time.h>
878 #define debug(f, ...) { if (DEBUG) printf(f, __VA_ARGS__); }
880 unsigned char *createList() {
881 unsigned char *zl
= ziplistNew();
882 zl
= ziplistPush(zl
, (unsigned char*)"foo", 3, ZIPLIST_TAIL
);
883 zl
= ziplistPush(zl
, (unsigned char*)"quux", 4, ZIPLIST_TAIL
);
884 zl
= ziplistPush(zl
, (unsigned char*)"hello", 5, ZIPLIST_HEAD
);
885 zl
= ziplistPush(zl
, (unsigned char*)"1024", 4, ZIPLIST_TAIL
);
889 unsigned char *createIntList() {
890 unsigned char *zl
= ziplistNew();
894 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
895 sprintf(buf
, "128000");
896 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
897 sprintf(buf
, "-100");
898 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_HEAD
);
899 sprintf(buf
, "4294967296");
900 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_HEAD
);
901 sprintf(buf
, "non integer");
902 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
903 sprintf(buf
, "much much longer non integer");
904 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
908 long long usec(void) {
910 gettimeofday(&tv
,NULL
);
911 return (((long long)tv
.tv_sec
)*1000000)+tv
.tv_usec
;
914 void stress(int pos
, int num
, int maxsize
, int dnum
) {
917 char posstr
[2][5] = { "HEAD", "TAIL" };
919 for (i
= 0; i
< maxsize
; i
+=dnum
) {
921 for (j
= 0; j
< i
; j
++) {
922 zl
= ziplistPush(zl
,(unsigned char*)"quux",4,ZIPLIST_TAIL
);
925 /* Do num times a push+pop from pos */
927 for (k
= 0; k
< num
; k
++) {
928 zl
= ziplistPush(zl
,(unsigned char*)"quux",4,pos
);
929 zl
= ziplistDeleteRange(zl
,0,1);
931 printf("List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\n",
932 i
,intrev32ifbe(ZIPLIST_BYTES(zl
)),num
,posstr
[pos
],usec()-start
);
937 void pop(unsigned char *zl
, int where
) {
938 unsigned char *p
, *vstr
;
942 p
= ziplistIndex(zl
,where
== ZIPLIST_HEAD
? 0 : -1);
943 if (ziplistGet(p
,&vstr
,&vlen
,&vlong
)) {
944 if (where
== ZIPLIST_HEAD
)
945 printf("Pop head: ");
947 printf("Pop tail: ");
950 if (vlen
&& fwrite(vstr
,vlen
,1,stdout
) == 0) perror("fwrite");
952 printf("%lld", vlong
);
955 ziplistDeleteRange(zl
,-1,1);
957 printf("ERROR: Could not pop\n");
962 int randstring(char *target
, unsigned int min
, unsigned int max
) {
963 int p
, len
= min
+rand()%(max
-min
+1);
983 target
[p
++] = minval
+rand()%(maxval
-minval
+1);
987 int main(int argc
, char **argv
) {
988 unsigned char *zl
, *p
;
989 unsigned char *entry
;
993 /* If an argument is given, use it as the random seed. */
995 srand(atoi(argv
[1]));
997 zl
= createIntList();
1003 pop(zl
,ZIPLIST_TAIL
);
1006 pop(zl
,ZIPLIST_HEAD
);
1009 pop(zl
,ZIPLIST_TAIL
);
1012 pop(zl
,ZIPLIST_TAIL
);
1015 printf("Get element at index 3:\n");
1018 p
= ziplistIndex(zl
, 3);
1019 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
1020 printf("ERROR: Could not access index 3\n");
1024 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1027 printf("%lld\n", value
);
1032 printf("Get element at index 4 (out of range):\n");
1035 p
= ziplistIndex(zl
, 4);
1037 printf("No entry\n");
1039 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p
-zl
);
1045 printf("Get element at index -1 (last element):\n");
1048 p
= ziplistIndex(zl
, -1);
1049 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
1050 printf("ERROR: Could not access index -1\n");
1054 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1057 printf("%lld\n", value
);
1062 printf("Get element at index -4 (first element):\n");
1065 p
= ziplistIndex(zl
, -4);
1066 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
1067 printf("ERROR: Could not access index -4\n");
1071 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1074 printf("%lld\n", value
);
1079 printf("Get element at index -5 (reverse out of range):\n");
1082 p
= ziplistIndex(zl
, -5);
1084 printf("No entry\n");
1086 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p
-zl
);
1092 printf("Iterate list from 0 to end:\n");
1095 p
= ziplistIndex(zl
, 0);
1096 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1099 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1101 printf("%lld", value
);
1103 p
= ziplistNext(zl
,p
);
1109 printf("Iterate list from 1 to end:\n");
1112 p
= ziplistIndex(zl
, 1);
1113 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1116 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1118 printf("%lld", value
);
1120 p
= ziplistNext(zl
,p
);
1126 printf("Iterate list from 2 to end:\n");
1129 p
= ziplistIndex(zl
, 2);
1130 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1133 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1135 printf("%lld", value
);
1137 p
= ziplistNext(zl
,p
);
1143 printf("Iterate starting out of range:\n");
1146 p
= ziplistIndex(zl
, 4);
1147 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
1148 printf("No entry\n");
1155 printf("Iterate from back to front:\n");
1158 p
= ziplistIndex(zl
, -1);
1159 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1162 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1164 printf("%lld", value
);
1166 p
= ziplistPrev(zl
,p
);
1172 printf("Iterate from back to front, deleting all items:\n");
1175 p
= ziplistIndex(zl
, -1);
1176 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1179 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1181 printf("%lld", value
);
1183 zl
= ziplistDelete(zl
,&p
);
1184 p
= ziplistPrev(zl
,p
);
1190 printf("Delete inclusive range 0,0:\n");
1193 zl
= ziplistDeleteRange(zl
, 0, 1);
1197 printf("Delete inclusive range 0,1:\n");
1200 zl
= ziplistDeleteRange(zl
, 0, 2);
1204 printf("Delete inclusive range 1,2:\n");
1207 zl
= ziplistDeleteRange(zl
, 1, 2);
1211 printf("Delete with start index out of range:\n");
1214 zl
= ziplistDeleteRange(zl
, 5, 1);
1218 printf("Delete with num overflow:\n");
1221 zl
= ziplistDeleteRange(zl
, 1, 5);
1225 printf("Delete foo while iterating:\n");
1228 p
= ziplistIndex(zl
,0);
1229 while (ziplistGet(p
,&entry
,&elen
,&value
)) {
1230 if (entry
&& strncmp("foo",(char*)entry
,elen
) == 0) {
1231 printf("Delete foo\n");
1232 zl
= ziplistDelete(zl
,&p
);
1236 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0)
1239 printf("%lld",value
);
1241 p
= ziplistNext(zl
,p
);
1249 printf("Regression test for >255 byte strings:\n");
1251 char v1
[257],v2
[257];
1255 zl
= ziplistPush(zl
,(unsigned char*)v1
,strlen(v1
),ZIPLIST_TAIL
);
1256 zl
= ziplistPush(zl
,(unsigned char*)v2
,strlen(v2
),ZIPLIST_TAIL
);
1258 /* Pop values again and compare their value. */
1259 p
= ziplistIndex(zl
,0);
1260 assert(ziplistGet(p
,&entry
,&elen
,&value
));
1261 assert(strncmp(v1
,(char*)entry
,elen
) == 0);
1262 p
= ziplistIndex(zl
,1);
1263 assert(ziplistGet(p
,&entry
,&elen
,&value
));
1264 assert(strncmp(v2
,(char*)entry
,elen
) == 0);
1265 printf("SUCCESS\n\n");
1268 printf("Create long list and check indices:\n");
1273 for (i
= 0; i
< 1000; i
++) {
1274 len
= sprintf(buf
,"%d",i
);
1275 zl
= ziplistPush(zl
,(unsigned char*)buf
,len
,ZIPLIST_TAIL
);
1277 for (i
= 0; i
< 1000; i
++) {
1278 p
= ziplistIndex(zl
,i
);
1279 assert(ziplistGet(p
,NULL
,NULL
,&value
));
1282 p
= ziplistIndex(zl
,-i
-1);
1283 assert(ziplistGet(p
,NULL
,NULL
,&value
));
1284 assert(999-i
== value
);
1286 printf("SUCCESS\n\n");
1289 printf("Compare strings with ziplist entries:\n");
1292 p
= ziplistIndex(zl
,0);
1293 if (!ziplistCompare(p
,(unsigned char*)"hello",5)) {
1294 printf("ERROR: not \"hello\"\n");
1297 if (ziplistCompare(p
,(unsigned char*)"hella",5)) {
1298 printf("ERROR: \"hella\"\n");
1302 p
= ziplistIndex(zl
,3);
1303 if (!ziplistCompare(p
,(unsigned char*)"1024",4)) {
1304 printf("ERROR: not \"1024\"\n");
1307 if (ziplistCompare(p
,(unsigned char*)"1025",4)) {
1308 printf("ERROR: \"1025\"\n");
1311 printf("SUCCESS\n\n");
1314 printf("Stress with random payloads of different encoding:\n");
1323 /* Hold temp vars from ziplist */
1324 unsigned char *sstr
;
1328 for (i
= 0; i
< 20000; i
++) {
1331 listSetFreeMethod(ref
,sdsfree
);
1335 for (j
= 0; j
< len
; j
++) {
1336 where
= (rand() & 1) ? ZIPLIST_HEAD
: ZIPLIST_TAIL
;
1338 buflen
= randstring(buf
,1,sizeof(buf
)-1);
1340 switch(rand() % 3) {
1342 buflen
= sprintf(buf
,"%lld",(0LL + rand()) >> 20);
1345 buflen
= sprintf(buf
,"%lld",(0LL + rand()));
1348 buflen
= sprintf(buf
,"%lld",(0LL + rand()) << 20);
1355 /* Add to ziplist */
1356 zl
= ziplistPush(zl
, (unsigned char*)buf
, buflen
, where
);
1358 /* Add to reference list */
1359 if (where
== ZIPLIST_HEAD
) {
1360 listAddNodeHead(ref
,sdsnewlen(buf
, buflen
));
1361 } else if (where
== ZIPLIST_TAIL
) {
1362 listAddNodeTail(ref
,sdsnewlen(buf
, buflen
));
1368 assert(listLength(ref
) == ziplistLen(zl
));
1369 for (j
= 0; j
< len
; j
++) {
1370 /* Naive way to get elements, but similar to the stresser
1371 * executed from the Tcl test suite. */
1372 p
= ziplistIndex(zl
,j
);
1373 refnode
= listIndex(ref
,j
);
1375 assert(ziplistGet(p
,&sstr
,&slen
,&sval
));
1377 buflen
= sprintf(buf
,"%lld",sval
);
1380 memcpy(buf
,sstr
,buflen
);
1383 assert(memcmp(buf
,listNodeValue(refnode
),buflen
) == 0);
1388 printf("SUCCESS\n\n");
1391 printf("Stress with variable ziplist size:\n");
1393 stress(ZIPLIST_HEAD
,100000,16384,256);
1394 stress(ZIPLIST_TAIL
,100000,16384,256);