Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(985)

Unified Diff: runtime/vm/malloc_hooks_test.cc

Issue 2623613003: Implemented basic heap memory allocation tracking in MallocHooks using hooks registered with tcmall… (Closed)
Patch Set: Implemented basic heap memory allocation tracking in MallocHooks using hooks registered with tcmall… Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/vm/malloc_hooks_test.cc
diff --git a/runtime/vm/malloc_hooks_test.cc b/runtime/vm/malloc_hooks_test.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b0c5d361228f27db9c2e9d4d010d4c5f9044b816
--- /dev/null
+++ b/runtime/vm/malloc_hooks_test.cc
@@ -0,0 +1,80 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+#include "platform/globals.h"
+
+#if defined(DART_USE_TCMALLOC) && !defined(PRODUCT)
+
+#include "platform/assert.h"
+#include "vm/class_finalizer.h"
+#include "vm/globals.h"
+#include "vm/malloc_hooks.h"
+#include "vm/symbols.h"
+#include "vm/unit_test.h"
+
+namespace dart {
+
+// Note: for these tests, there is no need to call MallocHooks::Init() or
+// MallocHooks::TearDown() as this is all done by the VM test framework.
+
+static void MallocHookTestBufferInitializer(volatile char* buffer,
+ uintptr_t size) {
+ // Run through the buffer and do something. If we don't do this and the memory
+ // in buffer isn't touched, the tcmalloc hooks won't be called.
+ for (uintptr_t i = 0; i < size; ++i) {
+ buffer[i] = i;
+ }
+}
+
+
+UNIT_TEST_CASE(BasicMallocHookTest) {
+ MallocHooks::ResetStats();
+ EXPECT_EQ(0L, MallocHooks::allocation_count());
+ EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes());
+
+ const intptr_t buffer_size = 10;
+ char* buffer = new char[buffer_size];
+ MallocHookTestBufferInitializer(buffer, buffer_size);
+
+ EXPECT_EQ(1L, MallocHooks::allocation_count());
+ EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size),
+ MallocHooks::heap_allocated_memory_in_bytes());
+
+ delete[] buffer;
+ EXPECT_EQ(0L, MallocHooks::allocation_count());
+ EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes());
+}
+
+
+UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) {
+ const intptr_t pre_hook_buffer_size = 3;
+ char* pre_hook_buffer = new char[pre_hook_buffer_size];
+ MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size);
+
+ MallocHooks::ResetStats();
+ EXPECT_EQ(0L, MallocHooks::allocation_count());
+ EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes());
+
+ const intptr_t buffer_size = 10;
+ volatile char* buffer = new char[buffer_size];
+ MallocHookTestBufferInitializer(buffer, buffer_size);
+
+ EXPECT_EQ(1L, MallocHooks::allocation_count());
+ EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size),
+ MallocHooks::heap_allocated_memory_in_bytes());
+
+ delete[] pre_hook_buffer;
+ EXPECT_EQ(1L, MallocHooks::allocation_count());
+ EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size),
+ MallocHooks::heap_allocated_memory_in_bytes());
+
+
+ delete[] buffer;
+ EXPECT_EQ(0L, MallocHooks::allocation_count());
+ EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes());
+}
+
+}; // namespace dart
+
+#endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT)

Powered by Google App Engine
This is Rietveld 408576698