]> mj.ucw.cz Git - libucw.git/commitdiff
Mempool: Implemented mp_shrink().
authorPavel Charvat <pchar@ucw.cz>
Tue, 20 May 2014 11:36:32 +0000 (13:36 +0200)
committerPavel Charvat <pchar@ucw.cz>
Tue, 20 May 2014 11:36:32 +0000 (13:36 +0200)
ucw/mempool.c
ucw/mempool.h

index 9e5d62b0022909d20dc1254fbddd0c1f4128b47f..1048686f6801f98a11773e61dd0a1fadc9568905 100644 (file)
@@ -230,6 +230,19 @@ mp_total_size(struct mempool *pool)
   return pool->total_size;
 }
 
+void
+mp_shrink(struct mempool *pool, u64 min_total_size)
+{
+  while (1)
+    {
+      struct mempool_chunk *chunk = pool->unused;
+      if (!chunk || pool->total_size - (chunk->size + MP_CHUNK_TAIL) < min_total_size)
+       break;
+      pool->unused = chunk->next;
+      mp_free_chunk(pool, chunk);
+    }
+}
+
 void *
 mp_alloc_internal(struct mempool *pool, uns size)
 {
index 9427a8ac122e3e99f51bb101a8eba0ef9024c710..b44b955a452713c35afffcfe94cdad1c2c8fd028 100644 (file)
@@ -137,6 +137,11 @@ void mp_stats(struct mempool *pool, struct mempool_stats *stats);
  **/
 u64 mp_total_size(struct mempool *pool);
 
+/**
+ * Release unused chunks of memory reserved for further allocation
+ * requests, but stop if mp_total_size() would drop below @min_total_size.
+ **/
+void mp_shrink(struct mempool *pool, u64 min_total_size);
 
 /***
  * [[alloc]]