view 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
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)
{
  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);
}//malloc8

/*
  function free8: deallocation for malloc8()

  No return value.
*/
void free8(void* buffer8)
{
  if (buffer8) free(((void**)buffer8)[-1]);
}//free8