File tree Expand file tree Collapse file tree 3 files changed +31
-3
lines changed Expand file tree Collapse file tree 3 files changed +31
-3
lines changed Original file line number Diff line number Diff line change @@ -63,6 +63,7 @@ cc_library(
6363 ],
6464 deps = [
6565 ":config" ,
66+ "//tcmalloc:malloc_hook" ,
6667 "@com_google_absl//absl/base" ,
6768 "@com_google_absl//absl/base:core_headers" ,
6869 ],
Original file line number Diff line number Diff line change 2020GOOGLE_MALLOC_SECTION_BEGIN
2121namespace tcmalloc ::tcmalloc_internal {
2222
23+ ABSL_CONST_INIT thread_local int AllocationGuard::disallowed_ = 0 ;
24+
2325} // namespace tcmalloc::tcmalloc_internal
2426GOOGLE_MALLOC_SECTION_END
Original file line number Diff line number Diff line change 1919#include " absl/base/internal/spinlock.h"
2020#include " absl/base/thread_annotations.h"
2121#include " tcmalloc/internal/config.h"
22+ #include " tcmalloc/malloc_hook.h"
2223
2324GOOGLE_MALLOC_SECTION_BEGIN
2425namespace tcmalloc ::tcmalloc_internal {
2526
26- // TODO(b/143069684): actually ensure no allocations in debug mode here.
27- struct AllocationGuard {
28- AllocationGuard () {}
27+ // In debug mode, ensures that no allocations will occur on the current thread
28+ // while this class is in scope.
29+ class AllocationGuard {
30+ public:
31+ AllocationGuard () {
32+ #ifndef NDEBUG
33+ if (disallowed_ == 0 ) {
34+ (void )MallocHook::AddNewHook (Hook);
35+ }
36+ ++disallowed_;
37+ #endif
38+ }
39+ ~AllocationGuard () {
40+ #ifndef NDEBUG
41+ --disallowed_;
42+ if (disallowed_ == 0 ) {
43+ (void )MallocHook::RemoveNewHook (Hook);
44+ }
45+ #endif
46+ }
47+
48+ private:
49+ static void Hook (const MallocHook::NewInfo& info) {
50+ if (disallowed_ > 0 ) abort ();
51+ }
52+
53+ ABSL_CONST_INIT static thread_local int disallowed_;
2954};
3055
3156// A SpinLockHolder that also enforces no allocations while the lock is held in
You can’t perform that action at this time.
0 commit comments