1 // define a passthrough allocator that tracks alloc calls.
2 // (note that we can't drop this in to the usual test suite, because it's a big
3 // global variable).
4 use std::alloc::{GlobalAlloc, Layout, System};
5
6 static mut N_ALLOCS: usize = 0;
7
8 struct TrackingAllocator;
9
10 impl TrackingAllocator {
n_allocs(&self) -> usize11 fn n_allocs(&self) -> usize {
12 unsafe { N_ALLOCS }
13 }
14 }
15 unsafe impl GlobalAlloc for TrackingAllocator {
alloc(&self, layout: Layout) -> *mut u816 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
17 N_ALLOCS += 1;
18 System.alloc(layout)
19 }
dealloc(&self, ptr: *mut u8, layout: Layout)20 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
21 System.dealloc(ptr, layout)
22 }
23 }
24
25 // use the tracking allocator:
26 #[global_allocator]
27 static A: TrackingAllocator = TrackingAllocator;
28
29 // import the flatbuffers generated code:
30 extern crate flatbuffers;
31 #[path = "../../monster_test_generated.rs"]
32 mod monster_test_generated;
33 pub use monster_test_generated::my_game;
34
35 // verbatim from the test suite:
create_serialized_example_with_generated_code(builder: &mut flatbuffers::FlatBufferBuilder)36 fn create_serialized_example_with_generated_code(builder: &mut flatbuffers::FlatBufferBuilder) {
37 let mon = {
38 let _ = builder.create_vector_of_strings(&["these", "unused", "strings", "check", "the", "create_vector_of_strings", "function"]);
39
40 let s0 = builder.create_string("test1");
41 let s1 = builder.create_string("test2");
42 let fred_name = builder.create_string("Fred");
43
44 // can't inline creation of this Vec3 because we refer to it by reference, so it must live
45 // long enough to be used by MonsterArgs.
46 let pos = my_game::example::Vec3::new(1.0, 2.0, 3.0, 3.0, my_game::example::Color::Green, &my_game::example::Test::new(5i16, 6i8));
47
48 let args = my_game::example::MonsterArgs{
49 hp: 80,
50 mana: 150,
51 name: Some(builder.create_string("MyMonster")),
52 pos: Some(&pos),
53 test_type: my_game::example::Any::Monster,
54 test: Some(my_game::example::Monster::create(builder, &my_game::example::MonsterArgs{
55 name: Some(fred_name),
56 ..Default::default()
57 }).as_union_value()),
58 inventory: Some(builder.create_vector_direct(&[0u8, 1, 2, 3, 4][..])),
59 test4: Some(builder.create_vector_direct(&[my_game::example::Test::new(10, 20),
60 my_game::example::Test::new(30, 40)])),
61 testarrayofstring: Some(builder.create_vector(&[s0, s1])),
62 ..Default::default()
63 };
64 my_game::example::Monster::create(builder, &args)
65 };
66 my_game::example::finish_monster_buffer(builder, mon);
67 }
68
main()69 fn main() {
70 // test the allocation tracking:
71 {
72 let before = A.n_allocs();
73 let _x: Vec<u8> = vec![0u8; 1];
74 let after = A.n_allocs();
75 assert_eq!(before + 1, after);
76 }
77
78 let builder = &mut flatbuffers::FlatBufferBuilder::new();
79 {
80 // warm up the builder (it can make small allocs internally, such as for storing vtables):
81 create_serialized_example_with_generated_code(builder);
82 }
83
84 // reset the builder, clearing its heap-allocated memory:
85 builder.reset();
86
87 {
88 let before = A.n_allocs();
89 create_serialized_example_with_generated_code(builder);
90 let after = A.n_allocs();
91 assert_eq!(before, after, "KO: Heap allocs occurred in Rust write path");
92 }
93
94 let buf = builder.finished_data();
95
96 // use the allocation tracking on the read path:
97 {
98 let before = A.n_allocs();
99
100 // do many reads, forcing them to execute by using assert_eq:
101 {
102 let m = my_game::example::get_root_as_monster(buf);
103 assert_eq!(80, m.hp());
104 assert_eq!(150, m.mana());
105 assert_eq!("MyMonster", m.name());
106
107 let pos = m.pos().unwrap();
108 assert_eq!(pos.x(), 1.0f32);
109 assert_eq!(pos.y(), 2.0f32);
110 assert_eq!(pos.z(), 3.0f32);
111 assert_eq!(pos.test1(), 3.0f64);
112 assert_eq!(pos.test2(), my_game::example::Color::Green);
113 let pos_test3 = pos.test3();
114 assert_eq!(pos_test3.a(), 5i16);
115 assert_eq!(pos_test3.b(), 6i8);
116 assert_eq!(m.test_type(), my_game::example::Any::Monster);
117 let table2 = m.test().unwrap();
118 let m2 = my_game::example::Monster::init_from_table(table2);
119
120 assert_eq!(m2.name(), "Fred");
121
122 let inv = m.inventory().unwrap();
123 assert_eq!(inv.len(), 5);
124 assert_eq!(inv.iter().sum::<u8>(), 10u8);
125
126 let test4 = m.test4().unwrap();
127 assert_eq!(test4.len(), 2);
128 assert_eq!(test4[0].a() as i32 + test4[0].b() as i32 +
129 test4[1].a() as i32 + test4[1].b() as i32, 100);
130
131 let testarrayofstring = m.testarrayofstring().unwrap();
132 assert_eq!(testarrayofstring.len(), 2);
133 assert_eq!(testarrayofstring.get(0), "test1");
134 assert_eq!(testarrayofstring.get(1), "test2");
135 }
136
137 // assert that no allocs occurred:
138 let after = A.n_allocs();
139 assert_eq!(before, after, "KO: Heap allocs occurred in Rust read path");
140 }
141 println!("Rust: Heap alloc checks completed successfully");
142 }
143