OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "mojo/public/cpp/environment/logging.h" | 5 #include "mojo/public/cpp/environment/logging.h" |
6 #include "mojo/services/media/common/cpp/fifo_allocator.h" | 6 #include "mojo/services/media/common/cpp/fifo_allocator.h" |
7 | 7 |
8 namespace mojo { | 8 namespace mojo { |
9 namespace media { | 9 namespace media { |
10 | 10 |
(...skipping 42 matching lines...) Loading... |
53 // placeholder. | 53 // placeholder. |
54 MakeActivePlaceholder(); | 54 MakeActivePlaceholder(); |
55 } | 55 } |
56 return result; | 56 return result; |
57 } | 57 } |
58 | 58 |
59 // The active region can accommodate this allocation with room left over. | 59 // The active region can accommodate this allocation with room left over. |
60 // Create a new region (allocated) of the requested size at the front of the | 60 // Create a new region (allocated) of the requested size at the front of the |
61 // active region, and adjust the active region to reflect the deficit. | 61 // active region, and adjust the active region to reflect the deficit. |
62 MOJO_DCHECK(active_->size > size); | 62 MOJO_DCHECK(active_->size > size); |
63 Region *allocated = get_free(true, size, active_->offset); | 63 Region* allocated = get_free(true, size, active_->offset); |
64 active_->size -= size; | 64 active_->size -= size; |
65 active_->offset += size; | 65 active_->offset += size; |
66 insert_before(allocated, active_); | 66 insert_before(allocated, active_); |
67 return allocated->offset; | 67 return allocated->offset; |
68 } | 68 } |
69 | 69 |
70 void FifoAllocator::ReleaseRegion(uint64_t size, uint64_t offset) { | 70 void FifoAllocator::ReleaseRegion(uint64_t size, uint64_t offset) { |
71 // Start at active_->next. That's usually the region we're looking for. | 71 // Start at active_->next. That's usually the region we're looking for. |
72 bool released = | 72 bool released = Release(size, offset, active_->next, nullptr) || |
73 Release(size, offset, active_->next, nullptr) || | 73 Release(size, offset, front_, active_); |
74 Release(size, offset, front_, active_); | |
75 MOJO_DCHECK(released); | 74 MOJO_DCHECK(released); |
76 } | 75 } |
77 | 76 |
78 bool FifoAllocator::Release( | 77 bool FifoAllocator::Release(uint64_t size, |
79 uint64_t size, | 78 uint64_t offset, |
80 uint64_t offset, | 79 Region* begin, |
81 Region* begin, | 80 Region* end) { |
82 Region* end) { | |
83 MOJO_DCHECK(begin != nullptr || end == nullptr); | 81 MOJO_DCHECK(begin != nullptr || end == nullptr); |
84 for (Region* region = begin; region != end; region = region->next) { | 82 for (Region* region = begin; region != end; region = region->next) { |
85 if (region->offset == offset) { | 83 if (region->offset == offset) { |
86 MOJO_DCHECK(region->allocated); | 84 MOJO_DCHECK(region->allocated); |
87 MOJO_DCHECK(region->size == size); | 85 MOJO_DCHECK(region->size == size); |
88 region->allocated = false; | 86 region->allocated = false; |
89 | 87 |
90 Region *prev = region->prev; | 88 Region* prev = region->prev; |
91 if (prev != nullptr && !prev->allocated) { | 89 if (prev != nullptr && !prev->allocated) { |
92 // Coalesce wtih the previous region. | 90 // Coalesce wtih the previous region. |
93 prev->size += region->size; | 91 prev->size += region->size; |
94 remove(region); | 92 remove(region); |
95 put_free(region); | 93 put_free(region); |
96 region = prev; | 94 region = prev; |
97 } | 95 } |
98 | 96 |
99 Region *next = region->next; | 97 Region* next = region->next; |
100 if (next != nullptr && !next->allocated) { | 98 if (next != nullptr && !next->allocated) { |
101 // Coalesce wtih the next region. | 99 // Coalesce wtih the next region. |
102 next->offset = region->offset; | 100 next->offset = region->offset; |
103 next->size += region->size; | 101 next->size += region->size; |
104 if (active_ == region) { | 102 if (active_ == region) { |
105 // This can happen if we coalesced the previous region. | 103 // This can happen if we coalesced the previous region. |
106 active_ = next; | 104 active_ = next; |
107 } | 105 } |
108 remove(region); | 106 remove(region); |
109 put_free(region); | 107 put_free(region); |
110 } | 108 } |
111 | 109 |
112 return true; | 110 return true; |
113 } | 111 } |
114 } | 112 } |
115 | 113 |
116 return false; | 114 return false; |
117 } | 115 } |
118 | 116 |
119 bool FifoAllocator::AdvanceActive(uint64_t size) { | 117 bool FifoAllocator::AdvanceActive(uint64_t size) { |
120 MOJO_DCHECK(size != 0); | 118 MOJO_DCHECK(size != 0); |
121 return | 119 return AdvanceActive(size, active_->next, nullptr) || |
122 AdvanceActive(size, active_->next, nullptr) || | 120 AdvanceActive(size, front_, active_); |
123 AdvanceActive(size, front_, active_); | |
124 } | 121 } |
125 | 122 |
126 bool FifoAllocator::AdvanceActive(uint64_t size, Region* begin, Region* end) { | 123 bool FifoAllocator::AdvanceActive(uint64_t size, Region* begin, Region* end) { |
127 for (Region* region = begin; region != end; region = region->next) { | 124 for (Region* region = begin; region != end; region = region->next) { |
128 if (!region->allocated && region->size >= size) { | 125 if (!region->allocated && region->size >= size) { |
129 if (active_->size == 0) { | 126 if (active_->size == 0) { |
130 // The old active region is zero-sized. Get rid of it. | 127 // The old active region is zero-sized. Get rid of it. |
131 MOJO_DCHECK(!active_->allocated); | 128 MOJO_DCHECK(!active_->allocated); |
132 remove(active_); | 129 remove(active_); |
133 put_free(active_); | 130 put_free(active_); |
134 } | 131 } |
135 active_ = region; | 132 active_ = region; |
136 return true; | 133 return true; |
137 } | 134 } |
138 } | 135 } |
139 return false; | 136 return false; |
140 } | 137 } |
141 | 138 |
142 void FifoAllocator::MakeActivePlaceholder() { | 139 void FifoAllocator::MakeActivePlaceholder() { |
143 // If the old active region was at the back of the list, we'll be inserting | 140 // If the old active region was at the back of the list, we'll be inserting |
144 // at the front, so make the offset zero. We insert at the front, because it's | 141 // at the front, so make the offset zero. We insert at the front, because it's |
145 // a bit more efficient and because we don't need to implement insert_after. | 142 // a bit more efficient and because we don't need to implement insert_after. |
146 Region *new_active = get_free( | 143 Region* new_active = get_free( |
147 false, | 144 false, 0, active_ == back_ ? 0 : active_->offset + active_->size); |
148 0, | |
149 active_ == back_ ? 0 : active_->offset + active_->size); | |
150 | 145 |
151 MOJO_DCHECK((active_ == back_) == (active_->next == nullptr)); | 146 MOJO_DCHECK((active_ == back_) == (active_->next == nullptr)); |
152 insert_before(new_active, active_ == back_ ? front_ : active_->next); | 147 insert_before(new_active, active_ == back_ ? front_ : active_->next); |
153 active_ = new_active; | 148 active_ = new_active; |
154 } | 149 } |
155 | 150 |
156 void FifoAllocator::remove(Region* region) { | 151 void FifoAllocator::remove(Region* region) { |
157 MOJO_DCHECK(region); | 152 MOJO_DCHECK(region); |
158 | 153 |
159 if (front_ == region) { | 154 if (front_ == region) { |
160 MOJO_DCHECK(region->prev == nullptr); | 155 MOJO_DCHECK(region->prev == nullptr); |
161 front_ = region->next; | 156 front_ = region->next; |
162 } else { | 157 } else { |
163 MOJO_DCHECK(region->prev); | 158 MOJO_DCHECK(region->prev); |
164 MOJO_DCHECK(region->prev->next ==region); | 159 MOJO_DCHECK(region->prev->next == region); |
165 region->prev->next = region->next; | 160 region->prev->next = region->next; |
166 } | 161 } |
167 | 162 |
168 if (back_ == region) { | 163 if (back_ == region) { |
169 MOJO_DCHECK(region->next == nullptr); | 164 MOJO_DCHECK(region->next == nullptr); |
170 back_ = region->prev; | 165 back_ = region->prev; |
171 } else { | 166 } else { |
172 MOJO_DCHECK(region->next); | 167 MOJO_DCHECK(region->next); |
173 MOJO_DCHECK(region->next->prev == region); | 168 MOJO_DCHECK(region->next->prev == region); |
174 region->next->prev = region->prev; | 169 region->next->prev = region->prev; |
175 } | 170 } |
176 } | 171 } |
177 | 172 |
178 void FifoAllocator::insert_before(Region* region, Region* before_this) { | 173 void FifoAllocator::insert_before(Region* region, Region* before_this) { |
179 MOJO_DCHECK(region); | 174 MOJO_DCHECK(region); |
180 MOJO_DCHECK(before_this); | 175 MOJO_DCHECK(before_this); |
181 | 176 |
182 region->prev = before_this->prev; | 177 region->prev = before_this->prev; |
183 before_this->prev = region; | 178 before_this->prev = region; |
184 region->next = before_this; | 179 region->next = before_this; |
185 if (front_ == before_this) { | 180 if (front_ == before_this) { |
186 MOJO_DCHECK(region->prev == nullptr); | 181 MOJO_DCHECK(region->prev == nullptr); |
187 front_ = region; | 182 front_ = region; |
188 } else { | 183 } else { |
189 MOJO_DCHECK(region->prev); | 184 MOJO_DCHECK(region->prev); |
190 region->prev->next = region; | 185 region->prev->next = region; |
191 } | 186 } |
192 } | 187 } |
193 | 188 |
194 FifoAllocator::Region* FifoAllocator::get_free( | 189 FifoAllocator::Region* FifoAllocator::get_free(bool allocated, |
195 bool allocated, uint64_t size, uint64_t offset) { | 190 uint64_t size, |
| 191 uint64_t offset) { |
196 MOJO_DCHECK(size <= size_); | 192 MOJO_DCHECK(size <= size_); |
197 MOJO_DCHECK(offset <= size_ - size); | 193 MOJO_DCHECK(offset <= size_ - size); |
198 | 194 |
199 Region *result = free_; | 195 Region* result = free_; |
200 if (result == nullptr) { | 196 if (result == nullptr) { |
201 result = new Region(); | 197 result = new Region(); |
202 } else { | 198 } else { |
203 free_ = free_->next; | 199 free_ = free_->next; |
204 } | 200 } |
205 | 201 |
206 result->allocated = allocated; | 202 result->allocated = allocated; |
207 result->size = size; | 203 result->size = size; |
208 result->offset = offset; | 204 result->offset = offset; |
209 | 205 |
210 return result; | 206 return result; |
211 } | 207 } |
212 | 208 |
213 void FifoAllocator::DeleteFrontToBack(Region* region) { | 209 void FifoAllocator::DeleteFrontToBack(Region* region) { |
214 while (region != nullptr) { | 210 while (region != nullptr) { |
215 Region *to_delete = region; | 211 Region* to_delete = region; |
216 region = region->next; | 212 region = region->next; |
217 delete to_delete; | 213 delete to_delete; |
218 } | 214 } |
219 } | 215 } |
220 | 216 |
221 } // namespace media | 217 } // namespace media |
222 } // namespace mojo | 218 } // namespace mojo |
OLD | NEW |