-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtests.cpp
More file actions
138 lines (111 loc) · 3.43 KB
/
tests.cpp
File metadata and controls
138 lines (111 loc) · 3.43 KB
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
/* iso_alloc tests.cpp
* Copyright 2023 - chris.rohlf@gmail.com */
#include <memory>
#include <array>
#include <vector>
#if THREAD_SUPPORT
#include <thread>
#endif
#include "iso_alloc.h"
#include "iso_alloc_internal.h"
using namespace std;
static const uint32_t allocation_sizes[] = {ZONE_16, ZONE_32, ZONE_64, ZONE_128,
ZONE_256, ZONE_512, ZONE_1024,
ZONE_2048, ZONE_4096, ZONE_8192};
static const uint32_t array_sizes[] = {16, 32, 64, 128, 256, 512, 1024,
2048, 4096, 8192, 16384, 32768, 65536};
int32_t alloc_count;
class Base {
public:
int32_t type;
char *str;
};
class Derived : Base {
public:
Derived(int32_t i) {
count = i;
type = count * count;
str = (char *) iso_alloc(1024);
memcpy(str, "AAAAA", 5);
}
~Derived() {
count = 0;
type = 0;
iso_free(str);
}
void operator delete(void *p) {}
void operator delete(void *p, void *h) {
Derived *d = static_cast<Derived *>(p);
d->~Derived();
uint8_t *plc = static_cast<uint8_t *>(h);
delete[] plc;
}
#if __cplusplus >= 202002L
/* Note this need to be class member */
void operator delete(Derived *ptr, std::destroying_delete_t) {
ptr->~Derived();
::operator delete(ptr);
}
#endif
uint32_t count;
};
int allocate(size_t array_size, size_t allocation_size) {
std::vector<void *> p(array_size);
memset(p.data(), 0x0, p.size());
for(size_t i = 0; i < array_size; i++) {
if(allocation_size == 0) {
allocation_size = allocation_sizes[(rand() % sizeof(allocation_sizes) / sizeof(uint32_t))] + (rand() % 32);
}
p[i] = new uint8_t[allocation_size];
if(p[i] == NULL) {
LOG_AND_ABORT("Failed to allocate %ld bytes after %d total allocations", allocation_size, alloc_count);
}
alloc_count++;
/* Randomly free some allocations */
if((rand() % 2) == 1) {
delete[](uint8_t *) p[i];
p[i] = NULL;
}
}
/* Free the remaining allocations */
for(size_t i = 0; i < array_size; i++) {
if(p[i] != NULL) {
delete[](uint8_t *) p[i];
}
}
return OK;
}
int main(int argc, char *argv[]) {
char *a = (char *) iso_alloc(100);
iso_free(a);
auto d = std::make_unique<Derived>(100);
constexpr size_t array_sizeslen = sizeof(array_sizes) / sizeof(uint32_t);
for(size_t i = 0; i < array_sizeslen; i++) {
for(size_t z = 0; z < sizeof(allocation_sizes) / sizeof(uint32_t); z++) {
allocate(array_sizes[i], allocation_sizes[z]);
}
}
for(size_t i = 0; i < array_sizeslen; i++) {
allocate(array_sizes[i], 0);
Base *b = new Base();
delete b;
auto d = std::make_unique<Derived>(i);
}
for(size_t i = 0; i < array_sizeslen; i++) {
allocate(array_sizes[i], 0);
auto *ptr = new uint8_t[sizeof(Derived)];
auto *d = new(ptr) Derived(i);
Derived::operator delete(d, ptr);
}
#if THREAD_SUPPORT && __linux__
for(size_t i = 0; i < 4; i++) {
std::array<std::thread, 4> t;
for(size_t z = 0; z < 4; z++) {
t[i] = std::thread(allocate, array_sizes[i], allocation_sizes[z]);
t[i].join();
}
}
#endif
iso_verify_zones();
return 0;
}