File: allocate.cpp

package info (click to toggle)
monotone 0.31-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 20,680 kB
  • ctags: 14,801
  • sloc: cpp: 87,711; ansic: 64,862; sh: 5,691; lisp: 954; perl: 783; makefile: 509; python: 265; sql: 98; sed: 16
file content (178 lines) | stat: -rw-r--r-- 5,095 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*************************************************
* Allocator Factory Source File                  *
* (C) 1999-2005 The Botan Project                *
*************************************************/

#include <botan/allocate.h>
#include <botan/defalloc.h>
#include <botan/mutex.h>
#include <botan/util.h>
#include <botan/init.h>
#include <map>

namespace Botan {

namespace {

/*************************************************
* A factory for creating Allocators              *
*************************************************/
class AllocatorFactory
   {
   public:
      Allocator* get(const std::string&) const;
      Allocator* get_default() const;
      void add(const std::string&, Allocator*);
      std::string set_default_allocator(const std::string&);

      AllocatorFactory() { factory_mutex = get_mutex(); }
      ~AllocatorFactory();
   private:
      std::map<std::string, Allocator*> alloc_map;
      std::string default_allocator;
      Mutex* factory_mutex;
   };

/*************************************************
* Get an allocator from the factory              *
*************************************************/
Allocator* AllocatorFactory::get(const std::string& type) const
   {
   Mutex_Holder lock(factory_mutex);

   std::map<std::string, Allocator*>::const_iterator iter;
   if(type == "default") iter = alloc_map.find(default_allocator);
   else                  iter = alloc_map.find(type);

   if(iter == alloc_map.end())
      return 0;
   return iter->second;
   }

/*************************************************
* Get the default allocator from the factory     *
*************************************************/
Allocator* AllocatorFactory::get_default() const
   {
   Mutex_Holder lock(factory_mutex);

   std::map<std::string, Allocator*>::const_iterator iter;
   iter = alloc_map.find(default_allocator);

   if(iter == alloc_map.end())
      return 0;
   return iter->second;
   }

/*************************************************
* Make a new type available to the factory       *
*************************************************/
void AllocatorFactory::add(const std::string& type, Allocator* allocator)
   {
   Mutex_Holder lock(factory_mutex);
   allocator->init();
   alloc_map[type] = allocator;
   }

/*************************************************
* Set the default allocator type                 *
*************************************************/
std::string AllocatorFactory::set_default_allocator(const std::string& alloc)
   {
   Mutex_Holder lock(factory_mutex);

   std::string old_default = default_allocator;
   default_allocator = alloc;
   return old_default;
   }

/*************************************************
* Destroy an allocator factory                   *
*************************************************/
AllocatorFactory::~AllocatorFactory()
   {
   std::map<std::string, Allocator*>::iterator iter;
   for(iter = alloc_map.begin(); iter != alloc_map.end(); iter++)
      {
      iter->second->destroy();
      delete iter->second;
      }
   delete factory_mutex;
   }

/*************************************************
* Global State                                   *
*************************************************/
AllocatorFactory* factory = 0;

}

/*************************************************
* Get an allocator                               *
*************************************************/
Allocator* get_allocator(const std::string& type)
   {
   if(!factory)
      throw Invalid_State("LibraryInitializer not created, or it failed");

   Allocator* alloc = 0;

   if(!type.empty())
      {
      alloc = factory->get(type);
      if(alloc) return alloc;
      }

   alloc = factory->get_default();
   if(alloc) return alloc;

   alloc = factory->get("locking");
   if(alloc) return alloc;

   throw Exception("Couldn't find an allocator to use in get_allocator");
   }

/*************************************************
* Set the default allocator type                 *
*************************************************/
std::string set_default_allocator(const std::string& type)
   {
   return factory->set_default_allocator(type);
   }

/*************************************************
* Add new allocator type                         *
*************************************************/
bool add_allocator_type(const std::string& type, Allocator* alloc)
   {
   if(type == "" || factory->get(type))
      return false;
   factory->add(type, alloc);
   return true;
   }

namespace Init {

/*************************************************
* Initialize the memory subsystem                *
*************************************************/
void startup_memory_subsystem()
   {
   factory = new AllocatorFactory;

   add_allocator_type("malloc", new Malloc_Allocator);
   add_allocator_type("locking", new Locking_Allocator);
   }

/*************************************************
* Shut down the memory subsystem                 *
*************************************************/
void shutdown_memory_subsystem()
   {
   delete factory;
   factory = 0;
   }

}

}