]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/cssmalloc.h
Security-54.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / cssmalloc.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // cssmalloc - memory allocation in the CDSA world
21 //
22 #ifndef _H_CSSMALLOC
23 #define _H_CSSMALLOC
24
25 #include <Security/utilities.h>
26 #include <Security/cssm.h>
27 #include <cstring>
28
29 #ifdef _CPP_CSSMALLOC
30 # pragma export on
31 #endif
32
33 namespace Security
34 {
35
36 //
37 // An abstract allocator superclass, based on the simple malloc/realloc/free paradigm
38 // that CDSA loves so much. If you have an allocation strategy and want objects
39 // to be allocated through it, inherit from this.
40 //
41 class CssmAllocator {
42 public:
43 virtual ~CssmAllocator();
44 virtual void *malloc(size_t) throw(std::bad_alloc) = 0;
45 virtual void free(void *) throw() = 0;
46 virtual void *realloc(void *, size_t) throw(std::bad_alloc) = 0;
47
48 //
49 // Template versions for added expressiveness.
50 // Note that the integers are element counts, not byte sizes.
51 //
52 template <class T> T *alloc() throw(std::bad_alloc)
53 { return reinterpret_cast<T *>(malloc(sizeof(T))); }
54
55 template <class T> T *alloc(uint32 count) throw(std::bad_alloc)
56 { return reinterpret_cast<T *>(malloc(sizeof(T) * count)); }
57
58 template <class T> T *alloc(T *old, uint32 count) throw(std::bad_alloc)
59 { return reinterpret_cast<T *>(realloc(old, sizeof(T) * count)); }
60
61 template <class Data> CssmData alloc(const Data &source) throw(std::bad_alloc)
62 {
63 size_t length = source.length();
64 return CssmData(memcpy(malloc(length), source.data(), length), length);
65 }
66
67 //
68 // Happier malloc/realloc for any type. Note that these still have
69 // the original (byte-sized) argument profile.
70 //
71 template <class T> T *malloc(size_t size) throw(std::bad_alloc)
72 { return reinterpret_cast<T *>(malloc(size)); }
73
74 template <class T> T *realloc(void *addr, size_t size) throw(std::bad_alloc)
75 { return reinterpret_cast<T *>(realloc(addr, size)); }
76
77 // All right, if you *really* have to have calloc...
78 void *calloc(size_t size, unsigned int count) throw(std::bad_alloc)
79 {
80 void *addr = malloc(size * count);
81 memset(addr, 0, size * count);
82 return addr;
83 }
84
85 // compare CssmAllocators for identity
86 virtual bool operator == (const CssmAllocator &alloc) const throw();
87
88 public:
89 // allocator chooser options
90 enum {
91 normal = 0x0000,
92 sensitive = 0x0001
93 };
94
95 static CssmAllocator &standard(uint32 request = normal);
96 };
97
98
99 //
100 // A POD wrapper for the memory functions structure passed around in CSSM.
101 //
102 class CssmMemoryFunctions : public PodWrapper<CssmMemoryFunctions, CSSM_MEMORY_FUNCS> {
103 public:
104 CssmMemoryFunctions(const CSSM_MEMORY_FUNCS &funcs)
105 { *(CSSM_MEMORY_FUNCS *)this = funcs; }
106 CssmMemoryFunctions() { }
107
108 void *malloc(size_t size) const throw(std::bad_alloc);
109 void free(void *mem) const throw() { free_func(mem, AllocRef); }
110 void *realloc(void *mem, size_t size) const throw(std::bad_alloc);
111 void *calloc(uint32 count, size_t size) const throw(std::bad_alloc);
112
113 bool operator == (const CSSM_MEMORY_FUNCS &other) const throw()
114 { return !memcmp(this, &other, sizeof(*this)); }
115 };
116
117 inline void *CssmMemoryFunctions::malloc(size_t size) const throw(std::bad_alloc)
118 {
119 if (void *addr = malloc_func(size, AllocRef))
120 return addr;
121 throw std::bad_alloc();
122 }
123
124 inline void *CssmMemoryFunctions::calloc(uint32 count, size_t size) const throw(std::bad_alloc)
125 {
126 if (void *addr = calloc_func(count, size, AllocRef))
127 return addr;
128 throw std::bad_alloc();
129 }
130
131 inline void *CssmMemoryFunctions::realloc(void *mem, size_t size) const throw(std::bad_alloc)
132 {
133 if (void *addr = realloc_func(mem, size, AllocRef))
134 return addr;
135 throw std::bad_alloc();
136 }
137
138
139 //
140 // A CssmAllocator based on CssmMemoryFunctions
141 //
142 class CssmMemoryFunctionsAllocator : public CssmAllocator {
143 public:
144 CssmMemoryFunctionsAllocator(const CssmMemoryFunctions &memFuncs) : functions(memFuncs) { }
145
146 void *malloc(size_t size) throw(std::bad_alloc);
147 void free(void *addr) throw();
148 void *realloc(void *addr, size_t size) throw(std::bad_alloc);
149
150 operator const CssmMemoryFunctions & () const throw() { return functions; }
151
152 private:
153 const CssmMemoryFunctions functions;
154 };
155
156 } // end namespace Security
157
158 //
159 // Global C++ allocation hooks to use CssmAllocators
160 //
161 inline void *operator new (size_t size, CssmAllocator &allocator) throw(std::bad_alloc)
162 { return allocator.malloc(size); }
163
164 //
165 // You'd think that this is operator delete(const T *, CssmAllocator &), but you'd
166 // be wrong. Specialized operator delete is only called during constructor cleanup.
167 // Use this to cleanly destroy things.
168 //
169 template <class T>
170 inline void destroy(T *obj, CssmAllocator &alloc) throw()
171 {
172 obj->~T();
173 alloc.free(obj);
174 }
175
176 // untyped (release memory only, no destructor call)
177 inline void destroy(void *obj, CssmAllocator &alloc) throw()
178 {
179 alloc.free(obj);
180 }
181
182 namespace Security
183 {
184
185 //
186 // A MemoryFunctions object based on a CssmAllocator.
187 // Note that we don't copy the CssmAllocator object. It needs to live (at least)
188 // as long as any CssmAllocatorMemoryFunctions object based on it.
189 //
190 class CssmAllocatorMemoryFunctions : public CssmMemoryFunctions {
191 public:
192 CssmAllocatorMemoryFunctions(CssmAllocator &alloc);
193 CssmAllocatorMemoryFunctions() { /*IFDEBUG(*/ AllocRef = NULL /*)*/ ; } // later assignment req'd
194
195 private:
196 static void *relayMalloc(size_t size, void *ref) throw(std::bad_alloc);
197 static void relayFree(void *mem, void *ref) throw();
198 static void *relayRealloc(void *mem, size_t size, void *ref) throw(std::bad_alloc);
199 static void *relayCalloc(uint32 count, size_t size, void *ref) throw(std::bad_alloc);
200
201 static CssmAllocator &allocator(void *ref) throw()
202 { return *reinterpret_cast<CssmAllocator *>(ref); }
203 };
204
205
206 //
207 // A mixin class to automagically manage your allocator.
208 // To allow allocation (of your object) from any instance of CssmAllocator,
209 // inherit from CssmHeap. Your users can then create heap instances of your thing by
210 // new (an-allocator) YourClass(...)
211 // or (still)
212 // new YourClass(...)
213 // for the default allocation source. The beauty is that when someone does a
214 // delete pointer-to-your-instance
215 // then the magic fairies will find the allocator that created the object and ask it
216 // to free the memory (by calling its free() method).
217 // The price of all that glory is memory overhead - typically one pointer per object.
218 //
219 class CssmHeap {
220 public:
221 void *operator new (size_t size, CssmAllocator *alloc = NULL) throw(std::bad_alloc);
222 void operator delete (void *addr, size_t size) throw();
223 void operator delete (void *addr, size_t size, CssmAllocator *alloc) throw();
224 };
225
226
227 //
228 // Here is a version of auto_ptr that works with CssmAllocators. It is designed
229 // to be pretty much a drop-in replacement. It requires an allocator as a constructor
230 // argument, of course.
231 // Note that CssmAutoPtr<void> is perfectly valid, unlike its auto_ptr look-alike.
232 // You can't dereference it, naturally.
233 //
234 template <class T>
235 class CssmAutoPtr {
236 public:
237 CssmAllocator &allocator;
238
239 CssmAutoPtr(CssmAllocator &alloc = CssmAllocator::standard())
240 : allocator(alloc), mine(NULL) { }
241 CssmAutoPtr(CssmAllocator &alloc, T *p)
242 : allocator(alloc), mine(p) { }
243 CssmAutoPtr(T *p)
244 : allocator(CssmAllocator::standard()), mine(p) { }
245 template <class T1> CssmAutoPtr(CssmAutoPtr<T1> &src)
246 : allocator(src.allocator), mine(src.release()) { }
247 template <class T1> CssmAutoPtr(CssmAllocator &alloc, CssmAutoPtr<T1> &src)
248 : allocator(alloc), mine(rc.release()) { assert(allocator == src.allocator); }
249
250 ~CssmAutoPtr() { allocator.free(mine); }
251
252 T *get() const throw() { return mine; }
253 T *release() { T *result = mine; mine = NULL; return result; }
254 void reset() { allocator.free(mine); mine = NULL; }
255
256 operator T * () const { return mine; }
257 T *operator -> () const { return mine; }
258 T &operator * () const { assert(mine); return *mine; }
259
260 private:
261 T *mine;
262 };
263
264 // specialization for void (i.e. void *), omitting the troublesome dereferencing ops.
265 template <>
266 class CssmAutoPtr<void> {
267 public:
268 CssmAllocator &allocator;
269
270 CssmAutoPtr(CssmAllocator &alloc) : allocator(alloc), mine(NULL) { }
271 CssmAutoPtr(CssmAllocator &alloc, void *p) : allocator(alloc), mine(p) { }
272 template <class T1> CssmAutoPtr(CssmAutoPtr<T1> &src)
273 : allocator(src.allocator), mine(src.release()) { }
274 template <class T1> CssmAutoPtr(CssmAllocator &alloc, CssmAutoPtr<T1> &src)
275 : allocator(alloc), mine(rc.release()) { assert(allocator == src.allocator); }
276
277 ~CssmAutoPtr() { destroy(mine, allocator); }
278
279 void *get() throw() { return mine; }
280 void *release() { void *result = mine; mine = NULL; return result; }
281 void reset() { allocator.free(mine); mine = NULL; }
282
283 private:
284 void *mine;
285 };
286
287
288 //
289 // Convenience forms of CssmAutoPtr that automatically make their (initial) object.
290 //
291 template <class T>
292 class CssmNewAutoPtr : public CssmAutoPtr<T> {
293 public:
294 CssmNewAutoPtr(CssmAllocator &alloc = CssmAllocator::standard())
295 : CssmAutoPtr<T>(alloc, new(alloc) T) { }
296
297 template <class A1>
298 CssmNewAutoPtr(CssmAllocator &alloc, A1 &arg1) : CssmAutoPtr<T>(alloc, new(alloc) T(arg1)) { }
299 template <class A1>
300 CssmNewAutoPtr(CssmAllocator &alloc, const A1 &arg1)
301 : CssmAutoPtr<T>(alloc, new(alloc) T(arg1)) { }
302
303 template <class A1, class A2>
304 CssmNewAutoPtr(CssmAllocator &alloc, A1 &arg1, A2 &arg2)
305 : CssmAutoPtr<T>(alloc, new(alloc) T(arg1, arg2)) { }
306 template <class A1, class A2>
307 CssmNewAutoPtr(CssmAllocator &alloc, const A1 &arg1, A2 &arg2)
308 : CssmAutoPtr<T>(alloc, new(alloc) T(arg1, arg2)) { }
309 template <class A1, class A2>
310 CssmNewAutoPtr(CssmAllocator &alloc, A1 &arg1, const A2 &arg2)
311 : CssmAutoPtr<T>(alloc, new(alloc) T(arg1, arg2)) { }
312 template <class A1, class A2>
313 CssmNewAutoPtr(CssmAllocator &alloc, const A1 &arg1, const A2 &arg2)
314 : CssmAutoPtr<T>(alloc, new(alloc) T(arg1, arg2)) { }
315 };
316
317
318 //
319 // A generic helper for the unhappily ubiquitous CSSM-style
320 // (count, pointer-to-array) style of arrays.
321 //
322 template <class Base, class Wrapper = Base>
323 class CssmVector {
324 public:
325 CssmVector(uint32 &cnt, Base * &vec, CssmAllocator &alloc = CssmAllocator::standard())
326 : count(cnt), vector(reinterpret_cast<Wrapper * &>(vec)),
327 allocator(alloc)
328 {
329 count = 0;
330 vector = NULL;
331 }
332
333 ~CssmVector() { allocator.free(vector); }
334
335 uint32 &count;
336 Wrapper * &vector;
337 CssmAllocator &allocator;
338
339 public:
340 Wrapper &operator [] (uint32 ix)
341 { assert(ix < count); return vector[ix]; }
342
343 void operator += (const Wrapper &add)
344 {
345 vector = reinterpret_cast<Wrapper *>(allocator.realloc(vector, (count + 1) * sizeof(Wrapper)));
346 //@@@???compiler bug??? vector = allocator.alloc<Wrapper>(vector, count + 1);
347 vector[count++] = add;
348 }
349 };
350
351
352 } // end namespace Security
353
354 #ifdef _CPP_CSSMALLOC
355 # pragma export off
356 #endif
357
358 #endif //_H_CSSMALLOC