64 Commits

Author SHA1 Message Date
Kendall Condon
b4b1daf001
Allocator.create: properly handle alignment for zero-sized types (#21864) 2025-03-26 15:31:57 +00:00
Alex Rønne Petersen
e1c6af2840
Merge pull request #23220 from samy-00007/bytesAsSlice-fix
Minor fix for `Allocator.remap` and `mem.bytesAsSlice` for zero-sized types
2025-03-25 13:45:10 +01:00
samy007
be483dabc8 fix: Allocator.remap now handles zero-bytes sized types 2025-03-24 22:05:57 +01:00
mlugg
24db007cde std.mem.Allocator.remap: fix incorrect doc comment (part 2) 2025-03-11 22:15:43 +00:00
mlugg
5ffd8bf5f6 std.mem.Allocator.remap: fix incorrect doc comment
Resolves: #23194
2025-03-11 04:15:38 +00:00
Andrew Kelley
e630b20c62 std.mem.Allocator.VTable: improve doc comment wording 2025-02-06 14:50:20 -08:00
Andrew Kelley
36e9b0f026 std.mem.Allocator: keep the undefined memset
Reversal on the decision: the Allocator interface is the correct place
for the memset to undefined because it allows Allocator implementations
to bypass the interface and use a backing allocator directly, skipping
the performance penalty of memsetting the entire allocation, which may
be very large, as well as having valuable zeroes on them.

closes #4298
2025-02-06 14:23:23 -08:00
Andrew Kelley
7eeef5fb2b std.mem.Allocator: introduce remap function to the interface
This one changes the size of an allocation, allowing it to be relocated.
However, the implementation will still return `null` if it would be
equivalent to

new = alloc
memcpy(new, old)
free(old)

Mainly this prepares for taking advantage of `mremap` which I thought
would be a bigger deal but apparently is only available on Linux. Still,
we should use it on Linux.
2025-02-06 14:23:23 -08:00
Andrew Kelley
4913de3c88 GeneralPurposeAllocator: minimal fix
This keeps the implementation matching master branch, however,
introduces a compile error that applications can work around by
explicitly setting page_size_max and page_size_min to match their
computer's settings, in the case that those values are not already
equal.

I plan to rework this allocator in a follow-up enhancement with the goal
of reducing total active memory mappings.
2025-02-06 14:23:23 -08:00
Andrew Kelley
284de7d957 adjust runtime page size APIs
* fix merge conflicts
* rename the declarations
* reword documentation
* extract FixedBufferAllocator to separate file
* take advantage of locals
* remove the assertion about max alignment in Allocator API, leaving it
  Allocator implementation defined
* fix non-inline function call in start logic

The GeneralPurposeAllocator implementation is totally broken because it
uses global state but I didn't address that in this commit.
2025-02-06 14:23:23 -08:00
Archbirdplus
439667be04 runtime page size detection
heap.zig: define new default page sizes
heap.zig: add min/max_page_size and their options
lib/std/c: add miscellaneous declarations
heap.zig: add pageSize() and its options
switch to new page sizes, especially in GPA/stdlib
mem.zig: remove page_size
2025-02-06 14:23:23 -08:00
Ryan Liptak
4041cc06d5 Allocator/Random: document that comparing ptr may result in illegal behavior
See #21756 and #17704
2025-02-03 17:07:28 -08:00
mlugg
f244c8891a
std.mem.Allocator: remove redundant check
This check doesn't make sense with the modern Allocator API; it's left over
from when realloc could change alignment. It's statically known (but not
comptime-known) to be true always. This check was one of the things
blocking Allocator from being used at comptime (related: #1291).
2025-01-22 02:00:47 +00:00
mlugg
9804cc8bc6
all: update to std.builtin.Type.{Pointer,Array,StructField} field renames 2025-01-16 12:49:58 +00:00
mlugg
d00e05f186
all: update to std.builtin.Type.Pointer.Size field renames
This was done by regex substitution with `sed`. I then manually went
over the entire diff and fixed any incorrect changes.

This diff also changes a lot of `callconv(.C)` to `callconv(.c)`, since
my regex happened to also trigger here. I opted to leave these changes
in, since they *are* a correct migration, even if they're not the one I
was trying to do!
2025-01-16 12:46:29 +00:00
Ilia Choly
e2f24a2d70 Allocator.free: document zero-length behavior
It wasn't immediately clear from the implementation whether passing
zero-length memory to free() was undefined behavior or intentionally
supported. Since ArrayList and other core data structures rely on
this behavior working correctly, this should be explicitly documented
as part of the public API contract.
2024-11-24 18:19:11 -05:00
mlugg
0fe3fd01dd
std: update std.builtin.Type fields to follow naming conventions
The compiler actually doesn't need any functional changes for this: Sema
does reification based on the tag indices of `std.builtin.Type` already!
So, no zig1.wasm update is necessary.

This change is necessary to disallow name clashes between fields and
decls on a type, which is a prerequisite of #9938.
2024-08-28 08:39:59 +01:00
Pascal S. de Kloe
9126852ba9 mem: explicit dupe and dupeZ error on Allocator 2023-09-07 21:56:57 +03:00
xdBronch
737b366279 add error when passing a non-single-item pointer to allocator.destroy 2023-07-25 10:55:04 -07:00
IntegratedQuantum
49ac816e36
Optimize Allocator functions to create less duplicate code for similar types (#16332)
* Move functionality from generic functions that doesn't depend on the type into a function that only depends on comptime alignment.

This reduces comptime code duplication because e.g. `alloc(u32, )` and `alloc(i32, )` now use the same function `allocWithFoo(4, 4, )` under the hood.
2023-07-06 14:41:49 -04:00
mlugg
f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -07:00
Eric Joldasov
50339f595a all: zig fmt and rename "@XToY" to "@YFromX"
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-19 12:34:42 -07:00
Motiejus Jakštys
d41111d7ef mem: rename align*Generic to mem.align*
Anecdote 1: The generic version is way more popular than the non-generic
one in Zig codebase:

     git grep -w alignForward | wc -l
    56
     git grep -w alignForwardGeneric | wc -l
    149

     git grep -w alignBackward | wc -l
    6
     git grep -w alignBackwardGeneric | wc -l
    15

Anecdote 2: In my project (turbonss) that does much arithmetic and
alignment I exclusively use the Generic functions.

Anecdote 3: we used only the Generic versions in the Macho Man's linker
workshop.
2023-06-17 12:49:13 -07:00
Andrew Kelley
6261c13731 update codebase to use @memset and @memcpy 2023-04-28 13:24:43 -07:00
Andrew Kelley
edb5e493e6 update @memcpy to require equal src and dest lens
* Sema: upgrade operands to array pointers if possible when emitting
   AIR.
 * Implement safety checks for length mismatch and aliasing.
 * AIR: make ptrtoint support slice operands. Implement in LLVM backend.
 * C backend: implement new `@memset` semantics. `@memcpy` is not done
   yet.
2023-04-25 11:23:40 -07:00
Andrew Kelley
a5c910adb6 change semantics of @memcpy and @memset
Now they use slices or array pointers with any element type instead of
requiring byte pointers.

This is a breaking enhancement to the language.

The safety check for overlapping pointers will be implemented in a
future commit.

closes #14040
2023-04-25 11:23:40 -07:00
Veikka Tuominen
7199d7c777 split @qualCast into @constCast and @volatileCast 2023-02-15 01:43:57 +02:00
Andrew Kelley
3c2a43fdcc Revert "std: check types of pointers passed to allocator functions"
This reverts commit abc9530a88d24350481d9264edcde300f293929a.

This patch implies that the idiomatic Zig way of handling anytype
parameter is to write a bunch of boilerplate instead of directly
accessing type information and relying on the compiler to be useful.

I don't want it to be this way.

It is the compiler's job to make useful error messages when the wrong
field of a type info result is accessed, and it is the zig programmer's
job to understand what it means when a compile error points at the field
access of `@typeInfo` (along with the relevant callsites).

One thing that might be useful would be having the compiler be aware of
module boundaries and highlighting the boundaries of them. The first
reference note after crossing a module boundary is likely the most
interesting one.
2023-02-12 05:59:28 -07:00
Leo Constantinides
abc9530a88
std: check types of pointers passed to allocator functions 2023-02-12 00:04:27 +00:00
Ganesan Rajagopal
21c2bca908 Allocator.zig: minor fixes
* Remove recursive call using null alignment, since it's no longer relevant.
* Fix comment
2022-12-13 15:04:03 -05:00
Andrew Kelley
50eb7983cd remove most conditional compilation based on stage1
There are still a few occurrences of "stage1" in the standard library
and self-hosted compiler source, however, these instances need a bit
more careful inspection to ensure no breakage.
2022-12-06 20:38:54 -07:00
Andrew Kelley
ceb0a632cf std.mem.Allocator: allow shrink to fail
closes #13535
2022-11-29 23:30:38 -07:00
Veikka Tuominen
b6b3462796 std.mem.Allocator: do not return undefined pointers from create
Closes #13517
2022-11-16 01:12:27 +02:00
kkHAIKE
949cca9f2a Allocator: fix len_align calc in large type size case 2022-10-29 17:53:56 -04:00
Veikka Tuominen
5b79f42dc5 std.mem.Allocator: do not return undefined pointers 2022-10-27 01:31:18 +03:00
ominitay
295451dfe5
std: Replace use of stage1 function pointers 2022-09-29 21:45:30 +03:00
Veikka Tuominen
72c60acd5d Revert "std.mem.Allocator: add alignedCreate"
This reverts commit 5647a73fea7ecc9e1ee190362ef47f402eb95dff.
2022-07-23 15:30:12 +03:00
devins2518
5647a73fea
std.mem.Allocator: add alignedCreate 2022-07-23 14:22:35 +03:00
Lee Cannon
3679d737f8 Allocator: correct PanicFree function name 2022-05-02 15:29:27 -04:00
Damien Firmenich
5fafcc2b62
zig fmt: remove trailing whitespace on doc comments
Fixes #11353

The renderer treats comments and doc comments differently since doc
comments are parsed into the Ast. This commit adds a check after getting
the text for the doc comment and trims whitespace at the end before
rendering.

The `a = 0,` in the test is here to avoid a ParseError while parsing the
test.
2022-04-05 18:08:33 +03:00
Andrew Kelley
db55f469c1 std.mem.Allocator: upgrade to new function pointer semantics 2022-01-26 20:02:01 -07:00
Lee Cannon
16b7535497
Allocator: allocBytes and reallocBytes (#10352)
Closes #10348
2021-12-19 01:58:13 -05:00
Isaac Freund
9f9f215305
stage1, stage2: rename c_void to anyopaque (#10316)
zig fmt now replaces c_void with anyopaque to make updating
code easy.
2021-12-19 00:24:45 -05:00
Lee Cannon
fb9fcf5632 allocator: Move vtable into gen struct 2021-12-02 11:56:32 -08:00
Lee Cannon
066eaa5e9c
allocgate: change resize to return optional instead of error 2021-11-30 23:45:01 +00:00
Lee Cannon
f68cda738a
allocgate: split free out from resize 2021-11-30 23:32:48 +00:00
Lee Cannon
02e5e0ba1f
allocgate: apply missed changes 2021-11-30 23:32:48 +00:00
Lee Cannon
9377f32c08
allocgate: utilize a *const vtable field 2021-11-30 23:32:48 +00:00
Lee Cannon
85de022c56
allocgate: std Allocator interface refactor 2021-11-30 23:32:47 +00:00
Andrew Kelley
902df103c6 std lib API deprecations for the upcoming 0.9.0 release
See #3811
2021-11-30 00:13:07 -07:00