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
|
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include "tests/common/slab_tests.h"
#include "common/slab/slab.h"
#include "knot/common.h"
/*! \brief Type-safe maximum macro. */
#define SLAB_MAX(a, b) \
({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
/* Explicitly ask for symbols,
* as the constructor and destructor
* aren't created for test modules.
*/
extern void slab_init();
extern void slab_deinit();
static int slab_tests_count(int argc, char *argv[]);
static int slab_tests_run(int argc, char *argv[]);
/*! Exported unit API.
*/
unit_api slab_tests_api = {
"SLAB allocator", //! Unit name
&slab_tests_count, //! Count scheduled tests
&slab_tests_run //! Run scheduled tests
};
static int slab_tests_count(int argc, char *argv[])
{
return 7;
}
static int slab_tests_run(int argc, char *argv[])
{
// 1. Create slab cache
srand(time(0));
const unsigned pattern = 0xdeadbeef;
slab_cache_t cache;
int ret = slab_cache_init(&cache, sizeof(int));
ok(ret == 0, "slab: created empty cache");
// 2. Couple alloc/free
bool valid_free = true;
lives_ok({
for(int i = 0; i < 100; ++i) {
int* data = (int*)slab_cache_alloc(&cache);
*data = pattern;
slab_free(data);
if (*data == pattern)
valid_free = false;
}
}, "slab: couple alloc/free");
// 5. Verify freed block
ok(valid_free, "slab: freed memory is correctly invalidated");
// 4. Reap memory
slab_t* slab = cache.slabs_free;
int free_count = 0;
while (slab) {
slab_t* next = slab->next;
if (slab_isempty(slab)) {
++free_count;
}
slab = next;
}
int reaped = slab_cache_reap(&cache);
cmp_ok(reaped, "==", free_count, "slab: cache reaping works");
// Stress cache
int alloc_count = 73521;
void** ptrs = alloca(alloc_count * sizeof(void*));
int ptrs_i = 0;
for(int i = 0; i < alloc_count; ++i) {
double roll = rand() / (double) RAND_MAX;
if ((ptrs_i == 0) || (roll < 0.6)) {
int id = ptrs_i++;
ptrs[id] = slab_cache_alloc(&cache);
if (ptrs[id] == 0) {
ptrs_i--;
} else {
int* data = (int*)ptrs[id];
*data = pattern;
}
} else {
slab_free(ptrs[--ptrs_i]);
}
}
// 5. Delete cache
slab_cache_destroy(&cache);
ok(cache.bufsize == 0, "slab: freed cache");
// 6. Greate GP allocator
slab_alloc_t alloc;
ret = slab_alloc_init(&alloc);
ok(ret == 0, "slab: created GP allocator");
// 7. Stress allocator
unsigned ncount = 0;
ptrs_i = 0;
for(int i = 0; i < alloc_count; ++i) {
double roll = rand() / (double) RAND_MAX;
size_t bsize = roll * 2048;
bsize = SLAB_MAX(bsize, 8);
if ((ptrs_i == 0) || (roll < 0.6)) {
void* m = slab_alloc_alloc(&alloc, bsize);
if (m == 0) {
++ncount;
} else {
ptrs[ptrs_i++] = m;
}
} else {
slab_free(ptrs[--ptrs_i]);
}
}
cmp_ok(ncount, "==", 0, "slab: GP allocator alloc/free working");
// 7. Destroy allocator
slab_alloc_destroy(&alloc);
return 0;
}
|