[llvm-branch-commits] [compiler-rt] [CopyProf] Add standalone allocator. (PR #235453)
Snehasish Kumar via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 4 21:25:54 PST 2026
================
@@ -0,0 +1,312 @@
+//===-- copyprof_allocator.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
+//
+//===----------------------------------------------------------------------===//
+///
+/// This file implements the standalone memory allocator for CopyProf, managing
+/// internal dynamic allocations required during profiling.
+///
+//===----------------------------------------------------------------------===//
+
+#include "copyprof_allocator.h"
+
+#include <pthread.h>
+
+#include "copyprof_interceptors.h"
+#include "copyprof_interface_internal.h"
+#include "copyprof_shadow.h"
+#include "sanitizer_common/sanitizer_allocator.h"
+#include "sanitizer_common/sanitizer_allocator_checks.h"
+#include "sanitizer_common/sanitizer_allocator_interface.h"
+#include "sanitizer_common/sanitizer_allocator_report.h"
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_errno.h"
+#include "sanitizer_common/sanitizer_flags.h"
+#include "sanitizer_common/sanitizer_internal_defs.h"
+#include "sanitizer_common/sanitizer_platform_limits_posix.h"
+#include "sanitizer_common/sanitizer_stacktrace.h"
+
+namespace __copyprof {
+namespace {
+
+using namespace __sanitizer;
+
+// Callbacks invoked by the sanitizer allocator whenever it maps or unmaps
+// memory. They are no-ops, as CopyProf does not need to track the allocator's
+// mappings.
+struct CopyProfMapUnmapCallback {
+ // Called when the primary allocator maps memory for a region from which it
+ // serves small and medium sized allocations.
+ void OnMap(uptr p, uptr size) const {}
+ // Called when the secondary allocator maps memory for an allocation that is
+ // too large for the primary allocator. Each such allocation is mmap()ed
+ // separately.
+ void OnMapSecondary(uptr p, uptr size, uptr user_begin,
+ uptr user_size) const {}
+ // Called when either allocator unmaps memory.
+ void OnUnmap(uptr p, uptr size) const {}
+};
+
+struct CopyProfMetadata {
+ // The requested allocation size in bytes.
+ uptr requested_size;
+};
+
+struct AllocatorParameters {
+ static constexpr uptr kSpaceBeg = ~(uptr)0;
+ static constexpr uptr kSpaceSize = 0x40000000000ULL;
+ static constexpr uptr kMetadataSize = sizeof(CopyProfMetadata);
+ static constexpr uptr kFlags = 0;
+ using SizeClassMap = DefaultSizeClassMap;
+ using MapUnmapCallback = CopyProfMapUnmapCallback;
+ using AddressSpaceView = LocalAddressSpaceView;
+};
+
+using PrimaryAllocator = SizeClassAllocator64<AllocatorParameters>;
+using Allocator = CombinedAllocator<PrimaryAllocator>;
+using AllocatorCache = Allocator::AllocatorCache;
+
+// Per-thread allocator cache. It is mmap()ed, and thus zero-initialized as
+// `AllocatorCache` requires, and committed back to `allocator` when its thread
+// exits. As CopyProf does not intercept thread creation, the cache is created
+// by the thread's first (de)allocation, which also arms the TSD destructor that
----------------
snehasish wrote:
Expand TSD?
https://github.com/llvm/llvm-project/pull/235453
More information about the llvm-branch-commits
mailing list