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.
8 *
9 * ----------------------------------------------------------------------------
10 *
11 * ZIPLIST OVERALL LAYOUT:
12 * The general layout of the ziplist is as follows:
13 * <zlbytes><zltail><zllen><entry><entry><zlend>
14 *
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.
18 *
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.
21 *
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.
24 *
25 * <zlend> is a single byte special value, equal to 255, which indicates the
26 * end of the list.
27 *
28 * ZIPLIST ENTRIES:
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.
33 *
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.
40 *
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:
48 *
49 * |00pppppp| - 1 byte
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.
55 * |11000000| - 1 byte
56 * Integer encoded as int16_t (2 bytes).
57 * |11010000| - 1 byte
58 * Integer encoded as int32_t (4 bytes).
59 * |11100000| - 1 byte
60 * Integer encoded as int64_t (8 bytes).
61 * |11110000| - 1 byte
62 * Integer encoded as 24 bit signed (3 bytes).
63 * |11111110| - 1 byte
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.
70 *
71 * All the integers are represented in little endian byte order.
72 *
73 * ----------------------------------------------------------------------------
74 *
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.
78 *
79 * Redistribution and use in source and binary forms, with or without
80 * modification, are permitted provided that the following conditions are met:
81 *
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.
90 *
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.
102 */
103
104 #include <stdio.h>
105 #include <stdlib.h>
106 #include <string.h>
107 #include <stdint.h>
108 #include <limits.h>
109 #include "zmalloc.h"
110 #include "util.h"
111 #include "ziplist.h"
112 #include "endianconv.h"
113 #include "redisassert.h"
114
115 #define ZIP_END 255
116 #define ZIP_BIGLEN 254
117
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)
134
135 #define INT24_MAX 0x7fffff
136 #define INT24_MIN (-INT24_MAX - 1)
137
138 /* Macro to determine type */
139 #define ZIP_IS_STR(enc) (((enc) & ZIP_STR_MASK) < ZIP_STR_MASK)
140
141 /* Utility macros */
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_END_SIZE (sizeof(uint8_t))
147 #define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE)
148 #define ZIPLIST_ENTRY_TAIL(zl) ((zl)+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)))
149 #define ZIPLIST_ENTRY_END(zl) ((zl)+intrev32ifbe(ZIPLIST_BYTES(zl))-1)
150
151 /* We know a positive increment can only be 1 because entries can only be
152 * pushed one at a time. */
153 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
154 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) \
155 ZIPLIST_LENGTH(zl) = intrev16ifbe(intrev16ifbe(ZIPLIST_LENGTH(zl))+incr); \
156 }
157
158 typedef struct zlentry {
159 unsigned int prevrawlensize, prevrawlen;
160 unsigned int lensize, len;
161 unsigned int headersize;
162 unsigned char encoding;
163 unsigned char *p;
164 } zlentry;
165
166 #define ZIPLIST_ENTRY_ZERO(zle) { \
167 (zle)->prevrawlensize = (zle)->prevrawlen = 0; \
168 (zle)->lensize = (zle)->len = (zle)->headersize = 0; \
169 (zle)->encoding = 0; \
170 (zle)->p = NULL; \
171 }
172
173 /* Extract the encoding from the byte pointed by 'ptr' and set it into
174 * 'encoding'. */
175 #define ZIP_ENTRY_ENCODING(ptr, encoding) do { \
176 (encoding) = (ptr[0]); \
177 if ((encoding) < ZIP_STR_MASK) (encoding) &= ZIP_STR_MASK; \
178 } while(0)
179
180 void ziplistRepr(unsigned char *zl);
181
182 /* Return bytes needed to store integer encoded by 'encoding' */
zipIntSize(unsigned char encoding)183 static unsigned int zipIntSize(unsigned char encoding) {
184 switch(encoding) {
185 case ZIP_INT_8B: return 1;
186 case ZIP_INT_16B: return 2;
187 case ZIP_INT_24B: return 3;
188 case ZIP_INT_32B: return 4;
189 case ZIP_INT_64B: return 8;
190 default: return 0; /* 4 bit immediate */
191 }
192 assert(NULL);
193 return 0;
194 }
195
196 /* Encode the length 'rawlen' writing it in 'p'. If p is NULL it just returns
197 * the amount of bytes required to encode such a length. */
zipEncodeLength(unsigned char * p,unsigned char encoding,unsigned int rawlen)198 static unsigned int zipEncodeLength(unsigned char *p, unsigned char encoding, unsigned int rawlen) {
199 unsigned char len = 1, buf[5];
200
201 if (ZIP_IS_STR(encoding)) {
202 /* Although encoding is given it may not be set for strings,
203 * so we determine it here using the raw length. */
204 if (rawlen <= 0x3f) {
205 if (!p) return len;
206 buf[0] = ZIP_STR_06B | rawlen;
207 } else if (rawlen <= 0x3fff) {
208 len += 1;
209 if (!p) return len;
210 buf[0] = ZIP_STR_14B | ((rawlen >> 8) & 0x3f);
211 buf[1] = rawlen & 0xff;
212 } else {
213 len += 4;
214 if (!p) return len;
215 buf[0] = ZIP_STR_32B;
216 buf[1] = (rawlen >> 24) & 0xff;
217 buf[2] = (rawlen >> 16) & 0xff;
218 buf[3] = (rawlen >> 8) & 0xff;
219 buf[4] = rawlen & 0xff;
220 }
221 } else {
222 /* Implies integer encoding, so length is always 1. */
223 if (!p) return len;
224 buf[0] = encoding;
225 }
226
227 /* Store this length at p */
228 memcpy(p,buf,len);
229 return len;
230 }
231
232 /* Decode the length encoded in 'ptr'. The 'encoding' variable will hold the
233 * entries encoding, the 'lensize' variable will hold the number of bytes
234 * required to encode the entries length, and the 'len' variable will hold the
235 * entries length. */
236 #define ZIP_DECODE_LENGTH(ptr, encoding, lensize, len) do { \
237 ZIP_ENTRY_ENCODING((ptr), (encoding)); \
238 if ((encoding) < ZIP_STR_MASK) { \
239 if ((encoding) == ZIP_STR_06B) { \
240 (lensize) = 1; \
241 (len) = (ptr)[0] & 0x3f; \
242 } else if ((encoding) == ZIP_STR_14B) { \
243 (lensize) = 2; \
244 (len) = (((ptr)[0] & 0x3f) << 8) | (ptr)[1]; \
245 } else if (encoding == ZIP_STR_32B) { \
246 (lensize) = 5; \
247 (len) = ((ptr)[1] << 24) | \
248 ((ptr)[2] << 16) | \
249 ((ptr)[3] << 8) | \
250 ((ptr)[4]); \
251 } else { \
252 assert(NULL); \
253 } \
254 } else { \
255 (lensize) = 1; \
256 (len) = zipIntSize(encoding); \
257 } \
258 } while(0);
259
260 /* Encode the length of the previous entry and write it to "p". Return the
261 * number of bytes needed to encode this length if "p" is NULL. */
zipPrevEncodeLength(unsigned char * p,unsigned int len)262 static unsigned int zipPrevEncodeLength(unsigned char *p, unsigned int len) {
263 if (p == NULL) {
264 return (len < ZIP_BIGLEN) ? 1 : sizeof(len)+1;
265 } else {
266 if (len < ZIP_BIGLEN) {
267 p[0] = len;
268 return 1;
269 } else {
270 p[0] = ZIP_BIGLEN;
271 memcpy(p+1,&len,sizeof(len));
272 memrev32ifbe(p+1);
273 return 1+sizeof(len);
274 }
275 }
276 }
277
278 /* Encode the length of the previous entry and write it to "p". This only
279 * uses the larger encoding (required in __ziplistCascadeUpdate). */
zipPrevEncodeLengthForceLarge(unsigned char * p,unsigned int len)280 static void zipPrevEncodeLengthForceLarge(unsigned char *p, unsigned int len) {
281 if (p == NULL) return;
282 p[0] = ZIP_BIGLEN;
283 memcpy(p+1,&len,sizeof(len));
284 memrev32ifbe(p+1);
285 }
286
287 /* Decode the number of bytes required to store the length of the previous
288 * element, from the perspective of the entry pointed to by 'ptr'. */
289 #define ZIP_DECODE_PREVLENSIZE(ptr, prevlensize) do { \
290 if ((ptr)[0] < ZIP_BIGLEN) { \
291 (prevlensize) = 1; \
292 } else { \
293 (prevlensize) = 5; \
294 } \
295 } while(0);
296
297 /* Decode the length of the previous element, from the perspective of the entry
298 * pointed to by 'ptr'. */
299 #define ZIP_DECODE_PREVLEN(ptr, prevlensize, prevlen) do { \
300 ZIP_DECODE_PREVLENSIZE(ptr, prevlensize); \
301 if ((prevlensize) == 1) { \
302 (prevlen) = (ptr)[0]; \
303 } else if ((prevlensize) == 5) { \
304 assert(sizeof((prevlensize)) == 4); \
305 memcpy(&(prevlen), ((char*)(ptr)) + 1, 4); \
306 memrev32ifbe(&prevlen); \
307 } \
308 } while(0);
309
310 /* Return the difference in number of bytes needed to store the length of the
311 * previous element 'len', in the entry pointed to by 'p'. */
zipPrevLenByteDiff(unsigned char * p,unsigned int len)312 static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) {
313 unsigned int prevlensize;
314 ZIP_DECODE_PREVLENSIZE(p, prevlensize);
315 return zipPrevEncodeLength(NULL, len) - prevlensize;
316 }
317
318 /* Return the total number of bytes used by the entry pointed to by 'p'. */
zipRawEntryLength(unsigned char * p)319 static unsigned int zipRawEntryLength(unsigned char *p) {
320 unsigned int prevlensize, encoding, lensize, len;
321 ZIP_DECODE_PREVLENSIZE(p, prevlensize);
322 ZIP_DECODE_LENGTH(p + prevlensize, encoding, lensize, len);
323 return prevlensize + lensize + len;
324 }
325
326 /* Check if string pointed to by 'entry' can be encoded as an integer.
327 * Stores the integer value in 'v' and its encoding in 'encoding'. */
zipTryEncoding(unsigned char * entry,unsigned int entrylen,long long * v,unsigned char * encoding)328 static int zipTryEncoding(unsigned char *entry, unsigned int entrylen, long long *v, unsigned char *encoding) {
329 long long value;
330
331 if (entrylen >= 32 || entrylen == 0) return 0;
332 if (string2ll((char*)entry,entrylen,&value)) {
333 /* Great, the string can be encoded. Check what's the smallest
334 * of our encoding types that can hold this value. */
335 if (value >= 0 && value <= 12) {
336 *encoding = ZIP_INT_IMM_MIN+value;
337 } else if (value >= INT8_MIN && value <= INT8_MAX) {
338 *encoding = ZIP_INT_8B;
339 } else if (value >= INT16_MIN && value <= INT16_MAX) {
340 *encoding = ZIP_INT_16B;
341 } else if (value >= INT24_MIN && value <= INT24_MAX) {
342 *encoding = ZIP_INT_24B;
343 } else if (value >= INT32_MIN && value <= INT32_MAX) {
344 *encoding = ZIP_INT_32B;
345 } else {
346 *encoding = ZIP_INT_64B;
347 }
348 *v = value;
349 return 1;
350 }
351 return 0;
352 }
353
354 /* Store integer 'value' at 'p', encoded as 'encoding' */
zipSaveInteger(unsigned char * p,int64_t value,unsigned char encoding)355 static void zipSaveInteger(unsigned char *p, int64_t value, unsigned char encoding) {
356 int16_t i16;
357 int32_t i32;
358 int64_t i64;
359 if (encoding == ZIP_INT_8B) {
360 ((int8_t*)p)[0] = (int8_t)value;
361 } else if (encoding == ZIP_INT_16B) {
362 i16 = value;
363 memcpy(p,&i16,sizeof(i16));
364 memrev16ifbe(p);
365 } else if (encoding == ZIP_INT_24B) {
366 i32 = value<<8;
367 memrev32ifbe(&i32);
368 memcpy(p,((uint8_t*)&i32)+1,sizeof(i32)-sizeof(uint8_t));
369 } else if (encoding == ZIP_INT_32B) {
370 i32 = value;
371 memcpy(p,&i32,sizeof(i32));
372 memrev32ifbe(p);
373 } else if (encoding == ZIP_INT_64B) {
374 i64 = value;
375 memcpy(p,&i64,sizeof(i64));
376 memrev64ifbe(p);
377 } else if (encoding >= ZIP_INT_IMM_MIN && encoding <= ZIP_INT_IMM_MAX) {
378 /* Nothing to do, the value is stored in the encoding itself. */
379 } else {
380 assert(NULL);
381 }
382 }
383
384 /* Read integer encoded as 'encoding' from 'p' */
zipLoadInteger(unsigned char * p,unsigned char encoding)385 static int64_t zipLoadInteger(unsigned char *p, unsigned char encoding) {
386 int16_t i16;
387 int32_t i32;
388 int64_t i64, ret = 0;
389 if (encoding == ZIP_INT_8B) {
390 ret = ((int8_t*)p)[0];
391 } else if (encoding == ZIP_INT_16B) {
392 memcpy(&i16,p,sizeof(i16));
393 memrev16ifbe(&i16);
394 ret = i16;
395 } else if (encoding == ZIP_INT_32B) {
396 memcpy(&i32,p,sizeof(i32));
397 memrev32ifbe(&i32);
398 ret = i32;
399 } else if (encoding == ZIP_INT_24B) {
400 i32 = 0;
401 memcpy(((uint8_t*)&i32)+1,p,sizeof(i32)-sizeof(uint8_t));
402 memrev32ifbe(&i32);
403 ret = i32>>8;
404 } else if (encoding == ZIP_INT_64B) {
405 memcpy(&i64,p,sizeof(i64));
406 memrev64ifbe(&i64);
407 ret = i64;
408 } else if (encoding >= ZIP_INT_IMM_MIN && encoding <= ZIP_INT_IMM_MAX) {
409 ret = (encoding & ZIP_INT_IMM_MASK)-1;
410 } else {
411 assert(NULL);
412 }
413 return ret;
414 }
415
416 /* Return a struct with all information about an entry. */
zipEntry(unsigned char * p,zlentry * e)417 static void zipEntry(unsigned char *p, zlentry *e) {
418
419 ZIP_DECODE_PREVLEN(p, e->prevrawlensize, e->prevrawlen);
420 ZIP_DECODE_LENGTH(p + e->prevrawlensize, e->encoding, e->lensize, e->len);
421 e->headersize = e->prevrawlensize + e->lensize;
422 e->p = p;
423 }
424
425 /* Create a new empty ziplist. */
ziplistNew(void)426 unsigned char *ziplistNew(void) {
427 unsigned int bytes = ZIPLIST_HEADER_SIZE+1;
428 unsigned char *zl = zmalloc(bytes);
429 ZIPLIST_BYTES(zl) = intrev32ifbe(bytes);
430 ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(ZIPLIST_HEADER_SIZE);
431 ZIPLIST_LENGTH(zl) = 0;
432 zl[bytes-1] = ZIP_END;
433 return zl;
434 }
435
436 /* Resize the ziplist. */
ziplistResize(unsigned char * zl,unsigned int len)437 static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
438 zl = zrealloc(zl,len);
439 ZIPLIST_BYTES(zl) = intrev32ifbe(len);
440 zl[len-1] = ZIP_END;
441 return zl;
442 }
443
444 /* When an entry is inserted, we need to set the prevlen field of the next
445 * entry to equal the length of the inserted entry. It can occur that this
446 * length cannot be encoded in 1 byte and the next entry needs to be grow
447 * a bit larger to hold the 5-byte encoded prevlen. This can be done for free,
448 * because this only happens when an entry is already being inserted (which
449 * causes a realloc and memmove). However, encoding the prevlen may require
450 * that this entry is grown as well. This effect may cascade throughout
451 * the ziplist when there are consecutive entries with a size close to
452 * ZIP_BIGLEN, so we need to check that the prevlen can be encoded in every
453 * consecutive entry.
454 *
455 * Note that this effect can also happen in reverse, where the bytes required
456 * to encode the prevlen field can shrink. This effect is deliberately ignored,
457 * because it can cause a "flapping" effect where a chain prevlen fields is
458 * first grown and then shrunk again after consecutive inserts. Rather, the
459 * field is allowed to stay larger than necessary, because a large prevlen
460 * field implies the ziplist is holding large entries anyway.
461 *
462 * The pointer "p" points to the first entry that does NOT need to be
463 * updated, i.e. consecutive fields MAY need an update. */
__ziplistCascadeUpdate(unsigned char * zl,unsigned char * p)464 static unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p) {
465 size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), rawlen, rawlensize;
466 size_t offset, noffset, extra;
467 unsigned char *np;
468 zlentry cur, next;
469
470 while (p[0] != ZIP_END) {
471 zipEntry(p, &cur);
472 rawlen = cur.headersize + cur.len;
473 rawlensize = zipPrevEncodeLength(NULL,rawlen);
474
475 /* Abort if there is no next entry. */
476 if (p[rawlen] == ZIP_END) break;
477 zipEntry(p+rawlen, &next);
478
479 /* Abort when "prevlen" has not changed. */
480 if (next.prevrawlen == rawlen) break;
481
482 if (next.prevrawlensize < rawlensize) {
483 /* The "prevlen" field of "next" needs more bytes to hold
484 * the raw length of "cur". */
485 offset = p-zl;
486 extra = rawlensize-next.prevrawlensize;
487 zl = ziplistResize(zl,curlen+extra);
488 p = zl+offset;
489
490 /* Current pointer and offset for next element. */
491 np = p+rawlen;
492 noffset = np-zl;
493
494 /* Update tail offset when next element is not the tail element. */
495 if ((zl+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))) != np) {
496 ZIPLIST_TAIL_OFFSET(zl) =
497 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+extra);
498 }
499
500 /* Move the tail to the back. */
501 memmove(np+rawlensize,
502 np+next.prevrawlensize,
503 curlen-noffset-next.prevrawlensize-1);
504 zipPrevEncodeLength(np,rawlen);
505
506 /* Advance the cursor */
507 p += rawlen;
508 curlen += extra;
509 } else {
510 if (next.prevrawlensize > rawlensize) {
511 /* This would result in shrinking, which we want to avoid.
512 * So, set "rawlen" in the available bytes. */
513 zipPrevEncodeLengthForceLarge(p+rawlen,rawlen);
514 } else {
515 zipPrevEncodeLength(p+rawlen,rawlen);
516 }
517
518 /* Stop here, as the raw length of "next" has not changed. */
519 break;
520 }
521 }
522 return zl;
523 }
524
525 /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
__ziplistDelete(unsigned char * zl,unsigned char * p,unsigned int num)526 static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsigned int num) {
527 unsigned int i, totlen, deleted = 0;
528 size_t offset;
529 int nextdiff = 0;
530 zlentry first, tail;
531
532 zipEntry(p, &first);
533 for (i = 0; p[0] != ZIP_END && i < num; i++) {
534 p += zipRawEntryLength(p);
535 deleted++;
536 }
537
538 totlen = p-first.p;
539 if (totlen > 0) {
540 if (p[0] != ZIP_END) {
541 /* Storing `prevrawlen` in this entry may increase or decrease the
542 * number of bytes required compare to the current `prevrawlen`.
543 * There always is room to store this, because it was previously
544 * stored by an entry that is now being deleted. */
545 nextdiff = zipPrevLenByteDiff(p,first.prevrawlen);
546 p -= nextdiff;
547 zipPrevEncodeLength(p,first.prevrawlen);
548
549 /* Update offset for tail */
550 ZIPLIST_TAIL_OFFSET(zl) =
551 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))-totlen);
552
553 /* When the tail contains more than one entry, we need to take
554 * "nextdiff" in account as well. Otherwise, a change in the
555 * size of prevlen doesn't have an effect on the *tail* offset. */
556 zipEntry(p, &tail);
557 if (p[tail.headersize+tail.len] != ZIP_END) {
558 ZIPLIST_TAIL_OFFSET(zl) =
559 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+nextdiff);
560 }
561
562 /* Move tail to the front of the ziplist */
563 memmove(first.p,p,
564 intrev32ifbe(ZIPLIST_BYTES(zl))-(p-zl)-1);
565 } else {
566 /* The entire tail was deleted. No need to move memory. */
567 ZIPLIST_TAIL_OFFSET(zl) =
568 intrev32ifbe((first.p-zl)-first.prevrawlen);
569 }
570
571 /* Resize and update length */
572 offset = first.p-zl;
573 zl = ziplistResize(zl, intrev32ifbe(ZIPLIST_BYTES(zl))-totlen+nextdiff);
574 ZIPLIST_INCR_LENGTH(zl,-deleted);
575 p = zl+offset;
576
577 /* When nextdiff != 0, the raw length of the next entry has changed, so
578 * we need to cascade the update throughout the ziplist */
579 if (nextdiff != 0)
580 zl = __ziplistCascadeUpdate(zl,p);
581 }
582 return zl;
583 }
584
585 /* Insert item at "p". */
__ziplistInsert(unsigned char * zl,unsigned char * p,unsigned char * s,unsigned int slen)586 static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
587 size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), reqlen;
588 unsigned int prevlensize, prevlen = 0;
589 size_t offset;
590 int nextdiff = 0;
591 unsigned char encoding = 0;
592 long long value = 123456789; /* initialized to avoid warning. Using a value
593 that is easy to see if for some reason
594 we use it uninitialized. */
595 zlentry tail;
596
597 /* Find out prevlen for the entry that is inserted. */
598 if (p[0] != ZIP_END) {
599 ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
600 } else {
601 unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl);
602 if (ptail[0] != ZIP_END) {
603 prevlen = zipRawEntryLength(ptail);
604 }
605 }
606
607 /* See if the entry can be encoded */
608 if (zipTryEncoding(s,slen,&value,&encoding)) {
609 /* 'encoding' is set to the appropriate integer encoding */
610 reqlen = zipIntSize(encoding);
611 } else {
612 /* 'encoding' is untouched, however zipEncodeLength will use the
613 * string length to figure out how to encode it. */
614 reqlen = slen;
615 }
616 /* We need space for both the length of the previous entry and
617 * the length of the payload. */
618 reqlen += zipPrevEncodeLength(NULL,prevlen);
619 reqlen += zipEncodeLength(NULL,encoding,slen);
620
621 /* When the insert position is not equal to the tail, we need to
622 * make sure that the next entry can hold this entry's length in
623 * its prevlen field. */
624 nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;
625
626 /* Store offset because a realloc may change the address of zl. */
627 offset = p-zl;
628 zl = ziplistResize(zl,curlen+reqlen+nextdiff);
629 p = zl+offset;
630
631 /* Apply memory move when necessary and update tail offset. */
632 if (p[0] != ZIP_END) {
633 /* Subtract one because of the ZIP_END bytes */
634 memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);
635
636 /* Encode this entry's raw length in the next entry. */
637 zipPrevEncodeLength(p+reqlen,reqlen);
638
639 /* Update offset for tail */
640 ZIPLIST_TAIL_OFFSET(zl) =
641 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+reqlen);
642
643 /* When the tail contains more than one entry, we need to take
644 * "nextdiff" in account as well. Otherwise, a change in the
645 * size of prevlen doesn't have an effect on the *tail* offset. */
646 zipEntry(p+reqlen, &tail);
647 if (p[reqlen+tail.headersize+tail.len] != ZIP_END) {
648 ZIPLIST_TAIL_OFFSET(zl) =
649 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+nextdiff);
650 }
651 } else {
652 /* This element will be the new tail. */
653 ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(p-zl);
654 }
655
656 /* When nextdiff != 0, the raw length of the next entry has changed, so
657 * we need to cascade the update throughout the ziplist */
658 if (nextdiff != 0) {
659 offset = p-zl;
660 zl = __ziplistCascadeUpdate(zl,p+reqlen);
661 p = zl+offset;
662 }
663
664 /* Write the entry */
665 p += zipPrevEncodeLength(p,prevlen);
666 p += zipEncodeLength(p,encoding,slen);
667 if (ZIP_IS_STR(encoding)) {
668 memcpy(p,s,slen);
669 } else {
670 zipSaveInteger(p,value,encoding);
671 }
672 ZIPLIST_INCR_LENGTH(zl,1);
673 return zl;
674 }
675
676 /* Merge ziplists 'first' and 'second' by appending 'second' to 'first'.
677 *
678 * NOTE: The larger ziplist is reallocated to contain the new merged ziplist.
679 * Either 'first' or 'second' can be used for the result. The parameter not
680 * used will be free'd and set to NULL.
681 *
682 * After calling this function, the input parameters are no longer valid since
683 * they are changed and free'd in-place.
684 *
685 * The result ziplist is the contents of 'first' followed by 'second'.
686 *
687 * On failure: returns NULL if the merge is impossible.
688 * On success: returns the merged ziplist (which is expanded version of either
689 * 'first' or 'second', also frees the other unused input ziplist, and sets the
690 * input ziplist argument equal to newly reallocated ziplist return value. */
ziplistMerge(unsigned char ** first,unsigned char ** second)691 unsigned char *ziplistMerge(unsigned char **first, unsigned char **second) {
692 /* If any params are null, we can't merge, so NULL. */
693 if (first == NULL || *first == NULL || second == NULL || *second == NULL)
694 return NULL;
695
696 /* Can't merge same list into itself. */
697 if (*first == *second)
698 return NULL;
699
700 size_t first_bytes = intrev32ifbe(ZIPLIST_BYTES(*first));
701 size_t first_len = intrev16ifbe(ZIPLIST_LENGTH(*first));
702
703 size_t second_bytes = intrev32ifbe(ZIPLIST_BYTES(*second));
704 size_t second_len = intrev16ifbe(ZIPLIST_LENGTH(*second));
705
706 int append;
707 unsigned char *source, *target;
708 size_t target_bytes, source_bytes;
709 /* Pick the largest ziplist so we can resize easily in-place.
710 * We must also track if we are now appending or prepending to
711 * the target ziplist. */
712 if (first_len >= second_len) {
713 /* retain first, append second to first. */
714 target = *first;
715 target_bytes = first_bytes;
716 source = *second;
717 source_bytes = second_bytes;
718 append = 1;
719 } else {
720 /* else, retain second, prepend first to second. */
721 target = *second;
722 target_bytes = second_bytes;
723 source = *first;
724 source_bytes = first_bytes;
725 append = 0;
726 }
727
728 /* Calculate final bytes (subtract one pair of metadata) */
729 size_t zlbytes = first_bytes + second_bytes -
730 ZIPLIST_HEADER_SIZE - ZIPLIST_END_SIZE;
731 size_t zllength = first_len + second_len;
732
733 /* Combined zl length should be limited within UINT16_MAX */
734 zllength = zllength < UINT16_MAX ? zllength : UINT16_MAX;
735
736 /* Save offset positions before we start ripping memory apart. */
737 size_t first_offset = intrev32ifbe(ZIPLIST_TAIL_OFFSET(*first));
738 size_t second_offset = intrev32ifbe(ZIPLIST_TAIL_OFFSET(*second));
739
740 /* Extend target to new zlbytes then append or prepend source. */
741 target = zrealloc(target, zlbytes);
742 if (append) {
743 /* append == appending to target */
744 /* Copy source after target (copying over original [END]):
745 * [TARGET - END, SOURCE - HEADER] */
746 memcpy(target + target_bytes - ZIPLIST_END_SIZE,
747 source + ZIPLIST_HEADER_SIZE,
748 source_bytes - ZIPLIST_HEADER_SIZE);
749 } else {
750 /* !append == prepending to target */
751 /* Move target *contents* exactly size of (source - [END]),
752 * then copy source into vacataed space (source - [END]):
753 * [SOURCE - END, TARGET - HEADER] */
754 memmove(target + source_bytes - ZIPLIST_END_SIZE,
755 target + ZIPLIST_HEADER_SIZE,
756 target_bytes - ZIPLIST_HEADER_SIZE);
757 memcpy(target, source, source_bytes - ZIPLIST_END_SIZE);
758 }
759
760 /* Update header metadata. */
761 ZIPLIST_BYTES(target) = intrev32ifbe(zlbytes);
762 ZIPLIST_LENGTH(target) = intrev16ifbe(zllength);
763 /* New tail offset is:
764 * + N bytes of first ziplist
765 * - 1 byte for [END] of first ziplist
766 * + M bytes for the offset of the original tail of the second ziplist
767 * - J bytes for HEADER because second_offset keeps no header. */
768 ZIPLIST_TAIL_OFFSET(target) = intrev32ifbe(
769 (first_bytes - ZIPLIST_END_SIZE) +
770 (second_offset - ZIPLIST_HEADER_SIZE));
771
772 /* __ziplistCascadeUpdate just fixes the prev length values until it finds a
773 * correct prev length value (then it assumes the rest of the list is okay).
774 * We tell CascadeUpdate to start at the first ziplist's tail element to fix
775 * the merge seam. */
776 target = __ziplistCascadeUpdate(target, target+first_offset);
777
778 /* Now free and NULL out what we didn't realloc */
779 if (append) {
780 zfree(*second);
781 *second = NULL;
782 *first = target;
783 } else {
784 zfree(*first);
785 *first = NULL;
786 *second = target;
787 }
788 return target;
789 }
790
ziplistPush(unsigned char * zl,unsigned char * s,unsigned int slen,int where)791 unsigned char *ziplistPush(unsigned char *zl, unsigned char *s, unsigned int slen, int where) {
792 unsigned char *p;
793 p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_END(zl);
794 return __ziplistInsert(zl,p,s,slen);
795 }
796
797 /* Returns an offset to use for iterating with ziplistNext. When the given
798 * index is negative, the list is traversed back to front. When the list
799 * doesn't contain an element at the provided index, NULL is returned. */
ziplistIndex(unsigned char * zl,int index)800 unsigned char *ziplistIndex(unsigned char *zl, int index) {
801 unsigned char *p;
802 unsigned int prevlensize, prevlen = 0;
803 if (index < 0) {
804 index = (-index)-1;
805 p = ZIPLIST_ENTRY_TAIL(zl);
806 if (p[0] != ZIP_END) {
807 ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
808 while (prevlen > 0 && index--) {
809 p -= prevlen;
810 ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
811 }
812 }
813 } else {
814 p = ZIPLIST_ENTRY_HEAD(zl);
815 while (p[0] != ZIP_END && index--) {
816 p += zipRawEntryLength(p);
817 }
818 }
819 return (p[0] == ZIP_END || index > 0) ? NULL : p;
820 }
821
822 /* Return pointer to next entry in ziplist.
823 *
824 * zl is the pointer to the ziplist
825 * p is the pointer to the current element
826 *
827 * The element after 'p' is returned, otherwise NULL if we are at the end. */
ziplistNext(unsigned char * zl,unsigned char * p)828 unsigned char *ziplistNext(unsigned char *zl, unsigned char *p) {
829 ((void) zl);
830
831 /* "p" could be equal to ZIP_END, caused by ziplistDelete,
832 * and we should return NULL. Otherwise, we should return NULL
833 * when the *next* element is ZIP_END (there is no next entry). */
834 if (p[0] == ZIP_END) {
835 return NULL;
836 }
837
838 p += zipRawEntryLength(p);
839 if (p[0] == ZIP_END) {
840 return NULL;
841 }
842
843 return p;
844 }
845
846 /* Return pointer to previous entry in ziplist. */
ziplistPrev(unsigned char * zl,unsigned char * p)847 unsigned char *ziplistPrev(unsigned char *zl, unsigned char *p) {
848 unsigned int prevlensize, prevlen = 0;
849
850 /* Iterating backwards from ZIP_END should return the tail. When "p" is
851 * equal to the first element of the list, we're already at the head,
852 * and should return NULL. */
853 if (p[0] == ZIP_END) {
854 p = ZIPLIST_ENTRY_TAIL(zl);
855 return (p[0] == ZIP_END) ? NULL : p;
856 } else if (p == ZIPLIST_ENTRY_HEAD(zl)) {
857 return NULL;
858 } else {
859 ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
860 assert(prevlen > 0);
861 return p-prevlen;
862 }
863 }
864
865 /* Get entry pointed to by 'p' and store in either '*sstr' or 'sval' depending
866 * on the encoding of the entry. '*sstr' is always set to NULL to be able
867 * to find out whether the string pointer or the integer value was set.
868 * Return 0 if 'p' points to the end of the ziplist, 1 otherwise. */
ziplistGet(unsigned char * p,unsigned char ** sstr,unsigned int * slen,long long * sval)869 unsigned int ziplistGet(unsigned char *p, unsigned char **sstr, unsigned int *slen, long long *sval) {
870 zlentry entry;
871 if (p == NULL || p[0] == ZIP_END) return 0;
872 if (sstr) *sstr = NULL;
873
874 zipEntry(p, &entry);
875 if (ZIP_IS_STR(entry.encoding)) {
876 if (sstr) {
877 *slen = entry.len;
878 *sstr = p+entry.headersize;
879 }
880 } else {
881 if (sval) {
882 *sval = zipLoadInteger(p+entry.headersize,entry.encoding);
883 }
884 }
885 return 1;
886 }
887
888 /* Insert an entry at "p". */
ziplistInsert(unsigned char * zl,unsigned char * p,unsigned char * s,unsigned int slen)889 unsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
890 return __ziplistInsert(zl,p,s,slen);
891 }
892
893 /* Delete a single entry from the ziplist, pointed to by *p.
894 * Also update *p in place, to be able to iterate over the
895 * ziplist, while deleting entries. */
ziplistDelete(unsigned char * zl,unsigned char ** p)896 unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
897 size_t offset = *p-zl;
898 zl = __ziplistDelete(zl,*p,1);
899
900 /* Store pointer to current element in p, because ziplistDelete will
901 * do a realloc which might result in a different "zl"-pointer.
902 * When the delete direction is back to front, we might delete the last
903 * entry and end up with "p" pointing to ZIP_END, so check this. */
904 *p = zl+offset;
905 return zl;
906 }
907
908 /* Delete a range of entries from the ziplist. */
ziplistDeleteRange(unsigned char * zl,int index,unsigned int num)909 unsigned char *ziplistDeleteRange(unsigned char *zl, int index, unsigned int num) {
910 unsigned char *p = ziplistIndex(zl,index);
911 return (p == NULL) ? zl : __ziplistDelete(zl,p,num);
912 }
913
914 /* Compare entry pointer to by 'p' with 'sstr' of length 'slen'. */
915 /* Return 1 if equal. */
ziplistCompare(unsigned char * p,unsigned char * sstr,unsigned int slen)916 unsigned int ziplistCompare(unsigned char *p, unsigned char *sstr, unsigned int slen) {
917 zlentry entry;
918 unsigned char sencoding;
919 long long zval, sval;
920 if (p[0] == ZIP_END) return 0;
921
922 zipEntry(p, &entry);
923 if (ZIP_IS_STR(entry.encoding)) {
924 /* Raw compare */
925 if (entry.len == slen) {
926 return memcmp(p+entry.headersize,sstr,slen) == 0;
927 } else {
928 return 0;
929 }
930 } else {
931 /* Try to compare encoded values. Don't compare encoding because
932 * different implementations may encoded integers differently. */
933 if (zipTryEncoding(sstr,slen,&sval,&sencoding)) {
934 zval = zipLoadInteger(p+entry.headersize,entry.encoding);
935 return zval == sval;
936 }
937 }
938 return 0;
939 }
940
941 /* Find pointer to the entry equal to the specified entry. Skip 'skip' entries
942 * between every comparison. Returns NULL when the field could not be found. */
ziplistFind(unsigned char * p,unsigned char * vstr,unsigned int vlen,unsigned int skip)943 unsigned char *ziplistFind(unsigned char *p, unsigned char *vstr, unsigned int vlen, unsigned int skip) {
944 int skipcnt = 0;
945 unsigned char vencoding = 0;
946 long long vll = 0;
947
948 while (p[0] != ZIP_END) {
949 unsigned int prevlensize, encoding, lensize, len;
950 unsigned char *q;
951
952 ZIP_DECODE_PREVLENSIZE(p, prevlensize);
953 ZIP_DECODE_LENGTH(p + prevlensize, encoding, lensize, len);
954 q = p + prevlensize + lensize;
955
956 if (skipcnt == 0) {
957 /* Compare current entry with specified entry */
958 if (ZIP_IS_STR(encoding)) {
959 if (len == vlen && memcmp(q, vstr, vlen) == 0) {
960 return p;
961 }
962 } else {
963 /* Find out if the searched field can be encoded. Note that
964 * we do it only the first time, once done vencoding is set
965 * to non-zero and vll is set to the integer value. */
966 if (vencoding == 0) {
967 if (!zipTryEncoding(vstr, vlen, &vll, &vencoding)) {
968 /* If the entry can't be encoded we set it to
969 * UCHAR_MAX so that we don't retry again the next
970 * time. */
971 vencoding = UCHAR_MAX;
972 }
973 /* Must be non-zero by now */
974 assert(vencoding);
975 }
976
977 /* Compare current entry with specified entry, do it only
978 * if vencoding != UCHAR_MAX because if there is no encoding
979 * possible for the field it can't be a valid integer. */
980 if (vencoding != UCHAR_MAX) {
981 long long ll = zipLoadInteger(q, encoding);
982 if (ll == vll) {
983 return p;
984 }
985 }
986 }
987
988 /* Reset skip count */
989 skipcnt = skip;
990 } else {
991 /* Skip entry */
992 skipcnt--;
993 }
994
995 /* Move to next entry */
996 p = q + len;
997 }
998
999 return NULL;
1000 }
1001
1002 /* Return length of ziplist. */
ziplistLen(unsigned char * zl)1003 unsigned int ziplistLen(unsigned char *zl) {
1004 unsigned int len = 0;
1005 if (intrev16ifbe(ZIPLIST_LENGTH(zl)) < UINT16_MAX) {
1006 len = intrev16ifbe(ZIPLIST_LENGTH(zl));
1007 } else {
1008 unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
1009 while (*p != ZIP_END) {
1010 p += zipRawEntryLength(p);
1011 len++;
1012 }
1013
1014 /* Re-store length if small enough */
1015 if (len < UINT16_MAX) ZIPLIST_LENGTH(zl) = intrev16ifbe(len);
1016 }
1017 return len;
1018 }
1019
1020 /* Return ziplist blob size in bytes. */
ziplistBlobLen(unsigned char * zl)1021 size_t ziplistBlobLen(unsigned char *zl) {
1022 return intrev32ifbe(ZIPLIST_BYTES(zl));
1023 }
1024
ziplistRepr(unsigned char * zl)1025 void ziplistRepr(unsigned char *zl) {
1026 unsigned char *p;
1027 int index = 0;
1028 zlentry entry;
1029
1030 printf(
1031 "{total bytes %d} "
1032 "{length %u}\n"
1033 "{tail offset %u}\n",
1034 intrev32ifbe(ZIPLIST_BYTES(zl)),
1035 intrev16ifbe(ZIPLIST_LENGTH(zl)),
1036 intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)));
1037 p = ZIPLIST_ENTRY_HEAD(zl);
1038 while(*p != ZIP_END) {
1039 zipEntry(p, &entry);
1040 printf(
1041 "{"
1042 "addr 0x%08lx, "
1043 "index %2d, "
1044 "offset %5ld, "
1045 "rl: %5u, "
1046 "hs %2u, "
1047 "pl: %5u, "
1048 "pls: %2u, "
1049 "payload %5u"
1050 "} ",
1051 (long unsigned)p,
1052 index,
1053 (unsigned long) (p-zl),
1054 entry.headersize+entry.len,
1055 entry.headersize,
1056 entry.prevrawlen,
1057 entry.prevrawlensize,
1058 entry.len);
1059 p += entry.headersize;
1060 if (ZIP_IS_STR(entry.encoding)) {
1061 if (entry.len > 40) {
1062 if (fwrite(p,40,1,stdout) == 0) perror("fwrite");
1063 printf("...");
1064 } else {
1065 if (entry.len &&
1066 fwrite(p,entry.len,1,stdout) == 0) perror("fwrite");
1067 }
1068 } else {
1069 printf("%lld", (long long) zipLoadInteger(p,entry.encoding));
1070 }
1071 printf("\n");
1072 p += entry.len;
1073 index++;
1074 }
1075 printf("{end}\n\n");
1076 }
1077
1078 #ifdef REDIS_TEST
1079 #include <sys/time.h>
1080 #include "adlist.h"
1081 #include "sds.h"
1082
1083 #define debug(f, ...) { if (DEBUG) printf(f, __VA_ARGS__); }
1084
createList()1085 static unsigned char *createList() {
1086 unsigned char *zl = ziplistNew();
1087 zl = ziplistPush(zl, (unsigned char*)"foo", 3, ZIPLIST_TAIL);
1088 zl = ziplistPush(zl, (unsigned char*)"quux", 4, ZIPLIST_TAIL);
1089 zl = ziplistPush(zl, (unsigned char*)"hello", 5, ZIPLIST_HEAD);
1090 zl = ziplistPush(zl, (unsigned char*)"1024", 4, ZIPLIST_TAIL);
1091 return zl;
1092 }
1093
createIntList()1094 static unsigned char *createIntList() {
1095 unsigned char *zl = ziplistNew();
1096 char buf[32];
1097
1098 sprintf(buf, "100");
1099 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
1100 sprintf(buf, "128000");
1101 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
1102 sprintf(buf, "-100");
1103 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
1104 sprintf(buf, "4294967296");
1105 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
1106 sprintf(buf, "non integer");
1107 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
1108 sprintf(buf, "much much longer non integer");
1109 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
1110 return zl;
1111 }
1112
usec(void)1113 static long long usec(void) {
1114 struct timeval tv;
1115 gettimeofday(&tv,NULL);
1116 return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
1117 }
1118
stress(int pos,int num,int maxsize,int dnum)1119 static void stress(int pos, int num, int maxsize, int dnum) {
1120 int i,j,k;
1121 unsigned char *zl;
1122 char posstr[2][5] = { "HEAD", "TAIL" };
1123 long long start;
1124 for (i = 0; i < maxsize; i+=dnum) {
1125 zl = ziplistNew();
1126 for (j = 0; j < i; j++) {
1127 zl = ziplistPush(zl,(unsigned char*)"quux",4,ZIPLIST_TAIL);
1128 }
1129
1130 /* Do num times a push+pop from pos */
1131 start = usec();
1132 for (k = 0; k < num; k++) {
1133 zl = ziplistPush(zl,(unsigned char*)"quux",4,pos);
1134 zl = ziplistDeleteRange(zl,0,1);
1135 }
1136 printf("List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\n",
1137 i,intrev32ifbe(ZIPLIST_BYTES(zl)),num,posstr[pos],usec()-start);
1138 zfree(zl);
1139 }
1140 }
1141
pop(unsigned char * zl,int where)1142 static unsigned char *pop(unsigned char *zl, int where) {
1143 unsigned char *p, *vstr;
1144 unsigned int vlen;
1145 long long vlong;
1146
1147 p = ziplistIndex(zl,where == ZIPLIST_HEAD ? 0 : -1);
1148 if (ziplistGet(p,&vstr,&vlen,&vlong)) {
1149 if (where == ZIPLIST_HEAD)
1150 printf("Pop head: ");
1151 else
1152 printf("Pop tail: ");
1153
1154 if (vstr) {
1155 if (vlen && fwrite(vstr,vlen,1,stdout) == 0) perror("fwrite");
1156 }
1157 else {
1158 printf("%lld", vlong);
1159 }
1160
1161 printf("\n");
1162 return ziplistDelete(zl,&p);
1163 } else {
1164 printf("ERROR: Could not pop\n");
1165 exit(1);
1166 }
1167 }
1168
randstring(char * target,unsigned int min,unsigned int max)1169 static int randstring(char *target, unsigned int min, unsigned int max) {
1170 int p = 0;
1171 int len = min+rand()%(max-min+1);
1172 int minval, maxval;
1173 switch(rand() % 3) {
1174 case 0:
1175 minval = 0;
1176 maxval = 255;
1177 break;
1178 case 1:
1179 minval = 48;
1180 maxval = 122;
1181 break;
1182 case 2:
1183 minval = 48;
1184 maxval = 52;
1185 break;
1186 default:
1187 assert(NULL);
1188 }
1189
1190 while(p < len)
1191 target[p++] = minval+rand()%(maxval-minval+1);
1192 return len;
1193 }
1194
verify(unsigned char * zl,zlentry * e)1195 static void verify(unsigned char *zl, zlentry *e) {
1196 int len = ziplistLen(zl);
1197 zlentry _e;
1198
1199 ZIPLIST_ENTRY_ZERO(&_e);
1200
1201 for (int i = 0; i < len; i++) {
1202 memset(&e[i], 0, sizeof(zlentry));
1203 zipEntry(ziplistIndex(zl, i), &e[i]);
1204
1205 memset(&_e, 0, sizeof(zlentry));
1206 zipEntry(ziplistIndex(zl, -len+i), &_e);
1207
1208 assert(memcmp(&e[i], &_e, sizeof(zlentry)) == 0);
1209 }
1210 }
1211
ziplistTest(int argc,char ** argv)1212 int ziplistTest(int argc, char **argv) {
1213 unsigned char *zl, *p;
1214 unsigned char *entry;
1215 unsigned int elen;
1216 long long value;
1217
1218 /* If an argument is given, use it as the random seed. */
1219 if (argc == 2)
1220 srand(atoi(argv[1]));
1221
1222 zl = createIntList();
1223 ziplistRepr(zl);
1224
1225 zfree(zl);
1226
1227 zl = createList();
1228 ziplistRepr(zl);
1229
1230 zl = pop(zl,ZIPLIST_TAIL);
1231 ziplistRepr(zl);
1232
1233 zl = pop(zl,ZIPLIST_HEAD);
1234 ziplistRepr(zl);
1235
1236 zl = pop(zl,ZIPLIST_TAIL);
1237 ziplistRepr(zl);
1238
1239 zl = pop(zl,ZIPLIST_TAIL);
1240 ziplistRepr(zl);
1241
1242 zfree(zl);
1243
1244 printf("Get element at index 3:\n");
1245 {
1246 zl = createList();
1247 p = ziplistIndex(zl, 3);
1248 if (!ziplistGet(p, &entry, &elen, &value)) {
1249 printf("ERROR: Could not access index 3\n");
1250 return 1;
1251 }
1252 if (entry) {
1253 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1254 printf("\n");
1255 } else {
1256 printf("%lld\n", value);
1257 }
1258 printf("\n");
1259 zfree(zl);
1260 }
1261
1262 printf("Get element at index 4 (out of range):\n");
1263 {
1264 zl = createList();
1265 p = ziplistIndex(zl, 4);
1266 if (p == NULL) {
1267 printf("No entry\n");
1268 } else {
1269 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
1270 return 1;
1271 }
1272 printf("\n");
1273 zfree(zl);
1274 }
1275
1276 printf("Get element at index -1 (last element):\n");
1277 {
1278 zl = createList();
1279 p = ziplistIndex(zl, -1);
1280 if (!ziplistGet(p, &entry, &elen, &value)) {
1281 printf("ERROR: Could not access index -1\n");
1282 return 1;
1283 }
1284 if (entry) {
1285 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1286 printf("\n");
1287 } else {
1288 printf("%lld\n", value);
1289 }
1290 printf("\n");
1291 zfree(zl);
1292 }
1293
1294 printf("Get element at index -4 (first element):\n");
1295 {
1296 zl = createList();
1297 p = ziplistIndex(zl, -4);
1298 if (!ziplistGet(p, &entry, &elen, &value)) {
1299 printf("ERROR: Could not access index -4\n");
1300 return 1;
1301 }
1302 if (entry) {
1303 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1304 printf("\n");
1305 } else {
1306 printf("%lld\n", value);
1307 }
1308 printf("\n");
1309 zfree(zl);
1310 }
1311
1312 printf("Get element at index -5 (reverse out of range):\n");
1313 {
1314 zl = createList();
1315 p = ziplistIndex(zl, -5);
1316 if (p == NULL) {
1317 printf("No entry\n");
1318 } else {
1319 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
1320 return 1;
1321 }
1322 printf("\n");
1323 zfree(zl);
1324 }
1325
1326 printf("Iterate list from 0 to end:\n");
1327 {
1328 zl = createList();
1329 p = ziplistIndex(zl, 0);
1330 while (ziplistGet(p, &entry, &elen, &value)) {
1331 printf("Entry: ");
1332 if (entry) {
1333 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1334 } else {
1335 printf("%lld", value);
1336 }
1337 p = ziplistNext(zl,p);
1338 printf("\n");
1339 }
1340 printf("\n");
1341 zfree(zl);
1342 }
1343
1344 printf("Iterate list from 1 to end:\n");
1345 {
1346 zl = createList();
1347 p = ziplistIndex(zl, 1);
1348 while (ziplistGet(p, &entry, &elen, &value)) {
1349 printf("Entry: ");
1350 if (entry) {
1351 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1352 } else {
1353 printf("%lld", value);
1354 }
1355 p = ziplistNext(zl,p);
1356 printf("\n");
1357 }
1358 printf("\n");
1359 zfree(zl);
1360 }
1361
1362 printf("Iterate list from 2 to end:\n");
1363 {
1364 zl = createList();
1365 p = ziplistIndex(zl, 2);
1366 while (ziplistGet(p, &entry, &elen, &value)) {
1367 printf("Entry: ");
1368 if (entry) {
1369 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1370 } else {
1371 printf("%lld", value);
1372 }
1373 p = ziplistNext(zl,p);
1374 printf("\n");
1375 }
1376 printf("\n");
1377 zfree(zl);
1378 }
1379
1380 printf("Iterate starting out of range:\n");
1381 {
1382 zl = createList();
1383 p = ziplistIndex(zl, 4);
1384 if (!ziplistGet(p, &entry, &elen, &value)) {
1385 printf("No entry\n");
1386 } else {
1387 printf("ERROR\n");
1388 }
1389 printf("\n");
1390 zfree(zl);
1391 }
1392
1393 printf("Iterate from back to front:\n");
1394 {
1395 zl = createList();
1396 p = ziplistIndex(zl, -1);
1397 while (ziplistGet(p, &entry, &elen, &value)) {
1398 printf("Entry: ");
1399 if (entry) {
1400 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1401 } else {
1402 printf("%lld", value);
1403 }
1404 p = ziplistPrev(zl,p);
1405 printf("\n");
1406 }
1407 printf("\n");
1408 zfree(zl);
1409 }
1410
1411 printf("Iterate from back to front, deleting all items:\n");
1412 {
1413 zl = createList();
1414 p = ziplistIndex(zl, -1);
1415 while (ziplistGet(p, &entry, &elen, &value)) {
1416 printf("Entry: ");
1417 if (entry) {
1418 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1419 } else {
1420 printf("%lld", value);
1421 }
1422 zl = ziplistDelete(zl,&p);
1423 p = ziplistPrev(zl,p);
1424 printf("\n");
1425 }
1426 printf("\n");
1427 zfree(zl);
1428 }
1429
1430 printf("Delete inclusive range 0,0:\n");
1431 {
1432 zl = createList();
1433 zl = ziplistDeleteRange(zl, 0, 1);
1434 ziplistRepr(zl);
1435 zfree(zl);
1436 }
1437
1438 printf("Delete inclusive range 0,1:\n");
1439 {
1440 zl = createList();
1441 zl = ziplistDeleteRange(zl, 0, 2);
1442 ziplistRepr(zl);
1443 zfree(zl);
1444 }
1445
1446 printf("Delete inclusive range 1,2:\n");
1447 {
1448 zl = createList();
1449 zl = ziplistDeleteRange(zl, 1, 2);
1450 ziplistRepr(zl);
1451 zfree(zl);
1452 }
1453
1454 printf("Delete with start index out of range:\n");
1455 {
1456 zl = createList();
1457 zl = ziplistDeleteRange(zl, 5, 1);
1458 ziplistRepr(zl);
1459 zfree(zl);
1460 }
1461
1462 printf("Delete with num overflow:\n");
1463 {
1464 zl = createList();
1465 zl = ziplistDeleteRange(zl, 1, 5);
1466 ziplistRepr(zl);
1467 zfree(zl);
1468 }
1469
1470 printf("Delete foo while iterating:\n");
1471 {
1472 zl = createList();
1473 p = ziplistIndex(zl,0);
1474 while (ziplistGet(p,&entry,&elen,&value)) {
1475 if (entry && strncmp("foo",(char*)entry,elen) == 0) {
1476 printf("Delete foo\n");
1477 zl = ziplistDelete(zl,&p);
1478 } else {
1479 printf("Entry: ");
1480 if (entry) {
1481 if (elen && fwrite(entry,elen,1,stdout) == 0)
1482 perror("fwrite");
1483 } else {
1484 printf("%lld",value);
1485 }
1486 p = ziplistNext(zl,p);
1487 printf("\n");
1488 }
1489 }
1490 printf("\n");
1491 ziplistRepr(zl);
1492 zfree(zl);
1493 }
1494
1495 printf("Regression test for >255 byte strings:\n");
1496 {
1497 char v1[257] = {0}, v2[257] = {0};
1498 memset(v1,'x',256);
1499 memset(v2,'y',256);
1500 zl = ziplistNew();
1501 zl = ziplistPush(zl,(unsigned char*)v1,strlen(v1),ZIPLIST_TAIL);
1502 zl = ziplistPush(zl,(unsigned char*)v2,strlen(v2),ZIPLIST_TAIL);
1503
1504 /* Pop values again and compare their value. */
1505 p = ziplistIndex(zl,0);
1506 assert(ziplistGet(p,&entry,&elen,&value));
1507 assert(strncmp(v1,(char*)entry,elen) == 0);
1508 p = ziplistIndex(zl,1);
1509 assert(ziplistGet(p,&entry,&elen,&value));
1510 assert(strncmp(v2,(char*)entry,elen) == 0);
1511 printf("SUCCESS\n\n");
1512 zfree(zl);
1513 }
1514
1515 printf("Regression test deleting next to last entries:\n");
1516 {
1517 char v[3][257] = {{0}};
1518 zlentry e[3] = {{.prevrawlensize = 0, .prevrawlen = 0, .lensize = 0,
1519 .len = 0, .headersize = 0, .encoding = 0, .p = NULL}};
1520 size_t i;
1521
1522 for (i = 0; i < (sizeof(v)/sizeof(v[0])); i++) {
1523 memset(v[i], 'a' + i, sizeof(v[0]));
1524 }
1525
1526 v[0][256] = '\0';
1527 v[1][ 1] = '\0';
1528 v[2][256] = '\0';
1529
1530 zl = ziplistNew();
1531 for (i = 0; i < (sizeof(v)/sizeof(v[0])); i++) {
1532 zl = ziplistPush(zl, (unsigned char *) v[i], strlen(v[i]), ZIPLIST_TAIL);
1533 }
1534
1535 verify(zl, e);
1536
1537 assert(e[0].prevrawlensize == 1);
1538 assert(e[1].prevrawlensize == 5);
1539 assert(e[2].prevrawlensize == 1);
1540
1541 /* Deleting entry 1 will increase `prevrawlensize` for entry 2 */
1542 unsigned char *p = e[1].p;
1543 zl = ziplistDelete(zl, &p);
1544
1545 verify(zl, e);
1546
1547 assert(e[0].prevrawlensize == 1);
1548 assert(e[1].prevrawlensize == 5);
1549
1550 printf("SUCCESS\n\n");
1551 zfree(zl);
1552 }
1553
1554 printf("Create long list and check indices:\n");
1555 {
1556 zl = ziplistNew();
1557 char buf[32];
1558 int i,len;
1559 for (i = 0; i < 1000; i++) {
1560 len = sprintf(buf,"%d",i);
1561 zl = ziplistPush(zl,(unsigned char*)buf,len,ZIPLIST_TAIL);
1562 }
1563 for (i = 0; i < 1000; i++) {
1564 p = ziplistIndex(zl,i);
1565 assert(ziplistGet(p,NULL,NULL,&value));
1566 assert(i == value);
1567
1568 p = ziplistIndex(zl,-i-1);
1569 assert(ziplistGet(p,NULL,NULL,&value));
1570 assert(999-i == value);
1571 }
1572 printf("SUCCESS\n\n");
1573 zfree(zl);
1574 }
1575
1576 printf("Compare strings with ziplist entries:\n");
1577 {
1578 zl = createList();
1579 p = ziplistIndex(zl,0);
1580 if (!ziplistCompare(p,(unsigned char*)"hello",5)) {
1581 printf("ERROR: not \"hello\"\n");
1582 return 1;
1583 }
1584 if (ziplistCompare(p,(unsigned char*)"hella",5)) {
1585 printf("ERROR: \"hella\"\n");
1586 return 1;
1587 }
1588
1589 p = ziplistIndex(zl,3);
1590 if (!ziplistCompare(p,(unsigned char*)"1024",4)) {
1591 printf("ERROR: not \"1024\"\n");
1592 return 1;
1593 }
1594 if (ziplistCompare(p,(unsigned char*)"1025",4)) {
1595 printf("ERROR: \"1025\"\n");
1596 return 1;
1597 }
1598 printf("SUCCESS\n\n");
1599 zfree(zl);
1600 }
1601
1602 printf("Merge test:\n");
1603 {
1604 /* create list gives us: [hello, foo, quux, 1024] */
1605 zl = createList();
1606 unsigned char *zl2 = createList();
1607
1608 unsigned char *zl3 = ziplistNew();
1609 unsigned char *zl4 = ziplistNew();
1610
1611 if (ziplistMerge(&zl4, &zl4)) {
1612 printf("ERROR: Allowed merging of one ziplist into itself.\n");
1613 return 1;
1614 }
1615
1616 /* Merge two empty ziplists, get empty result back. */
1617 zl4 = ziplistMerge(&zl3, &zl4);
1618 ziplistRepr(zl4);
1619 if (ziplistLen(zl4)) {
1620 printf("ERROR: Merging two empty ziplists created entries.\n");
1621 return 1;
1622 }
1623 zfree(zl4);
1624
1625 zl2 = ziplistMerge(&zl, &zl2);
1626 /* merge gives us: [hello, foo, quux, 1024, hello, foo, quux, 1024] */
1627 ziplistRepr(zl2);
1628
1629 if (ziplistLen(zl2) != 8) {
1630 printf("ERROR: Merged length not 8, but: %u\n", ziplistLen(zl2));
1631 return 1;
1632 }
1633
1634 p = ziplistIndex(zl2,0);
1635 if (!ziplistCompare(p,(unsigned char*)"hello",5)) {
1636 printf("ERROR: not \"hello\"\n");
1637 return 1;
1638 }
1639 if (ziplistCompare(p,(unsigned char*)"hella",5)) {
1640 printf("ERROR: \"hella\"\n");
1641 return 1;
1642 }
1643
1644 p = ziplistIndex(zl2,3);
1645 if (!ziplistCompare(p,(unsigned char*)"1024",4)) {
1646 printf("ERROR: not \"1024\"\n");
1647 return 1;
1648 }
1649 if (ziplistCompare(p,(unsigned char*)"1025",4)) {
1650 printf("ERROR: \"1025\"\n");
1651 return 1;
1652 }
1653
1654 p = ziplistIndex(zl2,4);
1655 if (!ziplistCompare(p,(unsigned char*)"hello",5)) {
1656 printf("ERROR: not \"hello\"\n");
1657 return 1;
1658 }
1659 if (ziplistCompare(p,(unsigned char*)"hella",5)) {
1660 printf("ERROR: \"hella\"\n");
1661 return 1;
1662 }
1663
1664 p = ziplistIndex(zl2,7);
1665 if (!ziplistCompare(p,(unsigned char*)"1024",4)) {
1666 printf("ERROR: not \"1024\"\n");
1667 return 1;
1668 }
1669 if (ziplistCompare(p,(unsigned char*)"1025",4)) {
1670 printf("ERROR: \"1025\"\n");
1671 return 1;
1672 }
1673 printf("SUCCESS\n\n");
1674 zfree(zl);
1675 }
1676
1677 printf("Stress with random payloads of different encoding:\n");
1678 {
1679 int i,j,len,where;
1680 unsigned char *p;
1681 char buf[1024];
1682 int buflen;
1683 list *ref;
1684 listNode *refnode;
1685
1686 /* Hold temp vars from ziplist */
1687 unsigned char *sstr;
1688 unsigned int slen;
1689 long long sval;
1690
1691 for (i = 0; i < 20000; i++) {
1692 zl = ziplistNew();
1693 ref = listCreate();
1694 listSetFreeMethod(ref,(void (*)(void*))sdsfree);
1695 len = rand() % 256;
1696
1697 /* Create lists */
1698 for (j = 0; j < len; j++) {
1699 where = (rand() & 1) ? ZIPLIST_HEAD : ZIPLIST_TAIL;
1700 if (rand() % 2) {
1701 buflen = randstring(buf,1,sizeof(buf)-1);
1702 } else {
1703 switch(rand() % 3) {
1704 case 0:
1705 buflen = sprintf(buf,"%lld",(0LL + rand()) >> 20);
1706 break;
1707 case 1:
1708 buflen = sprintf(buf,"%lld",(0LL + rand()));
1709 break;
1710 case 2:
1711 buflen = sprintf(buf,"%lld",(0LL + rand()) << 20);
1712 break;
1713 default:
1714 assert(NULL);
1715 }
1716 }
1717
1718 /* Add to ziplist */
1719 zl = ziplistPush(zl, (unsigned char*)buf, buflen, where);
1720
1721 /* Add to reference list */
1722 if (where == ZIPLIST_HEAD) {
1723 listAddNodeHead(ref,sdsnewlen(buf, buflen));
1724 } else if (where == ZIPLIST_TAIL) {
1725 listAddNodeTail(ref,sdsnewlen(buf, buflen));
1726 } else {
1727 assert(NULL);
1728 }
1729 }
1730
1731 assert(listLength(ref) == ziplistLen(zl));
1732 for (j = 0; j < len; j++) {
1733 /* Naive way to get elements, but similar to the stresser
1734 * executed from the Tcl test suite. */
1735 p = ziplistIndex(zl,j);
1736 refnode = listIndex(ref,j);
1737
1738 assert(ziplistGet(p,&sstr,&slen,&sval));
1739 if (sstr == NULL) {
1740 buflen = sprintf(buf,"%lld",sval);
1741 } else {
1742 buflen = slen;
1743 memcpy(buf,sstr,buflen);
1744 buf[buflen] = '\0';
1745 }
1746 assert(memcmp(buf,listNodeValue(refnode),buflen) == 0);
1747 }
1748 zfree(zl);
1749 listRelease(ref);
1750 }
1751 printf("SUCCESS\n\n");
1752 }
1753
1754 printf("Stress with variable ziplist size:\n");
1755 {
1756 stress(ZIPLIST_HEAD,100000,16384,256);
1757 stress(ZIPLIST_TAIL,100000,16384,256);
1758 }
1759
1760 return 0;
1761 }
1762 #endif
1763