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/04/28 01:29:47 UTC
[lucy-commits] [1/2] git commit: refs/heads/LUCY-263-mempool-semantics - Change
semantics of Release_All().
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 <ma...@rectangular.com>
Authored: Sun Apr 27 09:50:56 2014 -0700
Committer: Marvin Humphrey <ma...@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);
[lucy-commits] [2/2] git commit: refs/heads/LUCY-263-mempool-semantics - Remove
unused method MemPool_Eat().
Posted by ma...@apache.org.
Remove unused method MemPool_Eat().
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/763ec7f2
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/763ec7f2
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/763ec7f2
Branch: refs/heads/LUCY-263-mempool-semantics
Commit: 763ec7f2313d36dc984e25c1c1ea9899e81e599e
Parents: 0dcc56e
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Sun Apr 27 10:45:46 2014 -0700
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Sun Apr 27 10:45:46 2014 -0700
----------------------------------------------------------------------
core/Lucy/Test/Util/TestMemoryPool.c | 12 +-----------
core/Lucy/Util/MemoryPool.c | 21 ---------------------
core/Lucy/Util/MemoryPool.cfh | 8 --------
3 files changed, 1 insertion(+), 40 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy/blob/763ec7f2/core/Lucy/Test/Util/TestMemoryPool.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Test/Util/TestMemoryPool.c b/core/Lucy/Test/Util/TestMemoryPool.c
index 1c9d3e1..30199df 100644
--- a/core/Lucy/Test/Util/TestMemoryPool.c
+++ b/core/Lucy/Test/Util/TestMemoryPool.c
@@ -31,12 +31,10 @@ TestMemPool_new() {
void
TestMemPool_Run_IMP(TestMemoryPool *self, TestBatchRunner *runner) {
- TestBatchRunner_Plan(runner, (TestBatch*)self, 7);
+ TestBatchRunner_Plan(runner, (TestBatch*)self, 5);
MemoryPool *mem_pool = MemPool_new(0);
- MemoryPool *other = MemPool_new(0);
MemoryPoolIVARS *const ivars = MemPool_IVARS(mem_pool);
- MemoryPoolIVARS *const ovars = MemPool_IVARS(other);
char *ptr_a, *ptr_b;
ptr_a = (char*)MemPool_Grab(mem_pool, 10);
@@ -57,15 +55,7 @@ TestMemPool_Run_IMP(TestMemoryPool *self, TestBatchRunner *runner) {
TEST_INT_EQ(runner, MemPool_Get_Consumed(mem_pool), 0,
"Release_All() resets `consumed`");
- 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");
- TEST_TRUE(runner, ovars->buf != NULL, "Eat");
-
DECREF(mem_pool);
- DECREF(other);
}
http://git-wip-us.apache.org/repos/asf/lucy/blob/763ec7f2/core/Lucy/Util/MemoryPool.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Util/MemoryPool.c b/core/Lucy/Util/MemoryPool.c
index a2680a5..862fefb 100644
--- a/core/Lucy/Util/MemoryPool.c
+++ b/core/Lucy/Util/MemoryPool.c
@@ -157,24 +157,3 @@ MemPool_Release_All_IMP(MemoryPool *self) {
ivars->consumed = 0;
}
-void
-MemPool_Eat_IMP(MemoryPool *self, MemoryPool *other) {
- MemoryPoolIVARS *const ivars = MemPool_IVARS(self);
- MemoryPoolIVARS *const ovars = MemPool_IVARS(other);
- if (ivars->buf != NULL) {
- THROW(ERR, "Memory pool is not empty");
- }
-
- // Move active arenas from other to self.
- for (int32_t i = 0; i <= ovars->tick; i++) {
- ByteBuf *arena = (ByteBuf*)VA_Shift(ovars->arenas);
- // Maybe displace existing arena.
- VA_Store(ivars->arenas, i, (Obj*)arena);
- }
- ivars->tick = ovars->tick;
- ivars->last_buf = ovars->last_buf;
- ivars->buf = ovars->buf;
- ivars->limit = ovars->limit;
-}
-
-
http://git-wip-us.apache.org/repos/asf/lucy/blob/763ec7f2/core/Lucy/Util/MemoryPool.cfh
----------------------------------------------------------------------
diff --git a/core/Lucy/Util/MemoryPool.cfh b/core/Lucy/Util/MemoryPool.cfh
index 1c97a87..684895e 100644
--- a/core/Lucy/Util/MemoryPool.cfh
+++ b/core/Lucy/Util/MemoryPool.cfh
@@ -62,14 +62,6 @@ class Lucy::Util::MemoryPool nickname MemPool
void
Release_All(MemoryPool *self);
- /** Take ownership of all the arenas in another MemoryPool. Can only be
- * called when the original memory pool has no outstanding allocations,
- * typically just after a call to Release_All. The purpose is to support
- * bulk reallocation.
- */
- void
- Eat(MemoryPool *self, MemoryPool *other);
-
size_t
Get_Consumed(MemoryPool *self);