1 | /* |
2 | Copyright (C) 1999-2007 The Botan Project. All rights reserved. |
3 | |
4 | Redistribution and use in source and binary forms, for any use, with or without |
5 | modification, is permitted provided that the following conditions are met: |
6 | |
7 | 1. Redistributions of source code must retain the above copyright notice, this |
8 | list of conditions, and the following disclaimer. |
9 | |
10 | 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | this list of conditions, and the following disclaimer in the documentation |
12 | and/or other materials provided with the distribution. |
13 | |
14 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED |
15 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
16 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. |
17 | |
18 | IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT, |
19 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
20 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
21 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
22 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
23 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
24 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | // LICENSEHEADER_END |
27 | namespace QCA { // WRAPNS_LINE |
28 | /************************************************* |
29 | * Basic Allocators Source File * |
30 | * (C) 1999-2007 The Botan Project * |
31 | *************************************************/ |
32 | |
33 | } // WRAPNS_LINE |
34 | #include <botan/defalloc.h> |
35 | namespace QCA { // WRAPNS_LINE |
36 | } // WRAPNS_LINE |
37 | #include <botan/libstate.h> |
38 | namespace QCA { // WRAPNS_LINE |
39 | } // WRAPNS_LINE |
40 | #include <botan/util.h> |
41 | namespace QCA { // WRAPNS_LINE |
42 | } // WRAPNS_LINE |
43 | #include <cstdlib> |
44 | namespace QCA { // WRAPNS_LINE |
45 | } // WRAPNS_LINE |
46 | #include <cstring> |
47 | namespace QCA { // WRAPNS_LINE |
48 | } // WRAPNS_LINE |
49 | #include <cstdlib> |
50 | namespace QCA { // WRAPNS_LINE |
51 | } // WRAPNS_LINE |
52 | #include <cstring> |
53 | namespace QCA { // WRAPNS_LINE |
54 | |
55 | namespace Botan { |
56 | |
57 | namespace { |
58 | |
59 | /************************************************* |
60 | * Perform Memory Allocation * |
61 | *************************************************/ |
62 | void *do_malloc(u32bit n, bool do_lock) |
63 | { |
64 | void *ptr = malloc(size: n); |
65 | |
66 | if (!ptr) |
67 | return nullptr; |
68 | |
69 | if (do_lock) |
70 | lock_mem(ptr, n); |
71 | |
72 | memset(s: ptr, c: 0, n: n); |
73 | return ptr; |
74 | } |
75 | |
76 | /************************************************* |
77 | * Perform Memory Deallocation * |
78 | *************************************************/ |
79 | void do_free(void *ptr, u32bit n, bool do_lock) |
80 | { |
81 | if (!ptr) |
82 | return; |
83 | |
84 | memset(s: ptr, c: 0, n: n); |
85 | if (do_lock) |
86 | unlock_mem(ptr, n); |
87 | |
88 | free(ptr: ptr); |
89 | } |
90 | |
91 | } |
92 | |
93 | /************************************************* |
94 | * Malloc_Allocator's Allocation * |
95 | *************************************************/ |
96 | void *Malloc_Allocator::alloc_block(u32bit n) |
97 | { |
98 | return do_malloc(n, do_lock: false); |
99 | } |
100 | |
101 | /************************************************* |
102 | * Malloc_Allocator's Deallocation * |
103 | *************************************************/ |
104 | void Malloc_Allocator::dealloc_block(void *ptr, u32bit n) |
105 | { |
106 | do_free(ptr, n, do_lock: false); |
107 | } |
108 | |
109 | /************************************************* |
110 | * Locking_Allocator's Allocation * |
111 | *************************************************/ |
112 | void *Locking_Allocator::alloc_block(u32bit n) |
113 | { |
114 | return do_malloc(n, do_lock: true); |
115 | } |
116 | |
117 | /************************************************* |
118 | * Locking_Allocator's Deallocation * |
119 | *************************************************/ |
120 | void Locking_Allocator::dealloc_block(void *ptr, u32bit n) |
121 | { |
122 | do_free(ptr, n, do_lock: true); |
123 | } |
124 | |
125 | /************************************************* |
126 | * Get an allocator * |
127 | *************************************************/ |
128 | Allocator *Allocator::get(bool locking) |
129 | { |
130 | std::string type = "" ; |
131 | if (!locking) |
132 | type = "malloc" ; |
133 | |
134 | Allocator *alloc = global_state().get_allocator(type); |
135 | if (alloc) |
136 | return alloc; |
137 | |
138 | throw Exception("Couldn't find an allocator to use in get_allocator" ); |
139 | } |
140 | |
141 | } |
142 | } // WRAPNS_LINE |
143 | |