paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Buffering to output and input. |
| 3 | * Copyright (C) 1998 Kunihiro Ishiguro |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published |
| 9 | * by the Free Software Foundation; either version 2, or (at your |
| 10 | * option) any later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the |
| 19 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 | * Boston, MA 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #ifndef _ZEBRA_BUFFER_H |
| 24 | #define _ZEBRA_BUFFER_H |
| 25 | |
| 26 | /* Buffer master. */ |
| 27 | struct buffer |
| 28 | { |
| 29 | /* Data list. */ |
| 30 | struct buffer_data *head; |
| 31 | struct buffer_data *tail; |
paul | 2265d20 | 2004-11-08 15:43:21 +0000 | [diff] [blame] | 32 | |
| 33 | /* XXX: These unsigned longs should be size_t's */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 34 | /* Current allocated data. */ |
| 35 | unsigned long alloc; |
| 36 | |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 37 | /* Size of each buffer_data chunk. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 38 | unsigned long size; |
| 39 | |
| 40 | /* For allocation. */ |
| 41 | struct buffer_data *unused_head; |
| 42 | struct buffer_data *unused_tail; |
| 43 | |
| 44 | /* Current total length of this buffer. */ |
| 45 | unsigned long length; |
| 46 | }; |
| 47 | |
| 48 | /* Data container. */ |
| 49 | struct buffer_data |
| 50 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 51 | struct buffer_data *next; |
| 52 | struct buffer_data *prev; |
| 53 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 54 | /* Current pointer. */ |
| 55 | unsigned long cp; |
| 56 | |
| 57 | /* Start pointer. */ |
| 58 | unsigned long sp; |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 59 | |
| 60 | /* Actual data stream (variable length). */ |
| 61 | unsigned char data[0]; /* real dimension is buffer->size */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | /* Buffer prototypes. */ |
| 65 | struct buffer *buffer_new (size_t); |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 66 | int buffer_write (struct buffer *, const void *, size_t); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 67 | void buffer_free (struct buffer *); |
| 68 | char *buffer_getstr (struct buffer *); |
| 69 | int buffer_putc (struct buffer *, u_char); |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 70 | int buffer_putstr (struct buffer *, const char *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 71 | void buffer_reset (struct buffer *); |
| 72 | int buffer_flush_all (struct buffer *, int); |
| 73 | int buffer_flush_vty_all (struct buffer *, int, int, int); |
| 74 | int buffer_flush_window (struct buffer *, int, int, int, int, int); |
| 75 | int buffer_empty (struct buffer *); |
| 76 | |
ajs | 49ff6d9 | 2004-11-04 19:26:16 +0000 | [diff] [blame] | 77 | /* buffer_flush_available attempts to flush the queued data to the given |
| 78 | file descriptor. It returns 0 if the buffers are now empty (after |
| 79 | flushing), or 1 if more data remains on the buffer queue (must be flushed |
| 80 | later). This function (unlike the other buffer_flush* functions) is |
| 81 | designed to work with non-blocking file descriptors. */ |
| 82 | int buffer_flush_available(struct buffer *, int fd); |
| 83 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | #endif /* _ZEBRA_BUFFER_H */ |