# mco_flashsim_media_context
RT

> Retrieve the internal media context of a flash simulator instance for integration with eXtremeDB database.

### Prototype

        MCO_RET mco_flashsim_media_context(
            /*IN*/  mco_flashsim_h fs,
            /*OUT*/ void **context,
            /*OUT*/ mco_size_t *context_size
        );

### Arguments

`fs`
Handle to an open flash simulator instance.
`context`
Pointer to a variable that will receive the address of the simulator’s internal media context.
`context_size`
Pointer to a variable that will receive the size (in bytes) of the media context structure.

### Description

> This function provides access to the internal media context of the flash simulator, which is required when binding the simulator to an eXtremeDB persistent device. The context contains the memory device descriptor used by the simulator and is assigned to the `dev.file.media_context` field of the persistent device during database configuration.

> The returned `context` is a pointer to the simulator’s context. This allows the database runtime to interact with the simulator as if it were real flash media.

### Return Codes

`MCO_S_OK`
Media context successfully retrieved.
`MCO_E_INVALID_HANDLE`
The provided simulator handle is invalid or not initialized.
`MCO_E_ILLEGAL_PARAM`
One or both output pointers (`context`, `context_size`) are `NULL`.

### Example

        mco_device_t db_devs[3];  // memory & persistent devices for database
        mco_device_t sim_dev;    // memory device for flash simulator
        mco_db_params_t    db_params;
        mco_flashsim_params_t fsp = {0};
        mco_flashsim_h flash_handle;

        /***************** Create flash simulator *****************/
        fsp.page_size       = P_RAMFLASH_PAGE_SIZE;
        fsp.pages_per_block = P_RAMFLASH_PAGES_PER_BLOCK;
        fsp.n_blocks        = P_RAMFLASH_NUM_BLOCKS;

        sim_dev.assignment = MCO_MEMORY_ASSIGN_PERSISTENT;
        sim_dev.type       = MCO_MEMORY_CONV;
        sim_dev.size       = mco_flashsim_memory_size(&fsp); // get required memory size
        sim_dev.dev.conv.ptr = (char*)malloc(sim_dev.size);  // allocate memory
        sim_dev.dev.conv.flags = 0;

        CHECK(mco_flashsim_open(&sim_dev, &fsp, &flash_handle)); // create flash simulator, flash_handle refers

        /***************** Create database *****************/
        db_devs[0].assignment = MCO_MEMORY_ASSIGN_DATABASE;
        /*   ...fill db_devs[0]     */

        db_devs[1].assignment = MCO_MEMORY_ASSIGN_CACHE;
        /*   ...fill db_devs[1]     */

        db_devs[2].assignment = MCO_MEMORY_ASSIGN_PERSISTENT;
        db_devs[2].type       = MCO_MEMORY_FILE;
        db_devs[2].dev.file.flags = MCO_FILE_OPEN_WITH_MEDIA_CTX; // media context attached
        db_devs[2].dev.file.name[0] = '\0';    // not used
        // get context by flashsim handle
        CHECK(mco_flashsim_media_context(flash_handle, &db_devs[2].dev.file.media_context, &db_devs[2].dev.file.media_context_size));

        mco_db_params_init ( &db_params );
        /*  ...set db_params        */

        CHECK(mco_db_open_dev(db_name, perf_get_dictionary(), db_devs, 3, &db_params ));

        /***************** Use database *****************/

        /***************** Cleanup *****************/
        CHECK(mco_db_close(db_name));
        CHECK(mco_flashsim_close(flash_handle));
        /*  ...free malloc()-ed memory   */

### Files

Header file:
mcoflashsim.h
Source file:
mcoflashsim.c
Library:
libmcolib.a