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