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 "profile/MemProfData.inc"
15#include "sanitizer_common/sanitizer_allocator_internal.h"
16#include "sanitizer_common/sanitizer_mutex.h"
17
18namespace __memprof {
19using ::llvm::memprof::MemInfoBlock;
20
21void InsertOrMerge(const uptr Id, const MemInfoBlock &Block, MIBMapTy &Map) {
22  MIBMapTy::Handle h(&Map, static_cast<uptr>(Id), /*remove=*/false,
23                     /*create=*/true);
24  if (h.created()) {
25    LockedMemInfoBlock *lmib =
26        (LockedMemInfoBlock *)InternalAlloc(sizeof(LockedMemInfoBlock));
27    lmib->mutex.Init();
28    lmib->mib = Block;
29    *h = lmib;
30  } else {
31    LockedMemInfoBlock *lmib = *h;
32    SpinMutexLock lock(&lmib->mutex);
33    lmib->mib.Merge(Block);
34  }
35}
36
37} // namespace __memprof
38