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

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>
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]----
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) {
James Simonsen 2012/09/15 02:19:06 Should we DCHECK that these are all integer multip
46 DCHECK(!IsInitialized());
47
48 buf_size_ = buffer_size;
49 min_alloc_size_ = min_allocation_size;
50 max_alloc_size_ = max_allocation_size;
51
52 return shared_mem_.CreateAndMapAnonymous(buf_size_);
53 }
54
55 bool ResourceBuffer::IsInitialized() const {
56 return shared_mem_.memory() != NULL;
57 }
58
59 bool ResourceBuffer::ShareToProcess(
60 base::ProcessHandle process_handle,
61 base::SharedMemoryHandle* shared_memory_handle,
62 int* shared_memory_size) {
63 DCHECK(IsInitialized());
64
65 if (!shared_mem_.ShareToProcess(process_handle, shared_memory_handle))
66 return false;
67
68 *shared_memory_size = buf_size_;
69 return true;
70 }
71
72 bool ResourceBuffer::CanAllocate() const {
73 DCHECK(IsInitialized());
74
75 if (alloc_start_ == -1)
76 return true;
77
78 int diff = alloc_end_ - alloc_start_;
79 if (diff > 0)
80 return (buf_size_ - diff) >= min_alloc_size_;
81
82 return -diff >= min_alloc_size_;
83 }
84
85 char* ResourceBuffer::Allocate(int* size) {
86 DCHECK(CanAllocate());
87
88 int alloc_offset = 0;
89 int alloc_size;
90
91 if (alloc_start_ == -1) {
92 alloc_start_ = 0;
93 alloc_end_ = buf_size_;
94 alloc_size = buf_size_;
95 } else if (alloc_start_ < alloc_end_) {
96 if ((buf_size_ - alloc_end_) >= min_alloc_size_) {
97 alloc_offset = alloc_end_;
98 alloc_size = buf_size_ - alloc_end_;
99 alloc_end_ = buf_size_;
100 } else {
101 DCHECK(alloc_start_ > 0);
102 alloc_size = alloc_start_;
103 alloc_end_ = alloc_start_;
104 }
105 } else {
106 DCHECK(alloc_end_ < alloc_start_);
107 alloc_offset = alloc_end_;
108 alloc_size = alloc_start_ - alloc_end_;
109 alloc_end_ = alloc_start_;
110 }
111
112 alloc_sizes_.push(alloc_size);
113
114 if (alloc_size > max_alloc_size_) {
115 alloc_size = max_alloc_size_;
116 ShrinkLastAllocation(alloc_size);
117 }
118
119 *size = alloc_size;
120 return static_cast<char*>(shared_mem_.memory()) + alloc_offset;
121 }
122
123 int ResourceBuffer::GetLastAllocationOffset() const {
124 DCHECK(!alloc_sizes_.empty());
125 DCHECK(alloc_end_ >= alloc_sizes_.back());
126 return alloc_end_ - alloc_sizes_.back();
127 }
128
129 void ResourceBuffer::ShrinkLastAllocation(int new_size) {
130 DCHECK(!alloc_sizes_.empty());
131
132 int aligned_size = (new_size / min_alloc_size_) * min_alloc_size_;
133 if (aligned_size < new_size)
134 aligned_size += min_alloc_size_;
135
136 DCHECK_LE(new_size, aligned_size);
137 DCHECK_GE(alloc_sizes_.back(), aligned_size);
138
139 int* last_allocation_size = &alloc_sizes_.back();
140 alloc_end_ -= (*last_allocation_size - aligned_size);
141 *last_allocation_size = aligned_size;
142 }
143
144 void ResourceBuffer::RecycleLeastRecentlyAllocated() {
145 DCHECK(!alloc_sizes_.empty());
146 int allocation_size = alloc_sizes_.front();
147 alloc_sizes_.pop();
148
149 alloc_start_ += allocation_size;
150 DCHECK(alloc_start_ <= buf_size_);
151
152 if (alloc_start_ == alloc_end_) {
153 DCHECK(alloc_sizes_.empty());
154 alloc_start_ = -1;
155 alloc_end_ = -1;
156 } else if (alloc_start_ == buf_size_) {
157 DCHECK(!alloc_sizes_.empty());
158 alloc_start_ = 0;
159 }
160 }
161
162 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698