qca
defalloc.cpp
Go to the documentation of this file.00001 /* 00002 Copyright (C) 1999-2007 The Botan Project. All rights reserved. 00003 00004 Redistribution and use in source and binary forms, for any use, with or without 00005 modification, is permitted provided that the following conditions are met: 00006 00007 1. Redistributions of source code must retain the above copyright notice, this 00008 list of conditions, and the following disclaimer. 00009 00010 2. Redistributions in binary form must reproduce the above copyright notice, 00011 this list of conditions, and the following disclaimer in the documentation 00012 and/or other materials provided with the distribution. 00013 00014 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED 00015 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00016 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. 00017 00018 IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT, 00019 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00020 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00021 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00022 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00023 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 00024 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00025 */ 00026 // LICENSEHEADER_END 00027 namespace QCA { // WRAPNS_LINE 00028 /************************************************* 00029 * Basic Allocators Source File * 00030 * (C) 1999-2007 The Botan Project * 00031 *************************************************/ 00032 00033 } // WRAPNS_LINE 00034 #include <botan/defalloc.h> 00035 namespace QCA { // WRAPNS_LINE 00036 } // WRAPNS_LINE 00037 #include <botan/libstate.h> 00038 namespace QCA { // WRAPNS_LINE 00039 } // WRAPNS_LINE 00040 #include <botan/util.h> 00041 namespace QCA { // WRAPNS_LINE 00042 } // WRAPNS_LINE 00043 #include <cstdlib> 00044 namespace QCA { // WRAPNS_LINE 00045 } // WRAPNS_LINE 00046 #include <cstring> 00047 namespace QCA { // WRAPNS_LINE 00048 } // WRAPNS_LINE 00049 #include <stdlib.h> 00050 namespace QCA { // WRAPNS_LINE 00051 } // WRAPNS_LINE 00052 #include <string.h> 00053 namespace QCA { // WRAPNS_LINE 00054 00055 namespace Botan { 00056 00057 namespace { 00058 00059 /************************************************* 00060 * Perform Memory Allocation * 00061 *************************************************/ 00062 void* do_malloc(u32bit n, bool do_lock) 00063 { 00064 void* ptr = malloc(n); 00065 00066 if(!ptr) 00067 return 0; 00068 00069 if(do_lock) 00070 lock_mem(ptr, n); 00071 00072 memset(ptr, 0, n); 00073 return ptr; 00074 } 00075 00076 /************************************************* 00077 * Perform Memory Deallocation * 00078 *************************************************/ 00079 void do_free(void* ptr, u32bit n, bool do_lock) 00080 { 00081 if(!ptr) 00082 return; 00083 00084 memset(ptr, 0, n); 00085 if(do_lock) 00086 unlock_mem(ptr, n); 00087 00088 free(ptr); 00089 } 00090 00091 } 00092 00093 /************************************************* 00094 * Malloc_Allocator's Allocation * 00095 *************************************************/ 00096 void* Malloc_Allocator::alloc_block(u32bit n) 00097 { 00098 return do_malloc(n, false); 00099 } 00100 00101 /************************************************* 00102 * Malloc_Allocator's Deallocation * 00103 *************************************************/ 00104 void Malloc_Allocator::dealloc_block(void* ptr, u32bit n) 00105 { 00106 do_free(ptr, n, false); 00107 } 00108 00109 /************************************************* 00110 * Locking_Allocator's Allocation * 00111 *************************************************/ 00112 void* Locking_Allocator::alloc_block(u32bit n) 00113 { 00114 return do_malloc(n, true); 00115 } 00116 00117 /************************************************* 00118 * Locking_Allocator's Deallocation * 00119 *************************************************/ 00120 void Locking_Allocator::dealloc_block(void* ptr, u32bit n) 00121 { 00122 do_free(ptr, n, true); 00123 } 00124 00125 /************************************************* 00126 * Get an allocator * 00127 *************************************************/ 00128 Allocator* Allocator::get(bool locking) 00129 { 00130 std::string type = ""; 00131 if(!locking) 00132 type = "malloc"; 00133 00134 Allocator* alloc = global_state().get_allocator(type); 00135 if(alloc) 00136 return alloc; 00137 00138 throw Exception("Couldn't find an allocator to use in get_allocator"); 00139 } 00140 00141 } 00142 } // WRAPNS_LINE
KDE 4.4 API Reference