You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2014/06/18 03:08:41 UTC

[lucy-commits] [2/2] git commit: refs/heads/master - Change semantics of Release_All().

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/01820a39
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/01820a39
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/01820a39

Branch: refs/heads/master
Commit: 01820a39ada3276f3be99269d548395bc9a0565e
Parents: c159093
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Sun Apr 27 09:50:56 2014 -0700
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Tue Jun 17 18:05:09 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/01820a39/core/Lucy/Test/Util/TestMemoryPool.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Test/Util/TestMemoryPool.c b/core/Lucy/Test/Util/TestMemoryPool.c
index e86ba18..1c9d3e1 100644
--- a/core/Lucy/Test/Util/TestMemoryPool.c
+++ b/core/Lucy/Test/Util/TestMemoryPool.c
@@ -31,7 +31,7 @@ TestMemPool_new() {
 
 void
 TestMemPool_Run_IMP(TestMemoryPool *self, TestBatchRunner *runner) {
-    TestBatchRunner_Plan(runner, (TestBatch*)self, 4);
+    TestBatchRunner_Plan(runner, (TestBatch*)self, 7);
 
     MemoryPool *mem_pool = MemPool_new(0);
     MemoryPool *other    = MemPool_new(0);
@@ -40,17 +40,25 @@ TestMemPool_Run_IMP(TestMemoryPool *self, TestBatchRunner *runner) {
     char *ptr_a, *ptr_b;
 
     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(runner, MemPool_Get_Consumed(mem_pool), expected,
+                "Round up allocation to word size");
     ptr_b = (char*)MemPool_Grab(mem_pool, 10);
-    TEST_STR_EQ(runner, ptr_b, "foo", "Recycle RAM on Release_All");
+    TEST_INT_EQ(runner, MemPool_Get_Consumed(mem_pool), expected * 2,
+                "Accumulate consumed.");
 
     ptr_a = ivars->buf;
     MemPool_Resize(mem_pool, ptr_b, 6);
-    TEST_TRUE(runner, ivars->buf < ptr_a, "Resize");
+    TEST_TRUE(runner, ivars->buf < ptr_a, "Resize adjusts next allocation");
+    TEST_TRUE(runner, MemPool_Get_Consumed(mem_pool) < expected * 2,
+                "Resize() adjusts `consumed`");
+
+    MemPool_Release_All(mem_pool);
+    TEST_INT_EQ(runner, 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(runner, ovars->buf == ivars->buf, "Eat");

http://git-wip-us.apache.org/repos/asf/lucy/blob/01820a39/core/Lucy/Util/MemoryPool.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Util/MemoryPool.c b/core/Lucy/Util/MemoryPool.c
index ac12587..a2680a5 100644
--- a/core/Lucy/Util/MemoryPool.c
+++ b/core/Lucy/Util/MemoryPool.c
@@ -148,10 +148,13 @@ MemPool_Resize_IMP(MemoryPool *self, void *ptr, size_t new_amount) {
 void
 MemPool_Release_All_IMP(MemoryPool *self) {
     MemoryPoolIVARS *const ivars = MemPool_IVARS(self);
+    DECREF(ivars->arenas);
+    ivars->arenas   = VA_new(16);
     ivars->tick     = -1;
     ivars->buf      = NULL;
     ivars->last_buf = NULL;
     ivars->limit    = NULL;
+    ivars->consumed = 0;
 }
 
 void

http://git-wip-us.apache.org/repos/asf/lucy/blob/01820a39/core/Lucy/Util/MemoryPool.cfh
----------------------------------------------------------------------
diff --git a/core/Lucy/Util/MemoryPool.cfh b/core/Lucy/Util/MemoryPool.cfh
index 8301464..1c97a87 100644
--- a/core/Lucy/Util/MemoryPool.cfh
+++ b/core/Lucy/Util/MemoryPool.cfh
@@ -57,7 +57,7 @@ class Lucy::Util::MemoryPool nickname 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);