OLD | NEW |
---|---|
(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 MOJO_DCHECK(front_ == back_); | |
18 DeleteFrontToBack(front_); | |
19 DeleteFrontToBack(free_); | |
20 } | |
21 | |
22 void FifoAllocator::Reset(uint64_t size) { | |
23 MOJO_DCHECK(size < kNullOffset); | |
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 MOJO_DCHECK(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 uint64_t result = active_->offset; | |
47 active_->allocated = true; | |
48 if (active_ == back_ && !front_->allocated) { | |
49 // active_ was the back region and the front region isn't allocated. Make | |
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 MOJO_DCHECK(active_->size > size); | |
64 Region *allocated = get_free(true, size, active_->offset); | |
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 bool released = | |
74 Release(size, offset, active_->next, nullptr) || | |
75 Release(size, offset, front_, active_); | |
76 MOJO_DCHECK(released); | |
77 } | |
78 | |
79 bool FifoAllocator::Release( | |
80 uint64_t size, | |
81 uint64_t offset, | |
82 Region* begin, | |
83 Region* end) { | |
84 MOJO_DCHECK(begin != nullptr || end == nullptr); | |
85 for (Region* region = begin; region != end; region = region->next) { | |
86 if (region->offset == offset) { | |
87 MOJO_DCHECK(region->allocated); | |
88 MOJO_DCHECK(region->size == size); | |
89 region->allocated = false; | |
90 | |
91 Region *prev = region->prev; | |
92 if (prev != nullptr && !prev->allocated) { | |
93 // Coalesce wtih the previous region. | |
94 prev->size += region->size; | |
95 remove(region); | |
96 put_free(region); | |
97 region = prev; | |
98 } | |
99 | |
100 Region *next = region->next; | |
101 if (next != nullptr && !next->allocated) { | |
102 // Coalesce wtih the next region. | |
103 next->offset = region->offset; | |
104 next->size += region->size; | |
105 if (active_ == region) { | |
106 // This can happen if we coalesced the previous region. | |
107 active_ = next; | |
108 } | |
109 remove(region); | |
110 put_free(region); | |
111 } | |
112 | |
113 return true; | |
114 } | |
115 } | |
116 | |
117 return false; | |
118 } | |
119 | |
120 bool FifoAllocator::AdvanceActive(uint64_t size) { | |
121 MOJO_DCHECK(size != 0); | |
122 return | |
123 AdvanceActive(size, active_->next, nullptr) || | |
124 AdvanceActive(size, front_, active_); | |
125 } | |
126 | |
127 bool FifoAllocator::AdvanceActive(uint64_t size, Region* begin, Region* end) { | |
128 for (Region* region = begin; region != end; region = region->next) { | |
129 if (!region->allocated && region->size >= size) { | |
130 if (active_->size == 0) { | |
131 // The old active region is zero-sized. Get rid of it. | |
132 MOJO_DCHECK(!active_->allocated); | |
133 remove(active_); | |
134 put_free(active_); | |
johngro
2015/12/10 18:38:08
If we are going to start using intrusive container
dalesat
2015/12/10 22:34:39
Understood.
I didn't want to create a reusable in
| |
135 } | |
136 active_ = region; | |
137 return true; | |
138 } | |
139 } | |
140 return false; | |
141 } | |
142 | |
143 void FifoAllocator::MakeActivePlaceholder() { | |
144 // If the old active region was at the back of the list, we'll be inserting | |
145 // at the front, so make the offset zero. We insert at the front, because it's | |
146 // a bit more efficient and because we don't need to implement insert_after. | |
147 Region *new_active = get_free( | |
148 false, | |
149 0, | |
150 active_ == back_ ? 0 : active_->offset + active_->size); | |
151 | |
152 MOJO_DCHECK((active_ == back_) == (active_->next == nullptr)); | |
153 insert_before(new_active, active_ == back_ ? front_ : active_->next); | |
154 active_ = new_active; | |
155 } | |
156 | |
157 void FifoAllocator::remove(Region* region) { | |
158 MOJO_DCHECK(region); | |
159 | |
160 if (front_ == region) { | |
161 MOJO_DCHECK(region->prev == nullptr); | |
162 front_ = region->next; | |
163 } else { | |
164 MOJO_DCHECK(region->prev); | |
165 MOJO_DCHECK(region->prev->next ==region); | |
166 region->prev->next = region->next; | |
167 } | |
168 | |
169 if (back_ == region) { | |
170 MOJO_DCHECK(region->next == nullptr); | |
171 back_ = region->prev; | |
172 } else { | |
173 MOJO_DCHECK(region->next); | |
174 MOJO_DCHECK(region->next->prev == region); | |
175 region->next->prev = region->prev; | |
176 } | |
177 } | |
178 | |
179 void FifoAllocator::insert_before(Region* region, Region* before_this) { | |
180 MOJO_DCHECK(region); | |
181 MOJO_DCHECK(before_this); | |
182 | |
183 region->prev = before_this->prev; | |
184 before_this->prev = region; | |
185 region->next = before_this; | |
186 if (front_ == before_this) { | |
187 MOJO_DCHECK(region->prev == nullptr); | |
188 front_ = region; | |
189 } else { | |
190 MOJO_DCHECK(region->prev); | |
191 region->prev->next = region; | |
192 } | |
193 } | |
194 | |
195 FifoAllocator::Region* FifoAllocator::get_free( | |
196 bool allocated, uint64_t size, uint64_t offset) { | |
197 MOJO_DCHECK(size <= size_); | |
198 MOJO_DCHECK(offset <= size_ - size); | |
199 | |
200 Region *result = free_; | |
201 if (result == nullptr) { | |
202 result = new Region(); | |
203 } else { | |
204 free_ = free_->next; | |
205 } | |
206 | |
207 result->allocated = allocated; | |
208 result->size = size; | |
209 result->offset = offset; | |
210 | |
211 return result; | |
212 } | |
213 | |
214 void FifoAllocator::DeleteFrontToBack(Region* region) { | |
215 while (region != nullptr) { | |
216 Region *to_delete = region; | |
217 region = region->next; | |
218 delete to_delete; | |
219 } | |
220 } | |
221 | |
222 } // namespace media | |
223 } // namespace mojo | |
OLD | NEW |