[evolution-patches] Re: [patch] another 64-bit bug-fix




Hmm, i think there is a G_* macro for struct alignment.  That might be better/more portable than using the attribute stuff.  Otherwise (and maybe infact - *shrug*) yeah its on the right track.

Z

On Mon, 2004-12-20 at 23:07 -0800, David Mosberger wrote:
>>>>> On Wed, 15 Dec 2004 15:48:51 +0800, Not Zed <notzed ximian com> said:

  Not> This code should really be removed, and it should use e-memory,
  Not> and the e-memory stuff fixed instead.

Is this what you had in mind?  If so, please apply.

The patch appears to work fine for me, with the caveat that I only
tested this with evo 2.0.3 (where the files reside in slightly
different places).

Thanks,

	--david

--------------------------------------
 camel/camel-mime-parser.c |  125 +-----------------------------
 libedataserver/e-memory.c |   18 +---
 2 files changed, 16 insertions(+), 127 deletions(-)

ChangeLog

2004-12-20  David Mosberger-Tang  <David Mosberger acm org>

	* libedataserver/e-memory.c (STRUCT_ALIGN): Change from 4 to
	"sizeof (long double)".  That should ensure sufficient
	alignment for all known platforms, including ia64, where "long
	double" needs to be 16-byte aligned.
	(MemPoolNode): Remove "data" member and force STRUCT_ALIGN alignment.
	(MemPoolThresholdNode): Likewise.
	(e_mempool_alloc): Use ((char*)(n + 1) + N) in lieu of &n->data[N]
	to ensure proper alignment.

camel/ChangeLog

2004-12-20  David Mosberger-Tang  <David Mosberger acm org>

	* camel-mime-parser.c: Include "e-memory.h".
	(STRUCT_ALIGN): Remove.
	(MemPoolNode): Likewise.
	(MemPoolThresholdNode): Likewise.
	(MemPool): Likewise.
	(mempool_new): Likewise.
	(mempool_alloc): Likewise.
	(mempool_flush): Likewise.
	(mempool_free): Likewise.
	(struct _header_scan_stack): Change type of "pool" from MemPool to
	EMemPool.
	(folder_pull_part): Call e_mempool_destroy() instead of
	mempool_free().
	(header_append_mempool): Call e_mempool_alloc() instead of
	mempool_alloc().
	(folder_scan_header): Call e_mempool_new() instead of
	mempool_new().

Index: camel/camel-mime-parser.c
===================================================================
RCS file: /cvs/gnome/evolution-data-server/camel/camel-mime-parser.c,v
retrieving revision 1.61
diff -u -r1.61 camel-mime-parser.c
--- camel/camel-mime-parser.c	1 Dec 2004 03:03:09 -0000	1.61
+++ camel/camel-mime-parser.c	21 Dec 2004 07:03:56 -0000
@@ -46,6 +46,8 @@
 #include "camel-stream.h"
 #include "camel-seekable-stream.h"
 
+#include "e-memory.h"
+
 #define r(x) 
 #define h(x) 
 #define c(x) 
@@ -57,8 +59,6 @@
 
 #define MEMPOOL
 
-#define STRUCT_ALIGN 4
-
 #ifdef PURIFY
 int inend_id = -1,
   inbuffer_id = -1;
@@ -71,115 +71,6 @@
 #define _header_scan_state _CamelMimeParserPrivate
 #define _PRIVATE(o) (((CamelMimeParser *)(o))->priv)
 
-#ifdef MEMPOOL
-typedef struct _MemPoolNode {
-	struct _MemPoolNode *next;
-
-	int free;
-	char data[1];
-} MemPoolNode;
-
-typedef struct _MemPoolThresholdNode {
-	struct _MemPoolThresholdNode *next;
-	char data[1];
-} MemPoolThresholdNode;
-
-typedef struct _MemPool {
-	int blocksize;
-	int threshold;
-	struct _MemPoolNode *blocks;
-	struct _MemPoolThresholdNode *threshold_blocks;
-} MemPool;
-
-MemPool *mempool_new(int blocksize, int threshold);
-void *mempool_alloc(MemPool *pool, int size);
-void mempool_flush(MemPool *pool, int freeall);
-void mempool_free(MemPool *pool);
-
-MemPool *mempool_new(int blocksize, int threshold)
-{
-	MemPool *pool;
-
-	pool = g_malloc(sizeof(*pool));
-	if (threshold >= blocksize)
-		threshold = blocksize * 2 / 3;
-	pool->blocksize = blocksize;
-	pool->threshold = threshold;
-	pool->blocks = NULL;
-	pool->threshold_blocks = NULL;
-	return pool;
-}
-
-void *mempool_alloc(MemPool *pool, int size)
-{
-	size = (size + STRUCT_ALIGN) & (~(STRUCT_ALIGN-1));
-	if (size>=pool->threshold) {
-		MemPoolThresholdNode *n;
-
-		n = g_malloc(sizeof(*n) - sizeof(char) + size);
-		n->next = pool->threshold_blocks;
-		pool->threshold_blocks = n;
-		return &n->data[0];
-	} else {
-		MemPoolNode *n;
-
-		n = pool->blocks;
-		while (n) {
-			if (n->free >= size) {
-				n->free -= size;
-				return &n->data[n->free];
-			}
-			n = n->next;
-		}
-
-		n = g_malloc(sizeof(*n) - sizeof(char) + pool->blocksize);
-		n->next = pool->blocks;
-		pool->blocks = n;
-		n->free = pool->blocksize - size;
-		return &n->data[n->free];
-	}
-}
-
-void mempool_flush(MemPool *pool, int freeall)
-{
-	MemPoolThresholdNode *tn, *tw;
-	MemPoolNode *pw, *pn;
-
-	tw = pool->threshold_blocks;
-	while (tw) {
-		tn = tw->next;
-		g_free(tw);
-		tw = tn;
-	}
-	pool->threshold_blocks = NULL;
-
-	if (freeall) {
-		pw = pool->blocks;
-		while (pw) {
-			pn = pw->next;
-			g_free(pw);
-			pw = pn;
-		}
-		pool->blocks = NULL;
-	} else {
-		pw = pool->blocks;
-		while (pw) {
-			pw->free = pool->blocksize;
-			pw = pw->next;
-		}
-	}
-}
-
-void mempool_free(MemPool *pool)
-{
-	if (pool) {
-		mempool_flush(pool, 1);
-		g_free(pool);
-	}
-}
-
-#endif
-
 struct _header_scan_state {
 
     /* global state */
@@ -232,7 +123,7 @@
 	enum _camel_mime_parser_state savestate; /* state at invocation of this part */
 
 #ifdef MEMPOOL
-	MemPool *pool;		/* memory pool to keep track of headers/etc at this level */
+	EMemPool *pool;		/* memory pool to keep track of headers/etc at this level */
 #endif
 	struct _camel_header_raw *headers;	/* headers for this part */
 
@@ -1067,7 +958,7 @@
 		s->parts = h->parent;
 		g_free(h->boundary);
 #ifdef MEMPOOL
-		mempool_free(h->pool);
+		e_mempool_destroy(h->pool);
 #else
 		camel_header_raw_clear(&h->headers);
 #endif
@@ -1175,18 +1066,18 @@
 	content = strchr(header, ':');
 	if (content) {
 		register int len;
-		n = mempool_alloc(h->pool, sizeof(*n));
+		n = e_mempool_alloc(h->pool, sizeof(*n));
 		n->next = NULL;
 		
 		len = content-header;
-		n->name = mempool_alloc(h->pool, len+1);
+		n->name = e_mempool_alloc(h->pool, len+1);
 		memcpy(n->name, header, len);
 		n->name[len] = 0;
 		
 		content++;
 		
 		len = s->outptr - content;
-		n->value = mempool_alloc(h->pool, len+1);
+		n->value = e_mempool_alloc(h->pool, len+1);
 		memcpy(n->value, content, len);
 		n->value[len] = 0;
 		
@@ -1246,7 +1137,7 @@
 
 	h = g_malloc0(sizeof(*h));
 #ifdef MEMPOOL
-	h->pool = mempool_new(8192, 4096);
+	h->pool = e_mempool_new(8192, 4096, E_MEMPOOL_ALIGN_STRUCT);
 #endif
 
 	if (s->parts)
Index: libedataserver/e-memory.c
===================================================================
RCS file: /cvs/gnome/evolution-data-server/libedataserver/e-memory.c,v
retrieving revision 1.4
diff -u -r1.4 e-memory.c
--- libedataserver/e-memory.c	3 Dec 2004 03:33:06 -0000	1.4
+++ libedataserver/e-memory.c	21 Dec 2004 07:03:57 -0000
@@ -100,7 +100,7 @@
 
 /* mempool class */
 
-#define STRUCT_ALIGN (4)
+#define STRUCT_ALIGN (sizeof (long double))
 
 typedef struct _MemChunkFreeNode {
 	struct _MemChunkFreeNode *next;
@@ -357,13 +357,11 @@
 	struct _MemPoolNode *next;
 
 	int free;
-	char data[1];
-} MemPoolNode;
+} MemPoolNode  __attribute__((aligned (STRUCT_ALIGN)));
 
 typedef struct _MemPoolThresholdNode {
 	struct _MemPoolThresholdNode *next;
-	char data[1];
-} MemPoolThresholdNode;
+} MemPoolThresholdNode  __attribute__((aligned (STRUCT_ALIGN)));
 
 typedef struct _EMemPool {
 	int blocksize;
@@ -451,27 +449,27 @@
 	if (size>=pool->threshold) {
 		MemPoolThresholdNode *n;
 
-		n = g_malloc(sizeof(*n) - sizeof(char) + size);
+		n = g_malloc(sizeof(*n) + size);
 		n->next = pool->threshold_blocks;
 		pool->threshold_blocks = n;
-		return &n->data[0];
+		return n + 1;
 	} else {
 		register MemPoolNode *n;
 
 		n = pool->blocks;
 		if (n && n->free >= size) {
 			n->free -= size;
-			return &n->data[n->free];
+			return ((char *) (n + 1)) + n->free;
 		}
 
 		/* maybe we could do some sort of the free blocks based on size, but
 		   it doubt its worth it at all */
 
-		n = g_malloc(sizeof(*n) - sizeof(char) + pool->blocksize);
+		n = g_malloc(sizeof(*n) + pool->blocksize);
 		n->next = pool->blocks;
 		pool->blocks = n;
 		n->free = pool->blocksize - size;
-		return &n->data[n->free];
+		return ((char *) (n + 1)) + n->free;
 	}
 }
 
--
Michael Zucchi <notzed ximian com>
"Ride, Work, Sleep. Beer."
Novell's Evolution and Free Software Developer


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]