TheAlgorithms-C/d2/ddd/malloc__dbg_8h.html

477 lines
41 KiB
HTML
Raw Normal View History

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.5"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: developer_tools/malloc_dbg.h File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">Algorithms_in_C<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.5 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('d2/ddd/malloc__dbg_8h.html','../../'); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="summary">
<a href="#define-members">Macros</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">malloc_dbg.h File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>Header file that contains macros used to replace malloc/calloc and free.
<a href="#details">More...</a></p>
<div class="textblock"><div class="dynheader">
This graph shows which files directly or indirectly include this file:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../dd/dd7/malloc__dbg_8h__dep__incl.svg" width="328" height="139"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
<p><a href="../../d2/ddd/malloc__dbg_8h_source.html">Go to the source code of this file.</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="define-members" name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:a725f50ecaf1959d96de79b36b4788fee"><td class="memItemLeft" align="right" valign="top"><a id="a725f50ecaf1959d96de79b36b4788fee" name="a725f50ecaf1959d96de79b36b4788fee"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>malloc</b>(bytes)&#160;&#160;&#160;<a class="el" href="../../db/d84/malloc__dbg_8c.html#a37914d05ba1fe8848aa1b48cda719422">malloc_dbg</a>(bytes, __LINE__, __FILE__, __FUNCTION__)</td></tr>
<tr class="memdesc:a725f50ecaf1959d96de79b36b4788fee"><td class="mdescLeft">&#160;</td><td class="mdescRight">This macro replace the standard malloc function with malloc_dbg. <br /></td></tr>
<tr class="separator:a725f50ecaf1959d96de79b36b4788fee"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:afdddaa949a93c1ef559a638e98f9c21b"><td class="memItemLeft" align="right" valign="top"><a id="afdddaa949a93c1ef559a638e98f9c21b" name="afdddaa949a93c1ef559a638e98f9c21b"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>calloc</b>(elemCount, elemSize)&#160;&#160;&#160;<a class="el" href="../../db/d84/malloc__dbg_8c.html#ab07f946b77155ca78d0e14e231a99f5d">calloc_dbg</a>(elemCount, elemSize, __LINE__, __FILE__, __FUNCTION__)</td></tr>
<tr class="memdesc:afdddaa949a93c1ef559a638e98f9c21b"><td class="mdescLeft">&#160;</td><td class="mdescRight">This macro replace the standard calloc function with calloc_dbg. <br /></td></tr>
<tr class="separator:afdddaa949a93c1ef559a638e98f9c21b"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a9cc854374299a1dd933bf62029761768"><td class="memItemLeft" align="right" valign="top"><a id="a9cc854374299a1dd933bf62029761768" name="a9cc854374299a1dd933bf62029761768"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>free</b>(ptr)&#160;&#160;&#160;<a class="el" href="../../db/d84/malloc__dbg_8c.html#a3f9195a04ac8e8a9868ee3e416db7e8c">free_dbg</a>(ptr)</td></tr>
<tr class="memdesc:a9cc854374299a1dd933bf62029761768"><td class="mdescLeft">&#160;</td><td class="mdescRight">This macro replace the standard free function with free_dbg. <br /></td></tr>
<tr class="separator:a9cc854374299a1dd933bf62029761768"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a37914d05ba1fe8848aa1b48cda719422"><td class="memItemLeft" align="right" valign="top">void *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d2/ddd/malloc__dbg_8h.html#a37914d05ba1fe8848aa1b48cda719422">malloc_dbg</a> (size_t bytes, int line, const char *filename, const char *functionName)</td></tr>
<tr class="memdesc:a37914d05ba1fe8848aa1b48cda719422"><td class="mdescLeft">&#160;</td><td class="mdescRight">malloc_dbg function is a wrapper around the malloc function. <a href="../../d2/ddd/malloc__dbg_8h.html#a37914d05ba1fe8848aa1b48cda719422">More...</a><br /></td></tr>
<tr class="separator:a37914d05ba1fe8848aa1b48cda719422"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ab07f946b77155ca78d0e14e231a99f5d"><td class="memItemLeft" align="right" valign="top">void *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d2/ddd/malloc__dbg_8h.html#ab07f946b77155ca78d0e14e231a99f5d">calloc_dbg</a> (size_t elementCount, size_t elementSize, int line, const char *filename, const char *functionName)</td></tr>
<tr class="memdesc:ab07f946b77155ca78d0e14e231a99f5d"><td class="mdescLeft">&#160;</td><td class="mdescRight">calloc_dbg function is a wrapper around the calloc function. <a href="../../d2/ddd/malloc__dbg_8h.html#ab07f946b77155ca78d0e14e231a99f5d">More...</a><br /></td></tr>
<tr class="separator:ab07f946b77155ca78d0e14e231a99f5d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3f9195a04ac8e8a9868ee3e416db7e8c"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d2/ddd/malloc__dbg_8h.html#a3f9195a04ac8e8a9868ee3e416db7e8c">free_dbg</a> (void *ptrToFree)</td></tr>
<tr class="memdesc:a3f9195a04ac8e8a9868ee3e416db7e8c"><td class="mdescLeft">&#160;</td><td class="mdescRight">free_dbg function is used to free the memory allocated to a pointer. <a href="../../d2/ddd/malloc__dbg_8h.html#a3f9195a04ac8e8a9868ee3e416db7e8c">More...</a><br /></td></tr>
<tr class="separator:a3f9195a04ac8e8a9868ee3e416db7e8c"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:abfd0a4452069af4cfefe4a5d037e92ef"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d2/ddd/malloc__dbg_8h.html#abfd0a4452069af4cfefe4a5d037e92ef">printLeaks</a> (void)</td></tr>
<tr class="memdesc:abfd0a4452069af4cfefe4a5d037e92ef"><td class="mdescLeft">&#160;</td><td class="mdescRight">printLeaks function is used to print all the memory leaks. <a href="../../d2/ddd/malloc__dbg_8h.html#abfd0a4452069af4cfefe4a5d037e92ef">More...</a><br /></td></tr>
<tr class="separator:abfd0a4452069af4cfefe4a5d037e92ef"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p >Header file that contains macros used to replace malloc/calloc and free. </p>
<p >Macros malloc, calloc and free respectively calls malloc_dbg, calloc_dbg and free_dbg. malloc_dbg and calloc_dbg allocates memory using the "real" malloc and calloc and store the pointer returned (with additional informations) in a linked list. Thanks to this linked list, it is possible to check memory leaks. </p><dl class="section author"><dt>Author</dt><dd><a href="https://github.com/tinouduart33" target="_blank">tinouduart33</a> </dd></dl>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="../../db/d84/malloc__dbg_8c.html" title="This file contains malloc_dbg, calloc_dbg, free_dbg and printLeaks implementations.">malloc_dbg.c</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="ab07f946b77155ca78d0e14e231a99f5d" name="ab07f946b77155ca78d0e14e231a99f5d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ab07f946b77155ca78d0e14e231a99f5d">&#9670;&#160;</a></span>calloc_dbg()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void * calloc_dbg </td>
<td>(</td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>elementCount</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>elementSize</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>line</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const char *&#160;</td>
<td class="paramname"><em>filename</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const char *&#160;</td>
<td class="paramname"><em>functionName</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>calloc_dbg function is a wrapper around the calloc function. </p>
<p >This function calls calloc and allocates the number of bytes passed in the parameters. If the allocation succeeds then it add the pointer returned by malloc in the mem_info list. </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">elementCount</td><td>number of element to allocate </td></tr>
<tr><td class="paramname">elementSize</td><td>Size of each element </td></tr>
<tr><td class="paramname">line</td><td>Line number in the caller file </td></tr>
<tr><td class="paramname">filename</td><td>Caller file </td></tr>
<tr><td class="paramname">functionName</td><td>Caller function </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>Pointer returned by calloc if it's valid, NULL otherwhise. </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 175</span>{</div>
<div class="line"><span class="lineno"> 176</span> <span class="keywordtype">void</span>* ptrToReturn = <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#afdddaa949a93c1ef559a638e98f9c21b">calloc</a>(elementCount, elementSize);</div>
<div class="line"><span class="lineno"> 177</span> <span class="keywordflow">if</span> (!ptrToReturn)</div>
<div class="line"><span class="lineno"> 178</span> {</div>
<div class="line"><span class="lineno"> 179</span> <span class="keywordflow">return</span> NULL;</div>
<div class="line"><span class="lineno"> 180</span> }</div>
<div class="line"><span class="lineno"> 181</span> </div>
<div class="line"><span class="lineno"> 182</span> <span class="comment">// We must check atexitCalled value to know if we already called the function</span></div>
<div class="line"><span class="lineno"> 183</span> <span class="keywordflow">if</span> (!<a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#ad04afbbcb9f0f10bc7628ec4f0d7c950">atexitCalled</a>)</div>
<div class="line"><span class="lineno"> 184</span> {</div>
<div class="line"><span class="lineno"> 185</span> atexit(<a class="code hl_function" href="../../db/d84/malloc__dbg_8c.html#a2a47e4c38db8c00b80248e5535adf797">printLeaks</a>); <span class="comment">// Used to call printLeaks when the program exit</span></div>
<div class="line"><span class="lineno"> 186</span> <a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#ad04afbbcb9f0f10bc7628ec4f0d7c950">atexitCalled</a> = 1;</div>
<div class="line"><span class="lineno"> 187</span> }</div>
<div class="line"><span class="lineno"> 188</span> </div>
<div class="line"><span class="lineno"> 189</span> <span class="comment">// Add a new element in the mem_info list</span></div>
<div class="line"><span class="lineno"> 190</span> <a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a> = <a class="code hl_function" href="../../db/d84/malloc__dbg_8c.html#a062aa89cafd39a97971a2eb22653ff2a">addMemInfo</a>(<a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a>, ptrToReturn, elementCount * elementSize, line, filename, functionName);</div>
<div class="line"><span class="lineno"> 191</span> <span class="keywordflow">if</span> (!<a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a>)</div>
<div class="line"><span class="lineno"> 192</span> {</div>
<div class="line"><span class="lineno"> 193</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(ptrToReturn);</div>
<div class="line"><span class="lineno"> 194</span> <span class="keywordflow">return</span> NULL;</div>
<div class="line"><span class="lineno"> 195</span> }</div>
<div class="line"><span class="lineno"> 196</span> </div>
<div class="line"><span class="lineno"> 197</span> <span class="keywordflow">return</span> ptrToReturn;</div>
<div class="line"><span class="lineno"> 198</span>}</div>
<div class="ttc" id="amalloc__dbg_8c_html_a062aa89cafd39a97971a2eb22653ff2a"><div class="ttname"><a href="../../db/d84/malloc__dbg_8c.html#a062aa89cafd39a97971a2eb22653ff2a">addMemInfo</a></div><div class="ttdeci">mem_info * addMemInfo(mem_info *memoryInfo, void *ptrToReturn, size_t bytes, int line, const char *filename, const char *functionName)</div><div class="ttdoc">addMemInfo function add a memory allocation in the memoryInfo list.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:51</div></div>
<div class="ttc" id="amalloc__dbg_8c_html_a2a47e4c38db8c00b80248e5535adf797"><div class="ttname"><a href="../../db/d84/malloc__dbg_8c.html#a2a47e4c38db8c00b80248e5535adf797">printLeaks</a></div><div class="ttdeci">void printLeaks()</div><div class="ttdoc">printLeaks function is used to print all the memory leaks.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:264</div></div>
<div class="ttc" id="amalloc__dbg_8c_html_ad04afbbcb9f0f10bc7628ec4f0d7c950"><div class="ttname"><a href="../../db/d84/malloc__dbg_8c.html#ad04afbbcb9f0f10bc7628ec4f0d7c950">atexitCalled</a></div><div class="ttdeci">int atexitCalled</div><div class="ttdoc">Another global variable.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:37</div></div>
<div class="ttc" id="amalloc__dbg_8c_html_afebd751704cd878d2e4b88499519c6e3"><div class="ttname"><a href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a></div><div class="ttdeci">mem_info * memoryInformation</div><div class="ttdoc">We use a global variable for the list so we can use it at any time.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:33</div></div>
<div class="ttc" id="amalloc__dbg_8h_html_a9cc854374299a1dd933bf62029761768"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a></div><div class="ttdeci">#define free(ptr)</div><div class="ttdoc">This macro replace the standard free function with free_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:26</div></div>
<div class="ttc" id="amalloc__dbg_8h_html_afdddaa949a93c1ef559a638e98f9c21b"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#afdddaa949a93c1ef559a638e98f9c21b">calloc</a></div><div class="ttdeci">#define calloc(elemCount, elemSize)</div><div class="ttdoc">This macro replace the standard calloc function with calloc_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:22</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d2/ddd/malloc__dbg_8h_ab07f946b77155ca78d0e14e231a99f5d_cgraph.svg" width="238" height="86"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a3f9195a04ac8e8a9868ee3e416db7e8c" name="a3f9195a04ac8e8a9868ee3e416db7e8c"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a3f9195a04ac8e8a9868ee3e416db7e8c">&#9670;&#160;</a></span>free_dbg()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void free_dbg </td>
<td>(</td>
<td class="paramtype">void *&#160;</td>
<td class="paramname"><em>ptrToFree</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>free_dbg function is used to free the memory allocated to a pointer. </p>
<p >This function free the memory pointed by the pointer passed in parameter. To free this pointer, we loop through the mem_info list and check if we find the pointer. Once it's found, the pointer is freed and the element is deleted from the list. </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">ptrToFree</td><td>Pointer that must be freed </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>Nothing. </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 209</span>{</div>
<div class="line"><span class="lineno"> 210</span> <a class="code hl_struct" href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html">mem_info</a>* tmp = <a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a>;</div>
<div class="line"><span class="lineno"> 211</span> <a class="code hl_struct" href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html">mem_info</a>* toFree = NULL;</div>
<div class="line"><span class="lineno"> 212</span> <a class="code hl_struct" href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html">mem_info</a>* previous = NULL;</div>
<div class="line"><span class="lineno"> 213</span> </div>
<div class="line"><span class="lineno"> 214</span> <span class="comment">// Check if the head contains the pointer to free</span></div>
<div class="line"><span class="lineno"> 215</span> <span class="keywordflow">if</span> (tmp-&gt;ptr == ptrToFree)</div>
<div class="line"><span class="lineno"> 216</span> {</div>
<div class="line"><span class="lineno"> 217</span> toFree = tmp;</div>
<div class="line"><span class="lineno"> 218</span> <a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a> = tmp-&gt;<a class="code hl_variable" href="../../d5/da1/structnode.html#a111a569ab2765add9b91c9f94cf9f063">next</a>;</div>
<div class="line"><span class="lineno"> 219</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(toFree-&gt;<a class="code hl_variable" href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html#a6b0971c1415de6e0123b8d0d0a626fde">ptr</a>);</div>
<div class="line"><span class="lineno"> 220</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(toFree);</div>
<div class="line"><span class="lineno"> 221</span> <span class="keywordflow">if</span> (<a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a>)</div>
<div class="line"><span class="lineno"> 222</span> {</div>
<div class="line"><span class="lineno"> 223</span> <a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a>-&gt;<a class="code hl_variable" href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html#a1cec46413acf776e3ee2b0b9241490c3">previous</a> = NULL;</div>
<div class="line"><span class="lineno"> 224</span> }</div>
<div class="line"><span class="lineno"> 225</span> <span class="keywordflow">return</span>;</div>
<div class="line"><span class="lineno"> 226</span> }</div>
<div class="line"><span class="lineno"> 227</span> </div>
<div class="line"><span class="lineno"> 228</span> <span class="comment">// We can loop through the list without any problems, the head is not the pointer</span></div>
<div class="line"><span class="lineno"> 229</span> <span class="keywordflow">while</span> (tmp)</div>
<div class="line"><span class="lineno"> 230</span> {</div>
<div class="line"><span class="lineno"> 231</span> <span class="keywordflow">if</span> (tmp-&gt;ptr == ptrToFree) <span class="comment">// If we found the pointer that must be freed</span></div>
<div class="line"><span class="lineno"> 232</span> {</div>
<div class="line"><span class="lineno"> 233</span> toFree = tmp;</div>
<div class="line"><span class="lineno"> 234</span> tmp = tmp-&gt;<a class="code hl_variable" href="../../d5/da1/structnode.html#a111a569ab2765add9b91c9f94cf9f063">next</a>;</div>
<div class="line"><span class="lineno"> 235</span> previous = toFree-&gt;<a class="code hl_variable" href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html#a1cec46413acf776e3ee2b0b9241490c3">previous</a>;</div>
<div class="line"><span class="lineno"> 236</span> </div>
<div class="line"><span class="lineno"> 237</span> <span class="keywordflow">if</span> (previous)</div>
<div class="line"><span class="lineno"> 238</span> {</div>
<div class="line"><span class="lineno"> 239</span> previous-&gt;<a class="code hl_variable" href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html#aa296b903d0e2ac54acaa7c305bae8007">next</a> = tmp;</div>
<div class="line"><span class="lineno"> 240</span> }</div>
<div class="line"><span class="lineno"> 241</span> <span class="keywordflow">if</span> (tmp)</div>
<div class="line"><span class="lineno"> 242</span> {</div>
<div class="line"><span class="lineno"> 243</span> tmp-&gt;previous = previous;</div>
<div class="line"><span class="lineno"> 244</span> }</div>
<div class="line"><span class="lineno"> 245</span> </div>
<div class="line"><span class="lineno"> 246</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(toFree-&gt;<a class="code hl_variable" href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html#a6b0971c1415de6e0123b8d0d0a626fde">ptr</a>);</div>
<div class="line"><span class="lineno"> 247</span> <span class="keywordflow">if</span> (toFree == <a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a>)</div>
<div class="line"><span class="lineno"> 248</span> {</div>
<div class="line"><span class="lineno"> 249</span> <a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a> = NULL;</div>
<div class="line"><span class="lineno"> 250</span> }</div>
<div class="line"><span class="lineno"> 251</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(toFree);</div>
<div class="line"><span class="lineno"> 252</span> <span class="keywordflow">return</span>;</div>
<div class="line"><span class="lineno"> 253</span> }</div>
<div class="line"><span class="lineno"> 254</span> tmp = tmp-&gt;<a class="code hl_variable" href="../../d5/da1/structnode.html#a111a569ab2765add9b91c9f94cf9f063">next</a>;</div>
<div class="line"><span class="lineno"> 255</span> }</div>
<div class="line"><span class="lineno"> 256</span>}</div>
<div class="ttc" id="astruct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n_html"><div class="ttname"><a href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html">MEMORY_INFORMATION</a></div><div class="ttdoc">For the malloc, calloc and free functions.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:21</div></div>
<div class="ttc" id="astruct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n_html_a1cec46413acf776e3ee2b0b9241490c3"><div class="ttname"><a href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html#a1cec46413acf776e3ee2b0b9241490c3">MEMORY_INFORMATION::previous</a></div><div class="ttdeci">struct MEMORY_INFORMATION * previous</div><div class="ttdoc">Previous element in the list.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:28</div></div>
<div class="ttc" id="astruct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n_html_a6b0971c1415de6e0123b8d0d0a626fde"><div class="ttname"><a href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html#a6b0971c1415de6e0123b8d0d0a626fde">MEMORY_INFORMATION::ptr</a></div><div class="ttdeci">void * ptr</div><div class="ttdoc">Pointer returned by malloc / calloc.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:22</div></div>
<div class="ttc" id="astruct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n_html_aa296b903d0e2ac54acaa7c305bae8007"><div class="ttname"><a href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html#aa296b903d0e2ac54acaa7c305bae8007">MEMORY_INFORMATION::next</a></div><div class="ttdeci">struct MEMORY_INFORMATION * next</div><div class="ttdoc">Next element in the list.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:27</div></div>
<div class="ttc" id="astructnode_html_a111a569ab2765add9b91c9f94cf9f063"><div class="ttname"><a href="../../d5/da1/structnode.html#a111a569ab2765add9b91c9f94cf9f063">node::next</a></div><div class="ttdeci">struct node * next</div><div class="ttdoc">pointer to the node</div><div class="ttdef"><b>Definition:</b> bfs.c:24</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a37914d05ba1fe8848aa1b48cda719422" name="a37914d05ba1fe8848aa1b48cda719422"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a37914d05ba1fe8848aa1b48cda719422">&#9670;&#160;</a></span>malloc_dbg()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void * malloc_dbg </td>
<td>(</td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>bytes</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>line</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const char *&#160;</td>
<td class="paramname"><em>filename</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const char *&#160;</td>
<td class="paramname"><em>functionName</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>malloc_dbg function is a wrapper around the malloc function. </p>
<p >This function calls malloc and allocates the number of bytes passed in the parameters. If the allocation succeeds then it add the pointer returned by malloc in the mem_info list. </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">bytes</td><td>Size of the allocation in bytes </td></tr>
<tr><td class="paramname">filename</td><td>Caller file </td></tr>
<tr><td class="paramname">functionName</td><td>Caller function </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>Pointer returned by malloc if it's valid, NULL otherwhise. </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 130</span>{</div>
<div class="line"><span class="lineno"> 131</span> <span class="keywordtype">void</span>* ptrToReturn = <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(bytes);</div>
<div class="line"><span class="lineno"> 132</span> <span class="keywordtype">int</span> pos = 0;</div>
<div class="line"><span class="lineno"> 133</span> <span class="keywordflow">if</span> (!ptrToReturn)</div>
<div class="line"><span class="lineno"> 134</span> {</div>
<div class="line"><span class="lineno"> 135</span> <span class="keywordflow">return</span> NULL;</div>
<div class="line"><span class="lineno"> 136</span> }</div>
<div class="line"><span class="lineno"> 137</span> </div>
<div class="line"><span class="lineno"> 138</span> <span class="comment">// We must check atexitCalled value to know if we already called the function</span></div>
<div class="line"><span class="lineno"> 139</span> <span class="keywordflow">if</span> (!<a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#ad04afbbcb9f0f10bc7628ec4f0d7c950">atexitCalled</a>)</div>
<div class="line"><span class="lineno"> 140</span> {</div>
<div class="line"><span class="lineno"> 141</span> atexit(<a class="code hl_function" href="../../db/d84/malloc__dbg_8c.html#a2a47e4c38db8c00b80248e5535adf797">printLeaks</a>); <span class="comment">// Used to call printLeaks when the program exit</span></div>
<div class="line"><span class="lineno"> 142</span> <a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#ad04afbbcb9f0f10bc7628ec4f0d7c950">atexitCalled</a> = 1;</div>
<div class="line"><span class="lineno"> 143</span> }</div>
<div class="line"><span class="lineno"> 144</span> </div>
<div class="line"><span class="lineno"> 145</span> pos = <a class="code hl_function" href="../../db/d84/malloc__dbg_8c.html#acd08c54b257fb81e57f16c94690072f2">inList</a>(filename, line);</div>
<div class="line"><span class="lineno"> 146</span> <span class="keywordflow">if</span> (pos == -1)</div>
<div class="line"><span class="lineno"> 147</span> {</div>
<div class="line"><span class="lineno"> 148</span> <span class="comment">// Add a new element in the mem_info list</span></div>
<div class="line"><span class="lineno"> 149</span> <a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a> = <a class="code hl_function" href="../../db/d84/malloc__dbg_8c.html#a062aa89cafd39a97971a2eb22653ff2a">addMemInfo</a>(<a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a>, ptrToReturn, bytes, line, filename, functionName);</div>
<div class="line"><span class="lineno"> 150</span> <span class="keywordflow">if</span> (!<a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a>)</div>
<div class="line"><span class="lineno"> 151</span> {</div>
<div class="line"><span class="lineno"> 152</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(ptrToReturn);</div>
<div class="line"><span class="lineno"> 153</span> <span class="keywordflow">return</span> NULL;</div>
<div class="line"><span class="lineno"> 154</span> }</div>
<div class="line"><span class="lineno"> 155</span> }</div>
<div class="line"><span class="lineno"> 156</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 157</span> {</div>
<div class="line"><span class="lineno"> 158</span> <a class="code hl_function" href="../../db/d84/malloc__dbg_8c.html#a8ac3a93239838ac88c18df64c58b9275">editInfo</a>(pos, bytes);</div>
<div class="line"><span class="lineno"> 159</span> }</div>
<div class="line"><span class="lineno"> 160</span> <span class="keywordflow">return</span> ptrToReturn;</div>
<div class="line"><span class="lineno"> 161</span>}</div>
<div class="ttc" id="amalloc__dbg_8c_html_a8ac3a93239838ac88c18df64c58b9275"><div class="ttname"><a href="../../db/d84/malloc__dbg_8c.html#a8ac3a93239838ac88c18df64c58b9275">editInfo</a></div><div class="ttdeci">void editInfo(int elemPos, size_t bytes)</div><div class="ttdoc">editInfo function is used to edit an element in the memoryInfo list.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:107</div></div>
<div class="ttc" id="amalloc__dbg_8c_html_acd08c54b257fb81e57f16c94690072f2"><div class="ttname"><a href="../../db/d84/malloc__dbg_8c.html#acd08c54b257fb81e57f16c94690072f2">inList</a></div><div class="ttdeci">int inList(const char *filename, int line)</div><div class="ttdoc">inList function is used to know if an element is already in the memoryInfo list.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:79</div></div>
<div class="ttc" id="amalloc__dbg_8h_html_a725f50ecaf1959d96de79b36b4788fee"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a></div><div class="ttdeci">#define malloc(bytes)</div><div class="ttdoc">This macro replace the standard malloc function with malloc_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:18</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d2/ddd/malloc__dbg_8h_a37914d05ba1fe8848aa1b48cda719422_cgraph.svg" width="242" height="184"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="abfd0a4452069af4cfefe4a5d037e92ef" name="abfd0a4452069af4cfefe4a5d037e92ef"></a>
<h2 class="memtitle"><span class="permalink"><a href="#abfd0a4452069af4cfefe4a5d037e92ef">&#9670;&#160;</a></span>printLeaks()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void printLeaks </td>
<td>(</td>
<td class="paramtype">void&#160;</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>printLeaks function is used to print all the memory leaks. </p>
<p >This function is called when the program exits. It loop through the mem_info list and if it's not empty, it prints the memory leaks. </p><dl class="section return"><dt>Returns</dt><dd>Nothing. </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 265</span>{</div>
<div class="line"><span class="lineno"> 266</span> <a class="code hl_struct" href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html">mem_info</a>* tmp = <a class="code hl_variable" href="../../db/d84/malloc__dbg_8c.html#afebd751704cd878d2e4b88499519c6e3">memoryInformation</a>;</div>
<div class="line"><span class="lineno"> 267</span> <a class="code hl_struct" href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html">mem_info</a>* previous = NULL;</div>
<div class="line"><span class="lineno"> 268</span> <span class="keywordtype">size_t</span> sum = 0;</div>
<div class="line"><span class="lineno"> 269</span> <span class="keywordtype">int</span> nbBlocks = 0;</div>
<div class="line"><span class="lineno"> 270</span> </div>
<div class="line"><span class="lineno"> 271</span> <span class="keywordflow">if</span> (tmp)</div>
<div class="line"><span class="lineno"> 272</span> {</div>
<div class="line"><span class="lineno"> 273</span> printf(<span class="stringliteral">&quot;Memory Leaks detected.\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 274</span> }</div>
<div class="line"><span class="lineno"> 275</span> </div>
<div class="line"><span class="lineno"> 276</span> <span class="keywordflow">while</span> (tmp)</div>
<div class="line"><span class="lineno"> 277</span> {</div>
<div class="line"><span class="lineno"> 278</span> previous = tmp;</div>
<div class="line"><span class="lineno"> 279</span> printf(<span class="stringliteral">&quot;\n%ld bytes lost\n&quot;</span>, tmp-&gt;bytes);</div>
<div class="line"><span class="lineno"> 280</span> printf(<span class="stringliteral">&quot;address : 0x%p in %s\t%s:%d\n&quot;</span>, tmp-&gt;ptr, tmp-&gt;functionName, tmp-&gt;fileName, tmp-&gt;line);</div>
<div class="line"><span class="lineno"> 281</span> printf(<span class="stringliteral">&quot;\n====================================\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 282</span> sum += tmp-&gt;bytes;</div>
<div class="line"><span class="lineno"> 283</span> tmp = tmp-&gt;<a class="code hl_variable" href="../../d5/da1/structnode.html#a111a569ab2765add9b91c9f94cf9f063">next</a>;</div>
<div class="line"><span class="lineno"> 284</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(previous);</div>
<div class="line"><span class="lineno"> 285</span> nbBlocks++;</div>
<div class="line"><span class="lineno"> 286</span> }</div>
<div class="line"><span class="lineno"> 287</span> </div>
<div class="line"><span class="lineno"> 288</span> printf(<span class="stringliteral">&quot;SUMMARY :\n%ld bytes lost in %d blocks\n&quot;</span>, sum, nbBlocks);</div>
<div class="line"><span class="lineno"> 289</span>}</div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_abf1a26ece768c4076e73592705ae077.html">developer_tools</a></li><li class="navelem"><a class="el" href="../../d2/ddd/malloc__dbg_8h.html">malloc_dbg.h</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.5 </li>
</ul>
</div>
</body>
</html>