ATLAS Offline Software
Loading...
Searching...
No Matches
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
10namespace 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
24 clear();
25 deallocate(m_heap.heap);
26 m_heap.heap = nullptr;
27 m_heap.pos = nullptr;
28 }
29
30 /* @brief provide a clean heap of original size*/
31 void
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
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
76 std::string m_name{};
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
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
std::vector< void * > heapCollection
Definition Heap.h:72
Heap(const std::string &name, size_t capacity=120)
Definition Heap.h:15
void clear()
Definition Heap.h:32
size_t capacity() const
Definition Heap.h:62
std::string m_name
Definition Heap.h:76
void * allocate(size_t size)
Definition Heap.h:80
void extend()
Definition Heap.h:91
size_t m_originalCapacity
Definition Heap.h:77
size_t size() const
Definition Heap.h:58
T * create(const T &obj)
create an object on the heap
Definition Heap.h:50
void deallocate(void *&mem)
Definition Heap.h:85
HeapStructure m_heap
Definition Heap.h:75
~Heap()
Definition Heap.h:23