YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/allocation_tracker.h
Line
Count
Source (jump to first uncovered line)
1
//
2
// Copyright (c) YugaByte, Inc.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
// in compliance with the License.  You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software distributed under the License
10
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
// or implied.  See the License for the specific language governing permissions and limitations
12
// under the License.
13
//
14
//
15
16
#ifndef YB_UTIL_ALLOCATION_TRACKER_H
17
#define YB_UTIL_ALLOCATION_TRACKER_H
18
19
#include <atomic>
20
#include <mutex>
21
#include <unordered_map>
22
23
namespace yb {
24
25
class AllocationTrackerBase {
26
 protected:
27
0
  explicit AllocationTrackerBase(std::string name) : name_(std::move(name)) {}
28
  ~AllocationTrackerBase();
29
30
  void DoCreated(void* object);
31
  void DoDestroyed(void* object);
32
 private:
33
  std::string name_;
34
#ifndef NDEBUG
35
  std::mutex mutex_;
36
  size_t id_ = 0;
37
  std::unordered_map<void*, std::pair<std::string, size_t>> objects_;
38
#else
39
  std::atomic<std::ptrdiff_t> count_ = {0};
40
#endif
41
};
42
43
// This class is created as light ASAN replacement.
44
// To debug memory leaks under MAC OS.
45
// When one know class of leaked object.
46
//
47
// Usage is following, to constructor of MyClass add:
48
//    AllocationTracker<MyClass>::Created(this);
49
// in destructor:
50
//    AllocationTracker<MyClass>::Destroyed(this);
51
template<class T>
52
class AllocationTracker : public AllocationTrackerBase {
53
 public:
54
  static void Created(T* object) { Instance().DoCreated(object); }
55
  static void Destroyed(T* object) { Instance().DoDestroyed(object); }
56
57
 private:
58
  AllocationTracker() : AllocationTrackerBase(typeid(T).name()) {}
59
60
  static AllocationTracker<T>& Instance() {
61
    static AllocationTracker<T> instance;
62
    return instance;
63
  }
64
};
65
66
} // namespace yb
67
68
#endif // YB_UTIL_ALLOCATION_TRACKER_H