-
-
Save neerajkanhere/f91db3bd6ea54b63c47371ab7df62e7d to your computer and use it in GitHub Desktop.
C / C++ portable aligned memory allocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Allocate aligned memory in a portable way. | |
* | |
* Memory allocated with aligned alloc *MUST* be freed using aligned_free. | |
* | |
* @param alignment The number of bytes to which memory must be aligned. This | |
* value *must* be <= 255. | |
* @param bytes The number of bytes to allocate. | |
* @param zero If true, the returned memory will be zeroed. If false, the | |
* contents of the returned memory are undefined. | |
* @returns A pointer to `size` bytes of memory, aligned to an `alignment`-byte | |
* boundary. | |
*/ | |
void *aligned_alloc(size_t alignment, size_t size, bool zero) { | |
size_t request_size = size + alignment; | |
char* buf = (char*)(zero ? calloc(1, request_size) : malloc(request_size)); | |
size_t remainder = ((size_t)buf) % alignment; | |
size_t offset = alignment - remainder; | |
char* ret = buf + (unsigned char)offset; | |
// store how many extra bytes we allocated in the byte just before the | |
// pointer we return | |
*(unsigned char*)(ret - 1) = offset; | |
return (void*)ret; | |
} | |
/* Free memory allocated with aligned_alloc */ | |
void aligned_free(void* aligned_ptr) { | |
int offset = *(((char*)aligned_ptr) - 1); | |
free(((char*)aligned_ptr) - offset); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment