#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2021 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_STORAGE_MEMORYBLOB_H #define NUCLEX_STORAGE_MEMORYBLOB_H #include "Nuclex/Storage/Config.h" #include "Nuclex/Storage/Blob.h" #include // for std::vector #include // for std::shared_mutex namespace Nuclex { namespace Storage { // ------------------------------------------------------------------------------------------- // /// Chunk of arbitrary data stored completely in system memory class MemoryBlob : public Blob { /// Initializes a new in-memory blob public: NUCLEX_STORAGE_API MemoryBlob() = default; /// Frees all resources owned by the instance public: NUCLEX_STORAGE_API virtual ~MemoryBlob() override = default; /// Determines the size of the binary data in bytes /// The size of the binary data in bytes public: NUCLEX_STORAGE_API std::uint64_t GetSize() const override { this->mutex.lock_shared(); std::size_t size = this->memory.size(); this->mutex.unlock_shared(); return size; } /// Reads raw data from the blob /// Absolute position data will be read from /// Buffer into which data will be read /// Number of bytes that will be read public: NUCLEX_STORAGE_API void ReadAt( std::uint64_t location, void *buffer, std::size_t count ) const override; /// Writes raw data into the blob /// Absolute position data will be written to /// Buffer from which data will be taken /// Number of bytes that will be written public: NUCLEX_STORAGE_API void WriteAt( std::uint64_t location, const void *buffer, std::size_t count ) override; /// Flushes all caches that may be chained to the blob public: NUCLEX_STORAGE_API void Flush() override {} /// Stores the data of the in-memory blob private: std::vector memory; /// Mutex used to sequentialize accesses to the memory buffer /// /// Shared access = read or write to the buffer without reallocating /// Exclusive access = reallocate the buffer for a read or a write /// private: mutable std::shared_mutex mutex; }; // ------------------------------------------------------------------------------------------- // }} // namespace Nuclex::Storage #endif // NUCLEX_STORAGE_MEMORYBLOB_H