# mco_register_alloc_callback

> Register a callback function that is called by the runtime when it detects low memory conditions.

### Prototype

        void	mco_register_alloc_callback(	/*IN*/ mco_db_h db,
                           /*IN*/ mco_alloc_callback_t callback,
                           /*IN*/ mco_alloc_callback_threshold_kind_t threshold_kind,
                           /*IN*/ mco_offs_t threshold_value );

### Arguments

db | The database handle that was established by `mco_db_connect()`.
callback | An allocation handler callback function called by the runtime to determine memory allocation status.
threshold_kind | An allocation threshold kind indicating the kind of threshold to be applied.
threshold_value | The value to set the threshold.

### Description

> Register a callback function that is called by the runtime when it detects low memory conditions so that the application can react accordingly. The callback function will be called depending on threshold conditions specified in parameters `threshold_kind` and `threshold_value`. When it is necessary to pass any application specific data into the callback, this is done through the connection context created via `mco_db_connect_ctx()`.

> The callback registration is per connection and the callback function is called when memory is allocated from the connection that registered the callback. Important note: applications must not free the database memory from the callback itself. Instead it is recommended to either notify a different garbage collection thread, or simply set a "global" flag by the callback function and periodically check the flag from the main application thread.

### Return Codes

void | No value returned.

### Example

        Application snippet:

        const char * dbname = "SimpleDb";

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

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

            rc = mco_db_open( dbname, simple_get_dictionary(), mem, DBSIZE, (uint2)PAGESIZE );

            if( (MCO_S_OK == rc)
            {
                rc = mco_db_connect(  dbname, &db );
                ...

                /* start a thread to manage the garbage collection procedure */
                sample_start_connected_task( &task, gc_task, dbname, 0 );

                /* register alloc callback */
                mco_register_alloc_callback( db, alloc_callback,

                                 MCO_ALLOC_USED, DATABASE_SIZE / 2 );

                /* connect to database */
                rc = mco_db_connect(dbName, &db);
                ...
            }
            ...
        }

### Files

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