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.
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "BufferedBlockDevice.h"
#include "HeapBlockDevice.h"
#include <cstdio>
int main()
{
// Define a HeapBlockDevice with program size 512 bytes and read size 256
HeapBlockDevice heap_bd(1024, 256, 512, 512);
// BufferedBlockDevice is used to program or read a much smaller amount of data than the minimum program or read size supported by the underlying block device
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 (uint8_t i = 0; i < sizeof(buf); i++) {
buf[i] = i;
}
// Now we can program an 8-byte buffer (we couldn't do that in the underlying block device, which had a 512-byte program size)
err = buf_bd.program(buf, 0, sizeof(buf));
// Now read any amount of data from any address (for example, the last three bytes of data from address 5)
memset(buf, 0, 8);
err = buf_bd.read(buf, 5, 3);
printf("Read Data (Expected 5,6,7): %d %d %d\n", (int)(buf[0]), (int)(buf[1]), (int)(buf[2]));
// Ensure programmed data is flushed to the underlying block device
err = buf_bd.sync();
// Finally, deinint the BufferedBlockDevice (which also deinits its underlying block device)
err = buf_bd.deinit();
}
Related content
- HeapBlockDevice API reference.
- BlockDevice configuration documentation.