| 1 | /* |
| 2 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 3 | * Copyright (C) 2003-2008 Benny Prijono <[email protected]> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #ifndef __PJ_FIFOBUF_H__ |
| 20 | #define __PJ_FIFOBUF_H__ |
| 21 | |
| 22 | /** |
| 23 | * @file fifobuf.h |
| 24 | * @brief Circular buffer |
| 25 | */ |
| 26 | /** |
| 27 | * @defgroup PJ_FIFOBUF Circular buffer |
| 28 | * @ingroup PJ_DS |
| 29 | * @{ |
| 30 | */ |
| 31 | |
| 32 | #include <pj/types.h> |
| 33 | |
| 34 | PJ_BEGIN_DECL |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * A FIFO buffer provides chunks of memory to the application with first in |
| 39 | * first out policy (or more correctly, first out first in). The fifobuf is |
| 40 | * created by providing it with a fixed buffer. After that, application may |
| 41 | * request chunks of memory from this buffer. When the app is done with a |
| 42 | * chunk of memory, it must return that chunk back to the fifobuf, with the |
| 43 | * requirement that the oldest allocated chunk must be returned first. |
| 44 | */ |
| 45 | typedef struct pj_fifobuf_t |
| 46 | { |
| 47 | /** The start of the buffer */ |
| 48 | char *first; |
| 49 | |
| 50 | /** The end of the buffer */ |
| 51 | char *last; |
| 52 | |
| 53 | /** The start of used area in the buffer */ |
| 54 | char *ubegin; |
| 55 | |
| 56 | /** The end of used area in the buffer */ |
| 57 | char *uend; |
| 58 | |
| 59 | /** Full flag when ubegin==uend */ |
| 60 | int full; |
| 61 | |
| 62 | } pj_fifobuf_t; |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Initialize the fifobuf by giving it a buffer and size. |
| 67 | * |
| 68 | * @param fb The fifobuf |
| 69 | * @param buffer Buffer to be used to allocate/free chunks of memory from by |
| 70 | * the fifo buffer. |
| 71 | * @param size The size of the buffer. |
| 72 | */ |
| 73 | PJ_DECL(void) pj_fifobuf_init(pj_fifobuf_t *fb, void *buffer, unsigned size); |
| 74 | |
| 75 | /** |
| 76 | * Returns the capacity (initial size) of the buffer. |
| 77 | * |
| 78 | * @param fb The fifobuf |
| 79 | * |
| 80 | * @return Capacity in bytes. |
| 81 | */ |
| 82 | PJ_DECL(unsigned) pj_fifobuf_capacity(pj_fifobuf_t *fb); |
| 83 | |
| 84 | /** |
| 85 | * Returns maximum size of memory chunk that can be allocated from the buffer. |
| 86 | * |
| 87 | * @param fb The fifobuf |
| 88 | * |
| 89 | * @return Size in bytes |
| 90 | */ |
| 91 | PJ_DECL(unsigned) pj_fifobuf_available_size(pj_fifobuf_t *fb); |
| 92 | |
| 93 | /** |
| 94 | * Allocate a chunk of memory from the fifobuf. |
| 95 | * |
| 96 | * @param fb The fifobuf |
| 97 | * @param size Size to allocate |
| 98 | * |
| 99 | * @return Allocated buffer or NULL if the buffer cannot be allocated |
| 100 | */ |
| 101 | PJ_DECL(void*) pj_fifobuf_alloc(pj_fifobuf_t *fb, unsigned size); |
| 102 | |
| 103 | /** |
| 104 | * Return the space used by the earliest allocated memory chunk back to the |
| 105 | * fifobuf. For example, if app previously allocated ptr0, ptr1, and ptr2 |
| 106 | * (in that order), then pj_fifobuf_free() can only be called with ptr0 as |
| 107 | * parameter. Subsequent pj_fifobuf_free() must be called with ptr1, and |
| 108 | * the next one with ptr2, and so on. |
| 109 | * |
| 110 | * @param fb The fifobuf |
| 111 | * @param buf Pointer to memory chunk previously returned by |
| 112 | * pj_fifobuf_alloc() |
| 113 | * |
| 114 | * @return PJ_SUCCESS or the appropriate error. |
| 115 | */ |
| 116 | PJ_DECL(pj_status_t) pj_fifobuf_free(pj_fifobuf_t *fb, void *buf); |
| 117 | |
| 118 | |
| 119 | PJ_END_DECL |
| 120 | |
| 121 | /** |
| 122 | * @} // PJ_FIFOBUF group |
| 123 | */ |
| 124 | |
| 125 | #endif /* __PJ_FIFOBUF_H__ */ |
| 126 |