aboutsummaryrefslogtreecommitdiff
path: root/test/arena.c
blob: e09acebb778db697ff9599cfede90a8f092f1f9f (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <sys/mman.h>

#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>

#include "alloc.h"
#include "common.h"
#include "test-internal.h"

static void make_and_free(void),
            make_and_resize(void),
            free_empty_arena(void),
            make_snapshot(void);

int
main(void)
{
	make_and_free();
	make_and_resize();
	free_empty_arena();
	make_snapshot();
	return rv;
}

void
make_and_free(void)
{
	arena_t a = NULL;
	int *xs = arena_new(&a, int, 69);

	for (size_t i = 0; i < 69; i++)
		xs[i] = i;

	arena_free(&a);

	/* Assert that after arena_free(), the page was actually freed */

	unsigned char *vec = NULL;
	size_t pagesz = (size_t)sysconf(_SC_PAGESIZE);
	oryx_assert(pagesz != (size_t)-1);

	/* Vector size documented in mincore(2) */
	vec = malloc((_ARENA_DFLT_CAP + pagesz - 1) / pagesz);
	oryx_assert(vec != NULL);

	errno = 0;
	mincore(xs, sizeof(int) * 69, vec);
	oryx_assert(errno == ENOMEM);
cleanup:
	free(vec);
}

void
make_and_resize(void)
{
	arena_t a = NULL;
	size_t *xs = arena_new(&a, size_t, 100);
	for (size_t i = 0; i < 100; i++)
		xs[i] = i;

	static size_t ranges[][2] = {
		{  100,   1000},
		{ 1000,  10000},
		{10000, 100000},
	};

	for (size_t i = 0; i < lengthof(ranges); i++) {
		size_t lo = ranges[i][0];
		size_t hi = ranges[i][1];

		xs = arena_grow(&a, xs, size_t, lo, hi);
		for (size_t j = lo; j < hi; j++)
			xs[j] = j;
	}

	for (size_t i = 0; i < ranges[lengthof(ranges) - 1][1]; i++)
		oryx_assert(xs[i] == i);

cleanup:
	arena_free(&a);
}

void
free_empty_arena(void)
{
	arena_free(&(arena_t){0});
}

void
make_snapshot(void)
{
	arena_t a = NULL;

	char      *p1, *p2, *p3;
	snapshot_t s1,  s2,  s3;

	/* The arena by default is backed my mmap(), and makes pages of size
	   _ARENA_DFLT_CAP or larger.  This means we can test to see if the
	   arena snapshotting is properly implemented by:

	   1.  Creating allocations of the blocksize.  This ensures each
	       allocation is on its own page as allocated by mmap(2).
	   2.  Creating a snapshot before each allocation.
	   3.  Restoring snapshots and checking to see which pages are still
	       valid via mincore() which errors with ENOMEM if the page isn’t
		   mapped.

	   Technically mincore() is non-standard… but it exists on all major
	   BSDs as well as Linux and Darwin. */

	s1 = arena_snapshot_create(a); p1 = arena_new(&a, char, _ARENA_DFLT_CAP);
	s2 = arena_snapshot_create(a); p2 = arena_new(&a, char, _ARENA_DFLT_CAP);
	s3 = arena_snapshot_create(a); p3 = arena_new(&a, char, _ARENA_DFLT_CAP);

	unsigned char *vec = NULL;
	size_t pagesz = (size_t)sysconf(_SC_PAGESIZE);
	oryx_assert(pagesz != (size_t)-1);

	/* Vector size documented in mincore(2) */
	vec = malloc((_ARENA_DFLT_CAP + pagesz - 1) / pagesz);
	oryx_assert(vec != NULL);

	errno = 0; mincore(p1, _ARENA_DFLT_CAP, vec); oryx_assert(errno == 0);
	errno = 0; mincore(p2, _ARENA_DFLT_CAP, vec); oryx_assert(errno == 0);
	errno = 0; mincore(p3, _ARENA_DFLT_CAP, vec); oryx_assert(errno == 0);

	arena_snapshot_restore(&a, s3);
	errno = 0; mincore(p1, _ARENA_DFLT_CAP, vec); oryx_assert(errno == 0);
	errno = 0; mincore(p2, _ARENA_DFLT_CAP, vec); oryx_assert(errno == 0);
	errno = 0; mincore(p3, _ARENA_DFLT_CAP, vec); oryx_assert(errno == ENOMEM);
	oryx_assert(a != NULL);

	arena_snapshot_restore(&a, s2);
	errno = 0; mincore(p1, _ARENA_DFLT_CAP, vec); oryx_assert(errno == 0);
	errno = 0; mincore(p2, _ARENA_DFLT_CAP, vec); oryx_assert(errno == ENOMEM);
	errno = 0; mincore(p3, _ARENA_DFLT_CAP, vec); oryx_assert(errno == ENOMEM);
	oryx_assert(a != NULL);

	arena_snapshot_restore(&a, s1);
	errno = 0; mincore(p1, _ARENA_DFLT_CAP, vec); oryx_assert(errno == ENOMEM);
	errno = 0; mincore(p2, _ARENA_DFLT_CAP, vec); oryx_assert(errno == ENOMEM);
	errno = 0; mincore(p3, _ARENA_DFLT_CAP, vec); oryx_assert(errno == ENOMEM);
	oryx_assert(a == NULL);

cleanup:
	free(vec);
	arena_free(&a);
}