OCILIB (C and C++ Driver for Oracle)  4.7.6
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
MemoryDebugInfo.hpp
1 /*
2  * OCILIB - C Driver for Oracle (C Wrapper for Oracle OCI)
3  *
4  * Website: http://www.ocilib.net
5  *
6  * Copyright (c) 2007-2023 Vincent ROGIER <vince.rogier@ocilib.net>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #pragma once
22 
23 #include <algorithm>
24 
25 namespace ocilib
26 {
27  namespace core
28  {
29 #ifdef OCILIBPP_DEBUG_MEMORY_ENABLED
30 
31  struct MemoryAllocation
32  {
33  void* Address;
34  const char* Name;
35  size_t Size;
36  size_t Count;
37  };
38 
39  class MemoryDebugInfo
40  {
41  private:
42  std::vector<MemoryAllocation> Allocations;
43  public:
44 
45  template<class T>
46  void OnAllocate(T* address, size_t count)
47  {
48  Allocations.push_back({ address, typeid(T).name(), sizeof(T), count });
49  }
50 
51  template<class T>
52  void OnDeallocate(T* address)
53  {
54  auto it = std::find_if(std::begin(Allocations), std::end(Allocations), [address](const auto& alloc)
55  {
56  return alloc.Address == address;
57  });
58  if (it != std::end(Allocations))
59  {
60  Allocations.erase(it);
61  }
62  }
63 
64  void PrintAllocations()
65  {
66  if (Allocations.empty()) return;
67 
68  std::cout << "Unfreed memory found" << std::endl;
69 
70  for (auto& alloc : Allocations)
71  {
72  std::cout << "=> Address " << alloc.Address
73  << " - Size " << alloc.Count
74  << " - Count " << alloc.Size
75  << " - Type " << alloc.Name
76  << std::endl;
77  }
78  }
79  };
80 
81  inline MemoryDebugInfo& GetMemoryDebugInfo()
82  {
83  static MemoryDebugInfo memoryDebugInfo;
84 
85  return memoryDebugInfo;
86  }
87 
88 #endif
89  }
90 }
OCILIB ++ Namespace.