tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MemoryBlockCache.h (3095B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef MEMORY_BLOCK_CACHE_H_
      8 #define MEMORY_BLOCK_CACHE_H_
      9 
     10 #include "MediaBlockCacheBase.h"
     11 #include "mozilla/Mutex.h"
     12 
     13 namespace mozilla {
     14 
     15 // Manages block management for the media cache. Data comes in over the network
     16 // via callbacks on the main thread, however we don't want to write the
     17 // incoming data to the media cache on the main thread, as this could block
     18 // causing UI jank.
     19 //
     20 // So MediaBlockCacheBase provides an abstraction for a temporary memory buffer
     21 // as an array of blocks, which supports a block move operation, and
     22 // allows synchronous reading and writing from any thread.
     23 //
     24 // Writes and cache block moves (which require reading) may be deferred to
     25 // their own non-main thread. This object also ensures that data which has
     26 // been scheduled to be written, but hasn't actually *been* written, is read
     27 // as if it had, i.e. pending writes are cached in readable memory until
     28 // they're flushed to file.
     29 //
     30 // To improve efficiency, writes can only be done at block granularity,
     31 // whereas reads can be done with byte granularity.
     32 class MemoryBlockCache : public MediaBlockCacheBase {
     33 public:
     34  explicit MemoryBlockCache(int64_t aContentLength);
     35 
     36 protected:
     37  virtual ~MemoryBlockCache();
     38 
     39 public:
     40  // Allocate initial buffer.
     41  // If re-initializing, clear buffer.
     42  virtual nsresult Init() override;
     43 
     44  void Flush() override;
     45 
     46  // Maximum number of blocks allowed in this block cache.
     47  // Based on initial content length, and minimum usable block cache.
     48  size_t GetMaxBlocks(size_t) const override { return mMaxBlocks; }
     49 
     50  // Can be called on any thread.
     51  virtual nsresult WriteBlock(uint32_t aBlockIndex, Span<const uint8_t> aData1,
     52                              Span<const uint8_t> aData2) override;
     53 
     54  // Synchronously reads data from buffer.
     55  nsresult Read(int64_t aOffset, uint8_t* aData, int32_t aLength) override;
     56 
     57  // Moves a block. Can be called on any thread.
     58  virtual nsresult MoveBlock(int32_t aSourceBlockIndex,
     59                             int32_t aDestBlockIndex) override;
     60 
     61 private:
     62  static size_t BlockIndexToOffset(uint32_t aBlockIndex) {
     63    return static_cast<size_t>(aBlockIndex) * BLOCK_SIZE;
     64  }
     65 
     66  // Ensure the buffer has at least a multiple of BLOCK_SIZE that can contain
     67  // aContentLength bytes. Buffer size can only grow.
     68  // Returns false if allocation failed.
     69  bool EnsureBufferCanContain(size_t aContentLength);
     70 
     71  // Initial content length.
     72  const size_t mInitialContentLength;
     73 
     74  // Maximum number of blocks that this MemoryBlockCache expects.
     75  const size_t mMaxBlocks;
     76 
     77  // Mutex which controls access to all members below.
     78  Mutex mMutex MOZ_UNANNOTATED;
     79 
     80  nsTArray<uint8_t> mBuffer;
     81  bool mHasGrown = false;
     82 };
     83 
     84 }  // End namespace mozilla.
     85 
     86 #endif /* MEMORY_BLOCK_CACHE_H_ */