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