]> git.saurik.com Git - redis.git/blob - src/ziplist.c
Merge branch 'unstable' of github.com:antirez/redis into unstable
[redis.git] / 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.
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 * |1100____| - 1 byte
56 * Integer encoded as int16_t (2 bytes).
57 * |1101____| - 1 byte
58 * Integer encoded as int32_t (4 bytes).
59 * |1110____| - 1 byte
60 * Integer encoded as int64_t (8 bytes).
61 */
62
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <stdint.h>
67 #include <assert.h>
68 #include <limits.h>
69 #include "zmalloc.h"
70 #include "ziplist.h"
71 #include "endian.h"
72
73 int ll2string(char *s, size_t len, long long value);
74
75 #define ZIP_END 255
76 #define ZIP_BIGLEN 254
77
78 /* Different encoding/length possibilities */
79 #define ZIP_STR_06B (0 << 6)
80 #define ZIP_STR_14B (1 << 6)
81 #define ZIP_STR_32B (2 << 6)
82 #define ZIP_INT_16B (0xc0 | 0<<4)
83 #define ZIP_INT_32B (0xc0 | 1<<4)
84 #define ZIP_INT_64B (0xc0 | 2<<4)
85
86 /* Macro's to determine type */
87 #define ZIP_IS_STR(enc) (((enc) & 0xc0) < 0xc0)
88 #define ZIP_IS_INT(enc) (!ZIP_IS_STR(enc) && ((enc) & 0x30) < 0x30)
89
90 /* Utility macros */
91 #define ZIPLIST_BYTES(zl) (*((uint32_t*)(zl)))
92 #define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t))))
93 #define ZIPLIST_LENGTH(zl) (*((uint16_t*)((zl)+sizeof(uint32_t)*2)))
94 #define ZIPLIST_HEADER_SIZE (sizeof(uint32_t)*2+sizeof(uint16_t))
95 #define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE)
96 #define ZIPLIST_ENTRY_TAIL(zl) ((zl)+ZIPLIST_TAIL_OFFSET(zl))
97 #define ZIPLIST_ENTRY_END(zl) ((zl)+ZIPLIST_BYTES(zl)-1)
98
99 /* We know a positive increment can only be 1 because entries can only be
100 * pushed one at a time. */
101 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
102 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) ZIPLIST_LENGTH(zl)+=incr; }
103
104 typedef struct zlentry {
105 unsigned int prevrawlensize, prevrawlen;
106 unsigned int lensize, len;
107 unsigned int headersize;
108 unsigned char encoding;
109 unsigned char *p;
110 } zlentry;
111
112 /* Return the encoding pointer to by 'p'. */
113 static unsigned int zipEntryEncoding(unsigned char *p) {
114 /* String encoding: 2 MSBs */
115 unsigned char b = p[0] & 0xc0;
116 if (b < 0xc0) {
117 return b;
118 } else {
119 /* Integer encoding: 4 MSBs */
120 return p[0] & 0xf0;
121 }
122 assert(NULL);
123 return 0;
124 }
125
126 /* Return bytes needed to store integer encoded by 'encoding' */
127 static unsigned int zipIntSize(unsigned char encoding) {
128 switch(encoding) {
129 case ZIP_INT_16B: return sizeof(int16_t);
130 case ZIP_INT_32B: return sizeof(int32_t);
131 case ZIP_INT_64B: return sizeof(int64_t);
132 }
133 assert(NULL);
134 return 0;
135 }
136
137 /* Decode the encoded length pointed by 'p'. If a pointer to 'lensize' is
138 * provided, it is set to the number of bytes required to encode the length. */
139 static unsigned int zipDecodeLength(unsigned char *p, unsigned int *lensize) {
140 unsigned char encoding = zipEntryEncoding(p);
141 unsigned int len = 0;
142
143 if (ZIP_IS_STR(encoding)) {
144 switch(encoding) {
145 case ZIP_STR_06B:
146 len = p[0] & 0x3f;
147 if (lensize) *lensize = 1;
148 break;
149 case ZIP_STR_14B:
150 len = ((p[0] & 0x3f) << 8) | p[1];
151 if (lensize) *lensize = 2;
152 break;
153 case ZIP_STR_32B:
154 len = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
155 if (lensize) *lensize = 5;
156 break;
157 default:
158 assert(NULL);
159 }
160 } else {
161 len = zipIntSize(encoding);
162 if (lensize) *lensize = 1;
163 }
164 return len;
165 }
166
167 /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
168 * the amount of bytes required to encode such a length. */
169 static unsigned int zipEncodeLength(unsigned char *p, unsigned char encoding, unsigned int rawlen) {
170 unsigned char len = 1, buf[5];
171
172 if (ZIP_IS_STR(encoding)) {
173 /* Although encoding is given it may not be set for strings,
174 * so we determine it here using the raw length. */
175 if (rawlen <= 0x3f) {
176 if (!p) return len;
177 buf[0] = ZIP_STR_06B | rawlen;
178 } else if (rawlen <= 0x3fff) {
179 len += 1;
180 if (!p) return len;
181 buf[0] = ZIP_STR_14B | ((rawlen >> 8) & 0x3f);
182 buf[1] = rawlen & 0xff;
183 } else {
184 len += 4;
185 if (!p) return len;
186 buf[0] = ZIP_STR_32B;
187 buf[1] = (rawlen >> 24) & 0xff;
188 buf[2] = (rawlen >> 16) & 0xff;
189 buf[3] = (rawlen >> 8) & 0xff;
190 buf[4] = rawlen & 0xff;
191 }
192 } else {
193 /* Implies integer encoding, so length is always 1. */
194 if (!p) return len;
195 buf[0] = encoding;
196 }
197
198 /* Store this length at p */
199 memcpy(p,buf,len);
200 return len;
201 }
202
203 /* Decode the length of the previous element stored at "p". */
204 static unsigned int zipPrevDecodeLength(unsigned char *p, unsigned int *lensize) {
205 unsigned int len = *p;
206 if (len < ZIP_BIGLEN) {
207 if (lensize) *lensize = 1;
208 } else {
209 if (lensize) *lensize = 1+sizeof(len);
210 memcpy(&len,p+1,sizeof(len));
211 memrev32ifbe(&len);
212 }
213 return len;
214 }
215
216 /* Encode the length of the previous entry and write it to "p". Return the
217 * number of bytes needed to encode this length if "p" is NULL. */
218 static unsigned int zipPrevEncodeLength(unsigned char *p, unsigned int len) {
219 if (p == NULL) {
220 return (len < ZIP_BIGLEN) ? 1 : sizeof(len)+1;
221 } else {
222 if (len < ZIP_BIGLEN) {
223 p[0] = len;
224 return 1;
225 } else {
226 p[0] = ZIP_BIGLEN;
227 memcpy(p+1,&len,sizeof(len));
228 memrev32ifbe(p+1);
229 return 1+sizeof(len);
230 }
231 }
232 }
233
234 /* Encode the length of the previous entry and write it to "p". This only
235 * uses the larger encoding (required in __ziplistCascadeUpdate). */
236 static void zipPrevEncodeLengthForceLarge(unsigned char *p, unsigned int len) {
237 if (p == NULL) return;
238 p[0] = ZIP_BIGLEN;
239 memcpy(p+1,&len,sizeof(len));
240 memrev32ifbe(p+1);
241 }
242
243 /* Return the difference in number of bytes needed to store the new length
244 * "len" on the entry pointed to by "p". */
245 static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) {
246 unsigned int prevlensize;
247 zipPrevDecodeLength(p,&prevlensize);
248 return zipPrevEncodeLength(NULL,len)-prevlensize;
249 }
250
251 /* Check if string pointed to by 'entry' can be encoded as an integer.
252 * Stores the integer value in 'v' and its encoding in 'encoding'. */
253 static int zipTryEncoding(unsigned char *entry, unsigned int entrylen, long long *v, unsigned char *encoding) {
254 long long value;
255 char *eptr;
256 char buf[32];
257
258 if (entrylen >= 32 || entrylen == 0) return 0;
259 if (entry[0] == '-' || (entry[0] >= '0' && entry[0] <= '9')) {
260 int slen;
261
262 /* Perform a back-and-forth conversion to make sure that
263 * the string turned into an integer is not losing any info. */
264 memcpy(buf,entry,entrylen);
265 buf[entrylen] = '\0';
266 value = strtoll(buf,&eptr,10);
267 if (eptr[0] != '\0') return 0;
268 slen = ll2string(buf,32,value);
269 if (entrylen != (unsigned)slen || memcmp(buf,entry,slen)) return 0;
270
271 /* Great, the string can be encoded. Check what's the smallest
272 * of our encoding types that can hold this value. */
273 if (value >= INT16_MIN && value <= INT16_MAX) {
274 *encoding = ZIP_INT_16B;
275 } else if (value >= INT32_MIN && value <= INT32_MAX) {
276 *encoding = ZIP_INT_32B;
277 } else {
278 *encoding = ZIP_INT_64B;
279 }
280 *v = value;
281 return 1;
282 }
283 return 0;
284 }
285
286 /* Store integer 'value' at 'p', encoded as 'encoding' */
287 static void zipSaveInteger(unsigned char *p, int64_t value, unsigned char encoding) {
288 int16_t i16;
289 int32_t i32;
290 int64_t i64;
291 if (encoding == ZIP_INT_16B) {
292 i16 = value;
293 memcpy(p,&i16,sizeof(i16));
294 memrev16ifbe(p);
295 } else if (encoding == ZIP_INT_32B) {
296 i32 = value;
297 memcpy(p,&i32,sizeof(i32));
298 memrev32ifbe(p);
299 } else if (encoding == ZIP_INT_64B) {
300 i64 = value;
301 memcpy(p,&i64,sizeof(i64));
302 memrev64ifbe(p);
303 } else {
304 assert(NULL);
305 }
306 }
307
308 /* Read integer encoded as 'encoding' from 'p' */
309 static int64_t zipLoadInteger(unsigned char *p, unsigned char encoding) {
310 int16_t i16;
311 int32_t i32;
312 int64_t i64, ret = 0;
313 if (encoding == ZIP_INT_16B) {
314 memcpy(&i16,p,sizeof(i16));
315 memrev16ifbe(&i16);
316 ret = i16;
317 } else if (encoding == ZIP_INT_32B) {
318 memcpy(&i32,p,sizeof(i32));
319 memrev16ifbe(&i32);
320 ret = i32;
321 } else if (encoding == ZIP_INT_64B) {
322 memcpy(&i64,p,sizeof(i64));
323 memrev16ifbe(&i64);
324 ret = i64;
325 } else {
326 assert(NULL);
327 }
328 return ret;
329 }
330
331 /* Return a struct with all information about an entry. */
332 static zlentry zipEntry(unsigned char *p) {
333 zlentry e;
334 e.prevrawlen = zipPrevDecodeLength(p,&e.prevrawlensize);
335 e.len = zipDecodeLength(p+e.prevrawlensize,&e.lensize);
336 e.headersize = e.prevrawlensize+e.lensize;
337 e.encoding = zipEntryEncoding(p+e.prevrawlensize);
338 e.p = p;
339 return e;
340 }
341
342 /* Return the total number of bytes used by the entry at "p". */
343 static unsigned int zipRawEntryLength(unsigned char *p) {
344 zlentry e = zipEntry(p);
345 return e.headersize + e.len;
346 }
347
348 /* Create a new empty ziplist. */
349 unsigned char *ziplistNew(void) {
350 unsigned int bytes = ZIPLIST_HEADER_SIZE+1;
351 unsigned char *zl = zmalloc(bytes);
352 ZIPLIST_BYTES(zl) = bytes;
353 ZIPLIST_TAIL_OFFSET(zl) = ZIPLIST_HEADER_SIZE;
354 ZIPLIST_LENGTH(zl) = 0;
355 zl[bytes-1] = ZIP_END;
356 return zl;
357 }
358
359 /* Resize the ziplist. */
360 static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
361 zl = zrealloc(zl,len);
362 ZIPLIST_BYTES(zl) = len;
363 zl[len-1] = ZIP_END;
364 return zl;
365 }
366
367 /* When an entry is inserted, we need to set the prevlen field of the next
368 * entry to equal the length of the inserted entry. It can occur that this
369 * length cannot be encoded in 1 byte and the next entry needs to be grow
370 * a bit larger to hold the 5-byte encoded prevlen. This can be done for free,
371 * because this only happens when an entry is already being inserted (which
372 * causes a realloc and memmove). However, encoding the prevlen may require
373 * that this entry is grown as well. This effect may cascade throughout
374 * the ziplist when there are consecutive entries with a size close to
375 * ZIP_BIGLEN, so we need to check that the prevlen can be encoded in every
376 * consecutive entry.
377 *
378 * Note that this effect can also happen in reverse, where the bytes required
379 * to encode the prevlen field can shrink. This effect is deliberately ignored,
380 * because it can cause a "flapping" effect where a chain prevlen fields is
381 * first grown and then shrunk again after consecutive inserts. Rather, the
382 * field is allowed to stay larger than necessary, because a large prevlen
383 * field implies the ziplist is holding large entries anyway.
384 *
385 * The pointer "p" points to the first entry that does NOT need to be
386 * updated, i.e. consecutive fields MAY need an update. */
387 static unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p) {
388 unsigned int curlen = ZIPLIST_BYTES(zl), rawlen, rawlensize;
389 unsigned int offset, noffset, extra;
390 unsigned char *np;
391 zlentry cur, next;
392
393 while (p[0] != ZIP_END) {
394 cur = zipEntry(p);
395 rawlen = cur.headersize + cur.len;
396 rawlensize = zipPrevEncodeLength(NULL,rawlen);
397
398 /* Abort if there is no next entry. */
399 if (p[rawlen] == ZIP_END) break;
400 next = zipEntry(p+rawlen);
401
402 /* Abort when "prevlen" has not changed. */
403 if (next.prevrawlen == rawlen) break;
404
405 if (next.prevrawlensize < rawlensize) {
406 /* The "prevlen" field of "next" needs more bytes to hold
407 * the raw length of "cur". */
408 offset = p-zl;
409 extra = rawlensize-next.prevrawlensize;
410 zl = ziplistResize(zl,curlen+extra);
411 ZIPLIST_TAIL_OFFSET(zl) += extra;
412 p = zl+offset;
413
414 /* Move the tail to the back. */
415 np = p+rawlen;
416 noffset = np-zl;
417 memmove(np+rawlensize,
418 np+next.prevrawlensize,
419 curlen-noffset-next.prevrawlensize-1);
420 zipPrevEncodeLength(np,rawlen);
421
422 /* Advance the cursor */
423 p += rawlen;
424 curlen += extra;
425 } else {
426 if (next.prevrawlensize > rawlensize) {
427 /* This would result in shrinking, which we want to avoid.
428 * So, set "rawlen" in the available bytes. */
429 zipPrevEncodeLengthForceLarge(p+rawlen,rawlen);
430 } else {
431 zipPrevEncodeLength(p+rawlen,rawlen);
432 }
433
434 /* Stop here, as the raw length of "next" has not changed. */
435 break;
436 }
437 }
438 return zl;
439 }
440
441 /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
442 static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsigned int num) {
443 unsigned int i, totlen, deleted = 0;
444 int offset, nextdiff = 0;
445 zlentry first, tail;
446
447 first = zipEntry(p);
448 for (i = 0; p[0] != ZIP_END && i < num; i++) {
449 p += zipRawEntryLength(p);
450 deleted++;
451 }
452
453 totlen = p-first.p;
454 if (totlen > 0) {
455 if (p[0] != ZIP_END) {
456 /* Tricky: storing the prevlen in this entry might reduce or
457 * increase the number of bytes needed, compared to the current
458 * prevlen. Note that we can always store this length because
459 * it was previously stored by an entry that is being deleted. */
460 nextdiff = zipPrevLenByteDiff(p,first.prevrawlen);
461 zipPrevEncodeLength(p-nextdiff,first.prevrawlen);
462
463 /* Update offset for tail */
464 ZIPLIST_TAIL_OFFSET(zl) -= totlen;
465
466 /* When the tail contains more than one entry, we need to take
467 * "nextdiff" in account as well. Otherwise, a change in the
468 * size of prevlen doesn't have an effect on the *tail* offset. */
469 tail = zipEntry(p);
470 if (p[tail.headersize+tail.len] != ZIP_END)
471 ZIPLIST_TAIL_OFFSET(zl) += nextdiff;
472
473 /* Move tail to the front of the ziplist */
474 memmove(first.p,p-nextdiff,ZIPLIST_BYTES(zl)-(p-zl)-1+nextdiff);
475 } else {
476 /* The entire tail was deleted. No need to move memory. */
477 ZIPLIST_TAIL_OFFSET(zl) = (first.p-zl)-first.prevrawlen;
478 }
479
480 /* Resize and update length */
481 offset = first.p-zl;
482 zl = ziplistResize(zl, ZIPLIST_BYTES(zl)-totlen+nextdiff);
483 ZIPLIST_INCR_LENGTH(zl,-deleted);
484 p = zl+offset;
485
486 /* When nextdiff != 0, the raw length of the next entry has changed, so
487 * we need to cascade the update throughout the ziplist */
488 if (nextdiff != 0)
489 zl = __ziplistCascadeUpdate(zl,p);
490 }
491 return zl;
492 }
493
494 /* Insert item at "p". */
495 static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
496 unsigned int curlen = ZIPLIST_BYTES(zl), reqlen, prevlen = 0;
497 unsigned int offset, nextdiff = 0;
498 unsigned char encoding = 0;
499 long long value;
500 zlentry entry, tail;
501
502 /* Find out prevlen for the entry that is inserted. */
503 if (p[0] != ZIP_END) {
504 entry = zipEntry(p);
505 prevlen = entry.prevrawlen;
506 } else {
507 unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl);
508 if (ptail[0] != ZIP_END) {
509 prevlen = zipRawEntryLength(ptail);
510 }
511 }
512
513 /* See if the entry can be encoded */
514 if (zipTryEncoding(s,slen,&value,&encoding)) {
515 /* 'encoding' is set to the appropriate integer encoding */
516 reqlen = zipIntSize(encoding);
517 } else {
518 /* 'encoding' is untouched, however zipEncodeLength will use the
519 * string length to figure out how to encode it. */
520 reqlen = slen;
521 }
522 /* We need space for both the length of the previous entry and
523 * the length of the payload. */
524 reqlen += zipPrevEncodeLength(NULL,prevlen);
525 reqlen += zipEncodeLength(NULL,encoding,slen);
526
527 /* When the insert position is not equal to the tail, we need to
528 * make sure that the next entry can hold this entry's length in
529 * its prevlen field. */
530 nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;
531
532 /* Store offset because a realloc may change the address of zl. */
533 offset = p-zl;
534 zl = ziplistResize(zl,curlen+reqlen+nextdiff);
535 p = zl+offset;
536
537 /* Apply memory move when necessary and update tail offset. */
538 if (p[0] != ZIP_END) {
539 /* Subtract one because of the ZIP_END bytes */
540 memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);
541
542 /* Encode this entry's raw length in the next entry. */
543 zipPrevEncodeLength(p+reqlen,reqlen);
544
545 /* Update offset for tail */
546 ZIPLIST_TAIL_OFFSET(zl) += reqlen;
547
548 /* When the tail contains more than one entry, we need to take
549 * "nextdiff" in account as well. Otherwise, a change in the
550 * size of prevlen doesn't have an effect on the *tail* offset. */
551 tail = zipEntry(p+reqlen);
552 if (p[reqlen+tail.headersize+tail.len] != ZIP_END)
553 ZIPLIST_TAIL_OFFSET(zl) += nextdiff;
554 } else {
555 /* This element will be the new tail. */
556 ZIPLIST_TAIL_OFFSET(zl) = p-zl;
557 }
558
559 /* When nextdiff != 0, the raw length of the next entry has changed, so
560 * we need to cascade the update throughout the ziplist */
561 if (nextdiff != 0) {
562 offset = p-zl;
563 zl = __ziplistCascadeUpdate(zl,p+reqlen);
564 p = zl+offset;
565 }
566
567 /* Write the entry */
568 p += zipPrevEncodeLength(p,prevlen);
569 p += zipEncodeLength(p,encoding,slen);
570 if (ZIP_IS_STR(encoding)) {
571 memcpy(p,s,slen);
572 } else {
573 zipSaveInteger(p,value,encoding);
574 }
575 ZIPLIST_INCR_LENGTH(zl,1);
576 return zl;
577 }
578
579 unsigned char *ziplistPush(unsigned char *zl, unsigned char *s, unsigned int slen, int where) {
580 unsigned char *p;
581 p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_END(zl);
582 return __ziplistInsert(zl,p,s,slen);
583 }
584
585 /* Returns an offset to use for iterating with ziplistNext. When the given
586 * index is negative, the list is traversed back to front. When the list
587 * doesn't contain an element at the provided index, NULL is returned. */
588 unsigned char *ziplistIndex(unsigned char *zl, int index) {
589 unsigned char *p;
590 zlentry entry;
591 if (index < 0) {
592 index = (-index)-1;
593 p = ZIPLIST_ENTRY_TAIL(zl);
594 if (p[0] != ZIP_END) {
595 entry = zipEntry(p);
596 while (entry.prevrawlen > 0 && index--) {
597 p -= entry.prevrawlen;
598 entry = zipEntry(p);
599 }
600 }
601 } else {
602 p = ZIPLIST_ENTRY_HEAD(zl);
603 while (p[0] != ZIP_END && index--) {
604 p += zipRawEntryLength(p);
605 }
606 }
607 return (p[0] == ZIP_END || index > 0) ? NULL : p;
608 }
609
610 /* Return pointer to next entry in ziplist.
611 *
612 * zl is the pointer to the ziplist
613 * p is the pointer to the current element
614 *
615 * The element after 'p' is returned, otherwise NULL if we are at the end. */
616 unsigned char *ziplistNext(unsigned char *zl, unsigned char *p) {
617 ((void) zl);
618
619 /* "p" could be equal to ZIP_END, caused by ziplistDelete,
620 * and we should return NULL. Otherwise, we should return NULL
621 * when the *next* element is ZIP_END (there is no next entry). */
622 if (p[0] == ZIP_END) {
623 return NULL;
624 } else {
625 p = p+zipRawEntryLength(p);
626 return (p[0] == ZIP_END) ? NULL : p;
627 }
628 }
629
630 /* Return pointer to previous entry in ziplist. */
631 unsigned char *ziplistPrev(unsigned char *zl, unsigned char *p) {
632 zlentry entry;
633
634 /* Iterating backwards from ZIP_END should return the tail. When "p" is
635 * equal to the first element of the list, we're already at the head,
636 * and should return NULL. */
637 if (p[0] == ZIP_END) {
638 p = ZIPLIST_ENTRY_TAIL(zl);
639 return (p[0] == ZIP_END) ? NULL : p;
640 } else if (p == ZIPLIST_ENTRY_HEAD(zl)) {
641 return NULL;
642 } else {
643 entry = zipEntry(p);
644 assert(entry.prevrawlen > 0);
645 return p-entry.prevrawlen;
646 }
647 }
648
649 /* Get entry pointer to by 'p' and store in either 'e' or 'v' depending
650 * on the encoding of the entry. 'e' is always set to NULL to be able
651 * to find out whether the string pointer or the integer value was set.
652 * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */
653 unsigned int ziplistGet(unsigned char *p, unsigned char **sstr, unsigned int *slen, long long *sval) {
654 zlentry entry;
655 if (p == NULL || p[0] == ZIP_END) return 0;
656 if (sstr) *sstr = NULL;
657
658 entry = zipEntry(p);
659 if (ZIP_IS_STR(entry.encoding)) {
660 if (sstr) {
661 *slen = entry.len;
662 *sstr = p+entry.headersize;
663 }
664 } else {
665 if (sval) {
666 *sval = zipLoadInteger(p+entry.headersize,entry.encoding);
667 }
668 }
669 return 1;
670 }
671
672 /* Insert an entry at "p". */
673 unsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
674 return __ziplistInsert(zl,p,s,slen);
675 }
676
677 /* Delete a single entry from the ziplist, pointed to by *p.
678 * Also update *p in place, to be able to iterate over the
679 * ziplist, while deleting entries. */
680 unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
681 unsigned int offset = *p-zl;
682 zl = __ziplistDelete(zl,*p,1);
683
684 /* Store pointer to current element in p, because ziplistDelete will
685 * do a realloc which might result in a different "zl"-pointer.
686 * When the delete direction is back to front, we might delete the last
687 * entry and end up with "p" pointing to ZIP_END, so check this. */
688 *p = zl+offset;
689 return zl;
690 }
691
692 /* Delete a range of entries from the ziplist. */
693 unsigned char *ziplistDeleteRange(unsigned char *zl, unsigned int index, unsigned int num) {
694 unsigned char *p = ziplistIndex(zl,index);
695 return (p == NULL) ? zl : __ziplistDelete(zl,p,num);
696 }
697
698 /* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
699 unsigned int ziplistCompare(unsigned char *p, unsigned char *sstr, unsigned int slen) {
700 zlentry entry;
701 unsigned char sencoding;
702 long long zval, sval;
703 if (p[0] == ZIP_END) return 0;
704
705 entry = zipEntry(p);
706 if (ZIP_IS_STR(entry.encoding)) {
707 /* Raw compare */
708 if (entry.len == slen) {
709 return memcmp(p+entry.headersize,sstr,slen) == 0;
710 } else {
711 return 0;
712 }
713 } else {
714 /* Try to compare encoded values */
715 if (zipTryEncoding(sstr,slen,&sval,&sencoding)) {
716 if (entry.encoding == sencoding) {
717 zval = zipLoadInteger(p+entry.headersize,entry.encoding);
718 return zval == sval;
719 }
720 }
721 }
722 return 0;
723 }
724
725 /* Return length of ziplist. */
726 unsigned int ziplistLen(unsigned char *zl) {
727 unsigned int len = 0;
728 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) {
729 len = ZIPLIST_LENGTH(zl);
730 } else {
731 unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
732 while (*p != ZIP_END) {
733 p += zipRawEntryLength(p);
734 len++;
735 }
736
737 /* Re-store length if small enough */
738 if (len < UINT16_MAX) ZIPLIST_LENGTH(zl) = len;
739 }
740 return len;
741 }
742
743 /* Return ziplist blob size in bytes. */
744 size_t ziplistBlobLen(unsigned char *zl) {
745 return ZIPLIST_BYTES(zl);
746 }
747
748 void ziplistRepr(unsigned char *zl) {
749 unsigned char *p;
750 int index = 0;
751 zlentry entry;
752
753 printf(
754 "{total bytes %d} "
755 "{length %u}\n"
756 "{tail offset %u}\n",
757 ZIPLIST_BYTES(zl),
758 ZIPLIST_LENGTH(zl),
759 ZIPLIST_TAIL_OFFSET(zl));
760 p = ZIPLIST_ENTRY_HEAD(zl);
761 while(*p != ZIP_END) {
762 entry = zipEntry(p);
763 printf(
764 "{"
765 "addr 0x%08lx, "
766 "index %2d, "
767 "offset %5ld, "
768 "rl: %5u, "
769 "hs %2u, "
770 "pl: %5u, "
771 "pls: %2u, "
772 "payload %5u"
773 "} ",
774 (long unsigned)p,
775 index,
776 (unsigned long) (p-zl),
777 entry.headersize+entry.len,
778 entry.headersize,
779 entry.prevrawlen,
780 entry.prevrawlensize,
781 entry.len);
782 p += entry.headersize;
783 if (ZIP_IS_STR(entry.encoding)) {
784 if (entry.len > 40) {
785 if (fwrite(p,40,1,stdout) == 0) perror("fwrite");
786 printf("...");
787 } else {
788 if (entry.len &&
789 fwrite(p,entry.len,1,stdout) == 0) perror("fwrite");
790 }
791 } else {
792 printf("%lld", (long long) zipLoadInteger(p,entry.encoding));
793 }
794 printf("\n");
795 p += entry.len;
796 index++;
797 }
798 printf("{end}\n\n");
799 }
800
801 #ifdef ZIPLIST_TEST_MAIN
802 #include <sys/time.h>
803 #include "adlist.h"
804 #include "sds.h"
805
806 #define debug(f, ...) { if (DEBUG) printf(f, __VA_ARGS__); }
807
808 unsigned char *createList() {
809 unsigned char *zl = ziplistNew();
810 zl = ziplistPush(zl, (unsigned char*)"foo", 3, ZIPLIST_TAIL);
811 zl = ziplistPush(zl, (unsigned char*)"quux", 4, ZIPLIST_TAIL);
812 zl = ziplistPush(zl, (unsigned char*)"hello", 5, ZIPLIST_HEAD);
813 zl = ziplistPush(zl, (unsigned char*)"1024", 4, ZIPLIST_TAIL);
814 return zl;
815 }
816
817 unsigned char *createIntList() {
818 unsigned char *zl = ziplistNew();
819 char buf[32];
820
821 sprintf(buf, "100");
822 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
823 sprintf(buf, "128000");
824 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
825 sprintf(buf, "-100");
826 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
827 sprintf(buf, "4294967296");
828 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
829 sprintf(buf, "non integer");
830 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
831 sprintf(buf, "much much longer non integer");
832 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
833 return zl;
834 }
835
836 long long usec(void) {
837 struct timeval tv;
838 gettimeofday(&tv,NULL);
839 return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
840 }
841
842 void stress(int pos, int num, int maxsize, int dnum) {
843 int i,j,k;
844 unsigned char *zl;
845 char posstr[2][5] = { "HEAD", "TAIL" };
846 long long start;
847 for (i = 0; i < maxsize; i+=dnum) {
848 zl = ziplistNew();
849 for (j = 0; j < i; j++) {
850 zl = ziplistPush(zl,(unsigned char*)"quux",4,ZIPLIST_TAIL);
851 }
852
853 /* Do num times a push+pop from pos */
854 start = usec();
855 for (k = 0; k < num; k++) {
856 zl = ziplistPush(zl,(unsigned char*)"quux",4,pos);
857 zl = ziplistDeleteRange(zl,0,1);
858 }
859 printf("List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\n",
860 i,ZIPLIST_BYTES(zl),num,posstr[pos],usec()-start);
861 zfree(zl);
862 }
863 }
864
865 void pop(unsigned char *zl, int where) {
866 unsigned char *p, *vstr;
867 unsigned int vlen;
868 long long vlong;
869
870 p = ziplistIndex(zl,where == ZIPLIST_HEAD ? 0 : -1);
871 if (ziplistGet(p,&vstr,&vlen,&vlong)) {
872 if (where == ZIPLIST_HEAD)
873 printf("Pop head: ");
874 else
875 printf("Pop tail: ");
876
877 if (vstr)
878 if (vlen && fwrite(vstr,vlen,1,stdout) == 0) perror("fwrite");
879 else
880 printf("%lld", vlong);
881
882 printf("\n");
883 ziplistDeleteRange(zl,-1,1);
884 } else {
885 printf("ERROR: Could not pop\n");
886 exit(1);
887 }
888 }
889
890 void randstring(char *target, unsigned int min, unsigned int max) {
891 int p, len = min+rand()%(max-min+1);
892 int minval, maxval;
893 switch(rand() % 3) {
894 case 0:
895 minval = 0;
896 maxval = 255;
897 break;
898 case 1:
899 minval = 48;
900 maxval = 122;
901 break;
902 case 2:
903 minval = 48;
904 maxval = 52;
905 break;
906 default:
907 assert(NULL);
908 }
909
910 while(p < len)
911 target[p++] = minval+rand()%(maxval-minval+1);
912 return;
913 }
914
915
916 int main(int argc, char **argv) {
917 unsigned char *zl, *p;
918 unsigned char *entry;
919 unsigned int elen;
920 long long value;
921
922 /* If an argument is given, use it as the random seed. */
923 if (argc == 2)
924 srand(atoi(argv[1]));
925
926 zl = createIntList();
927 ziplistRepr(zl);
928
929 zl = createList();
930 ziplistRepr(zl);
931
932 pop(zl,ZIPLIST_TAIL);
933 ziplistRepr(zl);
934
935 pop(zl,ZIPLIST_HEAD);
936 ziplistRepr(zl);
937
938 pop(zl,ZIPLIST_TAIL);
939 ziplistRepr(zl);
940
941 pop(zl,ZIPLIST_TAIL);
942 ziplistRepr(zl);
943
944 printf("Get element at index 3:\n");
945 {
946 zl = createList();
947 p = ziplistIndex(zl, 3);
948 if (!ziplistGet(p, &entry, &elen, &value)) {
949 printf("ERROR: Could not access index 3\n");
950 return 1;
951 }
952 if (entry) {
953 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
954 printf("\n");
955 } else {
956 printf("%lld\n", value);
957 }
958 printf("\n");
959 }
960
961 printf("Get element at index 4 (out of range):\n");
962 {
963 zl = createList();
964 p = ziplistIndex(zl, 4);
965 if (p == NULL) {
966 printf("No entry\n");
967 } else {
968 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
969 return 1;
970 }
971 printf("\n");
972 }
973
974 printf("Get element at index -1 (last element):\n");
975 {
976 zl = createList();
977 p = ziplistIndex(zl, -1);
978 if (!ziplistGet(p, &entry, &elen, &value)) {
979 printf("ERROR: Could not access index -1\n");
980 return 1;
981 }
982 if (entry) {
983 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
984 printf("\n");
985 } else {
986 printf("%lld\n", value);
987 }
988 printf("\n");
989 }
990
991 printf("Get element at index -4 (first element):\n");
992 {
993 zl = createList();
994 p = ziplistIndex(zl, -4);
995 if (!ziplistGet(p, &entry, &elen, &value)) {
996 printf("ERROR: Could not access index -4\n");
997 return 1;
998 }
999 if (entry) {
1000 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1001 printf("\n");
1002 } else {
1003 printf("%lld\n", value);
1004 }
1005 printf("\n");
1006 }
1007
1008 printf("Get element at index -5 (reverse out of range):\n");
1009 {
1010 zl = createList();
1011 p = ziplistIndex(zl, -5);
1012 if (p == NULL) {
1013 printf("No entry\n");
1014 } else {
1015 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
1016 return 1;
1017 }
1018 printf("\n");
1019 }
1020
1021 printf("Iterate list from 0 to end:\n");
1022 {
1023 zl = createList();
1024 p = ziplistIndex(zl, 0);
1025 while (ziplistGet(p, &entry, &elen, &value)) {
1026 printf("Entry: ");
1027 if (entry) {
1028 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1029 } else {
1030 printf("%lld", value);
1031 }
1032 p = ziplistNext(zl,p);
1033 printf("\n");
1034 }
1035 printf("\n");
1036 }
1037
1038 printf("Iterate list from 1 to end:\n");
1039 {
1040 zl = createList();
1041 p = ziplistIndex(zl, 1);
1042 while (ziplistGet(p, &entry, &elen, &value)) {
1043 printf("Entry: ");
1044 if (entry) {
1045 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1046 } else {
1047 printf("%lld", value);
1048 }
1049 p = ziplistNext(zl,p);
1050 printf("\n");
1051 }
1052 printf("\n");
1053 }
1054
1055 printf("Iterate list from 2 to end:\n");
1056 {
1057 zl = createList();
1058 p = ziplistIndex(zl, 2);
1059 while (ziplistGet(p, &entry, &elen, &value)) {
1060 printf("Entry: ");
1061 if (entry) {
1062 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1063 } else {
1064 printf("%lld", value);
1065 }
1066 p = ziplistNext(zl,p);
1067 printf("\n");
1068 }
1069 printf("\n");
1070 }
1071
1072 printf("Iterate starting out of range:\n");
1073 {
1074 zl = createList();
1075 p = ziplistIndex(zl, 4);
1076 if (!ziplistGet(p, &entry, &elen, &value)) {
1077 printf("No entry\n");
1078 } else {
1079 printf("ERROR\n");
1080 }
1081 printf("\n");
1082 }
1083
1084 printf("Iterate from back to front:\n");
1085 {
1086 zl = createList();
1087 p = ziplistIndex(zl, -1);
1088 while (ziplistGet(p, &entry, &elen, &value)) {
1089 printf("Entry: ");
1090 if (entry) {
1091 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1092 } else {
1093 printf("%lld", value);
1094 }
1095 p = ziplistPrev(zl,p);
1096 printf("\n");
1097 }
1098 printf("\n");
1099 }
1100
1101 printf("Iterate from back to front, deleting all items:\n");
1102 {
1103 zl = createList();
1104 p = ziplistIndex(zl, -1);
1105 while (ziplistGet(p, &entry, &elen, &value)) {
1106 printf("Entry: ");
1107 if (entry) {
1108 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1109 } else {
1110 printf("%lld", value);
1111 }
1112 zl = ziplistDelete(zl,&p);
1113 p = ziplistPrev(zl,p);
1114 printf("\n");
1115 }
1116 printf("\n");
1117 }
1118
1119 printf("Delete inclusive range 0,0:\n");
1120 {
1121 zl = createList();
1122 zl = ziplistDeleteRange(zl, 0, 1);
1123 ziplistRepr(zl);
1124 }
1125
1126 printf("Delete inclusive range 0,1:\n");
1127 {
1128 zl = createList();
1129 zl = ziplistDeleteRange(zl, 0, 2);
1130 ziplistRepr(zl);
1131 }
1132
1133 printf("Delete inclusive range 1,2:\n");
1134 {
1135 zl = createList();
1136 zl = ziplistDeleteRange(zl, 1, 2);
1137 ziplistRepr(zl);
1138 }
1139
1140 printf("Delete with start index out of range:\n");
1141 {
1142 zl = createList();
1143 zl = ziplistDeleteRange(zl, 5, 1);
1144 ziplistRepr(zl);
1145 }
1146
1147 printf("Delete with num overflow:\n");
1148 {
1149 zl = createList();
1150 zl = ziplistDeleteRange(zl, 1, 5);
1151 ziplistRepr(zl);
1152 }
1153
1154 printf("Delete foo while iterating:\n");
1155 {
1156 zl = createList();
1157 p = ziplistIndex(zl,0);
1158 while (ziplistGet(p,&entry,&elen,&value)) {
1159 if (entry && strncmp("foo",(char*)entry,elen) == 0) {
1160 printf("Delete foo\n");
1161 zl = ziplistDelete(zl,&p);
1162 } else {
1163 printf("Entry: ");
1164 if (entry) {
1165 if (elen && fwrite(entry,elen,1,stdout) == 0)
1166 perror("fwrite");
1167 } else {
1168 printf("%lld",value);
1169 }
1170 p = ziplistNext(zl,p);
1171 printf("\n");
1172 }
1173 }
1174 printf("\n");
1175 ziplistRepr(zl);
1176 }
1177
1178 printf("Regression test for >255 byte strings:\n");
1179 {
1180 char v1[257],v2[257];
1181 memset(v1,'x',256);
1182 memset(v2,'y',256);
1183 zl = ziplistNew();
1184 zl = ziplistPush(zl,(unsigned char*)v1,strlen(v1),ZIPLIST_TAIL);
1185 zl = ziplistPush(zl,(unsigned char*)v2,strlen(v2),ZIPLIST_TAIL);
1186
1187 /* Pop values again and compare their value. */
1188 p = ziplistIndex(zl,0);
1189 assert(ziplistGet(p,&entry,&elen,&value));
1190 assert(strncmp(v1,(char*)entry,elen) == 0);
1191 p = ziplistIndex(zl,1);
1192 assert(ziplistGet(p,&entry,&elen,&value));
1193 assert(strncmp(v2,(char*)entry,elen) == 0);
1194 printf("SUCCESS\n\n");
1195 }
1196
1197 printf("Create long list and check indices:\n");
1198 {
1199 zl = ziplistNew();
1200 char buf[32];
1201 int i,len;
1202 for (i = 0; i < 1000; i++) {
1203 len = sprintf(buf,"%d",i);
1204 zl = ziplistPush(zl,(unsigned char*)buf,len,ZIPLIST_TAIL);
1205 }
1206 for (i = 0; i < 1000; i++) {
1207 p = ziplistIndex(zl,i);
1208 assert(ziplistGet(p,NULL,NULL,&value));
1209 assert(i == value);
1210
1211 p = ziplistIndex(zl,-i-1);
1212 assert(ziplistGet(p,NULL,NULL,&value));
1213 assert(999-i == value);
1214 }
1215 printf("SUCCESS\n\n");
1216 }
1217
1218 printf("Compare strings with ziplist entries:\n");
1219 {
1220 zl = createList();
1221 p = ziplistIndex(zl,0);
1222 if (!ziplistCompare(p,(unsigned char*)"hello",5)) {
1223 printf("ERROR: not \"hello\"\n");
1224 return 1;
1225 }
1226 if (ziplistCompare(p,(unsigned char*)"hella",5)) {
1227 printf("ERROR: \"hella\"\n");
1228 return 1;
1229 }
1230
1231 p = ziplistIndex(zl,3);
1232 if (!ziplistCompare(p,(unsigned char*)"1024",4)) {
1233 printf("ERROR: not \"1024\"\n");
1234 return 1;
1235 }
1236 if (ziplistCompare(p,(unsigned char*)"1025",4)) {
1237 printf("ERROR: \"1025\"\n");
1238 return 1;
1239 }
1240 printf("SUCCESS\n\n");
1241 }
1242
1243 printf("Stress with random payloads of different encoding:\n");
1244 {
1245 int i,j,len,where;
1246 unsigned char *p;
1247 char buf[1024];
1248 list *ref;
1249 listNode *refnode;
1250
1251 /* Hold temp vars from ziplist */
1252 unsigned char *sstr;
1253 unsigned int slen;
1254 long long sval;
1255
1256 /* In the regression for the cascade bug, it was triggered
1257 * with a random seed of 2. */
1258 srand(2);
1259
1260 for (i = 0; i < 20000; i++) {
1261 zl = ziplistNew();
1262 ref = listCreate();
1263 listSetFreeMethod(ref,sdsfree);
1264 len = rand() % 256;
1265
1266 /* Create lists */
1267 for (j = 0; j < len; j++) {
1268 where = (rand() & 1) ? ZIPLIST_HEAD : ZIPLIST_TAIL;
1269 switch(rand() % 4) {
1270 case 0:
1271 sprintf(buf,"%lld",(0LL + rand()) >> 20);
1272 break;
1273 case 1:
1274 sprintf(buf,"%lld",(0LL + rand()));
1275 break;
1276 case 2:
1277 sprintf(buf,"%lld",(0LL + rand()) << 20);
1278 break;
1279 case 3:
1280 randstring(buf,0,256);
1281 break;
1282 default:
1283 assert(NULL);
1284 }
1285
1286 /* Add to ziplist */
1287 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), where);
1288
1289 /* Add to reference list */
1290 if (where == ZIPLIST_HEAD) {
1291 listAddNodeHead(ref,sdsnew(buf));
1292 } else if (where == ZIPLIST_TAIL) {
1293 listAddNodeTail(ref,sdsnew(buf));
1294 } else {
1295 assert(NULL);
1296 }
1297 }
1298
1299 assert(listLength(ref) == ziplistLen(zl));
1300 for (j = 0; j < len; j++) {
1301 /* Naive way to get elements, but similar to the stresser
1302 * executed from the Tcl test suite. */
1303 p = ziplistIndex(zl,j);
1304 refnode = listIndex(ref,j);
1305
1306 assert(ziplistGet(p,&sstr,&slen,&sval));
1307 if (sstr == NULL) {
1308 sprintf(buf,"%lld",sval);
1309 } else {
1310 memcpy(buf,sstr,slen);
1311 buf[slen] = '\0';
1312 }
1313 assert(strcmp(buf,listNodeValue(refnode)) == 0);
1314 }
1315 zfree(zl);
1316 listRelease(ref);
1317 }
1318 printf("SUCCESS\n\n");
1319 }
1320
1321 printf("Stress with variable ziplist size:\n");
1322 {
1323 stress(ZIPLIST_HEAD,100000,16384,256);
1324 stress(ZIPLIST_TAIL,100000,16384,256);
1325 }
1326
1327 return 0;
1328 }
1329
1330 #endif