1/*
2Copyright (C) 1999-2007 The Botan Project. All rights reserved.
3
4Redistribution and use in source and binary forms, for any use, with or without
5modification, is permitted provided that the following conditions are met:
6
71. Redistributions of source code must retain the above copyright notice, this
8list of conditions, and the following disclaimer.
9
102. Redistributions in binary form must reproduce the above copyright notice,
11this list of conditions, and the following disclaimer in the documentation
12and/or other materials provided with the distribution.
13
14THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED
15WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
17
18IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT,
19INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*/
26// LICENSEHEADER_END
27namespace 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>
35namespace QCA { // WRAPNS_LINE
36} // WRAPNS_LINE
37#include <botan/libstate.h>
38namespace QCA { // WRAPNS_LINE
39} // WRAPNS_LINE
40#include <botan/util.h>
41namespace QCA { // WRAPNS_LINE
42} // WRAPNS_LINE
43#include <cstdlib>
44namespace QCA { // WRAPNS_LINE
45} // WRAPNS_LINE
46#include <cstring>
47namespace QCA { // WRAPNS_LINE
48} // WRAPNS_LINE
49#include <cstdlib>
50namespace QCA { // WRAPNS_LINE
51} // WRAPNS_LINE
52#include <cstring>
53namespace QCA { // WRAPNS_LINE
54
55namespace Botan {
56
57namespace {
58
59/*************************************************
60 * Perform Memory Allocation *
61 *************************************************/
62void *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 *************************************************/
79void 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 *************************************************/
96void *Malloc_Allocator::alloc_block(u32bit n)
97{
98 return do_malloc(n, do_lock: false);
99}
100
101/*************************************************
102 * Malloc_Allocator's Deallocation *
103 *************************************************/
104void 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 *************************************************/
112void *Locking_Allocator::alloc_block(u32bit n)
113{
114 return do_malloc(n, do_lock: true);
115}
116
117/*************************************************
118 * Locking_Allocator's Deallocation *
119 *************************************************/
120void 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 *************************************************/
128Allocator *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

source code of qca/src/botantools/botan/defalloc.cpp