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
30#include "bm.h"
31#include "bmundef.h" /* clear the pre-proc defines from BM */
32
33using namespace std;
34
35
36// Custom allocator keeps number of all alloc/free calls.
37// It also reservs the front word of the allocated block and saves
38// number of elements allocated. On deallocation it makes sure
39// it deallocates the same size as allocated
40//
41// Please note, that this sample allocator is NOT compatible with SIMD
42// optimizations, requiring special address alignment
43//
44
46{
47public:
48static unsigned na_;
49static unsigned nf_;
50
51 static bm::word_t* allocate(size_t n, const void *)
52 {
53 ++na_;
54 assert(n);
55 bm::word_t* p =
56 (bm::word_t*) ::malloc((n+1) * sizeof(bm::word_t));
57 *p = (unsigned)n;
58 return ++p;
59 }
60
61 static void deallocate(bm::word_t* p, size_t /* n */)
62 {
63 ++nf_;
64 --p;
65 ::free(p);
66 }
67
68 static int balance()
69 {
70 return int(nf_ - na_);
71 }
72};
73
74unsigned dbg_block_allocator::na_ = 0;
75unsigned dbg_block_allocator::nf_ = 0;
76
78{
79public:
80static unsigned na_;
81static unsigned nf_;
82
83 static void* allocate(size_t n, const void *)
84 {
85 ++na_;
86 assert(sizeof(size_t) == sizeof(void*));
87 void* p = ::malloc((n+1) * sizeof(void*));
88 size_t* s = (size_t*) p;
89 *s = n;
90 return (void*)++s;
91 }
92
93 static void deallocate(void* p, size_t /* n */)
94 {
95 ++nf_;
96 size_t* s = (size_t*) p;
97 --s;
98 ::free(s);
99 }
100
101 static int balance()
102 {
103 return int(nf_ - na_);
104 }
105
106};
107
108unsigned dbg_ptr_allocator::na_ = 0;
109unsigned dbg_ptr_allocator::nf_ = 0;
110
111
112typedef
115
117
118int main(void)
119{
120 try
121 {
122 {
123 bvect bv;
124
125 bv[10] = true;
126 bv[100000] = true;
127 bv[10000000] = false;
128 }
129
130 cout << "Number of BLOCK allocations = " << dbg_block_allocator::na_ << endl;
131 cout << "Number of PTR allocations = " << dbg_ptr_allocator::na_ << endl;
132 }
133 catch(std::exception& ex)
134 {
135 std::cerr << ex.what() << std::endl;
136 return 1;
137 }
138
139 assert(dbg_block_allocator::balance() == 0);
140 assert(dbg_ptr_allocator::balance() == 0);
141
142 return 0;
143}
144
145
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
pre-processor un-defines to avoid global space pollution (internal)
Allocation pool object.
Definition: bmalloc.h:224
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
BM style allocator adapter.
Definition: bmalloc.h:291
static unsigned na_
Definition: sample6.cpp:48
static int balance()
Definition: sample6.cpp:68
static bm::word_t * allocate(size_t n, const void *)
Definition: sample6.cpp:51
static unsigned nf_
Definition: sample6.cpp:49
static void deallocate(bm::word_t *p, size_t)
Definition: sample6.cpp:61
static void * allocate(size_t n, const void *)
Definition: sample6.cpp:83
static unsigned na_
Definition: sample6.cpp:80
static int balance()
Definition: sample6.cpp:101
static void deallocate(void *p, size_t)
Definition: sample6.cpp:93
static unsigned nf_
Definition: sample6.cpp:81
unsigned int word_t
Definition: bmconst.h:39
int main(void)
Definition: sample6.cpp:118
bm::bvector< dbg_alloc > bvect
Definition: sample6.cpp:116
bm::mem_alloc< dbg_block_allocator, dbg_ptr_allocator, bm::alloc_pool< dbg_block_allocator, dbg_ptr_allocator > > dbg_alloc
Definition: sample6.cpp:114