aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/ansi/alloc.c
blob: c6ce870a15ae0abb0e5043dfc30c63f3eacb88f5 (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
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>
#include <errno.h>

int main() {
	void *p;

	p = aligned_alloc(sizeof(void *), sizeof(void *));
	assert(p != NULL && (uintptr_t)p % sizeof(void *) == 0);
	free(p);

	p = aligned_alloc(256, 256);
	assert(p != NULL && (uintptr_t)p % 256 == 0);
	free(p);

	// small alignments are okay
	p = aligned_alloc(1, 8);
	assert(p != NULL);
	free(p);
	p = aligned_alloc(1, 1);
	assert(p != NULL);
	free(p);

	// It seems that glibc doesn't report error in these cases.
#if !(defined(USE_HOST_LIBC) && defined(__GLIBC__))
	// size % align must be 0
	p = aligned_alloc(256, 1);
	assert(errno == EINVAL);
	assert(p == NULL);

	// align must be a 'valid alignment supported by the implementation'
	p = aligned_alloc(3, 1);
	assert(errno == EINVAL);
	assert(p == NULL);
#endif
}