# mco_db_extend_dev_ctx
RT

Extend database memory by adding a memory device.

### Prototype

        MCO_RET	mco_db_extend_dev_ctx(	/*IN*/ mco_trans_h  t,
                        /*IN*/ mco_device_t * dev,
                        /*IN*/ void const* ctx);

### Arguments

t | The current transaction handle
dev | The device(s) to be added
ctx | The address of database context data or a call to `mco_db_connection_context()` which returns the address of the context associated with a connection to an existing database

### Description

> This function extends the available database memory by adding the specified memory device. Typically it is used when a database write operation fails due to low memory.

### Return Codes

MCO_S_OK | The database was created successfully
MCO_E_NOINSTANCE | The specified database is not opened
MCO_ERR_TRN | A database transaction error occurred. (eg. The database is already opened by some other thread that has an open transaction.)
MCO_E_ILLEGAL_PARAM | Returned if the device size is too small. The minimum size is runtime configuration- and hardware architecture-dependent. On an x64 platform and configured for 100 connections (default) the minimum size is a little over 1K (1104 bytes) .

### Example

        Application snippet:

        const char * dbname = "SimpleDb";

        int main(int argc, char* argv[])
        {
            mco_db_h db;
            MCO_RET rc;
            char *mem = malloc( DBSIZE );
            uint4 total_pages;
            uint4 free_pages;

            if( (rc = mco_runtime_start()) != MCO_S_OK)
                exit(-1);

            rc = mco_db_open( dbname, simpledb_get_dictionary(), mem, DBSIZE, (uint2)PAGESIZE );
            ...
            rc = mco_db_connect(dbname, &db);
            ...

            /* If low memory situation detected (see "Auxiliary APIs:  Statistics") */
            mco_db_free_pages(db, &freepages);
            mco_db_total_pages(db, &totalpages );
            if(freepages  < totalpages / 10)
            {
                // yes.  Extend the database
                rc =  mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
                while ( MAX_SEGMENTS > n_segments )
                {
                    dev[n_segments].type         = MCO_MEMORY_CONV;
                    dev[n_segments].assignment   = MCO_MEMORY_ASSIGN_DATABASE;
                    dev[n_segments].size         = SEGMENT_SIZE;
                    dev[n_segments].dev.conv.ptr = (void*)malloc( SEGMENT_SIZE );
                    rc = mco_db_extend_dev_ctx(t, &dev[n_segments], mco_db_connection_context());

                    if ( MCO_S_OK != rc ) break;  /* if extend failed exit loop */
                    n_segments++;
                }
                if ( MCO_S_OK != rc )
                    mco_trans_rollback();
                else
                    mco_trans_commit();
                ...
            }
        }

### Files

Header file:
mco.h
Source file:
mcodb.c
Library:
libmcolib.a