aboutsummaryrefslogtreecommitdiff
path: root/src/common/mempool.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2007-04-11 13:18:25 +0000
committerNick Mathewson <nickm@torproject.org>2007-04-11 13:18:25 +0000
commit38a5f0950250ad21e48ad60c1036621c58ecd9d4 (patch)
treea792258f5224faff370d13bc0fd541211da439fb /src/common/mempool.c
parent1c8f9b319b6cd94c1c00731349c9dda6ec723979 (diff)
downloadtor-38a5f0950250ad21e48ad60c1036621c58ecd9d4.tar
tor-38a5f0950250ad21e48ad60c1036621c58ecd9d4.tar.gz
r12349@catbus: nickm | 2007-04-11 09:18:15 -0400
Add code to shrink the cell memory pool by discarding empty chunks that have been empty for the last 60 seconds. Also, instead of having test.c duplicate declarations for exposed functions, put them inside #ifdef foo_PRIVATE blocks in the headers. This prevents bugs where test.c gets out of sync. svn:r9944
Diffstat (limited to 'src/common/mempool.c')
-rw-r--r--src/common/mempool.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/common/mempool.c b/src/common/mempool.c
index 06f23ad94..ca196530d 100644
--- a/src/common/mempool.c
+++ b/src/common/mempool.c
@@ -210,6 +210,8 @@ mp_pool_get(mp_pool_t *pool)
ASSERT(!chunk->prev);
--pool->n_empty_chunks;
+ if (pool->n_empty_chunks < pool->min_empty_chunks)
+ pool->min_empty_chunks = pool->n_empty_chunks;
} else {
/* We have no used or empty chunks: allocate a new chunk. */
chunk = mp_chunk_new(pool);
@@ -375,11 +377,20 @@ mp_pool_new(size_t item_size, size_t chunk_capacity)
}
/** If there are more than <b>n</b> empty chunks in <b>pool</b>, free the
- * exces ones that have been empty for the longest. */
+ * exces ones that have been empty for the longest. (If <b>n</b> is less
+ * than zero, free only empty chunks that were not used since the last
+ * call to mp_pool_clean(), leaving only -<b>n</b>.) */
void
mp_pool_clean(mp_pool_t *pool, int n)
{
mp_chunk_t *chunk, **first_to_free;
+ if (n < 0) {
+ n = pool->min_empty_chunks + (-n);
+ if (n < pool->n_empty_chunks)
+ pool->min_empty_chunks = n;
+ }
+ ASSERT(n>=0);
+
first_to_free = &pool->empty_chunks;
while (*first_to_free && n > 0) {
first_to_free = &(*first_to_free)->next;