Skip to content

Commit 1b8aa0a

Browse files
authored
update allocators for smp_allocator and gpa deprecation (#328)
1 parent 038575f commit 1b8aa0a

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

website/versioned_docs/version-0.15.x/02-standard-library/01-allocators.mdx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import AllocatorsAlloc from "!!raw-loader!./01.allocators-alloc.zig";
88
import AllocatorsFba from "!!raw-loader!./01.allocators-fba.zig";
99
import AllocatorsArena from "!!raw-loader!./01.allocators-arena.zig";
1010
import AllocatorsCreate from "!!raw-loader!./01.allocators-create.zig";
11-
import AllocatorsGpa from "!!raw-loader!./01.allocators-gpa.zig";
11+
import AllocatorsDebug from "!!raw-loader!./01.allocators-debug.zig";
12+
import AllocatorsSmp from "!!raw-loader!./01.allocators-smp.zig";
1213

1314
# Allocators
1415

@@ -17,7 +18,7 @@ the programmer to choose precisely how memory allocations are done within the
1718
standard library - no allocations happen behind your back in the standard
1819
library.
1920

20-
The most basic allocator is
21+
The most fundamental allocator is
2122
[`std.heap.page_allocator`](https://ziglang.org/documentation/master/std/#std.heap.page_allocator).
2223
Whenever this allocator makes an allocation, it will ask your OS for entire
2324
pages of memory; an allocation of a single byte will likely reserve multiple
@@ -50,18 +51,24 @@ once. Here, `.deinit()` is called on the arena, which frees all memory. Using
5051

5152
<CodeBlock language="zig">{AllocatorsCreate}</CodeBlock>
5253

53-
The Zig standard library also has a general-purpose allocator. This is a safe
54+
The Zig standard library also has a general-purpose debug allocator. This is a safe
5455
allocator that can prevent double-free, use-after-free and can detect leaks.
5556
Safety checks and thread safety can be turned off via its configuration struct
56-
(left empty below). Zig's GPA is designed for safety over performance, but may
57+
(left empty below). Zig's DebugAllocator is designed for safety over performance, but may
5758
still be many times faster than page_allocator.
5859

59-
<CodeBlock language="zig">{AllocatorsGpa}</CodeBlock>
60+
<CodeBlock language="zig">{AllocatorsDebug}</CodeBlock>
6061

61-
For high performance (but very few safety features!),
62-
[`std.heap.c_allocator`](https://ziglang.org/documentation/master/std/#std.heap.c_allocator)
63-
may be considered. This,however, has the disadvantage of requiring linking Libc,
64-
which can be done with `-lc`.
62+
For high performance (but very few safety features!), the Zig standard library offers
63+
[`std.heap.SmpAllocator`](https://ziglang.org/documentation/master/std/#std.heap.SmpAllocator).
64+
This is a general-purpose allocator designed for [maximum performance](https://ziglang.org/download/0.14.0/release-notes.html#SmpAllocator)
65+
on multithreaded machines.
66+
67+
<CodeBlock language="zig">{AllocatorsSmp}</CodeBlock>
68+
69+
As an alternative when SmpAllocator is not available or desired, the libc allocator
70+
[`std.heap.c_allocator`](https://ziglang.org/documentation/master/std/#std.heap.c_allocator) may be considered.
71+
This, however, has the disadvantage of requiring linking Libc, which can be done with `-lc`.
6572

6673
Benjamin Feng's talk
6774
[_What's a Memory Allocator Anyway?_](https://www.youtube.com/watch?v=vHWiDx_l4V0)

website/versioned_docs/version-0.15.x/02-standard-library/01.allocators-gpa.zig renamed to website/versioned_docs/version-0.15.x/02-standard-library/01.allocators-debug.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const std = @import("std");
33
const expect = std.testing.expect;
44

55
// hide-end
6-
test "GPA" {
7-
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
6+
test "DebugAllocator" {
7+
var gpa: std.heap.DebugAllocator(.{}) = .init;
88
const allocator = gpa.allocator();
99
defer {
1010
const deinit_status = gpa.deinit();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// hide-start
2+
const std = @import("std");
3+
const expect = std.testing.expect;
4+
5+
// hide-end
6+
test "SmpAllocator" {
7+
const allocator = std.heap.smp_allocator;
8+
9+
const bytes = try allocator.alloc(u8, 100);
10+
defer allocator.free(bytes);
11+
}

0 commit comments

Comments
 (0)