diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-09-06 11:50:59 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-09-06 11:51:27 -0700 |
| commit | 91b3769b032a8a0e2cf9ff4f213290dd29016cdc (patch) | |
| tree | 7e3a68afe897c2f60fb7c58c8c91f08dac96d97c /doc | |
| parent | 1a5cf072a8894976c1cd16d7132af671c7cdbb4d (diff) | |
| download | zig-91b3769b032a8a0e2cf9ff4f213290dd29016cdc.tar.gz zig-91b3769b032a8a0e2cf9ff4f213290dd29016cdc.zip | |
langref: update "Choosing an Allocator" section
and delete "Implementing an Allocator" section because it is out of
scope.
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/langref.html.in | 31 |
1 files changed, 10 insertions, 21 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in index 9e2659d977..f56d9d1912 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -6278,10 +6278,6 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val <li>Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely the right choice, at least for your main allocator.</li> <li> - Need to use the same allocator in multiple threads? Use one of your choice - wrapped around {#syntax#}std.heap.ThreadSafeAllocator{#endsyntax#} - </li> - <li> Is the maximum number of bytes that you will need bounded by a number known at {#link|comptime#}? In this case, use {#syntax#}std.heap.FixedBufferAllocator{#endsyntax#}. </li> @@ -6290,7 +6286,7 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val cyclical pattern (such as a video game main loop, or a web server request handler), such that it would make sense to free everything at once at the end? In this case, it is recommended to follow this pattern: - {#code|cli_allocation.zig#} + {#code|cli_allocation.zig#} When using this kind of allocator, there is no need to free anything manually. Everything gets freed at once with the call to {#syntax#}arena.deinit(){#endsyntax#}. @@ -6313,14 +6309,18 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val </li> <li> Finally, if none of the above apply, you need a general purpose allocator. - Zig's general purpose allocator is available as a function that takes a {#link|comptime#} - {#link|struct#} of configuration options and returns a type. - Generally, you will set up one {#syntax#}std.heap.GeneralPurposeAllocator{#endsyntax#} in - your main function, and then pass it or sub-allocators around to various parts of your + If you are in Debug mode, {#syntax#}std.heap.DebugAllocator{#endsyntax#} is available as a + function that takes a {#link|comptime#} {#link|struct#} of configuration options and returns a type. + Generally, you will set up exactly one in your main function, and + then pass it or sub-allocators around to various parts of your application. </li> <li> - You can also consider {#link|Implementing an Allocator#}. + If you are compiling in ReleaseFast mode, {#syntax#}std.heap.smp_allocator{#endsyntax#} is + a solid choice for a general purpose allocator. + </li> + <li> + You can also consider implementing an allocator. </li> </ol> {#header_close#} @@ -6355,17 +6355,6 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val <p>TODO: thread local variables</p> {#header_close#} - {#header_open|Implementing an Allocator#} - <p>Zig programmers can implement their own allocators by fulfilling the Allocator interface. - In order to do this one must read carefully the documentation comments in std/mem.zig and - then supply a {#syntax#}allocFn{#endsyntax#} and a {#syntax#}resizeFn{#endsyntax#}. - </p> - <p> - There are many example allocators to look at for inspiration. Look at std/heap.zig and - {#syntax#}std.heap.GeneralPurposeAllocator{#endsyntax#}. - </p> - {#header_close#} - {#header_open|Heap Allocation Failure#} <p> Many programming languages choose to handle the possibility of heap allocation failure by |
