cosmopolitan/third_party/dlmalloc/dlposix_memalign.c

29 lines
742 B
C
Raw Normal View History

2020-06-15 14:18:57 +00:00
#include "libc/errno.h"
#include "libc/mem/mem.h"
2020-09-03 12:44:37 +00:00
#include "libc/sysv/errfuns.h"
#include "third_party/dlmalloc/dlmalloc.internal.h"
2020-06-15 14:18:57 +00:00
int dlposix_memalign(void** pp, size_t alignment, size_t bytes) {
2020-09-03 12:44:37 +00:00
void* mem;
size_t d, r;
mem = NULL;
if (alignment == MALLOC_ALIGNMENT) {
2020-06-15 14:18:57 +00:00
mem = dlmalloc(bytes);
2020-09-03 12:44:37 +00:00
} else {
d = alignment / sizeof(void*);
r = alignment % sizeof(void*);
if (r != 0 || d == 0 || (d & (d - SIZE_T_ONE)) != 0) {
return einval();
} else if (bytes <= MAX_REQUEST - alignment) {
2020-06-15 14:18:57 +00:00
if (alignment < MIN_CHUNK_SIZE) alignment = MIN_CHUNK_SIZE;
mem = dlmemalign$impl(g_dlmalloc, alignment, bytes);
2020-06-15 14:18:57 +00:00
}
}
2020-09-03 12:44:37 +00:00
if (mem == 0) {
return enomem();
} else {
2020-06-15 14:18:57 +00:00
*pp = mem;
return 0;
}
}