diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/memory%2Faligned_alloc.html | |
new repository
Diffstat (limited to 'devdocs/c/memory%2Faligned_alloc.html')
| -rw-r--r-- | devdocs/c/memory%2Faligned_alloc.html | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/devdocs/c/memory%2Faligned_alloc.html b/devdocs/c/memory%2Faligned_alloc.html new file mode 100644 index 00000000..839438f0 --- /dev/null +++ b/devdocs/c/memory%2Faligned_alloc.html @@ -0,0 +1,42 @@ + <h1 id="firstHeading" class="firstHeading">aligned_alloc</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdlib.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">void *aligned_alloc( size_t alignment, size_t size );</pre> +</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <p>Allocate <code>size</code> bytes of uninitialized storage whose alignment is specified by <code>alignment</code>. The <code>size</code> parameter must be an integral multiple of <code>alignment</code>.</p> +<p><code>aligned_alloc</code> is thread-safe: it behaves as though only accessing the memory locations visible through its argument, and not any static storage.</p> +<p>A previous call to <code><a href="free" title="c/memory/free">free</a></code> or <code><a href="realloc" title="c/memory/realloc">realloc</a></code> that deallocates a region of memory <i>synchronizes-with</i> a call to <code>aligned_alloc</code> that allocates the same or a part of the same region of memory. This synchronization occurs after any access to the memory by the deallocating function and before any access to the memory by <code>aligned_alloc</code>. There is a single total order of all allocation and deallocation functions operating on each particular region of memory.</p> +<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> alignment </td> <td> - </td> <td> specifies the alignment. Must be a valid alignment supported by the implementation. </td> +</tr> <tr class="t-par"> <td> size </td> <td> - </td> <td> number of bytes to allocate. An integral multiple of <code>alignment</code> </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <p>On success, returns the pointer to the beginning of newly allocated memory. To avoid a memory leak, the returned pointer must be deallocated with <code><a href="free" title="c/memory/free">free</a></code> or <code><a href="realloc" title="c/memory/realloc">realloc</a></code>.</p> +<p>On failure, returns a null pointer.</p> +<h3 id="Notes"> Notes</h3> <p>Passing a <code>size</code> which is not an integral multiple of <code>alignment</code> or an <code>alignment</code> which is not valid or not supported by the implementation causes the function to fail and return a null pointer (C11, as published, specified undefined behavior in this case, this was corrected by <a rel="nofollow" class="external text" href="https://open-std.org/JTC1/SC22/WG14/www/docs/summary.htm#dr_460">DR460</a>). Removal of size restrictions to make it possible to allocate small objects at restrictive alignment boundaries (similar to <a href="../language/_alignas" title="c/language/ Alignas"><code>alignas</code></a>) has been proposed by <a rel="nofollow" class="external text" href="https://open-std.org/JTC1/SC22/WG14/www/docs/n2072.htm">N2072</a>.</p> +<p>As an example of the "supported by the implementation" requirement, POSIX function <a rel="nofollow" class="external text" href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.html"><code>posix_memalign</code></a> accepts any <code>alignment</code> that is a power of two and a multiple of <code>sizeof(void *)</code>, and POSIX-based implementations of <code>aligned_alloc</code> inherit this requirements.</p> +<p>Fundamental alignments are always supported. If <code>alignment</code> is a power of two and not greater than <code>_Alignof<span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/max_align_t"><span class="kw104">max_align_t</span></a><span class="br0">)</span></code>, <code>aligned_alloc</code> may simply call <code><a href="malloc" title="c/memory/malloc">malloc</a></code>.</p> +<p>Regular <code><a href="malloc" title="c/memory/malloc">malloc</a></code> aligns memory suitable for any object type with a fundamental alignment. The <code>aligned_alloc</code> is useful for over-aligned allocations, such as to <a href="https://en.wikipedia.org/wiki/Streaming_SIMD_Extensions" class="extiw" title="enwiki:Streaming SIMD Extensions">SSE</a>, cache line, or <a href="https://en.wikipedia.org/wiki/Page_(computer_memory)#Multiple_page_sizes" class="extiw" title="enwiki:Page (computer memory)">VM page</a> boundary.</p> +<p>This function is not supported in Microsoft C Runtime library because its implementation of <code>std::free</code> is <a rel="nofollow" class="external text" href="https://learn.microsoft.com/en-us/cpp/standard-library/cstdlib#remarks-6">unable to handle aligned allocations</a> of any kind. Instead, MS CRT provides <a rel="nofollow" class="external text" href="https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/aligned-malloc"><code>_aligned_malloc</code></a> (to be freed with <a rel="nofollow" class="external text" href="https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/aligned-free"><code>_aligned_free</code></a>).</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h> +#include <stdlib.h> + +int main(void) +{ + int *p1 = malloc(10*sizeof *p1); + printf("default-aligned addr: %p\n", (void*)p1); + free(p1); + + int *p2 = aligned_alloc(1024, 1024*sizeof *p2); + printf("1024-byte aligned addr: %p\n", (void*)p2); + free(p2); +}</pre></div> <p>Possible output:</p> +<div class="text source-text"><pre data-language="c">default-aligned addr: 0x1e40c20 +1024-byte aligned addr: 0x1e41000</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C23 standard (ISO/IEC 9899:2023): </li> +<ul><li> 7.22.3.1 The aligned_alloc function (p: TBD) </li></ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 7.22.3.1 The aligned_alloc function (p: 253) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.22.3.1 The aligned_alloc function (p: 347-348) </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/memory/c/aligned_alloc" title="cpp/memory/c/aligned alloc">C++ documentation</a></span> for <code>aligned_alloc</code> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/memory/aligned_alloc" class="_attribution-link">https://en.cppreference.com/w/c/memory/aligned_alloc</a> + </p> +</div> |
