Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 4e2dbb669434a7691662cb2f0ad38972 > files > 11963

rust-doc-1.28.0-1.mga6.armv7hl.rpm

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `alloc` mod in crate `std`."><meta name="keywords" content="rust, rustlang, rust-lang, alloc"><title>std::alloc - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><a href='../../std/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a><p class='location'>Module alloc</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'><a href='../index.html'>std</a></p><script>window.sidebarCurrent = {name: 'alloc', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='in-band'>Module <a href='../index.html'>std</a>::<wbr><a class="mod" href=''>alloc</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.28.0'>1.28.0</span><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>&#x2212;</span>]</a></span><a class='srclink' href='../../src/std/alloc.rs.html#11-184' title='goto source code'>[src]</a></span></h1><div class='docblock'><p>Memory allocation APIs</p>
<p>In a given program, the standard library has one “global” memory allocator
that is used for example by <code>Box&lt;T&gt;</code> and <code>Vec&lt;T&gt;</code>.</p>
<p>Currently the default global allocator is unspecified.
The compiler may link to a version of <a href="https://github.com/jemalloc/jemalloc">jemalloc</a> on some platforms,
but this is not guaranteed.
Libraries, however, like <code>cdylib</code>s and <code>staticlib</code>s are guaranteed
to use the <a href="struct.System.html"><code>System</code></a> by default.</p>
<h1 id="the-global_allocator-attribute" class="section-header"><a href="#the-global_allocator-attribute">The <code>#[global_allocator]</code> attribute</a></h1>
<p>This attribute allows configuring the choice of global allocator.
You can use this to implement a completely custom global allocator
to route all default allocation requests to a custom object.</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">alloc</span>::{<span class="ident">GlobalAlloc</span>, <span class="ident">System</span>, <span class="ident">Layout</span>};

<span class="kw">struct</span> <span class="ident">MyAllocator</span>;

<span class="kw">unsafe</span> <span class="kw">impl</span> <span class="ident">GlobalAlloc</span> <span class="kw">for</span> <span class="ident">MyAllocator</span> {
    <span class="kw">unsafe</span> <span class="kw">fn</span> <span class="ident">alloc</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">layout</span>: <span class="ident">Layout</span>) <span class="op">-&gt;</span> <span class="kw-2">*</span><span class="kw-2">mut</span> <span class="ident">u8</span> {
        <span class="ident">System</span>.<span class="ident">alloc</span>(<span class="ident">layout</span>)
    }

    <span class="kw">unsafe</span> <span class="kw">fn</span> <span class="ident">dealloc</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">ptr</span>: <span class="kw-2">*</span><span class="kw-2">mut</span> <span class="ident">u8</span>, <span class="ident">layout</span>: <span class="ident">Layout</span>) {
        <span class="ident">System</span>.<span class="ident">dealloc</span>(<span class="ident">ptr</span>, <span class="ident">layout</span>)
    }
}

<span class="attribute">#[<span class="ident">global_allocator</span>]</span>
<span class="kw">static</span> <span class="ident">GLOBAL</span>: <span class="ident">MyAllocator</span> <span class="op">=</span> <span class="ident">MyAllocator</span>;

<span class="kw">fn</span> <span class="ident">main</span>() {
    <span class="comment">// This `Vec` will allocate memory through `GLOBAL` above</span>
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">v</span> <span class="op">=</span> <span class="ident">Vec</span>::<span class="ident">new</span>();
    <span class="ident">v</span>.<span class="ident">push</span>(<span class="number">1</span>);
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Ause%20std%3A%3Aalloc%3A%3A%7BGlobalAlloc%2C%20System%2C%20Layout%7D%3B%0A%0Astruct%20MyAllocator%3B%0A%0Aunsafe%20impl%20GlobalAlloc%20for%20MyAllocator%20%7B%0A%20%20%20%20unsafe%20fn%20alloc(%26self%2C%20layout%3A%20Layout)%20-%3E%20*mut%20u8%20%7B%0A%20%20%20%20%20%20%20%20System.alloc(layout)%0A%20%20%20%20%7D%0A%0A%20%20%20%20unsafe%20fn%20dealloc(%26self%2C%20ptr%3A%20*mut%20u8%2C%20layout%3A%20Layout)%20%7B%0A%20%20%20%20%20%20%20%20System.dealloc(ptr%2C%20layout)%0A%20%20%20%20%7D%0A%7D%0A%0A%23%5Bglobal_allocator%5D%0Astatic%20GLOBAL%3A%20MyAllocator%20%3D%20MyAllocator%3B%0A%0Afn%20main()%20%7B%0A%20%20%20%20%2F%2F%20This%20%60Vec%60%20will%20allocate%20memory%20through%20%60GLOBAL%60%20above%0A%20%20%20%20let%20mut%20v%20%3D%20Vec%3A%3Anew()%3B%0A%20%20%20%20v.push(1)%3B%0A%7D">Run</a></pre>
<p>The attribute is used on a <code>static</code> item whose type implements the
<a href="../../core/alloc/trait.GlobalAlloc.html"><code>GlobalAlloc</code></a> trait. This type can be provided by an external library:</p>

<div class='information'><div class='tooltip ignore'>ⓘ<span class='tooltiptext'>This example is not tested</span></div></div><pre class="rust rust-example-rendered ignore">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">jemallocator</span>;

<span class="kw">use</span> <span class="ident">jemallacator</span>::<span class="ident">Jemalloc</span>;

<span class="attribute">#[<span class="ident">global_allocator</span>]</span>
<span class="kw">static</span> <span class="ident">GLOBAL</span>: <span class="ident">Jemalloc</span> <span class="op">=</span> <span class="ident">Jemalloc</span>;

<span class="kw">fn</span> <span class="ident">main</span>() {}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Aextern%20crate%20jemallocator%3B%0A%0Ause%20jemallacator%3A%3AJemalloc%3B%0A%0A%23%5Bglobal_allocator%5D%0Astatic%20GLOBAL%3A%20Jemalloc%20%3D%20Jemalloc%3B%0A%0Afn%20main()%20%7B%7D">Run</a></pre>
<p>The <code>#[global_allocator]</code> can only be used once in a crate
or its recursive dependencies.</p>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.Layout.html"
                                  title='struct std::alloc::Layout'>Layout</a></td>
                           <td class='docblock-short'>
                                <p>Layout of a block of memory.</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.LayoutErr.html"
                                  title='struct std::alloc::LayoutErr'>LayoutErr</a></td>
                           <td class='docblock-short'>
                                <p>The parameters given to <code>Layout::from_size_align</code>
or some other <code>Layout</code> constructor
do not satisfy its documented constraints.</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.System.html"
                                  title='struct std::alloc::System'>System</a></td>
                           <td class='docblock-short'>
                                <p>The default memory allocator provided by the operating system.</p>

                           </td>
                       </tr>
                       <tr class='unstable module-item'>
                           <td><a class="struct" href="struct.AllocErr.html"
                                  title='struct std::alloc::AllocErr'>AllocErr</a></td>
                           <td class='docblock-short'>
                               [<div class='stab unstable'>Experimental</div>] <p>The <code>AllocErr</code> error indicates an allocation failure
that may be due to resource exhaustion or to
something wrong when combining the given input arguments with this
allocator.</p>

                           </td>
                       </tr>
                       <tr class='unstable module-item'>
                           <td><a class="struct" href="struct.CannotReallocInPlace.html"
                                  title='struct std::alloc::CannotReallocInPlace'>CannotReallocInPlace</a></td>
                           <td class='docblock-short'>
                               [<div class='stab unstable'>Experimental</div>] <p>The <code>CannotReallocInPlace</code> error is used when <code>grow_in_place</code> or
<code>shrink_in_place</code> were unable to reuse the given memory block for
a requested layout.</p>

                           </td>
                       </tr>
                       <tr class='unstable module-item'>
                           <td><a class="struct" href="struct.Excess.html"
                                  title='struct std::alloc::Excess'>Excess</a></td>
                           <td class='docblock-short'>
                               [<div class='stab unstable'>Experimental</div>] <p>Represents the combination of a starting address and
a total capacity of the returned block.</p>

                           </td>
                       </tr>
                       <tr class='unstable module-item'>
                           <td><a class="struct" href="struct.Global.html"
                                  title='struct std::alloc::Global'>Global</a></td>
                           <td class='docblock-short'>
                               [<div class='stab unstable'>Experimental</div>] <p>The global memory allocator.</p>

                           </td>
                       </tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
<table>
                       <tr class='unstable module-item'>
                           <td><a class="enum" href="enum.CollectionAllocErr.html"
                                  title='enum std::alloc::CollectionAllocErr'>CollectionAllocErr</a></td>
                           <td class='docblock-short'>
                               [<div class='stab unstable'>Experimental</div>] <p>Augments <code>AllocErr</code> with a CapacityOverflow variant.</p>

                           </td>
                       </tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
<table>
                       <tr class=' module-item'>
                           <td><a class="trait" href="trait.GlobalAlloc.html"
                                  title='trait std::alloc::GlobalAlloc'>GlobalAlloc</a></td>
                           <td class='docblock-short'>
                                <p>A memory allocator that can be registered as the standard library’s default
though the <code>#[global_allocator]</code> attributes.</p>

                           </td>
                       </tr>
                       <tr class='unstable module-item'>
                           <td><a class="trait" href="trait.Alloc.html"
                                  title='trait std::alloc::Alloc'>Alloc</a></td>
                           <td class='docblock-short'>
                               [<div class='stab unstable'>Experimental</div>] <p>An implementation of <code>Alloc</code> can allocate, reallocate, and
deallocate arbitrary blocks of data described via <code>Layout</code>.</p>

                           </td>
                       </tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
<table>
                       <tr class=' module-item'>
                           <td><a class="fn" href="fn.alloc.html"
                                  title='fn std::alloc::alloc'>alloc</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td>
                           <td class='docblock-short'>
                                <p>Allocate memory with the global allocator.</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="fn" href="fn.alloc_zeroed.html"
                                  title='fn std::alloc::alloc_zeroed'>alloc_zeroed</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td>
                           <td class='docblock-short'>
                                <p>Allocate zero-initialized memory with the global allocator.</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="fn" href="fn.dealloc.html"
                                  title='fn std::alloc::dealloc'>dealloc</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td>
                           <td class='docblock-short'>
                                <p>Deallocate memory with the global allocator.</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="fn" href="fn.handle_alloc_error.html"
                                  title='fn std::alloc::handle_alloc_error'>handle_alloc_error</a></td>
                           <td class='docblock-short'>
                                <p>Abort on memory allocation error or failure.</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="fn" href="fn.realloc.html"
                                  title='fn std::alloc::realloc'>realloc</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td>
                           <td class='docblock-short'>
                                <p>Reallocate memory with the global allocator.</p>

                           </td>
                       </tr>
                       <tr class='unstable module-item'>
                           <td><a class="fn" href="fn.set_alloc_error_hook.html"
                                  title='fn std::alloc::set_alloc_error_hook'>set_alloc_error_hook</a></td>
                           <td class='docblock-short'>
                               [<div class='stab unstable'>Experimental</div>] <p>Registers a custom allocation error hook, replacing any that was previously registered.</p>

                           </td>
                       </tr>
                       <tr class='unstable module-item'>
                           <td><a class="fn" href="fn.take_alloc_error_hook.html"
                                  title='fn std::alloc::take_alloc_error_hook'>take_alloc_error_hook</a></td>
                           <td class='docblock-short'>
                               [<div class='stab unstable'>Experimental</div>] <p>Unregisters the current allocation error hook, returning it.</p>

                           </td>
                       </tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>&#9166;</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "std";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>