How to wrap posix_memalign (to compile the old code base on Mac)?

I am trying to compile toolkit on Mac. It has a reference to the memalign function malloc.h, but the only close function I could find for mac was posix_memalign. So I'm trying to wrap posix_memalign so that it looks like memalign.

I am a little confused about how to do this (due to void * and void ** pointers):

Signature for posix_memalign -

int posix_memalign(void **memptr, size_t alignment, size_t size); 

and signature for memalign:

 void *memalign(size_t blocksize, size_t bytes); 

Any pointers are greatly appreciated. (Lame pun is random! :)

thanks

+4
source share
2 answers

Sort of:

 void *memalign(size_t blocksize, size_t bytes) { void *result=0; posix_memalign(&result, blocksize, bytes); return result; } 

&result will give you void** to call posix_memalign , and then you can return the result as memalign.

One note: this does not exactly match the behavior - memalign returns errors via errno , but posix_memalign returns them as int and does not apply to errno . You must ensure that errors are handled in some way.

+5
source

Handle errors correctly:

 void *memalign(size_t blocksize, size_t bytes) { void *m; errno = posix_memalign(&m, blocksize, bytes); return errno ? NULL : m; } 
0
source

All Articles