aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core
diff options
context:
space:
mode:
authorGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-03-18 19:08:46 +0000
committerGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-03-18 19:08:46 +0000
commitd5ea2aeb6082840c598818aba37fcb8e08773341 (patch)
tree1c35825e1964cfb3e70acdbf72759a9ec54b0a59 /src/core
parenta74302d628f48c7c1c3e14742b0bf293ccd633f7 (diff)
move SK_MMAP_SUPPORT into SkPreConfig, so we can know about its availability
throughout the code. Add SkData::NewFromMMap() help factory. Refactor (now gone) SkMMapStream into SkStream::NewFromFile() factory Review URL: https://codereview.chromium.org/12919013 git-svn-id: http://skia.googlecode.com/svn/trunk@8200 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/core')
-rw-r--r--src/core/SkData.cpp22
-rw-r--r--src/core/SkMMapStream.cpp77
-rw-r--r--src/core/SkStream.cpp64
3 files changed, 86 insertions, 77 deletions
diff --git a/src/core/SkData.cpp b/src/core/SkData.cpp
index 3e0c71a0e8..8fbca7af5f 100644
--- a/src/core/SkData.cpp
+++ b/src/core/SkData.cpp
@@ -8,6 +8,13 @@
#include "SkData.h"
#include "SkFlattenableBuffers.h"
+#if SK_MMAP_SUPPORT
+ #include <unistd.h>
+ #include <sys/mman.h>
+ #include <fcntl.h>
+ #include <errno.h>
+#endif
+
SK_DEFINE_INST_COUNT(SkData)
SkData::SkData(const void* ptr, size_t size, ReleaseProc proc, void* context) {
@@ -120,6 +127,20 @@ SkData* SkData::NewWithCString(const char cstr[]) {
return NewWithCopy(cstr, size);
}
+#if SK_MMAP_SUPPORT
+static void sk_munmap_releaseproc(const void* addr, size_t length, void*) {
+ munmap(const_cast<void*>(addr), length);
+}
+
+SkData* SkData::NewFromMMap(const void* addr, size_t length) {
+ return SkNEW_ARGS(SkData, (addr, length, sk_munmap_releaseproc, NULL));
+}
+#else
+SkData* SkData::NewFromMMap(const void* addr, size_t length) {
+ return NULL;
+}
+#endif
+
///////////////////////////////////////////////////////////////////////////////
void SkData::flatten(SkFlattenableWriteBuffer& buffer) const {
@@ -300,3 +321,4 @@ SkDataSet* SkDataSet::NewEmpty() {
gEmptySet->ref();
return gEmptySet;
}
+
diff --git a/src/core/SkMMapStream.cpp b/src/core/SkMMapStream.cpp
deleted file mode 100644
index 7388e81498..0000000000
--- a/src/core/SkMMapStream.cpp
+++ /dev/null
@@ -1,77 +0,0 @@
-
-/*
- * Copyright 2011 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-#include "SkMMapStream.h"
-
-#include <unistd.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <errno.h>
-
-SkMMAPStream::SkMMAPStream(const char filename[])
-{
- fAddr = NULL; // initialize to failure case
- fSize = 0;
-
- int fildes = open(filename, O_RDONLY);
- if (fildes < 0)
- {
- SkDEBUGF(("---- failed to open(%s) for mmap stream error=%d\n", filename, errno));
- return;
- }
-
- off_t offset = lseek(fildes, 0, SEEK_END); // find the file size
- if (offset == -1)
- {
- SkDEBUGF(("---- failed to lseek(%s) for mmap stream error=%d\n", filename, errno));
- close(fildes);
- return;
- }
- (void)lseek(fildes, 0, SEEK_SET); // restore file offset to beginning
-
- // to avoid a 64bit->32bit warning, I explicitly create a size_t size
- size_t size = static_cast<size_t>(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));
- return;
- }
-
- this->INHERITED::setMemory(addr, size);
-
- fAddr = addr;
- fSize = size;
-}
-
-SkMMAPStream::~SkMMAPStream()
-{
- this->closeMMap();
-}
-
-void SkMMAPStream::setMemory(const void* data, size_t length, bool copyData)
-{
- this->closeMMap();
- this->INHERITED::setMemory(data, length, copyData);
-}
-
-void SkMMAPStream::closeMMap()
-{
- if (fAddr)
- {
- munmap(fAddr, fSize);
- fAddr = NULL;
- }
-}
diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp
index fb343ea76a..acbcfbc53d 100644
--- a/src/core/SkStream.cpp
+++ b/src/core/SkStream.cpp
@@ -13,6 +13,14 @@
#include "SkString.h"
#include "SkOSFile.h"
+#if SK_MMAP_SUPPORT
+ #include <unistd.h>
+ #include <sys/mman.h>
+ #include <fcntl.h>
+ #include <errno.h>
+ #include <unistd.h>
+#endif
+
SK_DEFINE_INST_COUNT(SkStream)
SK_DEFINE_INST_COUNT(SkWStream)
SK_DEFINE_INST_COUNT(SkFILEStream)
@@ -789,3 +797,59 @@ bool SkDebugWStream::write(const void* buffer, size_t size)
#endif
return true;
}
+
+///////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
+
+static bool mmap_filename(const char path[], void** addrPtr, size_t* sizePtr) {
+#if SK_MMAP_SUPPORT
+ int fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ return false;
+ }
+
+ off_t offset = lseek(fd, 0, SEEK_END); // find the file size
+ if (offset == -1) {
+ close(fd);
+ return false;
+ }
+ (void)lseek(fd, 0, SEEK_SET); // restore file offset to beginning
+
+ // to avoid a 64bit->32bit warning, I explicitly create a size_t size
+ size_t size = static_cast<size_t>(offset);
+
+ void* addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
+ close(fd);
+
+ if (MAP_FAILED == addr) {
+ return false;
+ }
+
+ *addrPtr = addr;
+ *sizePtr = size;
+ return true;
+#else
+ return false;
+#endif
+}
+
+SkStream* SkStream::NewFromFile(const char path[]) {
+ void* addr;
+ size_t size;
+ if (mmap_filename(path, &addr, &size)) {
+ SkAutoTUnref<SkData> data(SkData::NewFromMMap(addr, size));
+ if (data.get()) {
+ return SkNEW_ARGS(SkMemoryStream, (data.get()));
+ }
+ }
+
+ // If we get here, then our attempt at using mmap failed, so try normal
+ // file access.
+ SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path));
+ if (!stream->isValid()) {
+ stream->unref();
+ stream = NULL;
+ }
+ return stream;
+}
+