00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <iostream>
00033 #include <assert.h>
00034 #include "bm.h"
00035
00036 using namespace std;
00037
00038
00039
00040
00041
00042
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