Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Function

setCapacity

Public Function
std.multi_array_list.MultiArrayList.setCapacity
pub fn setCapacity(self: *Self, gpa: Allocator, new_capacity: usize) Allocator.Error!void

Modify the array so that it can hold exactly new_capacity items. Invalidates pointers if additional memory is needed. new_capacity must be greater or equal to len.

Params

File

multi_array_list.zig:543
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
        pub fn setCapacity(self: *Self, gpa: Allocator, new_capacity: usize) Allocator.Error!void {
            assert(new_capacity >= self.len);
            const new_bytes = try gpa.alignedAlloc(u8, sizes.big_align, capacityInBytes(new_capacity));
            if (self.len == 0) {
                gpa.free(self.allocatedBytes());
                self.bytes = new_bytes.ptr;
                self.capacity = new_capacity;
                return;
            }
            var other = Self{
                .bytes = new_bytes.ptr,
                .capacity = new_capacity,
                .len = self.len,
            };
            const self_slice = self.slice();
            const other_slice = other.slice();
            inline for (field_types, 0..) |field_type, i| {
                if (@sizeOf(field_type) != 0) {
                    const field = @as(Field, @fromBackingInt(@intCast(i)));
                    @memcpy(other_slice.items(field), self_slice.items(field));
                }
            }
            gpa.free(self.allocatedBytes());
            self.* = other;
        }