Path: blob/main/contrib/llvm-project/compiler-rt/lib/memprof/memprof_mibmap.cpp
35236 views
//===-- memprof_mibmap.cpp -----------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of MemProfiler, a memory profiler.9//10//===----------------------------------------------------------------------===//1112#include "memprof_mibmap.h"13#include "profile/MemProfData.inc"14#include "sanitizer_common/sanitizer_allocator_internal.h"15#include "sanitizer_common/sanitizer_mutex.h"1617namespace __memprof {18using ::llvm::memprof::MemInfoBlock;1920void InsertOrMerge(const uptr Id, const MemInfoBlock &Block, MIBMapTy &Map) {21MIBMapTy::Handle h(&Map, static_cast<uptr>(Id), /*remove=*/false,22/*create=*/true);23if (h.created()) {24LockedMemInfoBlock *lmib =25(LockedMemInfoBlock *)InternalAlloc(sizeof(LockedMemInfoBlock));26lmib->mutex.Init();27lmib->mib = Block;28*h = lmib;29} else {30LockedMemInfoBlock *lmib = *h;31SpinMutexLock lock(&lmib->mutex);32uintptr_t ShorterHistogram;33if (Block.AccessHistogramSize > lmib->mib.AccessHistogramSize)34ShorterHistogram = lmib->mib.AccessHistogram;35else36ShorterHistogram = Block.AccessHistogram;3738lmib->mib.Merge(Block);39// The larger histogram is kept and the shorter histogram is discarded after40// adding the counters to the larger historam. Free only the shorter41// Histogram42if (Block.AccessHistogramSize > 0 || lmib->mib.AccessHistogramSize > 0)43InternalFree((void *)ShorterHistogram);44}45}4647} // namespace __memprof484950