]> git.saurik.com Git - redis.git/blobdiff - zipmap.c
fix some warnings
[redis.git] / zipmap.c
index 7f6268b4827fd1f48b8a68901bb24a1b2852a4f1..35faeabefa78434046e9cc6d922389255ec6d04d 100644 (file)
--- a/zipmap.c
+++ b/zipmap.c
@@ -136,14 +136,15 @@ static unsigned int zipmapEncodeLength(unsigned char *p, unsigned int len) {
  * reallocate the original zipmap to make room for more entries. */
 static unsigned char *zipmapLookupRaw(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned int *totlen) {
     unsigned char *p = zm+1, *k = NULL;
-    unsigned int l;
+    unsigned int l,llen;
 
     while(*p != ZIPMAP_END) {
         unsigned char free;
 
         /* Match or skip the key */
         l = zipmapDecodeLength(p);
-        if (k == NULL && l == klen && !memcmp(p+1,key,l)) {
+        llen = zipmapEncodeLength(NULL,l);
+        if (k == NULL && l == klen && !memcmp(p+llen,key,l)) {
             /* Only return when the user doesn't care
              * for the total length of the zipmap. */
             if (totlen != NULL) {
@@ -152,7 +153,7 @@ static unsigned char *zipmapLookupRaw(unsigned char *zm, unsigned char *key, uns
                 return p;
             }
         }
-        p += zipmapEncodeLength(NULL,l) + l;
+        p += llen+l;
         /* Skip the value as well */
         l = zipmapDecodeLength(p);
         p += zipmapEncodeLength(NULL,l);
@@ -175,7 +176,6 @@ static unsigned long zipmapRequiredLength(unsigned int klen, unsigned int vlen)
 /* Return the total amount used by a key (encoded length + payload) */
 static unsigned int zipmapRawKeyLength(unsigned char *p) {
     unsigned int l = zipmapDecodeLength(p);
-    
     return zipmapEncodeLength(NULL,l) + l;
 }
 
@@ -208,7 +208,7 @@ static inline unsigned char *zipmapResize(unsigned char *zm, unsigned int len) {
  * If 'update' is not NULL, *update is set to 1 if the key was
  * already preset, otherwise to 0. */
 unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char *val, unsigned int vlen, int *update) {
-    unsigned int zmlen;
+    unsigned int zmlen, offset;
     unsigned int freelen, reqlen = zipmapRequiredLength(klen,vlen);
     unsigned int empty, vempty;
     unsigned char *p;
@@ -230,27 +230,34 @@ unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int kle
         if (update) *update = 1;
         freelen = zipmapRawEntryLength(p);
         if (freelen < reqlen) {
-            /* Move remaining entries to the current position, so this
-             * pair can be appended. Note: the +1 in memmove is caused
-             * by the end-of-zipmap byte. */
-            memmove(p, p+freelen, zmlen-((p-zm)+freelen+1));
+            /* Store the offset of this key within the current zipmap, so
+             * it can be resized. Then, move the tail backwards so this
+             * pair fits at the current position. */
+            offset = p-zm;
             zm = zipmapResize(zm, zmlen-freelen+reqlen);
-            p = zm+zmlen-1-freelen;
-            zmlen = zmlen-1-freelen+reqlen;
+            p = zm+offset;
+
+            /* The +1 in the number of bytes to be moved is caused by the
+             * end-of-zipmap byte. Note: the *original* zmlen is used. */
+            memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1));
+            zmlen = zmlen-freelen+reqlen;
             freelen = reqlen;
         }
     }
 
-    /* Ok we have a suitable block where to write the new key/value
-     * entry. */
+    /* We now have a suitable block where the key/value entry can
+     * be written. If there is too much free space, move the tail
+     * of the zipmap a few bytes to the front and shrink the zipmap,
+     * as we want zipmaps to be very space efficient. */
     empty = freelen-reqlen;
-    /* If there is too much free space mark it as a free block instead
-     * of adding it as trailing empty space for the value, as we want
-     * zipmaps to be very space efficient. */
     if (empty >= ZIPMAP_VALUE_MAX_FREE) {
-        memmove(p+reqlen, p+freelen, zmlen-((p-zm)+freelen+1));
+        /* First, move the tail <empty> bytes to the front, then resize
+         * the zipmap to be <empty> bytes smaller. */
+        offset = p-zm;
+        memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1));
         zmlen -= empty;
         zm = zipmapResize(zm, zmlen);
+        p = zm+offset;
         vempty = 0;
     } else {
         vempty = empty;
@@ -396,7 +403,6 @@ int main(void) {
     zm = zipmapSet(zm,(unsigned char*) "surname",7, (unsigned char*) "foo",3,NULL);
     zm = zipmapSet(zm,(unsigned char*) "age",3, (unsigned char*) "foo",3,NULL);
     zipmapRepr(zm);
-    exit(1);
 
     zm = zipmapSet(zm,(unsigned char*) "hello",5, (unsigned char*) "world!",6,NULL);
     zm = zipmapSet(zm,(unsigned char*) "foo",3, (unsigned char*) "bar",3,NULL);
@@ -409,6 +415,21 @@ int main(void) {
     zipmapRepr(zm);
     zm = zipmapDel(zm,(unsigned char*) "new",3,NULL);
     zipmapRepr(zm);
+
+    printf("\nLook up large key:\n");
+    {
+        unsigned char buf[512];
+        unsigned char *value;
+        unsigned int vlen, i;
+        for (i = 0; i < 512; i++) buf[i] = 'a';
+
+        zm = zipmapSet(zm,buf,512,(unsigned char*) "long",4,NULL);
+        if (zipmapGet(zm,buf,512,&value,&vlen)) {
+            printf("  <long key> is associated to the %d bytes value: %.*s\n",
+                vlen, vlen, value);
+        }
+    }
+
     printf("\nPerform a direct lookup:\n");
     {
         unsigned char *value;