From 59204b4813bfbf50189b256a29cdfae85a77bca1 Mon Sep 17 00:00:00 2001 From: "djsollen@google.com" Date: Fri, 9 Dec 2011 21:19:38 +0000 Subject: [PATCH] Closing fd for mmap after mapping succeeds. Review URL: http://codereview.appspot.com/5467058 git-svn-id: http://skia.googlecode.com/svn/trunk@2847 2bbb7eff-a529-9590-31e7-b0007b416f81 --- include/core/SkMMapStream.h | 1 - src/core/SkMMapStream.cpp | 16 ++++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/include/core/SkMMapStream.h b/include/core/SkMMapStream.h index 0836791..19ba634 100644 --- a/include/core/SkMMapStream.h +++ b/include/core/SkMMapStream.h @@ -19,7 +19,6 @@ public: virtual void setMemory(const void* data, size_t length, bool); private: - int fFildes; void* fAddr; size_t fSize; diff --git a/src/core/SkMMapStream.cpp b/src/core/SkMMapStream.cpp index 005c8e5..0aec5e1 100644 --- a/src/core/SkMMapStream.cpp +++ b/src/core/SkMMapStream.cpp @@ -14,7 +14,7 @@ SkMMAPStream::SkMMAPStream(const char filename[]) { - fFildes = -1; // initialize to failure case + fAddr = NULL; // initialize to failure case int fildes = open(filename, O_RDONLY); if (fildes < 0) @@ -36,16 +36,21 @@ SkMMAPStream::SkMMAPStream(const char filename[]) size_t size = static_cast(offset); void* addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fildes, 0); + + // According to the POSIX documentation of mmap it adds an extra reference + // to the file associated with the fildes which is not removed by a + // subsequent close() on that fildes. This reference is removed when there + // are no more mappings to the file. + close(fildes); + if (MAP_FAILED == addr) { SkDEBUGF(("---- failed to mmap(%s) for mmap stream error=%d\n", filename, errno)); - close(fildes); return; } this->INHERITED::setMemory(addr, size); - fFildes = fildes; fAddr = addr; fSize = size; } @@ -63,11 +68,10 @@ void SkMMAPStream::setMemory(const void* data, size_t length, bool copyData) void SkMMAPStream::closeMMap() { - if (fFildes >= 0) + if (fAddr) { munmap(fAddr, fSize); - close(fFildes); - fFildes = -1; + fAddr = NULL; } } -- 2.7.4