view align8.cpp @ 8:f0d2c9b5d3a3

bug fix: ACPower
author Wen X <xue.wen@elec.qmul.ac.uk>
date Fri, 15 Oct 2010 14:12:33 +0100
parents 92ee28024c05
children 977f541d6683
line wrap: on
line source
//---------------------------------------------------------------------------


#include <memory.h>
#include <stdlib.h>
#include "align8.h"

//---------------------------------------------------------------------------
/*
  function malloc8: 8-byte (64-bit) aligned memory allocation.

  Returns pointer to a memory block of $size starting at an address divisible by 8.
*/
void* malloc8(unsigned size)
{
#if (!defined(__SIZEOF_POINTER__) || (__SIZEOF_POINTER__ == 4))
  char *buffer, *result;
  buffer=(char*)malloc(size+8+sizeof(void*));
  if(!buffer) return(NULL);
  char* tmp=&buffer[sizeof(void*)];
  result=&((char*)((unsigned)tmp&0xFFFFFFF8))[8];
  ((void**)result)[-1]=buffer;
  return(result);
#else
  return malloc(size);
#endif
}//malloc8

/*
  function free8: deallocation for malloc8()

  No return value.
*/
void free8(void* buffer8)
{
#if (!defined(__SIZEOF_POINTER__) || (__SIZEOF_POINTER__ == 4))
    if (buffer8) free(((void**)buffer8)[-1]);
#else 
    free(buffer8);
#endif
}//free8