Skip to content

Instantly share code, notes, and snippets.

@r-lyeh-archived
Created July 9, 2016 11:01
Show Gist options
  • Save r-lyeh-archived/35375347478bfb6353f5b243f70529ed to your computer and use it in GitHub Desktop.
Save r-lyeh-archived/35375347478bfb6353f5b243f70529ed to your computer and use it in GitHub Desktop.
/* Mike F */
#include <stdint.h>
void *memalign( int align, size_t size ) {
void *mem = malloc( size + (align-1) + sizeof(void*) );
if(!mem) return 0;
char *amem = ((char*)mem) + sizeof(void*);
amem += (align - ((uintptr_t)amem & (align - 1)) & (align-1));
((void**)amem)[-1] = mem;
return amem;
}
void memfree( void *mem ) {
if(mem) free( ((void**)mem)[-1] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment