Mistake on this page?
Report an issue in GitHub or email us


SlicingBlockDevice class hierarchy

The SlicingBlockDevice class provides a way to break up a block device into subunits without the need to manually track offsets. The SlicingBlockDevice acts as an opposite of the ChainingBlockDevice class.

The constructor takes in the master block device pointer and the start and end addresses of where you would like to partition the sub-block. By not specifying the end address, you create a block device that spans from the provided start address to the end of the underlying block device.

  • bd - Block device to back the SlicingBlockDevice.
  • start - Start block address to map to block 0. Negative addresses are calculated from the end of the underlying block device.
  • end - End block address to mark the end of the block device. This block is not mapped; negative addresses are calculated from the end of the underlying block device.

SlicingBlockDevice class reference

Public Member Functions
 SlicingBlockDevice (BlockDevice *bd, bd_addr_t start, bd_addr_t end=0)
virtual ~SlicingBlockDevice ()
virtual int init ()
virtual int deinit ()
virtual int sync ()
virtual int read (void *buffer, bd_addr_t addr, bd_size_t size)
virtual int program (const void *buffer, bd_addr_t addr, bd_size_t size)
virtual int erase (bd_addr_t addr, bd_size_t size)
virtual bd_size_t get_read_size () const
virtual bd_size_t get_program_size () const
virtual bd_size_t get_erase_size () const
virtual bd_size_t get_erase_size (bd_addr_t addr) const
virtual int get_erase_value () const
virtual bd_size_t size () const
 Public Member Functions inherited from BlockDevice
virtual ~BlockDevice ()
virtual int trim (bd_addr_t addr, bd_size_t size)
bool is_valid_read (bd_addr_t addr, bd_size_t size) const
bool is_valid_program (bd_addr_t addr, bd_size_t size) const
bool is_valid_erase (bd_addr_t addr, bd_size_t size) const
Protected Attributes
bool _start_from_end
bd_size_t _start
bool _stop_from_end
bd_size_t _stop

SlicingBlockDevice example

This SlicingBlockDevice example partitions a HeapBlockDevice into three subunits and showcases programming and reading back data segments through both the underlying master block device and the sliced subunits.

#include "mbed.h"
#include "HeapBlockDevice.h"
#include "SlicingBlockDevice.h"

#define BLOCK 512
#define NUMSLICES 3

char buff[BLOCK];

int main(void) {
    // Create a block device with 64 blocks of size 512
    HeapBlockDevice mem(64*BLOCK, BLOCK);

    SlicingBlockDevice slices[NUMSLICES] = {
        // Create a block device that maps to the first 32 blocks
        SlicingBlockDevice(&mem, 0 *BLOCK, 32*BLOCK),
        // Create a block device that maps to the middle 32 blocks
        SlicingBlockDevice(&mem, 16*BLOCK, -16*BLOCK),

        // Create a block device that maps to the last 32 blocks
        SlicingBlockDevice(&mem, 32*BLOCK)

    for (int i = 0; i < NUMSLICES; i++) {
        // Initialize and erase the slice to prepar for programming
        slices[i].erase(0, 32*BLOCK);
        // Construct the message for the block and write to the slice
        sprintf((char *)&buff, "test: %d", i);
        slices[i].program(&buff, 0, BLOCK);

    for (int i = 0; i < NUMSLICES; i++) {
        // Read back the programmed blocks through the underlying block device
        mem.read(&buff, (i * 16 * BLOCK), BLOCK);
        printf("%s  --> ", buff);
        // Read back the programmed blocks through the sliced block device.
        slices[i].read(&buff,0, BLOCK);
        printf("%s\r\n", buff);
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.