1 // 2 // Copyright © 2019 Arm Ltd. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 // 5 6 #pragma once 7 8 #include "IBufferManager.hpp" 9 #include "IConsumer.hpp" 10 11 #if !defined(ARMNN_DISABLE_THREADS) 12 #include <mutex> 13 #endif 14 15 #include <vector> 16 #include <queue> 17 18 namespace arm 19 { 20 21 namespace pipe 22 { 23 24 class BufferManager : public IBufferManager 25 { 26 public: 27 BufferManager(unsigned int numberOfBuffers = 5, unsigned int maxPacketSize = 4096); 28 ~BufferManager()29 ~BufferManager() {} 30 31 IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override; 32 33 void Reset(); 34 35 void Commit(IPacketBufferPtr& packetBuffer, unsigned int size, bool notifyConsumer = true) override; 36 37 void Release(IPacketBufferPtr& packetBuffer) override; 38 39 IPacketBufferPtr GetReadableBuffer() override; 40 41 void MarkRead(IPacketBufferPtr& packetBuffer) override; 42 43 /// Set Consumer on the buffer manager to be notified when there is a Commit 44 /// Can only be one consumer 45 void SetConsumer(IConsumer* consumer) override; 46 47 /// Notify the Consumer buffer can be read 48 void FlushReadList() override; 49 50 private: 51 void Initialize(); 52 53 // Maximum buffer size 54 unsigned int m_MaxBufferSize; 55 // Number of buffers 56 const unsigned int m_NumberOfBuffers; 57 const unsigned int m_MaxNumberOfBuffers; 58 unsigned int m_CurrentNumberOfBuffers; 59 60 // List of available packet buffers 61 std::vector<IPacketBufferPtr> m_AvailableList; 62 63 // List of readable packet buffers 64 std::queue<IPacketBufferPtr> m_ReadableList; 65 66 #if !defined(ARMNN_DISABLE_THREADS) 67 // Mutex for available packet buffer list 68 std::mutex m_AvailableMutex; 69 70 // Mutex for readable packet buffer list 71 std::mutex m_ReadableMutex; 72 #endif 73 74 // Consumer thread to notify packet is ready to read 75 IConsumer* m_Consumer = nullptr; 76 }; 77 78 } // namespace pipe 79 80 } // namespace arm 81