Go to the documentation of this file.    1 #ifndef CSNIP_RINGBUF_H 
    2 #define CSNIP_RINGBUF_H 
   95 #define csnip_ringbuf_Init(head, len, N) \ 
  125 #define csnip_ringbuf_GetHeadIdx(head, len, N, ret, err) \ 
  128             csnip_err_Raise(csnip_err_UNDERFLOW, err); \ 
  143 #define csnip_ringbuf_GetTailIdx(head, len, N, ret, err) \ 
  144     csnip_ringbuf__GetTailIdx((head), (len), (N), (ret), (err)) 
  147 #define csnip_ringbuf__GetTailIdx(head, len, N, ret, err) \ 
  150             csnip_err_Raise(csnip_err_UNDERFLOW, err); \ 
  153         ret = csnip_ringbuf__AddWrap(N, (len - 1), head); \ 
  168 #define csnip_ringbuf_PushHeadIdx(head, len, N, err) \ 
  169     csnip_ringbuf__PushHeadIdx((head), (len), (N), (err)) 
  172 #define csnip_ringbuf__PushHeadIdx(head, len, N, err) \ 
  175             csnip_err_Raise(csnip_err_NOMEM, err); \ 
  178         csnip_ringbuf__SubWrapSet(N, 1, head); \ 
  184 #define csnip_ringbuf_PopHeadIdx(head, len, N, err) \ 
  185     csnip_ringbuf__PopHeadIdx((head), (len), (N), (err)) 
  188 #define csnip_ringbuf__PopHeadIdx(head, len, N, err) \ 
  191             csnip_err_Raise(csnip_err_UNDERFLOW, err); \ 
  194         csnip_ringbuf__AddWrapSet(N, 1, head); \ 
  203 #define csnip_ringbuf_PushTailIdx(head, len, N, err) \ 
  204     csnip_ringbuf__PushTailIdx((head), (len), (N), (err)) 
  207 #define csnip_ringbuf__PushTailIdx(head, len, N, err) \ 
  210             csnip_err_Raise(csnip_err_NOMEM, err); \ 
  218 #define csnip_ringbuf_PopTailIdx(head, len, N, err) \ 
  219     csnip_ringbuf__PopTailIdx((head), (len), (N), (err)) 
  222 #define csnip_ringbuf__PopTailIdx(head, len, N, err) \ 
  225             csnip_err_Raise(csnip_err_UNDERFLOW, err); \ 
  237 #define csnip_ringbuf_IsFull(head, len, N) ((len) == (N)) 
  244 #define csnip_ringbuf_IsEmpty(head, len, N) ((len) == 0) 
  252 #define csnip_ringbuf_CheckIdx(head, len, N, idx, err) \ 
  253     csnip_ringbuf__CheckIdx((head), (len), (N), (idx), (err)) 
  256 #define csnip_ringbuf__CheckIdx(head, len, N, idx, err) \ 
  258         if ((idx) >= (head)) { \ 
  259             if ((idx) - (head) >= (len)) { \ 
  260                 csnip_err_Raise(csnip_err_RANGE, err); \ 
  263             if ((idx) - (head) + (N)  >= (len)) { \ 
  264                 csnip_err_Raise(csnip_err_RANGE, err); \ 
  295 #define csnip_ringbuf_AddWrap(N, amount, idx) \ 
  296     ((amount) >= 0 ? csnip_ringbuf__AddWrap((N), (amount), (idx)) \ 
  297         : csnip_ringbuf__SubWrap((N), -(amount), (idx))) 
  304 #define csnip_ringbuf_AddWrapSet(N, amount, idx) \ 
  305     ((amount) >= 0 ? csnip_ringbuf__AddWrapSet((N), (amount), (idx)) \ 
  306         : csnip_ringbuf__SubWrapSet((N), -(amount), (idx))) 
  333 #define csnip_ringbuf_SubWrap(N, amount, idx) \ 
  334     ((amount) >= 0 ? csnip_ringbuf__SubWrap((N), (amount), (idx)) \ 
  335         : csnip_ringbuf__AddWrap((N), -(amount), (idx))) 
  342 #define csnip_ringbuf_SubWrapSet(N, amount, idx) \ 
  343     ((amount) >= 0 ? csnip_ringbuf__SubWrapSet((N), (amount), (idx)) \ 
  344         : csnip_ringbuf__AddWrapSet((N), -(amount), (idx)sebase)) 
  347 #define csnip_ringbuf__AddWrap(N, amount, idx) \ 
  348     (idx < N - amount ? idx + amount : amount - (N - idx)) 
  350 #define csnip_ringbuf__AddWrapSet(N, amount, idx) \ 
  351     (idx < N - amount ? (idx += amount) \ 
  352                 : (idx = amount - (N - idx))) 
  354 #define csnip_ringbuf__SubWrap(N, amount, idx) \ 
  355     (idx >= amount ? idx - amount : idx - amount + N) 
  357 #define csnip_ringbuf__SubWrapSet(N, amount, idx) \ 
  358     (idx >= amount ? (idx -= amount) : (idx = idx - amount + N)) 
  367 #define csnip_ringbuf_PushHead(head, len, N, arr, val, err) \ 
  369         int csnip__err2 = 0; \ 
  370         csnip_ringbuf_PushHeadIdx(head, len, N, csnip__err2); \ 
  371         if (csnip__err2 != 0) { \ 
  372             csnip_err_Raise(csnip__err2, err); \ 
  375         (arr)[head] = (val); \ 
  383 #define csnip_ringbuf_PopHead(head, len, N, arr, ret, err) \ 
  385         (ret) = (arr)[head]; \ 
  386         csnip_ringbuf_PopHeadIdx(head, len, N, err); \ 
  393 #define csnip_ringbuf_PushTail(head, len, N, arr, val, err) \ 
  395         int csnip__err2 = 0; \ 
  397         csnip_ringbuf_PushTailIdx(head, len, N, csnip__err2); \ 
  398         if (csnip__err2 != 0) { \ 
  399             csnip_err_Raise(csnip__err2, err); \ 
  402         csnip_ringbuf_GetTailIdx(head, len, N, csnip__i, csnip__err2); \ 
  403         if (csnip__err2 != 0) { \ 
  404             csnip_err_Raise(csnip__err2, err); \ 
  407         (arr)[csnip__i] = (val); \ 
  414 #define csnip_ringbuf_PopTail(head, len, N, arr, ret, err) \ 
  416         int csnip__err2 = 0; \ 
  418         csnip_ringbuf_GetTailIdx(head, len, N, csnip__i, csnip__err2); \ 
  419         if (csnip__err2 != 0) { \ 
  420             csnip_err_Raise(csnip__err2, err); \ 
  423         (ret) = (arr)[csnip__i]; \ 
  424         csnip_ringbuf_PopTailIdx(head, len, N, err); \ 
  443 #define CSNIP_RINGBUF_DECL_IDX_FUNCS(scope, prefix, idx_type, gen_args) \ 
  444     scope void prefix##init(csnip_pp_list_##gen_args); \ 
  445     scope idx_type prefix##get_head_idx(csnip_pp_list_##gen_args); \ 
  446     scope idx_type prefix##get_tail_idx(csnip_pp_list_##gen_args); \ 
  447     scope void prefix##push_head_idx(csnip_pp_list_##gen_args); \ 
  448     scope void prefix##pop_head_idx(csnip_pp_list_##gen_args); \ 
  449     scope void prefix##push_tail_idx(csnip_pp_list_##gen_args); \ 
  450     scope void prefix##pop_tail_idx(csnip_pp_list_##gen_args); \ 
  451     scope int prefix##is_full(csnip_pp_list_##gen_args); \ 
  452     scope int prefix##is_empty(csnip_pp_list_##gen_args); \ 
  453     scope void prefix##check_idx(csnip_pp_prepend_##gen_args idx_type); \ 
  454     scope idx_type prefix##add_wrap(csnip_pp_prepend_##gen_args \ 
  455                 idx_type, idx_type); \ 
  456     scope idx_type prefix##sub_wrap(csnip_pp_prepend_##gen_args \ 
  457                 idx_type, idx_type); \ 
  477 #define CSNIP_RINGBUF_DECL_VAL_FUNCS(scope, prefix, val_type, gen_args) \ 
  478     scope void prefix##push_head(csnip_pp_prepend_##gen_args \ 
  480     scope val_type prefix##pop_head(csnip_pp_list_##gen_args); \ 
  481     scope void prefix##push_tail(csnip_pp_prepend_##gen_args \ 
  483     scope val_type prefix##pop_tail(csnip_pp_list_##gen_args); 
  560 #define CSNIP_RINGBUF_DEF_IDX_FUNCS(scope, prefix, idx_type, gen_args, \ 
  562     scope void prefix##init(csnip_pp_list_##gen_args) { \ 
  563         csnip_ringbuf_Init(head, len, N); \ 
  565     scope idx_type prefix##get_head_idx(csnip_pp_list_##gen_args) { \ 
  566         idx_type csnip__ret; \ 
  567         csnip_ringbuf_GetHeadIdx(head, len, N, csnip__ret, err); \ 
  570     scope idx_type prefix##get_tail_idx(csnip_pp_list_##gen_args) { \ 
  571         idx_type csnip__ret; \ 
  572         csnip_ringbuf_GetTailIdx(head, len, N, csnip__ret, err); \ 
  575     scope void prefix##push_head_idx(csnip_pp_list_##gen_args) { \ 
  576         csnip_ringbuf_PushHeadIdx(head, len, N, err); \ 
  578     scope void prefix##pop_head_idx(csnip_pp_list_##gen_args) { \ 
  579         csnip_ringbuf_PopHeadIdx(head, len, N, err); \ 
  581     scope void prefix##push_tail_idx(csnip_pp_list_##gen_args) { \ 
  582         csnip_ringbuf_PushTailIdx(head, len, N, err); \ 
  584     scope void prefix##pop_tail_idx(csnip_pp_list_##gen_args) { \ 
  585         csnip_ringbuf_PopTailIdx(head, len, N, err); \ 
  587     scope int prefix##is_full(csnip_pp_list_##gen_args) { \ 
  588         return csnip_ringbuf_IsFull(head, len, N); \ 
  590     scope int prefix##is_empty(csnip_pp_list_##gen_args) { \ 
  591         return csnip_ringbuf_IsEmpty(head, len, N); \ 
  593     scope void prefix##check_idx(csnip_pp_prepend_##gen_args \ 
  596         csnip_ringbuf_CheckIdx(head, len, N, csnip__i, err); \ 
  598     scope idx_type prefix##add_wrap(csnip_pp_prepend_##gen_args \ 
  599             idx_type csnip__amount, idx_type csnip__base) \ 
  601         return csnip_ringbuf_AddWrap(N, csnip__amount, csnip__base); \ 
  603     scope idx_type prefix##sub_wrap(csnip_pp_prepend_##gen_args \ 
  604             idx_type csnip__amount, idx_type csnip__base) \ 
  606         return csnip_ringbuf_SubWrap(N, csnip__amount, csnip__base); \ 
  622 #define CSNIP_RINGBUF_DEF_VAL_FUNCS(scope, prefix, val_type, \ 
  623     gen_args, head, len, N, arr, err) \ 
  624     scope void prefix##push_head(csnip_pp_prepend_##gen_args \ 
  626         csnip_ringbuf_PushHead(head, len, N, arr, val, err); \ 
  628     scope val_type prefix##pop_head(csnip_pp_list_##gen_args) { \ 
  629         val_type csnip__ret; \ 
  630         csnip_ringbuf_PopHead(head, len, N, arr, csnip__ret, err); \ 
  633     scope void prefix##push_tail(csnip_pp_prepend_##gen_args \ 
  635         csnip_ringbuf_PushTail(head, len, N, arr, val, err); \ 
  637     scope val_type prefix##pop_tail(csnip_pp_list_##gen_args) { \ 
  638         val_type csnip__ret; \ 
  639         csnip_ringbuf_PopTail(head, len, N, arr, csnip__ret, err); \ 
  647 #if defined(CSNIP_SHORT_NAMES) && !defined(CSNIP_RINGBUF_HAVE_SHORT_NAMES) 
  648 #define ringbuf_Init        csnip_ringbuf_Init 
  649 #define ringbuf_GetHeadIdx  csnip_ringbuf_GetHeadIdx 
  650 #define ringbuf_GetTailIdx  csnip_ringbuf_GetTailIdx 
  651 #define ringbuf_PushHeadIdx csnip_ringbuf_PushHeadIdx 
  652 #define ringbuf_PopHeadIdx  csnip_ringbuf_PopHeadIdx 
  653 #define ringbuf_PushTailIdx csnip_ringbuf_PushTailIdx 
  654 #define ringbuf_PopTailIdx  csnip_ringbuf_PopTailIdx 
  655 #define ringbuf_CheckIdx    csnip_ringbuf_CheckIdx 
  656 #define ringbuf_AddWrap     csnip_ringbuf_AddWrap 
  657 #define ringbuf_AddWrapSet  csnip_ringbuf_AddWrapSet 
  658 #define ringbuf_SubWrap     csnip_ringbuf_SubWrap 
  659 #define ringbuf_SubWrapSet  csnip_ringbuf_SubWrapSet 
  660 #define ringbuf_PushHead    csnip_ringbuf_PushHead 
  661 #define ringbuf_PopHead     csnip_ringbuf_PopHead 
  662 #define ringbuf_PushTail    csnip_ringbuf_PushTail 
  663 #define ringbuf_PopTail     csnip_ringbuf_PopTail 
  664 #define CSNIP_RINGBUF_HAVE_SHORT_NAMES