BitMagic-C++: sample6.cpp Source File

BitMagic-C++

sample6.cpp
Go to the documentation of this file.
1 /*
2 Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 For more information please visit: http://bitmagic.io
17 */
18
19 /** \example sample6.cpp
20 This example demonstrates using of custom memory allocators.
21 In this case allocator works as a memory checker, counts number of
22 allocations and deallocations to make sure that there is no memory leaks.
23 */
24 /*! \file sample6.cpp
25 \brief Example: bvector<> custom memory allocator
26 */
27 #include <iostream>
28 #include <cassert>
29 #include "bm.h"
30
31 using namespace std;
32
33
34 // Custom allocator keeps number of all alloc/free calls.
35 // It also reservs the front word of the allocated block and saves
36 // number of elements allocated. On deallocation it makes sure
37 // it deallocates the same size as allocated
38 //
39 // Please note, that this sample allocator is NOT compatible with SIMD
40 // optimizations, requiring special address alignment
41 //
42
43 class dbg_block_allocator
44 {
45 public:
46 static unsigned na_;
47 static unsigned nf_;
48
49 static bm::word_t* allocate(size_t n, const void *)
50 {
51 ++na_;
52 assert(n);
53 bm::word_t* p =
54 (bm::word_t*) ::malloc((n+1) * sizeof(bm::word_t));
55 *p = (unsigned)n;
56 return ++p;
57 }
58
59 static void deallocate(bm::word_t* p, size_t /* n */)
60 {
61 ++nf_;
62 --p;
63 ::free(p);
64 }
65
66 static int balance()
67 {
68 return int(nf_ - na_);
69 }
70 };
71
72 unsigned dbg_block_allocator::na_ = 0;
73 unsigned dbg_block_allocator::nf_ = 0;
74
75 class dbg_ptr_allocator
76 {
77 public:
78 static unsigned na_;
79 static unsigned nf_;
80
81 static void* allocate(size_t n, const void *)
82 {
83 ++na_;
84 assert(sizeof(size_t) == sizeof(void*));
85 void* p = ::malloc((n+1) * sizeof(void*));
86 size_t* s = (size_t*) p;
87 *s = n;
88 return (void*)++s;
89 }
90
91 static void deallocate(void* p, size_t /* n */)
92 {
93 ++nf_;
94 size_t* s = (size_t*) p;
95 --s;
96 ::free(s);
97 }
98
99 static int balance()
100 {
101 return int(nf_ - na_);
102 }
103
104 };
105
106 unsigned dbg_ptr_allocator::na_ = 0;
107 unsigned dbg_ptr_allocator::nf_ = 0;
108
109
110 typedef
111 bm::mem_alloc<dbg_block_allocator, dbg_ptr_allocator,
112 bm::alloc_pool<dbg_block_allocator, dbg_ptr_allocator>> dbg_alloc;
113
114 typedef bm::bvector<dbg_alloc> bvect;
115
116 int main(void)
117 {
118 try
119 {
120 {
121 bvect bv;
122
123 bv[10] = true;
124 bv[100000] = true;
125 bv[10000000] = false;
126 }
127
128 cout << "Number of BLOCK allocations = " << dbg_block_allocator::na_ << endl;
129 cout << "Number of PTR allocations = " << dbg_ptr_allocator::na_ << endl;
130 }
131 catch(std::exception& ex)
132 {
133 std::cerr << ex.what() << std::endl;
134 return 1;
135 }
136
137 assert(dbg_block_allocator::balance() == 0);
138 assert(dbg_ptr_allocator::balance() == 0);
139
140 return 0;
141 }
142
143
dbg_block_allocator::balance
static int balance()
Definition: sample6.cpp:66
dbg_ptr_allocator::allocate
static void * allocate(size_t n, const void *)
Definition: sample6.cpp:81
dbg_ptr_allocator
Definition: sample6.cpp:75
bm::alloc_pool
Allocation pool object.
Definition: bmalloc.h:200
dbg_block_allocator::allocate
static bm::word_t * allocate(size_t n, const void *)
Definition: sample6.cpp:49
dbg_ptr_allocator::na_
static unsigned na_
Definition: sample6.cpp:78
dbg_ptr_allocator::deallocate
static void deallocate(void *p, size_t)
Definition: sample6.cpp:91
bm::bvector
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:107
dbg_block_allocator::nf_
static unsigned nf_
Definition: sample6.cpp:47
dbg_ptr_allocator::balance
static int balance()
Definition: sample6.cpp:99
dbg_block_allocator::deallocate
static void deallocate(bm::word_t *p, size_t)
Definition: sample6.cpp:59
dbg_ptr_allocator::nf_
static unsigned nf_
Definition: sample6.cpp:79
dbg_block_allocator
Definition: sample6.cpp:43
main
int main(void)
Definition: sample6.cpp:116
bm::word_t
unsigned int word_t
Definition: bmconst.h:38
dbg_block_allocator::na_
static unsigned na_
Definition: sample6.cpp:46
bvect
bm::bvector< dbg_alloc > bvect
Definition: sample6.cpp:114
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
dbg_alloc
bm::mem_alloc< dbg_block_allocator, dbg_ptr_allocator, bm::alloc_pool< dbg_block_allocator, dbg_ptr_allocator > > dbg_alloc
Definition: sample6.cpp:112
bm::mem_alloc
BM style allocator adapter.
Definition: bmalloc.h:254