Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef EncodedBufferCache_h_ |
michael@0 | 8 | #define EncodedBufferCache_h_ |
michael@0 | 9 | |
michael@0 | 10 | #include "nsCOMPtr.h" |
michael@0 | 11 | #include "nsTArray.h" |
michael@0 | 12 | #include "mozilla/Mutex.h" |
michael@0 | 13 | |
michael@0 | 14 | struct PRFileDesc; |
michael@0 | 15 | class nsIDOMBlob; |
michael@0 | 16 | |
michael@0 | 17 | namespace mozilla { |
michael@0 | 18 | |
michael@0 | 19 | class ReentrantMonitor; |
michael@0 | 20 | /** |
michael@0 | 21 | * Data is moved into a temporary file when it grows beyond |
michael@0 | 22 | * the maximal size passed in the Init function. |
michael@0 | 23 | * The AppendBuffer and ExtractBlob methods are thread-safe and can be called on |
michael@0 | 24 | * different threads at the same time. |
michael@0 | 25 | */ |
michael@0 | 26 | class EncodedBufferCache |
michael@0 | 27 | { |
michael@0 | 28 | public: |
michael@0 | 29 | EncodedBufferCache(uint32_t aMaxMemoryStorage) |
michael@0 | 30 | : mFD(nullptr), |
michael@0 | 31 | mMutex("EncodedBufferCache.Data.Mutex"), |
michael@0 | 32 | mDataSize(0), |
michael@0 | 33 | mMaxMemoryStorage(aMaxMemoryStorage), |
michael@0 | 34 | mTempFileEnabled(false) { } |
michael@0 | 35 | ~EncodedBufferCache() |
michael@0 | 36 | { |
michael@0 | 37 | } |
michael@0 | 38 | // Append buffers in cache, check if the queue is too large then switch to write buffer to file system |
michael@0 | 39 | // aBuf will append to mEncodedBuffers or temporary File, aBuf also be cleared |
michael@0 | 40 | void AppendBuffer(nsTArray<uint8_t> & aBuf); |
michael@0 | 41 | // Read all buffer from memory or file System, also Remove the temporary file or clean the buffers in memory. |
michael@0 | 42 | already_AddRefed<nsIDOMBlob> ExtractBlob(const nsAString &aContentType); |
michael@0 | 43 | |
michael@0 | 44 | private: |
michael@0 | 45 | //array for storing the encoded data. |
michael@0 | 46 | nsTArray<nsTArray<uint8_t> > mEncodedBuffers; |
michael@0 | 47 | // File handle for the temporary file |
michael@0 | 48 | PRFileDesc* mFD; |
michael@0 | 49 | // Used to protect the mEncodedBuffer for avoiding AppendBuffer/Consume on different thread at the same time. |
michael@0 | 50 | Mutex mMutex; |
michael@0 | 51 | // the current buffer size can be read |
michael@0 | 52 | uint64_t mDataSize; |
michael@0 | 53 | // The maximal buffer allowed in memory |
michael@0 | 54 | uint32_t mMaxMemoryStorage; |
michael@0 | 55 | // indicate the buffer is stored on temporary file or not |
michael@0 | 56 | bool mTempFileEnabled; |
michael@0 | 57 | }; |
michael@0 | 58 | |
michael@0 | 59 | } // namespace mozilla |
michael@0 | 60 | |
michael@0 | 61 | #endif |