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

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: sync Created 4 years, 11 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
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 "mojo/public/cpp/environment/logging.h"
6 #include "mojo/services/media/common/cpp/fifo_allocator.h"
7
8 namespace mojo {
9 namespace media {
10
11 FifoAllocator::FifoAllocator(uint64_t size) : front_(nullptr), free_(nullptr) {
12 // front_ and free_ need to be set to nullptr before calling reset.
13 Reset(size);
14 }
15
16 FifoAllocator::~FifoAllocator() {
17 DeleteFrontToBack(front_);
18 DeleteFrontToBack(free_);
19 }
20
21 void FifoAllocator::Reset(uint64_t size) {
22 MOJO_DCHECK(size < kNullOffset);
23 DeleteFrontToBack(front_);
24 DeleteFrontToBack(free_);
25 size_ = size;
26 free_ = nullptr;
27 front_ = back_ = active_ = get_free(false, size, 0);
28 active_->prev = nullptr;
29 active_->next = nullptr;
30 }
31
32 uint64_t FifoAllocator::AllocateRegion(uint64_t size) {
33 MOJO_DCHECK(size != 0);
34
35 if (active_->size < size) {
36 // The active region is too small. Look for one that's large enough.
37 if (!AdvanceActive(size)) {
38 // No unallocated regions are large enough. Can't do the allocation.
39 return kNullOffset;
40 }
41 }
42
43 if (active_->size == size) {
44 // The active region is exactly the right size. Use it for the allocation.
45 uint64_t result = active_->offset;
46 active_->allocated = true;
47 if (active_ == back_ && !front_->allocated) {
48 // active_ was the back region and the front region isn't allocated. Make
49 // the front region the new active region.
50 active_ = front_;
51 } else {
52 // The region after active_ is allocated, so make a zero-sized
53 // placeholder.
54 MakeActivePlaceholder();
55 }
56 return result;
57 }
58
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
61 // active region, and adjust the active region to reflect the deficit.
62 MOJO_DCHECK(active_->size > size);
63 Region *allocated = get_free(true, size, active_->offset);
64 active_->size -= size;
65 active_->offset += size;
66 insert_before(allocated, active_);
67 return allocated->offset;
68 }
69
70 void FifoAllocator::ReleaseRegion(uint64_t size, uint64_t offset) {
71 // Start at active_->next. That's usually the region we're looking for.
72 bool released =
73 Release(size, offset, active_->next, nullptr) ||
74 Release(size, offset, front_, active_);
75 MOJO_DCHECK(released);
76 }
77
78 bool FifoAllocator::Release(
79 uint64_t size,
80 uint64_t offset,
81 Region* begin,
82 Region* end) {
83 MOJO_DCHECK(begin != nullptr || end == nullptr);
84 for (Region* region = begin; region != end; region = region->next) {
85 if (region->offset == offset) {
86 MOJO_DCHECK(region->allocated);
87 MOJO_DCHECK(region->size == size);
88 region->allocated = false;
89
90 Region *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 Region *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 MOJO_DCHECK(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 MOJO_DCHECK(!active_->allocated);
132 remove(active_);
133 put_free(active_);
134 }
135 active_ = region;
136 return true;
137 }
138 }
139 return false;
140 }
141
142 void FifoAllocator::MakeActivePlaceholder() {
143 // 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
145 // a bit more efficient and because we don't need to implement insert_after.
146 Region *new_active = get_free(
147 false,
148 0,
149 active_ == back_ ? 0 : active_->offset + active_->size);
150
151 MOJO_DCHECK((active_ == back_) == (active_->next == nullptr));
152 insert_before(new_active, active_ == back_ ? front_ : active_->next);
153 active_ = new_active;
154 }
155
156 void FifoAllocator::remove(Region* region) {
157 MOJO_DCHECK(region);
158
159 if (front_ == region) {
160 MOJO_DCHECK(region->prev == nullptr);
161 front_ = region->next;
162 } else {
163 MOJO_DCHECK(region->prev);
164 MOJO_DCHECK(region->prev->next ==region);
165 region->prev->next = region->next;
166 }
167
168 if (back_ == region) {
169 MOJO_DCHECK(region->next == nullptr);
170 back_ = region->prev;
171 } else {
172 MOJO_DCHECK(region->next);
173 MOJO_DCHECK(region->next->prev == region);
174 region->next->prev = region->prev;
175 }
176 }
177
178 void FifoAllocator::insert_before(Region* region, Region* before_this) {
179 MOJO_DCHECK(region);
180 MOJO_DCHECK(before_this);
181
182 region->prev = before_this->prev;
183 before_this->prev = region;
184 region->next = before_this;
185 if (front_ == before_this) {
186 MOJO_DCHECK(region->prev == nullptr);
187 front_ = region;
188 } else {
189 MOJO_DCHECK(region->prev);
190 region->prev->next = region;
191 }
192 }
193
194 FifoAllocator::Region* FifoAllocator::get_free(
195 bool allocated, uint64_t size, uint64_t offset) {
196 MOJO_DCHECK(size <= size_);
197 MOJO_DCHECK(offset <= size_ - size);
198
199 Region *result = free_;
200 if (result == nullptr) {
201 result = new Region();
202 } else {
203 free_ = free_->next;
204 }
205
206 result->allocated = allocated;
207 result->size = size;
208 result->offset = offset;
209
210 return result;
211 }
212
213 void FifoAllocator::DeleteFrontToBack(Region* region) {
214 while (region != nullptr) {
215 Region *to_delete = region;
216 region = region->next;
217 delete to_delete;
218 }
219 }
220
221 } // namespace media
222 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/services/media/common/cpp/fifo_allocator.h ('k') | mojo/services/media/common/cpp/mapped_shared_buffer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698