1 #pragma once 2 #ifndef IWRB_H 3 #define IWRB_H 4 5 /************************************************************************************************** 6 * Ring buffer. 7 * 8 * IOWOW library 9 * 10 * MIT License 11 * 12 * Copyright (c) 2012-2022 Softmotions Ltd <info@softmotions.com> 13 * 14 * Permission is hereby granted, free of charge, to any person obtaining a copy 15 * of this software and associated documentation files (the "Software"), to deal 16 * in the Software without restriction, including without limitation the rights 17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 * copies of the Software, and to permit persons to whom the Software is 19 * furnished to do so, subject to the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be included in all 22 * copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 *************************************************************************************************/ 32 33 #include "basedefs.h" 34 35 IW_EXTERN_C_START 36 37 typedef struct { 38 ssize_t pos; 39 size_t len; 40 size_t usize; 41 char *buf; 42 } IWRB; 43 44 typedef struct { 45 const IWRB *rb; 46 size_t pos; 47 ssize_t ipos; 48 } IWRB_ITER; 49 50 IW_EXPORT IW_ALLOC IWRB* iwrb_create(size_t usize, size_t len); 51 52 IW_EXPORT void iwrb_clear(IWRB *rb); 53 54 IW_EXPORT void iwrb_destroy(IWRB **rbp); 55 56 IW_EXPORT IWRB* iwrb_wrap(void *buf, size_t len, size_t usize); 57 58 IW_EXPORT void iwrb_put(IWRB *rb, const void *buf); 59 60 IW_EXPORT void iwrb_back(IWRB *rb); 61 62 IW_EXPORT void* iwrb_peek(const IWRB *rb); 63 64 IW_EXPORT size_t iwrb_num_cached(const IWRB *rb); 65 66 IW_EXPORT void iwrb_iter_init(const IWRB *rb, IWRB_ITER *iter); 67 68 IW_EXPORT void* iwrb_iter_prev(IWRB_ITER *iter); 69 70 IW_EXTERN_C_END 71 #endif 72