lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject [lucy-commits] git commit: refs/heads/LUCY-263-mempool-semantics-0.3 - Change semantics of Release_All().
Date Tue, 29 Apr 2014 00:29:58 GMT
Repository: lucy
Updated Branches:
  refs/heads/LUCY-263-mempool-semantics-0.3 [created] 432f79efc


Change semantics of Release_All().

At present, Release_All() does not free previous allocations, but allows
reuse.  However, all the existing usages of Release_All() expect
the memory to be freed, resulting in a number of memory leaks.  To fix
these problems, change the behavior Release_All() to free memory instead
of reuse.


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/432f79ef
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/432f79ef
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/432f79ef

Branch: refs/heads/LUCY-263-mempool-semantics-0.3
Commit: 432f79efcd6797f4eca047c2dfe02a55cd5d8407
Parents: e6e6125
Author: Marvin Humphrey <marvin@rectangular.com>
Authored: Sun Apr 27 09:50:56 2014 -0700
Committer: Marvin Humphrey <marvin@rectangular.com>
Committed: Mon Apr 28 17:00:52 2014 -0700

----------------------------------------------------------------------
 core/Lucy/Test/Util/TestMemoryPool.c | 22 +++++++++++++++-------
 core/Lucy/Util/MemoryPool.c          |  3 +++
 core/Lucy/Util/MemoryPool.cfh        |  2 +-
 3 files changed, 19 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/432f79ef/core/Lucy/Test/Util/TestMemoryPool.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Test/Util/TestMemoryPool.c b/core/Lucy/Test/Util/TestMemoryPool.c
index 35551ed..3c11a5c 100644
--- a/core/Lucy/Test/Util/TestMemoryPool.c
+++ b/core/Lucy/Test/Util/TestMemoryPool.c
@@ -24,7 +24,7 @@
 
 void
 TestMemPool_run_tests() {
-    TestBatch  *batch     = TestBatch_new(4);
+    TestBatch  *batch     = TestBatch_new(7);
     MemoryPool *mem_pool  = MemPool_new(0);
     MemoryPool *other     = MemPool_new(0);
     char *ptr_a, *ptr_b;
@@ -32,17 +32,25 @@ TestMemPool_run_tests() {
     TestBatch_Plan(batch);
 
     ptr_a = (char*)MemPool_Grab(mem_pool, 10);
-    strcpy(ptr_a, "foo");
-    MemPool_Release_All(mem_pool);
-
+    size_t expected = sizeof(void*) == 8 ? 16 : 12;
+    TEST_INT_EQ(batch, MemPool_Get_Consumed(mem_pool), expected,
+                "Round up allocation to word size");
     ptr_b = (char*)MemPool_Grab(mem_pool, 10);
-    TEST_STR_EQ(batch, ptr_b, "foo", "Recycle RAM on Release_All");
+    TEST_INT_EQ(batch, MemPool_Get_Consumed(mem_pool), expected * 2,
+                "Accumulate consumed.");
 
     ptr_a = mem_pool->buf;
     MemPool_Resize(mem_pool, ptr_b, 6);
-    TEST_TRUE(batch, mem_pool->buf < ptr_a, "Resize");
+    TEST_TRUE(batch, mem_pool->buf < ptr_a, "Resize adjusts next allocation");
+    TEST_TRUE(batch, MemPool_Get_Consumed(mem_pool) < expected * 2,
+                "Resize() adjusts `consumed`");
+
+    MemPool_Release_All(mem_pool);
+    TEST_INT_EQ(batch, MemPool_Get_Consumed(mem_pool), 0,
+                "Release_All() resets `consumed`");
 
-    ptr_a = (char*)MemPool_Grab(other, 20);
+    ptr_a = (char*)MemPool_Grab(mem_pool, 20);
+    ptr_b = (char*)MemPool_Grab(other, 20);
     MemPool_Release_All(other);
     MemPool_Eat(other, mem_pool);
     TEST_TRUE(batch, other->buf == mem_pool->buf, "Eat");

http://git-wip-us.apache.org/repos/asf/lucy/blob/432f79ef/core/Lucy/Util/MemoryPool.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Util/MemoryPool.c b/core/Lucy/Util/MemoryPool.c
index d2bfebd..43f5a8c 100644
--- a/core/Lucy/Util/MemoryPool.c
+++ b/core/Lucy/Util/MemoryPool.c
@@ -142,10 +142,13 @@ MemPool_resize(MemoryPool *self, void *ptr, size_t new_amount) {
 
 void
 MemPool_release_all(MemoryPool *self) {
+    DECREF(self->arenas);
+    self->arenas   = VA_new(16);
     self->tick     = -1;
     self->buf      = NULL;
     self->last_buf = NULL;
     self->limit    = NULL;
+    self->consumed = 0;
 }
 
 void

http://git-wip-us.apache.org/repos/asf/lucy/blob/432f79ef/core/Lucy/Util/MemoryPool.cfh
----------------------------------------------------------------------
diff --git a/core/Lucy/Util/MemoryPool.cfh b/core/Lucy/Util/MemoryPool.cfh
index 61934d1..e9a967f 100644
--- a/core/Lucy/Util/MemoryPool.cfh
+++ b/core/Lucy/Util/MemoryPool.cfh
@@ -57,7 +57,7 @@ class Lucy::Util::MemoryPool cnick MemPool
     void
     Resize(MemoryPool *self, void *ptr, size_t revised_amount);
 
-    /** Tell the pool to consider all previous allocations released.
+    /** Bulk free all previous allocations.
      */
     void
     Release_All(MemoryPool *self);


Mime
View raw message