From 91b3769b032a8a0e2cf9ff4f213290dd29016cdc Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 6 Sep 2025 11:50:59 -0700 Subject: [PATCH] langref: update "Choosing an Allocator" section and delete "Implementing an Allocator" section because it is out of scope. --- doc/langref.html.in | 31 ++++++++++--------------------- 1 file 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 @@ -6277,10 +6277,6 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
  • Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely the right choice, at least for your main allocator.
  • -
  • - Need to use the same allocator in multiple threads? Use one of your choice - wrapped around {#syntax#}std.heap.ThreadSafeAllocator{#endsyntax#} -
  • 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#}. @@ -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
  • 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.
  • - 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. +
  • +
  • + You can also consider implementing an allocator.
  • {#header_close#} @@ -6355,17 +6355,6 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val

    TODO: thread local variables

    {#header_close#} - {#header_open|Implementing an Allocator#} -

    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#}. -

    -

    - There are many example allocators to look at for inspiration. Look at std/heap.zig and - {#syntax#}std.heap.GeneralPurposeAllocator{#endsyntax#}. -

    - {#header_close#} - {#header_open|Heap Allocation Failure#}

    Many programming languages choose to handle the possibility of heap allocation failure by