1*c11f373cSzrj /* 2*c11f373cSzrj * Copyright (c) 2016 Rimvydas Jasinskas 3*c11f373cSzrj * All rights reserved. 4*c11f373cSzrj * 5*c11f373cSzrj * Redistribution and use in source and binary forms, with or without 6*c11f373cSzrj * modification, are permitted provided that the following conditions 7*c11f373cSzrj * are met: 8*c11f373cSzrj * 1. Redistributions of source code must retain the above copyright 9*c11f373cSzrj * notice unmodified, this list of conditions, and the following 10*c11f373cSzrj * disclaimer. 11*c11f373cSzrj * 2. Redistributions in binary form must reproduce the above copyright 12*c11f373cSzrj * notice, this list of conditions and the following disclaimer in the 13*c11f373cSzrj * documentation and/or other materials provided with the distribution. 14*c11f373cSzrj * 15*c11f373cSzrj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16*c11f373cSzrj * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17*c11f373cSzrj * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18*c11f373cSzrj * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19*c11f373cSzrj * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20*c11f373cSzrj * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21*c11f373cSzrj * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22*c11f373cSzrj * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23*c11f373cSzrj * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24*c11f373cSzrj * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25*c11f373cSzrj */ 26*c11f373cSzrj 27*c11f373cSzrj #ifndef _LINUX_CIRC_BUF_H 28*c11f373cSzrj #define _LINUX_CIRC_BUF_H 29*c11f373cSzrj 30*c11f373cSzrj struct circ_buf { 31*c11f373cSzrj char *buf; 32*c11f373cSzrj int head; 33*c11f373cSzrj int tail; 34*c11f373cSzrj }; 35*c11f373cSzrj 36*c11f373cSzrj /* Return count in buffer. */ 37*c11f373cSzrj #define CIRC_CNT(head,tail,size) (((head) - (tail)) & ((size)-1)) 38*c11f373cSzrj 39*c11f373cSzrj /* Return space available, 0..size-1. We always leave one free char 40*c11f373cSzrj as a completely full buffer has head == tail, which is the same as 41*c11f373cSzrj empty. */ 42*c11f373cSzrj #define CIRC_SPACE(head,tail,size) CIRC_CNT((tail),((head)+1),(size)) 43*c11f373cSzrj 44*c11f373cSzrj /* Return count up to the end of the buffer. Carefully avoid 45*c11f373cSzrj accessing head and tail more than once, so they can change 46*c11f373cSzrj underneath us without returning inconsistent results. */ 47*c11f373cSzrj #define CIRC_CNT_TO_END(head,tail,size) \ 48*c11f373cSzrj ({int end = (size) - (tail); \ 49*c11f373cSzrj int n = ((head) + end) & ((size)-1); \ 50*c11f373cSzrj n < end ? n : end;}) 51*c11f373cSzrj 52*c11f373cSzrj /* Return space available up to the end of the buffer. */ 53*c11f373cSzrj #define CIRC_SPACE_TO_END(head,tail,size) \ 54*c11f373cSzrj ({int end = (size) - 1 - (head); \ 55*c11f373cSzrj int n = (end + (tail)) & ((size)-1); \ 56*c11f373cSzrj n <= end ? n : end+1;}) 57*c11f373cSzrj 58*c11f373cSzrj #endif /* _LINUX_CIRC_BUF_H */ 59