]> mj.ucw.cz Git - libucw.git/commitdiff
- buck2obj_alloc() initially allocates no buffers
authorRobert Spalek <robert@ucw.cz>
Fri, 25 Jun 2004 18:45:56 +0000 (18:45 +0000)
committerRobert Spalek <robert@ucw.cz>
Fri, 25 Jun 2004 18:45:56 +0000 (18:45 +0000)
- buck2obj_realloc() only stretches the main buffer (and not the lizard)
  + it is not exported anymore
- if lizard_decompress_safe() returns EFBIG, its buffer is shrinked

hence buck2obj.c allocates no memory if not needed.  the internal buffer is
allocated when the first non-compact bucket is encountered and lizard's
buffer is allocated when the first compressed bucket is encountered.

lib/buck2obj.c
lib/buck2obj.h

index 6aae928048494d1c4b39389debed31910e267022..e2b42c25ba47c799f949c35a84e2c49892952440 100644 (file)
@@ -33,16 +33,22 @@ static void
 buck2obj_alloc_internal(struct buck2obj_buf *buf, uns max_len)
 {
   buf->max_len = max_len;
+  if (!max_len)
+  {
+    buf->raw_len = 0;
+    buf->raw = NULL;
+    return;
+  }
   buf->raw_len = max_len * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD + MAX_HEADER_SIZE;
   buf->raw = xmalloc(buf->raw_len);
 }
 
 struct buck2obj_buf *
-buck2obj_alloc(uns max_len, struct mempool *mp)
+buck2obj_alloc(struct mempool *mp)
 {
   struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
-  buck2obj_alloc_internal(buf, max_len);
-  buf->lizard = lizard_alloc(max_len);
+  buck2obj_alloc_internal(buf, 0);
+  buf->lizard = lizard_alloc(0);
   buf->mp = mp;
   return buf;
 }
@@ -51,20 +57,21 @@ void
 buck2obj_free(struct buck2obj_buf *buf)
 {
   lizard_free(buf->lizard);
-  xfree(buf->raw);
+  if (buf->raw)
+    xfree(buf->raw);
   xfree(buf);
 }
 
-void
+static void
 buck2obj_realloc(struct buck2obj_buf *buf, uns max_len)
 {
   if (max_len <= buf->max_len)
     return;
-  if (max_len < 2*buf->max_len + 1)            // to ensure amortized logarithmic complexity
-    max_len = 2*buf->max_len + 1;
-  xfree(buf->raw);
+  if (max_len < 2*buf->max_len)                // to ensure amortized logarithmic complexity
+    max_len = 2*buf->max_len;
+  if (buf->raw)
+    xfree(buf->raw);
   buck2obj_alloc_internal(buf, max_len);
-  lizard_realloc(buf->lizard, max_len);
 }
 
 static inline byte *
@@ -171,12 +178,20 @@ obj_read_bucket(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body, u
     {
       len = GET_U32(ptr);
       ptr += 4;
-      int res = lizard_decompress_safe(ptr, buf->lizard, len);
+      int res;
+decompress:
+      res = lizard_decompress_safe(ptr, buf->lizard, len);
       if (res != (int) len)
       {
        if (res >= 0)
          errno = EINVAL;
-       return NULL;
+       else if (errno == EFBIG)
+       {
+         lizard_realloc(buf->lizard, len);
+         goto decompress;
+       }
+       else
+         return NULL;
       }
       ptr = buf->lizard->ptr;
       end = ptr + len;
index ea267723b1530b371aea57b91a7c8b647ff0ec2b..f7fad1b80f16dbfa04187f19899e79ee8ed3fa2a 100644 (file)
@@ -6,11 +6,8 @@
 
 struct buck2obj_buf;
 
-#define        BUCK2OBJ_INITIAL_MAX_LEN        (1<<16)
-
-struct buck2obj_buf *buck2obj_alloc(uns max_len, struct mempool *mp);
+struct buck2obj_buf *buck2obj_alloc(struct mempool *mp);
 void buck2obj_free(struct buck2obj_buf *buf);
-void buck2obj_realloc(struct buck2obj_buf *buf, uns max_len);
 struct odes *obj_read_bucket(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body, uns want_body);
   /* If BCONFIG_CAN_OVERWRITE(body)==2, then the buffer of body has probably
    * been tampered with (unless the bucket is larger than the buffer).  In such