monotone

monotone Mtn Source Tree

Root/botan/allocate.cpp

1/*************************************************
2* Allocator Factory Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/allocate.h>
7#include <botan/defalloc.h>
8#include <botan/mutex.h>
9#include <botan/util.h>
10#include <botan/init.h>
11#include <map>
12
13namespace Botan {
14
15namespace {
16
17/*************************************************
18* A factory for creating Allocators *
19*************************************************/
20class AllocatorFactory
21 {
22 public:
23 Allocator* get(const std::string&) const;
24 Allocator* get_default() const;
25 void add(const std::string&, Allocator*);
26 std::string set_default_allocator(const std::string&);
27
28 AllocatorFactory() { factory_mutex = get_mutex(); }
29 ~AllocatorFactory();
30 private:
31 std::map<std::string, Allocator*> alloc_map;
32 std::string default_allocator;
33 Mutex* factory_mutex;
34 };
35
36/*************************************************
37* Get an allocator from the factory *
38*************************************************/
39Allocator* AllocatorFactory::get(const std::string& type) const
40 {
41 Mutex_Holder lock(factory_mutex);
42
43 std::map<std::string, Allocator*>::const_iterator iter;
44 if(type == "default") iter = alloc_map.find(default_allocator);
45 else iter = alloc_map.find(type);
46
47 if(iter == alloc_map.end())
48 return 0;
49 return iter->second;
50 }
51
52/*************************************************
53* Get the default allocator from the factory *
54*************************************************/
55Allocator* AllocatorFactory::get_default() const
56 {
57 Mutex_Holder lock(factory_mutex);
58
59 std::map<std::string, Allocator*>::const_iterator iter;
60 iter = alloc_map.find(default_allocator);
61
62 if(iter == alloc_map.end())
63 return 0;
64 return iter->second;
65 }
66
67/*************************************************
68* Make a new type available to the factory *
69*************************************************/
70void AllocatorFactory::add(const std::string& type, Allocator* allocator)
71 {
72 Mutex_Holder lock(factory_mutex);
73 allocator->init();
74 alloc_map[type] = allocator;
75 }
76
77/*************************************************
78* Set the default allocator type *
79*************************************************/
80std::string AllocatorFactory::set_default_allocator(const std::string& alloc)
81 {
82 Mutex_Holder lock(factory_mutex);
83
84 std::string old_default = default_allocator;
85 default_allocator = alloc;
86 return old_default;
87 }
88
89/*************************************************
90* Destroy an allocator factory *
91*************************************************/
92AllocatorFactory::~AllocatorFactory()
93 {
94 std::map<std::string, Allocator*>::iterator iter;
95 for(iter = alloc_map.begin(); iter != alloc_map.end(); iter++)
96 {
97 iter->second->destroy();
98 delete iter->second;
99 }
100 delete factory_mutex;
101 }
102
103/*************************************************
104* Global State *
105*************************************************/
106AllocatorFactory* factory = 0;
107
108}
109
110/*************************************************
111* Get an allocator *
112*************************************************/
113Allocator* get_allocator(const std::string& type)
114 {
115 if(!factory)
116 throw Invalid_State("LibraryInitializer not created, or it failed");
117
118 Allocator* alloc = 0;
119
120 if(!type.empty())
121 {
122 alloc = factory->get(type);
123 if(alloc) return alloc;
124 }
125
126 alloc = factory->get_default();
127 if(alloc) return alloc;
128
129 alloc = factory->get("locking");
130 if(alloc) return alloc;
131
132 throw Exception("Couldn't find an allocator to use in get_allocator");
133 }
134
135/*************************************************
136* Set the default allocator type *
137*************************************************/
138std::string set_default_allocator(const std::string& type)
139 {
140 return factory->set_default_allocator(type);
141 }
142
143/*************************************************
144* Add new allocator type *
145*************************************************/
146bool add_allocator_type(const std::string& type, Allocator* alloc)
147 {
148 if(type == "" || factory->get(type))
149 return false;
150 factory->add(type, alloc);
151 return true;
152 }
153
154namespace Init {
155
156/*************************************************
157* Initialize the memory subsystem *
158*************************************************/
159void startup_memory_subsystem()
160 {
161 factory = new AllocatorFactory;
162
163 add_allocator_type("malloc", new Malloc_Allocator);
164 add_allocator_type("locking", new Locking_Allocator);
165 }
166
167/*************************************************
168* Shut down the memory subsystem *
169*************************************************/
170void shutdown_memory_subsystem()
171 {
172 delete factory;
173 factory = 0;
174 }
175
176}
177
178}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status