Chromium Code Reviews| 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,7 +6,9 @@ |
| #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> |
| @@ -17,6 +19,66 @@ |
| namespace content { |
| +// Does not retain the file-descriptor (but does call mmap on it), and so |
| +// the caller must still take care of closing the fd. |
| +class SkFileDescriptorStream : public SkStream { |
| + public: |
| + SkFileDescriptorStream(int fd) { |
| + memory_ = NULL; |
| + offset_ = 0; |
| + |
| + // this ensures that if we fail in the constructor, we will safely |
| + // ignore all subsequent calls to read() because we will always trim |
| + // the requested size down to 0 |
| + length_ = 0; |
| + |
| + struct stat st; |
| + if (fstat(fd, &st)) |
| + return; |
| + |
| + void* memory = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); |
| + if (memory == MAP_FAILED) |
| + return; |
| + |
| + memory_ = reinterpret_cast<uint8_t*>(memory); |
| + length_ = st.st_size; |
| + } |
| + |
| + virtual ~SkFileDescriptorStream() { |
| + munmap(const_cast<uint8_t*>(memory_), length_); |
| + } |
| + |
| + virtual bool rewind() OVERRIDE { |
| + offset_ = 0; |
| + return true; |
| + } |
| + |
| + // SkStream implementation. |
| + 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 +159,7 @@ |
| return NULL; |
| } |
| - return new SkFDStream(result_fd, true); |
| + return new SkFileDescriptorStream(result_fd); |
|
jln (very slow on Chromium)
2013/03/18 16:07:21
You say that the caller should close the fd, but t
reed1
2013/03/18 16:12:47
Good point. It is not the imnmediate caller (this
|
| } |
| } // namespace content |