145 Commits

Author SHA1 Message Date
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
John Benediktsson
1c07eacc7f std.process: adding hasNonEmptyEnvVar() and using for NO_COLOR 2025-02-06 15:00:48 +01:00
Adheesh Wadkar
b5a2487f7a Fix chdirC compile error 2025-01-20 15:37:57 -06:00
Andrew Kelley
9d00f69be5 move std.zig.fatal to std.process.fatal 2024-07-20 01:06:28 -07:00
Andrew Kelley
e8c4e79499 std.c reorganization
It is now composed of these main sections:
* Declarations that are shared among all operating systems.
* Declarations that have the same name, but different type signatures
  depending on the operating system. Often multiple operating systems
  share the same type signatures however.
* Declarations that are specific to a single operating system.
  - These are imported one per line so you can see where they come from,
    protected by a comptime block to prevent accessing the wrong one.

Closes #19352 by changing the convention to making types `void` and
functions `{}`, so that it becomes possible to update `@hasDecl` sites
to use `@TypeOf(f) != void` or `T != void`. Happily, this ended up
removing some duplicate logic and update some bitrotted feature
detection checks.

A handful of types have been modified to gain namespacing and type
safety. This is a breaking change.

Oh, and the last usage of `usingnamespace` site is eliminated.
2024-07-19 00:30:32 -07:00
Ryan Liptak
d48251d0f0 ArgIteratorWindows.init: Take []const u16 slice instead of multi-item pointer
Now that we use the PEB to get the precise length of the command line string, there's no need for a multi-item pointer/sliceTo call. This provides a minor speedup:

    Benchmark 1 (153 runs): benchargv-before.exe
      measurement          mean ± σ            min … max           outliers         delta
      wall_time          32.7ms ±  429us    32.1ms … 36.9ms          1 ( 1%)        0%
      peak_rss           6.49MB ± 5.62KB    6.46MB … 6.49MB         14 ( 9%)        0%
    Benchmark 2 (157 runs): benchargv-after.exe
      measurement          mean ± σ            min … max           outliers         delta
      wall_time          31.9ms ±  236us    31.4ms … 32.7ms          4 ( 3%)        -  2.4% ±  0.2%
      peak_rss           6.49MB ± 4.77KB    6.46MB … 6.49MB         14 ( 9%)          +  0.0% ±  0.0%
2024-07-13 18:37:49 -07:00
Ryan Liptak
1a62cfffa7 Replace GetCommandLineW with PEB access, delete GetCommandLine bindings 2024-07-13 18:19:19 -07:00
Ryan Liptak
1418c8a5d4 ArgIteratorWindows: Store last emitted code unit instead of checking the last 6 emitted bytes
Previously, to ensure args were encoded as well-formed WTF-8 (i.e. no encoded surrogate pairs), the code unit would be encoded and then the last 6 emitted bytes would be checked to see if they were a surrogate pair, and this was done for any emitted code unit (although this was not necessary, it should have only been done when emitting a low surrogate).

After this commit, we still want to ensure well-formed WTF-8, but, to do so, the last emitted code point is stored, meaning we can just directly check that the last code unit is a high surrogate and the current code unit is a low surrogate to determine if we have a surrogate pair.

This provides some performance benefit over and above a "use the same strategy as before but only check when we're emitting a low surrogate" implementation:

    Benchmark 1 (111 runs): benchargv-master.exe
      measurement          mean ± σ            min … max           outliers         delta
      wall_time          45.2ms ±  532us    44.5ms … 49.4ms          2 ( 2%)        0%
      peak_rss           6.49MB ± 3.94KB    6.46MB … 6.49MB         10 ( 9%)        0%
    Benchmark 2 (154 runs): benchargv-storelast.exe
      measurement          mean ± σ            min … max           outliers         delta
      wall_time          32.6ms ±  293us    32.2ms … 34.2ms          8 ( 5%)        - 27.8% ±  0.2%
      peak_rss           6.49MB ± 5.15KB    6.46MB … 6.49MB         15 (10%)          -  0.0% ±  0.0%
    Benchmark 3 (131 runs): benchargv-onlylow.exe
      measurement          mean ± σ            min … max           outliers         delta
      wall_time          38.4ms ±  257us    37.9ms … 39.6ms          5 ( 4%)        - 15.1% ±  0.2%
      peak_rss           6.49MB ± 5.70KB    6.46MB … 6.49MB          9 ( 7%)          -  0.0% ±  0.0%
2024-07-13 18:19:19 -07:00
Ryan Liptak
10914dc310 ArgIteratorWindows: Clarify buffer length comment 2024-07-13 17:48:08 -07:00
Ryan Liptak
959d227d13 ArgIteratorWindows: Reduce allocated memory by parsing the WTF-16 string directly
Before this commit, the WTF-16 command line string would be converted to WTF-8 in `init`, and then a second buffer of the WTF-8 size + 1 would be allocated to store the parsed arguments. The converted WTF-8 command line would then be parsed and the relevant bytes would be copied into the argument buffer before being returned.

After this commit, only the WTF-8 size of the WTF-16 string is calculated (without conversion) which is then used to allocate the buffer for the parsed arguments. Parsing is then done on the WTF-16 slice directly, with the arguments being converted to WTF-8 on-the-fly.

This has a few (minor) benefits:

- Cuts the amount of memory allocated by ArgIteratorWindows in half (or better)
- Makes the total amount of memory allocated by ArgIteratorWindows predictable, since, before, the upfront `wtf16LeToWtf8Alloc` call could end up allocating more-memory-than-necessary temporarily due to its internal use of an ArrayList. Now, the amount of memory allocated is always exactly `calcWtf8Len(cmd_line) + 1`.
2024-07-13 14:48:17 -07:00
Jora Troosh
13070448f5
std: fix typos (#20560) 2024-07-09 14:25:42 -07:00
Ryan Liptak
76fb2b685b std: Convert deprecated aliases to compile errors and fix usages
Deprecated aliases that are now compile errors:

- `std.fs.MAX_PATH_BYTES` (renamed to `std.fs.max_path_bytes`)
- `std.mem.tokenize` (split into `tokenizeAny`, `tokenizeSequence`, `tokenizeScalar`)
- `std.mem.split` (split into `splitSequence`, `splitAny`, `splitScalar`)
- `std.mem.splitBackwards` (split into `splitBackwardsSequence`, `splitBackwardsAny`, `splitBackwardsScalar`)
- `std.unicode`
  + `utf16leToUtf8Alloc`, `utf16leToUtf8AllocZ`, `utf16leToUtf8`, `fmtUtf16le` (all renamed to have capitalized `Le`)
  + `utf8ToUtf16LeWithNull` (renamed to `utf8ToUtf16LeAllocZ`)
- `std.zig.CrossTarget` (moved to `std.Target.Query`)

Deprecated `lib/std/std.zig` decls were deleted instead of made a `@compileError` because the `refAllDecls` in the test block would trigger the `@compileError`. The deleted top-level `std` namespaces are:

- `std.rand` (renamed to `std.Random`)
- `std.TailQueue` (renamed to `std.DoublyLinkedList`)
- `std.ChildProcess` (renamed/moved to `std.process.Child`)

This is not exhaustive. Deprecated aliases that I didn't touch:
  + `std.io.*`
  + `std.Build.*`
  + `std.builtin.Mode`
  + `std.zig.c_translation.CIntLiteralRadix`
  + anything in `src/`
2024-06-13 10:18:59 -04:00
Andrew Kelley
45b505c930 std.process.cleanExit: lock stderr before exiting
This makes it so that any other threads which are writing to stderr have
a chance to finish before the process terminates. It also clears the
terminal in case any progress has been written to stderr, while still
accomplishing the goal of not waiting until the update thread exits.
2024-05-27 20:56:49 -07:00
Andrew Kelley
eb718ceffa std.process: fix compilation on 32-bit targets 2024-05-27 20:56:48 -07:00
Jacob Young
d77f5e7aaa Progress: fix compile errors on windows
Works for `zig build-exe`, IPC still not implemented yet.
2024-05-27 20:56:48 -07:00
Andrew Kelley
c01cfde688 std.process.Child: fix ZIG_PROGRESS env var handling
and properly dup2 the file descriptor to make it handle the case when
other files are already open
2024-05-27 20:56:48 -07:00
Andrew Kelley
f07116404a std.Progress: child process sends updates via IPC 2024-05-27 20:56:48 -07:00
Andrew Kelley
f47824f24d std: restructure child process namespace 2024-05-26 09:31:55 -07:00
Jakub Konka
2e1fc0dd14 handle visionos target OS tag in the compiler
* rename .xros to .visionos as agreed in the tracking issue
* add support for VisionOS platform in the MachO linker
2024-05-09 15:04:15 +02:00
Andrew Kelley
16d368d0d2 introduce std.process.raiseFileDescriptorLimit 2024-05-03 21:03:29 -07:00
Ryan Liptak
f4c4c04f1c ArgIteratorWindows: Match post-2008 C runtime rather than CommandLineToArgvW
On Windows, the command line arguments of a program are a single WTF-16 encoded string and it's up to the program to split it into an array of strings. In C/C++, the entry point of the C runtime takes care of splitting the command line and passing argc/argv to the main function.

https://github.com/ziglang/zig/pull/18309 updated ArgIteratorWindows to match the behavior of CommandLineToArgvW, but it turns out that CommandLineToArgvW's behavior does not match the behavior of the C runtime post-2008. In 2008, the C runtime argv splitting changed how it handles consecutive double quotes within a quoted argument (it's now considered an escaped quote, e.g. `"foo""bar"` post-2008 would get parsed into `foo"bar`), and the rules around argv[0] were also changed.

This commit makes ArgIteratorWindows match the behavior of the post-2008 C runtime, and adds a standalone test that verifies the behavior matches both the MSVC and MinGW argv splitting exactly in all cases (it checks that randomly generated command line strings get split the same way).

The motivation here is roughly the same as when the same change was made in Rust (https://github.com/rust-lang/rust/pull/87580), that is (paraphrased):

- Consistent behavior between Zig and modern C/C++ programs
- Allows users to escape double quotes in a way that can be more straightforward

Additionally, the suggested mitigation for BatBadBut (https://flatt.tech/research/posts/batbadbut-you-cant-securely-execute-commands-on-windows/) relies on the post-2008 argv splitting behavior for roundtripping of the arguments given to `cmd.exe`. Note: it's not necessary for the suggested mitigation to work, but it is necessary for the suggested escaping to be parsed back into the intended argv by ArgIteratorWindows after being run through a `.bat` file.
2024-04-15 02:09:48 -07:00
Andrew Kelley
12191c8a22 std: promote tests to doctests
Now these show up as "example usage" in generated documentation.
2024-03-21 14:11:46 -07:00
Andrew Kelley
cd62005f19 extract std.posix from std.os
closes #5019
2024-03-19 11:45:09 -07:00
Ryan Liptak
abd250bb9c Use stack fallback allocator to usually avoid extra heap allocation in getEnvVarOwned 2024-02-24 14:05:24 -08:00
Ryan Liptak
68b87918df Fix handling of Windows (WTF-16) and WASI (UTF-8) paths
Windows paths now use WTF-16 <-> WTF-8 conversion everywhere, which is lossless. Previously, conversion of ill-formed UTF-16 paths would either fail or invoke illegal behavior.

WASI paths must be valid UTF-8, and the relevant function calls have been updated to handle the possibility of failure due to paths not being encoded/encodable as valid UTF-8.

Closes #18694
Closes #1774
Closes #2565
2024-02-24 14:05:24 -08:00
Ryan Liptak
80508b98c2 Update deprecated std.unicode function usages 2024-02-24 14:04:59 -08:00
Ian Kerins
ee36131e6a doc: fix typo in getEnvMap 2024-02-07 12:41:40 +02:00
Tristan Ross
c0e0bb385c std.process: return u64 in totalSystemMemory 2024-01-23 00:17:53 -08:00
Carl Åstholm
13f78e24b8 Update ArgIterator on Windows to follow standard Windows parsing rules
This adds `ArgIteratorWindows`, which faithfully replicates the quoting and escaping behavior observed in `CommandLineToArgvW` and should make Zig applications play better with processes that abuse these quirks.
2023-12-18 22:55:46 +01:00
mlugg
51595d6b75
lib: correct unnecessary uses of 'var' 2023-11-19 09:55:07 +00:00
Andrew Kelley
f708c5fafc CLI: finish updating module API usage
Finish the work started in 4c4fb839972f66f55aa44fc0aca5f80b0608c731.
Now the compiler compiles again.

Wire up dependency tree fetching code in the CLI for `zig build`.
Everything is hooked up except for `createDependenciesModule` is not yet
implemented.
2023-10-08 16:54:31 -07:00
Stephen Gregoratto
285970982a Add illumos OS tag
- Adds `illumos` to the `Target.Os.Tag` enum. A new function,
  `isSolarish` has been added that returns true if the tag is either
  Solaris or Illumos. This matches the naming convention found in Rust's
  `libc` crate[1].
- Add the tag wherever `.solaris` is being checked against.
- Check for the C pre-processor macro `__illumos__` in CMake to set the
  proper target tuple. Illumos distros patch their compilers to have
  this in the "built-in" set (verified with `echo | cc -dM -E -`).

  Alternatively you could check the output of `uname -o`.

Right now, both Solaris and Illumos import from `c/solaris.zig`. In the
future it may be worth putting the shared ABI bits in a base file, and
mixing that in with specific `c/solaris.zig`/`c/illumos.zig` files.

[1]: 6e02a329a2/src/unix/solarish
2023-10-02 15:31:49 -06:00
Kitty-Cricket Piapiac
d370005d34 std.process.totalSystemMemory: return correct error type on FreeBSD 2023-08-01 19:27:04 -07:00
Andrew Kelley
70d1bb8049 Revert "std.process: further totalSystemMemory portage"
This reverts commit 5c70d7bc723a8e0e47018d3606285005c280ddb8.
2023-07-31 11:20:21 -07: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
Michael Dusan
40fc71bf71
openbsd: fix std.c.getdents and debitrot
- fix getdents return type usize → c_int
- special-case process.zig to use sysctl instead of sysctlbyname
- use struct/field pattern for sysctl HW_* constants
2023-06-15 14:48:20 -04:00
Jacob Young
04e66e6b4d InternPool: add optional coercion 2023-06-10 20:47:59 -07:00
Jacob Young
dc18739a73 process: add more missing const 2023-06-10 20:47:58 -07:00
Ryan Liptak
2129f28953 Update all std.mem.split calls to their appropriate function
Everywhere that can now use `splitScalar` should get a nice little performance boost.
2023-05-13 13:45:05 -07:00
Ryan Liptak
815e53b147 Update all std.mem.tokenize calls to their appropriate function
Everywhere that can now use `tokenizeScalar` should get a nice little performance boost.
2023-05-13 13:45:04 -07:00
Andrew Kelley
6261c13731 update codebase to use @memset and @memcpy 2023-04-28 13:24:43 -07:00
David CARLIER
5c70d7bc72 std.process: further totalSystemMemory portage 2023-04-24 05:49:48 +03:00
Nicolas Sterchele
284c7b22a8 process: add args definition comment
To improve understandability of its purpose.
2023-04-23 20:26:10 +03:00
David CARLIER
29bc6fc242 process: totalSystemMemory freebsd portage 2023-04-22 14:10:30 +03:00
xEgoist
0733c8c5ca windows: replace GetPhysicallyInstalledSystemMemory with ntdll.
`GetPhysicallyInstalledSystemMemory` uses SMBios to grab the physical
memory size which can lead to unecessary allocation and inacurate
representation of the total memory. Using `System_Basic_Information`
help to retrieve the physical memory which is not reserved for the
kernel/tables. This aligns better with the linux side as `/proc/meminfo`
does the same thing.
2023-04-14 13:43:03 -04:00
alion02
8853005672 Fix crash on some Windows machines 2023-04-06 07:26:07 +02:00
tjog
f6a2b72ba8 std.process.Child: implement maxrss on Darwin
Notably the Darwin (XNU) kernel the maxrss field is number of bytes
and not kilobytes (kibibytes) like other platforms (e.g. Linux, BSD).

watchOS and tvOS are not supported because they do not have the ability
to spawn a child process. iOS is enabled but due to OS sandboxing it
should fail with a permission error.
2023-03-25 03:20:50 +01:00
Andrew Kelley
61d7e31078 remove bad unit test from std lib
This unit test tested the command line arguments which were passed to
the test runner, which is not really something that unit tests are
supposed to observe.

The proper way to test command line argument parsing is with a
standalone test, where the set of command line arguments being tested
for are also being controlled by the test itself.
2023-03-15 10:48:14 -07:00