2019-07-08 17:15:34 -07:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2019-06-22 10:30:09 -07:00
< 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=9" / >
2021-12-18 16:36:37 -08:00
< meta name = "generator" content = "Doxygen 1.9.1" / >
2019-06-22 10:30:09 -07:00
< meta name = "viewport" content = "width=device-width, initial-scale=1" / >
< title > mi-malloc: Overriding Malloc< / 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/javascript" >
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3& dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function() { init_search(); });
/* @license-end */
< / 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" > <!-- do not remove this div, it is closed by doxygen! -->
< div id = "titlearea" >
< table cellspacing = "0" cellpadding = "0" >
< tbody >
< tr style = "height: 56px;" >
< td id = "projectlogo" > < img alt = "Logo" src = "mimalloc-logo.svg" / > < / td >
< td id = "projectalign" style = "padding-left: 0.5em;" >
< div id = "projectname" > mi-malloc
2021-12-18 16:36:37 -08:00
  < span id = "projectnumber" > 1.7/2.0< / span >
2019-06-22 10:30:09 -07:00
< / div >
< / td >
< td > < div id = "MSearchBox" class = "MSearchBoxInactive" >
< span class = "left" >
2021-12-18 16:36:37 -08:00
< img id = "MSearchSelect" src = "search/mag_sel.svg"
2019-06-22 10:30:09 -07:00
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
< input type = "text" id = "MSearchField" value = "Search" accesskey = "S"
2022-12-03 00:23:43 +01:00
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
2019-06-22 10:30:09 -07:00
onkeyup="searchBox.OnSearchFieldChange(event)"/>
< / span > < span class = "right" >
2021-12-18 16:36:37 -08:00
< a id = "MSearchClose" href = "javascript:searchBox.CloseResultsWindow()" > < img id = "MSearchCloseImg" border = "0" src = "search/close.svg" alt = "" / > < / a >
2019-06-22 10:30:09 -07:00
< / span >
< / div >
< / td >
< / tr >
< / tbody >
< / table >
< / div >
<!-- end header part -->
2021-12-18 16:36:37 -08:00
<!-- Generated by Doxygen 1.9.1 -->
2019-06-22 10:30:09 -07:00
< script type = "text/javascript" >
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3& dn=gpl-2.0.txt GPL-v2 */
2021-12-18 16:36:37 -08:00
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
2019-06-22 10:30:09 -07:00
/* @license-end */
< / script >
< / 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 >
2022-12-03 00:23:43 +01:00
< div id = "splitbar" style = "-moz-user-select:none;"
2019-06-22 10:30:09 -07:00
class="ui-resizable-handle">
< / div >
< / div >
< script type = "text/javascript" >
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3& dn=gpl-2.0.txt GPL-v2 */
2021-12-18 16:36:37 -08:00
$(document).ready(function(){initNavTree('overrides.html',''); initResizable(); });
2019-06-22 10:30:09 -07:00
/* @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" >
2022-12-03 00:23:43 +01:00
< iframe src = "javascript:void(0)" frameborder = "0"
2019-06-22 10:30:09 -07:00
name="MSearchResults" id="MSearchResults">
< / iframe >
< / div >
2019-07-08 17:15:34 -07:00
< div class = "PageDoc" > < div class = "header" >
2019-06-22 10:30:09 -07:00
< div class = "headertitle" >
< div class = "title" > Overriding Malloc < / div > < / div >
< / div > <!-- header -->
< div class = "contents" >
< div class = "textblock" > < p > Overriding the standard < code > malloc< / code > can be done either < em > dynamically< / em > or < em > statically< / em > .< / p >
< h2 > Dynamic override< / h2 >
< p > This is the recommended way to override the standard malloc interface.< / p >
2019-08-10 17:27:07 -07:00
< h3 > Linux, BSD< / h3 >
2019-06-22 10:30:09 -07:00
< p > On these systems we preload the mimalloc shared library so all calls to the standard < code > malloc< / code > interface are resolved to the < em > mimalloc< / em > library.< / p >
< ul >
2019-08-10 17:27:07 -07:00
< li > < code > env LD_PRELOAD=/usr/lib/libmimalloc.so myprogram< / code > < / li >
2019-06-22 10:30:09 -07:00
< / ul >
2021-12-18 16:36:37 -08:00
< p > You can set extra environment variables to check that mimalloc is running, like: < / p > < div class = "fragment" > < div class = "line" > env MIMALLOC_VERBOSE=1 LD_PRELOAD=/usr/lib/libmimalloc.so myprogram< / div >
< / div > <!-- fragment --> < p > or run with the debug version to get detailed statistics: < / p > < div class = "fragment" > < div class = "line" > env MIMALLOC_SHOW_STATS=1 LD_PRELOAD=/usr/lib/libmimalloc-debug.so myprogram< / div >
< / div > <!-- fragment --> < h3 > MacOS< / h3 >
2019-08-10 17:27:07 -07:00
< p > On macOS we can also preload the mimalloc shared library so all calls to the standard < code > malloc< / code > interface are resolved to the < em > mimalloc< / em > library.< / p >
< ul >
< li > < code > env DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=/usr/lib/libmimalloc.dylib myprogram< / code > < / li >
< / ul >
< p > Note that certain security restrictions may apply when doing this from the < a href = "https://stackoverflow.com/questions/43941322/dyld-insert-libraries-ignored-when-calling-application-through-bash" > shell< / a > .< / p >
2020-04-20 12:00:39 -07:00
< p > (Note: macOS support for dynamic overriding is recent, please report any issues.)< / p >
2019-08-10 17:27:07 -07:00
< h3 > Windows< / h3 >
2020-04-20 12:00:39 -07:00
< p > Overriding on Windows is robust and has the particular advantage to be able to redirect all malloc/free calls that go through the (dynamic) C runtime allocator, including those from other DLL's or libraries.< / p >
< p > The overriding on Windows requires that you link your program explicitly with the mimalloc DLL and use the C-runtime library as a DLL (using the < code > /MD< / code > or < code > /MDd< / code > switch). Also, the < code > mimalloc-redirect.dll< / code > (or < code > mimalloc-redirect32.dll< / code > ) must be available in the same folder as the main < code > mimalloc-override.dll< / code > at runtime (as it is a dependency). The redirection DLL ensures that all calls to the C runtime malloc API get redirected to mimalloc (in < code > mimalloc-override.dll< / code > ).< / p >
2022-03-30 18:45:22 +01:00
< p > To ensure the mimalloc DLL is loaded at run-time it is easiest to insert some call to the mimalloc API in the < code > main< / code > function, like < code > mi_version()< / code > (or use the < code > /INCLUDE:mi_version< / code > switch on the linker). See the < code > mimalloc-override-test< / code > project for an example on how to use this. For best performance on Windows with C++, it is also recommended to also override the < code > new< / code > /< code > delete< / code > operations (by including < a href = "https://github.com/microsoft/mimalloc/blob/master/include/mimalloc-new-delete.h" > < code > mimalloc-new-delete.h< / code > < / a > a single(!) source file in your project without linking to the mimalloc library).< / p >
2020-01-16 19:39:14 -08:00
< p > The environment variable < code > MIMALLOC_DISABLE_REDIRECT=1< / code > can be used to disable dynamic overriding at run-time. Use < code > MIMALLOC_VERBOSE=1< / code > to check if mimalloc was successfully redirected.< / p >
2020-04-20 12:00:39 -07:00
< p > (Note: in principle, it is possible to even patch existing executables without any recompilation if they are linked with the dynamic C runtime (< code > ucrtbase.dll< / code > ) – just put the < code > mimalloc-override.dll< / code > into the import table (and put < code > mimalloc-redirect.dll< / code > in the same folder) Such patching can be done for example with < a href = "https://ntcore.com/?page_id=388" > CFF Explorer< / a > ).< / p >
2019-06-22 10:30:09 -07:00
< h2 > Static override< / h2 >
< p > On Unix systems, you can also statically link with < em > mimalloc< / em > to override the standard malloc interface. The recommended way is to link the final program with the < em > mimalloc< / em > single object file (< code > mimalloc-override.o< / code > ). We use an object file instead of a library file as linkers give preference to that over archives to resolve symbols. To ensure that the standard malloc interface resolves to the < em > mimalloc< / em > library, link it as the first object file. For example:< / p >
2021-12-18 16:36:37 -08:00
< div class = "fragment" > < div class = "line" > gcc -o myprogram mimalloc-< span class = "keyword" > override< / span > .o myfile1.c ...< / div >
< / div > <!-- fragment --> < h2 > List of Overrides:< / h2 >
2019-06-22 10:30:09 -07:00
< p > The specific functions that get redirected to the < em > mimalloc< / em > library are:< / p >
2021-12-18 16:36:37 -08:00
< div class = "fragment" > < div class = "line" > < span class = "comment" > // C< / span > < / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * malloc(< span class = "keywordtype" > size_t< / span > size);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * calloc(< span class = "keywordtype" > size_t< / span > size, < span class = "keywordtype" > size_t< / span > n);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * realloc(< span class = "keywordtype" > void< / span > * p, < span class = "keywordtype" > size_t< / span > newsize);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > free(< span class = "keywordtype" > void< / span > * p);< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * aligned_alloc(< span class = "keywordtype" > size_t< / span > alignment, < span class = "keywordtype" > size_t< / span > size);< / div >
< div class = "line" > < span class = "keywordtype" > char< / span > * strdup(< span class = "keyword" > const< / span > < span class = "keywordtype" > char< / span > * s);< / div >
< div class = "line" > < span class = "keywordtype" > char< / span > * strndup(< span class = "keyword" > const< / span > < span class = "keywordtype" > char< / span > * s, < span class = "keywordtype" > size_t< / span > n);< / div >
< div class = "line" > < span class = "keywordtype" > char< / span > * realpath(< span class = "keyword" > const< / span > < span class = "keywordtype" > char< / span > * fname, < span class = "keywordtype" > char< / span > * resolved_name);< / div >
< div class = "line" > < / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "comment" > // C++< / span > < / div >
< div class = "line" > < span class = "keywordtype" > void< / span > < span class = "keyword" > operator< / span > < span class = "keyword" > delete< / span > (< span class = "keywordtype" > void< / span > * p);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > < span class = "keyword" > operator< / span > < span class = "keyword" > delete< / span > [](< span class = "keywordtype" > void< / span > * p);< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * < span class = "keyword" > operator< / span > < span class = "keyword" > new< / span > (std::size_t n) noexcept(< span class = "keyword" > false< / span > );< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * < span class = "keyword" > operator< / span > < span class = "keyword" > new< / span > [](std::size_t n) noexcept(< span class = "keyword" > false< / span > );< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * < span class = "keyword" > operator< / span > < span class = "keyword" > new< / span > ( std::size_t n, std::align_val_t align) noexcept(< span class = "keyword" > false< / span > );< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * < span class = "keyword" > operator< / span > < span class = "keyword" > new< / span > []( std::size_t n, std::align_val_t align) noexcept(< span class = "keyword" > false< / span > );< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * < span class = "keyword" > operator< / span > < span class = "keyword" > new< / span > ( std::size_t count, < span class = "keyword" > const< / span > std::nothrow_t& tag);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * < span class = "keyword" > operator< / span > < span class = "keyword" > new< / span > []( std::size_t count, < span class = "keyword" > const< / span > std::nothrow_t& tag);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * < span class = "keyword" > operator< / span > < span class = "keyword" > new< / span > ( std::size_t count, std::align_val_t al, < span class = "keyword" > const< / span > std::nothrow_t& );< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * < span class = "keyword" > operator< / span > < span class = "keyword" > new< / span > []( std::size_t count, std::align_val_t al, < span class = "keyword" > const< / span > std::nothrow_t& );< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "comment" > // Posix< / span > < / div >
< div class = "line" > < span class = "keywordtype" > int< / span > posix_memalign(< span class = "keywordtype" > void< / span > ** p, < span class = "keywordtype" > size_t< / span > alignment, < span class = "keywordtype" > size_t< / span > size);< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "comment" > // Linux< / span > < / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * memalign(< span class = "keywordtype" > size_t< / span > alignment, < span class = "keywordtype" > size_t< / span > size);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * valloc(< span class = "keywordtype" > size_t< / span > size);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * pvalloc(< span class = "keywordtype" > size_t< / span > size);< / div >
< div class = "line" > < span class = "keywordtype" > size_t< / span > malloc_usable_size(< span class = "keywordtype" > void< / span > *p);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * reallocf(< span class = "keywordtype" > void< / span > * p, < span class = "keywordtype" > size_t< / span > newsize);< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "comment" > // macOS< / span > < / div >
< div class = "line" > < span class = "keywordtype" > void< / span > vfree(< span class = "keywordtype" > void< / span > * p);< / div >
< div class = "line" > < span class = "keywordtype" > size_t< / span > malloc_size(< span class = "keyword" > const< / span > < span class = "keywordtype" > void< / span > * p);< / div >
< div class = "line" > < span class = "keywordtype" > size_t< / span > malloc_good_size(< span class = "keywordtype" > size_t< / span > size);< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "comment" > // BSD< / span > < / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * reallocarray( < span class = "keywordtype" > void< / span > * p, < span class = "keywordtype" > size_t< / span > count, < span class = "keywordtype" > size_t< / span > size );< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * reallocf(< span class = "keywordtype" > void< / span > * p, < span class = "keywordtype" > size_t< / span > newsize);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > cfree(< span class = "keywordtype" > void< / span > * p);< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "comment" > // NetBSD< / span > < / div >
< div class = "line" > < span class = "keywordtype" > int< / span > reallocarr(< span class = "keywordtype" > void< / span > * p, < span class = "keywordtype" > size_t< / span > count, < span class = "keywordtype" > size_t< / span > size);< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "comment" > // Windows< / span > < / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * _expand(< span class = "keywordtype" > void< / span > * p, < span class = "keywordtype" > size_t< / span > newsize);< / div >
< div class = "line" > < span class = "keywordtype" > size_t< / span > _msize(< span class = "keywordtype" > void< / span > * p);< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * _malloc_dbg(< span class = "keywordtype" > size_t< / span > size, < span class = "keywordtype" > int< / span > block_type, < span class = "keyword" > const< / span > < span class = "keywordtype" > char< / span > * fname, < span class = "keywordtype" > int< / span > line);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * _realloc_dbg(< span class = "keywordtype" > void< / span > * p, < span class = "keywordtype" > size_t< / span > newsize, < span class = "keywordtype" > int< / span > block_type, < span class = "keyword" > const< / span > < span class = "keywordtype" > char< / span > * fname, < span class = "keywordtype" > int< / span > line);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * _calloc_dbg(< span class = "keywordtype" > size_t< / span > count, < span class = "keywordtype" > size_t< / span > size, < span class = "keywordtype" > int< / span > block_type, < span class = "keyword" > const< / span > < span class = "keywordtype" > char< / span > * fname, < span class = "keywordtype" > int< / span > line);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > * _expand_dbg(< span class = "keywordtype" > void< / span > * p, < span class = "keywordtype" > size_t< / span > size, < span class = "keywordtype" > int< / span > block_type, < span class = "keyword" > const< / span > < span class = "keywordtype" > char< / span > * fname, < span class = "keywordtype" > int< / span > line);< / div >
< div class = "line" > < span class = "keywordtype" > size_t< / span > _msize_dbg(< span class = "keywordtype" > void< / span > * p, < span class = "keywordtype" > int< / span > block_type);< / div >
< div class = "line" > < span class = "keywordtype" > void< / span > _free_dbg(< span class = "keywordtype" > void< / span > * p, < span class = "keywordtype" > int< / span > block_type);< / div >
< / div > <!-- fragment --> < / div > < / div > <!-- contents -->
< / div > <!-- PageDoc -->
2019-06-22 10:30:09 -07:00
< / div > <!-- doc - content -->
<!-- start footer part -->
< div id = "nav-path" class = "navpath" > <!-- id is needed for treeview function! -->
< ul >
2021-12-18 16:36:37 -08:00
< 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.1 < / li >
2019-06-22 10:30:09 -07:00
< / ul >
< / div >
< / body >
< / html >