00001 /* 00002 Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com) 00003 00004 Permission is hereby granted, free of charge, to any person 00005 obtaining a copy of this software and associated documentation 00006 files (the "Software"), to deal in the Software without restriction, 00007 including without limitation the rights to use, copy, modify, merge, 00008 publish, distribute, sublicense, and/or sell copies of the Software, 00009 and to permit persons to whom the Software is furnished to do so, 00010 subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included 00013 in all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00016 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00017 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00018 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00019 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00020 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00021 OTHER DEALINGS IN THE SOFTWARE. 00022 */ 00023 00024 /** \example sample6.cpp 00025 This example demonstrates using of custom memory allocators. 00026 In this case allocator works as a memory checker, counts number of 00027 allocations and deallocations to make sure that there is no memory leaks. 00028 00029 For more information please visit: http://bmagic.sourceforge.net 00030 */ 00031 00032 #include <iostream> 00033 #include <cassert> 00034 #include "bm.h" 00035 00036 using namespace std; 00037 00038 00039 // Custom allocator keeps number of all alloc/free calls. 00040 // It also reservs the front word of the allocated block and saves 00041 // number of elements allocated. On deallocation it makes sure 00042 // it deallocates the same size as allocated 00043 00044 class dbg_block_allocator 00045 { 00046 public: 00047 static unsigned na_; 00048 static unsigned nf_; 00049 00050 static bm::word_t* allocate(size_t n, const void *) 00051 { 00052 ++na_; 00053 assert(n); 00054 bm::word_t* p = 00055 (bm::word_t*) ::malloc((n+1) * sizeof(bm::word_t)); 00056 *p = n; 00057 return ++p; 00058 } 00059 00060 static void deallocate(bm::word_t* p, size_t n) 00061 { 00062 ++nf_; 00063 --p; 00064 assert(*p == n); 00065 ::free(p); 00066 } 00067 00068 static int balance() 00069 { 00070 return nf_ - na_; 00071 } 00072 }; 00073 00074 unsigned dbg_block_allocator::na_ = 0; 00075 unsigned dbg_block_allocator::nf_ = 0; 00076 00077 class dbg_ptr_allocator 00078 { 00079 public: 00080 static unsigned na_; 00081 static unsigned nf_; 00082 00083 static void* allocate(size_t n, const void *) 00084 { 00085 ++na_; 00086 assert(sizeof(size_t) == sizeof(void*)); 00087 void* p = ::malloc((n+1) * sizeof(void*)); 00088 size_t* s = (size_t*) p; 00089 *s = n; 00090 return (void*)++s; 00091 } 00092 00093 static void deallocate(void* p, size_t n) 00094 { 00095 ++nf_; 00096 size_t* s = (size_t*) p; 00097 --s; 00098 assert(*s == n); 00099 ::free(s); 00100 } 00101 00102 static int balance() 00103 { 00104 return nf_ - na_; 00105 } 00106 00107 }; 00108 00109 unsigned dbg_ptr_allocator::na_ = 0; 00110 unsigned dbg_ptr_allocator::nf_ = 0; 00111 00112 00113 typedef bm::mem_alloc<dbg_block_allocator, dbg_ptr_allocator> dbg_alloc; 00114 00115 typedef bm::bvector<dbg_alloc> bvect; 00116 00117 00118 00119 int main(void) 00120 { 00121 { 00122 bvect bv; 00123 00124 bv[10] = true; 00125 bv[100000] = true; 00126 bv[10000000] = false; 00127 } 00128 00129 cout << "Number of BLOCK allocations = " << dbg_block_allocator::na_ << endl; 00130 cout << "Number of PTR allocations = " << dbg_ptr_allocator::na_ << endl; 00131 00132 assert(dbg_block_allocator::balance() == 0); 00133 assert(dbg_ptr_allocator::balance() == 0); 00134 00135 return 0; 00136 } 00137 00138