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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/common/font_config_ipc_linux.h" 5 #include "content/common/font_config_ipc_linux.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <sys/mman.h>
9 #include <sys/socket.h> 10 #include <sys/socket.h>
11 #include <sys/stat.h>
10 #include <sys/uio.h> 12 #include <sys/uio.h>
11 #include <unistd.h> 13 #include <unistd.h>
12 14
13 #include "base/pickle.h" 15 #include "base/pickle.h"
14 #include "base/posix/unix_domain_socket_linux.h" 16 #include "base/posix/unix_domain_socket_linux.h"
15 #include "skia/ext/skia_utils_base.h" 17 #include "skia/ext/skia_utils_base.h"
16 #include "third_party/skia/include/core/SkStream.h" 18 #include "third_party/skia/include/core/SkStream.h"
17 19
18 namespace content { 20 namespace content {
19 21
22 // Does not retain the file-descriptor (but does call mmap on it), and so
23 // the caller must still take care of closing the fd.
24 class SkFileDescriptorStream : public SkStream {
25 public:
26 SkFileDescriptorStream(int fd) {
27 memory_ = NULL;
28 offset_ = 0;
29
30 // this ensures that if we fail in the constructor, we will safely
31 // ignore all subsequent calls to read() because we will always trim
32 // the requested size down to 0
33 length_ = 0;
34
35 struct stat st;
36 if (fstat(fd, &st))
37 return;
38
39 void* memory = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
40 if (memory == MAP_FAILED)
41 return;
42
43 memory_ = reinterpret_cast<uint8_t*>(memory);
44 length_ = st.st_size;
45 }
46
47 virtual ~SkFileDescriptorStream() {
48 munmap(const_cast<uint8_t*>(memory_), length_);
49 }
50
51 virtual bool rewind() OVERRIDE {
52 offset_ = 0;
53 return true;
54 }
55
56 // SkStream implementation.
57 virtual size_t read(void* buffer, size_t size) OVERRIDE {
58 if (!buffer && !size) {
59 // This is request for the length of the stream.
60 return length_;
61 }
62
63 size_t remaining = length_ - offset_;
64 if (size > remaining)
65 size = remaining;
66 if (buffer)
67 memcpy(buffer, memory_ + offset_, size);
68
69 offset_ += size;
70 return size;
71 }
72
73 virtual const void* getMemoryBase() OVERRIDE {
74 return memory_;
75 }
76
77 private:
78 const uint8_t* memory_;
79 size_t offset_, length_;
80 };
81
20 FontConfigIPC::FontConfigIPC(int fd) 82 FontConfigIPC::FontConfigIPC(int fd)
21 : fd_(fd) { 83 : fd_(fd) {
22 } 84 }
23 85
24 FontConfigIPC::~FontConfigIPC() { 86 FontConfigIPC::~FontConfigIPC() {
25 close(fd_); 87 close(fd_);
26 } 88 }
27 89
28 bool FontConfigIPC::matchFamilyName(const char familyName[], 90 bool FontConfigIPC::matchFamilyName(const char familyName[],
29 SkTypeface::Style requestedStyle, 91 SkTypeface::Style requestedStyle,
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 Pickle reply(reinterpret_cast<char*>(reply_buf), r); 152 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
91 bool result; 153 bool result;
92 PickleIterator iter(reply); 154 PickleIterator iter(reply);
93 if (!reply.ReadBool(&iter, &result) || 155 if (!reply.ReadBool(&iter, &result) ||
94 !result) { 156 !result) {
95 if (result_fd) 157 if (result_fd)
96 close(result_fd); 158 close(result_fd);
97 return NULL; 159 return NULL;
98 } 160 }
99 161
100 return new SkFDStream(result_fd, true); 162 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
101 } 163 }
102 164
103 } // namespace content 165 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698