8000 Correctly release the underlying file mapping. · ruby/ruby@02543d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 02543d1

Browse files
committed
Correctly release the underlying file mapping.
1 parent 37753f1 commit 02543d1

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

include/ruby/io/buffer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ enum rb_io_buffer_flags {
5858

5959
// The buffer is read-only and cannot be modified.
6060
RB_IO_BUFFER_READONLY = 128,
61+
62+
// The buffer is backed by a file.
63+
RB_IO_BUFFER_FILE = 256,
6164
};
6265

6366
// Represents the endian of the data types.

io_buffer.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,7 @@ io_buffer_map_file(struct rb_io_buffer *buffer, int descriptor, size_t size, rb_
155155
buffer->size = size;
156156

157157
buffer->flags |= RB_IO_BUFFER_MAPPED;
158-
}
159-
160-
// Release the memory associated with a mapped buffer.
161-
static inline void
162-
io_buffer_unmap(void* base, size_t size)
163-
{
164-
#ifdef _WIN32
165-
VirtualFree(base, 0, MEM_RELEASE);
166-
#else
167-
munmap(base, size);
168-
#endif
158+
buffer->flags |= RB_IO_BUFFER_FILE;
169159
}
170160

171161
static void
@@ -234,7 +224,16 @@ io_buffer_free(struct rb_io_buffer *buffer)
234224
}
235225

236226
if (buffer->flags & RB_IO_BUFFER_MAPPED) {
237-
io_buffer_unmap(buffer->base, buffer->size);
227+
#ifdef _WIN32
228+
if (buffer->flags & RB_IO_BUFFER_FILE) {
229+
UnmapViewOfFile(buffer->base);
230+
}
231+
else {
232+
VirtualFree(base, 0, MEM_RELEASE);
233+
}
234+
#else
235+
munmap(base, size);
236+
#endif
238237
}
239238

240239
// Previously we had this, but we found out due to the way GC works, we
@@ -245,19 +244,20 @@ io_buffer_free(struct rb_io_buffer *buffer)
245244

246245
buffer->base = NULL;
247246

248-
#if defined(_WIN32)
249-
if (buffer->mapping) {
250-
CloseHandle(buffer->mapping);
251-
buffer->mapping = NULL;
252-
}
253-
#endif
254247
buffer->size = 0;
255248
buffer->flags = 0;
256249
buffer->source = Qnil;
257250

258251
return 1;
259252
}
260253

254+
#if defined(_WIN32)
255+
if (buffer->mapping) {
256+
CloseHandle(buffer->mapping);
257+
buffer->mapping = NULL;
258+
}
259+
#endif
260+
261261
return 0;
262262
}
263263

@@ -926,6 +926,10 @@ rb_io_buffer_to_s(VALUE self)
926926
rb_str_cat2(result, " MAPPED");
927927
}
928928

929+
if (buffer->flags & RB_IO_BUFFER_FILE) {
930+
rb_str_cat2(result, " FILE");
931+
}
932+
929933
if (buffer->flags & RB_IO_BUFFER_SHARED) {
930934
rb_str_cat2(result, " SHARED");
931935
}

test/ruby/test_io_buffer.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,6 @@ def test_shared
519519
end
520520

521521
def test_private
522-
omit if RUBY_PLATFORM =~ /mswin|mingw/
523-
524522
Tempfile.create("buffer.txt") do |io|
525523
io.write("Hello World")
526524

0 commit comments

Comments
 (0)
0