void *aligned_alloc(size_t size, size_t align) {
void *p = NULL;
void *ret = NULL;
p = malloc(size + align + sizeof(void *));
if (!p) return NULL;
size_t offset = align - (size_t)p % align;
if (offset < sizeof (void*)) offset += align;
ret = (p + offset);
*((size_t*)ret - 1) = (size_t)p;
return ret;
}
void aligned_free(void *p) {
free((void *)(*((size_t *)p - 1)));
}
aligned memory allocation
最新推荐文章于 2021-09-27 12:54:16 发布