/pidgin/main: ca4afaddaffa: Memory pool: fix some issues

Tomasz Wasilczyk twasilczyk at pidgin.im
Thu Mar 27 12:48:48 EDT 2014


Changeset: ca4afaddaffa7c6d3d3792603b5f27af9535d0d3
Author:	 Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date:	 2014-03-27 17:48 +0100
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/ca4afaddaffa

Description:

Memory pool: fix some issues

diffstat:

 libpurple/memorypool.c |  49 ++++++++++++++++++++++++++++---------------------
 libpurple/memorypool.h |  13 +++++++------
 libpurple/trie.c       |   3 ++-
 3 files changed, 37 insertions(+), 28 deletions(-)

diffs (138 lines):

diff --git a/libpurple/memorypool.c b/libpurple/memorypool.c
--- a/libpurple/memorypool.c
+++ b/libpurple/memorypool.c
@@ -51,6 +51,17 @@ struct _PurpleMemoryPoolBlock
 	PurpleMemoryPoolBlock *next;
 };
 
+enum
+{
+	PROP_ZERO,
+	PROP_BLOCK_SIZE,
+	PROP_LAST
+};
+
+static GObjectClass *parent_class = NULL;
+static GParamSpec *properties[PROP_LAST];
+
+
 /*******************************************************************************
  * Memory allocation/deallocation
  ******************************************************************************/
@@ -62,8 +73,10 @@ purple_memory_pool_block_new(gulong bloc
 	PurpleMemoryPoolBlock *block;
 	gsize total_size;
 
-	total_size = (sizeof(PurpleMemoryPoolBlock) - 1) /
-		PURPLE_MEMORY_POOL_BLOCK_PADDING + 1;
+	/* ceil block struct size to the multipy of align */
+	total_size = ((sizeof(PurpleMemoryPoolBlock) - 1) /
+		PURPLE_MEMORY_POOL_BLOCK_PADDING + 1) *
+		sizeof(PurpleMemoryPoolBlock);
 	g_return_val_if_fail(block_size < G_MAXSIZE - total_size, NULL);
 	total_size += block_size;
 
@@ -124,7 +137,7 @@ purple_memory_pool_alloc_impl(PurpleMemo
 		}
 
 		mem = PURPLE_MEMORY_PADDED(blk->available_ptr, alignment);
-		g_assert(mem < blk->end_ptr);
+		g_assert(mem + size < blk->end_ptr);
 		g_assert(mem >= blk->available_ptr); /* gpointer overflow */
 	}
 
@@ -146,6 +159,7 @@ purple_memory_pool_cleanup_impl(PurpleMe
 	g_return_if_fail(priv != NULL);
 
 	blk = priv->first_block;
+	priv->first_block = NULL;
 	while (blk) {
 		PurpleMemoryPoolBlock *next = blk->next;
 		g_free(blk);
@@ -158,6 +172,17 @@ purple_memory_pool_cleanup_impl(PurpleMe
  * API implementation
  ******************************************************************************/
 
+void
+purple_memory_pool_set_block_size(PurpleMemoryPool *pool, gulong block_size)
+{
+	PurpleMemoryPoolPrivate *priv = PURPLE_MEMORY_POOL_GET_PRIVATE(pool);
+
+	g_return_if_fail(priv != NULL);
+
+	priv->block_size = block_size;
+	g_object_notify_by_pspec(G_OBJECT(pool), properties[PROP_BLOCK_SIZE]);
+}
+
 gpointer
 purple_memory_pool_alloc(PurpleMemoryPool *pool, gsize size, guint alignment)
 {
@@ -226,30 +251,12 @@ purple_memory_pool_cleanup(PurpleMemoryP
  * Object stuff
  ******************************************************************************/
 
-enum
-{
-	PROP_ZERO,
-	PROP_BLOCK_SIZE,
-	PROP_LAST
-};
-
-static GObjectClass *parent_class = NULL;
-static GParamSpec *properties[PROP_LAST];
-
 PurpleMemoryPool *
 purple_memory_pool_new(void)
 {
 	return g_object_new(PURPLE_TYPE_MEMORY_POOL, NULL);
 }
 
-PurpleMemoryPool *
-purple_memory_pool_new_sized(gulong block_size)
-{
-	return g_object_new(PURPLE_TYPE_MEMORY_POOL,
-		"block-size", block_size,
-		NULL);
-}
-
 static void
 purple_memory_pool_finalize(GObject *obj)
 {
diff --git a/libpurple/memorypool.h b/libpurple/memorypool.h
--- a/libpurple/memorypool.h
+++ b/libpurple/memorypool.h
@@ -77,14 +77,15 @@ PurpleMemoryPool *
 purple_memory_pool_new(void);
 
 /**
- * purple_memory_pool_new_sized:
+ * purple_memory_pool_set_block_size:
+ * @pool: The memory pool.
+ * @block_size: The new default block size.
  *
- * Creates a new memory pool with non-default block size.
- *
- * Returns: The new #PurpleMemoryPool.
+ * Sets new default block size for a memory pool. You might want to call this
+ * before any allocation, to have it applied to the every created block.
  */
-PurpleMemoryPool *
-purple_memory_pool_new_sized(gulong block_size);
+void
+purple_memory_pool_set_block_size(PurpleMemoryPool *pool, gulong block_size);
 
 /**
  * purple_memory_pool_alloc:
diff --git a/libpurple/trie.c b/libpurple/trie.c
--- a/libpurple/trie.c
+++ b/libpurple/trie.c
@@ -611,7 +611,8 @@ purple_trie_init(GTypeInstance *instance
 
 	priv->records_obj_mempool = purple_memory_pool_new();
 	priv->records_str_mempool = purple_memory_pool_new();
-	priv->states_mempool = purple_memory_pool_new_sized(
+	priv->states_mempool = purple_memory_pool_new();
+	purple_memory_pool_set_block_size(priv->states_mempool,
 		PURPLE_TRIE_STATES_POOL_BLOCK_SIZE);
 }
 



More information about the Commits mailing list