Sophie

Sophie

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

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 `GlobalAlloc` trait in crate `std`."><meta name="keywords" content="rust, rustlang, rust-lang, GlobalAlloc"><title>std::alloc::GlobalAlloc - 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 trait"><!--[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'>Trait GlobalAlloc</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.alloc">alloc</a><a href="#tymethod.dealloc">dealloc</a></div><a class="sidebar-title" href="#provided-methods">Provided Methods</a><div class="sidebar-links"><a href="#method.alloc_zeroed">alloc_zeroed</a><a href="#method.realloc">realloc</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../index.html'>std</a>::<wbr><a href='index.html'>alloc</a></p><script>window.sidebarCurrent = {name: 'GlobalAlloc', ty: 'trait', 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'>Trait <a href='../index.html'>std</a>::<wbr><a href='index.html'>alloc</a>::<wbr><a class="trait" href=''>GlobalAlloc</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/core/alloc.rs.html#466-610' title='goto source code'>[src]</a></span></h1><div class="docblock type-decl"><pre class='rust trait'>pub unsafe trait GlobalAlloc {
    unsafe fn <a href='#tymethod.alloc' class='fnname'>alloc</a>(&amp;self, layout: <a class="struct" href="../../std/alloc/struct.Layout.html" title="struct std::alloc::Layout">Layout</a>) -&gt; <a class="primitive" href="../primitive.pointer.html">*mut </a><a class="primitive" href="../primitive.u8.html">u8</a>;
<div class='item-spacer'></div>    unsafe fn <a href='#tymethod.dealloc' class='fnname'>dealloc</a>(&amp;self, ptr: <a class="primitive" href="../primitive.pointer.html">*mut </a><a class="primitive" href="../primitive.u8.html">u8</a>, layout: <a class="struct" href="../../std/alloc/struct.Layout.html" title="struct std::alloc::Layout">Layout</a>);

    unsafe fn <a href='#method.alloc_zeroed' class='fnname'>alloc_zeroed</a>(&amp;self, layout: <a class="struct" href="../../std/alloc/struct.Layout.html" title="struct std::alloc::Layout">Layout</a>) -&gt; <a class="primitive" href="../primitive.pointer.html">*mut </a><a class="primitive" href="../primitive.u8.html">u8</a> { ... }
<div class='item-spacer'></div>    unsafe fn <a href='#method.realloc' class='fnname'>realloc</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ptr: <a class="primitive" href="../primitive.pointer.html">*mut </a><a class="primitive" href="../primitive.u8.html">u8</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;layout: <a class="struct" href="../../std/alloc/struct.Layout.html" title="struct std::alloc::Layout">Layout</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new_size: <a class="primitive" href="../primitive.usize.html">usize</a><br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="primitive" href="../primitive.pointer.html">*mut </a><a class="primitive" href="../primitive.u8.html">u8</a> { ... }
}</pre></div><div class='docblock'><p>A memory allocator that can be registered as the standard library’s default
though the <code>#[global_allocator]</code> attributes.</p>
<p>Some of the methods require that a memory block be <em>currently
allocated</em> via an allocator. This means that:</p>
<ul>
<li>
<p>the starting address for that memory block was previously
returned by a previous call to an allocation method
such as <code>alloc</code>, and</p>
</li>
<li>
<p>the memory block has not been subsequently deallocated, where
blocks are deallocated either by being passed to a deallocation
method such as <code>dealloc</code> or by being
passed to a reallocation method that returns a non-null pointer.</p>
</li>
</ul>
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
<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">Layout</span>, <span class="ident">alloc</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">ptr</span>::<span class="ident">null_mut</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">null_mut</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="attribute">#[<span class="ident">global_allocator</span>]</span>
<span class="kw">static</span> <span class="ident">A</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="kw">unsafe</span> {
        <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">alloc</span>(<span class="ident">Layout</span>::<span class="ident">new</span>::<span class="op">&lt;</span><span class="ident">u32</span><span class="op">&gt;</span>()).<span class="ident">is_null</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%20Layout%2C%20alloc%7D%3B%0Ause%20std%3A%3Aptr%3A%3Anull_mut%3B%0A%0Astruct%20MyAllocator%3B%0A%0Aunsafe%20impl%20GlobalAlloc%20for%20MyAllocator%20%7B%0A%20%20%20%20unsafe%20fn%20alloc(%26self%2C%20_layout%3A%20Layout)%20-%3E%20*mut%20u8%20%7B%20null_mut()%20%7D%0A%20%20%20%20unsafe%20fn%20dealloc(%26self%2C%20_ptr%3A%20*mut%20u8%2C%20_layout%3A%20Layout)%20%7B%7D%0A%7D%0A%0A%23%5Bglobal_allocator%5D%0Astatic%20A%3A%20MyAllocator%20%3D%20MyAllocator%3B%0A%0Afn%20main()%20%7B%0A%20%20%20%20unsafe%20%7B%0A%20%20%20%20%20%20%20%20assert!(alloc(Layout%3A%3Anew%3A%3A%3Cu32%3E()).is_null())%0A%20%20%20%20%7D%0A%7D">Run</a></pre>
<h1 id="unsafety" class="section-header"><a href="#unsafety">Unsafety</a></h1>
<p>The <code>GlobalAlloc</code> trait is an <code>unsafe</code> trait for a number of reasons, and
implementors must ensure that they adhere to these contracts:</p>
<ul>
<li>
<p>It's undefined behavior if global allocators unwind.  This restriction may
be lifted in the future, but currently a panic from any of these
functions may lead to memory unsafety.</p>
</li>
<li>
<p><code>Layout</code> queries and calculations in general must be correct. Callers of
this trait are allowed to rely on the contracts defined on each method,
and implementors must ensure such contracts remain true.</p>
</li>
</ul>
</div>
            <h2 id='required-methods' class='small-section-header'>
              Required Methods<a href='#required-methods' class='anchor'></a>
            </h2>
            <div class='methods'>
        <h3 id='tymethod.alloc' class='method'><span id='alloc.v' class='invisible'><code>unsafe fn <a href='#tymethod.alloc' class='fnname'>alloc</a>(&amp;self, layout: <a class="struct" href="../../std/alloc/struct.Layout.html" title="struct std::alloc::Layout">Layout</a>) -&gt; <a class="primitive" href="../primitive.pointer.html">*mut </a><a class="primitive" href="../primitive.u8.html">u8</a></code></span></h3><div class='docblock'><p>Allocate memory as described by the given <code>layout</code>.</p>
<p>Returns a pointer to newly-allocated memory,
or null to indicate allocation failure.</p>
<h1 id="safety" class="section-header"><a href="#safety">Safety</a></h1>
<p>This function is unsafe because undefined behavior can result
if the caller does not ensure that <code>layout</code> has non-zero size.</p>
<p>(Extension subtraits might provide more specific bounds on
behavior, e.g. guarantee a sentinel address or a null pointer
in response to a zero-size allocation request.)</p>
<p>The allocated block of memory may or may not be initialized.</p>
<h1 id="errors" class="section-header"><a href="#errors">Errors</a></h1>
<p>Returning a null pointer indicates that either memory is exhausted
or <code>layout</code> does not meet allocator's size or alignment constraints.</p>
<p>Implementations are encouraged to return null on memory
exhaustion rather than aborting, but this is not
a strict requirement. (Specifically: it is <em>legal</em> to
implement this trait atop an underlying native allocation
library that aborts on memory exhaustion.)</p>
<p>Clients wishing to abort computation in response to an
allocation error are encouraged to call the <a href="../../alloc/alloc/fn.handle_alloc_error.html"><code>handle_alloc_error</code></a> function,
rather than directly invoking <code>panic!</code> or similar.</p>
</div><h3 id='tymethod.dealloc' class='method'><span id='dealloc.v' class='invisible'><code>unsafe fn <a href='#tymethod.dealloc' class='fnname'>dealloc</a>(&amp;self, ptr: <a class="primitive" href="../primitive.pointer.html">*mut </a><a class="primitive" href="../primitive.u8.html">u8</a>, layout: <a class="struct" href="../../std/alloc/struct.Layout.html" title="struct std::alloc::Layout">Layout</a>)</code></span></h3><div class='docblock'><p>Deallocate the block of memory at the given <code>ptr</code> pointer with the given <code>layout</code>.</p>
<h1 id="safety-1" class="section-header"><a href="#safety-1">Safety</a></h1>
<p>This function is unsafe because undefined behavior can result
if the caller does not ensure all of the following:</p>
<ul>
<li>
<p><code>ptr</code> must denote a block of memory currently allocated via
this allocator,</p>
</li>
<li>
<p><code>layout</code> must be the same layout that was used
to allocated that block of memory,</p>
</li>
</ul>
</div></div>
            <h2 id='provided-methods' class='small-section-header'>
              Provided Methods<a href='#provided-methods' class='anchor'></a>
            </h2>
            <div class='methods'>
        <h3 id='method.alloc_zeroed' class='method'><span id='alloc_zeroed.v' class='invisible'><code>unsafe fn <a href='#method.alloc_zeroed' class='fnname'>alloc_zeroed</a>(&amp;self, layout: <a class="struct" href="../../std/alloc/struct.Layout.html" title="struct std::alloc::Layout">Layout</a>) -&gt; <a class="primitive" href="../primitive.pointer.html">*mut </a><a class="primitive" href="../primitive.u8.html">u8</a></code></span></h3><div class='docblock'><p>Behaves like <code>alloc</code>, but also ensures that the contents
are set to zero before being returned.</p>
<h1 id="safety-2" class="section-header"><a href="#safety-2">Safety</a></h1>
<p>This function is unsafe for the same reasons that <code>alloc</code> is.
However the allocated block of memory is guaranteed to be initialized.</p>
<h1 id="errors-1" class="section-header"><a href="#errors-1">Errors</a></h1>
<p>Returning a null pointer indicates that either memory is exhausted
or <code>layout</code> does not meet allocator's size or alignment constraints,
just as in <code>alloc</code>.</p>
<p>Clients wishing to abort computation in response to an
allocation error are encouraged to call the <a href="../../alloc/alloc/fn.handle_alloc_error.html"><code>handle_alloc_error</code></a> function,
rather than directly invoking <code>panic!</code> or similar.</p>
</div><h3 id='method.realloc' class='method'><span id='realloc.v' class='invisible'><code>unsafe fn <a href='#method.realloc' class='fnname'>realloc</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&amp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;ptr: <a class="primitive" href="../primitive.pointer.html">*mut </a><a class="primitive" href="../primitive.u8.html">u8</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;layout: <a class="struct" href="../../std/alloc/struct.Layout.html" title="struct std::alloc::Layout">Layout</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;new_size: <a class="primitive" href="../primitive.usize.html">usize</a><br>) -&gt; <a class="primitive" href="../primitive.pointer.html">*mut </a><a class="primitive" href="../primitive.u8.html">u8</a></code></span></h3><div class='docblock'><p>Shink or grow a block of memory to the given <code>new_size</code>.
The block is described by the given <code>ptr</code> pointer and <code>layout</code>.</p>
<p>If this returns a non-null pointer, then ownership of the memory block
referenced by <code>ptr</code> has been transferred to this alloctor.
The memory may or may not have been deallocated,
and should be considered unusable (unless of course it was
transferred back to the caller again via the return value of
this method).</p>
<p>If this method returns null, then ownership of the memory
block has not been transferred to this allocator, and the
contents of the memory block are unaltered.</p>
<h1 id="safety-3" class="section-header"><a href="#safety-3">Safety</a></h1>
<p>This function is unsafe because undefined behavior can result
if the caller does not ensure all of the following:</p>
<ul>
<li>
<p><code>ptr</code> must be currently allocated via this allocator,</p>
</li>
<li>
<p><code>layout</code> must be the same layout that was used
to allocated that block of memory,</p>
</li>
<li>
<p><code>new_size</code> must be greater than zero.</p>
</li>
<li>
<p><code>new_size</code>, when rounded up to the nearest multiple of <code>layout.align()</code>,
must not overflow (i.e. the rounded value must be less than <code>usize::MAX</code>).</p>
</li>
</ul>
<p>(Extension subtraits might provide more specific bounds on
behavior, e.g. guarantee a sentinel address or a null pointer
in response to a zero-size allocation request.)</p>
<h1 id="errors-2" class="section-header"><a href="#errors-2">Errors</a></h1>
<p>Returns null if the new layout does not meet the size
and alignment constraints of the allocator, or if reallocation
otherwise fails.</p>
<p>Implementations are encouraged to return null on memory
exhaustion rather than panicking or aborting, but this is not
a strict requirement. (Specifically: it is <em>legal</em> to
implement this trait atop an underlying native allocation
library that aborts on memory exhaustion.)</p>
<p>Clients wishing to abort computation in response to a
reallocation error are encouraged to call the <a href="../../alloc/alloc/fn.handle_alloc_error.html"><code>handle_alloc_error</code></a> function,
rather than directly invoking <code>panic!</code> or similar.</p>
</div></div>
        <h2 id='implementors' class='small-section-header'>
          Implementors<a href='#implementors' class='anchor'></a>
        </h2>
        <ul class='item-list' id='implementors-list'>
    <li><table class='table-display'><tbody><tr><td><code>impl GlobalAlloc for <a class="struct" href="../../std/alloc/struct.System.html" title="struct std::alloc::System">System</a></code><td></td></tr></tbody></table></li>
</ul><script type="text/javascript">window.inlined_types=new Set([]);</script><script type="text/javascript" async
                         src="../../implementors/core/alloc/trait.GlobalAlloc.js">
                 </script></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>