Fix segfault when allocating zero-length memory
This commit is contained in:
parent
1fb5fc6048
commit
de82444ac3
|
@ -143,10 +143,6 @@ pub fn allocate_many(comptime Type: type, amount: usize, allocator: Allocator) A
|
|||
@compileError("Cannot allocate memory for 0-byte type " ++ @typeName(Type));
|
||||
}
|
||||
|
||||
if (amount == 0) {
|
||||
return &.{};
|
||||
}
|
||||
|
||||
return @ptrCast([*]Type, @alignCast(@alignOf(Type), allocator.invoke(.{.size = @sizeOf(Type) * amount}) orelse {
|
||||
return error.OutOfMemory;
|
||||
}))[0 .. amount];
|
||||
|
|
|
@ -7,17 +7,25 @@ const Context = struct {
|
|||
|
||||
const Self = @This();
|
||||
|
||||
const empty_allocation = [0]u8{};
|
||||
|
||||
fn reallocate(self: *Self, options: coral.io.AllocationOptions) ?[]u8 {
|
||||
if (options.size == 0) {
|
||||
if (options.allocation) |allocation| {
|
||||
if (allocation.ptr != &empty_allocation) {
|
||||
ext.SDL_free(allocation.ptr);
|
||||
|
||||
self.live_allocations -= 1;
|
||||
}
|
||||
|
||||
self.live_allocations -= 1;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
self.live_allocations += 1;
|
||||
|
||||
return &empty_allocation;
|
||||
}
|
||||
|
||||
if (options.allocation) |allocation| {
|
||||
if (ext.SDL_realloc(allocation.ptr, options.size)) |reallocation| {
|
||||
self.live_allocations += 1;
|
||||
|
|
Loading…
Reference in New Issue