BlockDevice.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * BlockkDevice.h
8  *
9  ****/
10 #pragma once
11 
12 #include <Storage/Device.h>
13 #include "Buffer.h"
14 #include <map>
15 
16 namespace Storage::Disk
17 {
33 class BlockDevice : public Device
34 {
35 public:
36  bool read(storage_size_t address, void* dst, size_t size) override;
37  bool write(storage_size_t address, const void* src, size_t size) override;
38  bool erase_range(storage_size_t address, storage_size_t size) override;
39 
40  size_t getBlockSize() const override
41  {
42  return sectorSize;
43  }
44 
45  storage_size_t getSize() const override
46  {
47  return sectorCount << sectorSizeShift;
48  }
49 
50  storage_size_t getSectorCount() const override
51  {
52  return sectorCount;
53  }
54 
55  bool sync() override;
56 
65  bool allocateBuffers(unsigned numBuffers);
66 
67  struct Stat {
68  enum Function { read, write, erase };
69  struct Func {
70  uint32_t count[2]{}; // Hit, Miss
71 
72  uint32_t totalCount() const
73  {
74  return count[0] + count[1];
75  }
76 
77  size_t printTo(Print& p) const;
78  };
79  Func func[3];
80  std::map<uint32_t, Func> sectors;
81 
82  void update(Function fn, uint32_t sector, uint32_t cacheSector);
83  size_t printTo(Print& p) const;
84  };
86 
87 protected:
88  virtual bool raw_sector_read(storage_size_t address, void* dst, size_t size) = 0;
89  virtual bool raw_sector_write(storage_size_t address, const void* src, size_t size) = 0;
90  virtual bool raw_sector_erase_range(storage_size_t address, size_t size) = 0;
91  virtual bool raw_sync() = 0;
92 
93  bool flushBuffer(Buffer& buf);
94  bool flushBuffers();
95 
96  std::unique_ptr<BufferList> buffers;
97  uint64_t sectorCount{0};
100 };
101 
102 } // namespace Storage::Disk
uint32_t storage_size_t
Definition: Components/Storage/src/include/Storage/Types.h:19
Provides formatted output to stream.
Definition: Print.h:37
Represents a storage device (e.g. flash memory)
Definition: Components/Storage/src/include/Storage/Device.h:34
static constexpr uint16_t defaultSectorSize
Definition: Components/Storage/src/include/Storage/Device.h:183
Base class for sector-addressable (block) devices.
Definition: BlockDevice.h:34
uint8_t sectorSizeShift
Definition: BlockDevice.h:99
bool flushBuffer(Buffer &buf)
bool write(storage_size_t address, const void *src, size_t size) override
Write data to the storage device.
uint16_t sectorSize
Definition: BlockDevice.h:98
size_t getBlockSize() const override
Obtain smallest allocation unit for erase operations.
Definition: BlockDevice.h:40
Stat stat
Definition: BlockDevice.h:85
virtual bool raw_sync()=0
storage_size_t getSectorCount() const override
Obtain total number of sectors on this device.
Definition: BlockDevice.h:50
bool read(storage_size_t address, void *dst, size_t size) override
Read data from the storage device.
std::unique_ptr< BufferList > buffers
Definition: BlockDevice.h:96
uint64_t sectorCount
Definition: BlockDevice.h:97
storage_size_t getSize() const override
Obtain addressable size of this device.
Definition: BlockDevice.h:45
virtual bool raw_sector_read(storage_size_t address, void *dst, size_t size)=0
bool sync() override
Flush any pending writes to the physical media.
bool allocateBuffers(unsigned numBuffers)
Set number of sector buffers to use.
virtual bool raw_sector_write(storage_size_t address, const void *src, size_t size)=0
bool erase_range(storage_size_t address, storage_size_t size) override
Erase a region of storage in preparation for writing.
virtual bool raw_sector_erase_range(storage_size_t address, size_t size)=0
Definition: Partition.h:78
constexpr std::enable_if<(sizeof(T)<=4), uint8_t >::type getSizeBits(T value)
Definition: Components/Storage/src/include/Storage/Types.h:49
Definition: BlockDevice.h:69
uint32_t count[2]
Definition: BlockDevice.h:70
uint32_t totalCount() const
Definition: BlockDevice.h:72
Definition: BlockDevice.h:67
size_t printTo(Print &p) const
Function
Definition: BlockDevice.h:68
@ read
Definition: BlockDevice.h:68
@ write
Definition: BlockDevice.h:68
@ erase
Definition: BlockDevice.h:68
std::map< uint32_t, Func > sectors
By sector.
Definition: BlockDevice.h:80
Func func[3]
Read, Write, Erase.
Definition: BlockDevice.h:79
void update(Function fn, uint32_t sector, uint32_t cacheSector)
Definition: DiskStorage/src/include/Storage/Disk/Buffer.h:17