[dovecot-cvs] dovecot/src/lib mempool-alloconly.c, 1.37, 1.38 mempool.h, 1.20, 1.21

tss at dovecot.org tss at dovecot.org
Wed Nov 8 20:00:33 UTC 2006


Update of /var/lib/cvs/dovecot/src/lib
In directory talvi:/tmp/cvs-serv6433

Modified Files:
	mempool-alloconly.c mempool.h 
Log Message:
Added pool_alloconly_create_clean() to create an alloconly pool which clears
the memory it uses before freeing it.



Index: mempool-alloconly.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/mempool-alloconly.c,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -d -r1.37 -r1.38
--- mempool-alloconly.c	10 Sep 2006 17:49:08 -0000	1.37
+++ mempool-alloconly.c	8 Nov 2006 20:00:31 -0000	1.38
@@ -2,6 +2,7 @@
 
 /* @UNSAFE: whole file */
 #include "lib.h"
+#include "safe-memset.h"
 #include "mempool.h"
 
 #include <stdlib.h>
@@ -23,6 +24,7 @@
 	const char *name;
 	size_t base_size;
 #endif
+	bool clean_frees;
 };
 
 struct pool_block {
@@ -118,6 +120,17 @@
 	return &new_apool->pool;
 }
 
+pool_t pool_alloconly_create_clean(const char *name, size_t size)
+{
+	struct alloconly_pool *apool;
+	pool_t pool;
+
+	pool = pool_alloconly_create(name, size);
+	apool = (struct alloconly_pool *)pool;
+	apool->clean_frees = TRUE;
+	return pool;
+}
+
 static void pool_alloconly_destroy(struct alloconly_pool *apool)
 {
 	void *block;
@@ -128,7 +141,10 @@
 	/* destroy the last block */
 	block = apool->block;
 #ifdef DEBUG
-	memset(block, 0xde, SIZEOF_POOLBLOCK + apool->block->size);
+	safe_memset(block, 0xde, SIZEOF_POOLBLOCK + apool->block->size);
+#else
+	if (apool->clean_frees)
+		safe_memset(block, 0, SIZEOF_POOLBLOCK + apool->block->size);
 #endif
 
 #ifndef USE_GC
@@ -139,7 +155,7 @@
 static const char *pool_alloconly_get_name(pool_t pool __attr_unused__)
 {
 #ifdef DEBUG
-	struct alloconly_pool *apool = (struct alloconly_pool *) pool;
+	struct alloconly_pool *apool = (struct alloconly_pool *)pool;
 
 	return apool->name;
 #else
@@ -149,7 +165,7 @@
 
 static void pool_alloconly_ref(pool_t pool)
 {
-	struct alloconly_pool *apool = (struct alloconly_pool *) pool;
+	struct alloconly_pool *apool = (struct alloconly_pool *)pool;
 
 	apool->refcount++;
 }
@@ -200,7 +216,7 @@
 
 static void *pool_alloconly_malloc(pool_t pool, size_t size)
 {
-	struct alloconly_pool *apool = (struct alloconly_pool *) pool;
+	struct alloconly_pool *apool = (struct alloconly_pool *)pool;
 	void *mem;
 
 	if (size == 0 || size > SSIZE_T_MAX)
@@ -223,7 +239,7 @@
 
 static void pool_alloconly_free(pool_t pool, void *mem)
 {
-	struct alloconly_pool *apool = (struct alloconly_pool *) pool;
+	struct alloconly_pool *apool = (struct alloconly_pool *)pool;
 
 	/* we can free only the last allocation */
 	if (POOL_BLOCK_DATA(apool->block) +
@@ -257,7 +273,7 @@
 static void *pool_alloconly_realloc(pool_t pool, void *mem,
 				    size_t old_size, size_t new_size)
 {
-	struct alloconly_pool *apool = (struct alloconly_pool *) pool;
+	struct alloconly_pool *apool = (struct alloconly_pool *)pool;
 	unsigned char *new_mem;
 
 	if (new_size == 0 || new_size > SSIZE_T_MAX)
@@ -284,7 +300,7 @@
 
 static void pool_alloconly_clear(pool_t pool)
 {
-	struct alloconly_pool *apool = (struct alloconly_pool *) pool;
+	struct alloconly_pool *apool = (struct alloconly_pool *)pool;
 	struct pool_block *block;
 	size_t base_size, avail_size;
 
@@ -299,7 +315,10 @@
 		apool->block = block->prev;
 
 #ifdef DEBUG
-		memset(block, 0xde, SIZEOF_POOLBLOCK + block->size);
+		safe_memset(block, 0xde, SIZEOF_POOLBLOCK + block->size);
+#else
+		if (apool->clean_frees)
+			safe_memset(block, 0, SIZEOF_POOLBLOCK + block->size);
 #endif
 #ifndef USE_GC
 		free(block);
@@ -313,15 +332,15 @@
 	base_size = DEFAULT_BASE_SIZE;
 #endif
 	avail_size = apool->block->size - base_size;
-	memset(PTR_OFFSET(POOL_BLOCK_DATA(apool->block), base_size), 0,
-	       avail_size - apool->block->left);
+	safe_memset(PTR_OFFSET(POOL_BLOCK_DATA(apool->block), base_size), 0,
+		    avail_size - apool->block->left);
 	apool->block->left = avail_size;
 	apool->block->last_alloc_size = 0;
 }
 
 static size_t pool_alloconly_get_max_easy_alloc_size(pool_t pool)
 {
-	struct alloconly_pool *apool = (struct alloconly_pool *) pool;
+	struct alloconly_pool *apool = (struct alloconly_pool *)pool;
 
 	return apool->block->left;
 }

Index: mempool.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/mempool.h,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- mempool.h	1 Nov 2006 19:19:35 -0000	1.20
+++ mempool.h	8 Nov 2006 20:00:31 -0000	1.21
@@ -45,6 +45,8 @@
 /* Create a new alloc-only pool. Note that `size' specifies the initial
    malloc()ed block size, part of it is used internally. */
 pool_t pool_alloconly_create(const char *name, size_t size);
+/* Like alloconly pool, but clear the memory before freeing it. */
+pool_t pool_alloconly_create_clean(const char *name, size_t size);
 
 /* When allocating memory from returned pool, the data stack frame must be
    the same as it was when calling this function. pool_unref() also checks



More information about the dovecot-cvs mailing list