LibC: Implement aligned_malloc
This is part of POSIX issue 8
This commit is contained in:
parent
52309e0754
commit
80ffde5e1e
|
@ -44,6 +44,7 @@ void _Exit(int status) __attribute__((__noreturn__));
|
||||||
long a64l(const char* s);
|
long a64l(const char* s);
|
||||||
void abort(void) __attribute__((__noreturn__));
|
void abort(void) __attribute__((__noreturn__));
|
||||||
int abs(int i);
|
int abs(int i);
|
||||||
|
void* aligned_alloc(size_t alignment, size_t size);
|
||||||
int atexit(void (*func)(void));
|
int atexit(void (*func)(void));
|
||||||
double atof(const char* str);
|
double atof(const char* str);
|
||||||
int atoi(const char* str);
|
int atoi(const char* str);
|
||||||
|
|
|
@ -338,14 +338,14 @@ void* calloc(size_t nmemb, size_t size)
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int posix_memalign(void** memptr, size_t alignment, size_t size)
|
void* aligned_alloc(size_t alignment, size_t size)
|
||||||
{
|
{
|
||||||
dprintln_if(DEBUG_MALLOC, "posix_memalign({}, {})", alignment, size);
|
dprintln_if(DEBUG_MALLOC, "aligned_alloc({}, {})", alignment, size);
|
||||||
|
|
||||||
if (alignment < sizeof(void*) || alignment % sizeof(void*) || !BAN::Math::is_power_of_two(alignment / sizeof(void*)))
|
if (alignment < sizeof(void*) || alignment % sizeof(void*) || !BAN::Math::is_power_of_two(alignment / sizeof(void*)))
|
||||||
{
|
{
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (alignment < s_malloc_default_align)
|
if (alignment < s_malloc_default_align)
|
||||||
|
@ -353,7 +353,7 @@ int posix_memalign(void** memptr, size_t alignment, size_t size)
|
||||||
|
|
||||||
void* unaligned = malloc(size + alignment + sizeof(malloc_node_t));
|
void* unaligned = malloc(size + alignment + sizeof(malloc_node_t));
|
||||||
if (unaligned == nullptr)
|
if (unaligned == nullptr)
|
||||||
return -1;
|
return nullptr;
|
||||||
|
|
||||||
pthread_mutex_lock(&s_malloc_mutex);
|
pthread_mutex_lock(&s_malloc_mutex);
|
||||||
|
|
||||||
|
@ -395,6 +395,11 @@ int posix_memalign(void** memptr, size_t alignment, size_t size)
|
||||||
pthread_mutex_unlock(&s_malloc_mutex);
|
pthread_mutex_unlock(&s_malloc_mutex);
|
||||||
|
|
||||||
assert(((uintptr_t)node->data & (alignment - 1)) == 0);
|
assert(((uintptr_t)node->data & (alignment - 1)) == 0);
|
||||||
*memptr = node->data;
|
return node->data;
|
||||||
return 0;
|
}
|
||||||
|
|
||||||
|
int posix_memalign(void** memptr, size_t alignment, size_t size)
|
||||||
|
{
|
||||||
|
dprintln_if(DEBUG_MALLOC, "posix_memalign({}, {})", alignment, size);
|
||||||
|
return (*memptr = aligned_alloc(alignment, size)) ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue