]>
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).
62 * Integer encoded as 24 bit signed (3 bytes).
64 * Integer encoded as 8 bit signed (1 byte).
65 * |1111xxxx| - (with xxxx between 0000 and 1101) immediate 4 bit integer.
66 * Unsigned integer from 0 to 12. The encoded value is actually from
67 * 1 to 13 because 0000 and 1111 can not be used, so 1 should be
68 * subtracted from the encoded 4 bit value to obtain the right value.
69 * |11111111| - End of ziplist.
71 * All the integers are represented in little endian byte order.
83 #include "endianconv.h"
86 #define ZIP_BIGLEN 254
88 /* Different encoding/length possibilities */
89 #define ZIP_STR_MASK 0xc0
90 #define ZIP_INT_MASK 0x30
91 #define ZIP_STR_06B (0 << 6)
92 #define ZIP_STR_14B (1 << 6)
93 #define ZIP_STR_32B (2 << 6)
94 #define ZIP_INT_16B (0xc0 | 0<<4)
95 #define ZIP_INT_32B (0xc0 | 1<<4)
96 #define ZIP_INT_64B (0xc0 | 2<<4)
97 #define ZIP_INT_24B (0xc0 | 3<<4)
98 #define ZIP_INT_8B 0xfe
99 /* 4 bit integer immediate encoding */
100 #define ZIP_INT_IMM_MASK 0x0f
101 #define ZIP_INT_IMM_MIN 0xf1 /* 11110001 */
102 #define ZIP_INT_IMM_MAX 0xfd /* 11111101 */
103 #define ZIP_INT_IMM_VAL(v) (v & ZIP_INT_IMM_MASK)
105 #define INT24_MAX 0x7fffff
106 #define INT24_MIN (-INT24_MAX - 1)
108 /* Macro to determine type */
109 #define ZIP_IS_STR(enc) (((enc) & ZIP_STR_MASK) < ZIP_STR_MASK)
112 #define ZIPLIST_BYTES(zl) (*((uint32_t*)(zl)))
113 #define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t))))
114 #define ZIPLIST_LENGTH(zl) (*((uint16_t*)((zl)+sizeof(uint32_t)*2)))
115 #define ZIPLIST_HEADER_SIZE (sizeof(uint32_t)*2+sizeof(uint16_t))
116 #define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE)
117 #define ZIPLIST_ENTRY_TAIL(zl) ((zl)+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)))
118 #define ZIPLIST_ENTRY_END(zl) ((zl)+intrev32ifbe(ZIPLIST_BYTES(zl))-1)
120 /* We know a positive increment can only be 1 because entries can only be
121 * pushed one at a time. */
122 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
123 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) \
124 ZIPLIST_LENGTH(zl) = intrev16ifbe(intrev16ifbe(ZIPLIST_LENGTH(zl))+incr); \
127 typedef struct zlentry
{
128 unsigned int prevrawlensize
, prevrawlen
;
129 unsigned int lensize
, len
;
130 unsigned int headersize
;
131 unsigned char encoding
;
135 /* Extract the encoding from the byte pointed by 'ptr' and set it into
137 #define ZIP_ENTRY_ENCODING(ptr, encoding) do { \
138 (encoding) = (ptr[0]); \
139 if ((encoding) < ZIP_STR_MASK) (encoding) &= ZIP_STR_MASK; \
142 /* Return bytes needed to store integer encoded by 'encoding' */
143 static unsigned int zipIntSize(unsigned char encoding
) {
145 case ZIP_INT_8B
: return 1;
146 case ZIP_INT_16B
: return 2;
147 case ZIP_INT_24B
: return 3;
148 case ZIP_INT_32B
: return 4;
149 case ZIP_INT_64B
: return 8;
150 default: return 0; /* 4 bit immediate */
156 /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
157 * the amount of bytes required to encode such a length. */
158 static unsigned int zipEncodeLength(unsigned char *p
, unsigned char encoding
, unsigned int rawlen
) {
159 unsigned char len
= 1, buf
[5];
161 if (ZIP_IS_STR(encoding
)) {
162 /* Although encoding is given it may not be set for strings,
163 * so we determine it here using the raw length. */
164 if (rawlen
<= 0x3f) {
166 buf
[0] = ZIP_STR_06B
| rawlen
;
167 } else if (rawlen
<= 0x3fff) {
170 buf
[0] = ZIP_STR_14B
| ((rawlen
>> 8) & 0x3f);
171 buf
[1] = rawlen
& 0xff;
175 buf
[0] = ZIP_STR_32B
;
176 buf
[1] = (rawlen
>> 24) & 0xff;
177 buf
[2] = (rawlen
>> 16) & 0xff;
178 buf
[3] = (rawlen
>> 8) & 0xff;
179 buf
[4] = rawlen
& 0xff;
182 /* Implies integer encoding, so length is always 1. */
187 /* Store this length at p */
192 /* Decode the length encoded in 'ptr'. The 'encoding' variable will hold the
193 * entries encoding, the 'lensize' variable will hold the number of bytes
194 * required to encode the entries length, and the 'len' variable will hold the
196 #define ZIP_DECODE_LENGTH(ptr, encoding, lensize, len) do { \
197 ZIP_ENTRY_ENCODING((ptr), (encoding)); \
198 if ((encoding) < ZIP_STR_MASK) { \
199 if ((encoding) == ZIP_STR_06B) { \
201 (len) = (ptr)[0] & 0x3f; \
202 } else if ((encoding) == ZIP_STR_14B) { \
204 (len) = (((ptr)[0] & 0x3f) << 8) | (ptr)[1]; \
205 } else if (encoding == ZIP_STR_32B) { \
207 (len) = ((ptr)[1] << 24) | \
216 (len) = zipIntSize(encoding); \
220 /* Encode the length of the previous entry and write it to "p". Return the
221 * number of bytes needed to encode this length if "p" is NULL. */
222 static unsigned int zipPrevEncodeLength(unsigned char *p
, unsigned int len
) {
224 return (len
< ZIP_BIGLEN
) ? 1 : sizeof(len
)+1;
226 if (len
< ZIP_BIGLEN
) {
231 memcpy(p
+1,&len
,sizeof(len
));
233 return 1+sizeof(len
);
238 /* Encode the length of the previous entry and write it to "p". This only
239 * uses the larger encoding (required in __ziplistCascadeUpdate). */
240 static void zipPrevEncodeLengthForceLarge(unsigned char *p
, unsigned int len
) {
241 if (p
== NULL
) return;
243 memcpy(p
+1,&len
,sizeof(len
));
247 /* Decode the number of bytes required to store the length of the previous
248 * element, from the perspective of the entry pointed to by 'ptr'. */
249 #define ZIP_DECODE_PREVLENSIZE(ptr, prevlensize) do { \
250 if ((ptr)[0] < ZIP_BIGLEN) { \
257 /* Decode the length of the previous element, from the perspective of the entry
258 * pointed to by 'ptr'. */
259 #define ZIP_DECODE_PREVLEN(ptr, prevlensize, prevlen) do { \
260 ZIP_DECODE_PREVLENSIZE(ptr, prevlensize); \
261 if ((prevlensize) == 1) { \
262 (prevlen) = (ptr)[0]; \
263 } else if ((prevlensize) == 5) { \
264 assert(sizeof((prevlensize)) == 4); \
265 memcpy(&(prevlen), ((char*)(ptr)) + 1, 4); \
266 memrev32ifbe(&prevlen); \
270 /* Return the difference in number of bytes needed to store the length of the
271 * previous element 'len', in the entry pointed to by 'p'. */
272 static int zipPrevLenByteDiff(unsigned char *p
, unsigned int len
) {
273 unsigned int prevlensize
;
274 ZIP_DECODE_PREVLENSIZE(p
, prevlensize
);
275 return zipPrevEncodeLength(NULL
, len
) - prevlensize
;
278 /* Return the total number of bytes used by the entry pointed to by 'p'. */
279 static unsigned int zipRawEntryLength(unsigned char *p
) {
280 unsigned int prevlensize
, encoding
, lensize
, len
;
281 ZIP_DECODE_PREVLENSIZE(p
, prevlensize
);
282 ZIP_DECODE_LENGTH(p
+ prevlensize
, encoding
, lensize
, len
);
283 return prevlensize
+ lensize
+ len
;
286 /* Check if string pointed to by 'entry' can be encoded as an integer.
287 * Stores the integer value in 'v' and its encoding in 'encoding'. */
288 static int zipTryEncoding(unsigned char *entry
, unsigned int entrylen
, long long *v
, unsigned char *encoding
) {
291 if (entrylen
>= 32 || entrylen
== 0) return 0;
292 if (string2ll((char*)entry
,entrylen
,&value
)) {
293 /* Great, the string can be encoded. Check what's the smallest
294 * of our encoding types that can hold this value. */
295 if (value
>= 0 && value
<= 12) {
296 *encoding
= ZIP_INT_IMM_MIN
+value
;
297 } else if (value
>= INT8_MIN
&& value
<= INT8_MAX
) {
298 *encoding
= ZIP_INT_8B
;
299 } else if (value
>= INT16_MIN
&& value
<= INT16_MAX
) {
300 *encoding
= ZIP_INT_16B
;
301 } else if (value
>= INT24_MIN
&& value
<= INT24_MAX
) {
302 *encoding
= ZIP_INT_24B
;
303 } else if (value
>= INT32_MIN
&& value
<= INT32_MAX
) {
304 *encoding
= ZIP_INT_32B
;
306 *encoding
= ZIP_INT_64B
;
314 /* Store integer 'value' at 'p', encoded as 'encoding' */
315 static void zipSaveInteger(unsigned char *p
, int64_t value
, unsigned char encoding
) {
319 if (encoding
== ZIP_INT_8B
) {
320 ((char*)p
)[0] = (char)value
;
321 } else if (encoding
== ZIP_INT_16B
) {
323 memcpy(p
,&i16
,sizeof(i16
));
325 } else if (encoding
== ZIP_INT_24B
) {
328 memcpy(p
,((unsigned char*)&i32
)+1,sizeof(i32
)-sizeof(int8_t));
329 } else if (encoding
== ZIP_INT_32B
) {
331 memcpy(p
,&i32
,sizeof(i32
));
333 } else if (encoding
== ZIP_INT_64B
) {
335 memcpy(p
,&i64
,sizeof(i64
));
337 } else if (encoding
>= ZIP_INT_IMM_MIN
&& encoding
<= ZIP_INT_IMM_MAX
) {
338 /* Nothing to do, the value is stored in the encoding itself. */
344 /* Read integer encoded as 'encoding' from 'p' */
345 static int64_t zipLoadInteger(unsigned char *p
, unsigned char encoding
) {
348 int64_t i64
, ret
= 0;
349 if (encoding
== ZIP_INT_8B
) {
351 } else if (encoding
== ZIP_INT_16B
) {
352 memcpy(&i16
,p
,sizeof(i16
));
355 } else if (encoding
== ZIP_INT_32B
) {
356 memcpy(&i32
,p
,sizeof(i32
));
359 } else if (encoding
== ZIP_INT_24B
) {
361 memcpy(((unsigned char*)&i32
)+1,p
,sizeof(i32
)-sizeof(int8_t));
364 } else if (encoding
== ZIP_INT_64B
) {
365 memcpy(&i64
,p
,sizeof(i64
));
368 } else if (encoding
>= ZIP_INT_IMM_MIN
&& encoding
<= ZIP_INT_IMM_MAX
) {
369 ret
= (encoding
& ZIP_INT_IMM_MASK
)-1;
376 /* Return a struct with all information about an entry. */
377 static zlentry
zipEntry(unsigned char *p
) {
380 ZIP_DECODE_PREVLEN(p
, e
.prevrawlensize
, e
.prevrawlen
);
381 ZIP_DECODE_LENGTH(p
+ e
.prevrawlensize
, e
.encoding
, e
.lensize
, e
.len
);
382 e
.headersize
= e
.prevrawlensize
+ e
.lensize
;
387 /* Create a new empty ziplist. */
388 unsigned char *ziplistNew(void) {
389 unsigned int bytes
= ZIPLIST_HEADER_SIZE
+1;
390 unsigned char *zl
= zmalloc(bytes
);
391 ZIPLIST_BYTES(zl
) = intrev32ifbe(bytes
);
392 ZIPLIST_TAIL_OFFSET(zl
) = intrev32ifbe(ZIPLIST_HEADER_SIZE
);
393 ZIPLIST_LENGTH(zl
) = 0;
394 zl
[bytes
-1] = ZIP_END
;
398 /* Resize the ziplist. */
399 static unsigned char *ziplistResize(unsigned char *zl
, unsigned int len
) {
400 zl
= zrealloc(zl
,len
);
401 ZIPLIST_BYTES(zl
) = intrev32ifbe(len
);
406 /* When an entry is inserted, we need to set the prevlen field of the next
407 * entry to equal the length of the inserted entry. It can occur that this
408 * length cannot be encoded in 1 byte and the next entry needs to be grow
409 * a bit larger to hold the 5-byte encoded prevlen. This can be done for free,
410 * because this only happens when an entry is already being inserted (which
411 * causes a realloc and memmove). However, encoding the prevlen may require
412 * that this entry is grown as well. This effect may cascade throughout
413 * the ziplist when there are consecutive entries with a size close to
414 * ZIP_BIGLEN, so we need to check that the prevlen can be encoded in every
417 * Note that this effect can also happen in reverse, where the bytes required
418 * to encode the prevlen field can shrink. This effect is deliberately ignored,
419 * because it can cause a "flapping" effect where a chain prevlen fields is
420 * first grown and then shrunk again after consecutive inserts. Rather, the
421 * field is allowed to stay larger than necessary, because a large prevlen
422 * field implies the ziplist is holding large entries anyway.
424 * The pointer "p" points to the first entry that does NOT need to be
425 * updated, i.e. consecutive fields MAY need an update. */
426 static unsigned char *__ziplistCascadeUpdate(unsigned char *zl
, unsigned char *p
) {
427 size_t curlen
= intrev32ifbe(ZIPLIST_BYTES(zl
)), rawlen
, rawlensize
;
428 size_t offset
, noffset
, extra
;
432 while (p
[0] != ZIP_END
) {
434 rawlen
= cur
.headersize
+ cur
.len
;
435 rawlensize
= zipPrevEncodeLength(NULL
,rawlen
);
437 /* Abort if there is no next entry. */
438 if (p
[rawlen
] == ZIP_END
) break;
439 next
= zipEntry(p
+rawlen
);
441 /* Abort when "prevlen" has not changed. */
442 if (next
.prevrawlen
== rawlen
) break;
444 if (next
.prevrawlensize
< rawlensize
) {
445 /* The "prevlen" field of "next" needs more bytes to hold
446 * the raw length of "cur". */
448 extra
= rawlensize
-next
.prevrawlensize
;
449 zl
= ziplistResize(zl
,curlen
+extra
);
452 /* Current pointer and offset for next element. */
456 /* Update tail offset when next element is not the tail element. */
457 if ((zl
+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))) != np
) {
458 ZIPLIST_TAIL_OFFSET(zl
) =
459 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))+extra
);
462 /* Move the tail to the back. */
463 memmove(np
+rawlensize
,
464 np
+next
.prevrawlensize
,
465 curlen
-noffset
-next
.prevrawlensize
-1);
466 zipPrevEncodeLength(np
,rawlen
);
468 /* Advance the cursor */
472 if (next
.prevrawlensize
> rawlensize
) {
473 /* This would result in shrinking, which we want to avoid.
474 * So, set "rawlen" in the available bytes. */
475 zipPrevEncodeLengthForceLarge(p
+rawlen
,rawlen
);
477 zipPrevEncodeLength(p
+rawlen
,rawlen
);
480 /* Stop here, as the raw length of "next" has not changed. */
487 /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
488 static unsigned char *__ziplistDelete(unsigned char *zl
, unsigned char *p
, unsigned int num
) {
489 unsigned int i
, totlen
, deleted
= 0;
495 for (i
= 0; p
[0] != ZIP_END
&& i
< num
; i
++) {
496 p
+= zipRawEntryLength(p
);
502 if (p
[0] != ZIP_END
) {
503 /* Tricky: storing the prevlen in this entry might reduce or
504 * increase the number of bytes needed, compared to the current
505 * prevlen. Note that we can always store this length because
506 * it was previously stored by an entry that is being deleted. */
507 nextdiff
= zipPrevLenByteDiff(p
,first
.prevrawlen
);
508 zipPrevEncodeLength(p
-nextdiff
,first
.prevrawlen
);
510 /* Update offset for tail */
511 ZIPLIST_TAIL_OFFSET(zl
) =
512 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))-totlen
);
514 /* When the tail contains more than one entry, we need to take
515 * "nextdiff" in account as well. Otherwise, a change in the
516 * size of prevlen doesn't have an effect on the *tail* offset. */
518 if (p
[tail
.headersize
+tail
.len
] != ZIP_END
) {
519 ZIPLIST_TAIL_OFFSET(zl
) =
520 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))+nextdiff
);
523 /* Move tail to the front of the ziplist */
524 memmove(first
.p
,p
-nextdiff
,
525 intrev32ifbe(ZIPLIST_BYTES(zl
))-(p
-zl
)-1+nextdiff
);
527 /* The entire tail was deleted. No need to move memory. */
528 ZIPLIST_TAIL_OFFSET(zl
) =
529 intrev32ifbe((first
.p
-zl
)-first
.prevrawlen
);
532 /* Resize and update length */
534 zl
= ziplistResize(zl
, intrev32ifbe(ZIPLIST_BYTES(zl
))-totlen
+nextdiff
);
535 ZIPLIST_INCR_LENGTH(zl
,-deleted
);
538 /* When nextdiff != 0, the raw length of the next entry has changed, so
539 * we need to cascade the update throughout the ziplist */
541 zl
= __ziplistCascadeUpdate(zl
,p
);
546 /* Insert item at "p". */
547 static unsigned char *__ziplistInsert(unsigned char *zl
, unsigned char *p
, unsigned char *s
, unsigned int slen
) {
548 size_t curlen
= intrev32ifbe(ZIPLIST_BYTES(zl
)), reqlen
, prevlen
= 0;
551 unsigned char encoding
= 0;
552 long long value
= 123456789; /* initialized to avoid warning. Using a value
553 that is easy to see if for some reason
554 we use it uninitialized. */
557 /* Find out prevlen for the entry that is inserted. */
558 if (p
[0] != ZIP_END
) {
560 prevlen
= entry
.prevrawlen
;
562 unsigned char *ptail
= ZIPLIST_ENTRY_TAIL(zl
);
563 if (ptail
[0] != ZIP_END
) {
564 prevlen
= zipRawEntryLength(ptail
);
568 /* See if the entry can be encoded */
569 if (zipTryEncoding(s
,slen
,&value
,&encoding
)) {
570 /* 'encoding' is set to the appropriate integer encoding */
571 reqlen
= zipIntSize(encoding
);
573 /* 'encoding' is untouched, however zipEncodeLength will use the
574 * string length to figure out how to encode it. */
577 /* We need space for both the length of the previous entry and
578 * the length of the payload. */
579 reqlen
+= zipPrevEncodeLength(NULL
,prevlen
);
580 reqlen
+= zipEncodeLength(NULL
,encoding
,slen
);
582 /* When the insert position is not equal to the tail, we need to
583 * make sure that the next entry can hold this entry's length in
584 * its prevlen field. */
585 nextdiff
= (p
[0] != ZIP_END
) ? zipPrevLenByteDiff(p
,reqlen
) : 0;
587 /* Store offset because a realloc may change the address of zl. */
589 zl
= ziplistResize(zl
,curlen
+reqlen
+nextdiff
);
592 /* Apply memory move when necessary and update tail offset. */
593 if (p
[0] != ZIP_END
) {
594 /* Subtract one because of the ZIP_END bytes */
595 memmove(p
+reqlen
,p
-nextdiff
,curlen
-offset
-1+nextdiff
);
597 /* Encode this entry's raw length in the next entry. */
598 zipPrevEncodeLength(p
+reqlen
,reqlen
);
600 /* Update offset for tail */
601 ZIPLIST_TAIL_OFFSET(zl
) =
602 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))+reqlen
);
604 /* When the tail contains more than one entry, we need to take
605 * "nextdiff" in account as well. Otherwise, a change in the
606 * size of prevlen doesn't have an effect on the *tail* offset. */
607 tail
= zipEntry(p
+reqlen
);
608 if (p
[reqlen
+tail
.headersize
+tail
.len
] != ZIP_END
) {
609 ZIPLIST_TAIL_OFFSET(zl
) =
610 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))+nextdiff
);
613 /* This element will be the new tail. */
614 ZIPLIST_TAIL_OFFSET(zl
) = intrev32ifbe(p
-zl
);
617 /* When nextdiff != 0, the raw length of the next entry has changed, so
618 * we need to cascade the update throughout the ziplist */
621 zl
= __ziplistCascadeUpdate(zl
,p
+reqlen
);
625 /* Write the entry */
626 p
+= zipPrevEncodeLength(p
,prevlen
);
627 p
+= zipEncodeLength(p
,encoding
,slen
);
628 if (ZIP_IS_STR(encoding
)) {
631 zipSaveInteger(p
,value
,encoding
);
633 ZIPLIST_INCR_LENGTH(zl
,1);
637 unsigned char *ziplistPush(unsigned char *zl
, unsigned char *s
, unsigned int slen
, int where
) {
639 p
= (where
== ZIPLIST_HEAD
) ? ZIPLIST_ENTRY_HEAD(zl
) : ZIPLIST_ENTRY_END(zl
);
640 return __ziplistInsert(zl
,p
,s
,slen
);
643 /* Returns an offset to use for iterating with ziplistNext. When the given
644 * index is negative, the list is traversed back to front. When the list
645 * doesn't contain an element at the provided index, NULL is returned. */
646 unsigned char *ziplistIndex(unsigned char *zl
, int index
) {
651 p
= ZIPLIST_ENTRY_TAIL(zl
);
652 if (p
[0] != ZIP_END
) {
654 while (entry
.prevrawlen
> 0 && index
--) {
655 p
-= entry
.prevrawlen
;
660 p
= ZIPLIST_ENTRY_HEAD(zl
);
661 while (p
[0] != ZIP_END
&& index
--) {
662 p
+= zipRawEntryLength(p
);
665 return (p
[0] == ZIP_END
|| index
> 0) ? NULL
: p
;
668 /* Return pointer to next entry in ziplist.
670 * zl is the pointer to the ziplist
671 * p is the pointer to the current element
673 * The element after 'p' is returned, otherwise NULL if we are at the end. */
674 unsigned char *ziplistNext(unsigned char *zl
, unsigned char *p
) {
677 /* "p" could be equal to ZIP_END, caused by ziplistDelete,
678 * and we should return NULL. Otherwise, we should return NULL
679 * when the *next* element is ZIP_END (there is no next entry). */
680 if (p
[0] == ZIP_END
) {
684 p
+= zipRawEntryLength(p
);
685 if (p
[0] == ZIP_END
) {
692 /* Return pointer to previous entry in ziplist. */
693 unsigned char *ziplistPrev(unsigned char *zl
, unsigned char *p
) {
696 /* Iterating backwards from ZIP_END should return the tail. When "p" is
697 * equal to the first element of the list, we're already at the head,
698 * and should return NULL. */
699 if (p
[0] == ZIP_END
) {
700 p
= ZIPLIST_ENTRY_TAIL(zl
);
701 return (p
[0] == ZIP_END
) ? NULL
: p
;
702 } else if (p
== ZIPLIST_ENTRY_HEAD(zl
)) {
706 assert(entry
.prevrawlen
> 0);
707 return p
-entry
.prevrawlen
;
711 /* Get entry pointer to by 'p' and store in either 'e' or 'v' depending
712 * on the encoding of the entry. 'e' is always set to NULL to be able
713 * to find out whether the string pointer or the integer value was set.
714 * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */
715 unsigned int ziplistGet(unsigned char *p
, unsigned char **sstr
, unsigned int *slen
, long long *sval
) {
717 if (p
== NULL
|| p
[0] == ZIP_END
) return 0;
718 if (sstr
) *sstr
= NULL
;
721 if (ZIP_IS_STR(entry
.encoding
)) {
724 *sstr
= p
+entry
.headersize
;
728 *sval
= zipLoadInteger(p
+entry
.headersize
,entry
.encoding
);
734 /* Insert an entry at "p". */
735 unsigned char *ziplistInsert(unsigned char *zl
, unsigned char *p
, unsigned char *s
, unsigned int slen
) {
736 return __ziplistInsert(zl
,p
,s
,slen
);
739 /* Delete a single entry from the ziplist, pointed to by *p.
740 * Also update *p in place, to be able to iterate over the
741 * ziplist, while deleting entries. */
742 unsigned char *ziplistDelete(unsigned char *zl
, unsigned char **p
) {
743 size_t offset
= *p
-zl
;
744 zl
= __ziplistDelete(zl
,*p
,1);
746 /* Store pointer to current element in p, because ziplistDelete will
747 * do a realloc which might result in a different "zl"-pointer.
748 * When the delete direction is back to front, we might delete the last
749 * entry and end up with "p" pointing to ZIP_END, so check this. */
754 /* Delete a range of entries from the ziplist. */
755 unsigned char *ziplistDeleteRange(unsigned char *zl
, unsigned int index
, unsigned int num
) {
756 unsigned char *p
= ziplistIndex(zl
,index
);
757 return (p
== NULL
) ? zl
: __ziplistDelete(zl
,p
,num
);
760 /* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
761 unsigned int ziplistCompare(unsigned char *p
, unsigned char *sstr
, unsigned int slen
) {
763 unsigned char sencoding
;
764 long long zval
, sval
;
765 if (p
[0] == ZIP_END
) return 0;
768 if (ZIP_IS_STR(entry
.encoding
)) {
770 if (entry
.len
== slen
) {
771 return memcmp(p
+entry
.headersize
,sstr
,slen
) == 0;
776 /* Try to compare encoded values. Don't compare encoding because
777 * different implementations may encoded integers differently. */
778 if (zipTryEncoding(sstr
,slen
,&sval
,&sencoding
)) {
779 zval
= zipLoadInteger(p
+entry
.headersize
,entry
.encoding
);
786 /* Find pointer to the entry equal to the specified entry. Skip 'skip' entries
787 * between every comparison. Returns NULL when the field could not be found. */
788 unsigned char *ziplistFind(unsigned char *p
, unsigned char *vstr
, unsigned int vlen
, unsigned int skip
) {
790 unsigned char vencoding
= 0;
793 while (p
[0] != ZIP_END
) {
794 unsigned int prevlensize
, encoding
, lensize
, len
;
797 ZIP_DECODE_PREVLENSIZE(p
, prevlensize
);
798 ZIP_DECODE_LENGTH(p
+ prevlensize
, encoding
, lensize
, len
);
799 q
= p
+ prevlensize
+ lensize
;
802 /* Compare current entry with specified entry */
803 if (ZIP_IS_STR(encoding
)) {
804 if (len
== vlen
&& memcmp(q
, vstr
, vlen
) == 0) {
808 /* Find out if the searched field can be encoded. Note that
809 * we do it only the first time, once done vencoding is set
810 * to non-zero and vll is set to the integer value. */
811 if (vencoding
== 0) {
812 if (!zipTryEncoding(vstr
, vlen
, &vll
, &vencoding
)) {
813 /* If the entry can't be encoded we set it to
814 * UCHAR_MAX so that we don't retry again the next
816 vencoding
= UCHAR_MAX
;
818 /* Must be non-zero by now */
822 /* Compare current entry with specified entry, do it only
823 * if vencoding != UCHAR_MAX because if there is no encoding
824 * possible for the field it can't be a valid integer. */
825 if (vencoding
!= UCHAR_MAX
) {
826 long long ll
= zipLoadInteger(q
, encoding
);
833 /* Reset skip count */
840 /* Move to next entry */
847 /* Return length of ziplist. */
848 unsigned int ziplistLen(unsigned char *zl
) {
849 unsigned int len
= 0;
850 if (intrev16ifbe(ZIPLIST_LENGTH(zl
)) < UINT16_MAX
) {
851 len
= intrev16ifbe(ZIPLIST_LENGTH(zl
));
853 unsigned char *p
= zl
+ZIPLIST_HEADER_SIZE
;
854 while (*p
!= ZIP_END
) {
855 p
+= zipRawEntryLength(p
);
859 /* Re-store length if small enough */
860 if (len
< UINT16_MAX
) ZIPLIST_LENGTH(zl
) = intrev16ifbe(len
);
865 /* Return ziplist blob size in bytes. */
866 size_t ziplistBlobLen(unsigned char *zl
) {
867 return intrev32ifbe(ZIPLIST_BYTES(zl
));
870 void ziplistRepr(unsigned char *zl
) {
878 "{tail offset %u}\n",
879 intrev32ifbe(ZIPLIST_BYTES(zl
)),
880 intrev16ifbe(ZIPLIST_LENGTH(zl
)),
881 intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
)));
882 p
= ZIPLIST_ENTRY_HEAD(zl
);
883 while(*p
!= ZIP_END
) {
898 (unsigned long) (p
-zl
),
899 entry
.headersize
+entry
.len
,
902 entry
.prevrawlensize
,
904 p
+= entry
.headersize
;
905 if (ZIP_IS_STR(entry
.encoding
)) {
906 if (entry
.len
> 40) {
907 if (fwrite(p
,40,1,stdout
) == 0) perror("fwrite");
911 fwrite(p
,entry
.len
,1,stdout
) == 0) perror("fwrite");
914 printf("%lld", (long long) zipLoadInteger(p
,entry
.encoding
));
923 #ifdef ZIPLIST_TEST_MAIN
924 #include <sys/time.h>
928 #define debug(f, ...) { if (DEBUG) printf(f, __VA_ARGS__); }
930 unsigned char *createList() {
931 unsigned char *zl
= ziplistNew();
932 zl
= ziplistPush(zl
, (unsigned char*)"foo", 3, ZIPLIST_TAIL
);
933 zl
= ziplistPush(zl
, (unsigned char*)"quux", 4, ZIPLIST_TAIL
);
934 zl
= ziplistPush(zl
, (unsigned char*)"hello", 5, ZIPLIST_HEAD
);
935 zl
= ziplistPush(zl
, (unsigned char*)"1024", 4, ZIPLIST_TAIL
);
939 unsigned char *createIntList() {
940 unsigned char *zl
= ziplistNew();
944 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
945 sprintf(buf
, "128000");
946 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
947 sprintf(buf
, "-100");
948 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_HEAD
);
949 sprintf(buf
, "4294967296");
950 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_HEAD
);
951 sprintf(buf
, "non integer");
952 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
953 sprintf(buf
, "much much longer non integer");
954 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
958 long long usec(void) {
960 gettimeofday(&tv
,NULL
);
961 return (((long long)tv
.tv_sec
)*1000000)+tv
.tv_usec
;
964 void stress(int pos
, int num
, int maxsize
, int dnum
) {
967 char posstr
[2][5] = { "HEAD", "TAIL" };
969 for (i
= 0; i
< maxsize
; i
+=dnum
) {
971 for (j
= 0; j
< i
; j
++) {
972 zl
= ziplistPush(zl
,(unsigned char*)"quux",4,ZIPLIST_TAIL
);
975 /* Do num times a push+pop from pos */
977 for (k
= 0; k
< num
; k
++) {
978 zl
= ziplistPush(zl
,(unsigned char*)"quux",4,pos
);
979 zl
= ziplistDeleteRange(zl
,0,1);
981 printf("List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\n",
982 i
,intrev32ifbe(ZIPLIST_BYTES(zl
)),num
,posstr
[pos
],usec()-start
);
987 void pop(unsigned char *zl
, int where
) {
988 unsigned char *p
, *vstr
;
992 p
= ziplistIndex(zl
,where
== ZIPLIST_HEAD
? 0 : -1);
993 if (ziplistGet(p
,&vstr
,&vlen
,&vlong
)) {
994 if (where
== ZIPLIST_HEAD
)
995 printf("Pop head: ");
997 printf("Pop tail: ");
1000 if (vlen
&& fwrite(vstr
,vlen
,1,stdout
) == 0) perror("fwrite");
1002 printf("%lld", vlong
);
1005 ziplistDeleteRange(zl
,-1,1);
1007 printf("ERROR: Could not pop\n");
1012 int randstring(char *target
, unsigned int min
, unsigned int max
) {
1013 int p
, len
= min
+rand()%(max
-min
+1);
1015 switch(rand() % 3) {
1033 target
[p
++] = minval
+rand()%(maxval
-minval
+1);
1037 int main(int argc
, char **argv
) {
1038 unsigned char *zl
, *p
;
1039 unsigned char *entry
;
1043 /* If an argument is given, use it as the random seed. */
1045 srand(atoi(argv
[1]));
1047 zl
= createIntList();
1053 pop(zl
,ZIPLIST_TAIL
);
1056 pop(zl
,ZIPLIST_HEAD
);
1059 pop(zl
,ZIPLIST_TAIL
);
1062 pop(zl
,ZIPLIST_TAIL
);
1065 printf("Get element at index 3:\n");
1068 p
= ziplistIndex(zl
, 3);
1069 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
1070 printf("ERROR: Could not access index 3\n");
1074 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1077 printf("%lld\n", value
);
1082 printf("Get element at index 4 (out of range):\n");
1085 p
= ziplistIndex(zl
, 4);
1087 printf("No entry\n");
1089 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p
-zl
);
1095 printf("Get element at index -1 (last element):\n");
1098 p
= ziplistIndex(zl
, -1);
1099 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
1100 printf("ERROR: Could not access index -1\n");
1104 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1107 printf("%lld\n", value
);
1112 printf("Get element at index -4 (first element):\n");
1115 p
= ziplistIndex(zl
, -4);
1116 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
1117 printf("ERROR: Could not access index -4\n");
1121 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1124 printf("%lld\n", value
);
1129 printf("Get element at index -5 (reverse out of range):\n");
1132 p
= ziplistIndex(zl
, -5);
1134 printf("No entry\n");
1136 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p
-zl
);
1142 printf("Iterate list from 0 to end:\n");
1145 p
= ziplistIndex(zl
, 0);
1146 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1149 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1151 printf("%lld", value
);
1153 p
= ziplistNext(zl
,p
);
1159 printf("Iterate list from 1 to end:\n");
1162 p
= ziplistIndex(zl
, 1);
1163 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1166 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1168 printf("%lld", value
);
1170 p
= ziplistNext(zl
,p
);
1176 printf("Iterate list from 2 to end:\n");
1179 p
= ziplistIndex(zl
, 2);
1180 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1183 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1185 printf("%lld", value
);
1187 p
= ziplistNext(zl
,p
);
1193 printf("Iterate starting out of range:\n");
1196 p
= ziplistIndex(zl
, 4);
1197 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
1198 printf("No entry\n");
1205 printf("Iterate from back to front:\n");
1208 p
= ziplistIndex(zl
, -1);
1209 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1212 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1214 printf("%lld", value
);
1216 p
= ziplistPrev(zl
,p
);
1222 printf("Iterate from back to front, deleting all items:\n");
1225 p
= ziplistIndex(zl
, -1);
1226 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1229 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1231 printf("%lld", value
);
1233 zl
= ziplistDelete(zl
,&p
);
1234 p
= ziplistPrev(zl
,p
);
1240 printf("Delete inclusive range 0,0:\n");
1243 zl
= ziplistDeleteRange(zl
, 0, 1);
1247 printf("Delete inclusive range 0,1:\n");
1250 zl
= ziplistDeleteRange(zl
, 0, 2);
1254 printf("Delete inclusive range 1,2:\n");
1257 zl
= ziplistDeleteRange(zl
, 1, 2);
1261 printf("Delete with start index out of range:\n");
1264 zl
= ziplistDeleteRange(zl
, 5, 1);
1268 printf("Delete with num overflow:\n");
1271 zl
= ziplistDeleteRange(zl
, 1, 5);
1275 printf("Delete foo while iterating:\n");
1278 p
= ziplistIndex(zl
,0);
1279 while (ziplistGet(p
,&entry
,&elen
,&value
)) {
1280 if (entry
&& strncmp("foo",(char*)entry
,elen
) == 0) {
1281 printf("Delete foo\n");
1282 zl
= ziplistDelete(zl
,&p
);
1286 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0)
1289 printf("%lld",value
);
1291 p
= ziplistNext(zl
,p
);
1299 printf("Regression test for >255 byte strings:\n");
1301 char v1
[257],v2
[257];
1305 zl
= ziplistPush(zl
,(unsigned char*)v1
,strlen(v1
),ZIPLIST_TAIL
);
1306 zl
= ziplistPush(zl
,(unsigned char*)v2
,strlen(v2
),ZIPLIST_TAIL
);
1308 /* Pop values again and compare their value. */
1309 p
= ziplistIndex(zl
,0);
1310 assert(ziplistGet(p
,&entry
,&elen
,&value
));
1311 assert(strncmp(v1
,(char*)entry
,elen
) == 0);
1312 p
= ziplistIndex(zl
,1);
1313 assert(ziplistGet(p
,&entry
,&elen
,&value
));
1314 assert(strncmp(v2
,(char*)entry
,elen
) == 0);
1315 printf("SUCCESS\n\n");
1318 printf("Create long list and check indices:\n");
1323 for (i
= 0; i
< 1000; i
++) {
1324 len
= sprintf(buf
,"%d",i
);
1325 zl
= ziplistPush(zl
,(unsigned char*)buf
,len
,ZIPLIST_TAIL
);
1327 for (i
= 0; i
< 1000; i
++) {
1328 p
= ziplistIndex(zl
,i
);
1329 assert(ziplistGet(p
,NULL
,NULL
,&value
));
1332 p
= ziplistIndex(zl
,-i
-1);
1333 assert(ziplistGet(p
,NULL
,NULL
,&value
));
1334 assert(999-i
== value
);
1336 printf("SUCCESS\n\n");
1339 printf("Compare strings with ziplist entries:\n");
1342 p
= ziplistIndex(zl
,0);
1343 if (!ziplistCompare(p
,(unsigned char*)"hello",5)) {
1344 printf("ERROR: not \"hello\"\n");
1347 if (ziplistCompare(p
,(unsigned char*)"hella",5)) {
1348 printf("ERROR: \"hella\"\n");
1352 p
= ziplistIndex(zl
,3);
1353 if (!ziplistCompare(p
,(unsigned char*)"1024",4)) {
1354 printf("ERROR: not \"1024\"\n");
1357 if (ziplistCompare(p
,(unsigned char*)"1025",4)) {
1358 printf("ERROR: \"1025\"\n");
1361 printf("SUCCESS\n\n");
1364 printf("Stress with random payloads of different encoding:\n");
1373 /* Hold temp vars from ziplist */
1374 unsigned char *sstr
;
1378 for (i
= 0; i
< 20000; i
++) {
1381 listSetFreeMethod(ref
,sdsfree
);
1385 for (j
= 0; j
< len
; j
++) {
1386 where
= (rand() & 1) ? ZIPLIST_HEAD
: ZIPLIST_TAIL
;
1388 buflen
= randstring(buf
,1,sizeof(buf
)-1);
1390 switch(rand() % 3) {
1392 buflen
= sprintf(buf
,"%lld",(0LL + rand()) >> 20);
1395 buflen
= sprintf(buf
,"%lld",(0LL + rand()));
1398 buflen
= sprintf(buf
,"%lld",(0LL + rand()) << 20);
1405 /* Add to ziplist */
1406 zl
= ziplistPush(zl
, (unsigned char*)buf
, buflen
, where
);
1408 /* Add to reference list */
1409 if (where
== ZIPLIST_HEAD
) {
1410 listAddNodeHead(ref
,sdsnewlen(buf
, buflen
));
1411 } else if (where
== ZIPLIST_TAIL
) {
1412 listAddNodeTail(ref
,sdsnewlen(buf
, buflen
));
1418 assert(listLength(ref
) == ziplistLen(zl
));
1419 for (j
= 0; j
< len
; j
++) {
1420 /* Naive way to get elements, but similar to the stresser
1421 * executed from the Tcl test suite. */
1422 p
= ziplistIndex(zl
,j
);
1423 refnode
= listIndex(ref
,j
);
1425 assert(ziplistGet(p
,&sstr
,&slen
,&sval
));
1427 buflen
= sprintf(buf
,"%lld",sval
);
1430 memcpy(buf
,sstr
,buflen
);
1433 assert(memcmp(buf
,listNodeValue(refnode
),buflen
) == 0);
1438 printf("SUCCESS\n\n");
1441 printf("Stress with variable ziplist size:\n");
1443 stress(ZIPLIST_HEAD
,100000,16384,256);
1444 stress(ZIPLIST_TAIL
,100000,16384,256);