dissoupo
BAN USER//
// Allocates and returns pointer aligned to requested boundary
//
void* AlignedAlloc( size_t cbSize, size_t cbAlign )
{
//
// Align Align-1 ~(Align-1)
// 8 00001000 00000111 11111000
// 16 00010000 00001111 11110000
// 32 00100000 00011111 11100000
// 64 01000000 00111111 11000000
// 128 10000000 01111111 10000000
void *p;
void *ap;
size_t cbExtra = cbAlign+sizeof(void*);
p = LocalAlloc( NONZEROLPTR, cbExtra + cbSize);
ap = (BYTE*) ((size_t)p & ~(cbAlign-1)) + cbAlign;
*((void**)ap-1) = p;
_ASSERT( 0 == (size_t)ap % cbAlign );
return ap;
}
void AlignedFree( void* p)
{
LocalFree( *((void**)p-1) );
}
I made a code to run on Win32 and align by 8-16-64 bytes.
You can replace LocalAlloc, LocalFree with any counterparts.
//
// Allocates and returns pointer aligned to requested boundary
//
void* AlignedAlloc( size_t cbSize, size_t cbAlign )
{
//
// Align Align-1 ~(Align-1)
// 8 00001000 00000111 11111000
// 16 00010000 00001111 11110000
// 32 00100000 00011111 11100000
// 64 01000000 00111111 11000000
// 128 10000000 01111111 10000000
void *p;
void *ap;
size_t cbExtra = cbAlign+sizeof(void*);
p = LocalAlloc( NONZEROLPTR, cbExtra + cbSize);
ap = (BYTE*) ((size_t)p & ~(cbAlign-1)) + cbAlign;
*((void**)ap-1) = p;
_ASSERT( 0 == (size_t)ap % cbAlign );
return ap;
}
void AlignedFree( void* p)
{
LocalFree( *((void**)p-1) );
}
I think there are 2 bugs in this function:
- it's 32 bits rotation on 64 bits 'long long'
- since x is NOT unsigned long, the >> will cary the sign bit over to the right and the result of (x & 0xFF00FF00) is not the same as for 'unsigned long' or DWORD.
Please correct me if I'm wrong.
Bug #2 in this code that it indends to swap (char) in the stream.
- dissoupo April 29, 2012This would work to convert (LE to BE) or (BE to LE).