BitMagic-C++
sample6.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16For 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
31using 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
44{
45public:
46static unsigned na_;
47static 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
72unsigned dbg_block_allocator::na_ = 0;
73unsigned dbg_block_allocator::nf_ = 0;
74
76{
77public:
78static unsigned na_;
79static 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
106unsigned dbg_ptr_allocator::na_ = 0;
107unsigned dbg_ptr_allocator::nf_ = 0;
108
109
110typedef
113
115
116int 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
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Allocation pool object.
Definition bmalloc.h:201
Bitvector Bit-vector container with runtime compression of bits.
Definition bm.h:108
BM style allocator adapter.
Definition bmalloc.h:255
static unsigned na_
Definition sample6.cpp:46
static int balance()
Definition sample6.cpp:66
static bm::word_t * allocate(size_t n, const void *)
Definition sample6.cpp:49
static unsigned nf_
Definition sample6.cpp:47
static void deallocate(bm::word_t *p, size_t)
Definition sample6.cpp:59
static unsigned na_
Definition sample6.cpp:78
static int balance()
Definition sample6.cpp:99
static void deallocate(void *p, size_t)
Definition sample6.cpp:91
static void * allocate(size_t n, const void *)
Definition sample6.cpp:81
static unsigned nf_
Definition sample6.cpp:79
unsigned int word_t
Definition bmconst.h:38
int main(void)
Definition sample6.cpp:116
bm::bvector< dbg_alloc > bvect
Definition sample6.cpp:114
bm::mem_alloc< dbg_block_allocator, dbg_ptr_allocator, bm::alloc_pool< dbg_block_allocator, dbg_ptr_allocator > > dbg_alloc
Definition sample6.cpp:112