[compiler-rt] 6bf71be - [NFC][sanitizer] Move inline implementation of StackStore into cpp
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 18 18:50:03 PST 2021
Author: Vitaly Buka
Date: 2021-11-18T18:38:06-08:00
New Revision: 6bf71be9f920417a772b123fd42ab855496ad149
URL: https://github.com/llvm/llvm-project/commit/6bf71be9f920417a772b123fd42ab855496ad149
DIFF: https://github.com/llvm/llvm-project/commit/6bf71be9f920417a772b123fd42ab855496ad149.diff
LOG: [NFC][sanitizer] Move inline implementation of StackStore into cpp
Added:
compiler-rt/lib/sanitizer_common/sanitizer_stack_store.cpp
Modified:
compiler-rt/lib/sanitizer_common/CMakeLists.txt
compiler-rt/lib/sanitizer_common/sanitizer_stack_store.h
compiler-rt/lib/tsan/go/build.bat
compiler-rt/lib/tsan/go/buildgo.sh
llvm/utils/gn/secondary/compiler-rt/lib/sanitizer_common/BUILD.gn
Removed:
################################################################################
diff --git a/compiler-rt/lib/sanitizer_common/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/CMakeLists.txt
index 0c14738c61f9c..21445554cdfe6 100644
--- a/compiler-rt/lib/sanitizer_common/CMakeLists.txt
+++ b/compiler-rt/lib/sanitizer_common/CMakeLists.txt
@@ -73,6 +73,7 @@ set(SANITIZER_COVERAGE_SOURCES
set(SANITIZER_SYMBOLIZER_SOURCES
sanitizer_allocator_report.cpp
sanitizer_chained_origin_depot.cpp
+ sanitizer_stack_store.cpp
sanitizer_stackdepot.cpp
sanitizer_stacktrace.cpp
sanitizer_stacktrace_libcdep.cpp
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.cpp
new file mode 100644
index 0000000000000..33ce0c34f77cd
--- /dev/null
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.cpp
@@ -0,0 +1,76 @@
+//===-- sanitizer_stack_store.cpp -------------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// A fast memory allocator that does not support free() nor realloc().
+// All allocations are forever.
+//===----------------------------------------------------------------------===//
+
+#include "sanitizer_stack_store.h"
+
+#include "sanitizer_atomic.h"
+#include "sanitizer_common.h"
+namespace __sanitizer {
+
+uptr *StackStore::tryAlloc(uptr count) {
+ // Optimisic lock-free allocation, essentially try to bump the region ptr.
+ for (;;) {
+ uptr cmp = atomic_load(®ion_pos, memory_order_acquire);
+ uptr end = atomic_load(®ion_end, memory_order_acquire);
+ uptr size = count * sizeof(uptr);
+ if (cmp == 0 || cmp + size > end)
+ return nullptr;
+ if (atomic_compare_exchange_weak(®ion_pos, &cmp, cmp + size,
+ memory_order_acquire))
+ return reinterpret_cast<uptr *>(cmp);
+ }
+}
+
+uptr *StackStore::alloc(uptr count) {
+ // First, try to allocate optimisitically.
+ uptr *s = tryAlloc(count);
+ if (LIKELY(s))
+ return s;
+ return refillAndAlloc(count);
+}
+
+uptr *StackStore::refillAndAlloc(uptr count) {
+ // If failed, lock, retry and alloc new superblock.
+ SpinMutexLock l(&mtx);
+ for (;;) {
+ uptr *s = tryAlloc(count);
+ if (s)
+ return s;
+ atomic_store(®ion_pos, 0, memory_order_relaxed);
+ uptr size = count * sizeof(uptr) + sizeof(BlockInfo);
+ uptr allocsz = RoundUpTo(Max<uptr>(size, 64u * 1024u), GetPageSizeCached());
+ uptr mem = (uptr)MmapOrDie(allocsz, "stack depot");
+ BlockInfo *new_block = (BlockInfo *)(mem + allocsz) - 1;
+ new_block->next = curr;
+ new_block->ptr = mem;
+ new_block->size = allocsz;
+ curr = new_block;
+
+ atomic_fetch_add(&mapped_size, allocsz, memory_order_relaxed);
+
+ allocsz -= sizeof(BlockInfo);
+ atomic_store(®ion_end, mem + allocsz, memory_order_release);
+ atomic_store(®ion_pos, mem, memory_order_release);
+ }
+}
+
+void StackStore::TestOnlyUnmap() {
+ while (curr) {
+ uptr mem = curr->ptr;
+ uptr allocsz = curr->size;
+ curr = curr->next;
+ UnmapOrDie((void *)mem, allocsz);
+ }
+ internal_memset(this, 0, sizeof(*this));
+}
+
+} // namespace __sanitizer
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.h b/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.h
index 34c23fc274b67..a23af375e5a99 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.h
@@ -14,7 +14,6 @@
#define SANITIZER_STACK_STORE_H
#include "sanitizer_atomic.h"
-#include "sanitizer_common.h"
#include "sanitizer_internal_defs.h"
#include "sanitizer_mutex.h"
@@ -43,63 +42,6 @@ class StackStore {
const BlockInfo *curr;
};
-inline uptr *StackStore::tryAlloc(uptr count) {
- // Optimisic lock-free allocation, essentially try to bump the region ptr.
- for (;;) {
- uptr cmp = atomic_load(®ion_pos, memory_order_acquire);
- uptr end = atomic_load(®ion_end, memory_order_acquire);
- uptr size = count * sizeof(uptr);
- if (cmp == 0 || cmp + size > end)
- return nullptr;
- if (atomic_compare_exchange_weak(®ion_pos, &cmp, cmp + size,
- memory_order_acquire))
- return reinterpret_cast<uptr *>(cmp);
- }
-}
-
-inline uptr *StackStore::alloc(uptr count) {
- // First, try to allocate optimisitically.
- uptr *s = tryAlloc(count);
- if (LIKELY(s))
- return s;
- return refillAndAlloc(count);
-}
-
-inline uptr *StackStore::refillAndAlloc(uptr count) {
- // If failed, lock, retry and alloc new superblock.
- SpinMutexLock l(&mtx);
- for (;;) {
- uptr *s = tryAlloc(count);
- if (s)
- return s;
- atomic_store(®ion_pos, 0, memory_order_relaxed);
- uptr size = count * sizeof(uptr) + sizeof(BlockInfo);
- uptr allocsz = RoundUpTo(Max<uptr>(size, 64u * 1024u), GetPageSizeCached());
- uptr mem = (uptr)MmapOrDie(allocsz, "stack depot");
- BlockInfo *new_block = (BlockInfo *)(mem + allocsz) - 1;
- new_block->next = curr;
- new_block->ptr = mem;
- new_block->size = allocsz;
- curr = new_block;
-
- atomic_fetch_add(&mapped_size, allocsz, memory_order_relaxed);
-
- allocsz -= sizeof(BlockInfo);
- atomic_store(®ion_end, mem + allocsz, memory_order_release);
- atomic_store(®ion_pos, mem, memory_order_release);
- }
-}
-
-inline void StackStore::TestOnlyUnmap() {
- while (curr) {
- uptr mem = curr->ptr;
- uptr allocsz = curr->size;
- curr = curr->next;
- UnmapOrDie((void *)mem, allocsz);
- }
- internal_memset(this, 0, sizeof(*this));
-}
-
} // namespace __sanitizer
-#endif // SANITIZER_STACK_STORE_H
+#endif // SANITIZER_STACK_STORE_H
\ No newline at end of file
diff --git a/compiler-rt/lib/tsan/go/build.bat b/compiler-rt/lib/tsan/go/build.bat
index 61336e7eac773..496e127d95817 100644
--- a/compiler-rt/lib/tsan/go/build.bat
+++ b/compiler-rt/lib/tsan/go/build.bat
@@ -25,6 +25,7 @@ type ^
..\rtl\tsan_platform_windows.cpp ^
..\..\sanitizer_common\sanitizer_win.cpp ^
..\..\sanitizer_common\sanitizer_deadlock_detector1.cpp ^
+ ..\..\sanitizer_common\sanitizer_stack_store.cpp ^
..\..\sanitizer_common\sanitizer_stackdepot.cpp ^
..\..\sanitizer_common\sanitizer_flag_parser.cpp ^
..\..\sanitizer_common\sanitizer_symbolizer.cpp ^
diff --git a/compiler-rt/lib/tsan/go/buildgo.sh b/compiler-rt/lib/tsan/go/buildgo.sh
index e66f27f444cf7..8f6ffd4d34c5b 100755
--- a/compiler-rt/lib/tsan/go/buildgo.sh
+++ b/compiler-rt/lib/tsan/go/buildgo.sh
@@ -31,6 +31,7 @@ SRCS="
../../sanitizer_common/sanitizer_printf.cpp
../../sanitizer_common/sanitizer_suppressions.cpp
../../sanitizer_common/sanitizer_thread_registry.cpp
+ ../../sanitizer_common/sanitizer_stack_store.cpp
../../sanitizer_common/sanitizer_stackdepot.cpp
../../sanitizer_common/sanitizer_stacktrace.cpp
../../sanitizer_common/sanitizer_symbolizer.cpp
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 c4ab70476274d..29cbf4c03bfe1 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
@@ -114,6 +114,7 @@ source_set("sources") {
"sanitizer_report_decorator.h",
"sanitizer_ring_buffer.h",
"sanitizer_solaris.cpp",
+ "sanitizer_stack_store.cpp",
"sanitizer_stack_store.h",
"sanitizer_stackdepot.cpp",
"sanitizer_stackdepot.h",
More information about the llvm-commits
mailing list