changeset 67:b28ac4106d5a HEAD

Allocated memory wasn't aligned according to MEM_ALIGN_SIZE.
author Timo Sirainen <tss@iki.fi>
date Wed, 28 Aug 2002 20:56:41 +0300
parents f239e9a2c96c
children 9381df54c9c3
files src/lib/mempool-alloconly.c src/lib/mempool-system.c
diffstat 2 files changed, 14 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/mempool-alloconly.c	Wed Aug 28 20:41:29 2002 +0300
+++ b/src/lib/mempool-alloconly.c	Wed Aug 28 20:56:41 2002 +0300
@@ -55,7 +55,10 @@
 #define SIZEOF_POOLBLOCK (sizeof(PoolBlock)-MEM_ALIGN_SIZE)
 
 typedef struct {
-	unsigned int size;
+	union {
+		unsigned int size;
+		unsigned char alignment[MEM_ALIGN_SIZE];
+	} size;
 	unsigned char data[MEM_ALIGN_SIZE]; /* variable size */
 } PoolAlloc;
 #define SIZEOF_POOLALLOC (sizeof(PoolAlloc)-MEM_ALIGN_SIZE)
@@ -143,7 +146,7 @@
 
 	alloc = (PoolAlloc *) (apool->block->data +
 			       apool->block->size - apool->block->left);
-	alloc->size = size;
+	alloc->size.size = size;
 
 	apool->block->left -= size + SIZEOF_POOLALLOC;
 	apool->last_alloc_size = size;
@@ -194,7 +197,7 @@
 	} else {
 		/* get old size */
                 alloc = (PoolAlloc *) ((char *) mem - SIZEOF_POOLALLOC);
-		old_size = alloc->size;
+		old_size = alloc->size.size;
 	}
 
 	if (old_size >= size)
--- a/src/lib/mempool-system.c	Wed Aug 28 20:41:29 2002 +0300
+++ b/src/lib/mempool-system.c	Wed Aug 28 20:56:41 2002 +0300
@@ -31,7 +31,10 @@
 #define MAX_ALLOC_SIZE (UINT_MAX - sizeof(unsigned int))
 
 typedef struct {
-	unsigned int size;
+	union {
+		unsigned int size;
+		unsigned char alignment[MEM_ALIGN_SIZE];
+	} size;
 	/* void data[]; */
 } PoolAlloc;
 
@@ -57,7 +60,7 @@
 	alloc = calloc(sizeof(PoolAlloc) + size, 1);
 	if (alloc == NULL)
 		i_panic("pool_system_malloc(): Out of memory");
-	alloc->size = size;
+	alloc->size.size = size;
 
 	return (char *) alloc + sizeof(PoolAlloc);
 }
@@ -81,14 +84,14 @@
 	} else {
 		/* get old size */
 		alloc = (PoolAlloc *) ((char *) mem - sizeof(PoolAlloc));
-		old_size = alloc->size;
+		old_size = alloc->size.size;
 	}
 
         /* alloc & set new size */
 	alloc = realloc(alloc, sizeof(PoolAlloc) + size);
 	if (alloc == NULL)
 		i_panic("pool_system_realloc(): Out of memory");
-	alloc->size = size;
+	alloc->size.size = size;
 
         rmem = (char *) alloc + sizeof(PoolAlloc);
 	if (size > old_size) {
@@ -109,7 +112,7 @@
 	else {
 		/* get old size */
                 alloc = (PoolAlloc *) ((char *) mem - sizeof(PoolAlloc));
-		old_size = alloc->size;
+		old_size = alloc->size.size;
 	}
 
 	if (old_size >= size)