-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathobstack.cpp
More file actions
69 lines (51 loc) · 1.45 KB
/
obstack.cpp
File metadata and controls
69 lines (51 loc) · 1.45 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
#include <limits>
#include <cstdlib>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include "obstack.hpp"
namespace boost {
namespace arena {
namespace arena_detail {
void free_marker_dtor(void*) {
}
void array_of_primitives_dtor(void*) {
}
static size_t seed_from_heap_memory() {
size_t const len = 64*1024 / sizeof(size_t);
size_t * const mem = new size_t[len];
size_t seed = reinterpret_cast<size_t>(mem);
for(size_t i=0; i<len; i++) {
seed ^= mem[i];
}
delete[] mem;
return seed;
}
static size_t make_strong_seed() {
size_t seed = 0;
seed ^= seed_from_heap_memory();
//TODO add more seed sources
return seed;
}
static size_t init_cookie() {
boost::mt19937 gen;
static size_t seed = 0;
if(!seed) {
seed = make_strong_seed();
} else {
seed++;
}
gen.seed(seed);
boost::uniform_int<size_t> dist(0, std::numeric_limits<size_t>::max());
size_t const cookie = dist(gen);
return cookie;
}
static int invalid_addr_reference;
size_t const ptr_sec::_checksum_cookie = init_cookie();
void * const ptr_sec::_xor_cookie = reinterpret_cast<void*>(init_cookie());
void * const ptr_sec::_invalid_addr = &invalid_addr_reference;
//warning: initialization order is important here
dtor_fptr const free_marker_dtor_xor = ptr_sec::xor_ptr(&free_marker_dtor);
dtor_fptr const array_of_primitives_dtor_xor = ptr_sec::xor_ptr(&array_of_primitives_dtor);
} //namespace arena_detail
} //namespace arena
} //namespace boost