<!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.18.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>mi-malloc: Using the Library</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="codefolding.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="cookie.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>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="mimalloc-doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><img alt="Logo" src="mimalloc-logo.svg"/></td>
<td id="projectalign">
<div id="projectname">mi-malloc<span id="projectnumber"> 3.6/2.5/1.15</span>
</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<span id="MSearchSelect" class="search-icon"><span class="search-icon-dropdown"></span></span>
<input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><div id="MSearchCloseImg" class="close-icon"></div></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<script type="text/javascript">
let searchBox = new SearchBox("searchBox", "search/",'.html');
</script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', codefold.init);
</script>
</div>
<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" class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded',() => { initNavTree('using.html','',''); });
</script>
<div id="container">
<div id="doc-content">
<div id="MSearchSelectWindow">
</div>
<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><div class="header">
<div class="headertitle"><div class="title">Using the Library </div></div>
</div>
<div class="contents">
<div class="textblock"><h3>Build</h3>
<p>The preferred usage is including <span class="tt"><mimalloc.h></span>, linking with the shared- or static library, and using the <span class="tt"><a class="el" href="group__malloc.html#gae1dd97b542420c87ae085e822b1229e8" title="Allocate size bytes.">mi_malloc</a></span> API exclusively for allocation. For example, </p><div class="fragment"><div class="line">gcc -o myprogram -lmimalloc myfile.c</div>
</div><p>mimalloc uses only safe OS calls (<span class="tt">mmap</span> and <span class="tt">VirtualAlloc</span>) and can co-exist with other allocators linked to the same program. If you use <span class="tt">cmake</span>, you can simply use: </p><div class="fragment"><div class="line">find_package(mimalloc 1.8 REQUIRED)</div>
</div><p> in your <span class="tt">CMakeLists.txt</span> to find a locally installed mimalloc. Then use either: </p><div class="fragment"><div class="line">target_link_libraries(myapp PUBLIC mimalloc)</div>
</div><p> to link with the shared (dynamic) library, or: </p><div class="fragment"><div class="line">target_link_libraries(myapp PUBLIC mimalloc-<span class="keyword">static</span>)</div>
</div><p> to link with the static library. See the test targets in <span class="tt">CMakeLists.txt</span> for examples.</p>
<h3>C++</h3>
<p>For best performance in C++ programs, it is also recommended to override the global <span class="tt">new</span> and <span class="tt">delete</span> operators. For convenience, mimalloc provides <a href="https://github.com/microsoft/mimalloc/blob/dev3/include/mimalloc-new-delete.h"><span class="tt">mimalloc-new-delete.h</span></a> which does this for you – just include it in a single(!) source file in your project.</p>
<p>In C++, mimalloc also provides the <span class="tt"><a class="el" href="group__cpp.html#structmi__stl__allocator" title="std::allocator implementation for mimalloc for use in STL containers.">mi_stl_allocator</a></span> struct which implements the <span class="tt">std::allocator</span> interface. For example: </p><div class="fragment"><div class="line">std::vector<some_struct, mi_stl_allocator<some_struct>> vec;</div>
<div class="line">vec.push_back(some_struct());</div>
</div><h3>Statistics</h3>
<p>You can pass environment variables to print verbose messages (<span class="tt">MIMALLOC_VERBOSE=1</span>) and statistics (<span class="tt">MIMALLOC_SHOW_STATS=1</span>). Debug builds provide the most detail. Elevated processes ignore these variables; see <a class="el" href="security.html#security_environment" title="security_environment">Environment Options</a>. </p><div class="fragment"><div class="line">> env MIMALLOC_SHOW_STATS=1 ./cfrac 175451865205073170563711388363</div>
<div class="line"> </div>
<div class="line">175451865205073170563711388363 = 374456281610909315237213 * 468551</div>
<div class="line"> </div>
<div class="line">subproc 0</div>
<div class="line"> blocks peak total current block total#</div>
<div class="line"> bin S 4: 75.3 KiB 55.2 MiB 0 32 B 1.8 M ok</div>
<div class="line"> bin S 6: 31.0 KiB 180.4 KiB 0 48 B 3.8 K ok</div>
<div class="line"> bin S 8: 64 B 64 B 0 64 B 1 ok</div>
<div class="line"> bin S 9: 160 B 160 B 0 80 B 2 ok</div>
<div class="line"> bin S 17: 1.2 KiB 1.2 KiB 0 320 B 4 ok</div>
<div class="line"> bin S 21: 640 B 3.1 KiB 0 640 B 5 ok</div>
<div class="line"> bin S 33: 5.0 KiB 5.0 KiB 0 5.0 KiB 1 ok</div>
<div class="line"> </div>
<div class="line"> binned : 84.2 Ki 41.5 Mi 0 ok</div>
<div class="line"> huge : 0 0 0 ok</div>
<div class="line"> total : 84.2 KiB 41.5 MiB 0</div>
<div class="line"> malloc req: 29.7 MiB</div>
<div class="line"> </div>
<div class="line"> pages peak total current block total#</div>
<div class="line"> touched : 152.8 KiB 152.8 KiB 152.8 KiB</div>
<div class="line"> pages : 8 14 0 ok</div>
<div class="line"> abandoned : 1 249 0 ok</div>
<div class="line"> reclaima : 0</div>
<div class="line"> reclaimf : 249</div>
<div class="line"> reabandon : 0</div>
<div class="line"> waits : 0</div>
<div class="line"> extended : 38</div>
<div class="line"> retire : 35</div>
<div class="line"> searches : 0.7 avg</div>
<div class="line"> </div>
<div class="line"> arenas peak total current block total#</div>
<div class="line"> reserved : 1.0 GiB 1.0 GiB 1.0 GiB</div>
<div class="line"> committed : 4.8 MiB 4.8 MiB 4.4 MiB</div>
<div class="line"> reset : 0</div>
<div class="line"> purged : 385.5 Ki</div>
<div class="line"> arenas : 1</div>
<div class="line"> rollback : 0</div>
<div class="line"> mmaps : 3</div>
<div class="line"> commits : 0</div>
<div class="line"> resets : 1</div>
<div class="line"> purges : 2</div>
<div class="line"> guarded : 0</div>
<div class="line"> heaps : 1 1 1</div>
<div class="line"> </div>
<div class="line"> process peak total current block total#</div>
<div class="line"> threads : 1 1 1</div>
<div class="line"> numa nodes: 1</div>
<div class="line"> elapsed : 0.553 s</div>
<div class="line"> process : user: 0.557 s, system: 0.013 s, faults: 29, peak rss: 2.1 MiB, peak commit: 4.8 MiB</div>
</div><p>The above model of using the <span class="tt">mi_</span> prefixed API is not always possible though in existing programs that already use the standard malloc interface, and another option is to override the standard malloc interface completely and redirect all calls to the <em>mimalloc</em> library instead.</p>
<p>See <a class="el" href="overrides.html" title="Overriding Standard Malloc">Overriding Standard Malloc</a> for more info.</p>
<h2>Debug Mode</h2>
<p>When <em>mimalloc</em> is built using debug mode (<span class="tt">-DCMAKE_BUILD_TYPE=Debug</span>), or with <span class="tt">-DMI_DEBUG=ON</span> explicitly, various checks are done at runtime to catch development errors.</p>
<ul>
<li>All objects have padding at the end to detect byte-precise heap block overflows.</li>
<li>Double frees and freeing invalid heap pointers are detected.</li>
<li>Corrupted free lists and some forms of use-after-free are detected. </li>
</ul>
</div></div>
</div>
</div>
</div>
<div id="nav-path" class="navpath">
<ul>
<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.18.0 </li>
</ul>
</div>
</body>
</html>