]>
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.
73 * ----------------------------------------------------------------------------
75 * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>
76 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
77 * All rights reserved.
79 * Redistribution and use in source and binary forms, with or without
80 * modification, are permitted provided that the following conditions are met:
82 * * Redistributions of source code must retain the above copyright notice,
83 * this list of conditions and the following disclaimer.
84 * * Redistributions in binary form must reproduce the above copyright
85 * notice, this list of conditions and the following disclaimer in the
86 * documentation and/or other materials provided with the distribution.
87 * * Neither the name of Redis nor the names of its contributors may be used
88 * to endorse or promote products derived from this software without
89 * specific prior written permission.
91 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
92 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
94 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
95 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
96 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
97 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
98 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
99 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
100 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
101 * POSSIBILITY OF SUCH DAMAGE.
113 #include "endianconv.h"
116 #define ZIP_BIGLEN 254
118 /* Different encoding/length possibilities */
119 #define ZIP_STR_MASK 0xc0
120 #define ZIP_INT_MASK 0x30
121 #define ZIP_STR_06B (0 << 6)
122 #define ZIP_STR_14B (1 << 6)
123 #define ZIP_STR_32B (2 << 6)
124 #define ZIP_INT_16B (0xc0 | 0<<4)
125 #define ZIP_INT_32B (0xc0 | 1<<4)
126 #define ZIP_INT_64B (0xc0 | 2<<4)
127 #define ZIP_INT_24B (0xc0 | 3<<4)
128 #define ZIP_INT_8B 0xfe
129 /* 4 bit integer immediate encoding */
130 #define ZIP_INT_IMM_MASK 0x0f
131 #define ZIP_INT_IMM_MIN 0xf1 /* 11110001 */
132 #define ZIP_INT_IMM_MAX 0xfd /* 11111101 */
133 #define ZIP_INT_IMM_VAL(v) (v & ZIP_INT_IMM_MASK)
135 #define INT24_MAX 0x7fffff
136 #define INT24_MIN (-INT24_MAX - 1)
138 /* Macro to determine type */
139 #define ZIP_IS_STR(enc) (((enc) & ZIP_STR_MASK) < ZIP_STR_MASK)
142 #define ZIPLIST_BYTES(zl) (*((uint32_t*)(zl)))
143 #define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t))))
144 #define ZIPLIST_LENGTH(zl) (*((uint16_t*)((zl)+sizeof(uint32_t)*2)))
145 #define ZIPLIST_HEADER_SIZE (sizeof(uint32_t)*2+sizeof(uint16_t))
146 #define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE)
147 #define ZIPLIST_ENTRY_TAIL(zl) ((zl)+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)))
148 #define ZIPLIST_ENTRY_END(zl) ((zl)+intrev32ifbe(ZIPLIST_BYTES(zl))-1)
150 /* We know a positive increment can only be 1 because entries can only be
151 * pushed one at a time. */
152 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
153 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) \
154 ZIPLIST_LENGTH(zl) = intrev16ifbe(intrev16ifbe(ZIPLIST_LENGTH(zl))+incr); \
157 typedef struct zlentry
{
158 unsigned int prevrawlensize
, prevrawlen
;
159 unsigned int lensize
, len
;
160 unsigned int headersize
;
161 unsigned char encoding
;
165 /* Extract the encoding from the byte pointed by 'ptr' and set it into
167 #define ZIP_ENTRY_ENCODING(ptr, encoding) do { \
168 (encoding) = (ptr[0]); \
169 if ((encoding) < ZIP_STR_MASK) (encoding) &= ZIP_STR_MASK; \
172 /* Return bytes needed to store integer encoded by 'encoding' */
173 static unsigned int zipIntSize(unsigned char encoding
) {
175 case ZIP_INT_8B
: return 1;
176 case ZIP_INT_16B
: return 2;
177 case ZIP_INT_24B
: return 3;
178 case ZIP_INT_32B
: return 4;
179 case ZIP_INT_64B
: return 8;
180 default: return 0; /* 4 bit immediate */
186 /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
187 * the amount of bytes required to encode such a length. */
188 static unsigned int zipEncodeLength(unsigned char *p
, unsigned char encoding
, unsigned int rawlen
) {
189 unsigned char len
= 1, buf
[5];
191 if (ZIP_IS_STR(encoding
)) {
192 /* Although encoding is given it may not be set for strings,
193 * so we determine it here using the raw length. */
194 if (rawlen
<= 0x3f) {
196 buf
[0] = ZIP_STR_06B
| rawlen
;
197 } else if (rawlen
<= 0x3fff) {
200 buf
[0] = ZIP_STR_14B
| ((rawlen
>> 8) & 0x3f);
201 buf
[1] = rawlen
& 0xff;
205 buf
[0] = ZIP_STR_32B
;
206 buf
[1] = (rawlen
>> 24) & 0xff;
207 buf
[2] = (rawlen
>> 16) & 0xff;
208 buf
[3] = (rawlen
>> 8) & 0xff;
209 buf
[4] = rawlen
& 0xff;
212 /* Implies integer encoding, so length is always 1. */
217 /* Store this length at p */
222 /* Decode the length encoded in 'ptr'. The 'encoding' variable will hold the
223 * entries encoding, the 'lensize' variable will hold the number of bytes
224 * required to encode the entries length, and the 'len' variable will hold the
226 #define ZIP_DECODE_LENGTH(ptr, encoding, lensize, len) do { \
227 ZIP_ENTRY_ENCODING((ptr), (encoding)); \
228 if ((encoding) < ZIP_STR_MASK) { \
229 if ((encoding) == ZIP_STR_06B) { \
231 (len) = (ptr)[0] & 0x3f; \
232 } else if ((encoding) == ZIP_STR_14B) { \
234 (len) = (((ptr)[0] & 0x3f) << 8) | (ptr)[1]; \
235 } else if (encoding == ZIP_STR_32B) { \
237 (len) = ((ptr)[1] << 24) | \
246 (len) = zipIntSize(encoding); \
250 /* Encode the length of the previous entry and write it to "p". Return the
251 * number of bytes needed to encode this length if "p" is NULL. */
252 static unsigned int zipPrevEncodeLength(unsigned char *p
, unsigned int len
) {
254 return (len
< ZIP_BIGLEN
) ? 1 : sizeof(len
)+1;
256 if (len
< ZIP_BIGLEN
) {
261 memcpy(p
+1,&len
,sizeof(len
));
263 return 1+sizeof(len
);
268 /* Encode the length of the previous entry and write it to "p". This only
269 * uses the larger encoding (required in __ziplistCascadeUpdate). */
270 static void zipPrevEncodeLengthForceLarge(unsigned char *p
, unsigned int len
) {
271 if (p
== NULL
) return;
273 memcpy(p
+1,&len
,sizeof(len
));
277 /* Decode the number of bytes required to store the length of the previous
278 * element, from the perspective of the entry pointed to by 'ptr'. */
279 #define ZIP_DECODE_PREVLENSIZE(ptr, prevlensize) do { \
280 if ((ptr)[0] < ZIP_BIGLEN) { \
287 /* Decode the length of the previous element, from the perspective of the entry
288 * pointed to by 'ptr'. */
289 #define ZIP_DECODE_PREVLEN(ptr, prevlensize, prevlen) do { \
290 ZIP_DECODE_PREVLENSIZE(ptr, prevlensize); \
291 if ((prevlensize) == 1) { \
292 (prevlen) = (ptr)[0]; \
293 } else if ((prevlensize) == 5) { \
294 assert(sizeof((prevlensize)) == 4); \
295 memcpy(&(prevlen), ((char*)(ptr)) + 1, 4); \
296 memrev32ifbe(&prevlen); \
300 /* Return the difference in number of bytes needed to store the length of the
301 * previous element 'len', in the entry pointed to by 'p'. */
302 static int zipPrevLenByteDiff(unsigned char *p
, unsigned int len
) {
303 unsigned int prevlensize
;
304 ZIP_DECODE_PREVLENSIZE(p
, prevlensize
);
305 return zipPrevEncodeLength(NULL
, len
) - prevlensize
;
308 /* Return the total number of bytes used by the entry pointed to by 'p'. */
309 static unsigned int zipRawEntryLength(unsigned char *p
) {
310 unsigned int prevlensize
, encoding
, lensize
, len
;
311 ZIP_DECODE_PREVLENSIZE(p
, prevlensize
);
312 ZIP_DECODE_LENGTH(p
+ prevlensize
, encoding
, lensize
, len
);
313 return prevlensize
+ lensize
+ len
;
316 /* Check if string pointed to by 'entry' can be encoded as an integer.
317 * Stores the integer value in 'v' and its encoding in 'encoding'. */
318 static int zipTryEncoding(unsigned char *entry
, unsigned int entrylen
, long long *v
, unsigned char *encoding
) {
321 if (entrylen
>= 32 || entrylen
== 0) return 0;
322 if (string2ll((char*)entry
,entrylen
,&value
)) {
323 /* Great, the string can be encoded. Check what's the smallest
324 * of our encoding types that can hold this value. */
325 if (value
>= 0 && value
<= 12) {
326 *encoding
= ZIP_INT_IMM_MIN
+value
;
327 } else if (value
>= INT8_MIN
&& value
<= INT8_MAX
) {
328 *encoding
= ZIP_INT_8B
;
329 } else if (value
>= INT16_MIN
&& value
<= INT16_MAX
) {
330 *encoding
= ZIP_INT_16B
;
331 } else if (value
>= INT24_MIN
&& value
<= INT24_MAX
) {
332 *encoding
= ZIP_INT_24B
;
333 } else if (value
>= INT32_MIN
&& value
<= INT32_MAX
) {
334 *encoding
= ZIP_INT_32B
;
336 *encoding
= ZIP_INT_64B
;
344 /* Store integer 'value' at 'p', encoded as 'encoding' */
345 static void zipSaveInteger(unsigned char *p
, int64_t value
, unsigned char encoding
) {
349 if (encoding
== ZIP_INT_8B
) {
350 ((int8_t*)p
)[0] = (int8_t)value
;
351 } else if (encoding
== ZIP_INT_16B
) {
353 memcpy(p
,&i16
,sizeof(i16
));
355 } else if (encoding
== ZIP_INT_24B
) {
358 memcpy(p
,((uint8_t*)&i32
)+1,sizeof(i32
)-sizeof(uint8_t));
359 } else if (encoding
== ZIP_INT_32B
) {
361 memcpy(p
,&i32
,sizeof(i32
));
363 } else if (encoding
== ZIP_INT_64B
) {
365 memcpy(p
,&i64
,sizeof(i64
));
367 } else if (encoding
>= ZIP_INT_IMM_MIN
&& encoding
<= ZIP_INT_IMM_MAX
) {
368 /* Nothing to do, the value is stored in the encoding itself. */
374 /* Read integer encoded as 'encoding' from 'p' */
375 static int64_t zipLoadInteger(unsigned char *p
, unsigned char encoding
) {
378 int64_t i64
, ret
= 0;
379 if (encoding
== ZIP_INT_8B
) {
380 ret
= ((int8_t*)p
)[0];
381 } else if (encoding
== ZIP_INT_16B
) {
382 memcpy(&i16
,p
,sizeof(i16
));
385 } else if (encoding
== ZIP_INT_32B
) {
386 memcpy(&i32
,p
,sizeof(i32
));
389 } else if (encoding
== ZIP_INT_24B
) {
391 memcpy(((uint8_t*)&i32
)+1,p
,sizeof(i32
)-sizeof(uint8_t));
394 } else if (encoding
== ZIP_INT_64B
) {
395 memcpy(&i64
,p
,sizeof(i64
));
398 } else if (encoding
>= ZIP_INT_IMM_MIN
&& encoding
<= ZIP_INT_IMM_MAX
) {
399 ret
= (encoding
& ZIP_INT_IMM_MASK
)-1;
406 /* Return a struct with all information about an entry. */
407 static zlentry
zipEntry(unsigned char *p
) {
410 ZIP_DECODE_PREVLEN(p
, e
.prevrawlensize
, e
.prevrawlen
);
411 ZIP_DECODE_LENGTH(p
+ e
.prevrawlensize
, e
.encoding
, e
.lensize
, e
.len
);
412 e
.headersize
= e
.prevrawlensize
+ e
.lensize
;
417 /* Create a new empty ziplist. */
418 unsigned char *ziplistNew(void) {
419 unsigned int bytes
= ZIPLIST_HEADER_SIZE
+1;
420 unsigned char *zl
= zmalloc(bytes
);
421 ZIPLIST_BYTES(zl
) = intrev32ifbe(bytes
);
422 ZIPLIST_TAIL_OFFSET(zl
) = intrev32ifbe(ZIPLIST_HEADER_SIZE
);
423 ZIPLIST_LENGTH(zl
) = 0;
424 zl
[bytes
-1] = ZIP_END
;
428 /* Resize the ziplist. */
429 static unsigned char *ziplistResize(unsigned char *zl
, unsigned int len
) {
430 zl
= zrealloc(zl
,len
);
431 ZIPLIST_BYTES(zl
) = intrev32ifbe(len
);
436 /* When an entry is inserted, we need to set the prevlen field of the next
437 * entry to equal the length of the inserted entry. It can occur that this
438 * length cannot be encoded in 1 byte and the next entry needs to be grow
439 * a bit larger to hold the 5-byte encoded prevlen. This can be done for free,
440 * because this only happens when an entry is already being inserted (which
441 * causes a realloc and memmove). However, encoding the prevlen may require
442 * that this entry is grown as well. This effect may cascade throughout
443 * the ziplist when there are consecutive entries with a size close to
444 * ZIP_BIGLEN, so we need to check that the prevlen can be encoded in every
447 * Note that this effect can also happen in reverse, where the bytes required
448 * to encode the prevlen field can shrink. This effect is deliberately ignored,
449 * because it can cause a "flapping" effect where a chain prevlen fields is
450 * first grown and then shrunk again after consecutive inserts. Rather, the
451 * field is allowed to stay larger than necessary, because a large prevlen
452 * field implies the ziplist is holding large entries anyway.
454 * The pointer "p" points to the first entry that does NOT need to be
455 * updated, i.e. consecutive fields MAY need an update. */
456 static unsigned char *__ziplistCascadeUpdate(unsigned char *zl
, unsigned char *p
) {
457 size_t curlen
= intrev32ifbe(ZIPLIST_BYTES(zl
)), rawlen
, rawlensize
;
458 size_t offset
, noffset
, extra
;
462 while (p
[0] != ZIP_END
) {
464 rawlen
= cur
.headersize
+ cur
.len
;
465 rawlensize
= zipPrevEncodeLength(NULL
,rawlen
);
467 /* Abort if there is no next entry. */
468 if (p
[rawlen
] == ZIP_END
) break;
469 next
= zipEntry(p
+rawlen
);
471 /* Abort when "prevlen" has not changed. */
472 if (next
.prevrawlen
== rawlen
) break;
474 if (next
.prevrawlensize
< rawlensize
) {
475 /* The "prevlen" field of "next" needs more bytes to hold
476 * the raw length of "cur". */
478 extra
= rawlensize
-next
.prevrawlensize
;
479 zl
= ziplistResize(zl
,curlen
+extra
);
482 /* Current pointer and offset for next element. */
486 /* Update tail offset when next element is not the tail element. */
487 if ((zl
+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))) != np
) {
488 ZIPLIST_TAIL_OFFSET(zl
) =
489 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))+extra
);
492 /* Move the tail to the back. */
493 memmove(np
+rawlensize
,
494 np
+next
.prevrawlensize
,
495 curlen
-noffset
-next
.prevrawlensize
-1);
496 zipPrevEncodeLength(np
,rawlen
);
498 /* Advance the cursor */
502 if (next
.prevrawlensize
> rawlensize
) {
503 /* This would result in shrinking, which we want to avoid.
504 * So, set "rawlen" in the available bytes. */
505 zipPrevEncodeLengthForceLarge(p
+rawlen
,rawlen
);
507 zipPrevEncodeLength(p
+rawlen
,rawlen
);
510 /* Stop here, as the raw length of "next" has not changed. */
517 /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
518 static unsigned char *__ziplistDelete(unsigned char *zl
, unsigned char *p
, unsigned int num
) {
519 unsigned int i
, totlen
, deleted
= 0;
525 for (i
= 0; p
[0] != ZIP_END
&& i
< num
; i
++) {
526 p
+= zipRawEntryLength(p
);
532 if (p
[0] != ZIP_END
) {
533 /* Storing `prevrawlen` in this entry may increase or decrease the
534 * number of bytes required compare to the current `prevrawlen`.
535 * There always is room to store this, because it was previously
536 * stored by an entry that is now being deleted. */
537 nextdiff
= zipPrevLenByteDiff(p
,first
.prevrawlen
);
539 zipPrevEncodeLength(p
,first
.prevrawlen
);
541 /* Update offset for tail */
542 ZIPLIST_TAIL_OFFSET(zl
) =
543 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))-totlen
);
545 /* When the tail contains more than one entry, we need to take
546 * "nextdiff" in account as well. Otherwise, a change in the
547 * size of prevlen doesn't have an effect on the *tail* offset. */
549 if (p
[tail
.headersize
+tail
.len
] != ZIP_END
) {
550 ZIPLIST_TAIL_OFFSET(zl
) =
551 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))+nextdiff
);
554 /* Move tail to the front of the ziplist */
556 intrev32ifbe(ZIPLIST_BYTES(zl
))-(p
-zl
)-1);
558 /* The entire tail was deleted. No need to move memory. */
559 ZIPLIST_TAIL_OFFSET(zl
) =
560 intrev32ifbe((first
.p
-zl
)-first
.prevrawlen
);
563 /* Resize and update length */
565 zl
= ziplistResize(zl
, intrev32ifbe(ZIPLIST_BYTES(zl
))-totlen
+nextdiff
);
566 ZIPLIST_INCR_LENGTH(zl
,-deleted
);
569 /* When nextdiff != 0, the raw length of the next entry has changed, so
570 * we need to cascade the update throughout the ziplist */
572 zl
= __ziplistCascadeUpdate(zl
,p
);
577 /* Insert item at "p". */
578 static unsigned char *__ziplistInsert(unsigned char *zl
, unsigned char *p
, unsigned char *s
, unsigned int slen
) {
579 size_t curlen
= intrev32ifbe(ZIPLIST_BYTES(zl
)), reqlen
, prevlen
= 0;
582 unsigned char encoding
= 0;
583 long long value
= 123456789; /* initialized to avoid warning. Using a value
584 that is easy to see if for some reason
585 we use it uninitialized. */
588 /* Find out prevlen for the entry that is inserted. */
589 if (p
[0] != ZIP_END
) {
591 prevlen
= entry
.prevrawlen
;
593 unsigned char *ptail
= ZIPLIST_ENTRY_TAIL(zl
);
594 if (ptail
[0] != ZIP_END
) {
595 prevlen
= zipRawEntryLength(ptail
);
599 /* See if the entry can be encoded */
600 if (zipTryEncoding(s
,slen
,&value
,&encoding
)) {
601 /* 'encoding' is set to the appropriate integer encoding */
602 reqlen
= zipIntSize(encoding
);
604 /* 'encoding' is untouched, however zipEncodeLength will use the
605 * string length to figure out how to encode it. */
608 /* We need space for both the length of the previous entry and
609 * the length of the payload. */
610 reqlen
+= zipPrevEncodeLength(NULL
,prevlen
);
611 reqlen
+= zipEncodeLength(NULL
,encoding
,slen
);
613 /* When the insert position is not equal to the tail, we need to
614 * make sure that the next entry can hold this entry's length in
615 * its prevlen field. */
616 nextdiff
= (p
[0] != ZIP_END
) ? zipPrevLenByteDiff(p
,reqlen
) : 0;
618 /* Store offset because a realloc may change the address of zl. */
620 zl
= ziplistResize(zl
,curlen
+reqlen
+nextdiff
);
623 /* Apply memory move when necessary and update tail offset. */
624 if (p
[0] != ZIP_END
) {
625 /* Subtract one because of the ZIP_END bytes */
626 memmove(p
+reqlen
,p
-nextdiff
,curlen
-offset
-1+nextdiff
);
628 /* Encode this entry's raw length in the next entry. */
629 zipPrevEncodeLength(p
+reqlen
,reqlen
);
631 /* Update offset for tail */
632 ZIPLIST_TAIL_OFFSET(zl
) =
633 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))+reqlen
);
635 /* When the tail contains more than one entry, we need to take
636 * "nextdiff" in account as well. Otherwise, a change in the
637 * size of prevlen doesn't have an effect on the *tail* offset. */
638 tail
= zipEntry(p
+reqlen
);
639 if (p
[reqlen
+tail
.headersize
+tail
.len
] != ZIP_END
) {
640 ZIPLIST_TAIL_OFFSET(zl
) =
641 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
))+nextdiff
);
644 /* This element will be the new tail. */
645 ZIPLIST_TAIL_OFFSET(zl
) = intrev32ifbe(p
-zl
);
648 /* When nextdiff != 0, the raw length of the next entry has changed, so
649 * we need to cascade the update throughout the ziplist */
652 zl
= __ziplistCascadeUpdate(zl
,p
+reqlen
);
656 /* Write the entry */
657 p
+= zipPrevEncodeLength(p
,prevlen
);
658 p
+= zipEncodeLength(p
,encoding
,slen
);
659 if (ZIP_IS_STR(encoding
)) {
662 zipSaveInteger(p
,value
,encoding
);
664 ZIPLIST_INCR_LENGTH(zl
,1);
668 unsigned char *ziplistPush(unsigned char *zl
, unsigned char *s
, unsigned int slen
, int where
) {
670 p
= (where
== ZIPLIST_HEAD
) ? ZIPLIST_ENTRY_HEAD(zl
) : ZIPLIST_ENTRY_END(zl
);
671 return __ziplistInsert(zl
,p
,s
,slen
);
674 /* Returns an offset to use for iterating with ziplistNext. When the given
675 * index is negative, the list is traversed back to front. When the list
676 * doesn't contain an element at the provided index, NULL is returned. */
677 unsigned char *ziplistIndex(unsigned char *zl
, int index
) {
682 p
= ZIPLIST_ENTRY_TAIL(zl
);
683 if (p
[0] != ZIP_END
) {
685 while (entry
.prevrawlen
> 0 && index
--) {
686 p
-= entry
.prevrawlen
;
691 p
= ZIPLIST_ENTRY_HEAD(zl
);
692 while (p
[0] != ZIP_END
&& index
--) {
693 p
+= zipRawEntryLength(p
);
696 return (p
[0] == ZIP_END
|| index
> 0) ? NULL
: p
;
699 /* Return pointer to next entry in ziplist.
701 * zl is the pointer to the ziplist
702 * p is the pointer to the current element
704 * The element after 'p' is returned, otherwise NULL if we are at the end. */
705 unsigned char *ziplistNext(unsigned char *zl
, unsigned char *p
) {
708 /* "p" could be equal to ZIP_END, caused by ziplistDelete,
709 * and we should return NULL. Otherwise, we should return NULL
710 * when the *next* element is ZIP_END (there is no next entry). */
711 if (p
[0] == ZIP_END
) {
715 p
+= zipRawEntryLength(p
);
716 if (p
[0] == ZIP_END
) {
723 /* Return pointer to previous entry in ziplist. */
724 unsigned char *ziplistPrev(unsigned char *zl
, unsigned char *p
) {
727 /* Iterating backwards from ZIP_END should return the tail. When "p" is
728 * equal to the first element of the list, we're already at the head,
729 * and should return NULL. */
730 if (p
[0] == ZIP_END
) {
731 p
= ZIPLIST_ENTRY_TAIL(zl
);
732 return (p
[0] == ZIP_END
) ? NULL
: p
;
733 } else if (p
== ZIPLIST_ENTRY_HEAD(zl
)) {
737 assert(entry
.prevrawlen
> 0);
738 return p
-entry
.prevrawlen
;
742 /* Get entry pointer to by 'p' and store in either 'e' or 'v' depending
743 * on the encoding of the entry. 'e' is always set to NULL to be able
744 * to find out whether the string pointer or the integer value was set.
745 * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */
746 unsigned int ziplistGet(unsigned char *p
, unsigned char **sstr
, unsigned int *slen
, long long *sval
) {
748 if (p
== NULL
|| p
[0] == ZIP_END
) return 0;
749 if (sstr
) *sstr
= NULL
;
752 if (ZIP_IS_STR(entry
.encoding
)) {
755 *sstr
= p
+entry
.headersize
;
759 *sval
= zipLoadInteger(p
+entry
.headersize
,entry
.encoding
);
765 /* Insert an entry at "p". */
766 unsigned char *ziplistInsert(unsigned char *zl
, unsigned char *p
, unsigned char *s
, unsigned int slen
) {
767 return __ziplistInsert(zl
,p
,s
,slen
);
770 /* Delete a single entry from the ziplist, pointed to by *p.
771 * Also update *p in place, to be able to iterate over the
772 * ziplist, while deleting entries. */
773 unsigned char *ziplistDelete(unsigned char *zl
, unsigned char **p
) {
774 size_t offset
= *p
-zl
;
775 zl
= __ziplistDelete(zl
,*p
,1);
777 /* Store pointer to current element in p, because ziplistDelete will
778 * do a realloc which might result in a different "zl"-pointer.
779 * When the delete direction is back to front, we might delete the last
780 * entry and end up with "p" pointing to ZIP_END, so check this. */
785 /* Delete a range of entries from the ziplist. */
786 unsigned char *ziplistDeleteRange(unsigned char *zl
, unsigned int index
, unsigned int num
) {
787 unsigned char *p
= ziplistIndex(zl
,index
);
788 return (p
== NULL
) ? zl
: __ziplistDelete(zl
,p
,num
);
791 /* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
792 unsigned int ziplistCompare(unsigned char *p
, unsigned char *sstr
, unsigned int slen
) {
794 unsigned char sencoding
;
795 long long zval
, sval
;
796 if (p
[0] == ZIP_END
) return 0;
799 if (ZIP_IS_STR(entry
.encoding
)) {
801 if (entry
.len
== slen
) {
802 return memcmp(p
+entry
.headersize
,sstr
,slen
) == 0;
807 /* Try to compare encoded values. Don't compare encoding because
808 * different implementations may encoded integers differently. */
809 if (zipTryEncoding(sstr
,slen
,&sval
,&sencoding
)) {
810 zval
= zipLoadInteger(p
+entry
.headersize
,entry
.encoding
);
817 /* Find pointer to the entry equal to the specified entry. Skip 'skip' entries
818 * between every comparison. Returns NULL when the field could not be found. */
819 unsigned char *ziplistFind(unsigned char *p
, unsigned char *vstr
, unsigned int vlen
, unsigned int skip
) {
821 unsigned char vencoding
= 0;
824 while (p
[0] != ZIP_END
) {
825 unsigned int prevlensize
, encoding
, lensize
, len
;
828 ZIP_DECODE_PREVLENSIZE(p
, prevlensize
);
829 ZIP_DECODE_LENGTH(p
+ prevlensize
, encoding
, lensize
, len
);
830 q
= p
+ prevlensize
+ lensize
;
833 /* Compare current entry with specified entry */
834 if (ZIP_IS_STR(encoding
)) {
835 if (len
== vlen
&& memcmp(q
, vstr
, vlen
) == 0) {
839 /* Find out if the searched field can be encoded. Note that
840 * we do it only the first time, once done vencoding is set
841 * to non-zero and vll is set to the integer value. */
842 if (vencoding
== 0) {
843 if (!zipTryEncoding(vstr
, vlen
, &vll
, &vencoding
)) {
844 /* If the entry can't be encoded we set it to
845 * UCHAR_MAX so that we don't retry again the next
847 vencoding
= UCHAR_MAX
;
849 /* Must be non-zero by now */
853 /* Compare current entry with specified entry, do it only
854 * if vencoding != UCHAR_MAX because if there is no encoding
855 * possible for the field it can't be a valid integer. */
856 if (vencoding
!= UCHAR_MAX
) {
857 long long ll
= zipLoadInteger(q
, encoding
);
864 /* Reset skip count */
871 /* Move to next entry */
878 /* Return length of ziplist. */
879 unsigned int ziplistLen(unsigned char *zl
) {
880 unsigned int len
= 0;
881 if (intrev16ifbe(ZIPLIST_LENGTH(zl
)) < UINT16_MAX
) {
882 len
= intrev16ifbe(ZIPLIST_LENGTH(zl
));
884 unsigned char *p
= zl
+ZIPLIST_HEADER_SIZE
;
885 while (*p
!= ZIP_END
) {
886 p
+= zipRawEntryLength(p
);
890 /* Re-store length if small enough */
891 if (len
< UINT16_MAX
) ZIPLIST_LENGTH(zl
) = intrev16ifbe(len
);
896 /* Return ziplist blob size in bytes. */
897 size_t ziplistBlobLen(unsigned char *zl
) {
898 return intrev32ifbe(ZIPLIST_BYTES(zl
));
901 void ziplistRepr(unsigned char *zl
) {
909 "{tail offset %u}\n",
910 intrev32ifbe(ZIPLIST_BYTES(zl
)),
911 intrev16ifbe(ZIPLIST_LENGTH(zl
)),
912 intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl
)));
913 p
= ZIPLIST_ENTRY_HEAD(zl
);
914 while(*p
!= ZIP_END
) {
929 (unsigned long) (p
-zl
),
930 entry
.headersize
+entry
.len
,
933 entry
.prevrawlensize
,
935 p
+= entry
.headersize
;
936 if (ZIP_IS_STR(entry
.encoding
)) {
937 if (entry
.len
> 40) {
938 if (fwrite(p
,40,1,stdout
) == 0) perror("fwrite");
942 fwrite(p
,entry
.len
,1,stdout
) == 0) perror("fwrite");
945 printf("%lld", (long long) zipLoadInteger(p
,entry
.encoding
));
954 #ifdef ZIPLIST_TEST_MAIN
955 #include <sys/time.h>
959 #define debug(f, ...) { if (DEBUG) printf(f, __VA_ARGS__); }
961 unsigned char *createList() {
962 unsigned char *zl
= ziplistNew();
963 zl
= ziplistPush(zl
, (unsigned char*)"foo", 3, ZIPLIST_TAIL
);
964 zl
= ziplistPush(zl
, (unsigned char*)"quux", 4, ZIPLIST_TAIL
);
965 zl
= ziplistPush(zl
, (unsigned char*)"hello", 5, ZIPLIST_HEAD
);
966 zl
= ziplistPush(zl
, (unsigned char*)"1024", 4, ZIPLIST_TAIL
);
970 unsigned char *createIntList() {
971 unsigned char *zl
= ziplistNew();
975 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
976 sprintf(buf
, "128000");
977 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
978 sprintf(buf
, "-100");
979 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_HEAD
);
980 sprintf(buf
, "4294967296");
981 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_HEAD
);
982 sprintf(buf
, "non integer");
983 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
984 sprintf(buf
, "much much longer non integer");
985 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
989 long long usec(void) {
991 gettimeofday(&tv
,NULL
);
992 return (((long long)tv
.tv_sec
)*1000000)+tv
.tv_usec
;
995 void stress(int pos
, int num
, int maxsize
, int dnum
) {
998 char posstr
[2][5] = { "HEAD", "TAIL" };
1000 for (i
= 0; i
< maxsize
; i
+=dnum
) {
1002 for (j
= 0; j
< i
; j
++) {
1003 zl
= ziplistPush(zl
,(unsigned char*)"quux",4,ZIPLIST_TAIL
);
1006 /* Do num times a push+pop from pos */
1008 for (k
= 0; k
< num
; k
++) {
1009 zl
= ziplistPush(zl
,(unsigned char*)"quux",4,pos
);
1010 zl
= ziplistDeleteRange(zl
,0,1);
1012 printf("List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\n",
1013 i
,intrev32ifbe(ZIPLIST_BYTES(zl
)),num
,posstr
[pos
],usec()-start
);
1018 void pop(unsigned char *zl
, int where
) {
1019 unsigned char *p
, *vstr
;
1023 p
= ziplistIndex(zl
,where
== ZIPLIST_HEAD
? 0 : -1);
1024 if (ziplistGet(p
,&vstr
,&vlen
,&vlong
)) {
1025 if (where
== ZIPLIST_HEAD
)
1026 printf("Pop head: ");
1028 printf("Pop tail: ");
1031 if (vlen
&& fwrite(vstr
,vlen
,1,stdout
) == 0) perror("fwrite");
1033 printf("%lld", vlong
);
1036 ziplistDeleteRange(zl
,-1,1);
1038 printf("ERROR: Could not pop\n");
1043 int randstring(char *target
, unsigned int min
, unsigned int max
) {
1044 int p
, len
= min
+rand()%(max
-min
+1);
1046 switch(rand() % 3) {
1064 target
[p
++] = minval
+rand()%(maxval
-minval
+1);
1068 void verify(unsigned char *zl
, zlentry
*e
) {
1070 int len
= ziplistLen(zl
);
1073 for (i
= 0; i
< len
; i
++) {
1074 memset(&e
[i
], 0, sizeof(zlentry
));
1075 e
[i
] = zipEntry(ziplistIndex(zl
, i
));
1077 memset(&_e
, 0, sizeof(zlentry
));
1078 _e
= zipEntry(ziplistIndex(zl
, -len
+i
));
1080 assert(memcmp(&e
[i
], &_e
, sizeof(zlentry
)) == 0);
1084 int main(int argc
, char **argv
) {
1085 unsigned char *zl
, *p
;
1086 unsigned char *entry
;
1090 /* If an argument is given, use it as the random seed. */
1092 srand(atoi(argv
[1]));
1094 zl
= createIntList();
1100 pop(zl
,ZIPLIST_TAIL
);
1103 pop(zl
,ZIPLIST_HEAD
);
1106 pop(zl
,ZIPLIST_TAIL
);
1109 pop(zl
,ZIPLIST_TAIL
);
1112 printf("Get element at index 3:\n");
1115 p
= ziplistIndex(zl
, 3);
1116 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
1117 printf("ERROR: Could not access index 3\n");
1121 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1124 printf("%lld\n", value
);
1129 printf("Get element at index 4 (out of range):\n");
1132 p
= ziplistIndex(zl
, 4);
1134 printf("No entry\n");
1136 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p
-zl
);
1142 printf("Get element at index -1 (last element):\n");
1145 p
= ziplistIndex(zl
, -1);
1146 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
1147 printf("ERROR: Could not access index -1\n");
1151 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1154 printf("%lld\n", value
);
1159 printf("Get element at index -4 (first element):\n");
1162 p
= ziplistIndex(zl
, -4);
1163 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
1164 printf("ERROR: Could not access index -4\n");
1168 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1171 printf("%lld\n", value
);
1176 printf("Get element at index -5 (reverse out of range):\n");
1179 p
= ziplistIndex(zl
, -5);
1181 printf("No entry\n");
1183 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p
-zl
);
1189 printf("Iterate list from 0 to end:\n");
1192 p
= ziplistIndex(zl
, 0);
1193 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1196 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1198 printf("%lld", value
);
1200 p
= ziplistNext(zl
,p
);
1206 printf("Iterate list from 1 to end:\n");
1209 p
= ziplistIndex(zl
, 1);
1210 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1213 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1215 printf("%lld", value
);
1217 p
= ziplistNext(zl
,p
);
1223 printf("Iterate list from 2 to end:\n");
1226 p
= ziplistIndex(zl
, 2);
1227 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1230 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1232 printf("%lld", value
);
1234 p
= ziplistNext(zl
,p
);
1240 printf("Iterate starting out of range:\n");
1243 p
= ziplistIndex(zl
, 4);
1244 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
1245 printf("No entry\n");
1252 printf("Iterate from back to front:\n");
1255 p
= ziplistIndex(zl
, -1);
1256 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1259 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1261 printf("%lld", value
);
1263 p
= ziplistPrev(zl
,p
);
1269 printf("Iterate from back to front, deleting all items:\n");
1272 p
= ziplistIndex(zl
, -1);
1273 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
1276 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0) perror("fwrite");
1278 printf("%lld", value
);
1280 zl
= ziplistDelete(zl
,&p
);
1281 p
= ziplistPrev(zl
,p
);
1287 printf("Delete inclusive range 0,0:\n");
1290 zl
= ziplistDeleteRange(zl
, 0, 1);
1294 printf("Delete inclusive range 0,1:\n");
1297 zl
= ziplistDeleteRange(zl
, 0, 2);
1301 printf("Delete inclusive range 1,2:\n");
1304 zl
= ziplistDeleteRange(zl
, 1, 2);
1308 printf("Delete with start index out of range:\n");
1311 zl
= ziplistDeleteRange(zl
, 5, 1);
1315 printf("Delete with num overflow:\n");
1318 zl
= ziplistDeleteRange(zl
, 1, 5);
1322 printf("Delete foo while iterating:\n");
1325 p
= ziplistIndex(zl
,0);
1326 while (ziplistGet(p
,&entry
,&elen
,&value
)) {
1327 if (entry
&& strncmp("foo",(char*)entry
,elen
) == 0) {
1328 printf("Delete foo\n");
1329 zl
= ziplistDelete(zl
,&p
);
1333 if (elen
&& fwrite(entry
,elen
,1,stdout
) == 0)
1336 printf("%lld",value
);
1338 p
= ziplistNext(zl
,p
);
1346 printf("Regression test for >255 byte strings:\n");
1348 char v1
[257],v2
[257];
1352 zl
= ziplistPush(zl
,(unsigned char*)v1
,strlen(v1
),ZIPLIST_TAIL
);
1353 zl
= ziplistPush(zl
,(unsigned char*)v2
,strlen(v2
),ZIPLIST_TAIL
);
1355 /* Pop values again and compare their value. */
1356 p
= ziplistIndex(zl
,0);
1357 assert(ziplistGet(p
,&entry
,&elen
,&value
));
1358 assert(strncmp(v1
,(char*)entry
,elen
) == 0);
1359 p
= ziplistIndex(zl
,1);
1360 assert(ziplistGet(p
,&entry
,&elen
,&value
));
1361 assert(strncmp(v2
,(char*)entry
,elen
) == 0);
1362 printf("SUCCESS\n\n");
1365 printf("Regression test deleting next to last entries:\n");
1371 for (i
= 0; i
< (sizeof(v
)/sizeof(v
[0])); i
++) {
1372 memset(v
[i
], 'a' + i
, sizeof(v
[0]));
1380 for (i
= 0; i
< (sizeof(v
)/sizeof(v
[0])); i
++) {
1381 zl
= ziplistPush(zl
, (unsigned char *) v
[i
], strlen(v
[i
]), ZIPLIST_TAIL
);
1386 assert(e
[0].prevrawlensize
== 1);
1387 assert(e
[1].prevrawlensize
== 5);
1388 assert(e
[2].prevrawlensize
== 1);
1390 /* Deleting entry 1 will increase `prevrawlensize` for entry 2 */
1391 unsigned char *p
= e
[1].p
;
1392 zl
= ziplistDelete(zl
, &p
);
1396 assert(e
[0].prevrawlensize
== 1);
1397 assert(e
[1].prevrawlensize
== 5);
1399 printf("SUCCESS\n\n");
1402 printf("Create long list and check indices:\n");
1407 for (i
= 0; i
< 1000; i
++) {
1408 len
= sprintf(buf
,"%d",i
);
1409 zl
= ziplistPush(zl
,(unsigned char*)buf
,len
,ZIPLIST_TAIL
);
1411 for (i
= 0; i
< 1000; i
++) {
1412 p
= ziplistIndex(zl
,i
);
1413 assert(ziplistGet(p
,NULL
,NULL
,&value
));
1416 p
= ziplistIndex(zl
,-i
-1);
1417 assert(ziplistGet(p
,NULL
,NULL
,&value
));
1418 assert(999-i
== value
);
1420 printf("SUCCESS\n\n");
1423 printf("Compare strings with ziplist entries:\n");
1426 p
= ziplistIndex(zl
,0);
1427 if (!ziplistCompare(p
,(unsigned char*)"hello",5)) {
1428 printf("ERROR: not \"hello\"\n");
1431 if (ziplistCompare(p
,(unsigned char*)"hella",5)) {
1432 printf("ERROR: \"hella\"\n");
1436 p
= ziplistIndex(zl
,3);
1437 if (!ziplistCompare(p
,(unsigned char*)"1024",4)) {
1438 printf("ERROR: not \"1024\"\n");
1441 if (ziplistCompare(p
,(unsigned char*)"1025",4)) {
1442 printf("ERROR: \"1025\"\n");
1445 printf("SUCCESS\n\n");
1448 printf("Stress with random payloads of different encoding:\n");
1457 /* Hold temp vars from ziplist */
1458 unsigned char *sstr
;
1462 for (i
= 0; i
< 20000; i
++) {
1465 listSetFreeMethod(ref
,sdsfree
);
1469 for (j
= 0; j
< len
; j
++) {
1470 where
= (rand() & 1) ? ZIPLIST_HEAD
: ZIPLIST_TAIL
;
1472 buflen
= randstring(buf
,1,sizeof(buf
)-1);
1474 switch(rand() % 3) {
1476 buflen
= sprintf(buf
,"%lld",(0LL + rand()) >> 20);
1479 buflen
= sprintf(buf
,"%lld",(0LL + rand()));
1482 buflen
= sprintf(buf
,"%lld",(0LL + rand()) << 20);
1489 /* Add to ziplist */
1490 zl
= ziplistPush(zl
, (unsigned char*)buf
, buflen
, where
);
1492 /* Add to reference list */
1493 if (where
== ZIPLIST_HEAD
) {
1494 listAddNodeHead(ref
,sdsnewlen(buf
, buflen
));
1495 } else if (where
== ZIPLIST_TAIL
) {
1496 listAddNodeTail(ref
,sdsnewlen(buf
, buflen
));
1502 assert(listLength(ref
) == ziplistLen(zl
));
1503 for (j
= 0; j
< len
; j
++) {
1504 /* Naive way to get elements, but similar to the stresser
1505 * executed from the Tcl test suite. */
1506 p
= ziplistIndex(zl
,j
);
1507 refnode
= listIndex(ref
,j
);
1509 assert(ziplistGet(p
,&sstr
,&slen
,&sval
));
1511 buflen
= sprintf(buf
,"%lld",sval
);
1514 memcpy(buf
,sstr
,buflen
);
1517 assert(memcmp(buf
,listNodeValue(refnode
),buflen
) == 0);
1522 printf("SUCCESS\n\n");
1525 printf("Stress with variable ziplist size:\n");
1527 stress(ZIPLIST_HEAD
,100000,16384,256);
1528 stress(ZIPLIST_TAIL
,100000,16384,256);