[dovecot-cvs] dovecot/src/lib buffer.c,1.7,1.8 hash.c,1.7,1.8 hash.h,1.4,1.5 imem.c,1.7,1.8 imem.h,1.7,1.8 ioloop-poll.c,1.7,1.8 istream-file.c,1.5,1.6 mempool-alloconly.c,1.16,1.17 mempool-datastack.c,1.5,1.6 mempool-system.c,1.8,1.9 mempool.h,1.8,1.9 Message-Id: <20030110205830.B5B422387C@danu.procontrol.fi>

cras at procontrol.fi cras at procontrol.fi
Fri Jan 10 22:58:30 EET 2003


Update of /home/cvs/dovecot/src/lib
In directory danu:/tmp/cvs-serv2584/lib

Modified Files:
	buffer.c hash.c hash.h imem.c imem.h ioloop-poll.c 
	istream-file.c mempool-alloconly.c mempool-datastack.c 
	mempool-system.c mempool.h ostream-file.c 
Log Message:
Added old_size parameter to p_realloc() - we rarely need it and this way
each allocation takes 8 bytes less memory.



Index: buffer.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/buffer.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- buffer.c	5 Jan 2003 13:09:51 -0000	1.7
+++ buffer.c	10 Jan 2003 20:58:28 -0000	1.8
@@ -54,11 +54,9 @@
 
 	i_assert(size > buf->alloc);
 
+	buf->w_buffer = p_realloc(buf->pool, buf->w_buffer, buf->alloc, size);
 	buf->alloc = size;
-	if (buf->w_buffer == NULL)
-		buf->w_buffer = p_malloc(buf->pool, buf->alloc);
-	else
-		buf->w_buffer = p_realloc(buf->pool, buf->w_buffer, buf->alloc);
+
 	buf->r_buffer = buf->w_buffer;
 	buf->alloced = TRUE;
 }

Index: hash.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/hash.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- hash.c	5 Jan 2003 13:09:51 -0000	1.7
+++ hash.c	10 Jan 2003 20:58:28 -0000	1.8
@@ -88,8 +88,8 @@
 	}
 }
 
-struct hash_table *hash_create(pool_t pool, unsigned int initial_size,
-			       HashFunc hash_func,
+struct hash_table *hash_create(pool_t node_pool, pool_t table_pool,
+			       unsigned int initial_size, HashFunc hash_func,
 			       HashCompareFunc key_compare_func)
 {
 	struct hash_table *table;
@@ -97,7 +97,8 @@
         i_assert(pool != NULL);
 
 	table = p_new(pool, struct hash_table, 1);
-        table->pool = pool;
+        table->node_pool = node_pool;
+        table->table_pool = table_pool;
 	table->size = CLAMP(primes_closest(initial_size),
 			    HASH_TABLE_MIN_SIZE,
 			    HASH_TABLE_MAX_SIZE);

Index: hash.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/hash.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- hash.h	5 Jan 2003 13:09:51 -0000	1.4
+++ hash.h	10 Jan 2003 20:58:28 -0000	1.5
@@ -10,8 +10,8 @@
 /* Create a new hash table. If initial_size is 0, the default value is used.
    If hash_func or key_compare_func is NULL, direct hashing/comparing
    is used. */
-struct hash_table *hash_create(pool_t pool, unsigned int initial_size,
-			       HashFunc hash_func,
+struct hash_table *hash_create(pool_t node_pool, pool_t hash_pool,
+			       unsigned int initial_size, HashFunc hash_func,
 			       HashCompareFunc key_compare_func);
 void hash_destroy(struct hash_table *table);
 

Index: imem.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/imem.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- imem.c	5 Jan 2003 13:09:51 -0000	1.7
+++ imem.c	10 Jan 2003 20:58:28 -0000	1.8
@@ -37,9 +37,9 @@
         p_free(default_pool, mem);
 }
 
-void *i_realloc(void *mem, size_t size)
+void *i_realloc(void *mem, size_t old_size, size_t new_size)
 {
-        return p_realloc(default_pool, mem, size);
+        return p_realloc(default_pool, mem, old_size, new_size);
 }
 
 char *i_strdup(const char *str)

Index: imem.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/imem.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- imem.h	5 Jan 2003 13:09:51 -0000	1.7
+++ imem.h	10 Jan 2003 20:58:28 -0000	1.8
@@ -8,7 +8,7 @@
         ((type *) i_malloc(sizeof(type) * (count)))
 void *i_malloc(size_t size);
 void i_free(void *mem);
-void *i_realloc(void *mem, size_t size);
+void *i_realloc(void *mem, size_t old_size, size_t new_size);
 
 /* string functions */
 char *i_strdup(const char *str);

Index: ioloop-poll.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/ioloop-poll.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- ioloop-poll.c	5 Jan 2003 13:09:51 -0000	1.7
+++ ioloop-poll.c	10 Jan 2003 20:58:28 -0000	1.8
@@ -71,7 +71,8 @@
 void io_loop_handle_add(struct ioloop *ioloop, int fd, int condition)
 {
 	struct ioloop_handler_data *data = ioloop->handler_data;
-	int index, old_size;
+	unsigned int old_size;
+	int index;
 
 	if ((unsigned int) fd >= data->idx_size) {
                 /* grow the fd -> index array */
@@ -79,6 +80,7 @@
 
 		data->idx_size = nearest_power((unsigned int) fd+1);
 		data->fd_index = p_realloc(ioloop->pool, data->fd_index,
+					   sizeof(int) * old_size,
 					   sizeof(int) * data->idx_size);
 		memset(data->fd_index + old_size, 0xff,
 		       sizeof(int) * (data->idx_size-old_size));
@@ -86,8 +88,11 @@
 
 	if (data->fds_pos >= data->fds_size) {
 		/* grow the fd array */
+		old_size = data->fds_size;
+
 		data->fds_size = nearest_power(data->fds_size+1);
 		data->fds = p_realloc(ioloop->pool, data->fds,
+				      sizeof(struct pollfd) * old_size,
 				      sizeof(struct pollfd) * data->fds_size);
 	}
 

Index: istream-file.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/istream-file.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- istream-file.c	7 Jan 2003 17:44:23 -0000	1.5
+++ istream-file.c	10 Jan 2003 20:58:28 -0000	1.6
@@ -97,11 +97,15 @@
 static void i_stream_grow_buffer(struct _istream *stream, size_t bytes)
 {
 	struct file_istream *fstream = (struct file_istream *) stream;
+	size_t old_size;
+
+	old_size = stream->buffer_size;
 
 	stream->buffer_size = stream->pos + bytes;
-	stream->buffer_size =
-		stream->buffer_size <= I_STREAM_MIN_SIZE ? I_STREAM_MIN_SIZE :
-		nearest_power(stream->buffer_size);
+	if (stream->buffer_size <= I_STREAM_MIN_SIZE)
+		stream->buffer_size = I_STREAM_MIN_SIZE;
+	else
+		stream->buffer_size = nearest_power(stream->buffer_size);
 
 	if (fstream->max_buffer_size > 0 &&
 	    stream->buffer_size > fstream->max_buffer_size)
@@ -109,7 +113,7 @@
 
 	stream->buffer = stream->w_buffer =
 		p_realloc(stream->iostream.pool, stream->w_buffer,
-			  stream->buffer_size);
+			  old_size, stream->buffer_size);
 }
 
 static void i_stream_compress(struct _istream *stream)

Index: mempool-alloconly.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/mempool-alloconly.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- mempool-alloconly.c	5 Jan 2003 13:09:51 -0000	1.16
+++ mempool-alloconly.c	10 Jan 2003 20:58:28 -0000	1.17
@@ -57,20 +57,12 @@
 #define POOL_BLOCK_DATA(block) \
 	((char *) (block) + SIZEOF_POOLBLOCK)
 
-struct pool_alloc {
-	union {
-		size_t size;
-		unsigned char alignment[MEM_ALIGN_SIZE];
-	} size;
-	unsigned char data[MEM_ALIGN_SIZE]; /* variable size */
-};
-#define SIZEOF_POOLALLOC (sizeof(struct pool_alloc)-MEM_ALIGN_SIZE)
-
 static void pool_alloconly_ref(pool_t pool);
 static void pool_alloconly_unref(pool_t pool);
 static void *pool_alloconly_malloc(pool_t pool, size_t size);
 static void pool_alloconly_free(pool_t pool, void *mem);
-static void *pool_alloconly_realloc(pool_t pool, void *mem, size_t size);
+static void *pool_alloconly_realloc(pool_t pool, void *mem,
+				    size_t old_size, size_t new_size);
 static void pool_alloconly_clear(pool_t pool);
 
 static void block_alloc(struct alloconly_pool *pool, size_t size);
@@ -160,25 +152,24 @@
 static void *pool_alloconly_malloc(pool_t pool, size_t size)
 {
 	struct alloconly_pool *apool = (struct alloconly_pool *) pool;
-	struct pool_alloc *alloc;
+	void *mem;
 
 	if (size == 0 || size > SSIZE_T_MAX)
 		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);
 
 	size = MEM_ALIGN(size);
 
-	if (apool->block->left < size + SIZEOF_POOLALLOC) {
+	if (apool->block->left < size) {
 		/* we need a new block */
 		block_alloc(apool, size);
 	}
 
-	alloc = (struct pool_alloc *) (POOL_BLOCK_DATA(apool->block) +
-				       apool->block->size - apool->block->left);
-	alloc->size.size = size;
+	mem = POOL_BLOCK_DATA(apool->block) +
+		(apool->block->size - apool->block->left);
 
-	apool->block->left -= size + SIZEOF_POOLALLOC;
+	apool->block->left -= size;
 	apool->block->last_alloc_size = size;
-	return alloc->data;
+	return mem;
 }
 
 static void pool_alloconly_free(pool_t pool __attr_unused__,
@@ -206,39 +197,34 @@
 	return FALSE;
 }
 
-static void *pool_alloconly_realloc(pool_t pool, void *mem, size_t size)
+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 pool_alloc *alloc;
 	unsigned char *new_mem;
-	size_t old_size;
 
-	if (size == 0 || size > SSIZE_T_MAX)
-		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);
+	if (new_size == 0 || new_size > SSIZE_T_MAX)
+		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size);
 
 	if (mem == NULL)
-		return pool_alloconly_malloc(pool, size);
-
-	/* get old size */
-	alloc = (struct pool_alloc *) ((char *) mem - SIZEOF_POOLALLOC);
-	old_size = alloc->size.size;
+		return pool_alloconly_malloc(pool, new_size);
 
-	if (size <= old_size)
+	if (new_size <= old_size)
 		return mem;
 
-	size = MEM_ALIGN(size);
+	new_size = MEM_ALIGN(new_size);
 
 	/* see if we can directly grow it */
-	if (!pool_try_grow(apool, mem, size)) {
+	if (!pool_try_grow(apool, mem, new_size)) {
 		/* slow way - allocate + copy */
-		new_mem = pool_alloconly_malloc(pool, size);
+		new_mem = pool_alloconly_malloc(pool, new_size);
 		memcpy(new_mem, mem, old_size);
 		mem = new_mem;
 	}
 
-	if (size > old_size) {
+	if (old_size < new_size) {
                 /* clear new data */
-		memset((char *) mem + old_size, 0, size - old_size);
+		memset((char *) mem + old_size, 0, new_size - old_size);
 	}
 
         return mem;

Index: mempool-datastack.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/mempool-datastack.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- mempool-datastack.c	5 Jan 2003 13:09:51 -0000	1.5
+++ mempool-datastack.c	10 Jan 2003 20:58:28 -0000	1.6
@@ -28,19 +28,12 @@
 
 #include <stdlib.h>
 
-struct pool_alloc {
-	union {
-		size_t size;
-		unsigned char alignment[MEM_ALIGN_SIZE];
-	} size;
-	/* void data[]; */
-};
-
 static void pool_data_stack_ref(pool_t pool);
 static void pool_data_stack_unref(pool_t pool);
 static void *pool_data_stack_malloc(pool_t pool, size_t size);
 static void pool_data_stack_free(pool_t pool, void *mem);
-static void *pool_data_stack_realloc(pool_t pool, void *mem, size_t size);
+static void *pool_data_stack_realloc(pool_t pool, void *mem,
+				     size_t old_size, size_t new_size);
 static void pool_data_stack_clear(pool_t pool);
 
 static struct pool static_data_stack_pool = {
@@ -67,15 +60,10 @@
 
 static void *pool_data_stack_malloc(pool_t pool __attr_unused__, size_t size)
 {
-	struct pool_alloc *alloc;
-
 	if (size == 0 || size > SSIZE_T_MAX)
 		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);
 
-	alloc = t_malloc0(sizeof(struct pool_alloc) + size);
-	alloc->size.size = size;
-
-	return (char *) alloc + sizeof(struct pool_alloc);
+	return t_malloc0(size);
 }
 
 static void pool_data_stack_free(pool_t pool __attr_unused__,
@@ -83,38 +71,29 @@
 {
 }
 
-static void *pool_data_stack_realloc(pool_t pool __attr_unused__,
-				     void *mem, size_t size)
+static void *pool_data_stack_realloc(pool_t pool __attr_unused__, void *mem,
+				     size_t old_size, size_t new_size)
 {
-	/* @UNSAFE */
-	struct pool_alloc *alloc, *new_alloc;
-        size_t old_size;
-	unsigned char *rmem;
+	void *new_mem;
 
-	if (size == 0 || size > SSIZE_T_MAX)
-		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);
+	/* @UNSAFE */
+	if (new_size == 0 || new_size > SSIZE_T_MAX)
+		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size);
 
 	if (mem == NULL)
-		return pool_data_stack_malloc(pool, size);
-
-	/* get old size */
-	alloc = (struct pool_alloc *)
-		((char *) mem - sizeof(struct pool_alloc));
-	old_size = alloc->size.size;
+		return pool_data_stack_malloc(pool, new_size);
 
-	if (old_size >= size)
+	if (old_size >= new_size)
 		return mem;
 
-	if (!t_try_realloc(alloc, sizeof(struct pool_alloc) + size)) {
-		new_alloc = t_malloc(sizeof(struct pool_alloc) + size);
-		memcpy(new_alloc, alloc, old_size + sizeof(struct pool_alloc));
-		alloc = new_alloc;
+	if (!t_try_realloc(mem, new_size)) {
+		new_mem = t_malloc(new_size);
+		memcpy(new_mem, mem, old_size);
+		mem = new_mem;
 	}
-	alloc->size.size = size;
 
-        rmem = (unsigned char *) alloc + sizeof(struct pool_alloc);
-	memset(rmem + old_size, 0, size-old_size);
-	return rmem;
+	memset((char *) mem + old_size, 0, new_size - old_size);
+	return mem;
 }
 
 static void pool_data_stack_clear(pool_t pool __attr_unused__)

Index: mempool-system.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/mempool-system.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- mempool-system.c	5 Jan 2003 13:09:51 -0000	1.8
+++ mempool-system.c	10 Jan 2003 20:58:28 -0000	1.9
@@ -30,19 +30,12 @@
 
 #include <stdlib.h>
 
-struct pool_alloc {
-	union {
-		size_t size;
-		unsigned char alignment[MEM_ALIGN_SIZE];
-	} size;
-	/* void data[]; */
-};
-
 static void pool_system_ref(pool_t pool);
 static void pool_system_unref(pool_t pool);
 static void *pool_system_malloc(pool_t pool, size_t size);
 static void pool_system_free(pool_t pool, void *mem);
-static void *pool_system_realloc(pool_t pool, void *mem, size_t size);
+static void *pool_system_realloc(pool_t pool, void *mem,
+				 size_t old_size, size_t new_size);
 static void pool_system_clear(pool_t pool);
 
 static struct pool static_system_pool = {
@@ -69,58 +62,40 @@
 
 static void *pool_system_malloc(pool_t pool __attr_unused__, size_t size)
 {
-	struct pool_alloc *alloc;
+	void *mem;
 
 	if (size == 0 || size > SSIZE_T_MAX)
 		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);
 
-	alloc = calloc(sizeof(struct pool_alloc) + size, 1);
-	if (alloc == NULL)
+	mem = calloc(size, 1);
+	if (mem == NULL)
 		i_panic("pool_system_malloc(): Out of memory");
-	alloc->size.size = size;
 
-	return (char *) alloc + sizeof(struct pool_alloc);
+	return mem;
 }
 
 static void pool_system_free(pool_t pool __attr_unused__, void *mem)
 {
 	if (mem != NULL)
-		free((char *) mem - sizeof(struct pool_alloc));
+		free(mem);
 }
 
 static void *pool_system_realloc(pool_t pool __attr_unused__, void *mem,
-				 size_t size)
+				 size_t old_size, size_t new_size)
 {
-	struct pool_alloc *alloc;
-	size_t old_size;
-	char *rmem;
-
-	if (size == 0 || size > SSIZE_T_MAX)
-		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);
-
-	if (mem == NULL) {
-		alloc = NULL;
-		old_size = 0;
-	} else {
-		/* get old size */
-		alloc = (struct pool_alloc *)
-			((char *) mem - sizeof(struct pool_alloc));
-		old_size = alloc->size.size;
-	}
+	if (new_size == 0 || new_size > SSIZE_T_MAX)
+		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size);
 
-        /* alloc & set new size */
-	alloc = realloc(alloc, sizeof(struct pool_alloc) + size);
-	if (alloc == NULL)
+	mem = realloc(mem, new_size);
+	if (mem == NULL)
 		i_panic("pool_system_realloc(): Out of memory");
-	alloc->size.size = size;
 
-        rmem = (char *) alloc + sizeof(struct pool_alloc);
-	if (size > old_size) {
+	if (old_size < new_size) {
                 /* clear new data */
-		memset(rmem + old_size, 0, size-old_size);
+		memset(mem + old_size, 0, new_size - old_size);
 	}
 
-        return rmem;
+        return mem;
 }
 
 static void pool_system_clear(pool_t pool __attr_unused__)

Index: mempool.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/mempool.h,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- mempool.h	5 Jan 2003 13:09:51 -0000	1.8
+++ mempool.h	10 Jan 2003 20:58:28 -0000	1.9
@@ -18,8 +18,9 @@
 	void *(*malloc)(pool_t pool, size_t size);
 	void (*free)(pool_t pool, void *mem);
 
-	/* reallocate the `mem' to be exactly `size' */
-	void *(*realloc)(pool_t pool, void *mem, size_t size);
+	/* memory in old_size..new_size will be zeroed */
+	void *(*realloc)(pool_t pool, void *mem,
+			 size_t old_size, size_t new_size);
 
 	/* Frees all the memory in pool. NOTE: system_pool doesn't support
 	   this and crashes if it's used */
@@ -41,7 +42,8 @@
 #define pool_unref(pool) (pool)->unref(pool)
 
 #define p_malloc(pool, size) (pool)->malloc(pool, size)
-#define p_realloc(pool, mem, size) (pool)->realloc(pool, mem, size)
+#define p_realloc(pool, mem, old_size, new_size) \
+	(pool)->realloc(pool, mem, old_size, new_size)
 #define p_free(pool, mem) (pool)->free(pool, mem)
 
 #define p_clear(pool) (pool)->clear(pool)

Index: ostream-file.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/ostream-file.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- ostream-file.c	8 Jan 2003 18:51:46 -0000	1.4
+++ ostream-file.c	10 Jan 2003 20:58:28 -0000	1.5
@@ -389,7 +389,8 @@
 		return;
 
 	fstream->buffer = p_realloc(fstream->ostream.iostream.pool,
-				    fstream->buffer, size);
+				    fstream->buffer,
+				    fstream->buffer_size, size);
 
 	if (fstream->tail <= fstream->head && !IS_STREAM_EMPTY(fstream)) {
 		head_size = I_MIN(fstream->head, size - fstream->buffer_size);




More information about the dovecot-cvs mailing list