diff options
Diffstat (limited to 'lib/vio_fifo.h')
-rw-r--r-- | lib/vio_fifo.h | 86 |
1 files changed, 53 insertions, 33 deletions
diff --git a/lib/vio_fifo.h b/lib/vio_fifo.h index 6d99afe5..52f3455e 100644 --- a/lib/vio_fifo.h +++ b/lib/vio_fifo.h @@ -22,7 +22,7 @@ #ifndef _ZEBRA_VIO_FIFO_H #define _ZEBRA_VIO_FIFO_H -#include <stddef.h> +#include "zebra.h" #include <stdint.h> #include <stdbool.h> @@ -37,6 +37,13 @@ #define Private extern #endif +/* GCC have printf type attribute check. */ +#ifdef __GNUC__ +#define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b))) +#else +#define PRINTF_ATTRIBUTE(a,b) +#endif /* __GNUC__ */ + /*============================================================================== * VTY I/O FIFO -- buffering of arbitrary amounts of I/O. */ @@ -70,7 +77,12 @@ struct vio_fifo char* get_ptr ; char* get_end ; + vio_fifo_lump rdr_lump ; + char* rdr_ptr ; + size_t size ; + + vio_fifo_lump spare ; } ; struct vio_fifo_lump @@ -78,6 +90,7 @@ struct vio_fifo_lump struct dl_list_pair(vio_fifo_lump) list ; char* end ; /* end of this particular lump */ + size_t size ; /* size of lump when allocated */ char data[] ; } ; @@ -85,52 +98,44 @@ struct vio_fifo_lump * Functions */ -extern vio_fifo -vio_fifo_init_new(vio_fifo vf, size_t size) ; - -extern vio_fifo -vio_fifo_reset(vio_fifo vf, int free_structure) ; +extern vio_fifo vio_fifo_init_new(vio_fifo vf, size_t size) ; +extern vio_fifo vio_fifo_reset(vio_fifo vf, int free_structure) ; #define vio_fifo_reset_keep(vf) vio_fifo_reset(vf, 0) #define vio_fifo_reset_free(vf) vio_fifo_reset(vf, 1) -extern void -vio_fifo_set_empty(vio_fifo vf) ; - -Inline bool -vio_fifo_empty(vio_fifo vf) ; - -extern void -vio_fifo_put(vio_fifo vf, const char* src, size_t n) ; - -Inline void -vio_fifo_put_byte(vio_fifo vf, char b) ; - -extern size_t -vio_fifo_get(vio_fifo vf, void* dst, size_t n) ; +extern void vio_fifo_clear(vio_fifo vf) ; +Inline bool vio_fifo_empty(vio_fifo vf) ; +extern size_t vio_fifo_room(vio_fifo vf) ; -Inline int -vio_fifo_get_byte(vio_fifo vf) ; +extern void vio_fifo_put(vio_fifo vf, const char* src, size_t n) ; +Inline void vio_fifo_put_byte(vio_fifo vf, char b) ; -extern void* -vio_fifo_get_lump(vio_fifo vf, size_t* have) ; +extern int vio_fifo_printf(vio_fifo vf, const char* format, ...) + PRINTF_ATTRIBUTE(2, 3) ; +extern int vio_fifo_vprintf(vio_fifo vf, const char *format, va_list args) ; -extern void -vio_fifo_got_upto(vio_fifo vf, void* here) ; +extern size_t vio_fifo_get(vio_fifo vf, void* dst, size_t n) ; +Inline int vio_fifo_get_byte(vio_fifo vf) ; +extern void* vio_fifo_get_lump(vio_fifo vf, size_t* have) ; +extern void vio_fifo_got_upto(vio_fifo vf, void* here) ; +Inline bool vio_fifo_full_lump(vio_fifo vf) ; +extern int vio_fifo_write_nb(vio_fifo vf, int fd, bool all) ; -Private void -vio_fifo_lump_new(vio_fifo vf) ; +extern void* vio_fifo_get_rdr(vio_fifo vf, size_t* have) ; +extern void* vio_fifo_step_rdr(vio_fifo vf, size_t* have, size_t step) ; +extern void vio_fifo_sync_rdr(vio_fifo vf) ; +extern void vio_fifo_drop_rdr(vio_fifo vf) ; -Private int -vio_fifo_get_next_byte(vio_fifo vf) ; +Private void vio_fifo_lump_new(vio_fifo vf, size_t size) ; +Private int vio_fifo_get_next_byte(vio_fifo vf) ; /*============================================================================== * Debug -- verification function */ -Private void -vio_fifo_verify(vio_fifo vf) ; +Private void vio_fifo_verify(vio_fifo vf) ; #if VIO_FIFO_DEBUG # define VIO_FIFO_DEBUG_VERIFY(vf) vio_fifo_verify(vf) @@ -158,7 +163,7 @@ Inline void vio_fifo_put_byte(vio_fifo vf, char b) { if (vf->put_ptr >= vf->put_end) - vio_fifo_lump_new(vf) ; /* traps broken vf->put_ptr > vf->put_end */ + vio_fifo_lump_new(vf, vf->size) ; /* traps put_ptr > put_end */ VIO_FIFO_DEBUG_VERIFY(vf) ; @@ -182,4 +187,19 @@ vio_fifo_get_byte(vio_fifo vf) return (unsigned char)*vf->get_ptr++ ; } ; +/*------------------------------------------------------------------------------ + * See if have at least one full lump. + * + * This may be used with vio_fifo_write_nb(..., false) to use FIFO as a sort of + * double buffer. + * + * Returns: true <=> there is at least one full lump in the FIFO + * (excluding the last lump if it happens to be full) + */ +Inline bool +vio_fifo_full_lump(vio_fifo vf) +{ + return (!vf->one && (vf->put_ptr != NULL)) ; +} ; + #endif /* _ZEBRA_VIO_FIFO_H */ |