[compiler-rt] 76b7784 - [NFC][sanitizer] Rename ByteMap to Map
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 10 22:24:04 PDT 2021
Author: Vitaly Buka
Date: 2021-10-10T22:23:48-07:00
New Revision: 76b7784bcd80404ba4e039ebb6a6413353ae2e9d
URL: https://github.com/llvm/llvm-project/commit/76b7784bcd80404ba4e039ebb6a6413353ae2e9d
DIFF: https://github.com/llvm/llvm-project/commit/76b7784bcd80404ba4e039ebb6a6413353ae2e9d.diff
LOG: [NFC][sanitizer] Rename ByteMap to Map
Added:
compiler-rt/lib/sanitizer_common/sanitizer_flat_map.h
compiler-rt/lib/sanitizer_common/tests/sanitizer_flat_map_test.cpp
Modified:
compiler-rt/lib/sanitizer_common/CMakeLists.txt
compiler-rt/lib/sanitizer_common/sanitizer_allocator.h
compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt
compiler-rt/lib/sanitizer_common/tests/sanitizer_allocator_test.cpp
llvm/utils/gn/secondary/compiler-rt/lib/sanitizer_common/BUILD.gn
Removed:
compiler-rt/lib/sanitizer_common/sanitizer_allocator_bytemap.h
################################################################################
diff --git a/compiler-rt/lib/sanitizer_common/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/CMakeLists.txt
index 5630543e7566b..bd830c2f36684 100644
--- a/compiler-rt/lib/sanitizer_common/CMakeLists.txt
+++ b/compiler-rt/lib/sanitizer_common/CMakeLists.txt
@@ -98,7 +98,6 @@ set(SANITIZER_IMPL_HEADERS
sancov_flags.inc
sanitizer_addrhashmap.h
sanitizer_allocator.h
- sanitizer_allocator_bytemap.h
sanitizer_allocator_checks.h
sanitizer_allocator_combined.h
sanitizer_allocator_interface.h
@@ -137,6 +136,7 @@ set(SANITIZER_IMPL_HEADERS
sanitizer_flag_parser.h
sanitizer_flags.h
sanitizer_flags.inc
+ sanitizer_flat_map.h
sanitizer_freebsd.h
sanitizer_fuchsia.h
sanitizer_getauxval.h
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator.h b/compiler-rt/lib/sanitizer_common/sanitizer_allocator.h
index 5ec47416fe0c9..ec23465d95846 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_allocator.h
@@ -14,6 +14,7 @@
#define SANITIZER_ALLOCATOR_H
#include "sanitizer_common.h"
+#include "sanitizer_flat_map.h"
#include "sanitizer_internal_defs.h"
#include "sanitizer_lfstack.h"
#include "sanitizer_libc.h"
@@ -43,12 +44,6 @@ void SetAllocatorOutOfMemory();
void PrintHintAllocatorCannotReturnNull();
-// Allocators call these callbacks on mmap/munmap.
-struct NoOpMapUnmapCallback {
- void OnMap(uptr p, uptr size) const { }
- void OnUnmap(uptr p, uptr size) const { }
-};
-
// Callback type for iterating over chunks.
typedef void (*ForEachChunkCallback)(uptr chunk, void *arg);
@@ -70,7 +65,6 @@ inline void RandomShuffle(T *a, u32 n, u32 *rand_state) {
#include "sanitizer_allocator_size_class_map.h"
#include "sanitizer_allocator_stats.h"
#include "sanitizer_allocator_primary64.h"
-#include "sanitizer_allocator_bytemap.h"
#include "sanitizer_allocator_primary32.h"
#include "sanitizer_allocator_local_cache.h"
#include "sanitizer_allocator_secondary.h"
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_bytemap.h b/compiler-rt/lib/sanitizer_common/sanitizer_flat_map.h
similarity index 77%
rename from compiler-rt/lib/sanitizer_common/sanitizer_allocator_bytemap.h
rename to compiler-rt/lib/sanitizer_common/sanitizer_flat_map.h
index 7926a07bee6dd..0b3b5d4f8aa16 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_bytemap.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_flat_map.h
@@ -1,4 +1,4 @@
-//===-- sanitizer_allocator_bytemap.h ---------------------------*- C++ -*-===//
+//===-- sanitizer_flat_map.h ------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -10,11 +10,26 @@
//
//===----------------------------------------------------------------------===//
+#ifndef SANITIZER_FLAT_MAP_H
+#define SANITIZER_FLAT_MAP_H
+
+#include "sanitizer_atomic.h"
+#include "sanitizer_common.h"
+#include "sanitizer_internal_defs.h"
#include "sanitizer_local_address_space_view.h"
+#include "sanitizer_mutex.h"
+
+namespace __sanitizer {
+
+// Call these callbacks on mmap/munmap.
+struct NoOpMapUnmapCallback {
+ void OnMap(uptr p, uptr size) const { }
+ void OnUnmap(uptr p, uptr size) const { }
+};
// Maps integers in rage [0, kSize) to u8 values.
template <u64 kSize, typename AddressSpaceViewTy = LocalAddressSpaceView>
-class FlatByteMap {
+class FlatMap {
public:
using AddressSpaceView = AddressSpaceViewTy;
void Init() {
@@ -43,7 +58,7 @@ class FlatByteMap {
template <u64 kSize1, u64 kSize2,
typename AddressSpaceViewTy = LocalAddressSpaceView,
class MapUnmapCallback = NoOpMapUnmapCallback>
-class TwoLevelByteMap {
+class TwoLevelMap {
public:
using AddressSpaceView = AddressSpaceViewTy;
void Init() {
@@ -91,7 +106,7 @@ class TwoLevelByteMap {
if (!res) {
SpinMutexLock l(&mu_);
if (!(res = Get(idx))) {
- res = (u8*)MmapOrDie(kSize2, "TwoLevelByteMap");
+ res = (u8*)MmapOrDie(kSize2, "TwoLevelMap");
MapUnmapCallback().OnMap(reinterpret_cast<uptr>(res), kSize2);
atomic_store(&map1_[idx], reinterpret_cast<uptr>(res),
memory_order_release);
@@ -104,3 +119,15 @@ class TwoLevelByteMap {
StaticSpinMutex mu_;
};
+template <u64 kSize, typename AddressSpaceViewTy = LocalAddressSpaceView>
+using FlatByteMap = FlatMap<kSize, AddressSpaceViewTy>;
+
+template <u64 kSize1, u64 kSize2,
+ typename AddressSpaceViewTy = LocalAddressSpaceView,
+ class MapUnmapCallback = NoOpMapUnmapCallback>
+using TwoLevelByteMap =
+ TwoLevelMap<kSize1, kSize2, AddressSpaceViewTy, MapUnmapCallback>;
+
+}
+
+#endif
\ No newline at end of file
diff --git a/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt
index 3682819d0e7a7..e982992af07a1 100644
--- a/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt
+++ b/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt
@@ -17,6 +17,7 @@ set(SANITIZER_UNITTESTS
sanitizer_common_test.cpp
sanitizer_deadlock_detector_test.cpp
sanitizer_flags_test.cpp
+ sanitizer_flat_map_test.cpp
sanitizer_format_interceptor_test.cpp
sanitizer_hash_test.cpp
sanitizer_ioctl_test.cpp
diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_allocator_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_allocator_test.cpp
index 8952fa4da0533..ad78782f98569 100644
--- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_allocator_test.cpp
+++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_allocator_test.cpp
@@ -1412,69 +1412,6 @@ TEST(SanitizerCommon, SizeClassAllocator64VeryCompactReleaseFreeMemoryToOS) {
#endif // SANITIZER_CAN_USE_ALLOCATOR64
-TEST(SanitizerCommon, TwoLevelByteMap) {
- const u64 kSize1 = 1 << 6, kSize2 = 1 << 12;
- const u64 n = kSize1 * kSize2;
- TwoLevelByteMap<kSize1, kSize2> m;
- m.Init();
- for (u64 i = 0; i < n; i += 7) {
- m.set(i, (i % 100) + 1);
- }
- for (u64 j = 0; j < n; j++) {
- if (j % 7)
- EXPECT_EQ(m[j], 0);
- else
- EXPECT_EQ(m[j], (j % 100) + 1);
- }
-
- m.TestOnlyUnmap();
-}
-
-template <typename AddressSpaceView>
-using TestByteMapASVT =
- TwoLevelByteMap<1 << 12, 1 << 13, AddressSpaceView, TestMapUnmapCallback>;
-using TestByteMap = TestByteMapASVT<LocalAddressSpaceView>;
-
-struct TestByteMapParam {
- TestByteMap *m;
- size_t shard;
- size_t num_shards;
-};
-
-void *TwoLevelByteMapUserThread(void *param) {
- TestByteMapParam *p = (TestByteMapParam*)param;
- for (size_t i = p->shard; i < p->m->size(); i += p->num_shards) {
- size_t val = (i % 100) + 1;
- p->m->set(i, val);
- EXPECT_EQ((*p->m)[i], val);
- }
- return 0;
-}
-
-TEST(SanitizerCommon, ThreadedTwoLevelByteMap) {
- TestByteMap m;
- m.Init();
- TestMapUnmapCallback::map_count = 0;
- TestMapUnmapCallback::unmap_count = 0;
- static const int kNumThreads = 4;
- pthread_t t[kNumThreads];
- TestByteMapParam p[kNumThreads];
- for (int i = 0; i < kNumThreads; i++) {
- p[i].m = &m;
- p[i].shard = i;
- p[i].num_shards = kNumThreads;
- PTHREAD_CREATE(&t[i], 0, TwoLevelByteMapUserThread, &p[i]);
- }
- for (int i = 0; i < kNumThreads; i++) {
- PTHREAD_JOIN(t[i], 0);
- }
- EXPECT_EQ((uptr)TestMapUnmapCallback::map_count, m.size1());
- EXPECT_EQ((uptr)TestMapUnmapCallback::unmap_count, 0UL);
- m.TestOnlyUnmap();
- EXPECT_EQ((uptr)TestMapUnmapCallback::map_count, m.size1());
- EXPECT_EQ((uptr)TestMapUnmapCallback::unmap_count, m.size1());
-}
-
TEST(SanitizerCommon, LowLevelAllocatorShouldRoundUpSizeOnAlloc) {
// When allocating a memory block slightly bigger than a memory page and
// LowLevelAllocator calls MmapOrDie for the internal buffer, it should round
diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_flat_map_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_flat_map_test.cpp
new file mode 100644
index 0000000000000..8fbf877f4aebf
--- /dev/null
+++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_flat_map_test.cpp
@@ -0,0 +1,88 @@
+//===-- sanitizer_flat_map_test.cpp ---------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "sanitizer_common/sanitizer_flat_map.h"
+
+#include "gtest/gtest.h"
+#include "sanitizer_common/tests/sanitizer_pthread_wrappers.h"
+
+using namespace __sanitizer;
+
+namespace {
+struct TestMapUnmapCallback1 {
+ static int map_count, unmap_count;
+ void OnMap(uptr p, uptr size) const { map_count++; }
+ void OnUnmap(uptr p, uptr size) const { unmap_count++; }
+};
+int TestMapUnmapCallback1::map_count;
+int TestMapUnmapCallback1::unmap_count;
+
+TEST(FlatMapTest, TwoLevelByteMap) {
+ const u64 kSize1 = 1 << 6, kSize2 = 1 << 12;
+ const u64 n = kSize1 * kSize2;
+ TwoLevelByteMap<kSize1, kSize2> m;
+ m.Init();
+ for (u64 i = 0; i < n; i += 7) {
+ m.set(i, (i % 100) + 1);
+ }
+ for (u64 j = 0; j < n; j++) {
+ if (j % 7)
+ EXPECT_EQ(m[j], 0);
+ else
+ EXPECT_EQ(m[j], (j % 100) + 1);
+ }
+
+ m.TestOnlyUnmap();
+}
+
+template <typename AddressSpaceView>
+using TestByteMapASVT =
+ TwoLevelByteMap<1 << 12, 1 << 13, AddressSpaceView, TestMapUnmapCallback1>;
+using TestByteMap = TestByteMapASVT<LocalAddressSpaceView>;
+
+struct TestByteMapParam {
+ TestByteMap *m;
+ size_t shard;
+ size_t num_shards;
+};
+
+static void *TwoLevelByteMapUserThread(void *param) {
+ TestByteMapParam *p = (TestByteMapParam *)param;
+ for (size_t i = p->shard; i < p->m->size(); i += p->num_shards) {
+ size_t val = (i % 100) + 1;
+ p->m->set(i, val);
+ EXPECT_EQ((*p->m)[i], val);
+ }
+ return 0;
+}
+
+TEST(FlatMapTest, ThreadedTwoLevelByteMap) {
+ TestByteMap m;
+ m.Init();
+ TestMapUnmapCallback1::map_count = 0;
+ TestMapUnmapCallback1::unmap_count = 0;
+ static const int kNumThreads = 4;
+ pthread_t t[kNumThreads];
+ TestByteMapParam p[kNumThreads];
+ for (int i = 0; i < kNumThreads; i++) {
+ p[i].m = &m;
+ p[i].shard = i;
+ p[i].num_shards = kNumThreads;
+ PTHREAD_CREATE(&t[i], 0, TwoLevelByteMapUserThread, &p[i]);
+ }
+ for (int i = 0; i < kNumThreads; i++) {
+ PTHREAD_JOIN(t[i], 0);
+ }
+ EXPECT_EQ((uptr)TestMapUnmapCallback1::map_count, m.size1());
+ EXPECT_EQ((uptr)TestMapUnmapCallback1::unmap_count, 0UL);
+ m.TestOnlyUnmap();
+ EXPECT_EQ((uptr)TestMapUnmapCallback1::map_count, m.size1());
+ EXPECT_EQ((uptr)TestMapUnmapCallback1::unmap_count, m.size1());
+}
+
+} // namespace
diff --git a/llvm/utils/gn/secondary/compiler-rt/lib/sanitizer_common/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/lib/sanitizer_common/BUILD.gn
index 00eaa1b63cdc0..c3361a1df6004 100644
--- a/llvm/utils/gn/secondary/compiler-rt/lib/sanitizer_common/BUILD.gn
+++ b/llvm/utils/gn/secondary/compiler-rt/lib/sanitizer_common/BUILD.gn
@@ -15,7 +15,6 @@ source_set("sources") {
"sanitizer_addrhashmap.h",
"sanitizer_allocator.cpp",
"sanitizer_allocator.h",
- "sanitizer_allocator_bytemap.h",
"sanitizer_allocator_checks.cpp",
"sanitizer_allocator_checks.h",
"sanitizer_allocator_combined.h",
@@ -60,6 +59,7 @@ source_set("sources") {
"sanitizer_flag_parser.h",
"sanitizer_flags.cpp",
"sanitizer_flags.h",
+ "sanitizer_flat_map.h",
"sanitizer_freebsd.h",
"sanitizer_fuchsia.cpp",
"sanitizer_fuchsia.h",
More information about the llvm-commits
mailing list