File indexing completed on 2024-05-12 04:45:14

0001 /*
0002 Copyright (C) 1999-2007 The Botan Project. All rights reserved.
0003 
0004 Redistribution and use in source and binary forms, for any use, with or without
0005 modification, is permitted provided that the following conditions are met:
0006 
0007 1. Redistributions of source code must retain the above copyright notice, this
0008 list of conditions, and the following disclaimer.
0009 
0010 2. Redistributions in binary form must reproduce the above copyright notice,
0011 this list of conditions, and the following disclaimer in the documentation
0012 and/or other materials provided with the distribution.
0013 
0014 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED
0015 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0016 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
0017 
0018 IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT,
0019 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
0020 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0022 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
0023 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
0024 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025 */
0026 // LICENSEHEADER_END
0027 namespace QCA { // WRAPNS_LINE
0028 /*************************************************
0029  * Basic Allocators Source File                   *
0030  * (C) 1999-2007 The Botan Project                *
0031  *************************************************/
0032 
0033 } // WRAPNS_LINE
0034 #include <botan/defalloc.h>
0035 namespace QCA { // WRAPNS_LINE
0036 } // WRAPNS_LINE
0037 #include <botan/libstate.h>
0038 namespace QCA { // WRAPNS_LINE
0039 } // WRAPNS_LINE
0040 #include <botan/util.h>
0041 namespace QCA { // WRAPNS_LINE
0042 } // WRAPNS_LINE
0043 #include <cstdlib>
0044 namespace QCA { // WRAPNS_LINE
0045 } // WRAPNS_LINE
0046 #include <cstring>
0047 namespace QCA { // WRAPNS_LINE
0048 } // WRAPNS_LINE
0049 #include <cstdlib>
0050 namespace QCA { // WRAPNS_LINE
0051 } // WRAPNS_LINE
0052 #include <cstring>
0053 namespace QCA { // WRAPNS_LINE
0054 
0055 namespace Botan {
0056 
0057 namespace {
0058 
0059 /*************************************************
0060  * Perform Memory Allocation                      *
0061  *************************************************/
0062 void *do_malloc(u32bit n, bool do_lock)
0063 {
0064     void *ptr = malloc(n);
0065 
0066     if (!ptr)
0067         return nullptr;
0068 
0069     if (do_lock)
0070         lock_mem(ptr, n);
0071 
0072     memset(ptr, 0, n);
0073     return ptr;
0074 }
0075 
0076 /*************************************************
0077  * Perform Memory Deallocation                    *
0078  *************************************************/
0079 void do_free(void *ptr, u32bit n, bool do_lock)
0080 {
0081     if (!ptr)
0082         return;
0083 
0084     memset(ptr, 0, n);
0085     if (do_lock)
0086         unlock_mem(ptr, n);
0087 
0088     free(ptr);
0089 }
0090 
0091 }
0092 
0093 /*************************************************
0094  * Malloc_Allocator's Allocation                  *
0095  *************************************************/
0096 void *Malloc_Allocator::alloc_block(u32bit n)
0097 {
0098     return do_malloc(n, false);
0099 }
0100 
0101 /*************************************************
0102  * Malloc_Allocator's Deallocation                *
0103  *************************************************/
0104 void Malloc_Allocator::dealloc_block(void *ptr, u32bit n)
0105 {
0106     do_free(ptr, n, false);
0107 }
0108 
0109 /*************************************************
0110  * Locking_Allocator's Allocation                 *
0111  *************************************************/
0112 void *Locking_Allocator::alloc_block(u32bit n)
0113 {
0114     return do_malloc(n, true);
0115 }
0116 
0117 /*************************************************
0118  * Locking_Allocator's Deallocation               *
0119  *************************************************/
0120 void Locking_Allocator::dealloc_block(void *ptr, u32bit n)
0121 {
0122     do_free(ptr, n, true);
0123 }
0124 
0125 /*************************************************
0126  * Get an allocator                               *
0127  *************************************************/
0128 Allocator *Allocator::get(bool locking)
0129 {
0130     std::string type = "";
0131     if (!locking)
0132         type = "malloc";
0133 
0134     Allocator *alloc = global_state().get_allocator(type);
0135     if (alloc)
0136         return alloc;
0137 
0138     throw Exception("Couldn't find an allocator to use in get_allocator");
0139 }
0140 
0141 }
0142 } // WRAPNS_LINE