view 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
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