Mercurial > hg > x
annotate align8.cpp @ 1:6422640a802f
first upload
author | Wen X <xue.wen@elec.qmul.ac.uk> |
---|---|
date | Tue, 05 Oct 2010 10:45:57 +0100 |
parents | |
children | 92ee28024c05 |
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 { |
xue@1 | 16 char *buffer, *result; |
xue@1 | 17 buffer=(char*)malloc(size+8+sizeof(void*)); |
xue@1 | 18 if(!buffer) return(NULL); |
xue@1 | 19 char* tmp=&buffer[sizeof(void*)]; |
xue@1 | 20 result=&((char*)((unsigned)tmp&0xFFFFFFF8))[8]; |
xue@1 | 21 ((void**)result)[-1]=buffer; |
xue@1 | 22 return(result); |
xue@1 | 23 }//malloc8 |
xue@1 | 24 |
xue@1 | 25 /* |
xue@1 | 26 function free8: deallocation for malloc8() |
xue@1 | 27 |
xue@1 | 28 No return value. |
xue@1 | 29 */ |
xue@1 | 30 void free8(void* buffer8) |
xue@1 | 31 { |
xue@1 | 32 if (buffer8) free(((void**)buffer8)[-1]); |
xue@1 | 33 }//free8 |