]>
git.saurik.com Git - redis.git/blob - ziplist.c
c2e56bfb0556123ebfc8b5fddf858c4ab00c7236
1 /* Memory layout of a ziplist, containing "foo", "bar", "quux":
2 * <zlbytes><zllen><len>"foo"<len>"bar"<len>"quux"
4 * <zlbytes> is an unsigned integer to hold the number of bytes that
5 * the ziplist occupies. This is stored to not have to traverse the ziplist
6 * to know the new length when pushing.
8 * <zllen> is the number of items in the ziplist. When this value is
9 * greater than 254, we need to traverse the entire list to know
10 * how many items it holds.
12 * <len> is the number of bytes occupied by a single entry. When this
13 * number is greater than 253, the length will occupy 5 bytes, where
14 * the extra bytes contain an unsigned integer to hold the length.
30 #define ZIP_ENC_SHORT 1
32 #define ZIP_ENC_LLONG 3
33 #define ZIP_ENCODING(p) ((p)[0] >> 6)
35 /* Length encoding for raw entries */
36 #define ZIP_LEN_INLINE 0
37 #define ZIP_LEN_UINT16 1
38 #define ZIP_LEN_UINT32 2
41 #define ZIPLIST_BYTES(zl) (*((unsigned int*)(zl)))
42 #define ZIPLIST_LENGTH(zl) (*((zl)+sizeof(unsigned int)))
43 #define ZIPLIST_HEADER_SIZE (sizeof(unsigned int)+1)
44 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
45 if (ZIPLIST_LENGTH(zl) < (ZIP_END-1)) ZIPLIST_LENGTH(zl)+=incr; }
47 /* Return bytes needed to store integer encoded by 'encoding' */
48 static unsigned int zipEncodingSize(char encoding
) {
49 if (encoding
== ZIP_ENC_SHORT
) {
50 return sizeof(short int);
51 } else if (encoding
== ZIP_ENC_INT
) {
53 } else if (encoding
== ZIP_ENC_LLONG
) {
54 return sizeof(long long);
59 /* Decode the encoded length pointed by 'p'. If a pointer to 'lensize' is
60 * provided, it is set to the number of bytes required to encode the length. */
61 static unsigned int zipDecodeLength(unsigned char *p
, unsigned int *lensize
) {
62 unsigned char encoding
= ZIP_ENCODING(p
), lenenc
;
65 if (encoding
== ZIP_ENC_RAW
) {
66 lenenc
= (p
[0] >> 4) & 0x3;
67 if (lenenc
== ZIP_LEN_INLINE
) {
69 if (lensize
) *lensize
= 1;
70 } else if (lenenc
== ZIP_LEN_UINT16
) {
71 len
= p
[1] | (p
[2] << 8);
72 if (lensize
) *lensize
= 3;
74 len
= p
[1] | (p
[2] << 8) | (p
[3] << 16) | (p
[4] << 24);
75 if (lensize
) *lensize
= 5;
78 len
= zipEncodingSize(encoding
);
79 if (lensize
) *lensize
= 1;
84 /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
85 * the amount of bytes required to encode such a length. */
86 static unsigned int zipEncodeLength(unsigned char *p
, char encoding
, unsigned int rawlen
) {
87 unsigned char len
= 1, lenenc
, buf
[5];
88 if (encoding
== ZIP_ENC_RAW
) {
91 lenenc
= ZIP_LEN_INLINE
;
93 } else if (rawlen
<= 0xffff) {
96 lenenc
= ZIP_LEN_UINT16
;
97 buf
[1] = (rawlen
) & 0xff;
98 buf
[2] = (rawlen
>> 8) & 0xff;
102 lenenc
= ZIP_LEN_UINT32
;
103 buf
[1] = (rawlen
) & 0xff;
104 buf
[2] = (rawlen
>> 8) & 0xff;
105 buf
[3] = (rawlen
>> 16) & 0xff;
106 buf
[4] = (rawlen
>> 24) & 0xff;
108 buf
[0] = (lenenc
<< 4) | (buf
[0] & 0xf);
112 /* Apparently we need to store the length in 'p' */
113 buf
[0] = (encoding
<< 6) | (buf
[0] & 0x3f);
118 /* Check if string pointed to by 'entry' can be encoded as an integer.
119 * Stores the integer value in 'v' and its encoding in 'encoding'.
120 * Warning: this function requires a NULL-terminated string! */
121 static int zipTryEncoding(unsigned char *entry
, long long *v
, char *encoding
) {
125 if (entry
[0] == '-' || (entry
[0] >= '0' && entry
[0] <= '9')) {
126 value
= strtoll(entry
,&eptr
,10);
127 if (eptr
[0] != '\0') return 0;
128 if (value
>= SHRT_MIN
&& value
<= SHRT_MAX
) {
129 *encoding
= ZIP_ENC_SHORT
;
130 } else if (value
>= INT_MIN
&& value
<= INT_MAX
) {
131 *encoding
= ZIP_ENC_INT
;
133 *encoding
= ZIP_ENC_LLONG
;
141 /* Store integer 'value' at 'p', encoded as 'encoding' */
142 static void zipSaveInteger(unsigned char *p
, long long value
, char encoding
) {
146 if (encoding
== ZIP_ENC_SHORT
) {
148 memcpy(p
,&s
,sizeof(s
));
149 } else if (encoding
== ZIP_ENC_INT
) {
151 memcpy(p
,&i
,sizeof(i
));
152 } else if (encoding
== ZIP_ENC_LLONG
) {
154 memcpy(p
,&l
,sizeof(l
));
160 /* Read integer encoded as 'encoding' from 'p' */
161 static long long zipLoadInteger(unsigned char *p
, char encoding
) {
165 if (encoding
== ZIP_ENC_SHORT
) {
166 memcpy(&s
,p
,sizeof(s
));
168 } else if (encoding
== ZIP_ENC_INT
) {
169 memcpy(&i
,p
,sizeof(i
));
171 } else if (encoding
== ZIP_ENC_LLONG
) {
172 memcpy(&l
,p
,sizeof(l
));
180 /* Return the total amount used by an entry (encoded length + payload). */
181 static unsigned int zipRawEntryLength(unsigned char *p
) {
182 unsigned int lensize
, len
;
183 len
= zipDecodeLength(p
, &lensize
);
184 return lensize
+ len
;
187 /* Create a new empty ziplist. */
188 unsigned char *ziplistNew(void) {
189 unsigned int bytes
= ZIPLIST_HEADER_SIZE
+1;
190 unsigned char *zl
= zmalloc(bytes
);
191 ZIPLIST_BYTES(zl
) = bytes
;
192 ZIPLIST_LENGTH(zl
) = 0;
193 zl
[bytes
-1] = ZIP_END
;
197 /* Resize the ziplist. */
198 static unsigned char *ziplistResize(unsigned char *zl
, unsigned int len
) {
199 zl
= zrealloc(zl
,len
);
200 ZIPLIST_BYTES(zl
) = len
;
205 static unsigned char *ziplistHead(unsigned char *zl
) {
206 return zl
+ZIPLIST_HEADER_SIZE
;
209 static unsigned char *ziplistTail(unsigned char *zl
) {
210 unsigned char *p
, *q
;
211 p
= q
= ziplistHead(zl
);
212 while (*p
!= ZIP_END
) {
214 p
+= zipRawEntryLength(p
);
219 unsigned char *ziplistPush(unsigned char *zl
, unsigned char *entry
, unsigned int elen
, int where
) {
220 unsigned int curlen
= ZIPLIST_BYTES(zl
), reqlen
;
222 char encoding
= ZIP_ENC_RAW
;
225 /* See if the entry can be encoded */
226 if (zipTryEncoding(entry
,&value
,&encoding
)) {
227 reqlen
= zipEncodingSize(encoding
);
231 reqlen
+= zipEncodeLength(NULL
,encoding
,elen
);
233 /* Resize the ziplist and move if needed */
234 zl
= ziplistResize(zl
,curlen
+reqlen
);
235 if (where
== ZIPLIST_HEAD
) {
236 p
= zl
+ZIPLIST_HEADER_SIZE
;
238 /* Subtract one because of the ZIP_END bytes */
239 memmove(p
+reqlen
,p
,curlen
-ZIPLIST_HEADER_SIZE
-1);
245 /* Write the entry */
246 p
+= zipEncodeLength(p
,encoding
,elen
);
247 if (encoding
!= ZIP_ENC_RAW
) {
248 zipSaveInteger(p
,value
,encoding
);
250 memcpy(p
,entry
,elen
);
252 ZIPLIST_INCR_LENGTH(zl
,1);
256 unsigned char *ziplistPop(unsigned char *zl
, sds
*target
, int where
) {
257 unsigned int curlen
= ZIPLIST_BYTES(zl
), rawlen
;
258 unsigned int len
, lensize
;
261 if (target
) *target
= NULL
;
263 /* Get pointer to element to remove */
264 p
= (where
== ZIPLIST_HEAD
) ? ziplistHead(zl
) : ziplistTail(zl
);
265 if (*p
== ZIP_END
) return zl
;
266 len
= zipDecodeLength(p
,&lensize
);
268 if (ZIP_ENCODING(p
) == ZIP_ENC_RAW
) {
269 *target
= sdsnewlen(p
+lensize
,len
);
271 value
= zipLoadInteger(p
+lensize
,ZIP_ENCODING(p
));
272 *target
= sdscatprintf(sdsempty(), "%lld", value
);
276 /* Move list to front when popping from the head */
277 rawlen
= lensize
+len
;
278 if (where
== ZIPLIST_HEAD
) {
279 memmove(p
,p
+rawlen
,curlen
-ZIPLIST_HEADER_SIZE
-len
);
282 /* Resize and update length */
283 zl
= ziplistResize(zl
,curlen
-rawlen
);
284 ZIPLIST_INCR_LENGTH(zl
,-1);
288 /* Returns an offset to use for iterating with ziplistNext. */
289 unsigned char *ziplistIndex(unsigned char *zl
, unsigned int index
) {
290 unsigned char *p
= zl
+ZIPLIST_HEADER_SIZE
;
292 for (; i
< index
; i
++) {
293 if (*p
== ZIP_END
) break;
294 p
+= zipRawEntryLength(p
);
299 /* Store entry at current position in sds *value and return pointer
300 * to the next entry. */
301 unsigned char *ziplistNext(unsigned char *p
, unsigned char **q
, unsigned char **entry
, unsigned int *elen
) {
302 unsigned int lensize
;
303 if (*p
== ZIP_END
) return NULL
;
305 *elen
= zipDecodeLength(p
,&lensize
);
308 if (q
!= NULL
) *q
= p
;
309 p
+= zipRawEntryLength(p
);
313 /* Delete a range of entries from the ziplist. */
314 unsigned char *ziplistDeleteRange(unsigned char *zl
, unsigned int index
, unsigned int num
) {
315 unsigned char *p
, *first
= ziplistIndex(zl
, index
);
316 unsigned int i
, deleted
= 0, totlen
, newlen
;
317 for (p
= first
, i
= 0; *p
!= ZIP_END
&& i
< num
; i
++) {
318 p
+= zipRawEntryLength(p
);
324 /* Move current tail to the new tail when there *is* a tail */
325 if (*p
!= ZIP_END
) memmove(first
,p
,ZIPLIST_BYTES(zl
)-(p
-zl
)-1);
327 /* Resize and update length */
328 zl
= ziplistResize(zl
, ZIPLIST_BYTES(zl
)-totlen
);
329 ZIPLIST_INCR_LENGTH(zl
,-deleted
);
334 /* Delete a single entry from the ziplist, pointed to by *p.
335 * Also update *p in place, to be able to iterate over the
336 * ziplist, while deleting entries. */
337 unsigned char *ziplistDelete(unsigned char *zl
, unsigned char **p
) {
338 unsigned int offset
= *p
-zl
, tail
, len
;
339 len
= zipRawEntryLength(*p
);
340 tail
= ZIPLIST_BYTES(zl
)-offset
-len
-1;
342 /* Move current tail to the new tail when there *is* a tail */
343 if (tail
> 0) memmove(*p
,*p
+len
,tail
);
345 /* Resize and update length */
346 zl
= ziplistResize(zl
, ZIPLIST_BYTES(zl
)-len
);
347 ZIPLIST_INCR_LENGTH(zl
,-1);
349 /* Store new pointer to current element in p.
350 * This needs to be done because zl can change on realloc. */
355 void ziplistRepr(unsigned char *zl
) {
356 unsigned char *p
, encoding
;
357 unsigned int l
, lsize
;
360 printf("{total bytes %d} {length %u}\n",ZIPLIST_BYTES(zl
), ZIPLIST_LENGTH(zl
));
362 while(*p
!= ZIP_END
) {
363 l
= zipDecodeLength(p
,&lsize
);
364 printf("{header %u, payload %u} ",lsize
,l
);
365 encoding
= ZIP_ENCODING(p
);
367 if (encoding
== ZIP_ENC_RAW
) {
368 fwrite(p
,l
,1,stdout
);
370 printf("%lld", zipLoadInteger(p
,encoding
));
378 #ifdef ZIPLIST_TEST_MAIN
380 unsigned char *createList() {
381 unsigned char *zl
= ziplistNew();
382 zl
= ziplistPush(zl
, (unsigned char*)"foo", 3, ZIPLIST_TAIL
);
383 zl
= ziplistPush(zl
, (unsigned char*)"quux", 4, ZIPLIST_TAIL
);
384 zl
= ziplistPush(zl
, (unsigned char*)"hello", 5, ZIPLIST_HEAD
);
388 unsigned char *createIntList() {
389 unsigned char *zl
= ziplistNew();
393 zl
= ziplistPush(zl
, buf
, strlen(buf
), ZIPLIST_TAIL
);
394 sprintf(buf
, "128000");
395 zl
= ziplistPush(zl
, buf
, strlen(buf
), ZIPLIST_TAIL
);
396 sprintf(buf
, "-100");
397 zl
= ziplistPush(zl
, buf
, strlen(buf
), ZIPLIST_HEAD
);
398 sprintf(buf
, "4294967296");
399 zl
= ziplistPush(zl
, buf
, strlen(buf
), ZIPLIST_HEAD
);
400 sprintf(buf
, "non integer");
401 zl
= ziplistPush(zl
, buf
, strlen(buf
), ZIPLIST_TAIL
);
402 sprintf(buf
, "much much longer non integer");
403 zl
= ziplistPush(zl
, buf
, strlen(buf
), ZIPLIST_TAIL
);
407 int main(int argc
, char **argv
) {
408 unsigned char *zl
, *p
, *q
, *entry
;
412 zl
= createIntList();
418 zl
= ziplistPop(zl
, &s
, ZIPLIST_TAIL
);
419 printf("Pop tail: %s (length %ld)\n", s
, sdslen(s
));
422 zl
= ziplistPop(zl
, &s
, ZIPLIST_HEAD
);
423 printf("Pop head: %s (length %ld)\n", s
, sdslen(s
));
426 printf("Iterate list from 0 to end:\n");
429 p
= ziplistIndex(zl
, 0);
430 while ((p
= ziplistNext(p
, NULL
, &entry
, &elen
)) != NULL
) {
432 fwrite(entry
,elen
,1,stdout
);
433 printf(" (length %d)\n", elen
);
438 printf("Iterate list from 1 to end:\n");
441 p
= ziplistIndex(zl
, 1);
442 while ((p
= ziplistNext(p
, NULL
, &entry
, &elen
)) != NULL
) {
444 fwrite(entry
,elen
,1,stdout
);
445 printf(" (length %d)\n", elen
);
450 printf("Iterate list from 2 to end:\n");
453 p
= ziplistIndex(zl
, 2);
454 while ((p
= ziplistNext(p
, NULL
, &entry
, &elen
)) != NULL
) {
456 fwrite(entry
,elen
,1,stdout
);
457 printf(" (length %d)\n", elen
);
462 printf("Iterate starting out of range:\n");
465 p
= ziplistIndex(zl
, 3);
466 if (ziplistNext(p
, &entry
, NULL
, &elen
) == NULL
) {
467 printf("No entry\n");
474 printf("Delete inclusive range 0,0:\n");
477 zl
= ziplistDeleteRange(zl
, 0, 1);
481 printf("Delete inclusive range 0,1:\n");
484 zl
= ziplistDeleteRange(zl
, 0, 2);
488 printf("Delete inclusive range 1,2:\n");
491 zl
= ziplistDeleteRange(zl
, 1, 2);
495 printf("Delete with start index out of range:\n");
498 zl
= ziplistDeleteRange(zl
, 5, 1);
502 printf("Delete with num overflow:\n");
505 zl
= ziplistDeleteRange(zl
, 1, 5);
509 printf("Delete foo while iterating:\n");
512 p
= ziplistIndex(zl
, 0);
513 while ((p
= ziplistNext(p
, &q
, &entry
, &elen
)) != NULL
) {
514 if (strncmp("foo", entry
, elen
) == 0) {
515 printf("Delete foo\n");
516 zl
= ziplistDelete(zl
, &q
);
520 fwrite(entry
,elen
,1,stdout
);
521 printf(" (length %d)\n", elen
);