[dovecot-cvs] dovecot/src/lib mempool.h, 1.24, 1.25 mempool-system.c, 1.21, 1.22 mempool-system-clean.c, NONE, 1.1 imem.c, 1.12, 1.13 imem.h, 1.13, 1.14 Makefile.am, 1.70, 1.71

tss at dovecot.org tss at dovecot.org
Sun Mar 18 03:57:14 EET 2007


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

Modified Files:
	mempool.h mempool-system.c imem.c imem.h Makefile.am 
Added Files:
	mempool-system-clean.c 
Log Message:
Added system_clean_pool. default_pool is now set statically, so it can be
changed before calling lib_init(). Use malloc_usable_size() if it's
available for asserts and for implementing system_clean_pool.



Index: mempool.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/mempool.h,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -d -r1.24 -r1.25
--- mempool.h	18 Jan 2007 15:33:36 -0000	1.24
+++ mempool.h	18 Mar 2007 01:57:11 -0000	1.25
@@ -48,6 +48,10 @@
 
 /* system_pool uses calloc() + realloc() + free() */
 extern pool_t system_pool;
+extern struct pool static_system_pool;
+/* Similar to pool_alloconly_create_clean(), but uses system_pool to
+   allocate the memory. */
+extern pool_t system_clean_pool;
 
 /* memory allocated from data_stack is valid only until next t_pop() call.
    No checks are performed. */

Index: mempool-system.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/mempool-system.c,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -d -r1.21 -r1.22
--- mempool-system.c	18 Jan 2007 15:33:36 -0000	1.21
+++ mempool-system.c	18 Mar 2007 01:57:11 -0000	1.22
@@ -6,6 +6,9 @@
 #include "mempool.h"
 
 #include <stdlib.h>
+#ifdef HAVE_MALLOC_H
+#  include <malloc.h>
+#endif
 
 #ifdef HAVE_GC_GC_H
 #  include <gc/gc.h>
@@ -38,14 +41,13 @@
 	pool_system_get_max_easy_alloc_size
 };
 
-static struct pool static_system_pool = {
+struct pool static_system_pool = {
 	MEMBER(v) &static_system_pool_vfuncs,
 
 	MEMBER(alloconly_pool) FALSE,
 	MEMBER(datastack_pool) FALSE
 };
 
-
 pool_t system_pool = &static_system_pool;
 
 static const char *pool_system_get_name(pool_t pool __attr_unused__)
@@ -105,6 +107,10 @@
 			       "pool_system_realloc(): Out of memory");
 	}
 
+#if !defined(USE_GC) && defined(HAVE_MALLOC_USABLE_SIZE)
+	i_assert(old_size == (size_t)-1 || mem == NULL ||
+		 old_size <= malloc_usable_size(mem));
+#endif
 	if (old_size < new_size) {
                 /* clear new data */
 		memset((char *) mem + old_size, 0, new_size - old_size);

--- NEW FILE: mempool-system-clean.c ---
/* Copyright (c) 2007 Timo Sirainen */

/* @UNSAFE: whole file */

#include "lib.h"
#include "safe-memset.h"
#include "mempool.h"

#ifdef HAVE_MALLOC_H
#  include <malloc.h>
#endif
#include <stdlib.h>

#ifdef HAVE_GC_GC_H
#  include <gc/gc.h>
#elif defined (HAVE_GC_H)
#  include <gc.h>
#endif

static const char *pool_system_clean_get_name(pool_t pool);
static void pool_system_clean_ref(pool_t pool);
static void pool_system_clean_unref(pool_t *pool);
static void *pool_system_clean_malloc(pool_t pool, size_t size);
static void pool_system_clean_free(pool_t pool, void *mem);
static void *pool_system_clean_realloc(pool_t pool, void *mem,
				       size_t old_size, size_t new_size);
static void pool_system_clean_clear(pool_t pool);
static size_t pool_system_clean_get_max_easy_alloc_size(pool_t pool);

static struct pool_vfuncs static_system_clean_pool_vfuncs = {
	pool_system_clean_get_name,

	pool_system_clean_ref,
	pool_system_clean_unref,

	pool_system_clean_malloc,
	pool_system_clean_free,

	pool_system_clean_realloc,

	pool_system_clean_clear,
	pool_system_clean_get_max_easy_alloc_size
};

static struct pool static_system_clean_pool = {
	MEMBER(v) &static_system_clean_pool_vfuncs,

	MEMBER(alloconly_pool) FALSE,
	MEMBER(datastack_pool) FALSE
};

pool_t system_clean_pool = &static_system_clean_pool;

static const char *pool_system_clean_get_name(pool_t pool __attr_unused__)
{
	return "system clean";
}

static void pool_system_clean_ref(pool_t pool __attr_unused__)
{
}

static void pool_system_clean_unref(pool_t *pool __attr_unused__)
{
}

static size_t mem_get_size(void *mem)
{
#ifdef USE_GC
	return GC_size(mem);
#elif defined(HAVE_MALLOC_USABLE_SIZE)
	return malloc_usable_size(mem);
#else
	return ((size_t *)mem)[-1];
#endif
}

static void *pool_system_clean_malloc(pool_t pool __attr_unused__, size_t size)
{
	void *mem;

	if (size == 0 || size > SSIZE_T_MAX)
		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);

#ifdef USE_GC
	mem = GC_malloc(size);
#else
#ifndef HAVE_MALLOC_USABLE_SIZE
	size += sizeof(size_t);
#endif
	mem = calloc(size, 1);
#endif
	if (mem == NULL) {
		i_fatal_status(FATAL_OUTOFMEM,
			       "pool_system_clean_malloc(): Out of memory");
	}
#if !defined(USE_GC) && !defined(HAVE_MALLOC_USABLE_SIZE)
	{
		size_t *saved_size = mem;

		*saved_size = size;
		mem = saved_size + 1;
	}
#endif
	return mem;
}

static void pool_system_clean_free(pool_t pool __attr_unused__, void *mem)
{
	if (mem != NULL) {
		safe_memset(mem, 0, mem_get_size(mem));
#ifndef USE_GC
#ifndef HAVE_MALLOC_USABLE_SIZE
		mem = (size_t *)mem - 1;
#endif
		free(mem);
#endif
	}
}

static void *pool_system_clean_realloc(pool_t pool __attr_unused__, void *mem,
				       size_t old_size, size_t new_size)
{
	void *new_mem;

	if (new_size == 0 || new_size > SSIZE_T_MAX)
		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size);

	new_mem = pool_system_clean_malloc(pool, new_size);
	if (mem != NULL) {
#if !defined(USE_GC) && defined(HAVE_MALLOC_USABLE_SIZE)
		i_assert(old_size == (size_t)-1 ||
			 old_size <= malloc_usable_size(mem));
#endif
		memcpy(new_mem, mem, mem_get_size(mem));
		pool_system_clean_free(pool, mem);

		if (old_size < new_size) {
			/* clear new data */
			memset((char *)new_mem + old_size, 0,
			       new_size - old_size);
		}
	}

        return new_mem;
}

static void __attr_noreturn__
pool_system_clean_clear(pool_t pool __attr_unused__)
{
	i_panic("pool_system_clean_clear() must not be called");
}

static size_t
pool_system_clean_get_max_easy_alloc_size(pool_t pool __attr_unused__)
{
	return 0;
}

Index: imem.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/imem.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- imem.c	8 Oct 2006 13:28:56 -0000	1.12
+++ imem.c	18 Mar 2007 01:57:11 -0000	1.13
@@ -2,7 +2,7 @@
 
 #include "lib.h"
 
-pool_t default_pool;
+pool_t default_pool = &static_system_pool;
 
 void *i_malloc(size_t size)
 {
@@ -73,8 +73,3 @@
 	va_end(args);
         return ret;
 }
-
-void imem_init(void)
-{
-	default_pool = system_pool;
-}

Index: imem.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/imem.h,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- imem.h	1 Nov 2006 19:19:35 -0000	1.13
+++ imem.h	18 Mar 2007 01:57:11 -0000	1.14
@@ -28,6 +28,4 @@
 
 char *i_strconcat(const char *str1, ...)  __attr_sentinel__ __attr_malloc__;
 
-void imem_init(void);
-
 #endif

Index: Makefile.am
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/Makefile.am,v
retrieving revision 1.70
retrieving revision 1.71
diff -u -d -r1.70 -r1.71
--- Makefile.am	8 Mar 2007 22:04:21 -0000	1.70
+++ Makefile.am	18 Mar 2007 01:57:11 -0000	1.71
@@ -53,6 +53,7 @@
 	mempool-alloconly.c \
 	mempool-datastack.c \
 	mempool-system.c \
+	mempool-system-clean.c \
 	mempool-unsafe-datastack.c \
 	mkdir-parents.c \
 	mmap-anon.c \



More information about the dovecot-cvs mailing list