]> mj.ucw.cz Git - libucw.git/commitdiff
- lizard_alloc() does not allocate anything if max_len == 0
authorRobert Spalek <robert@ucw.cz>
Fri, 25 Jun 2004 18:41:45 +0000 (18:41 +0000)
committerRobert Spalek <robert@ucw.cz>
Fri, 25 Jun 2004 18:41:45 +0000 (18:41 +0000)
- lizard_realloc() only allows stretching the memory and not shrinking

lib/lizard-safe.c

index 4e07036a67a5a1eed30f3bb27b55a2c1efd62778..218faec2d3bc7436dc00bf8a7a074720479b8c15 100644 (file)
 static void
 lizard_alloc_internal(struct lizard_buffer *buf, uns max_len)
 {
+  if (!max_len)
+  {
+    buf->len = 0;
+    buf->start = NULL;
+    return;
+  }
   buf->len = ALIGN(max_len + 3, PAGE_SIZE);            // +3 due to the unaligned access
   buf->start = mmap(NULL, buf->len + PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   if (buf->start == MAP_FAILED)
@@ -42,7 +48,8 @@ lizard_alloc(uns max_len)
 void
 lizard_free(struct lizard_buffer *buf)
 {
-  munmap(buf->start, buf->len + PAGE_SIZE);
+  if (buf->start)
+    munmap(buf->start, buf->len + PAGE_SIZE);
   unhandle_signal(SIGSEGV, buf->old_sigsegv_handler);
   xfree(buf->old_sigsegv_handler);
   xfree(buf);
@@ -51,7 +58,13 @@ lizard_free(struct lizard_buffer *buf)
 void
 lizard_realloc(struct lizard_buffer *buf, uns max_len)
 {
-  munmap(buf->start, buf->len + PAGE_SIZE);
+  max_len += 3;
+  if (max_len <= buf->len)
+    return;
+  if (max_len < 2*buf->len)                            // to ensure amortized logarithmic complexity
+    max_len = 2*buf->len;
+  if (buf->start)
+    munmap(buf->start - 3, buf->len + PAGE_SIZE);
   lizard_alloc_internal(buf, max_len);
 }