ATLAS Offline Software
ArenaAllocatorRegistry.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
18 #include <vector>
19 #include <map>
20 #include <mutex>
21 #include <cassert>
22 
23 
24 namespace SG {
25 
26 
31 {
32 public:
35 
36 
47  size_t registerCreator (const std::string& name,
48  std::unique_ptr<ArenaAllocatorCreator> creator);
49 
50 
57  size_t lookup (const std::string& name);
58 
59 
65  std::unique_ptr<ArenaAllocatorBase> create (size_t i);
66 
67 
68 private:
70  typedef std::map<std::string, size_t> map_t;
72 
74  std::vector<std::unique_ptr<ArenaAllocatorCreator> > m_creators;
75 
78 
79  typedef std::lock_guard<std::mutex> lock_t;
80 };
81 
82 
87 {
88 }
89 
90 
101 size_t
103  std::unique_ptr<ArenaAllocatorCreator> creator)
104 {
105  lock_t lock (m_mutex);
106 
107  // See if there's an existing one by this name.
108  // Shouldn't usually happen, since we check that the allocator doesn't
109  // exist before calling this, but could happen in a MT job.
110  // So we need to check it again with the lock held.
111  map_t::iterator it = m_map.find (name);
112  if (it != m_map.end()) {
113  // creator will be deleted.
114  return it->second;
115  }
116 
117  // The new index.
118  size_t i = m_creators.size();
119 
120  // Remember the index and store the creator.
121  m_map[name] = i;
122  m_creators.push_back (std::move (creator));
123  return i;
124 }
125 
126 
133 size_t ArenaAllocatorRegistryImpl::lookup (const std::string& name)
134 {
135  lock_t lock (m_mutex);
136  map_t::iterator it = m_map.find (name);
137  if (it == m_map.end()) {
138  return std::string::npos;
139  }
140  return it->second;
141 }
142 
143 
149 std::unique_ptr<ArenaAllocatorBase>
151 {
152  lock_t lock (m_mutex);
153  assert (i < m_creators.size());
154  return m_creators[i]->create();
155 }
156 
157 
158 //==========================================================================
159 
170 size_t
172  std::unique_ptr<ArenaAllocatorCreator> creator)
173 {
174  return m_impl->registerCreator (name, std::move (creator));
175 }
176 
177 
184 size_t ArenaAllocatorRegistry::lookup (const std::string& name)
185 {
186  return m_impl->lookup (name);
187 }
188 
189 
195 std::unique_ptr<ArenaAllocatorBase> ArenaAllocatorRegistry::create (size_t i)
196 {
197  return m_impl->create (i);
198 }
199 
200 
205 {
207  return &tmp;
208 }
209 
210 
215  : m_impl (std::make_unique<ArenaAllocatorRegistryImpl>())
216 {
217 }
218 
219 
224 {
225 }
226 
227 
228 } // namespace SG
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
SG::ArenaAllocatorRegistryImpl::create
std::unique_ptr< ArenaAllocatorBase > create(size_t i)
Create a new instance of an allocator.
Definition: ArenaAllocatorRegistry.cxx:150
SG::ArenaAllocatorRegistry::instance
static ArenaAllocatorRegistry * instance()
Return a pointer to the global ArenaAllocatorRegistry instance.
Definition: ArenaAllocatorRegistry.cxx:204
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: SkimmingToolEXOT5.cxx:23
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
skel.it
it
Definition: skel.GENtoEVGEN.py:396
SG::ArenaAllocatorRegistryImpl::map_t
std::map< std::string, size_t > map_t
Map from names to indices.
Definition: ArenaAllocatorRegistry.cxx:70
SG::ArenaAllocatorRegistryImpl
ArenaAllocatorRegistry implementation class.
Definition: ArenaAllocatorRegistry.cxx:31
SG::ArenaAllocatorRegistry::m_impl
std::unique_ptr< ArenaAllocatorRegistryImpl > m_impl
The implementation object.
Definition: ArenaAllocatorRegistry.h:85
ArenaAllocatorCreator.h
Provide an interface for creating an arena Allocator. See Arena.h for an overview of the arena-based ...
SG::ArenaAllocatorRegistry::ArenaAllocatorRegistry
ArenaAllocatorRegistry()
Constructor. Called only by instance.
Definition: ArenaAllocatorRegistry.cxx:214
SG::ArenaAllocatorRegistryImpl::m_map
map_t m_map
Definition: ArenaAllocatorRegistry.cxx:71
lumiFormat.i
int i
Definition: lumiFormat.py:85
SG::ArenaAllocatorRegistry
Registry of allocator factories.
Definition: ArenaAllocatorRegistry.h:44
ArenaAllocatorBase.h
Common base class for arena allocator classes. See Arena.h for an overview of the arena-based memory ...
SG::ArenaAllocatorRegistry::create
std::unique_ptr< ArenaAllocatorBase > create(size_t i)
Create a new instance of an allocator.
Definition: ArenaAllocatorRegistry.cxx:195
SG::ArenaAllocatorRegistryImpl::lookup
size_t lookup(const std::string &name)
Look up the index for an allocator type name.
Definition: ArenaAllocatorRegistry.cxx:133
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
SG::ArenaAllocatorRegistry::lookup
size_t lookup(const std::string &name)
Look up the index for an allocator type name.
Definition: ArenaAllocatorRegistry.cxx:184
SG::ArenaAllocatorRegistry::~ArenaAllocatorRegistry
~ArenaAllocatorRegistry()
Destructor. Called only by instance.
Definition: ArenaAllocatorRegistry.cxx:223
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
SG::ArenaAllocatorRegistry::registerCreator
size_t registerCreator(const std::string &name, std::unique_ptr< ArenaAllocatorCreator > creator)
Register a new allocator type.
Definition: ArenaAllocatorRegistry.cxx:171
SG::ArenaAllocatorRegistryImpl::m_mutex
std::mutex m_mutex
Mutex to protect the contents.
Definition: ArenaAllocatorRegistry.cxx:77
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
SG::ArenaAllocatorRegistryImpl::registerCreator
size_t registerCreator(const std::string &name, std::unique_ptr< ArenaAllocatorCreator > creator)
Register a new allocator type.
Definition: ArenaAllocatorRegistry.cxx:102
ArenaAllocatorRegistry.h
Registry of allocator factories. See Arena.h for an overview of the arena-based memory allocators.
checker_macros.h
Define macros for attributes used to control the static checker.
SG::ArenaAllocatorRegistryImpl::m_creators
std::vector< std::unique_ptr< ArenaAllocatorCreator > > m_creators
Vector of factory instances.
Definition: ArenaAllocatorRegistry.cxx:74
SG::ArenaAllocatorRegistryImpl::~ArenaAllocatorRegistryImpl
~ArenaAllocatorRegistryImpl()
Destructor.
Definition: ArenaAllocatorRegistry.cxx:86
SG::ArenaAllocatorRegistryImpl::lock_t
std::lock_guard< std::mutex > lock_t
Definition: ArenaAllocatorRegistry.cxx:79