# Extending Database Memory in C

> A C application can extend the amount of memory for use by a database by calling `mco_db_extend_dev()` to add a new memory device. The following code snippet demonstrates the recommended way for C applications to extend a database:

>         int extend_db_memory(const char * dbname)
>         {
>             MCO_RET         rc;
>             int 		 	 size = 1*1024*1024;
>             mco_device_t    extdev;

>             /* set extended memory region parameters */
>             extdev.type         = MCO_MEMORY_CONV;
>             extdev.assignment   = MCO_MEMORY_ASSIGN_DATABASE;
>             extdev.size         = size;
>             extdev.dev.conv.ptr = (void*)malloc( extdev.size );

>             if ( mco_db_extend_dev( dbname, &extdev ) == MCO_OK )
>             {
>                 /* the database RAM pool has been successfully increased */
>                 ...
>             }
>             return rc;
>         }

> The structure member `mco_runtime_info_t.max_extends` holds the maximum number of database extensions allowed (generally only applicable to databases in shared memory). Its value is -1 (which means "unlimited") for databases in conventional memory and persistent databases.

> ## Backward-compatibility

> For backward compatibility, the old-style `mco_db_extend()` API used in previous versions (prior to 4.0) of eXtremeDB is also provided to extend an all-in-memory database (but not a shared memory database) as illustrated in the following code snippet:

>         int extend_db_memory(const char * dbname)
>         {
>             MCO_RET         rc;
>             int 		        size = 1*1024*1024;
>             void *		 memory = malloc( size );

>             if ( mco_db_extend( dbname, memory, size ) == MCO_OK )
>             {
>                 /* the database memory size was increased */
>                 ...
>             }
>             return rc;
>         }

> ## Extending memory from within a transaction

> If database memory needs to be extended from within a transaction, for example when a write operation fails due to not enough memory, use the following alternate versions:

>         MCO_RET mco_db_extend_dev_t( /*IN*/ mco_trans_h  t, /*IN*/ mco_device_t * dev );

> or

>         MCO_RET mco_db_extend_t( /*IN*/ mco_trans_h  t, /*IN*/ void *start_ptr, /*IN*/ uint4 size );

> passing the current active transaction handle.