@zigc/lib 0.16.0 → 0.17.0-dev.135

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (268) hide show
  1. package/c/fcntl.zig +6 -1
  2. package/c/inttypes.zig +0 -10
  3. package/c/math.zig +46 -122
  4. package/c/pthread.zig +57 -0
  5. package/c/search.zig +1 -27
  6. package/c/stdlib/drand48.zig +0 -57
  7. package/c/stdlib.zig +0 -100
  8. package/c/string.zig +20 -7
  9. package/c/strings.zig +0 -38
  10. package/c/unistd.zig +27 -26
  11. package/c/wchar.zig +10 -0
  12. package/c.zig +2 -2
  13. package/compiler/aro/aro/CodeGen.zig +5 -6
  14. package/compiler/aro/aro/Compilation.zig +17 -14
  15. package/compiler/aro/aro/Driver.zig +14 -13
  16. package/compiler/aro/aro/Parser.zig +20 -15
  17. package/compiler/aro/aro/Pragma.zig +3 -2
  18. package/compiler/aro/aro/Preprocessor.zig +9 -6
  19. package/compiler/aro/aro/pragmas/message.zig +3 -2
  20. package/compiler/aro/aro/text_literal.zig +3 -2
  21. package/compiler/aro/assembly_backend/x86_64.zig +4 -4
  22. package/compiler/build_runner.zig +0 -2
  23. package/compiler/reduce/Walk.zig +7 -7
  24. package/compiler/test_runner.zig +2 -2
  25. package/compiler/translate-c/Translator.zig +6 -2
  26. package/compiler/translate-c/main.zig +1 -1
  27. package/compiler_rt/cos.zig +0 -2
  28. package/compiler_rt/divmodei4.zig +40 -17
  29. package/compiler_rt/exp.zig +1 -6
  30. package/compiler_rt/exp2.zig +1 -6
  31. package/compiler_rt/exp_f128.zig +377 -0
  32. package/compiler_rt/fabs.zig +0 -2
  33. package/compiler_rt/fma.zig +0 -2
  34. package/compiler_rt/fmax.zig +0 -2
  35. package/compiler_rt/fmin.zig +0 -2
  36. package/compiler_rt/fmod.zig +0 -2
  37. package/compiler_rt/limb64.zig +876 -15
  38. package/compiler_rt/log.zig +0 -2
  39. package/compiler_rt/log10.zig +0 -2
  40. package/compiler_rt/log2.zig +0 -2
  41. package/compiler_rt/mulXi3.zig +1 -1
  42. package/compiler_rt/round.zig +0 -2
  43. package/compiler_rt/sin.zig +0 -2
  44. package/compiler_rt/sincos.zig +0 -2
  45. package/compiler_rt/sqrt.zig +0 -2
  46. package/compiler_rt/ssp.zig +1 -1
  47. package/compiler_rt/tan.zig +0 -2
  48. package/compiler_rt/trunc.zig +0 -2
  49. package/compiler_rt/udivmodei4.zig +28 -0
  50. package/fuzzer.zig +2 -0
  51. package/libc/include/aarch64-linux-any/asm/hwcap.h +1 -0
  52. package/libc/include/aarch64-linux-any/asm/unistd_64.h +1 -0
  53. package/libc/include/any-linux-any/asm-generic/errno.h +2 -0
  54. package/libc/include/any-linux-any/asm-generic/unistd.h +4 -1
  55. package/libc/include/any-linux-any/drm/amdgpu_drm.h +20 -6
  56. package/libc/include/any-linux-any/drm/amdxdna_accel.h +8 -0
  57. package/libc/include/any-linux-any/drm/drm_fourcc.h +6 -6
  58. package/libc/include/any-linux-any/drm/panfrost_drm.h +75 -1
  59. package/libc/include/any-linux-any/drm/panthor_drm.h +154 -3
  60. package/libc/include/any-linux-any/drm/rocket_accel.h +74 -24
  61. package/libc/include/any-linux-any/drm/xe_drm.h +89 -6
  62. package/libc/include/any-linux-any/linux/android/binder.h +1 -1
  63. package/libc/include/any-linux-any/linux/bpf.h +28 -0
  64. package/libc/include/any-linux-any/linux/btrfs.h +1 -0
  65. package/libc/include/any-linux-any/linux/btrfs_tree.h +32 -2
  66. package/libc/include/any-linux-any/linux/dma-buf.h +1 -0
  67. package/libc/include/any-linux-any/linux/dpll.h +1 -0
  68. package/libc/include/any-linux-any/linux/elf.h +2 -0
  69. package/libc/include/any-linux-any/linux/ethtool.h +21 -5
  70. package/libc/include/any-linux-any/linux/fs.h +1 -0
  71. package/libc/include/any-linux-any/linux/hyperv.h +1 -1
  72. package/libc/include/any-linux-any/linux/idxd.h +134 -134
  73. package/libc/include/any-linux-any/linux/if_alg.h +1 -1
  74. package/libc/include/any-linux-any/linux/if_link.h +1 -0
  75. package/libc/include/any-linux-any/linux/input-event-codes.h +4 -0
  76. package/libc/include/any-linux-any/linux/io_uring/bpf_filter.h +68 -0
  77. package/libc/include/any-linux-any/linux/io_uring/query.h +5 -1
  78. package/libc/include/any-linux-any/linux/io_uring.h +33 -2
  79. package/libc/include/any-linux-any/linux/iommufd.h +39 -0
  80. package/libc/include/any-linux-any/linux/kfd_ioctl.h +13 -3
  81. package/libc/include/any-linux-any/linux/kfd_sysfs.h +2 -1
  82. package/libc/include/any-linux-any/linux/kvm.h +30 -6
  83. package/libc/include/any-linux-any/linux/landlock.h +22 -8
  84. package/libc/include/any-linux-any/linux/magic.h +1 -0
  85. package/libc/include/any-linux-any/linux/mempolicy.h +3 -0
  86. package/libc/include/any-linux-any/linux/mount.h +11 -2
  87. package/libc/include/any-linux-any/linux/mptcp_pm.h +1 -1
  88. package/libc/include/any-linux-any/linux/mshv.h +2 -0
  89. package/libc/include/any-linux-any/linux/netfilter_bridge.h +5 -4
  90. package/libc/include/any-linux-any/linux/netfilter_ipv4.h +4 -5
  91. package/libc/include/any-linux-any/linux/netfilter_ipv6.h +3 -4
  92. package/libc/include/any-linux-any/linux/nfs.h +1 -1
  93. package/libc/include/any-linux-any/linux/nfsd_netlink.h +1 -0
  94. package/libc/include/any-linux-any/linux/nilfs2_api.h +2 -2
  95. package/libc/include/any-linux-any/linux/nilfs2_ondisk.h +97 -66
  96. package/libc/include/any-linux-any/linux/nl80211.h +104 -3
  97. package/libc/include/any-linux-any/linux/pci.h +7 -0
  98. package/libc/include/any-linux-any/linux/pci_regs.h +65 -6
  99. package/libc/include/any-linux-any/linux/pcitest.h +1 -0
  100. package/libc/include/any-linux-any/linux/perf_event.h +24 -3
  101. package/libc/include/any-linux-any/linux/pkt_sched.h +1 -0
  102. package/libc/include/any-linux-any/linux/prctl.h +30 -0
  103. package/libc/include/any-linux-any/linux/rseq.h +62 -5
  104. package/libc/include/any-linux-any/linux/shm.h +0 -1
  105. package/libc/include/any-linux-any/linux/stddef.h +4 -0
  106. package/libc/include/any-linux-any/linux/sysctl.h +1 -2
  107. package/libc/include/any-linux-any/linux/taskstats.h +12 -1
  108. package/libc/include/any-linux-any/linux/tcp.h +23 -3
  109. package/libc/include/any-linux-any/linux/typelimits.h +8 -0
  110. package/libc/include/any-linux-any/linux/ublk_cmd.h +120 -1
  111. package/libc/include/any-linux-any/linux/v4l2-controls.h +63 -0
  112. package/libc/include/any-linux-any/linux/vbox_vmmdev_types.h +2 -2
  113. package/libc/include/any-linux-any/linux/vduse.h +80 -5
  114. package/libc/include/any-linux-any/linux/version.h +3 -3
  115. package/libc/include/any-linux-any/linux/vfio.h +4 -0
  116. package/libc/include/any-linux-any/linux/videodev2.h +3 -0
  117. package/libc/include/any-linux-any/linux/virtio_ring.h +1 -2
  118. package/libc/include/any-linux-any/linux/vmclock-abi.h +20 -0
  119. package/libc/include/any-linux-any/rdma/bnxt_re-abi.h +16 -0
  120. package/libc/include/any-linux-any/rdma/ib_user_ioctl_cmds.h +16 -0
  121. package/libc/include/any-linux-any/rdma/mana-abi.h +3 -0
  122. package/libc/include/any-linux-any/scsi/scsi_bsg_ufs.h +8 -9
  123. package/libc/include/any-linux-any/sound/sof/tokens.h +6 -0
  124. package/libc/include/arc-linux-any/asm/swab.h +0 -63
  125. package/libc/include/arc-linux-any/asm/unistd_32.h +1 -0
  126. package/libc/include/arm-linux-any/asm/ptrace.h +0 -9
  127. package/libc/include/arm-linux-any/asm/unistd-eabi.h +1 -0
  128. package/libc/include/arm-linux-any/asm/unistd-oabi.h +1 -0
  129. package/libc/include/csky-linux-any/asm/unistd_32.h +1 -0
  130. package/libc/include/hexagon-linux-any/asm/unistd_32.h +1 -0
  131. package/libc/include/loongarch-linux-any/asm/hwcap.h +1 -0
  132. package/libc/include/loongarch-linux-any/asm/kvm.h +1 -0
  133. package/libc/include/loongarch-linux-any/asm/kvm_para.h +1 -0
  134. package/libc/include/loongarch-linux-any/asm/unistd_32.h +2 -0
  135. package/libc/include/loongarch-linux-any/asm/unistd_64.h +2 -0
  136. package/libc/include/m68k-linux-any/asm/unistd_32.h +1 -0
  137. package/libc/include/mips-linux-any/asm/errno.h +2 -0
  138. package/libc/include/mips-linux-any/asm/unistd_n32.h +1 -0
  139. package/libc/include/mips-linux-any/asm/unistd_n64.h +1 -0
  140. package/libc/include/mips-linux-any/asm/unistd_o32.h +1 -0
  141. package/libc/include/powerpc-linux-any/asm/unistd_32.h +1 -0
  142. package/libc/include/powerpc-linux-any/asm/unistd_64.h +1 -0
  143. package/libc/include/riscv-linux-any/asm/hwprobe.h +4 -0
  144. package/libc/include/riscv-linux-any/asm/kvm.h +3 -0
  145. package/libc/include/riscv-linux-any/asm/ptrace.h +37 -0
  146. package/libc/include/riscv-linux-any/asm/sigcontext.h +1 -0
  147. package/libc/include/riscv-linux-any/asm/unistd_32.h +1 -0
  148. package/libc/include/riscv-linux-any/asm/unistd_64.h +1 -0
  149. package/libc/include/s390x-linux-any/asm/unistd_64.h +1 -0
  150. package/libc/include/sparc-linux-any/asm/errno.h +2 -0
  151. package/libc/include/sparc-linux-any/asm/ioctls.h +4 -4
  152. package/libc/include/sparc-linux-any/asm/unistd_32.h +2 -0
  153. package/libc/include/sparc-linux-any/asm/unistd_64.h +2 -0
  154. package/libc/include/x86-linux-any/asm/auxvec.h +0 -4
  155. package/libc/include/x86-linux-any/asm/kvm.h +13 -8
  156. package/libc/include/x86-linux-any/asm/svm.h +16 -16
  157. package/libc/include/x86-linux-any/asm/unistd_32.h +1 -0
  158. package/libc/include/x86-linux-any/asm/unistd_64.h +1 -0
  159. package/libc/include/x86-linux-any/asm/unistd_x32.h +1 -0
  160. package/libc/include/xtensa-linux-any/asm/unistd_32.h +1 -0
  161. package/libc/musl/arch/mipsn32/syscall_arch.h +35 -32
  162. package/package.json +1 -1
  163. package/std/Build/Cache.zig +6 -6
  164. package/std/Build/Step/Compile.zig +0 -1
  165. package/std/Build/Step/Run.zig +2 -2
  166. package/std/Build/Step.zig +2 -4
  167. package/std/Build/WebServer.zig +2 -2
  168. package/std/Build.zig +0 -3
  169. package/std/Io/Dir.zig +7 -2
  170. package/std/Io/Dispatch.zig +3 -13
  171. package/std/Io/File/Writer.zig +8 -6
  172. package/std/Io/Reader.zig +8 -9
  173. package/std/Io/Semaphore.zig +112 -17
  174. package/std/Io/Terminal.zig +1 -1
  175. package/std/Io/Threaded.zig +171 -37
  176. package/std/Io/Uring.zig +13 -15
  177. package/std/Io/Writer.zig +46 -42
  178. package/std/Io/net.zig +11 -11
  179. package/std/Io.zig +90 -26
  180. package/std/SemanticVersion.zig +1 -1
  181. package/std/Target/Query.zig +2 -2
  182. package/std/Target.zig +50 -5
  183. package/std/array_hash_map.zig +9 -18
  184. package/std/builtin.zig +4 -0
  185. package/std/c/haiku.zig +3 -0
  186. package/std/c/serenity.zig +1 -6
  187. package/std/c.zig +89 -7
  188. package/std/compress/flate/Decompress.zig +2 -3
  189. package/std/compress/zstd/Decompress.zig +2 -4
  190. package/std/crypto/Certificate.zig +13 -1
  191. package/std/crypto/ascon.zig +75 -33
  192. package/std/crypto/codecs/asn1/Oid.zig +12 -1
  193. package/std/crypto/codecs/base64_hex_ct.zig +2 -4
  194. package/std/crypto/ml_kem.zig +2 -9
  195. package/std/crypto/tls/Client.zig +79 -4
  196. package/std/crypto/tls.zig +1 -1
  197. package/std/crypto.zig +1 -0
  198. package/std/debug/Pdb.zig +1 -1
  199. package/std/debug.zig +4 -3
  200. package/std/fmt.zig +8 -3
  201. package/std/fs/path.zig +6 -4
  202. package/std/heap/BufferFirstAllocator.zig +165 -0
  203. package/std/heap.zig +2 -126
  204. package/std/http/Client.zig +21 -24
  205. package/std/http.zig +3 -4
  206. package/std/json/Scanner.zig +2 -2
  207. package/std/os/emscripten.zig +1 -1
  208. package/std/os/linux/IoUring.zig +2 -0
  209. package/std/os/linux/aarch64.zig +41 -12
  210. package/std/os/linux/arc.zig +173 -0
  211. package/std/os/linux/arm.zig +41 -12
  212. package/std/os/linux/hexagon.zig +33 -11
  213. package/std/os/linux/loongarch32.zig +41 -13
  214. package/std/os/linux/loongarch64.zig +41 -12
  215. package/std/os/linux/m68k.zig +41 -13
  216. package/std/os/linux/mips.zig +67 -36
  217. package/std/os/linux/mips64.zig +60 -29
  218. package/std/os/linux/mipsn32.zig +60 -29
  219. package/std/os/linux/or1k.zig +41 -12
  220. package/std/os/linux/powerpc.zig +41 -12
  221. package/std/os/linux/powerpc64.zig +41 -12
  222. package/std/os/linux/riscv32.zig +41 -12
  223. package/std/os/linux/riscv64.zig +41 -12
  224. package/std/os/linux/s390x.zig +44 -7
  225. package/std/os/linux/sparc64.zig +83 -52
  226. package/std/os/linux/syscalls.zig +26 -1
  227. package/std/os/linux/thumb.zig +52 -36
  228. package/std/os/linux/x32.zig +41 -12
  229. package/std/os/linux/x86.zig +42 -13
  230. package/std/os/linux/x86_64.zig +41 -12
  231. package/std/os/linux.zig +412 -436
  232. package/std/os/uefi/tables/boot_services.zig +9 -8
  233. package/std/os.zig +41 -0
  234. package/std/process.zig +1 -1
  235. package/std/sort.zig +3 -3
  236. package/std/zig/Ast/Render.zig +3 -3
  237. package/std/zig/AstGen.zig +44 -98
  238. package/std/zig/AstRlAnnotate.zig +0 -11
  239. package/std/zig/BuiltinFn.zig +0 -32
  240. package/std/zig/LibCInstallation.zig +4 -3
  241. package/std/zig/Parse.zig +7 -7
  242. package/std/zig/WindowsSdk.zig +13 -13
  243. package/std/zig/Zir.zig +50 -63
  244. package/std/zig/ZonGen.zig +6 -5
  245. package/std/zig/llvm/Builder.zig +12 -12
  246. package/std/zig.zig +1 -10
  247. package/std/zip.zig +5 -5
  248. package/zig.h +340 -1
  249. package/libc/include/hexagon-linux-any/asm/signal.h +0 -29
  250. package/libc/include/s390x-linux-any/asm/tape390.h +0 -103
  251. package/libc/mingw/math/fdiml.c +0 -24
  252. package/libc/mingw/winpthreads/spinlock.c +0 -82
  253. package/libc/musl/src/linux/tee.c +0 -8
  254. package/libc/musl/src/math/fdimf.c +0 -10
  255. package/libc/musl/src/math/fdiml.c +0 -18
  256. package/libc/musl/src/string/strdup.c +0 -10
  257. package/libc/musl/src/string/strndup.c +0 -12
  258. package/libc/musl/src/string/wcsdup.c +0 -10
  259. package/libc/musl/src/thread/pthread_spin_destroy.c +0 -6
  260. package/libc/musl/src/thread/pthread_spin_init.c +0 -6
  261. package/libc/musl/src/thread/pthread_spin_lock.c +0 -8
  262. package/libc/musl/src/thread/pthread_spin_trylock.c +0 -7
  263. package/libc/musl/src/thread/pthread_spin_unlock.c +0 -7
  264. package/libc/musl/src/unistd/dup2.c +0 -20
  265. package/libc/musl/src/unistd/dup3.c +0 -26
  266. package/libc/wasi/thread-stub/pthread_spin_lock.c +0 -8
  267. package/libc/wasi/thread-stub/pthread_spin_trylock.c +0 -8
  268. package/libc/wasi/thread-stub/pthread_spin_unlock.c +0 -7
package/std/fmt.zig CHANGED
@@ -542,7 +542,7 @@ pub fn parseIntSizeSuffix(buf: []const u8, digit_base: u8) ParseIntError!usize {
542
542
  }
543
543
  const multiplier = math.powi(usize, magnitude_base, orders_of_magnitude) catch |err| switch (err) {
544
544
  error.Underflow => unreachable,
545
- error.Overflow => return error.Overflow,
545
+ error.Overflow => |e| return e,
546
546
  };
547
547
  const number = try std.fmt.parseInt(usize, without_suffix, digit_base);
548
548
  return math.mul(usize, number, multiplier);
@@ -1342,9 +1342,9 @@ pub const hex_charset = "0123456789abcdef";
1342
1342
 
1343
1343
  /// Converts an unsigned integer of any multiple of u8 to an array of lowercase
1344
1344
  /// hex bytes, little endian.
1345
- pub fn hex(x: anytype) [@sizeOf(@TypeOf(x)) * 2]u8 {
1345
+ pub fn hex(x: anytype) [@typeInfo(@TypeOf(x)).int.bits / 4]u8 {
1346
1346
  comptime assert(@typeInfo(@TypeOf(x)).int.signedness == .unsigned);
1347
- var result: [@sizeOf(@TypeOf(x)) * 2]u8 = undefined;
1347
+ var result: [@typeInfo(@TypeOf(x)).int.bits / 4]u8 = undefined;
1348
1348
  var i: usize = 0;
1349
1349
  while (i < result.len / 2) : (i += 1) {
1350
1350
  const byte: u8 = @truncate(x >> @intCast(8 * i));
@@ -1360,6 +1360,11 @@ test hex {
1360
1360
  try std.testing.expect(x.len == 8);
1361
1361
  try std.testing.expectEqualStrings("efbeadde", &x);
1362
1362
  }
1363
+ {
1364
+ const s = "[" ++ hex(@as(u48, 0x12345678_abcd)) ++ "]";
1365
+ try std.testing.expect(s.len == 14);
1366
+ try std.testing.expectEqualStrings("[cdab78563412]", s);
1367
+ }
1363
1368
  {
1364
1369
  const s = "[" ++ hex(@as(u64, 0x12345678_abcdef00)) ++ "]";
1365
1370
  try std.testing.expect(s.len == 18);
package/std/fs/path.zig CHANGED
@@ -894,8 +894,9 @@ pub fn resolve(allocator: Allocator, paths: []const []const u8) Allocator.Error!
894
894
  pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) Allocator.Error![]u8 {
895
895
  // Avoid heap allocation when paths.len is <= @bitSizeOf(usize) * 2
896
896
  // (we use `* 3` because stackFallback uses 1 usize as a length)
897
- var bit_set_allocator_state = std.heap.stackFallback(@sizeOf(usize) * 3, allocator);
898
- const bit_set_allocator = bit_set_allocator_state.get();
897
+ var buf: [3]usize = undefined;
898
+ var bit_set_allocator_state: std.heap.BufferFirstAllocator = .init(@ptrCast(&buf), allocator);
899
+ const bit_set_allocator = bit_set_allocator_state.allocator();
899
900
  var relevant_paths = try std.bit_set.DynamicBitSetUnmanaged.initEmpty(bit_set_allocator, paths.len);
900
901
  defer relevant_paths.deinit(bit_set_allocator);
901
902
 
@@ -1642,7 +1643,8 @@ fn windowsResolveAgainstCwd(
1642
1643
  parsed: WindowsPath2(u8),
1643
1644
  ) ![]u8 {
1644
1645
  // Space for 256 WTF-16 code units; potentially 3 WTF-8 bytes per WTF-16 code unit
1645
- var temp_allocator_state = std.heap.stackFallback(256 * 3, gpa);
1646
+ var buf: [256 * 3]u8 = undefined;
1647
+ var temp_allocator_state: std.heap.BufferFirstAllocator = .init(&buf, gpa);
1646
1648
  return switch (parsed.kind) {
1647
1649
  .drive_absolute,
1648
1650
  .unc_absolute,
@@ -1668,7 +1670,7 @@ fn windowsResolveAgainstCwd(
1668
1670
  }
1669
1671
  },
1670
1672
  .drive_relative => blk: {
1671
- const temp_allocator = temp_allocator_state.get();
1673
+ const temp_allocator = temp_allocator_state.allocator();
1672
1674
  const drive_cwd = drive_cwd: {
1673
1675
  const parsed_cwd = parsePathWindows(u8, cwd);
1674
1676
 
@@ -0,0 +1,165 @@
1
+ //! An allocator that attempts to allocate from the given buffer, falling back to
2
+ //! `fallback_allocator` if this fails.
3
+
4
+ const std = @import("../std.zig");
5
+ const heap = std.heap;
6
+ const testing = std.testing;
7
+
8
+ const Alignment = std.mem.Alignment;
9
+ const Allocator = std.mem.Allocator;
10
+ const FixedBufferAllocator = std.heap.FixedBufferAllocator;
11
+
12
+ const BufferFirstAllocator = @This();
13
+
14
+ fallback_allocator: Allocator,
15
+ fixed_buffer_allocator: FixedBufferAllocator,
16
+
17
+ pub fn init(buffer: []u8, fallback_allocator: Allocator) BufferFirstAllocator {
18
+ return .{
19
+ .fallback_allocator = fallback_allocator,
20
+ .fixed_buffer_allocator = .init(buffer),
21
+ };
22
+ }
23
+
24
+ pub fn allocator(self: *BufferFirstAllocator) Allocator {
25
+ return .{
26
+ .ptr = self,
27
+ .vtable = &.{
28
+ .alloc = alloc,
29
+ .resize = resize,
30
+ .remap = remap,
31
+ .free = free,
32
+ },
33
+ };
34
+ }
35
+
36
+ fn alloc(
37
+ ctx: *anyopaque,
38
+ len: usize,
39
+ alignment: Alignment,
40
+ ra: usize,
41
+ ) ?[*]u8 {
42
+ const self: *BufferFirstAllocator = @ptrCast(@alignCast(ctx));
43
+ return FixedBufferAllocator.alloc(&self.fixed_buffer_allocator, len, alignment, ra) orelse
44
+ return self.fallback_allocator.rawAlloc(len, alignment, ra);
45
+ }
46
+
47
+ fn resize(
48
+ ctx: *anyopaque,
49
+ buf: []u8,
50
+ alignment: Alignment,
51
+ new_len: usize,
52
+ ra: usize,
53
+ ) bool {
54
+ const self: *BufferFirstAllocator = @ptrCast(@alignCast(ctx));
55
+ if (self.fixed_buffer_allocator.ownsPtr(buf.ptr)) {
56
+ return FixedBufferAllocator.resize(&self.fixed_buffer_allocator, buf, alignment, new_len, ra);
57
+ } else {
58
+ return self.fallback_allocator.rawResize(buf, alignment, new_len, ra);
59
+ }
60
+ }
61
+
62
+ fn remap(
63
+ context: *anyopaque,
64
+ memory: []u8,
65
+ alignment: Alignment,
66
+ new_len: usize,
67
+ return_address: usize,
68
+ ) ?[*]u8 {
69
+ const self: *BufferFirstAllocator = @ptrCast(@alignCast(context));
70
+ if (self.fixed_buffer_allocator.ownsPtr(memory.ptr)) {
71
+ return FixedBufferAllocator.remap(&self.fixed_buffer_allocator, memory, alignment, new_len, return_address);
72
+ } else {
73
+ return self.fallback_allocator.rawRemap(memory, alignment, new_len, return_address);
74
+ }
75
+ }
76
+
77
+ fn free(
78
+ ctx: *anyopaque,
79
+ buf: []u8,
80
+ alignment: Alignment,
81
+ ra: usize,
82
+ ) void {
83
+ const self: *BufferFirstAllocator = @ptrCast(@alignCast(ctx));
84
+ if (self.fixed_buffer_allocator.ownsPtr(buf.ptr)) {
85
+ return FixedBufferAllocator.free(&self.fixed_buffer_allocator, buf, alignment, ra);
86
+ } else {
87
+ return self.fallback_allocator.rawFree(buf, alignment, ra);
88
+ }
89
+ }
90
+
91
+ test "BufferFirstAllocator" {
92
+ // Buffer first specific tests
93
+ {
94
+ var buffer: [10]u8 = undefined;
95
+ var bfa_state: BufferFirstAllocator = .init(&buffer, std.testing.allocator);
96
+ const bfa = bfa_state.allocator();
97
+
98
+ // We're under the limit, so we should be allocated in the buffer
99
+ const txt0 = "hellowrld";
100
+ const buf0 = try bfa.create(@TypeOf(txt0.*));
101
+ buf0.* = txt0.*;
102
+ try testing.expect(bfa_state.fixed_buffer_allocator.ownsPtr(buf0.ptr));
103
+
104
+ // We're now over the limit, so we should be allocated from the fallback
105
+ const txt1 = "test!";
106
+ const buf1 = try bfa.create(@TypeOf(txt1.*));
107
+ buf1.* = txt1.*;
108
+ try testing.expect(!bfa_state.fixed_buffer_allocator.ownsPtr(buf1.ptr));
109
+
110
+ // Free the allocation that took up space in the buffer
111
+ try testing.expectEqualStrings(txt0, buf0);
112
+ bfa.destroy(buf0);
113
+
114
+ // The next allocation would go in the buffer, but it's too big so it doesn't
115
+ const txt2 = "qwertyqwerty";
116
+ const buf2 = try bfa.create(@TypeOf(txt2.*));
117
+ buf2.* = txt2.*;
118
+ try testing.expect(!bfa_state.fixed_buffer_allocator.ownsPtr(buf2.ptr));
119
+
120
+ // The next allocation is smaller and fits in the buffer
121
+ const txt3 = "dvorak";
122
+ const buf3 = try bfa.create(@TypeOf(txt3.*));
123
+ buf3.* = txt3.*;
124
+ try testing.expect(bfa_state.fixed_buffer_allocator.ownsPtr(buf3.ptr));
125
+
126
+ // The remainder in the buffer is too small for the following allocation so it falls back
127
+ const txt4 = "moretext";
128
+ const buf4 = try bfa.create(@TypeOf(txt4.*));
129
+ buf4.* = txt4.*;
130
+ try testing.expect(!bfa_state.fixed_buffer_allocator.ownsPtr(buf4.ptr));
131
+
132
+ // Check equality on the remaining buffers and free them
133
+ try testing.expectEqualStrings(txt1, buf1);
134
+ bfa.destroy(buf1);
135
+ try testing.expectEqualStrings(txt2, buf2);
136
+ bfa.destroy(buf2);
137
+ try testing.expectEqualStrings(txt3, buf3);
138
+ bfa.destroy(buf3);
139
+ try testing.expectEqualStrings(txt4, buf4);
140
+ bfa.destroy(buf4);
141
+
142
+ try testing.expectEqual(0, bfa_state.fixed_buffer_allocator.end_index);
143
+ }
144
+
145
+ // Standard allocator tests
146
+ {
147
+ var buf: [4096]u8 = undefined;
148
+ {
149
+ var bfa: BufferFirstAllocator = .init(&buf, std.testing.allocator);
150
+ try heap.testAllocator(bfa.allocator());
151
+ }
152
+ {
153
+ var bfa: BufferFirstAllocator = .init(&buf, std.testing.allocator);
154
+ try heap.testAllocatorAligned(bfa.allocator());
155
+ }
156
+ {
157
+ var bfa: BufferFirstAllocator = .init(&buf, std.testing.allocator);
158
+ try heap.testAllocatorLargeAlignment(bfa.allocator());
159
+ }
160
+ {
161
+ var bfa: BufferFirstAllocator = .init(&buf, std.testing.allocator);
162
+ try heap.testAllocatorAlignedShrink(bfa.allocator());
163
+ }
164
+ }
165
+ }
package/std/heap.zig CHANGED
@@ -12,6 +12,7 @@ const Alignment = std.mem.Alignment;
12
12
  pub const ArenaAllocator = @import("heap/ArenaAllocator.zig");
13
13
  pub const SmpAllocator = @import("heap/SmpAllocator.zig");
14
14
  pub const FixedBufferAllocator = @import("heap/FixedBufferAllocator.zig");
15
+ pub const BufferFirstAllocator = @import("heap/BufferFirstAllocator.zig");
15
16
  pub const PageAllocator = @import("heap/PageAllocator.zig");
16
17
  pub const WasmAllocator = if (builtin.single_threaded) BrkAllocator else @compileError("unimplemented");
17
18
  pub const BrkAllocator = @import("heap/BrkAllocator.zig");
@@ -367,113 +368,6 @@ pub const brk_allocator: Allocator = .{
367
368
  .vtable = &BrkAllocator.vtable,
368
369
  };
369
370
 
370
- /// Returns a `StackFallbackAllocator` allocating using either a
371
- /// `FixedBufferAllocator` on an array of size `size` and falling back to
372
- /// `fallback_allocator` if that fails.
373
- pub fn stackFallback(comptime size: usize, fallback_allocator: Allocator) StackFallbackAllocator(size) {
374
- return StackFallbackAllocator(size){
375
- .buffer = undefined,
376
- .fallback_allocator = fallback_allocator,
377
- .fixed_buffer_allocator = undefined,
378
- };
379
- }
380
-
381
- /// An allocator that attempts to allocate using a
382
- /// `FixedBufferAllocator` using an array of size `size`. If the
383
- /// allocation fails, it will fall back to using
384
- /// `fallback_allocator`. Easily created with `stackFallback`.
385
- pub fn StackFallbackAllocator(comptime size: usize) type {
386
- return struct {
387
- const Self = @This();
388
-
389
- buffer: [size]u8,
390
- fallback_allocator: Allocator,
391
- fixed_buffer_allocator: FixedBufferAllocator,
392
- get_called: if (std.debug.runtime_safety) bool else void =
393
- if (std.debug.runtime_safety) false else {},
394
-
395
- /// This function both fetches a `Allocator` interface to this
396
- /// allocator *and* resets the internal buffer allocator.
397
- pub fn get(self: *Self) Allocator {
398
- if (std.debug.runtime_safety) {
399
- assert(!self.get_called); // `get` called multiple times; instead use `const allocator = stackFallback(N).get();`
400
- self.get_called = true;
401
- }
402
- self.fixed_buffer_allocator = FixedBufferAllocator.init(self.buffer[0..]);
403
- return .{
404
- .ptr = self,
405
- .vtable = &.{
406
- .alloc = alloc,
407
- .resize = resize,
408
- .remap = remap,
409
- .free = free,
410
- },
411
- };
412
- }
413
-
414
- /// Unlike most std allocators `StackFallbackAllocator` modifies
415
- /// its internal state before returning an implementation of
416
- /// the`Allocator` interface and therefore also doesn't use
417
- /// the usual `.allocator()` method.
418
- pub const allocator = @compileError("use 'const allocator = stackFallback(N).get();' instead");
419
-
420
- fn alloc(
421
- ctx: *anyopaque,
422
- len: usize,
423
- alignment: Alignment,
424
- ra: usize,
425
- ) ?[*]u8 {
426
- const self: *Self = @ptrCast(@alignCast(ctx));
427
- return FixedBufferAllocator.alloc(&self.fixed_buffer_allocator, len, alignment, ra) orelse
428
- return self.fallback_allocator.rawAlloc(len, alignment, ra);
429
- }
430
-
431
- fn resize(
432
- ctx: *anyopaque,
433
- buf: []u8,
434
- alignment: Alignment,
435
- new_len: usize,
436
- ra: usize,
437
- ) bool {
438
- const self: *Self = @ptrCast(@alignCast(ctx));
439
- if (self.fixed_buffer_allocator.ownsPtr(buf.ptr)) {
440
- return FixedBufferAllocator.resize(&self.fixed_buffer_allocator, buf, alignment, new_len, ra);
441
- } else {
442
- return self.fallback_allocator.rawResize(buf, alignment, new_len, ra);
443
- }
444
- }
445
-
446
- fn remap(
447
- context: *anyopaque,
448
- memory: []u8,
449
- alignment: Alignment,
450
- new_len: usize,
451
- return_address: usize,
452
- ) ?[*]u8 {
453
- const self: *Self = @ptrCast(@alignCast(context));
454
- if (self.fixed_buffer_allocator.ownsPtr(memory.ptr)) {
455
- return FixedBufferAllocator.remap(&self.fixed_buffer_allocator, memory, alignment, new_len, return_address);
456
- } else {
457
- return self.fallback_allocator.rawRemap(memory, alignment, new_len, return_address);
458
- }
459
- }
460
-
461
- fn free(
462
- ctx: *anyopaque,
463
- buf: []u8,
464
- alignment: Alignment,
465
- ra: usize,
466
- ) void {
467
- const self: *Self = @ptrCast(@alignCast(ctx));
468
- if (self.fixed_buffer_allocator.ownsPtr(buf.ptr)) {
469
- return FixedBufferAllocator.free(&self.fixed_buffer_allocator, buf, alignment, ra);
470
- } else {
471
- return self.fallback_allocator.rawFree(buf, alignment, ra);
472
- }
473
- }
474
- };
475
- }
476
-
477
371
  test c_allocator {
478
372
  if (builtin.link_libc) {
479
373
  try testAllocator(c_allocator);
@@ -524,25 +418,6 @@ test ArenaAllocator {
524
418
  try testAllocatorAlignedShrink(allocator);
525
419
  }
526
420
 
527
- test "StackFallbackAllocator" {
528
- {
529
- var stack_allocator = stackFallback(4096, std.testing.allocator);
530
- try testAllocator(stack_allocator.get());
531
- }
532
- {
533
- var stack_allocator = stackFallback(4096, std.testing.allocator);
534
- try testAllocatorAligned(stack_allocator.get());
535
- }
536
- {
537
- var stack_allocator = stackFallback(4096, std.testing.allocator);
538
- try testAllocatorLargeAlignment(stack_allocator.get());
539
- }
540
- {
541
- var stack_allocator = stackFallback(4096, std.testing.allocator);
542
- try testAllocatorAlignedShrink(stack_allocator.get());
543
- }
544
- }
545
-
546
421
  /// This one should not try alignments that exceed what C malloc can handle.
547
422
  pub fn testAllocator(base_allocator: mem.Allocator) !void {
548
423
  var validationAllocator = mem.validationWrap(base_allocator);
@@ -1011,6 +886,7 @@ test {
1011
886
  _ = ArenaAllocator;
1012
887
  _ = DebugAllocator(.{});
1013
888
  _ = FixedBufferAllocator;
889
+ _ = BufferFirstAllocator;
1014
890
  if (builtin.single_threaded) {
1015
891
  if (builtin.cpu.arch.isWasm() or (builtin.os.tag == .linux and !builtin.link_libc)) {
1016
892
  _ = brk_allocator;
@@ -4,8 +4,6 @@
4
4
  //!
5
5
  //! TLS support may be disabled via `std.options.http_disable_tls`.
6
6
  //!
7
- //! TODO all the lockUncancelable in this file should be changed to regular lock and
8
- //! `error.Canceled` added to more error sets.
9
7
  const Client = @This();
10
8
 
11
9
  const builtin = @import("builtin");
@@ -84,8 +82,8 @@ pub const ConnectionPool = struct {
84
82
  /// If no connection is found, null is returned.
85
83
  ///
86
84
  /// Threadsafe.
87
- pub fn findConnection(pool: *ConnectionPool, io: Io, criteria: Criteria) ?*Connection {
88
- pool.mutex.lockUncancelable(io);
85
+ pub fn findConnection(pool: *ConnectionPool, io: Io, criteria: Criteria) Io.Cancelable!?*Connection {
86
+ try pool.mutex.lock(io);
89
87
  defer pool.mutex.unlock(io);
90
88
 
91
89
  var next = pool.free.last;
@@ -113,8 +111,8 @@ pub const ConnectionPool = struct {
113
111
  }
114
112
 
115
113
  /// Acquires an existing connection from the connection pool. This function is threadsafe.
116
- pub fn acquire(pool: *ConnectionPool, io: Io, connection: *Connection) void {
117
- pool.mutex.lockUncancelable(io);
114
+ pub fn acquire(pool: *ConnectionPool, io: Io, connection: *Connection) Io.Cancelable!void {
115
+ try pool.mutex.lock(io);
118
116
  defer pool.mutex.unlock(io);
119
117
 
120
118
  return pool.acquireUnsafe(connection);
@@ -150,8 +148,8 @@ pub const ConnectionPool = struct {
150
148
  }
151
149
 
152
150
  /// Adds a newly created node to the pool of used connections. This function is threadsafe.
153
- pub fn addUsed(pool: *ConnectionPool, io: Io, connection: *Connection) void {
154
- pool.mutex.lockUncancelable(io);
151
+ pub fn addUsed(pool: *ConnectionPool, io: Io, connection: *Connection) Io.Cancelable!void {
152
+ try pool.mutex.lock(io);
155
153
  defer pool.mutex.unlock(io);
156
154
 
157
155
  pool.used.append(&connection.pool_node);
@@ -162,18 +160,15 @@ pub const ConnectionPool = struct {
162
160
  /// If the new size is smaller than the current size, then idle connections will be closed until the pool is the new size.
163
161
  ///
164
162
  /// Threadsafe.
165
- pub fn resize(pool: *ConnectionPool, io: Io, allocator: Allocator, new_size: usize) void {
166
- pool.mutex.lockUncancelable(io);
163
+ pub fn resize(pool: *ConnectionPool, io: Io, new_size: usize) Io.Cancelable!void {
164
+ try pool.mutex.lock(io);
167
165
  defer pool.mutex.unlock(io);
168
166
 
169
- const next = pool.free.first;
170
- _ = next;
171
167
  while (pool.free_len > new_size) {
172
- const popped = pool.free.popFirst() orelse unreachable;
168
+ const popped: *Connection = @alignCast(@fieldParentPtr("pool_node", pool.free.popFirst().?));
173
169
  pool.free_len -= 1;
174
170
 
175
- popped.data.close(allocator);
176
- allocator.destroy(popped);
171
+ popped.destroy(io);
177
172
  }
178
173
 
179
174
  pool.free_size = new_size;
@@ -1323,7 +1318,7 @@ pub fn initDefaultProxies(client: *Client, arena: Allocator, environ_map: *const
1323
1318
  const io = client.io;
1324
1319
 
1325
1320
  // Prevent any new connections from being created.
1326
- client.connection_pool.mutex.lockUncancelable(io);
1321
+ try client.connection_pool.mutex.lock(io);
1327
1322
  defer client.connection_pool.mutex.unlock(io);
1328
1323
 
1329
1324
  assert(client.connection_pool.used.first == null); // There are active requests.
@@ -1418,7 +1413,7 @@ pub const basic_authorization = struct {
1418
1413
 
1419
1414
  pub const ConnectTcpError = error{
1420
1415
  TlsInitializationFailed,
1421
- } || Allocator.Error || HostName.ConnectError;
1416
+ } || Allocator.Error || HostName.ConnectError || Io.Cancelable;
1422
1417
 
1423
1418
  /// Reuses a `Connection` if one matching `host` and `port` is already open.
1424
1419
  ///
@@ -1451,7 +1446,7 @@ pub fn connectTcpOptions(client: *Client, options: ConnectTcpOptions) ConnectTcp
1451
1446
  const proxied_host = options.proxied_host orelse host;
1452
1447
  const proxied_port = options.proxied_port orelse port;
1453
1448
 
1454
- if (client.connection_pool.findConnection(io, .{
1449
+ if (try client.connection_pool.findConnection(io, .{
1455
1450
  .host = proxied_host,
1456
1451
  .port = proxied_port,
1457
1452
  .protocol = protocol,
@@ -1469,18 +1464,20 @@ pub fn connectTcpOptions(client: *Client, options: ConnectTcpOptions) ConnectTcp
1469
1464
  error.Canceled => |e| return e,
1470
1465
  else => return error.TlsInitializationFailed,
1471
1466
  };
1472
- client.connection_pool.addUsed(io, &tc.connection);
1467
+ errdefer tc.destroy();
1468
+ try client.connection_pool.addUsed(io, &tc.connection);
1473
1469
  return &tc.connection;
1474
1470
  },
1475
1471
  .plain => {
1476
1472
  const pc = try Connection.Plain.create(client, proxied_host, proxied_port, stream);
1477
- client.connection_pool.addUsed(io, &pc.connection);
1473
+ errdefer pc.destroy();
1474
+ try client.connection_pool.addUsed(io, &pc.connection);
1478
1475
  return &pc.connection;
1479
1476
  },
1480
1477
  }
1481
1478
  }
1482
1479
 
1483
- pub const ConnectUnixError = Allocator.Error || std.posix.SocketError || error{NameTooLong} || std.posix.ConnectError;
1480
+ pub const ConnectUnixError = Allocator.Error || std.posix.SocketError || error{NameTooLong} || std.posix.ConnectError || Io.Cancelable;
1484
1481
 
1485
1482
  /// Connect to `path` as a unix domain socket. This will reuse a connection if one is already open.
1486
1483
  ///
@@ -1488,7 +1485,7 @@ pub const ConnectUnixError = Allocator.Error || std.posix.SocketError || error{N
1488
1485
  pub fn connectUnix(client: *Client, path: []const u8) ConnectUnixError!*Connection {
1489
1486
  const io = client.io;
1490
1487
 
1491
- if (client.connection_pool.findConnection(io, .{
1488
+ if (try client.connection_pool.findConnection(io, .{
1492
1489
  .host = path,
1493
1490
  .port = 0,
1494
1491
  .protocol = .plain,
@@ -1512,7 +1509,7 @@ pub fn connectUnix(client: *Client, path: []const u8) ConnectUnixError!*Connecti
1512
1509
  };
1513
1510
  errdefer client.allocator.free(conn.data.host);
1514
1511
 
1515
- client.connection_pool.addUsed(conn);
1512
+ try client.connection_pool.addUsed(conn);
1516
1513
 
1517
1514
  return &conn.data;
1518
1515
  }
@@ -1530,7 +1527,7 @@ pub fn connectProxied(
1530
1527
  const io = client.io;
1531
1528
  if (!proxy.supports_connect) return error.TunnelNotSupported;
1532
1529
 
1533
- if (client.connection_pool.findConnection(io, .{
1530
+ if (try client.connection_pool.findConnection(io, .{
1534
1531
  .host = proxied_host,
1535
1532
  .port = proxied_port,
1536
1533
  .protocol = proxy.protocol,
package/std/http.zig CHANGED
@@ -400,7 +400,7 @@ pub const Reader = struct {
400
400
  0 => return error.HttpConnectionClosing,
401
401
  else => return error.HttpRequestTruncated,
402
402
  },
403
- error.ReadFailed => return error.ReadFailed,
403
+ error.ReadFailed => |e| return e,
404
404
  };
405
405
  continue;
406
406
  }
@@ -543,8 +543,7 @@ pub const Reader = struct {
543
543
  else => unreachable,
544
544
  };
545
545
  return chunkedReadEndless(reader, w, limit, chunk_len_ptr) catch |err| switch (err) {
546
- error.ReadFailed => return error.ReadFailed,
547
- error.WriteFailed => return error.WriteFailed,
546
+ error.ReadFailed, error.WriteFailed => |e| return e,
548
547
  error.EndOfStream => {
549
548
  reader.body_err = error.HttpChunkTruncated;
550
549
  return error.ReadFailed;
@@ -613,7 +612,7 @@ pub const Reader = struct {
613
612
  else => unreachable,
614
613
  };
615
614
  return chunkedDiscardEndless(reader, limit, chunk_len_ptr) catch |err| switch (err) {
616
- error.ReadFailed => return error.ReadFailed,
615
+ error.ReadFailed => |e| return e,
617
616
  error.EndOfStream => {
618
617
  reader.body_err = error.HttpChunkTruncated;
619
618
  return error.ReadFailed;
@@ -1401,7 +1401,7 @@ pub fn validate(allocator: Allocator, s: []const u8) Allocator.Error!bool {
1401
1401
  while (true) {
1402
1402
  const token = scanner.next() catch |err| switch (err) {
1403
1403
  error.SyntaxError, error.UnexpectedEndOfInput => return false,
1404
- error.OutOfMemory => return error.OutOfMemory,
1404
+ error.OutOfMemory => |e| return e,
1405
1405
  error.BufferUnderrun => unreachable,
1406
1406
  };
1407
1407
  if (token == .end_of_document) break;
@@ -1734,7 +1734,7 @@ pub const Reader = struct {
1734
1734
 
1735
1735
  fn refillBuffer(self: *@This()) std.Io.Reader.Error!void {
1736
1736
  const input = self.reader.peekGreedy(1) catch |err| switch (err) {
1737
- error.ReadFailed => return error.ReadFailed,
1737
+ error.ReadFailed => |e| return e,
1738
1738
  error.EndOfStream => return self.scanner.endInput(),
1739
1739
  };
1740
1740
  self.reader.toss(input.len);
@@ -211,7 +211,7 @@ pub const W = struct {
211
211
  pub fn TERMSIG(s: u32) SIG {
212
212
  return @enumFromInt(s & 0x7f);
213
213
  }
214
- pub fn STOPSIG(s: u32) u32 {
214
+ pub fn STOPSIG(s: u32) SIG {
215
215
  return @enumFromInt(EXITSTATUS(s));
216
216
  }
217
217
  pub fn IFEXITED(s: u32) bool {
@@ -1856,6 +1856,7 @@ pub fn buf_ring_advance(br: *linux.io_uring_buf_ring, count: u16) void {
1856
1856
  }
1857
1857
 
1858
1858
  test BufferGroup {
1859
+ if (builtin.target.cpu.arch.isPowerPC()) return; // https://codeberg.org/ziglang/zig/issues/31562
1859
1860
  if (!is_linux) return error.SkipZigTest;
1860
1861
 
1861
1862
  const io = testing.io;
@@ -1924,5 +1925,6 @@ test BufferGroup {
1924
1925
  }
1925
1926
 
1926
1927
  test {
1928
+ if (builtin.target.cpu.arch.isPowerPC()) return; // https://codeberg.org/ziglang/zig/issues/31562
1927
1929
  if (is_linux) _ = @import("IoUring/test.zig");
1928
1930
  }