summaryrefslogtreecommitdiffstats
path: root/tests/alloc.c
blob: ec8e8f710a496f24c0cad92d2a0ab609006d4ba0 (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
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD

#include "prelude.h"
#include "tests.h"

#include "alloc.h"
#include "diag.h"

#include <errno.h>
#include <stdlib.h>
#include <stdint.h>

void check_alloc(void) {
	// Check sizeof_flex()
	struct flexible {
		alignas(64) int foo[8];
		int bar[];
	};
	bfs_check(sizeof_flex(struct flexible, bar, 0) >= sizeof(struct flexible));
	bfs_check(sizeof_flex(struct flexible, bar, 16) % alignof(struct flexible) == 0);

	size_t too_many = SIZE_MAX / sizeof(int) + 1;
	bfs_check(sizeof_flex(struct flexible, bar, too_many) == align_floor(alignof(struct flexible), SIZE_MAX));

	// Corner case: sizeof(type) > align_ceil(alignof(type), offsetof(type, member))
	// Doesn't happen in typical ABIs
	bfs_check(flex_size(8, 16, 4, 4, 1) == 16);

	// Make sure we detect allocation size overflows
#if __GNUC__ && !__clang__
#  pragma GCC diagnostic ignored "-Walloc-size-larger-than="
#endif

	bfs_check(ALLOC_ARRAY(int, too_many) == NULL && errno == EOVERFLOW);
	bfs_check(ZALLOC_ARRAY(int, too_many) == NULL && errno == EOVERFLOW);
	bfs_check(ALLOC_FLEX(struct flexible, bar, too_many) == NULL && errno == EOVERFLOW);
	bfs_check(ZALLOC_FLEX(struct flexible, bar, too_many) == NULL && errno == EOVERFLOW);

	// varena tests
	struct varena varena;
	VARENA_INIT(&varena, struct flexible, bar);

	for (size_t i = 0; i < 256; ++i) {
		bfs_verify(varena_alloc(&varena, i));
		struct arena *arena = &varena.arenas[varena.narenas - 1];
		bfs_check(arena->size >= sizeof_flex(struct flexible, bar, i));
	}

	varena_destroy(&varena);
}