Skip to content

Commit a6246c8

Browse files
committed
allocgate: shrink is not guaranteed to succeed
1 parent e70ef58 commit a6246c8

18 files changed

+145
-84
lines changed

lib/std/array_list.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
9696
/// The caller owns the returned memory. Empties this ArrayList.
9797
pub fn toOwnedSlice(self: *Self) Slice {
9898
const allocator = self.allocator;
99-
const result = allocator.shrink(self.allocatedSlice(), self.items.len);
99+
const result = allocator.realloc(self.allocatedSlice(), self.items.len) catch self.items;
100100
self.* = init(allocator);
101101
return result;
102102
}
@@ -477,7 +477,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
477477

478478
/// The caller owns the returned memory. ArrayList becomes empty.
479479
pub fn toOwnedSlice(self: *Self, allocator: Allocator) Slice {
480-
const result = allocator.shrink(self.allocatedSlice(), self.items.len);
480+
const result = allocator.realloc(self.allocatedSlice(), self.items.len) catch self.items;
481481
self.* = Self{};
482482
return result;
483483
}

lib/std/build/OptionsStep.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,5 +350,5 @@ test "OptionsStep" {
350350
\\
351351
, options.contents.items);
352352

353-
_ = try std.zig.parse(&arena.allocator, try options.contents.toOwnedSliceSentinel(0));
353+
_ = try std.zig.parse(arena.allocator(), try options.contents.toOwnedSliceSentinel(0));
354354
}

lib/std/child_process.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ pub fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const BufMap) !
11141114
i += 1;
11151115
result[i] = 0;
11161116
i += 1;
1117-
return allocator.shrink(result, i);
1117+
return allocator.realloc(result, i);
11181118
}
11191119

11201120
pub fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const std.BufMap) ![:null]?[*:0]u8 {

lib/std/debug.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ fn readMachODebugInfo(allocator: mem.Allocator, macho_file: File) !ModuleDebugIn
936936
}
937937
assert(state == .oso_close);
938938

939-
const symbols = allocator.shrink(symbols_buf, symbol_index);
939+
const symbols = try allocator.realloc(symbols_buf, symbol_index);
940940

941941
// Even though lld emits symbols in ascending order, this debug code
942942
// should work for programs linked in any valid way.

lib/std/fs/path.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {
638638
result_index += 1;
639639
}
640640

641-
return allocator.shrink(result, result_index);
641+
return allocator.realloc(result, result_index);
642642
}
643643

644644
/// This function is like a series of `cd` statements executed one after another.
@@ -707,7 +707,7 @@ pub fn resolvePosix(allocator: Allocator, paths: []const []const u8) ![]u8 {
707707
result_index += 1;
708708
}
709709

710-
return allocator.shrink(result, result_index);
710+
return allocator.realloc(result, result_index);
711711
}
712712

713713
test "resolve" {
@@ -1174,7 +1174,7 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![]
11741174
}
11751175
if (to_rest.len == 0) {
11761176
// shave off the trailing slash
1177-
return allocator.shrink(result, result_index - 1);
1177+
return allocator.realloc(result, result_index - 1);
11781178
}
11791179

11801180
mem.copy(u8, result[result_index..], to_rest);

lib/std/heap.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,11 +1116,11 @@ pub fn testAllocator(base_allocator: mem.Allocator) !void {
11161116
allocator.destroy(item);
11171117
}
11181118

1119-
slice = allocator.shrink(slice, 50);
1119+
slice = allocator.tryShrink(slice, 50) orelse return error.OutOfMemory; // this allocator does not support this shrink
11201120
try testing.expect(slice.len == 50);
1121-
slice = allocator.shrink(slice, 25);
1121+
slice = allocator.tryShrink(slice, 25) orelse return error.OutOfMemory; // this allocator does not support this shrink
11221122
try testing.expect(slice.len == 25);
1123-
slice = allocator.shrink(slice, 0);
1123+
slice = allocator.tryShrink(slice, 0) orelse return error.OutOfMemory; // this allocator does not support this shrink
11241124
try testing.expect(slice.len == 0);
11251125
slice = try allocator.realloc(slice, 10);
11261126
try testing.expect(slice.len == 10);
@@ -1156,19 +1156,19 @@ pub fn testAllocatorAligned(base_allocator: mem.Allocator) !void {
11561156
slice = try allocator.realloc(slice, 100);
11571157
try testing.expect(slice.len == 100);
11581158
// shrink
1159-
slice = allocator.shrink(slice, 10);
1159+
slice = allocator.tryShrink(slice, 10) orelse return error.OutOfMemory; // this allocator does not support this shrink
11601160
try testing.expect(slice.len == 10);
11611161
// go to zero
1162-
slice = allocator.shrink(slice, 0);
1162+
slice = allocator.tryShrink(slice, 0) orelse return error.OutOfMemory; // this allocator does not support this shrink
11631163
try testing.expect(slice.len == 0);
11641164
// realloc from zero
11651165
slice = try allocator.realloc(slice, 100);
11661166
try testing.expect(slice.len == 100);
11671167
// shrink with shrink
1168-
slice = allocator.shrink(slice, 10);
1168+
slice = allocator.tryShrink(slice, 10) orelse return error.OutOfMemory; // this allocator does not support this shrink
11691169
try testing.expect(slice.len == 10);
11701170
// shrink to zero
1171-
slice = allocator.shrink(slice, 0);
1171+
slice = allocator.tryShrink(slice, 0) orelse return error.OutOfMemory; // this allocator does not support this shrink
11721172
try testing.expect(slice.len == 0);
11731173
}
11741174
}
@@ -1190,13 +1190,13 @@ pub fn testAllocatorLargeAlignment(base_allocator: mem.Allocator) !void {
11901190
var slice = try allocator.alignedAlloc(u8, large_align, 500);
11911191
try testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
11921192

1193-
slice = allocator.shrink(slice, 100);
1193+
slice = allocator.tryShrink(slice, 100) orelse return error.OutOfMemory; // this allocator does not support this shrink
11941194
try testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
11951195

11961196
slice = try allocator.realloc(slice, 5000);
11971197
try testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
11981198

1199-
slice = allocator.shrink(slice, 10);
1199+
slice = allocator.tryShrink(slice, 10) orelse return error.OutOfMemory; // this allocator does not support this shrink
12001200
try testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
12011201

12021202
slice = try allocator.realloc(slice, 20000);

lib/std/heap/general_purpose_allocator.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -735,8 +735,8 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
735735
old_align: u29,
736736
ret_addr: usize,
737737
) void {
738-
const held = self.mutex.acquire();
739-
defer held.release();
738+
self.mutex.lock();
739+
defer self.mutex.unlock();
740740

741741
assert(old_mem.len != 0);
742742

@@ -850,7 +850,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
850850
return true;
851851
}
852852

853-
fn alloc(self: Allocator, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Error![]u8 {
853+
fn alloc(self: *Self, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Error![]u8 {
854854
self.mutex.lock();
855855
defer self.mutex.unlock();
856856

@@ -1007,13 +1007,13 @@ test "shrink" {
10071007

10081008
mem.set(u8, slice, 0x11);
10091009

1010-
slice = allocator.shrink(slice, 17);
1010+
slice = allocator.tryShrink(slice, 17) orelse unreachable; // GPA supports shrink
10111011

10121012
for (slice) |b| {
10131013
try std.testing.expect(b == 0x11);
10141014
}
10151015

1016-
slice = allocator.shrink(slice, 16);
1016+
slice = allocator.tryShrink(slice, 16) orelse unreachable; // GPA supports shrink
10171017

10181018
for (slice) |b| {
10191019
try std.testing.expect(b == 0x11);
@@ -1069,7 +1069,7 @@ test "shrink large object to large object" {
10691069
try std.testing.expect(slice[0] == 0x12);
10701070
try std.testing.expect(slice[60] == 0x34);
10711071

1072-
slice = allocator.shrink(slice, page_size * 2 + 1);
1072+
slice = allocator.tryShrink(slice, page_size * 2 + 1) orelse unreachable; // GPA supports shrink
10731073
try std.testing.expect(slice[0] == 0x12);
10741074
try std.testing.expect(slice[60] == 0x34);
10751075

@@ -1202,7 +1202,7 @@ test "large object shrinks to small but allocation fails during shrink" {
12021202

12031203
// Next allocation will fail in the backing allocator of the GeneralPurposeAllocator
12041204

1205-
slice = allocator.shrink(slice, 4);
1205+
slice = allocator.tryShrink(slice, 4) orelse unreachable; // GPA supports shrink
12061206
try std.testing.expect(slice[0] == 0x12);
12071207
try std.testing.expect(slice[3] == 0x34);
12081208
}

lib/std/heap/log_to_writer_allocator.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ test "LogToWriterAllocator" {
9494
const allocator = logToWriterAllocator(fixedBufferAllocator.allocator(), fbs.writer()).allocator();
9595

9696
var a = try allocator.alloc(u8, 10);
97-
a = allocator.shrink(a, 5);
97+
a = allocator.tryShrink(a, 5) orelse return error.OutOfMemory; // this allocator does not support this shrink
9898
try std.testing.expect(a.len == 5);
9999
try std.testing.expect(allocator.resize(a, 20) == null);
100100
allocator.free(a);

lib/std/math/big/int.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,7 @@ pub const Const = struct {
18861886
const limbs = try allocator.alloc(Limb, calcToStringLimbsBufferLen(self.limbs.len, base));
18871887
defer allocator.free(limbs);
18881888

1889-
return allocator.shrink(string, self.toString(string, base, case, limbs));
1889+
return allocator.realloc(string, self.toString(string, base, case, limbs));
18901890
}
18911891

18921892
/// Converts self to a string in the requested base.

0 commit comments

Comments
 (0)