1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3
<title>BitMagic: sample6.cpp</title>
4
<link href="doxygen.css" rel="stylesheet" type="text/css">
6
<!-- Generated by Doxygen 1.4.1 -->
7
<div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="examples.html">Examples</a></div>
8
<h1>sample6.cpp</h1>This example demonstrates using of custom memory allocators. In this case allocator works as a memory checker, counts number of allocations and deallocations to make sure that there is no memory leaks.<p>
9
For more information please visit: <a href="http://bmagic.sourceforge.net">http://bmagic.sourceforge.net</a><p>
10
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
11
<span class="comment">Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span>
12
<span class="comment"></span>
13
<span class="comment">Permission is hereby granted, free of charge, to any person </span>
14
<span class="comment">obtaining a copy of this software and associated documentation </span>
15
<span class="comment">files (the "Software"), to deal in the Software without restriction, </span>
16
<span class="comment">including without limitation the rights to use, copy, modify, merge, </span>
17
<span class="comment">publish, distribute, sublicense, and/or sell copies of the Software, </span>
18
<span class="comment">and to permit persons to whom the Software is furnished to do so, </span>
19
<span class="comment">subject to the following conditions:</span>
20
<span class="comment"></span>
21
<span class="comment">The above copyright notice and this permission notice shall be included </span>
22
<span class="comment">in all copies or substantial portions of the Software.</span>
23
<span class="comment"></span>
24
<span class="comment">THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, </span>
25
<span class="comment">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES </span>
26
<span class="comment">OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. </span>
27
<span class="comment">IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, </span>
28
<span class="comment">DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, </span>
29
<span class="comment">ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR </span>
30
<span class="comment">OTHER DEALINGS IN THE SOFTWARE.</span>
31
<span class="comment">*/</span>
32
<span class="comment"></span>
33
<span class="comment">/** \example sample6.cpp</span>
34
<span class="comment"> This example demonstrates using of custom memory allocators.</span>
35
<span class="comment"> In this case allocator works as a memory checker, counts number of </span>
36
<span class="comment"> allocations and deallocations to make sure that there is no memory leaks. </span>
37
<span class="comment"></span>
38
<span class="comment">For more information please visit: http://bmagic.sourceforge.net</span>
39
<span class="comment">*/</span>
41
<span class="preprocessor">#include <iostream></span>
42
<span class="preprocessor">#include <assert.h></span>
43
<span class="preprocessor">#include "<a class="code" href="a00074.html">bm.h</a>"</span>
45
<span class="keyword">using</span> <span class="keyword">namespace </span>std;
48
<span class="comment">// Custom allocator keeps number of all alloc/free calls.</span>
49
<span class="comment">// It also reservs the front word of the allocated block and saves</span>
50
<span class="comment">// number of elements allocated. On deallocation it makes sure</span>
51
<span class="comment">// it deallocates the same size as allocated</span>
53
<span class="keyword">class </span><a name="_a33"></a><a class="code" href="a00060.html">dbg_block_allocator</a>
55
<span class="keyword">public</span>:
56
<span class="keyword">static</span> <span class="keywordtype">unsigned</span> na_;
57
<span class="keyword">static</span> <span class="keywordtype">unsigned</span> nf_;
59
<span class="keyword">static</span> bm::word_t* allocate(size_t n, <span class="keyword">const</span> <span class="keywordtype">void</span> *)
64
(bm::word_t*) ::malloc((n+1) * <span class="keyword">sizeof</span>(bm::word_t));
66
<span class="keywordflow">return</span> ++p;
69
<span class="keyword">static</span> <span class="keywordtype">void</span> deallocate(bm::word_t* p, size_t n)
77
<span class="keyword">static</span> <span class="keywordtype">int</span> balance()
79
<span class="keywordflow">return</span> nf_ - na_;
83
<span class="keywordtype">unsigned</span> <a class="code" href="a00060.html#s0">dbg_block_allocator::na_</a> = 0;
84
<span class="keywordtype">unsigned</span> <a class="code" href="a00060.html#s1">dbg_block_allocator::nf_</a> = 0;
86
<span class="keyword">class </span><a name="_a34"></a><a class="code" href="a00061.html">dbg_ptr_allocator</a>
88
<span class="keyword">public</span>:
89
<span class="keyword">static</span> <span class="keywordtype">unsigned</span> na_;
90
<span class="keyword">static</span> <span class="keywordtype">unsigned</span> nf_;
92
<span class="keyword">static</span> <span class="keywordtype">void</span>* allocate(size_t n, <span class="keyword">const</span> <span class="keywordtype">void</span> *)
95
assert(<span class="keyword">sizeof</span>(size_t) == <span class="keyword">sizeof</span>(<span class="keywordtype">void</span>*));
96
<span class="keywordtype">void</span>* p = ::malloc((n+1) * <span class="keyword">sizeof</span>(<span class="keywordtype">void</span>*));
97
size_t* s = (size_t*) p;
99
<span class="keywordflow">return</span> (<span class="keywordtype">void</span>*)++s;
102
<span class="keyword">static</span> <span class="keywordtype">void</span> deallocate(<span class="keywordtype">void</span>* p, size_t n)
105
size_t* s = (size_t*) p;
111
<span class="keyword">static</span> <span class="keywordtype">int</span> balance()
113
<span class="keywordflow">return</span> nf_ - na_;
118
<span class="keywordtype">unsigned</span> <a class="code" href="a00061.html#s0">dbg_ptr_allocator::na_</a> = 0;
119
<span class="keywordtype">unsigned</span> <a class="code" href="a00061.html#s1">dbg_ptr_allocator::nf_</a> = 0;
122
<span class="keyword">typedef</span> bm::mem_alloc<dbg_block_allocator, dbg_ptr_allocator> <a class="code" href="a00088.html#a0">dbg_alloc</a>;
124
<span class="keyword">typedef</span> <a name="_a35"></a><a class="code" href="a00048.html">bm::bvector<dbg_alloc></a> <a class="code" href="a00048.html">bvect</a>;
128
<span class="keywordtype">int</span> <a name="a36"></a><a class="code" href="a00083.html#a0">main</a>(<span class="keywordtype">void</span>)
131
<a class="code" href="a00088.html#a1">bvect</a> bv;
133
bv[10] = <span class="keyword">true</span>;
134
bv[100000] = <span class="keyword">true</span>;
135
bv[10000000] = <span class="keyword">false</span>;
138
cout << <span class="stringliteral">"Number of BLOCK allocations = "</span> << <a class="code" href="a00060.html#s0">dbg_block_allocator::na_</a> << endl;
139
cout << <span class="stringliteral">"Number of PTR allocations = "</span> << <a class="code" href="a00061.html#s0">dbg_ptr_allocator::na_</a> << endl;
141
assert(dbg_block_allocator::balance() == 0);
142
assert(dbg_ptr_allocator::balance() == 0);
144
<span class="keywordflow">return</span> 0;
148
</pre></div> <hr size="1"><address style="align: right;"><small>Generated on Thu Apr 20 13:28:45 2006 for BitMagic by
149
<a href="http://www.doxygen.org/index.html">
150
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.1 </small></address>