Skip to content

Commit

Permalink
Simplify File#finalize
Browse files Browse the repository at this point in the history
Memory should never be allocated in a finalize method so can't have the
risk of raising something there
  • Loading branch information
carlhoerberg committed Sep 8, 2023
1 parent e268f2a commit a3edb44
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/lavinmq/mfile.cr
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ class MFile < IO
return if @closed
@closed = true
begin
munmap
unmap

return if @readonly || @deleted || !truncate_to_size
code = LibC.ftruncate(@fd, @size)
raise File::Error.from_errno("Error truncating file", file: @path) if code < 0
ensure
code = LibC.close(@fd)
raise File::Error.from_errno("Error closing file", file: @path) if code < 0
@fd = -1
end
end

Expand Down Expand Up @@ -147,7 +148,7 @@ class MFile < IO
# Won't mmap the file if it's unmapped already
def copy_to(output : IO, size = @size) : Int64
if unmapped? # don't remap unmapped files
io = IO::FileDescriptor.new(@fd, blocking: false, close_on_finalize: false)
io = IO::FileDescriptor.new(@fd, blocking: true, close_on_finalize: false)
io.rewind
IO.copy(io, output, size) == size || raise IO::EOFError.new
else
Expand Down Expand Up @@ -186,7 +187,8 @@ class MFile < IO
end

def finalize
close(truncate_to_size: false)
LibC.close(@fd) if @fd > -1
LibC.munmap(@buffer, @capacity) unless @buffer.null?
end

def write(slice : Bytes) : Nil
Expand Down

0 comments on commit a3edb44

Please sign in to comment.