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

Side by Side Diff: mojo/services/media/common/cpp/fifo_allocator.cc

Issue 1460693004: Add helper classes to for managing shared buffers. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: fix .gitignore Created 5 years, 1 month 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <cassert>
6
7 #include "mojo/services/media/common/cpp/fifo_allocator.h"
8
9 namespace mojo {
10 namespace media {
11
12 FifoAllocator::FifoAllocator(uint64_t size) : front_(nullptr), free_(nullptr) {
13 // front_ and free_ need to be set to nullptr before calling reset.
johngro 2015/12/05 00:45:55 MOJO_DCHECK(size < kNullOffset);
dalesat 2015/12/07 18:30:20 Done.
14 Reset(size);
15 }
16
17 FifoAllocator::~FifoAllocator() {
18 assert(front_ == back_);
johngro 2015/12/05 00:45:54 its probably better to use MOJO_DCHECK here instea
dalesat 2015/12/07 18:30:20 Done.
19 DeleteFrontToBack(front_);
20 DeleteFrontToBack(free_);
21 }
22
23 void FifoAllocator::Reset(uint64_t size) {
24 DeleteFrontToBack(front_);
25 DeleteFrontToBack(free_);
26 size_ = size;
27 free_ = nullptr;
28 front_ = back_ = active_ = get_free(false, size, 0);
29 active_->prev = nullptr;
30 active_->next = nullptr;
31 }
32
33 uint64_t FifoAllocator::AllocateRegion(uint64_t size) {
34 assert(size != 0);
35
36 if (active_->size < size) {
37 // The active region is too small. Look for one that's large enough.
38 if (!AdvanceActive(size)) {
39 // No unallocated regions are large enough. Can't do the allocation.
40 return kNullOffset;
41 }
42 }
43
44 if (active_->size == size) {
45 // The active region is exactly the right size. Use it for the allocation.
46 auto result = active_->offset;
johngro 2015/12/05 00:45:54 Generally, "auto" is supposed to be used when it r
dalesat 2015/12/07 18:30:20 Done.
47 active_->allocated = true;
48 if (active_ == back_ && !front_->allocated) {
49 // active_ was the back region and the front region isn't allocted. Make
johngro 2015/12/05 00:45:55 s/allocted/allocated
dalesat 2015/12/07 18:30:20 Done.
50 // the front region the new active region.
51 active_ = front_;
52 } else {
53 // The region after active_ is allocated, so make a zero-sized
54 // placeholder.
55 MakeActivePlaceholder();
56 }
57 return result;
58 }
59
60 // The active region can accommodate this allocation with room left over.
61 // Create a new region (allocated) of the requested size at the front of the
62 // active region, and adjust the active region to reflect the deficit.
63 assert(active_->size > size);
64 auto allocated = get_free(true, size, active_->offset);
johngro 2015/12/05 00:45:54 s/auto/Region*
dalesat 2015/12/07 18:30:20 Done.
65 active_->size -= size;
66 active_->offset += size;
67 insert_before(allocated, active_);
68 return allocated->offset;
69 }
70
71 void FifoAllocator::ReleaseRegion(uint64_t size, uint64_t offset) {
72 // Start at active_->next. That's usually the region we're looking for.
73 auto released =
johngro 2015/12/05 00:45:54 s/auto/bool
dalesat 2015/12/07 18:30:20 Done.
74 Release(size, offset, active_->next, nullptr) ||
75 Release(size, offset, front_, active_);
76 assert(released);
77 }
78
79 bool FifoAllocator::Release(
80 uint64_t size,
81 uint64_t offset,
82 Region* begin,
83 Region* end) {
johngro 2015/12/05 00:45:55 MOJO_DCHECK(begin);
dalesat 2015/12/07 18:30:20 Can be nullptr if end is nullptr. Done.
84 for (Region* region = begin; region != end; region = region->next) {
85 if (region->offset == offset) {
86 assert(region->allocated);
87 assert(region->size == size);
88 region->allocated = false;
89
90 auto prev = region->prev;
91 if (prev != nullptr && !prev->allocated) {
92 // Coalesce wtih the previous region.
93 prev->size += region->size;
94 remove(region);
95 put_free(region);
96 region = prev;
97 }
98
99 auto next = region->next;
100 if (next != nullptr && !next->allocated) {
101 // Coalesce wtih the next region.
102 next->offset = region->offset;
103 next->size += region->size;
104 if (active_ == region) {
105 // This can happen if we coalesced the previous region.
106 active_ = next;
107 }
108 remove(region);
109 put_free(region);
110 }
111
112 return true;
113 }
114 }
115
116 return false;
117 }
118
119 bool FifoAllocator::AdvanceActive(uint64_t size) {
120 assert(size != 0);
121 return
122 AdvanceActive(size, active_->next, nullptr) ||
123 AdvanceActive(size, front_, active_);
124 }
125
126 bool FifoAllocator::AdvanceActive(uint64_t size, Region* begin, Region* end) {
127 for (Region* region = begin; region != end; region = region->next) {
128 if (!region->allocated && region->size >= size) {
129 if (active_->size == 0) {
130 // The old active region is zero-sized. Get rid of it.
131 assert(!active_->allocated);
132 remove(active_);
johngro 2015/12/05 00:45:55 put_free(active_); In general, is there ever a ti
dalesat 2015/12/07 18:30:20 Done. I'd rather not put_free in remove, because t
133 }
134 active_ = region;
135 return true;
136 }
137 }
138 return false;
139 }
140
141 void FifoAllocator::MakeActivePlaceholder() {
142 // If the old active region was at the back of the list, we'll be inserting
143 // at the front, so make the offset zero. We insert at the front, because it's
144 // a bit more efficient and because we don't need to implement insert_after.
145 auto new_active = get_free(
146 false,
147 0,
148 active_ == back_ ? 0 : active_->offset + active_->size);
149
150 assert((active_ == back_) == (active_->next == nullptr));
151 insert_before(new_active, active_ == back_ ? front_ : active_->next);
152 active_ = new_active;
153 }
154
155 void FifoAllocator::remove(Region* region) {
156 assert(region);
157
158 if (front_ == region) {
159 assert(region->prev == nullptr);
160 front_ = region->next;
161 } else {
162 assert(region->prev);
163 assert(region->prev->next ==region);
164 region->prev->next = region->next;
165 }
166
167 if (back_ == region) {
168 assert(region->next == nullptr);
169 back_ = region->prev;
170 } else {
171 assert(region->next);
172 assert(region->next->prev == region);
173 region->next->prev = region->prev;
174 }
175 }
176
177 void FifoAllocator::insert_before(Region* region, Region* before_this) {
178 assert(region);
179 assert(before_this);
180
181 region->prev = before_this->prev;
182 before_this->prev = region;
183 region->next = before_this;
184 if (front_ == before_this) {
185 assert(region->prev == nullptr);
186 front_ = region;
187 } else {
188 assert(region->prev);
189 region->prev->next = region;
190 }
191 }
192
193 FifoAllocator::Region* FifoAllocator::get_free(
194 bool allocated, uint64_t size, uint64_t offset) {
195 assert(size + offset <= size_);
johngro 2015/12/05 00:45:54 in theory, size + offset could wrap and pass this
dalesat 2015/12/07 18:30:20 Done.
196
197 auto result = free_;
198 if (result == nullptr) {
199 result = new Region();
200 } else {
201 free_ = free_->next;
202 }
203
204 result->allocated = allocated;
205 result->size = size;
206 result->offset = offset;
207
208 return result;
209 }
210
211 void FifoAllocator::DeleteFrontToBack(Region* region) {
212 while (region != nullptr) {
213 auto to_delete = region;
214 region = region->next;
215 delete to_delete;
216 }
217 }
218
219 } // namespace media
220 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698