add btAlignPointer template and use it in serializer/allocator

this should fix Issue 559
This commit is contained in:
erwin.coumans
2012-02-25 19:24:59 +00:00
parent 22dc2ca6c3
commit bafef09b6e
3 changed files with 26 additions and 26 deletions

View File

@@ -58,16 +58,18 @@ static inline void btAlignedFreeDefault(void *ptr)
free(ptr); free(ptr);
} }
#else #else
static inline void *btAlignedAllocDefault(size_t size, int alignment) static inline void *btAlignedAllocDefault(size_t size, int alignment)
{ {
void *ret; void *ret;
char *real; char *real;
unsigned long offset;
real = (char *)sAllocFunc(size + sizeof(void *) + (alignment-1)); real = (char *)sAllocFunc(size + sizeof(void *) + (alignment-1));
if (real) { if (real) {
offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1); ret = btAlignPointer(real + sizeof(void *),alignment);
ret = (void *)((real + sizeof(void *)) + offset);
*((void **)(ret)-1) = (void *)(real); *((void **)(ret)-1) = (void *)(real);
} else { } else {
ret = (void *)(real); ret = (void *)(real);
@@ -110,7 +112,6 @@ void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filen
{ {
void *ret; void *ret;
char *real; char *real;
unsigned long offset;
gTotalBytesAlignedAllocs += size; gTotalBytesAlignedAllocs += size;
gNumAlignedAllocs++; gNumAlignedAllocs++;
@@ -118,9 +119,7 @@ void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filen
real = (char *)sAllocFunc(size + 2*sizeof(void *) + (alignment-1)); real = (char *)sAllocFunc(size + 2*sizeof(void *) + (alignment-1));
if (real) { if (real) {
offset = (alignment - (unsigned long)(real + 2*sizeof(void *))) & ret = (void*) btAlignPointer((real + 2*sizeof(void *), alignment);
(alignment-1);
ret = (void *)((real + 2*sizeof(void *)) + offset);
*((void **)(ret)-1) = (void *)(real); *((void **)(ret)-1) = (void *)(real);
*((int*)(ret)-2) = size; *((int*)(ret)-2) = size;

View File

@@ -519,4 +519,21 @@ struct btTypedObject
return m_objectType; return m_objectType;
} }
}; };
///align a pointer to the provided alignment, upwards
template <typename T>T* btAlignPointer(T* unalignedPtr, size_t alignment)
{
union
{
T* ptr;
size_t integer;
};
const size_t bit_mask = ~(alignment - 1);
ptr = unalignedPtr;
integer += alignment-1;
integer &= bit_mask;
return ptr;
}
#endif //BT_SCALAR_H #endif //BT_SCALAR_H

View File

@@ -246,15 +246,7 @@ protected:
while (*cp)cp++; while (*cp)cp++;
cp++; cp++;
} }
{ cp = btAlignPointer(cp,4);
nr= (long)cp;
// long mask=3;
nr= ((nr+3)&~3)-nr;
while (nr--)
{
cp++;
}
}
/* /*
TYPE (4 bytes) TYPE (4 bytes)
@@ -281,15 +273,7 @@ protected:
cp++; cp++;
} }
{ cp = btAlignPointer(cp,4);
nr= (long)cp;
// long mask=3;
nr= ((nr+3)&~3)-nr;
while (nr--)
{
cp++;
}
}
/* /*