ATLAS Offline Software
Heap.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 #ifndef L1TOPOEVENT_HEAP
4 #define L1TOPOEVENT_HEAP
5 
6 #include <vector>
7 #include <cstdlib>
8 #include <string>
9 
10 namespace TCS {
11 
12  template<class T>
13  class Heap {
14  public:
15  Heap(const std::string & name, size_t capacity = 120) :
16  m_name(name),
18  {
20  m_heap.pos = (T*)m_heap.heap;
21  }
22 
23  ~Heap() {
24  clear();
26  m_heap.heap = nullptr;
27  m_heap.pos = nullptr;
28  }
29 
30  /* @brief provide a clean heap of original size*/
31  void
32  clear() {
33  // destruct objects on the currently active memory block
34  T * p = (T*)m_heap.heap;
35  while(p != m_heap.pos) {
36  (p++)->~T();
37  }
38  m_heap.pos = (T*)m_heap.heap; // reset the position to the beginning of the active memory block
39  // destroy objects on all other memory blocks
40  for(void * heap : m_heap.heapCollection) {
41  T * p = (T*)heap;
42  for(unsigned int t=0; t<m_originalCapacity; ++t)
43  (p++)->~T();
44  deallocate(heap); // destroy other blocks
45  }
46  m_heap.heapCollection.clear(); // clear the collection of memory blocks
47  }
48 
50  T* create(const T & obj) {
51  if( (int)(m_heap.pos - (T*)m_heap.heap) == (int)m_originalCapacity ) {
52  extend();
53  }
54  T* newObject = new(m_heap.pos++) T(obj);
55  return newObject;
56  }
57 
58  size_t size() const {
59  return (m_heap.pos - (T*)m_heap.heap) + m_heap.heapCollection.size() * m_originalCapacity;
60  }
61 
62  size_t capacity() const {
63  return (m_heap.heapCollection.size() + 1) * m_originalCapacity;
64  }
65 
66  private:
67 
68  class HeapStructure {
69  public:
70  void * heap {nullptr}; // starting point of the currently active memory block
71  T * pos {nullptr}; // position in the currently active memory block
72  std::vector<void*> heapCollection; // vector of pointers to already allocated memory block
73  };
74 
75  HeapStructure m_heap;
76  std::string m_name{};
77  size_t m_originalCapacity{0};
78 
79  inline void *
80  allocate(size_t size) {
81  return ::operator new ( size * sizeof(T) );
82  }
83 
84  inline void
85  deallocate(void* & mem) {
86  ::operator delete ( mem );
87  mem = nullptr;
88  }
89 
90  void
91  extend() {
92  m_heap.heapCollection.push_back(m_heap.heap); // park the old heap which has grown too small
93  m_heap.heap = allocate(m_originalCapacity); // create a new heap the same size as the original one
94  m_heap.pos = (T*)m_heap.heap; // make current position point to it
95  }
96  };
97 }
98 
99 #endif
TCS::Heap::clear
void clear()
Definition: Heap.h:32
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
TCS::Heap::~Heap
~Heap()
Definition: Heap.h:23
TCS::Heap::m_originalCapacity
size_t m_originalCapacity
Definition: Heap.h:77
TCS::Heap::deallocate
void deallocate(void *&mem)
Definition: Heap.h:85
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
TCS::Heap
Definition: Heap.h:13
TCS::Heap::extend
void extend()
Definition: Heap.h:91
TCS::Heap::HeapStructure::pos
T * pos
Definition: Heap.h:71
TCS::Heap::Heap
Heap(const std::string &name, size_t capacity=120)
Definition: Heap.h:15
TCS::Heap::HeapStructure::heapCollection
std::vector< void * > heapCollection
Definition: Heap.h:72
TCS::Heap::m_heap
HeapStructure m_heap
Definition: Heap.h:75
TCS::Heap::capacity
size_t capacity() const
Definition: Heap.h:62
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
TCS::Heap::create
T * create(const T &obj)
create an object on the heap
Definition: Heap.h:50
TCS::Heap::size
size_t size() const
Definition: Heap.h:58
TCS::Heap::allocate
void * allocate(size_t size)
Definition: Heap.h:80
TCS::Heap::m_name
std::string m_name
Definition: Heap.h:76
TCS::Heap::HeapStructure::heap
void * heap
Definition: Heap.h:70
TCS
Definition: Global/GlobalSimulation/src/IO/Decision.h:18
python.PyAthena.obj
obj
Definition: PyAthena.py:135
TCS::Heap::HeapStructure
Definition: Heap.h:68