[Flac-dev] Patch : Fix pointer cast warning

Erik de Castro Lopo erikd-flac at mega-nerd.com
Sun Apr 15 00:09:45 PDT 2007


Hi all,

The code currently in CVS gives a compile warning:

    memory.c: In function 'FLAC__memory_alloc_aligned':
    memory.c:52: warning: cast from pointer to integer of different size
    memory.c:52: warning: cast to pointer from integer of different size

The patch below fixes this warning by detecting the sizeof (void*)
at configure time and then using that for a little pointer arithmetic.

Cheers,
Erik

diff -u -r1.141 configure.in
--- configure.in	14 Feb 2007 06:12:24 -0000	1.141
+++ configure.in	15 Apr 2007 07:06:43 -0000
@@ -41,6 +41,8 @@
 AC_SYS_LARGEFILE
 AC_FUNC_FSEEKO
 
+AC_CHECK_SIZEOF(void*,0)
+
 #@@@ new name is AC_CONFIG_HEADERS
 AM_CONFIG_HEADER(config.h)
 
diff -u -r1.19 memory.c
--- src/libFLAC/memory.c	2 Feb 2007 06:58:22 -0000	1.19
+++ src/libFLAC/memory.c	15 Apr 2007 07:06:44 -0000
@@ -38,25 +38,26 @@
 
 void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address)
 {
-	void *x;
+	char *x;
+	unsigned increment;
 
 	FLAC__ASSERT(0 != aligned_address);
 
 #ifdef FLAC__ALIGN_MALLOC_DATA
 	/* align on 32-byte (256-bit) boundary */
 	x = malloc(bytes+31);
-	/* there's got to be a better way to do this right for all archs */
-	if(sizeof(void*) == sizeof(unsigned))
-		*aligned_address = (void*)(((unsigned)x + 31) & -32);
-	else if(sizeof(void*) == sizeof(FLAC__uint64))
-		*aligned_address = (void*)(((FLAC__uint64)x + 31) & (FLAC__uint64)(-((FLAC__int64)32)));
-	else
-		return 0;
+#if SIZEOF_VOIDP == 4
+	increment = ((unsigned) (32 - (((unsigned) x) & 31))) & 31;
+#elif SIZEOF_VOIDP == 8
+	increment = ((unsigned) (32 - (((FLAC__uint64) x) & 31))) & 31;
+#else
+#endif
+	*aligned_address = (void *) (x + increment);
 #else
 	x = malloc(bytes);
-	*aligned_address = x;
+	*aligned_address = (void *) x;
 #endif
-	return x;
+	return (void *) x;
 }
 
 FLAC__bool FLAC__memory_alloc_aligned_int32_array(unsigned elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer)

-- 
+-----------------------------------------------------------+
  Erik de Castro Lopo
+-----------------------------------------------------------+
"If trees could scream, would we be so cavalier about cutting them
down? We might, if they screamed all the time, for no good reason."
-- Jack Handey


More information about the Flac-dev mailing list