/*! allocate and initialize queues, etc \param[in, out] context \returns 1 on success, 0 on failure */ int arpd_init(struct thread_context *context) { struct arpd_data *data; int rv; assert(context); data = context->data; context->msg_q = squeue_new(data->msg_q_capacity, data->msg_q_elem_size); if (!context->msg_q) return 0; context->pkt_xmit_q = cqueue_spsc_new(data->xmit_q_capacity, data->xmit_q_elem_size); if (!context->pkt_xmit_q) { squeue_delete(&context->msg_q); return 0; } rv = xmit_queue_init(context->pkt_xmit_q, &context->shared->inet_info->addr, &context->shared->if_info->mac); if (!rv) { squeue_delete(&context->msg_q); return 0; } context->pkt_recv_q = tqueue_new(data->recv_q_transactions, data->recv_q_actions_per_transaction); if (!context->pkt_recv_q) { squeue_delete(&context->msg_q); cqueue_spsc_delete(&context->pkt_xmit_q); return 0; } data->arp_cache = arp_cache_new(context); if (!data->arp_cache) { squeue_delete(&context->msg_q); cqueue_spsc_delete(&context->pkt_xmit_q); tqueue_delete(&context->pkt_recv_q); return 0; } return 1; }
void tqueue_delete(tqueue **p) { tqueue *q = *p; if (!q) return; if (q->transactions) cqueue_spsc_delete(&q->transactions); free(q); *p = NULL; }
int spsc_new_pass() { cqueue_spsc *q; ptrdiff_t d; q = cqueue_spsc_new(26, LEVEL1_DCACHE_LINESIZE-sizeof(size_t)); assert(q); assert(q->capacity == 32); // round up to power of 2 assert(q->elem_size == LEVEL1_DCACHE_LINESIZE); // round up to cacheline // check initialization assert(q->push_idx == 0); assert(q->pop_idx == 0); for(size_t i=0; i < q->capacity; i++) { size_t used = *(size_t *)(q->array + i*q->elem_size); assert(used == 0); } // check struct layout d = ((char *)(void *)q - (char *)(void *)&q->capacity); assert(d == 0); // capacity is the first member d = (char *)(void *)&q->push_idx - (char*)(void *)&q->capacity; assert(d == LEVEL1_DCACHE_LINESIZE); // should be 1 cacheline due to padding d = (char *)(void *)&q->pop_idx - (char *)(void *)&q->push_idx; assert(d == LEVEL1_DCACHE_LINESIZE); // should be 1 cacheline due to padding // check deletion cqueue_spsc_delete(&q); assert(!q); // check elem_size padding rounding up q = cqueue_spsc_new(26, LEVEL1_DCACHE_LINESIZE-sizeof(size_t)+1); assert(q); assert(q->elem_size == 2*LEVEL1_DCACHE_LINESIZE); // round up to 2 cachelines return 1; }