| 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 #ifndef MOJO_SERVICES_MEDIA_COMMON_CPP_FIFO_ALLOCATOR_H_ | 5 #ifndef MOJO_SERVICES_MEDIA_COMMON_CPP_FIFO_ALLOCATOR_H_ |
| 6 #define MOJO_SERVICES_MEDIA_COMMON_CPP_FIFO_ALLOCATOR_H_ | 6 #define MOJO_SERVICES_MEDIA_COMMON_CPP_FIFO_ALLOCATOR_H_ |
| 7 | 7 |
| 8 #include <cstdint> | 8 #include <cstdint> |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 // region structures to sit unused if the number of regions ever gets large. | 48 // region structures to sit unused if the number of regions ever gets large. |
| 49 // This is generally not an issue for the streaming scenarios for which the | 49 // This is generally not an issue for the streaming scenarios for which the |
| 50 // class is intended. | 50 // class is intended. |
| 51 // | 51 // |
| 52 // Deallocations (releases) employ a sequential search for a matching | 52 // Deallocations (releases) employ a sequential search for a matching |
| 53 // region. The search is done starting immediately after the active region, so | 53 // region. The search is done starting immediately after the active region, so |
| 54 // it typically finds the desired region immediately. If the number of regions | 54 // it typically finds the desired region immediately. If the number of regions |
| 55 // is very large and deallocation is frequently done out of order, the | 55 // is very large and deallocation is frequently done out of order, the |
| 56 // sequential searches may be a performance issue. | 56 // sequential searches may be a performance issue. |
| 57 class FifoAllocator { | 57 class FifoAllocator { |
| 58 public: | 58 public: |
| 59 // Returned by AllocatedRegion when the requested allocation cannot be | 59 // Returned by AllocatedRegion when the requested allocation cannot be |
| 60 // performed. | 60 // performed. |
| 61 static const uint64_t kNullOffset = std::numeric_limits<uint64_t>::max(); | 61 static const uint64_t kNullOffset = std::numeric_limits<uint64_t>::max(); |
| 62 | 62 |
| 63 FifoAllocator(uint64_t size); | 63 FifoAllocator(uint64_t size); |
| 64 | 64 |
| 65 ~FifoAllocator(); | 65 ~FifoAllocator(); |
| 66 | 66 |
| 67 // Returns the size of the entire buffer as determined by the call to the | 67 // Returns the size of the entire buffer as determined by the call to the |
| 68 // constructor or the most recent call to Reset. | 68 // constructor or the most recent call to Reset. |
| 69 uint64_t size() const { | 69 uint64_t size() const { return size_; } |
| 70 return size_; | |
| 71 } | |
| 72 | 70 |
| 73 // Resets the buffer manager to its initial state (no regions allocated) | 71 // Resets the buffer manager to its initial state (no regions allocated) |
| 74 // with a new buffer size. Also deletes all the regions in the lookaside. | 72 // with a new buffer size. Also deletes all the regions in the lookaside. |
| 75 void Reset(uint64_t size); | 73 void Reset(uint64_t size); |
| 76 | 74 |
| 77 // Allocates a region and returns its offset or kNullOffset if the allocation | 75 // Allocates a region and returns its offset or kNullOffset if the allocation |
| 78 // could not be performed. | 76 // could not be performed. |
| 79 uint64_t AllocateRegion(uint64_t size); | 77 uint64_t AllocateRegion(uint64_t size); |
| 80 | 78 |
| 81 // Releases a previously-allocated region. | 79 // Releases a previously-allocated region. |
| 82 void ReleaseRegion(uint64_t size, uint64_t offset); | 80 void ReleaseRegion(uint64_t size, uint64_t offset); |
| 83 | 81 |
| 84 private: | 82 private: |
| 85 // List element to track allocated and free regions. | 83 // List element to track allocated and free regions. |
| 86 struct Region { | 84 struct Region { |
| 87 bool allocated; | 85 bool allocated; |
| 88 uint64_t size; | 86 uint64_t size; |
| 89 uint64_t offset; | 87 uint64_t offset; |
| 90 | 88 |
| 91 // Intrusive list pointers. | 89 // Intrusive list pointers. |
| 92 Region* prev; | 90 Region* prev; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 Region* free_; | 136 Region* free_; |
| 139 | 137 |
| 140 // Unallocated region from which allocations are currently being made. | 138 // Unallocated region from which allocations are currently being made. |
| 141 Region* active_; | 139 Region* active_; |
| 142 }; | 140 }; |
| 143 | 141 |
| 144 } // namespace media | 142 } // namespace media |
| 145 } // namespace mojo | 143 } // namespace mojo |
| 146 | 144 |
| 147 #endif // MOJO_SERVICES_MEDIA_COMMON_CPP_FIFO_ALLOCATOR_H_ | 145 #endif // MOJO_SERVICES_MEDIA_COMMON_CPP_FIFO_ALLOCATOR_H_ |
| OLD | NEW |