Esempio n. 1
0
/* set tree to use given mempool or allocate a new one if pool is NULL */
int cp_avltree_use_mempool(cp_avltree *tree, cp_mempool *pool)
{
	int rc = 0;
	
	if ((rc = cp_avltree_txlock(tree, COLLECTION_LOCK_WRITE))) return rc;
	
	if (pool)
	{
		if (pool->item_size < sizeof(cp_avlnode))
		{
			rc = EINVAL;
			goto DONE;
		}
		if (tree->mempool) 
		{
			if (tree->items) 
			{
				rc = ENOTEMPTY;
				goto DONE;
			}
			cp_mempool_destroy(tree->mempool);
		}
		cp_mempool_inc_refcount(pool);
		tree->mempool = pool;
	}
	else
	{
		tree->mempool = 
			cp_mempool_create_by_option(COLLECTION_MODE_NOSYNC, 
										sizeof(cp_avlnode), 0);
		if (tree->mempool == NULL) 
		{
			rc = ENOMEM;
			goto DONE;
		}
	}

DONE:
	cp_avltree_txunlock(tree);
	return rc;
}
Esempio n. 2
0
int main()
{
	cp_mempool *node_pool;
    node_pool = cp_mempool_create_by_option(COLLECTION_MODE_NOSYNC, 
                                            sizeof(node), 20);

	node* n1, *n2, *n3, *n4, *n5;

    n1 = (node *) cp_mempool_alloc(node_pool);
	printMemPoolInfo( node_pool );

    n2 = (node *) cp_mempool_alloc(node_pool);
	printMemPoolInfo( node_pool );

    n3 = (node *) cp_mempool_alloc(node_pool);
	printMemPoolInfo( node_pool );

    n4 = (node *) cp_mempool_alloc(node_pool);
	printMemPoolInfo( node_pool );

    n5 = (node *) cp_mempool_alloc(node_pool);
	printMemPoolInfo( node_pool );
	
	cp_mempool_free(node_pool, n1);
	printMemPoolInfo( node_pool );

	cp_mempool_free(node_pool, n3);
	printMemPoolInfo( node_pool );

	cp_mempool_free(node_pool, n5);
	printMemPoolInfo( node_pool );
	
    n2 = (node *) cp_mempool_alloc(node_pool);
	printMemPoolInfo( node_pool );

    n2 = (node *) cp_mempool_alloc(node_pool);
	printMemPoolInfo( node_pool );

    n2 = (node *) cp_mempool_alloc(node_pool);
	printMemPoolInfo( node_pool );
}
Esempio n. 3
0
lkfqCrQueue(
        lkfq_tc_t* pQ,
        U16        nBlockSize,
        U32        nQueue,
        U16        nChunkBlock,
        U16        nPages,
        BOOL       bLock)
{
    tresult_t  _result;

    CCURASSERT(pQ);

    do
    {
        _result = lfds611_queue_new(
                        &(pQ->tQAlloc.pRbQs), nQueue);
        if(!_result )
        {
            _result = ENOMEM;
            break;
        }
        _result = lfds611_queue_new(
                        &(pQ->tQFree.pRbQs), nQueue);
        if(!_result )
        {
            _result = ENOMEM;
            break;
        }
        if(bLock)
            pQ->bLock = TRUE;
        _result = pthread_mutex_init (
                &(pQ->tQAlloc.tLock), NULL);
        if(ESUCCESS != _result)
        {
            _result = EFAILURE;
            break;
        }
        _result = pthread_mutex_init (
                &(pQ->tQFree.tLock), NULL);
        if(ESUCCESS != _result)
        {
            _result = EFAILURE;
            break;
        }

        pQ->pMp =
            cp_mempool_create_by_option(CP_SHARED_MEMPOOL_TYPE_1, nBlockSize, 100);
        if(NULL == pQ->pMp)
        {
            _result = ENOMEM;
            pQ->pMp = NULL;
            break;
        }
        _result = ESUCCESS;
    }while(FALSE);

    if(ESUCCESS != _result)
    {
        lkfqDestQueue(pQ);
    }
    return _result;
}