/pidgin/main: 1dc7369ff9f9: Trie, memory pool: we don't really n...

Tomasz Wasilczyk twasilczyk at pidgin.im
Tue Mar 25 09:32:49 EDT 2014


Changeset: 1dc7369ff9f9dd0853efa5c3babae195c8bdeadb
Author:	 Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date:	 2014-03-25 14:32 +0100
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/1dc7369ff9f9

Description:

Trie, memory pool: we don't really need block_size pref to be that strict

diffstat:

 libpurple/memorypool.c |  40 ++++++++++++++++++++++++----------------
 libpurple/memorypool.h |  10 ++++------
 libpurple/trie.c       |  16 +---------------
 3 files changed, 29 insertions(+), 37 deletions(-)

diffs (230 lines):

diff --git a/libpurple/memorypool.c b/libpurple/memorypool.c
--- a/libpurple/memorypool.c
+++ b/libpurple/memorypool.c
@@ -32,6 +32,8 @@
 #define PURPLE_MEMORY_PADDED(pointer, padding) \
 	(gpointer)(((guintptr)(pointer) - 1) % (padding) + 1)
 
+#define PURPLE_MEMORY_POOL_DEFAULT_BLOCK_SIZE 1024
+
 typedef struct _PurpleMemoryPoolBlock PurpleMemoryPoolBlock;
 
 typedef struct
@@ -62,13 +64,11 @@ purple_memory_pool_block_new(gulong bloc
 
 	total_size = (sizeof(PurpleMemoryPoolBlock) - 1) /
 		PURPLE_MEMORY_POOL_BLOCK_PADDING + 1;
+	g_return_val_if_fail(block_size < G_MAXSIZE - total_size, NULL);
 	total_size += block_size;
 
 	block_raw = g_try_malloc(total_size);
-	if (block_raw == NULL) {
-		g_error("out of memory");
-		return NULL;
-	}
+	g_return_val_if_fail(block_raw != NULL, NULL);
 	block = block_raw;
 
 	/* in fact, we don't set available_ptr padded to
@@ -84,14 +84,13 @@ purple_memory_pool_block_new(gulong bloc
 }
 
 static gpointer
-purple_memory_pool_alloc_impl(PurpleMemoryPool *pool, gulong size, guint alignment)
+purple_memory_pool_alloc_impl(PurpleMemoryPool *pool, gsize size, guint alignment)
 {
 	PurpleMemoryPoolPrivate *priv = PURPLE_MEMORY_POOL_GET_PRIVATE(pool);
 	PurpleMemoryPoolBlock *blk;
 	gpointer mem = NULL;
 
 	g_return_val_if_fail(priv != NULL, NULL);
-	g_return_val_if_fail(size <= priv->block_size, NULL);
 
 	g_warn_if_fail(alignment >= 1);
 	if (alignment < 1)
@@ -110,7 +109,10 @@ purple_memory_pool_alloc_impl(PurpleMemo
 	}
 
 	if (mem == NULL) {
-		blk = purple_memory_pool_block_new(priv->block_size);
+		gsize real_size = priv->block_size;
+		if (real_size < size)
+			real_size = size;
+		blk = purple_memory_pool_block_new(real_size);
 		g_return_val_if_fail(blk != NULL, NULL);
 
 		if (priv->first_block == NULL) {
@@ -140,10 +142,13 @@ purple_memory_pool_alloc_impl(PurpleMemo
  ******************************************************************************/
 
 gpointer
-purple_memory_pool_alloc(PurpleMemoryPool *pool, gulong size, guint alignment)
+purple_memory_pool_alloc(PurpleMemoryPool *pool, gsize size, guint alignment)
 {
 	PurpleMemoryPoolClass *klass;
 
+	if (size == 0)
+		return NULL;
+
 	g_return_val_if_fail(PURPLE_IS_MEMORY_POOL(pool), NULL);
 
 	klass = PURPLE_MEMORY_POOL_GET_CLASS(pool);
@@ -154,10 +159,13 @@ purple_memory_pool_alloc(PurpleMemoryPoo
 }
 
 gpointer
-purple_memory_pool_alloc0(PurpleMemoryPool *pool, gulong size, guint alignment)
+purple_memory_pool_alloc0(PurpleMemoryPool *pool, gsize size, guint alignment)
 {
 	gpointer mem;
 
+	if (size == 0)
+		return NULL;
+
 	mem = purple_memory_pool_alloc(pool, size, alignment);
 	g_return_val_if_fail(mem != NULL, NULL);
 
@@ -171,6 +179,9 @@ purple_memory_pool_free(PurpleMemoryPool
 {
 	PurpleMemoryPoolClass *klass;
 
+	if (mem == NULL)
+		return;
+
 	g_return_if_fail(PURPLE_IS_MEMORY_POOL(pool));
 
 	klass = PURPLE_MEMORY_POOL_GET_CLASS(pool);
@@ -196,11 +207,9 @@ static GObjectClass *parent_class = NULL
 static GParamSpec *properties[PROP_LAST];
 
 PurpleMemoryPool *
-purple_memory_pool_new(gulong block_size)
+purple_memory_pool_new(void)
 {
-	return g_object_new(PURPLE_TYPE_MEMORY_POOL,
-		"block-size", block_size,
-		NULL);
+	return g_object_new(PURPLE_TYPE_MEMORY_POOL, NULL);
 }
 
 static void
@@ -267,9 +276,8 @@ purple_memory_pool_class_init(PurpleMemo
 	klass->palloc = purple_memory_pool_alloc_impl;
 
 	properties[PROP_BLOCK_SIZE] = g_param_spec_ulong("block-size",
-		"Block size", "The size of each block of pool memory. Allocated"
-		" items have to be smaller than this value",
-		1, G_MAXULONG, 1024,
+		"Block size", "The default size of each block of pool memory.",
+		0, G_MAXULONG, PURPLE_MEMORY_POOL_DEFAULT_BLOCK_SIZE,
 		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
 
 	g_object_class_install_properties(obj_class, PROP_LAST, properties);
diff --git a/libpurple/memorypool.h b/libpurple/memorypool.h
--- a/libpurple/memorypool.h
+++ b/libpurple/memorypool.h
@@ -51,7 +51,7 @@ struct _PurpleMemoryPoolClass
 	/*< private >*/
 	GObjectClass parent_class;
 
-	gpointer (*palloc)(PurpleMemoryPool *pool, gulong size, guint alignment);
+	gpointer (*palloc)(PurpleMemoryPool *pool, gsize size, guint alignment);
 	gpointer (*pfree)(PurpleMemoryPool *pool, gpointer mem);
 
 	void (*purple_reserved1)(void);
@@ -67,15 +67,13 @@ purple_memory_pool_get_type(void);
 
 /**
  * purple_memory_pool_new:
- * @blocksize: The size of each block of pool memory. Allocated items have to
- *             be smaller than this value.
  *
  * Creates a new memory pool.
  *
  * Returns: The new #PurpleMemoryPool.
  */
 PurpleMemoryPool *
-purple_memory_pool_new(gulong block_size);
+purple_memory_pool_new(void);
 
 /**
  * purple_memory_pool_alloc:
@@ -89,7 +87,7 @@ purple_memory_pool_new(gulong block_size
  *          a call to purple_memory_pool_free.
  */
 gpointer
-purple_memory_pool_alloc(PurpleMemoryPool *pool, gulong size, guint alignment);
+purple_memory_pool_alloc(PurpleMemoryPool *pool, gsize size, guint alignment);
 
 /**
  * purple_memory_pool_alloc0:
@@ -104,7 +102,7 @@ purple_memory_pool_alloc(PurpleMemoryPoo
  *          a call to purple_memory_pool_free.
  */
 gpointer
-purple_memory_pool_alloc0(PurpleMemoryPool *pool, gulong size, guint alignment);
+purple_memory_pool_alloc0(PurpleMemoryPool *pool, gsize size, guint alignment);
 
 /**
  * purple_memory_pool_free:
diff --git a/libpurple/trie.c b/libpurple/trie.c
--- a/libpurple/trie.c
+++ b/libpurple/trie.c
@@ -32,7 +32,6 @@ typedef struct _PurpleTrieRecord PurpleT
 typedef struct
 {
 	gboolean reset_on_match;
-	gulong block_size;
 
 	PurpleMemoryPool *records_mempool;
 	GSList *records;
@@ -96,7 +95,6 @@ enum
 {
 	PROP_ZERO,
 	PROP_RESET_ON_MATCH,
-	PROP_BLOCK_SIZE,
 	PROP_LAST
 };
 
@@ -115,7 +113,7 @@ purple_trie_init(GTypeInstance *instance
 	PurpleTrie *trie = PURPLE_TRIE(instance);
 	PurpleTriePrivate *priv = PURPLE_TRIE_GET_PRIVATE(trie);
 
-	priv->records_mempool = purple_memory_pool_new(priv->block_size);
+	priv->records_mempool = purple_memory_pool_new();
 }
 
 static void
@@ -140,9 +138,6 @@ purple_trie_get_property(GObject *obj, g
 		case PROP_RESET_ON_MATCH:
 			g_value_set_boolean(value, priv->reset_on_match);
 			break;
-		case PROP_BLOCK_SIZE:
-			g_value_set_ulong(value, priv->block_size);
-			break;
 		default:
 			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
 	}
@@ -159,9 +154,6 @@ purple_trie_set_property(GObject *obj, g
 		case PROP_RESET_ON_MATCH:
 			priv->reset_on_match = g_value_get_boolean(value);
 			break;
-		case PROP_BLOCK_SIZE:
-			priv->block_size = g_value_get_ulong(value);
-			break;
 		default:
 			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
 	}
@@ -186,12 +178,6 @@ purple_trie_class_init(PurpleTrieClass *
 		"ensures, that every match is distinct from each other.", TRUE,
 		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
 
-	properties[PROP_BLOCK_SIZE] = g_param_spec_ulong("block-size",
-		"Block size", "The size of each block of pool memory. Every "
-		"string in trie have to be smaller than this value",
-		1, G_MAXULONG, 1024,
-		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
-
 	g_object_class_install_properties(obj_class, PROP_LAST, properties);
 }
 



More information about the Commits mailing list