FreeBSD/sys/dev/nxge/xgehal/xgehal-mm.c
/*
 * xge_hal_mempool_create
 * @memblock_size:
 * @items_initial:
 * @items_max:
 * @item_size:
 * @item_func:
 *
 * This function will create memory pool object. Pool may grow but will
 * never shrink. Pool consists of number of dynamically allocated blocks
 * with size enough to hold %items_initial number of items. Memory is
 * DMA-able but client must map/unmap before interoperating with the device.
 * See also: xge_os_dma_map(), xge_hal_dma_unmap(), xge_hal_status_e{}.
 */


This function will create memory pool object. Pool may grow but will
never shrink. Pool consists of number of dynamically allocated blocks
with size enough to hold %items_initial number of items. Memory is
DMA-able but client must map/unmap before interoperating with the device.
This function will create memory pool object . Pool may grow but will never shrink . Pool consists of number of dynamically allocated blocks with size enough to hold %items_initial number of items . Memory is DMA - able but client must map / unmap before interoperating with the device .
xge_hal_mempool_t*
__hal_mempool_create(pci_dev_h pdev, int memblock_size, int item_size,
	    int items_priv_size, int items_initial, int items_max,
	    xge_hal_mempool_item_f item_func_alloc,
	    xge_hal_mempool_item_f item_func_free, void *userdata)
{
	xge_hal_status_e status;
	int memblocks_to_allocate;
	xge_hal_mempool_t *mempool;
	int allocated;
	if (memblock_size < item_size) {
	    xge_debug_mm(XGE_ERR,
	        "memblock_size %d < item_size %d: misconfiguration",
	        memblock_size, item_size);
	    return NULL;
	}
	mempool = (xge_hal_mempool_t *) \
	        xge_os_malloc(pdev, sizeof(xge_hal_mempool_t));
	if (mempool == NULL) {
	    xge_debug_mm(XGE_ERR, "mempool allocation failure");
	    return NULL;
	}
	xge_os_memzero(mempool, sizeof(xge_hal_mempool_t));
	mempool->pdev           = pdev;
	mempool->memblock_size      = memblock_size;
	mempool->items_max      = items_max;
	mempool->items_initial      = items_initial;
	mempool->item_size      = item_size;
	mempool->items_priv_size    = items_priv_size;
	mempool->item_func_alloc    = item_func_alloc;
	mempool->item_func_free     = item_func_free;
	mempool->userdata       = userdata;
	mempool->memblocks_allocated = 0;
	mempool->items_per_memblock = memblock_size / item_size;
	mempool->memblocks_max = (items_max + mempool->items_per_memblock - 1) /
	                mempool->items_per_memblock;
	mempool->memblocks_arr = (void ** ) xge_os_malloc(mempool->pdev,
	                sizeof(void*) * mempool->memblocks_max);
	if (mempool->memblocks_arr == NULL) {
	    xge_debug_mm(XGE_ERR, "memblocks_arr allocation failure");
	    __hal_mempool_destroy(mempool);
	    return NULL;
	}
	xge_os_memzero(mempool->memblocks_arr,
	        sizeof(void*) * mempool->memblocks_max);
	mempool->memblocks_priv_arr = (void **) xge_os_malloc(mempool->pdev,
	                sizeof(void*) * mempool->memblocks_max);
	if (mempool->memblocks_priv_arr == NULL) {
	    xge_debug_mm(XGE_ERR, "memblocks_priv_arr allocation failure");
	    __hal_mempool_destroy(mempool);
	    return NULL;
	}
	xge_os_memzero(mempool->memblocks_priv_arr,
	        sizeof(void*) * mempool->memblocks_max);
	mempool->memblocks_dma_arr =
	    (xge_hal_mempool_dma_t *) xge_os_malloc(mempool->pdev,
	    sizeof(xge_hal_mempool_dma_t) * mempool->memblocks_max);
	if (mempool->memblocks_dma_arr == NULL) {
	    xge_debug_mm(XGE_ERR, "memblocks_dma_arr allocation failure");
	    __hal_mempool_destroy(mempool);
	    return NULL;
	}
	xge_os_memzero(mempool->memblocks_dma_arr,
	         sizeof(xge_hal_mempool_dma_t) * mempool->memblocks_max);
	mempool->items_arr = (void **) xge_os_malloc(mempool->pdev,
	             sizeof(void*) * mempool->items_max);
	if (mempool->items_arr == NULL) {
	    xge_debug_mm(XGE_ERR, "items_arr allocation failure");
	    __hal_mempool_destroy(mempool);
	    return NULL;
	}
	xge_os_memzero(mempool->items_arr, sizeof(void *) * mempool->items_max);
	mempool->shadow_items_arr = (void **) xge_os_malloc(mempool->pdev,
	                            sizeof(void*) *  mempool->items_max);
	if (mempool->shadow_items_arr == NULL) {
	    xge_debug_mm(XGE_ERR, "shadow_items_arr allocation failure");
	    __hal_mempool_destroy(mempool);
	    return NULL;
	}
	xge_os_memzero(mempool->shadow_items_arr,
	         sizeof(void *) * mempool->items_max);
	memblocks_to_allocate = (mempool->items_initial +
	             mempool->items_per_memblock - 1) /
	                    mempool->items_per_memblock;
	xge_debug_mm(XGE_TRACE, "allocating %d memblocks, "
	        "%d items per memblock", memblocks_to_allocate,
	        mempool->items_per_memblock);
	status = __hal_mempool_grow(mempool, memblocks_to_allocate, &allocated);
	xge_os_memcpy(mempool->shadow_items_arr, mempool->items_arr,
	        sizeof(void*) * mempool->items_max);
	if (status != XGE_HAL_OK) {
	    xge_debug_mm(XGE_ERR, "mempool_grow failure");
	    __hal_mempool_destroy(mempool);
	    return NULL;
	}
	xge_debug_mm(XGE_TRACE,
	    "total: allocated %dk of DMA-capable memory",
	    mempool->memblock_size * allocated / 1024);
	return mempool;
}
xge_hal_mempool_t * __hal_mempool_create ( pci_dev_h pdev , int memblock_size , int item_size , int items_priv_size , int items_initial , int items_max , xge_hal_mempool_item_f item_func_alloc , xge_hal_mempool_item_f item_func_free , void * userdata ) { xge_hal_status_e status ; int memblocks_to_allocate ; xge_hal_mempool_t * mempool ; int allocated ; if ( memblock_size < item_size ) { xge_debug_mm ( XGE_ERR , " " , memblock_size , item_size ) ; return NULL ; } mempool = ( xge_hal_mempool_t * ) xge_os_malloc ( pdev , sizeof ( xge_hal_mempool_t ) ) ; if ( mempool == NULL ) { xge_debug_mm ( XGE_ERR , " " ) ; return NULL ; } xge_os_memzero ( mempool , sizeof ( xge_hal_mempool_t ) ) ; mempool -> pdev = pdev ; mempool -> memblock_size = memblock_size ; mempool -> items_max = items_max ; mempool -> items_initial = items_initial ; mempool -> item_size = item_size ; mempool -> items_priv_size = items_priv_size ; mempool -> item_func_alloc = item_func_alloc ; mempool -> item_func_free = item_func_free ; mempool -> userdata = userdata ; mempool -> memblocks_allocated = 0 ; mempool -> items_per_memblock = memblock_size / item_size ; mempool -> memblocks_max = ( items_max + mempool -> items_per_memblock - 1 ) / mempool -> items_per_memblock ; mempool -> memblocks_arr = ( void * * ) xge_os_malloc ( mempool -> pdev , sizeof ( void * ) * mempool -> memblocks_max ) ; if ( mempool -> memblocks_arr == NULL ) { xge_debug_mm ( XGE_ERR , " " ) ; __hal_mempool_destroy ( mempool ) ; return NULL ; } xge_os_memzero ( mempool -> memblocks_arr , sizeof ( void * ) * mempool -> memblocks_max ) ; mempool -> memblocks_priv_arr = ( void * * ) xge_os_malloc ( mempool -> pdev , sizeof ( void * ) * mempool -> memblocks_max ) ; if ( mempool -> memblocks_priv_arr == NULL ) { xge_debug_mm ( XGE_ERR , " " ) ; __hal_mempool_destroy ( mempool ) ; return NULL ; } xge_os_memzero ( mempool -> memblocks_priv_arr , sizeof ( void * ) * mempool -> memblocks_max ) ; mempool -> memblocks_dma_arr = ( xge_hal_mempool_dma_t * ) xge_os_malloc ( mempool -> pdev , sizeof ( xge_hal_mempool_dma_t ) * mempool -> memblocks_max ) ; if ( mempool -> memblocks_dma_arr == NULL ) { xge_debug_mm ( XGE_ERR , " " ) ; __hal_mempool_destroy ( mempool ) ; return NULL ; } xge_os_memzero ( mempool -> memblocks_dma_arr , sizeof ( xge_hal_mempool_dma_t ) * mempool -> memblocks_max ) ; mempool -> items_arr = ( void * * ) xge_os_malloc ( mempool -> pdev , sizeof ( void * ) * mempool -> items_max ) ; if ( mempool -> items_arr == NULL ) { xge_debug_mm ( XGE_ERR , " " ) ; __hal_mempool_destroy ( mempool ) ; return NULL ; } xge_os_memzero ( mempool -> items_arr , sizeof ( void * ) * mempool -> items_max ) ; mempool -> shadow_items_arr = ( void * * ) xge_os_malloc ( mempool -> pdev , sizeof ( void * ) * mempool -> items_max ) ; if ( mempool -> shadow_items_arr == NULL ) { xge_debug_mm ( XGE_ERR , " " ) ; __hal_mempool_destroy ( mempool ) ; return NULL ; } xge_os_memzero ( mempool -> shadow_items_arr , sizeof ( void * ) * mempool -> items_max ) ; memblocks_to_allocate = ( mempool -> items_initial + mempool -> items_per_memblock - 1 ) / mempool -> items_per_memblock ; xge_debug_mm ( XGE_TRACE , " " " " , memblocks_to_allocate , mempool -> items_per_memblock ) ; status = __hal_mempool_grow ( mempool , memblocks_to_allocate , & allocated ) ; xge_os_memcpy ( mempool -> shadow_items_arr , mempool -> items_arr , sizeof ( void * ) * mempool -> items_max ) ; if ( status != XGE_HAL_OK ) { xge_debug_mm ( XGE_ERR , " " ) ; __hal_mempool_destroy ( mempool ) ; return NULL ; } xge_debug_mm ( XGE_TRACE , " " , mempool -> memblock_size * allocated / 1024 ) ; return mempool ; }
xge_hal_mempool_create
 @memblock_size:
 @items_initial:
 @items_max:
 @item_size:
 @item_func:
xge_hal_mempool_create @memblock_size : @items_initial : @items_max : @item_size : @item_func :
/* allocate array of memblocks */ /* allocate array of private parts of items per memblocks */ /* allocate array of memblocks DMA objects */ /* allocate hash array of items */ /* calculate initial number of memblocks */ /* pre-allocate the mempool */