OLD | NEW |
| (Empty) |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #include "platform/globals.h" | |
6 | |
7 #if defined(DART_USE_TCMALLOC) && !defined(PRODUCT) | |
8 | |
9 #include "platform/assert.h" | |
10 #include "vm/class_finalizer.h" | |
11 #include "vm/globals.h" | |
12 #include "vm/malloc_hooks.h" | |
13 #include "vm/symbols.h" | |
14 #include "vm/unit_test.h" | |
15 | |
16 namespace dart { | |
17 | |
18 // Note: for these tests, there is no need to call MallocHooks::Init() or | |
19 // MallocHooks::TearDown() as this is all done by the VM test framework. | |
20 | |
21 static void MallocHookTestBufferInitializer(volatile char* buffer, | |
22 uintptr_t size) { | |
23 // Run through the buffer and do something. If we don't do this and the memory | |
24 // in buffer isn't touched, the tcmalloc hooks won't be called. | |
25 for (uintptr_t i = 0; i < size; ++i) { | |
26 buffer[i] = i; | |
27 } | |
28 } | |
29 | |
30 | |
31 UNIT_TEST_CASE(BasicMallocHookTest) { | |
32 MallocHooks::ResetStats(); | |
33 EXPECT_EQ(0L, MallocHooks::allocation_count()); | |
34 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | |
35 | |
36 const intptr_t buffer_size = 10; | |
37 char* buffer = new char[buffer_size]; | |
38 MallocHookTestBufferInitializer(buffer, buffer_size); | |
39 | |
40 EXPECT_EQ(1L, MallocHooks::allocation_count()); | |
41 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), | |
42 MallocHooks::heap_allocated_memory_in_bytes()); | |
43 | |
44 delete[] buffer; | |
45 EXPECT_EQ(0L, MallocHooks::allocation_count()); | |
46 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | |
47 } | |
48 | |
49 | |
50 UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) { | |
51 const intptr_t pre_hook_buffer_size = 3; | |
52 char* pre_hook_buffer = new char[pre_hook_buffer_size]; | |
53 MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size); | |
54 | |
55 MallocHooks::ResetStats(); | |
56 EXPECT_EQ(0L, MallocHooks::allocation_count()); | |
57 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | |
58 | |
59 const intptr_t buffer_size = 10; | |
60 volatile char* buffer = new char[buffer_size]; | |
61 MallocHookTestBufferInitializer(buffer, buffer_size); | |
62 | |
63 EXPECT_EQ(1L, MallocHooks::allocation_count()); | |
64 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), | |
65 MallocHooks::heap_allocated_memory_in_bytes()); | |
66 | |
67 delete[] pre_hook_buffer; | |
68 EXPECT_EQ(1L, MallocHooks::allocation_count()); | |
69 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), | |
70 MallocHooks::heap_allocated_memory_in_bytes()); | |
71 | |
72 | |
73 delete[] buffer; | |
74 EXPECT_EQ(0L, MallocHooks::allocation_count()); | |
75 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | |
76 } | |
77 | |
78 }; // namespace dart | |
79 | |
80 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) | |
OLD | NEW |