Mercurial > hg > x
annotate align8.cpp @ 4:92ee28024c05
* Avoid trying to do explicit 32-bit-pointer alignment on 64-bit systems
author | Chris Cannam |
---|---|
date | Tue, 05 Oct 2010 17:03:27 +0100 |
parents | 6422640a802f |
children | 977f541d6683 |
rev | line source |
---|---|
xue@1 | 1 //--------------------------------------------------------------------------- |
xue@1 | 2 |
xue@1 | 3 |
xue@1 | 4 #include <memory.h> |
xue@1 | 5 #include <stdlib.h> |
xue@1 | 6 #include "align8.h" |
xue@1 | 7 |
xue@1 | 8 //--------------------------------------------------------------------------- |
xue@1 | 9 /* |
xue@1 | 10 function malloc8: 8-byte (64-bit) aligned memory allocation. |
xue@1 | 11 |
xue@1 | 12 Returns pointer to a memory block of $size starting at an address divisible by 8. |
xue@1 | 13 */ |
xue@1 | 14 void* malloc8(unsigned size) |
xue@1 | 15 { |
Chris@4 | 16 #if (!defined(__SIZEOF_POINTER__) || (__SIZEOF_POINTER__ == 4)) |
xue@1 | 17 char *buffer, *result; |
xue@1 | 18 buffer=(char*)malloc(size+8+sizeof(void*)); |
xue@1 | 19 if(!buffer) return(NULL); |
xue@1 | 20 char* tmp=&buffer[sizeof(void*)]; |
xue@1 | 21 result=&((char*)((unsigned)tmp&0xFFFFFFF8))[8]; |
xue@1 | 22 ((void**)result)[-1]=buffer; |
xue@1 | 23 return(result); |
Chris@4 | 24 #else |
Chris@4 | 25 return malloc(size); |
Chris@4 | 26 #endif |
xue@1 | 27 }//malloc8 |
xue@1 | 28 |
xue@1 | 29 /* |
xue@1 | 30 function free8: deallocation for malloc8() |
xue@1 | 31 |
xue@1 | 32 No return value. |
xue@1 | 33 */ |
xue@1 | 34 void free8(void* buffer8) |
xue@1 | 35 { |
Chris@4 | 36 #if (!defined(__SIZEOF_POINTER__) || (__SIZEOF_POINTER__ == 4)) |
Chris@4 | 37 if (buffer8) free(((void**)buffer8)[-1]); |
Chris@4 | 38 #else |
Chris@4 | 39 free(buffer8); |
Chris@4 | 40 #endif |
xue@1 | 41 }//free8 |
Chris@4 | 42 |