summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib/aligned_alloc.c
blob: f8ab24db3ce99ae0d1c5ce87d533b89dbf1d90e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "stddef.h" // for NULL

#include <errno.h>  // for errno, EINVAL, ENOMEM
#include <stdint.h> // for uintptr_t
#include <stdlib.h> // for size_t, aligned_alloc, malloc

void *aligned_alloc(size_t alignment, size_t size)
{
	if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
		errno = EINVAL;
		return NULL;
	}

	if (size % alignment != 0) {
		errno = EINVAL;
		return NULL;
	}

	if (size == 0) {
		return NULL;
	}

	size_t total_size = size + alignment - 1 + sizeof(void *);

	void *raw_ptr = malloc(total_size);
	if (raw_ptr == NULL) {
		errno = ENOMEM;
		return NULL;
	}

	uintptr_t raw_addr = (uintptr_t)raw_ptr;
	uintptr_t aligned_addr = (raw_addr + sizeof(void *) + alignment - 1) &
				 ~(alignment - 1);

	void **orig_ptr_slot = (void **)(aligned_addr - sizeof(void *));
	*orig_ptr_slot = raw_ptr;

	return (void *)aligned_addr;
}