Of course, don't forget to check malloc() :)
void * aligned_malloc(size_t size, size_t align)
{
// allocate storage area with space for an extra pointer and alignment
void * mem = malloc(size + sizeof(void*) + (align - 1));
if (!mem) return NULL;
// point to the start of the aligned storage to return
void * ptr = (void **)((uintptr_t)(mem + (align - 1) + sizeof(void
*)) & ~(align - 1));
// store the address of the allocated memory prior to the aligned
storage
((void **) ptr)[-1] = mem;
return ptr;
}
void aligned_free(void *ptr)
{
free(((void**) ptr)[-1]);
}
int main( int argc, char *argv[] )
{
return 0;
}
On 18/07/2018 4:14 PM, David Crayford wrote:
void * aligned_malloc(size_t size, size_t align)
{
// allocate storage area with space for an extra pointer and
alignment
void * mem = malloc(size + sizeof(void*) + (align - 1));
// point to the start of the aligned storage
void * ptr = (void **)((uintptr_t)(mem + (align - 1) + sizeof(void
*)) & ~(align - 1));
// store the address of the allocated memory prior to the aligned
storage
((void **) ptr)[-1] = mem;
return ptr;
}
void aligned_free(void *ptr)
{
free(((void**) ptr)[-1]);
}
----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN