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

Side by Side Diff: content/browser/renderer_host/resource_buffer.cc

Issue 10911297: Modify AsyncResourceHandler to use a single shared memory buffer (in ring (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 3 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/renderer_host/resource_buffer.h"
6
7 #include <math.h>
brettw 2012/09/17 23:53:10 Blank line after this.
darin (slow to review) 2012/09/18 05:00:50 Done.
8 #include "base/logging.h"
9
10 namespace content {
11
12 // A circular buffer allocator.
13 //
14 // We keep track of the starting offset (alloc_start_) and the ending offset
15 // (alloc_end_). There are two layouts to keep in mind:
16 //
17 // #1:
18 // ------------[XXXXXXXXXXXXXXXXXXXXXXX]----
brettw 2012/09/17 23:53:10 In these diagrams it might be better to have end p
darin (slow to review) 2012/09/18 05:00:50 Done.
19 // ^ ^
20 // start end
21 //
22 // #2:
23 // XXXXXXXXXX]---------------------[XXXXXXXX
24 // ^ ^
25 // end start
26 //
27 // If end <= start, then we have the buffer wraparound case (depicted second).
28 // If the buffer is empty, then start and end will be set to -1.
29 //
30 // Allocations are always contiguous.
31
32 ResourceBuffer::ResourceBuffer()
33 : buf_size_(0),
34 min_alloc_size_(0),
35 max_alloc_size_(0),
36 alloc_start_(-1),
37 alloc_end_(-1) {
38 }
39
40 ResourceBuffer::~ResourceBuffer() {
41 }
42
43 bool ResourceBuffer::Initialize(int buffer_size,
44 int min_allocation_size,
45 int max_allocation_size) {
46 DCHECK(!IsInitialized());
47
48 // It would be wasteful if these are not multiples of min_allocation_size.
49 DCHECK_EQ(0, buffer_size % min_allocation_size);
50 DCHECK_EQ(0, max_allocation_size % min_allocation_size);
51
52 buf_size_ = buffer_size;
53 min_alloc_size_ = min_allocation_size;
54 max_alloc_size_ = max_allocation_size;
55
56 return shared_mem_.CreateAndMapAnonymous(buf_size_);
57 }
58
59 bool ResourceBuffer::IsInitialized() const {
60 return shared_mem_.memory() != NULL;
61 }
62
63 bool ResourceBuffer::ShareToProcess(
64 base::ProcessHandle process_handle,
65 base::SharedMemoryHandle* shared_memory_handle,
66 int* shared_memory_size) {
67 DCHECK(IsInitialized());
68
69 if (!shared_mem_.ShareToProcess(process_handle, shared_memory_handle))
70 return false;
71
72 *shared_memory_size = buf_size_;
73 return true;
74 }
75
76 bool ResourceBuffer::CanAllocate() const {
77 DCHECK(IsInitialized());
78
79 if (alloc_start_ == -1)
80 return true;
81
82 int diff = alloc_end_ - alloc_start_;
83 if (diff > 0)
84 return (buf_size_ - diff) >= min_alloc_size_;
85
86 return -diff >= min_alloc_size_;
87 }
88
89 char* ResourceBuffer::Allocate(int* size) {
90 DCHECK(CanAllocate());
91
92 int alloc_offset = 0;
93 int alloc_size;
94
95 if (alloc_start_ == -1) {
brettw 2012/09/17 23:53:10 The different cases in this nested if statement co
96 alloc_start_ = 0;
97 alloc_end_ = buf_size_;
98 alloc_size = buf_size_;
99 } else if (alloc_start_ < alloc_end_) {
100 if ((buf_size_ - alloc_end_) >= min_alloc_size_) {
101 alloc_offset = alloc_end_;
102 alloc_size = buf_size_ - alloc_end_;
103 alloc_end_ = buf_size_;
104 } else {
105 DCHECK(alloc_start_ > 0);
106 alloc_size = alloc_start_;
107 alloc_end_ = alloc_start_;
108 }
109 } else {
110 DCHECK(alloc_end_ < alloc_start_);
111 alloc_offset = alloc_end_;
112 alloc_size = alloc_start_ - alloc_end_;
113 alloc_end_ = alloc_start_;
114 }
115
116 alloc_sizes_.push(alloc_size);
brettw 2012/09/17 23:53:10 It took me a minute to realize why this goes befor
darin (slow to review) 2012/09/18 05:00:50 Done.
117
118 if (alloc_size > max_alloc_size_) {
119 alloc_size = max_alloc_size_;
120 ShrinkLastAllocation(alloc_size);
121 }
122
123 *size = alloc_size;
124 return static_cast<char*>(shared_mem_.memory()) + alloc_offset;
125 }
126
127 int ResourceBuffer::GetLastAllocationOffset() const {
128 DCHECK(!alloc_sizes_.empty());
129 DCHECK(alloc_end_ >= alloc_sizes_.back());
130 return alloc_end_ - alloc_sizes_.back();
131 }
132
133 void ResourceBuffer::ShrinkLastAllocation(int new_size) {
134 DCHECK(!alloc_sizes_.empty());
135
136 int aligned_size = (new_size / min_alloc_size_) * min_alloc_size_;
137 if (aligned_size < new_size)
138 aligned_size += min_alloc_size_;
139
140 DCHECK_LE(new_size, aligned_size);
141 DCHECK_GE(alloc_sizes_.back(), aligned_size);
142
143 int* last_allocation_size = &alloc_sizes_.back();
144 alloc_end_ -= (*last_allocation_size - aligned_size);
145 *last_allocation_size = aligned_size;
146 }
147
148 void ResourceBuffer::RecycleLeastRecentlyAllocated() {
149 DCHECK(!alloc_sizes_.empty());
150 int allocation_size = alloc_sizes_.front();
151 alloc_sizes_.pop();
152
153 alloc_start_ += allocation_size;
154 DCHECK(alloc_start_ <= buf_size_);
155
156 if (alloc_start_ == alloc_end_) {
157 DCHECK(alloc_sizes_.empty());
158 alloc_start_ = -1;
159 alloc_end_ = -1;
160 } else if (alloc_start_ == buf_size_) {
161 DCHECK(!alloc_sizes_.empty());
162 alloc_start_ = 0;
163 }
164 }
165
166 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698