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 #ifndef MOJO_SERVICES_MEDIA_COMMON_CPP_MAPPED_SHARED_MEDIA_BUFFER_ALLOCATOR_H_ |
| 6 #define MOJO_SERVICES_MEDIA_COMMON_CPP_MAPPED_SHARED_MEDIA_BUFFER_ALLOCATOR_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <mutex> // NOLINT(build/c++11) |
| 10 |
| 11 #include "mojo/services/media/common/cpp/fifo_allocator.h" |
| 12 #include "mojo/services/media/common/cpp/mapped_shared_buffer.h" |
| 13 |
| 14 namespace mojo { |
| 15 namespace media { |
| 16 |
| 17 // SharedMediaBufferAllocator enhances MappedSharedBuffer by adding allocation |
| 18 // semantics (allocate and release) using FifoAllocator. This is useful in media |
| 19 // applications in which media buffers are typically allocated and released in |
| 20 // a first-allocated, first-released manner. SharedMediaBufferAllocator is |
| 21 // thread-safe. |
| 22 class SharedMediaBufferAllocator : public MappedSharedBuffer { |
| 23 public: |
| 24 static const uint64_t kNullOffset = FifoAllocator::kNullOffset; |
| 25 |
| 26 SharedMediaBufferAllocator() : fifo_allocator_(0) {} |
| 27 |
| 28 ~SharedMediaBufferAllocator() override; |
| 29 |
| 30 // Allocates a region of the buffer returning an offset. If the requested |
| 31 // region could not be allocated, returns kNullOffset. |
| 32 uint64_t AllocateRegionByOffset(uint64_t size) { |
| 33 std::lock_guard<std::mutex> lock(lock_); |
| 34 return fifo_allocator_.AllocateRegion(size); |
| 35 } |
| 36 |
| 37 // Releases a region of the buffer previously allocated by calling |
| 38 // AllocateRegionByOffset. |
| 39 void ReleaseRegionByOffset(uint64_t size, uint64_t offset) { |
| 40 std::lock_guard<std::mutex> lock(lock_); |
| 41 fifo_allocator_.ReleaseRegion(size, offset); |
| 42 } |
| 43 |
| 44 // Allocates a region of the buffer returning a pointer. If the requested |
| 45 // region could not be allocated, returns nullptr. |
| 46 void* AllocateRegion(uint64_t size) { |
| 47 return PtrFromOffset(AllocateRegionByOffset(size)); |
| 48 } |
| 49 |
| 50 // Releases a region of the buffer previously allocated by calling |
| 51 // AllocateRegion. |
| 52 void ReleaseRegion(uint64_t size, void* ptr) { |
| 53 ReleaseRegionByOffset(size, OffsetFromPtr(ptr)); |
| 54 } |
| 55 |
| 56 protected: |
| 57 void OnInit() override; |
| 58 |
| 59 private: |
| 60 mutable std::mutex lock_; |
| 61 FifoAllocator fifo_allocator_; |
| 62 }; |
| 63 |
| 64 } // namespace media |
| 65 } // namespace mojo |
| 66 |
| 67 #endif // MOJO_SERVICES_MEDIA_COMMON_CPP_MAPPED_SHARED_MEDIA_BUFFER_ALLOCATOR_H_ |
OLD | NEW |