BufferedBlockDevice
BufferedBlockDevice class hierarchy
The BufferedBlockDevice class is a block device adpator, whose purpose is to reduce the read and program sizes of the underlying block device to 1. Large read and/or program sizes may make life difficult for block device users, so BufferedBlockDevice reduces both sizes to the minimum, where reads and writes to the underlying BD use an internal buffer. Calling the sync
API ensures writes are flushed to the underlying BD.
The constructor only requires the underlying block device pointer.
- bd - Block device to back the BufferedBlockDevice.
To configure this class, please see our BlockDevice configuration documentation.
BufferedBlockDevice class reference
Public Member Functions | |
BufferedBlockDevice (BlockDevice *bd) | |
Lifetime of a memory-buffered block device wrapping an underlying block device. More... | |
virtual | ~BufferedBlockDevice () |
Lifetime of the memory-buffered block device. More... | |
virtual int | init () |
Initialize a buffered-memory block device and its underlying block device. More... | |
virtual int | deinit () |
Deinitialize the buffered-memory block device and its underlying block device. More... | |
virtual int | sync () |
Ensure that data on the underlying storage block device is in sync with the memory-buffered block device. More... | |
virtual int | read (void *buffer, bd_addr_t addr, bd_size_t size) |
Read blocks from the memory-buffered block device. More... | |
virtual int | program (const void *buffer, bd_addr_t addr, bd_size_t size) |
Program data to the memory-buffered block device. More... | |
virtual int | erase (bd_addr_t addr, bd_size_t size) |
Erase blocks from the memory-buffered block device. More... | |
virtual int | trim (bd_addr_t addr, bd_size_t size) |
Mark blocks as no longer in use. More... | |
virtual bd_size_t | get_read_size () const |
Get the size of a readable block. More... | |
virtual bd_size_t | get_program_size () const |
Get the size of a programmable block. More... | |
virtual bd_size_t | get_erase_size () const |
Get the size of an erasable block. More... | |
virtual bd_size_t | get_erase_size (bd_addr_t addr) const |
Get the size of an erasable block of a given address. More... | |
virtual int | get_erase_value () const |
Get the value of storage data after being erased. More... | |
virtual bd_size_t | size () const |
Get the total size of the underlying device. More... | |
virtual const char * | get_type () const |
Get the underlying BlockDevice class type. More... | |
virtual bool | is_valid_read (bd_addr_t addr, bd_size_t size) const |
Convenience function for checking block read validity. More... | |
virtual bool | is_valid_program (bd_addr_t addr, bd_size_t size) const |
Convenience function for checking block program validity. More... | |
virtual bool | is_valid_erase (bd_addr_t addr, bd_size_t size) const |
Convenience function for checking block erase validity. More... |
Static Public Member Functions | |
static BlockDevice * | get_default_instance () |
Return the default block device. More... |
BufferedBlockDevice example
This BufferedBlockDevice example takes a HeapBlockDevice, whose read size is 256 bytes and program size is 512 bytes, and shows how one can read or program this block device with much smaller read/program sizes, using BufferedBlockDevice.
HeapBlockDevice heap_bd(1024, 256, 512, 512);
BufferedBlockDevice buf_bd(&heap_bd);
// This initializes the buffered block device (as well as the underlying heap block device)
int err = buf_bd.init();
uint8_t buf[8];
for (int i = 0; i < sizeof(buf); i++) {
buf[i] = i;
}
// Now we can program an 8 byte buffer (couldn't do that in underlying BD, having 512-byte program size)
err = buf_bd.program(buf, 0, sizeof(buf));
// Now we can also read one byte
err = buf_bd.read(buf, 0, 1);
// Ensure programmed data is flushed to the underlying block device
err = buf_bd.sync();
Related content
- HeapBlockDevice API reference.
- BlockDevice configuration documentation.