1 //===-- memprof_mibmap.cpp -----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of MemProfiler, a memory profiler.
11 //===----------------------------------------------------------------------===//
13 #include "memprof_mibmap.h"
14 #include "profile/MemProfData.inc"
15 #include "sanitizer_common/sanitizer_allocator_internal.h"
16 #include "sanitizer_common/sanitizer_mutex.h"
19 using ::llvm::memprof::MemInfoBlock
;
21 void InsertOrMerge(const uptr Id
, const MemInfoBlock
&Block
, MIBMapTy
&Map
) {
22 MIBMapTy::Handle
h(&Map
, static_cast<uptr
>(Id
), /*remove=*/false,
25 LockedMemInfoBlock
*lmib
=
26 (LockedMemInfoBlock
*)InternalAlloc(sizeof(LockedMemInfoBlock
));
31 LockedMemInfoBlock
*lmib
= *h
;
32 SpinMutexLock
lock(&lmib
->mutex
);
33 uintptr_t ShorterHistogram
;
34 if (Block
.AccessHistogramSize
> lmib
->mib
.AccessHistogramSize
)
35 ShorterHistogram
= lmib
->mib
.AccessHistogram
;
37 ShorterHistogram
= Block
.AccessHistogram
;
39 lmib
->mib
.Merge(Block
);
40 // The larger histogram is kept and the shorter histogram is discarded after
41 // adding the counters to the larger historam. Free only the shorter
43 if (Block
.AccessHistogramSize
> 0 || lmib
->mib
.AccessHistogramSize
> 0)
44 InternalFree((void *)ShorterHistogram
);
48 } // namespace __memprof