# mco_db_extend_dev
RT

> Extend database memory by adding a new memory device.

### Prototype

        MCO_RET mco_db_extend_dev(
            /*IN*/ const char *dbname,
            /*IN*/ mco_device_t *dev
        );

### Arguments

`dbname`
Name of the open database to extend.
`dev`
Pointer to a configured `mco_device_t` structure describing the memory device to add.

### Description

> This function dynamically increases the available memory of an already opened database by attaching an additional memory segment (device). It is typically used in response to low-memory conditions detected via statistics APIs (e.g., when free pages fall below a threshold).

> The database must be opened and not have any active transactions from other threads during the extension. The added memory becomes part of the database’s addressable space and can be used for new object allocations.

### Return Codes

`MCO_S_OK`
Memory device successfully added to the database.
`MCO_E_NOINSTANCE`
The specified database is not currently open.
`MCO_ERR_TRN`
A transaction is active in another thread, preventing the extension.
`MCO_E_ILLEGAL_PARAM`
The provided device size is too small. Minimum size depends on runtime configuration and architecture (e.g., ~1104 bytes on x64 with default settings).

### Example

        const char * dbname = "SimpleDb";

        int main(int argc, char* argv[])
        {
            mco_db_h db;
            MCO_RET rc;
            mco_device_t       dev;
            int                n_segments = 0;
            char *mem = malloc( DBSIZE );
            ...

            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
                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(dbname, &dev[n_segments]);

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

### Files

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