lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject [lucy-commits] [1/2] git commit: refs/heads/LUCY-263-mempool-semantics - Change semantics of Release_All().
Date Sun, 27 Apr 2014 23:29:47 GMT
Repository: lucy
Updated Branches:
  refs/heads/LUCY-263-mempool-semantics [created] 763ec7f23


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

Branch: refs/heads/LUCY-263-mempool-semantics
Commit: 0dcc56eb70e5305ab246c7ffae33dfca528fff7c
Parents: d4282a9
Author: Marvin Humphrey <marvin@rectangular.com>
Authored: Sun Apr 27 09:50:56 2014 -0700
Committer: Marvin Humphrey <marvin@rectangular.com>
Committed: Sun Apr 27 10:39:38 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/0dcc56eb/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/0dcc56eb/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/0dcc56eb/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);


Mime
View raw message