ATLAS Offline Software
Loading...
Searching...
No Matches
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
12
13
18#include <vector>
19#include <map>
20#include <mutex>
21#include <cassert>
22
23
24namespace SG {
25
26
31{
32public:
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
68private:
70 typedef std::map<std::string, size_t> map_t;
72
74 std::vector<std::unique_ptr<ArenaAllocatorCreator> > m_creators;
75
77 std::mutex m_mutex;
78
79 typedef std::lock_guard<std::mutex> lock_t;
80};
81
82
89
90
101size_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
133size_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
149std::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
170size_t
172 std::unique_ptr<ArenaAllocatorCreator> creator)
173{
174 return m_impl->registerCreator (name, std::move (creator));
175}
176
177
184size_t ArenaAllocatorRegistry::lookup (const std::string& name)
185{
186 return m_impl->lookup (name);
187}
188
189
195std::unique_ptr<ArenaAllocatorBase> ArenaAllocatorRegistry::create (size_t i)
196{
197 return m_impl->create (i);
198}
199
200
209
210
218
219
226
227
228} // namespace SG
Common base class for arena allocator classes. See Arena.h for an overview of the arena-based memory ...
Provide an interface for creating an arena Allocator. See Arena.h for an overview of the arena-based ...
Registry of allocator factories. See Arena.h for an overview of the arena-based memory allocators.
Define macros for attributes used to control the static checker.
#define ATLAS_THREAD_SAFE
ArenaAllocatorRegistry implementation class.
std::unique_ptr< ArenaAllocatorBase > create(size_t i)
Create a new instance of an allocator.
std::map< std::string, size_t > map_t
Map from names to indices.
size_t lookup(const std::string &name)
Look up the index for an allocator type name.
size_t registerCreator(const std::string &name, std::unique_ptr< ArenaAllocatorCreator > creator)
Register a new allocator type.
std::vector< std::unique_ptr< ArenaAllocatorCreator > > m_creators
Vector of factory instances.
std::lock_guard< std::mutex > lock_t
std::mutex m_mutex
Mutex to protect the contents.
std::unique_ptr< ArenaAllocatorBase > create(size_t i)
Create a new instance of an allocator.
size_t registerCreator(const std::string &name, std::unique_ptr< ArenaAllocatorCreator > creator)
Register a new allocator type.
ArenaAllocatorRegistry()
Constructor. Called only by instance.
ArenaAllocatorRegistry(const ArenaAllocatorRegistry &)
size_t lookup(const std::string &name)
Look up the index for an allocator type name.
~ArenaAllocatorRegistry()
Destructor. Called only by instance.
std::unique_ptr< ArenaAllocatorRegistryImpl > m_impl
The implementation object.
static ArenaAllocatorRegistry * instance()
Return a pointer to the global ArenaAllocatorRegistry instance.
Forward declaration.
STL namespace.