xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/memprof/memprof_mibmap.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
1 //===-- memprof_mibmap.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of MemProfiler, a memory profiler.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "memprof_mibmap.h"
14 #include "sanitizer_common/sanitizer_allocator_internal.h"
15 #include "sanitizer_common/sanitizer_mutex.h"
16 
17 namespace __memprof {
18 
19 void InsertOrMerge(const uptr Id, const MemInfoBlock &Block, MIBMapTy &Map) {
20   MIBMapTy::Handle h(&Map, static_cast<uptr>(Id), /*remove=*/false,
21                      /*create=*/true);
22   if (h.created()) {
23     LockedMemInfoBlock *lmib =
24         (LockedMemInfoBlock *)InternalAlloc(sizeof(LockedMemInfoBlock));
25     lmib->mutex.Init();
26     lmib->mib = Block;
27     *h = lmib;
28   } else {
29     LockedMemInfoBlock *lmib = *h;
30     SpinMutexLock lock(&lmib->mutex);
31     lmib->mib.Merge(Block);
32   }
33 }
34 
35 } // namespace __memprof
36