authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-02-01 14:44:44+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-02-01 20:30:42+00:00
log8944935499ee6cdbce6e0384efb32e75753adcbd
tree11394e6151a7ba58156d2f69e30f9aa107303b62
parent776cd673f206099012d789fd5d05d49dd72b9faa

std: eliminate some uses of `usingnamespace`

This eliminates some simple usages of `usingnamespace` in the standard library. This construct may in future be removed from the language, and is generally an inappropriate way to formulate code. It is also problematic for incremental compilation, which may not initially support projects using it. I wasn't entirely sure what the appropriate namespacing for the types in `std.os.uefi.tables` would be, so I ofted to preserve the current namespacing, meaning this is not a breaking change. It's possible some of the moved types should instead be namespaced under `BootServices` etc, but this can be a future enhancement.

7 files changed, 226 insertions(+), 210 deletions(-)

lib/std/fifo.zig+34-31
......@@ -46,39 +46,42 @@ pub fn LinearFifo(
4646 // returned a slice into a copy on the stack
4747 const SliceSelfArg = if (buffer_type == .Static) *Self else Self;
4848
49 pub usingnamespace switch (buffer_type) {
50 .Static => struct {
51 pub fn init() Self {
52 return .{
53 .allocator = {},
54 .buf = undefined,
55 .head = 0,
56 .count = 0,
57 };
58 }
59 },
60 .Slice => struct {
61 pub fn init(buf: []T) Self {
62 return .{
63 .allocator = {},
64 .buf = buf,
65 .head = 0,
66 .count = 0,
67 };
68 }
69 },
70 .Dynamic => struct {
71 pub fn init(allocator: Allocator) Self {
72 return .{
73 .allocator = allocator,
74 .buf = &[_]T{},
75 .head = 0,
76 .count = 0,
77 };
78 }
79 },
49 pub const init = switch (buffer_type) {
50 .Static => initStatic,
51 .Slice => initSlice,
52 .Dynamic => initDynamic,
8053 };
8154
55 fn initStatic() Self {
56 comptime assert(buffer_type == .Static);
57 return .{
58 .allocator = {},
59 .buf = undefined,
60 .head = 0,
61 .count = 0,
62 };
63 }
64
65 fn initSlice(buf: []T) Self {
66 comptime assert(buffer_type == .Slice);
67 return .{
68 .allocator = {},
69 .buf = buf,
70 .head = 0,
71 .count = 0,
72 };
73 }
74
75 fn initDynamic(allocator: Allocator) Self {
76 comptime assert(buffer_type == .Dynamic);
77 return .{
78 .allocator = allocator,
79 .buf = &.{},
80 .head = 0,
81 .count = 0,
82 };
83 }
84
8285 pub fn deinit(self: Self) void {
8386 if (buffer_type == .Dynamic) self.allocator.free(self.buf);
8487 }
lib/std/heap.zig+6-17
......@@ -38,25 +38,14 @@ const CAllocator = struct {
3838 }
3939 }
4040
41 usingnamespace if (@hasDecl(c, "malloc_size"))
42 struct {
43 pub const supports_malloc_size = true;
44 pub const malloc_size = c.malloc_size;
45 }
41 pub const supports_malloc_size = @TypeOf(malloc_size) != void;
42 pub const malloc_size = if (@hasDecl(c, "malloc_size"))
43 c.malloc_size
4644 else if (@hasDecl(c, "malloc_usable_size"))
47 struct {
48 pub const supports_malloc_size = true;
49 pub const malloc_size = c.malloc_usable_size;
50 }
45 c.malloc_usable_size
5146 else if (@hasDecl(c, "_msize"))
52 struct {
53 pub const supports_malloc_size = true;
54 pub const malloc_size = c._msize;
55 }
56 else
57 struct {
58 pub const supports_malloc_size = false;
59 };
47 c._msize
48 else {};
6049
6150 pub const supports_posix_memalign = @hasDecl(c, "posix_memalign");
6251
lib/std/heap/general_purpose_allocator.zig+11-10
......@@ -454,18 +454,19 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
454454 }
455455 }
456456
457 pub usingnamespace if (config.retain_metadata) struct {
458 pub fn flushRetainedMetadata(self: *Self) void {
459 self.freeRetainedMetadata();
460 // also remove entries from large_allocations
461 var it = self.large_allocations.iterator();
462 while (it.next()) |large| {
463 if (large.value_ptr.freed) {
464 _ = self.large_allocations.remove(@intFromPtr(large.value_ptr.bytes.ptr));
465 }
457 pub fn flushRetainedMetadata(self: *Self) void {
458 if (!config.retain_metadata) {
459 @compileError("'flushRetainedMetadata' requires 'config.retain_metadata = true'");
460 }
461 self.freeRetainedMetadata();
462 // also remove entries from large_allocations
463 var it = self.large_allocations.iterator();
464 while (it.next()) |large| {
465 if (large.value_ptr.freed) {
466 _ = self.large_allocations.remove(@intFromPtr(large.value_ptr.bytes.ptr));
466467 }
467468 }
468 } else struct {};
469 }
469470
470471 /// Returns `Check.leak` if there were leaks; `Check.ok` otherwise.
471472 pub fn deinit(self: *Self) Check {
lib/std/io/peek_stream.zig+29-25
......@@ -1,4 +1,5 @@
11const std = @import("../std.zig");
2const assert = std.debug.assert;
23const io = std.io;
34const mem = std.mem;
45const testing = std.testing;
......@@ -20,33 +21,36 @@ pub fn PeekStream(
2021 const Self = @This();
2122 const FifoType = std.fifo.LinearFifo(u8, buffer_type);
2223
23 pub usingnamespace switch (buffer_type) {
24 .Static => struct {
25 pub fn init(base: ReaderType) Self {
26 return .{
27 .unbuffered_reader = base,
28 .fifo = FifoType.init(),
29 };
30 }
31 },
32 .Slice => struct {
33 pub fn init(base: ReaderType, buf: []u8) Self {
34 return .{
35 .unbuffered_reader = base,
36 .fifo = FifoType.init(buf),
37 };
38 }
39 },
40 .Dynamic => struct {
41 pub fn init(base: ReaderType, allocator: mem.Allocator) Self {
42 return .{
43 .unbuffered_reader = base,
44 .fifo = FifoType.init(allocator),
45 };
46 }
47 },
24 pub const init = switch (buffer_type) {
25 .Static => initStatic,
26 .Slice => initSlice,
27 .Dynamic => initDynamic,
4828 };
4929
30 fn initStatic(base: ReaderType) Self {
31 comptime assert(buffer_type == .Static);
32 return .{
33 .unbuffered_reader = base,
34 .fifo = FifoType.init(),
35 };
36 }
37
38 fn initSlice(base: ReaderType, buf: []u8) Self {
39 comptime assert(buffer_type == .Slice);
40 return .{
41 .unbuffered_reader = base,
42 .fifo = FifoType.init(buf),
43 };
44 }
45
46 fn initDynamic(base: ReaderType, allocator: mem.Allocator) Self {
47 comptime assert(buffer_type == .Dynamic);
48 return .{
49 .unbuffered_reader = base,
50 .fifo = FifoType.init(allocator),
51 };
52 }
53
5054 pub fn putBackByte(self: *Self, byte: u8) !void {
5155 try self.putBack(&[_]u8{byte});
5256 }
lib/std/os/uefi/tables.zig+134-6
......@@ -1,9 +1,137 @@
1pub usingnamespace @import("tables/boot_services.zig");
2pub usingnamespace @import("tables/runtime_services.zig");
3pub usingnamespace @import("tables/configuration_table.zig");
4pub usingnamespace @import("tables/system_table.zig");
5pub usingnamespace @import("tables/table_header.zig");
1pub const BootServices = @import("tables/boot_services.zig").BootServices;
2pub const RuntimeServices = @import("tables/runtime_services.zig").RuntimeServices;
3pub const ConfigurationTable = @import("tables/configuration_table.zig").ConfigurationTable;
4pub const SystemTable = @import("tables/system_table.zig").SystemTable;
5pub const TableHeader = @import("tables/table_header.zig").TableHeader;
6
7pub const EfiEventNotify = *const fn (event: Event, ctx: *anyopaque) callconv(cc) void;
8
9pub const TimerDelay = enum(u32) {
10 TimerCancel,
11 TimerPeriodic,
12 TimerRelative,
13};
14
15pub const MemoryType = enum(u32) {
16 ReservedMemoryType,
17 LoaderCode,
18 LoaderData,
19 BootServicesCode,
20 BootServicesData,
21 RuntimeServicesCode,
22 RuntimeServicesData,
23 ConventionalMemory,
24 UnusableMemory,
25 ACPIReclaimMemory,
26 ACPIMemoryNVS,
27 MemoryMappedIO,
28 MemoryMappedIOPortSpace,
29 PalCode,
30 PersistentMemory,
31 MaxMemoryType,
32 _,
33};
34
35pub const MemoryDescriptorAttribute = packed struct(u64) {
36 uc: bool,
37 wc: bool,
38 wt: bool,
39 wb: bool,
40 uce: bool,
41 _pad1: u7 = 0,
42 wp: bool,
43 rp: bool,
44 xp: bool,
45 nv: bool,
46 more_reliable: bool,
47 ro: bool,
48 sp: bool,
49 cpu_crypto: bool,
50 _pad2: u43 = 0,
51 memory_runtime: bool,
52};
53
54pub const MemoryDescriptor = extern struct {
55 type: MemoryType,
56 physical_start: u64,
57 virtual_start: u64,
58 number_of_pages: u64,
59 attribute: MemoryDescriptorAttribute,
60};
61
62pub const LocateSearchType = enum(u32) {
63 AllHandles,
64 ByRegisterNotify,
65 ByProtocol,
66};
67
68pub const OpenProtocolAttributes = packed struct(u32) {
69 by_handle_protocol: bool = false,
70 get_protocol: bool = false,
71 test_protocol: bool = false,
72 by_child_controller: bool = false,
73 by_driver: bool = false,
74 exclusive: bool = false,
75 reserved: u26 = 0,
76};
77
78pub const ProtocolInformationEntry = extern struct {
79 agent_handle: ?Handle,
80 controller_handle: ?Handle,
81 attributes: OpenProtocolAttributes,
82 open_count: u32,
83};
84
85pub const EfiInterfaceType = enum(u32) {
86 EfiNativeInterface,
87};
88
89pub const AllocateType = enum(u32) {
90 AllocateAnyPages,
91 AllocateMaxAddress,
92 AllocateAddress,
93};
94
95const EfiPhysicalAddress = u64;
96
97pub const CapsuleHeader = extern struct {
98 capsuleGuid: Guid align(8),
99 headerSize: u32,
100 flags: u32,
101 capsuleImageSize: u32,
102};
103
104pub const UefiCapsuleBlockDescriptor = extern struct {
105 length: u64,
106 address: extern union {
107 dataBlock: EfiPhysicalAddress,
108 continuationPointer: EfiPhysicalAddress,
109 },
110};
111
112pub const ResetType = enum(u32) {
113 ResetCold,
114 ResetWarm,
115 ResetShutdown,
116 ResetPlatformSpecific,
117};
118
119pub const global_variable align(8) = Guid{
120 .time_low = 0x8be4df61,
121 .time_mid = 0x93ca,
122 .time_high_and_version = 0x11d2,
123 .clock_seq_high_and_reserved = 0xaa,
124 .clock_seq_low = 0x0d,
125 .node = [_]u8{ 0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c },
126};
6127
7128test {
8 @import("std").testing.refAllDeclsRecursive(@This());
129 std.testing.refAllDeclsRecursive(@This());
9130}
131
132const std = @import("std");
133const uefi = std.os.uefi;
134const Handle = uefi.Handle;
135const Event = uefi.Event;
136const Guid = uefi.Guid;
137const cc = uefi.cc;
lib/std/os/uefi/tables/boot_services.zig+9-88
......@@ -6,6 +6,15 @@ const Handle = uefi.Handle;
66const Status = uefi.Status;
77const TableHeader = uefi.tables.TableHeader;
88const DevicePathProtocol = uefi.protocol.DevicePath;
9const AllocateType = uefi.tables.AllocateType;
10const MemoryType = uefi.tables.MemoryType;
11const MemoryDescriptor = uefi.tables.MemoryDescriptor;
12const TimerDelay = uefi.tables.TimerDelay;
13const EfiInterfaceType = uefi.tables.EfiInterfaceType;
14const LocateSearchType = uefi.tables.LocateSearchType;
15const OpenProtocolAttributes = uefi.tables.OpenProtocolAttributes;
16const ProtocolInformationEntry = uefi.tables.ProtocolInformationEntry;
17const EfiEventNotify = uefi.tables.EfiEventNotify;
918const cc = uefi.cc;
1019
1120/// Boot services are services provided by the system's firmware until the operating system takes
......@@ -193,91 +202,3 @@ pub const BootServices = extern struct {
193202 pub const tpl_notify: usize = 16;
194203 pub const tpl_high_level: usize = 31;
195204};
196
197pub const EfiEventNotify = *const fn (event: Event, ctx: *anyopaque) callconv(cc) void;
198
199pub const TimerDelay = enum(u32) {
200 TimerCancel,
201 TimerPeriodic,
202 TimerRelative,
203};
204
205pub const MemoryType = enum(u32) {
206 ReservedMemoryType,
207 LoaderCode,
208 LoaderData,
209 BootServicesCode,
210 BootServicesData,
211 RuntimeServicesCode,
212 RuntimeServicesData,
213 ConventionalMemory,
214 UnusableMemory,
215 ACPIReclaimMemory,
216 ACPIMemoryNVS,
217 MemoryMappedIO,
218 MemoryMappedIOPortSpace,
219 PalCode,
220 PersistentMemory,
221 MaxMemoryType,
222 _,
223};
224
225pub const MemoryDescriptorAttribute = packed struct(u64) {
226 uc: bool,
227 wc: bool,
228 wt: bool,
229 wb: bool,
230 uce: bool,
231 _pad1: u7 = 0,
232 wp: bool,
233 rp: bool,
234 xp: bool,
235 nv: bool,
236 more_reliable: bool,
237 ro: bool,
238 sp: bool,
239 cpu_crypto: bool,
240 _pad2: u43 = 0,
241 memory_runtime: bool,
242};
243
244pub const MemoryDescriptor = extern struct {
245 type: MemoryType,
246 physical_start: u64,
247 virtual_start: u64,
248 number_of_pages: u64,
249 attribute: MemoryDescriptorAttribute,
250};
251
252pub const LocateSearchType = enum(u32) {
253 AllHandles,
254 ByRegisterNotify,
255 ByProtocol,
256};
257
258pub const OpenProtocolAttributes = packed struct(u32) {
259 by_handle_protocol: bool = false,
260 get_protocol: bool = false,
261 test_protocol: bool = false,
262 by_child_controller: bool = false,
263 by_driver: bool = false,
264 exclusive: bool = false,
265 reserved: u26 = 0,
266};
267
268pub const ProtocolInformationEntry = extern struct {
269 agent_handle: ?Handle,
270 controller_handle: ?Handle,
271 attributes: OpenProtocolAttributes,
272 open_count: u32,
273};
274
275pub const EfiInterfaceType = enum(u32) {
276 EfiNativeInterface,
277};
278
279pub const AllocateType = enum(u32) {
280 AllocateAnyPages,
281 AllocateMaxAddress,
282 AllocateAddress,
283};
lib/std/os/uefi/tables/runtime_services.zig+3-33
......@@ -6,6 +6,9 @@ const Time = uefi.Time;
66const TimeCapabilities = uefi.TimeCapabilities;
77const Status = uefi.Status;
88const MemoryDescriptor = uefi.tables.MemoryDescriptor;
9const ResetType = uefi.tables.ResetType;
10const CapsuleHeader = uefi.tables.CapsuleHeader;
11const EfiPhysicalAddress = uefi.tables.EfiPhysicalAddress;
912const cc = uefi.cc;
1013
1114/// Runtime services are provided by the firmware before and after exitBootServices has been called.
......@@ -67,36 +70,3 @@ pub const RuntimeServices = extern struct {
6770
6871 pub const signature: u64 = 0x56524553544e5552;
6972};
70
71const EfiPhysicalAddress = u64;
72
73pub const CapsuleHeader = extern struct {
74 capsuleGuid: Guid align(8),
75 headerSize: u32,
76 flags: u32,
77 capsuleImageSize: u32,
78};
79
80pub const UefiCapsuleBlockDescriptor = extern struct {
81 length: u64,
82 address: extern union {
83 dataBlock: EfiPhysicalAddress,
84 continuationPointer: EfiPhysicalAddress,
85 },
86};
87
88pub const ResetType = enum(u32) {
89 ResetCold,
90 ResetWarm,
91 ResetShutdown,
92 ResetPlatformSpecific,
93};
94
95pub const global_variable align(8) = Guid{
96 .time_low = 0x8be4df61,
97 .time_mid = 0x93ca,
98 .time_high_and_version = 0x11d2,
99 .clock_seq_high_and_reserved = 0xaa,
100 .clock_seq_low = 0x0d,
101 .node = [_]u8{ 0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c },
102};