Back-port miscellaneous fixes from internal branch (mostly portability fixes).

These are changes that are in our internal branch, but somehow were skipped
so far. It was done using visual comparison of the branches --
it is likely that changes are being carried on the wrong way
This commit is contained in:
José Fonseca
2008-01-17 13:39:14 +09:00
parent b016f0adba
commit 271f9dac79
18 changed files with 98 additions and 49 deletions

View File

@@ -111,6 +111,32 @@ REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
#define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
/**
* Return a pointer aligned to next multiple of N bytes.
*/
static INLINE void *
align_pointer( void *unaligned, uint alignment )
{
if (sizeof(void *) == 64) {
union {
void *p;
uint64 u;
} pu;
pu.p = unaligned;
pu.u = (pu.u + alignment - 1) & ~(uint64) (alignment - 1);
return pu.p;
}
else {
/* 32-bit pointers */
union {
void *p;
uint u;
} pu;
pu.p = unaligned;
pu.u = (pu.u + alignment - 1) & ~(alignment - 1);
return pu.p;
}
}
/**
* Return memory on given byte alignment
@@ -123,19 +149,18 @@ align_malloc(size_t bytes, uint alignment)
(void) posix_memalign(& mem, alignment, bytes);
return mem;
#else
typedef unsigned long int uintptr_t;
uintptr_t ptr, buf;
char *ptr, *buf;
assert( alignment > 0 );
ptr = (uintptr_t) MALLOC(bytes + alignment + sizeof(void *));
ptr = (char *) MALLOC(bytes + alignment + sizeof(void *));
if (!ptr)
return NULL;
buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
*(uintptr_t *)(buf - sizeof(void *)) = ptr;
buf = (char *) align_pointer( ptr + sizeof(void *), alignment );
*(char **)(buf - sizeof(void *)) = ptr;
return (void *) buf;
return buf;
#endif /* defined(HAVE_POSIX_MEMALIGN) */
}
@@ -169,28 +194,17 @@ align_free(void *ptr)
static INLINE void *
align16( void *unaligned )
{
if (sizeof(void *) == 64) {
union {
void *p;
uint64 u;
} pu;
pu.p = unaligned;
pu.u = (pu.u + 15) & ~15;
return pu.p;
}
else {
/* 32-bit pointers */
union {
void *p;
uint u;
} pu;
pu.p = unaligned;
pu.u = (pu.u + 15) & ~15;
return pu.p;
}
return align_pointer( unaligned, 16 );
}
static INLINE int align_int(int x, int align)
{
return (x + align - 1) & ~(align - 1);
}
#if defined(__MSC__) && defined(__WIN32__)
static INLINE unsigned ffs( unsigned u )
{