1 /* ISC license. */ 2 3 #ifndef BUFFER_H 4 #define BUFFER_H 5 6 #include <sys/uio.h> 7 #include <skalibs/gccattributes.h> 8 #include <skalibs/allreadwrite.h> 9 #include <skalibs/cbuffer.h> 10 #include <skalibs/functypes.h> 11 12 typedef struct buffer_s buffer, buffer_t, *buffer_ref, *buffer_t_ref ; 13 struct buffer_s 14 { 15 iovfunc_t_ref op ; 16 int fd ; 17 cbuffer_t c ; 18 } ; 19 #define BUFFER_ZERO { 0, -1, CBUFFER_ZERO } 20 21 22 /* 23 Circular buffers need to be 1 char bigger than the storage space, 24 so that the head == tail case is nonambiguous (empty, not full). 25 */ 26 27 #define BUFFER_INSIZE 8192 28 #define BUFFER_OUTSIZE 8192 29 #define BUFFER_ERRSIZE 1024 30 #define BUFFER_INSIZE_SMALL 512 31 #define BUFFER_OUTSIZE_SMALL 512 32 33 #define BUFFER_INIT(f, d, buf, len) { (f), (d), CBUFFER_INIT(buf, len) } 34 extern int buffer_init (buffer *, iovfunc_t_ref, int, char *, size_t) ; 35 36 37 /* Writing */ 38 39 extern int buffer_flush (buffer *) ; 40 41 #define buffer_putnoflush(b, s, len) cbuffer_put(&(b)->c, s, len) 42 #define buffer_putvnoflush(b, v, n) cbuffer_putv(&(b)->c, v, n) 43 extern size_t buffer_putsnoflush (buffer *, char const *) ; 44 45 extern int buffer_putallnoflush (buffer *, char const *, size_t) ; 46 extern int buffer_putvallnoflush (buffer *, struct iovec const *, unsigned int) ; 47 extern int buffer_putsallnoflush (buffer *, char const *) ; 48 49 extern int buffer_putall (buffer *, char const *, size_t, size_t *) ; 50 extern int buffer_putvall (buffer *, struct iovec const *, unsigned int, size_t *) ; 51 extern int buffer_putsall (buffer *, char const *, size_t *) ; 52 53 #define buffer_putallflush(b, s, len, w) (buffer_putall(b, s, len, w) && buffer_flush(b)) 54 #define buffer_putvallflush(b, v, n, w) (buffer_putvall(b, v, n, w) && buffer_flush(b)) 55 extern int buffer_putsallflush (buffer *, char const *, size_t *) ; 56 57 extern ssize_t buffer_put (buffer *, char const *, size_t) ; 58 extern ssize_t buffer_putv (buffer *, struct iovec const *, unsigned int) ; 59 extern ssize_t buffer_puts (buffer *, char const *) ; 60 61 extern ssize_t buffer_putflush (buffer *, char const *, size_t) ; 62 extern ssize_t buffer_putvflush (buffer *, struct iovec const *, unsigned int) ; 63 extern ssize_t buffer_putsflush (buffer *, char const *) ; 64 65 #define buffer_unput(b, n) cbuffer_unput(&(b)->c, n) 66 #define buffer_wpeek(b, v) cbuffer_wpeek(&(b)->c, v) 67 #define buffer_wseek(b, n) cbuffer_wseek(&(b)->c, n) 68 69 70 /* Reading */ 71 72 extern ssize_t buffer_fill (buffer *) ; 73 74 #define buffer_getnofill(b, s, len) cbuffer_get(&(b)->c, s, len) 75 #define buffer_getvnofill(b, v, n) cbuffer_getv(&(b)->c, v, n) 76 77 extern int buffer_getallnofill (buffer *, char *, size_t) ; 78 extern int buffer_getvallnofill (buffer *, struct iovec const *, unsigned int) ; 79 80 extern int buffer_getall (buffer *, char *, size_t, size_t *) ; 81 extern int buffer_getvall (buffer *, struct iovec const *, unsigned int, size_t *) ; 82 83 extern ssize_t buffer_get (buffer *, char *, size_t) ; 84 extern ssize_t buffer_getv (buffer *, struct iovec const *, unsigned int) ; 85 86 #define buffer_unget(b, n) cbuffer_unget(&(b)->c, n) 87 #define buffer_rpeek(b, v) cbuffer_rpeek(&(b)->c, v) 88 #define buffer_rseek(b, n) cbuffer_rseek(&(b)->c, n) 89 90 91 /* Utility */ 92 93 #define buffer_len(b) cbuffer_len(&(b)->c) 94 extern size_t buffer_getlen (buffer const *) gccattr_pure ; 95 #define buffer_available(b) cbuffer_available(&(b)->c) 96 #define buffer_isempty(b) cbuffer_isempty(&(b)->c) 97 #define buffer_isfull(b) cbuffer_isfull(&(b)->c) 98 #define buffer_fd(b) ((b)->fd) 99 extern int buffer_getfd (buffer const *) gccattr_pure ; 100 #define buffer_isreadable(b) (!buffer_isfull(b)) 101 #define buffer_iswritable(b) (!buffer_isempty(b)) 102 103 104 /* Globals */ 105 106 #define buffer_read fd_readv 107 #define buffer_write fd_writev 108 extern iovfunc_t buffer_flush1read ; 109 110 extern buffer buffer_0_ ; 111 #define buffer_0 (&buffer_0_) 112 113 extern buffer buffer_0small_ ; 114 #define buffer_0small (&buffer_0small_) 115 116 extern buffer buffer_0f1_ ; 117 #define buffer_0f1 (&buffer_0f1_) 118 119 extern buffer buffer_1_ ; 120 #define buffer_1 (&buffer_1_) 121 122 extern buffer buffer_1small_ ; 123 #define buffer_1small (&buffer_1small_) 124 125 extern buffer buffer_2_ ; 126 #define buffer_2 (&buffer_2_) 127 128 #endif 129