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