/** pbuf_init * Initialize packet buffer. * @return 0 on success, -1 on error. */ static int pbuf_init(void) { /* Initialize packet buffers */ int i; pbuf_element_t *buf; for (i = 0; i < PBUF_ELEMENTS; i++) { buf = &pbuf[i]; buf->rx_count = 0; buf->tx_count = 0; buf->cfpid = 0; buf->packet = NULL; buf->state = BUF_FREE; buf->last_used = 0; buf->remain = 0; /* Create tx semaphore if blocking mode is enabled */ if (csp_bin_sem_create(&buf->tx_sem) != CSP_SEMAPHORE_OK) { csp_log_error("Failed to allocate TX semaphore\r\n"); return CSP_ERR_NOMEM; } } /* Initialize global lock */ if (CSP_INIT_CRITICAL(pbuf_sem) != CSP_ERR_NONE) { csp_log_error("No more memory for packet buffer semaphore\r\n"); return CSP_ERR_NOMEM; } return CSP_ERR_NONE; }
int csp_buffer_init(int buf_count, int buf_size) { unsigned int i; csp_skbf_t * buf; count = buf_count; size = buf_size; unsigned int skbfsize = (sizeof(csp_skbf_t) + size); skbfsize = CSP_BUFFER_ALIGN * ((skbfsize + CSP_BUFFER_ALIGN - 1) / CSP_BUFFER_ALIGN); unsigned int poolsize = count * skbfsize; csp_buffer_pool = csp_malloc(poolsize); if (csp_buffer_pool == NULL) goto fail_malloc; csp_buffers = csp_queue_create(count, sizeof(void *)); if (!csp_buffers) goto fail_queue; if (CSP_INIT_CRITICAL(csp_critical_lock) != CSP_ERR_NONE) goto fail_critical; memset(csp_buffer_pool, 0, poolsize); for (i = 0; i < count; i++) { /* We have already taken care of pointer alignment since * skbfsize is an integer multiple of sizeof(int *) * but the explicit cast to a void * is still necessary * to tell the compiler so. */ buf = (void *) &csp_buffer_pool[i * skbfsize]; buf->refcount = 0; buf->skbf_addr = buf; csp_queue_enqueue(csp_buffers, &buf, 0); } return CSP_ERR_NONE; fail_critical: csp_queue_remove(csp_buffers); fail_queue: csp_free(csp_buffer_pool); fail_malloc: return CSP_ERR_NOMEM; }
int csp_buffer_init(int buf_count, int buf_size) { unsigned int i; void *element; count = buf_count; size = buf_size; csp_buffer_list = csp_malloc(count * size); if (csp_buffer_list == NULL) goto fail_malloc; csp_buffers = csp_queue_create(count, sizeof(void *)); if (!csp_buffers) goto fail_queue; if (CSP_INIT_CRITICAL(csp_critical_lock) != CSP_ERR_NONE) goto fail_critical; memset(csp_buffer_list, 0, count * size); for (i = 0; i < count; i++) { element = csp_buffer_list + i * size; csp_queue_enqueue(csp_buffers, &element, 0); } return CSP_ERR_NONE; fail_critical: csp_queue_remove(csp_buffers); fail_queue: csp_free(csp_buffer_list); fail_malloc: return CSP_ERR_NOMEM; }