TheAlgorithms-C/db/d84/malloc__dbg_8c.html
2022-01-14 20:11:27 +00:00

720 lines
58 KiB
HTML

<!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">
<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.3"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: developer_tools/malloc_dbg.c 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.3 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search",'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('db/d84/malloc__dbg_8c.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">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="summary">
<a href="#nested-classes">Data Structures</a> &#124;
<a href="#typedef-members">Typedefs</a> &#124;
<a href="#func-members">Functions</a> &#124;
<a href="#var-members">Variables</a> </div>
<div class="headertitle"><div class="title">malloc_dbg.c File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>This file contains malloc_dbg, calloc_dbg, free_dbg and printLeaks implementations.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;stdlib.h&gt;</code><br />
<code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;string.h&gt;</code><br />
<code>#include &quot;<a class="el" href="../../d2/ddd/malloc__dbg_8h_source.html">malloc_dbg.h</a>&quot;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for malloc_dbg.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d7/d70/malloc__dbg_8c__incl.svg" width="374" height="127"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="nested-classes" name="nested-classes"></a>
Data Structures</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">struct &#160;</td><td class="memItemRight" valign="bottom"><a class="el" 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></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight">For the malloc, calloc and free functions. <a href="../../d4/d73/struct_m_e_m_o_r_y___i_n_f_o_r_m_a_t_i_o_n.html#details">More...</a><br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="typedef-members" name="typedef-members"></a>
Typedefs</h2></td></tr>
<tr class="memitem:ab6619fb17769f2e4e2e36c9ef2dda3c6"><td class="memItemLeft" align="right" valign="top">typedef struct <a class="el" 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>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../db/d84/malloc__dbg_8c.html#ab6619fb17769f2e4e2e36c9ef2dda3c6">mem_info</a></td></tr>
<tr class="memdesc:ab6619fb17769f2e4e2e36c9ef2dda3c6"><td class="mdescLeft">&#160;</td><td class="mdescRight">For the malloc, calloc and free functions. <a href="../../db/d84/malloc__dbg_8c.html#ab6619fb17769f2e4e2e36c9ef2dda3c6">More...</a><br /></td></tr>
<tr class="separator:ab6619fb17769f2e4e2e36c9ef2dda3c6"><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:a062aa89cafd39a97971a2eb22653ff2a"><td class="memItemLeft" align="right" valign="top"><a class="el" href="../../db/d84/malloc__dbg_8c.html#ab6619fb17769f2e4e2e36c9ef2dda3c6">mem_info</a> *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../db/d84/malloc__dbg_8c.html#a062aa89cafd39a97971a2eb22653ff2a">addMemInfo</a> (<a class="el" href="../../db/d84/malloc__dbg_8c.html#ab6619fb17769f2e4e2e36c9ef2dda3c6">mem_info</a> *memoryInfo, void *ptrToReturn, size_t bytes, int line, const char *filename, const char *functionName)</td></tr>
<tr class="memdesc:a062aa89cafd39a97971a2eb22653ff2a"><td class="mdescLeft">&#160;</td><td class="mdescRight">addMemInfo function add a memory allocation in the memoryInfo list. <a href="../../db/d84/malloc__dbg_8c.html#a062aa89cafd39a97971a2eb22653ff2a">More...</a><br /></td></tr>
<tr class="separator:a062aa89cafd39a97971a2eb22653ff2a"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:acd08c54b257fb81e57f16c94690072f2"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../db/d84/malloc__dbg_8c.html#acd08c54b257fb81e57f16c94690072f2">inList</a> (const char *filename, int line)</td></tr>
<tr class="memdesc:acd08c54b257fb81e57f16c94690072f2"><td class="mdescLeft">&#160;</td><td class="mdescRight">inList function is used to know if an element is already in the memoryInfo list. <a href="../../db/d84/malloc__dbg_8c.html#acd08c54b257fb81e57f16c94690072f2">More...</a><br /></td></tr>
<tr class="separator:acd08c54b257fb81e57f16c94690072f2"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a8ac3a93239838ac88c18df64c58b9275"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../db/d84/malloc__dbg_8c.html#a8ac3a93239838ac88c18df64c58b9275">editInfo</a> (int elemPos, size_t bytes)</td></tr>
<tr class="memdesc:a8ac3a93239838ac88c18df64c58b9275"><td class="mdescLeft">&#160;</td><td class="mdescRight">editInfo function is used to edit an element in the memoryInfo list. <a href="../../db/d84/malloc__dbg_8c.html#a8ac3a93239838ac88c18df64c58b9275">More...</a><br /></td></tr>
<tr class="separator:a8ac3a93239838ac88c18df64c58b9275"><td class="memSeparator" colspan="2">&#160;</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="../../db/d84/malloc__dbg_8c.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="../../db/d84/malloc__dbg_8c.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="../../db/d84/malloc__dbg_8c.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="../../db/d84/malloc__dbg_8c.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="../../db/d84/malloc__dbg_8c.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="../../db/d84/malloc__dbg_8c.html#a3f9195a04ac8e8a9868ee3e416db7e8c">More...</a><br /></td></tr>
<tr class="separator:a3f9195a04ac8e8a9868ee3e416db7e8c"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a2a47e4c38db8c00b80248e5535adf797"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../db/d84/malloc__dbg_8c.html#a2a47e4c38db8c00b80248e5535adf797">printLeaks</a> ()</td></tr>
<tr class="memdesc:a2a47e4c38db8c00b80248e5535adf797"><td class="mdescLeft">&#160;</td><td class="mdescRight">printLeaks function is used to print all the memory leaks. <a href="../../db/d84/malloc__dbg_8c.html#a2a47e4c38db8c00b80248e5535adf797">More...</a><br /></td></tr>
<tr class="separator:a2a47e4c38db8c00b80248e5535adf797"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="var-members" name="var-members"></a>
Variables</h2></td></tr>
<tr class="memitem:afebd751704cd878d2e4b88499519c6e3"><td class="memItemLeft" align="right" valign="top"><a id="afebd751704cd878d2e4b88499519c6e3" name="afebd751704cd878d2e4b88499519c6e3"></a>
<a class="el" href="../../db/d84/malloc__dbg_8c.html#ab6619fb17769f2e4e2e36c9ef2dda3c6">mem_info</a> *&#160;</td><td class="memItemRight" valign="bottom"><b>memoryInformation</b> = NULL</td></tr>
<tr class="memdesc:afebd751704cd878d2e4b88499519c6e3"><td class="mdescLeft">&#160;</td><td class="mdescRight">We use a global variable for the list so we can use it at any time. <br /></td></tr>
<tr class="separator:afebd751704cd878d2e4b88499519c6e3"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ad04afbbcb9f0f10bc7628ec4f0d7c950"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../db/d84/malloc__dbg_8c.html#ad04afbbcb9f0f10bc7628ec4f0d7c950">atexitCalled</a> = 0</td></tr>
<tr class="memdesc:ad04afbbcb9f0f10bc7628ec4f0d7c950"><td class="mdescLeft">&#160;</td><td class="mdescRight">Another global variable. <a href="../../db/d84/malloc__dbg_8c.html#ad04afbbcb9f0f10bc7628ec4f0d7c950">More...</a><br /></td></tr>
<tr class="separator:ad04afbbcb9f0f10bc7628ec4f0d7c950"><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 >This file contains malloc_dbg, calloc_dbg, free_dbg and printLeaks implementations. </p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/tinouduart33" target="_blank">tinouduart33</a> </dd></dl>
</div><h2 class="groupheader">Typedef Documentation</h2>
<a id="ab6619fb17769f2e4e2e36c9ef2dda3c6" name="ab6619fb17769f2e4e2e36c9ef2dda3c6"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ab6619fb17769f2e4e2e36c9ef2dda3c6">&#9670;&nbsp;</a></span>mem_info</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">typedef struct <a class="el" 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> <a class="el" href="../../db/d84/malloc__dbg_8c.html#ab6619fb17769f2e4e2e36c9ef2dda3c6">mem_info</a></td>
</tr>
</table>
</div><div class="memdoc">
<p>For the malloc, calloc and free functions. </p>
<p >For IO operations (printf). For the memcmp function. Header file which contains the prototypes of malloc_dbg, calloc_dbg and fre_dbg.</p>
<p >Structure used to save an allocated pointer </p>
</div>
</div>
<h2 class="groupheader">Function Documentation</h2>
<a id="a062aa89cafd39a97971a2eb22653ff2a" name="a062aa89cafd39a97971a2eb22653ff2a"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a062aa89cafd39a97971a2eb22653ff2a">&#9670;&nbsp;</a></span>addMemInfo()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="el" href="../../db/d84/malloc__dbg_8c.html#ab6619fb17769f2e4e2e36c9ef2dda3c6">mem_info</a> * addMemInfo </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../db/d84/malloc__dbg_8c.html#ab6619fb17769f2e4e2e36c9ef2dda3c6">mem_info</a> *&#160;</td>
<td class="paramname"><em>memoryInfo</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">void *&#160;</td>
<td class="paramname"><em>ptrToReturn</em>, </td>
</tr>
<tr>
<td class="paramkey"></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>addMemInfo function add a memory allocation in the memoryInfo list. </p>
<p >This function creates a new element and add it on top of the list </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">memoryInfo</td><td>Pointer to the doubly linked list used to store all of the allocations </td></tr>
<tr><td class="paramname">ptrToreturn</td><td>Pointer returned by malloc or calloc </td></tr>
<tr><td class="paramname">bytes</td><td>Size in bytes of the allocation </td></tr>
<tr><td class="paramname">line</td><td>Line where the allocation has been called </td></tr>
<tr><td class="paramname">filename</td><td>File where the allocation has been called </td></tr>
<tr><td class="paramname">functionName</td><td>Name of the function where the allocation has been called </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>mem_info pointer if it succeeds, NULL otherwise </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 52</span>{</div>
<div class="line"><span class="lineno"> 53</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>* newMemInfo = (<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>*)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</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>));</div>
<div class="line"><span class="lineno"> 54</span> <span class="keywordflow">if</span> (!newMemInfo)</div>
<div class="line"><span class="lineno"> 55</span> {</div>
<div class="line"><span class="lineno"> 56</span> <span class="keywordflow">return</span> NULL;</div>
<div class="line"><span class="lineno"> 57</span> }</div>
<div class="line"><span class="lineno"> 58</span> </div>
<div class="line"><span class="lineno"> 59</span> newMemInfo-&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> = ptrToReturn;</div>
<div class="line"><span class="lineno"> 60</span> newMemInfo-&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#a7f42967fd6562d77ac03445ea6e36a3d">bytes</a> = bytes;</div>
<div class="line"><span class="lineno"> 61</span> newMemInfo-&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#a731603550d2238abb179f2b572f20d99">line</a> = line;</div>
<div class="line"><span class="lineno"> 62</span> newMemInfo-&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#a934ad84d159c35b24ff54f7eceb1c6be">fileName</a> = filename;</div>
<div class="line"><span class="lineno"> 63</span> newMemInfo-&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#a1f13725b3de5ca6ab99b238b712cb417">functionName</a> = functionName;</div>
<div class="line"><span class="lineno"> 64</span> newMemInfo-&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> = memoryInfo;</div>
<div class="line"><span class="lineno"> 65</span> newMemInfo-&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"> 66</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"> 67</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> = newMemInfo;</div>
<div class="line"><span class="lineno"> 68</span> </div>
<div class="line"><span class="lineno"> 69</span> <span class="keywordflow">return</span> newMemInfo;</div>
<div class="line"><span class="lineno"> 70</span>}</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_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 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_a1f13725b3de5ca6ab99b238b712cb417"><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#a1f13725b3de5ca6ab99b238b712cb417">MEMORY_INFORMATION::functionName</a></div><div class="ttdeci">const char * functionName</div><div class="ttdoc">Function in which malloc or calloc has been called.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:24</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_a731603550d2238abb179f2b572f20d99"><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#a731603550d2238abb179f2b572f20d99">MEMORY_INFORMATION::line</a></div><div class="ttdeci">int line</div><div class="ttdoc">Line number (in file) corresponding to the malloc / calloc call.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:26</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_a7f42967fd6562d77ac03445ea6e36a3d"><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#a7f42967fd6562d77ac03445ea6e36a3d">MEMORY_INFORMATION::bytes</a></div><div class="ttdeci">size_t bytes</div><div class="ttdoc">Number of bytes allocated.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:25</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_a934ad84d159c35b24ff54f7eceb1c6be"><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#a934ad84d159c35b24ff54f7eceb1c6be">MEMORY_INFORMATION::fileName</a></div><div class="ttdeci">const char * fileName</div><div class="ttdoc">File in which malloc or calloc has been called.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.c:23</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><!-- fragment -->
</div>
</div>
<a id="ab07f946b77155ca78d0e14e231a99f5d" name="ab07f946b77155ca78d0e14e231a99f5d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ab07f946b77155ca78d0e14e231a99f5d">&#9670;&nbsp;</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_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="../../db/d84/malloc__dbg_8c_ab07f946b77155ca78d0e14e231a99f5d_cgraph.svg" width="238" height="88"><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="a8ac3a93239838ac88c18df64c58b9275" name="a8ac3a93239838ac88c18df64c58b9275"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a8ac3a93239838ac88c18df64c58b9275">&#9670;&nbsp;</a></span>editInfo()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void editInfo </td>
<td>(</td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>elemPos</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>bytes</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>editInfo function is used to edit an element in the memoryInfo list. </p>
<p >This function is used to edit the number of bytes allocated at a specific line. </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">elemPos</td><td>Position of an element in the doubly linked list memoryInfo </td></tr>
<tr><td class="paramname">bytes</td><td>Size of the allocation in bytes </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"> 108</span>{</div>
<div class="line"><span class="lineno"> 109</span> <span class="keywordtype">int</span> counter = 0;</div>
<div class="line"><span class="lineno"> 110</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"> 111</span> </div>
<div class="line"><span class="lineno"> 112</span> <span class="keywordflow">while</span> (counter != elemPos)</div>
<div class="line"><span class="lineno"> 113</span> {</div>
<div class="line"><span class="lineno"> 114</span> tmp = tmp-&gt;next;</div>
<div class="line"><span class="lineno"> 115</span> counter++;</div>
<div class="line"><span class="lineno"> 116</span> }</div>
<div class="line"><span class="lineno"> 117</span> tmp-&gt;bytes += bytes;</div>
<div class="line"><span class="lineno"> 118</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a3f9195a04ac8e8a9868ee3e416db7e8c" name="a3f9195a04ac8e8a9868ee3e416db7e8c"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a3f9195a04ac8e8a9868ee3e416db7e8c">&#9670;&nbsp;</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;next;</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;next;</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;next;</div>
<div class="line"><span class="lineno"> 255</span> }</div>
<div class="line"><span class="lineno"> 256</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="acd08c54b257fb81e57f16c94690072f2" name="acd08c54b257fb81e57f16c94690072f2"></a>
<h2 class="memtitle"><span class="permalink"><a href="#acd08c54b257fb81e57f16c94690072f2">&#9670;&nbsp;</a></span>inList()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int inList </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">int&#160;</td>
<td class="paramname"><em>line</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>inList function is used to know if an element is already in the memoryInfo list. </p>
<p >This function is used to know if an allocation in a specific file at a specific line already exists in the list. </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">filename</td><td>File in which malloc or calloc has been called </td></tr>
<tr><td class="paramname">line</td><td>Line number in the file in which malloc or calloc has been called </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>Position of the element in the list if the element is found, -1 otherwise. </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 80</span>{</div>
<div class="line"><span class="lineno"> 81</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"> 82</span> <span class="keywordtype">int</span> counter = 0;</div>
<div class="line"><span class="lineno"> 83</span> <span class="keywordtype">int</span> len = strlen(filename);</div>
<div class="line"><span class="lineno"> 84</span> </div>
<div class="line"><span class="lineno"> 85</span> <span class="keywordflow">while</span> (tmp)</div>
<div class="line"><span class="lineno"> 86</span> {</div>
<div class="line"><span class="lineno"> 87</span> <span class="keywordflow">if</span> (len == strlen(tmp-&gt;fileName))</div>
<div class="line"><span class="lineno"> 88</span> {</div>
<div class="line"><span class="lineno"> 89</span> <span class="keywordflow">if</span> (!memcmp(filename, tmp-&gt;fileName, len) &amp;&amp; tmp-&gt;line == line)</div>
<div class="line"><span class="lineno"> 90</span> {</div>
<div class="line"><span class="lineno"> 91</span> <span class="keywordflow">return</span> counter;</div>
<div class="line"><span class="lineno"> 92</span> }</div>
<div class="line"><span class="lineno"> 93</span> }</div>
<div class="line"><span class="lineno"> 94</span> tmp = tmp-&gt;next;</div>
<div class="line"><span class="lineno"> 95</span> counter++;</div>
<div class="line"><span class="lineno"> 96</span> }</div>
<div class="line"><span class="lineno"> 97</span> <span class="keywordflow">return</span> -1;</div>
<div class="line"><span class="lineno"> 98</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a37914d05ba1fe8848aa1b48cda719422" name="a37914d05ba1fe8848aa1b48cda719422"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a37914d05ba1fe8848aa1b48cda719422">&#9670;&nbsp;</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><!-- 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="../../db/d84/malloc__dbg_8c_a37914d05ba1fe8848aa1b48cda719422_cgraph.svg" width="242" height="190"><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="a2a47e4c38db8c00b80248e5535adf797" name="a2a47e4c38db8c00b80248e5535adf797"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a2a47e4c38db8c00b80248e5535adf797">&#9670;&nbsp;</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;next;</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>
<h2 class="groupheader">Variable Documentation</h2>
<a id="ad04afbbcb9f0f10bc7628ec4f0d7c950" name="ad04afbbcb9f0f10bc7628ec4f0d7c950"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ad04afbbcb9f0f10bc7628ec4f0d7c950">&#9670;&nbsp;</a></span>atexitCalled</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int atexitCalled = 0</td>
</tr>
</table>
</div><div class="memdoc">
<p>Another global variable. </p>
<p >This one is used to know if we already call the atexit function. </p>
</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="../../db/d84/malloc__dbg_8c.html">malloc_dbg.c</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.3 </li>
</ul>
</div>
</body>
</html>