Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1529)

Unified Diff: content/common/font_config_ipc_linux.cc

Issue 12918004: mmap the opened file descriptor in the renderer process, close the fd in the browser process. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/common/font_config_ipc_linux.cc
===================================================================
--- content/common/font_config_ipc_linux.cc (revision 188729)
+++ content/common/font_config_ipc_linux.cc (working copy)
@@ -6,10 +6,13 @@
#include <errno.h>
#include <fcntl.h>
+#include <sys/mman.h>
#include <sys/socket.h>
+#include <sys/stat.h>
#include <sys/uio.h>
#include <unistd.h>
+#include "base/file_util.h"
#include "base/pickle.h"
#include "base/posix/unix_domain_socket_linux.h"
#include "skia/ext/skia_utils_base.h"
@@ -17,6 +20,60 @@
namespace content {
+// Takes "ownership" of the file descriptor, and will close() it.
+class SkFileDescriptorStream : public SkStream {
+ public:
+ SkFileDescriptorStream(int fd) : memory_(NULL), offset_(0), length_(0) {
+ struct stat st;
+ if (fstat(fd, &st)) {
+ DCHECK(!HANDLE_EINTR(close(fd)));
piman 2013/03/18 19:49:14 no side effects inside of DCHECK please.
+ return;
+ }
+
+ void* memory = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+ DCHECK(!HANDLE_EINTR(close(fd)));
piman 2013/03/18 19:49:14 no side effects inside of DCHECK please.
+ if (memory == MAP_FAILED)
+ return;
+
+ memory_ = reinterpret_cast<uint8_t*>(memory);
+ length_ = st.st_size;
+ }
+
+ virtual ~SkFileDescriptorStream() {
+ if (memory_)
+ munmap(const_cast<uint8_t*>(memory_), length_);
+ }
+
+ virtual bool rewind() OVERRIDE {
+ offset_ = 0;
+ return true;
+ }
+
+ virtual size_t read(void* buffer, size_t size) OVERRIDE {
+ if (!buffer && !size) {
+ // This is request for the length of the stream.
+ return length_;
+ }
+
+ size_t remaining = length_ - offset_;
+ if (size > remaining)
+ size = remaining;
+ if (buffer)
+ memcpy(buffer, memory_ + offset_, size);
+
+ offset_ += size;
+ return size;
+ }
+
+ virtual const void* getMemoryBase() OVERRIDE {
+ return memory_;
+ }
+
+ private:
+ const uint8_t* memory_;
+ size_t offset_, length_;
+};
+
FontConfigIPC::FontConfigIPC(int fd)
: fd_(fd) {
}
@@ -97,7 +154,7 @@
return NULL;
}
- return new SkFDStream(result_fd, true);
+ return new SkFileDescriptorStream(result_fd);
piman 2013/03/18 19:49:14 If the map failed inside of the constructor, shoul
reed1 2013/03/18 20:15:41 Done.
}
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698