|
| 1 | +/* Copyright (C) 2010-2020 The RetroArch team |
| 2 | + * |
| 3 | + * --------------------------------------------------------------------------------------- |
| 4 | + * The following license statement only applies to this file (rbuf.h). |
| 5 | + * --------------------------------------------------------------------------------------- |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, |
| 8 | + * to any person obtaining a copy of this software and associated documentation files (the "Software"), |
| 9 | + * to deal in the Software without restriction, including without limitation the rights to |
| 10 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 11 | + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 16 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#ifndef __LIBRETRO_SDK_ARRAY_RBUF_H__ |
| 24 | +#define __LIBRETRO_SDK_ARRAY_RBUF_H__ |
| 25 | + |
| 26 | +/* |
| 27 | + * This file implements stretchy buffers as invented (?) by Sean Barrett. |
| 28 | + * Based on the implementation from the public domain Bitwise project |
| 29 | + * by Per Vognsen - https://github.com/pervognsen/bitwise |
| 30 | + * |
| 31 | + * It's a super simple type safe dynamic array for C with no need |
| 32 | + * to predeclare any type or anything. |
| 33 | + * The first time an element is added, memory for 16 elements are allocated. |
| 34 | + * Then every time length is about to exceed capacity, capacity is doubled. |
| 35 | + * |
| 36 | + * Sample usage: |
| 37 | + * |
| 38 | + * mytype_t* buf = NULL; |
| 39 | + * RBUF_PUSH(buf, some_element); |
| 40 | + * RBUF_PUSH(buf, other_element); |
| 41 | + * -- now RBUF_LEN(buf) == 2, buf[0] == some_element, buf[1] == other_element |
| 42 | + * |
| 43 | + * -- Free allocated memory: |
| 44 | + * RBUF_FREE(buf); |
| 45 | + * -- now buf == NULL, RBUF_LEN(buf) == 0, RBUF_CAP(buf) == 0 |
| 46 | + * |
| 47 | + * -- Explicitly increase allocated memory and set capacity: |
| 48 | + * RBUF_FIT(buf, 100); |
| 49 | + * -- now RBUF_LEN(buf) == 0, RBUF_CAP(buf) == 100 |
| 50 | + * |
| 51 | + * -- Resize buffer (does not initialize or zero memory!) |
| 52 | + * RBUF_RESIZE(buf, 200); |
| 53 | + * -- now RBUF_LEN(buf) == 200, RBUF_CAP(buf) == 200 |
| 54 | + * |
| 55 | + * -- To handle running out of memory: |
| 56 | + * bool ran_out_of_memory = !RBUF_TRYFIT(buf, 1000); |
| 57 | + * -- before RESIZE or PUSH. When out of memory, buf will stay unmodified. |
| 58 | + */ |
| 59 | + |
| 60 | +#include <retro_math.h> /* for MAX */ |
| 61 | +#include <stdlib.h> /* for malloc, realloc */ |
| 62 | + |
| 63 | +#define RBUF__HDR(b) (((struct rbuf__hdr *)(b))-1) |
| 64 | + |
| 65 | +#define RBUF_LEN(b) ((b) ? RBUF__HDR(b)->len : 0) |
| 66 | +#define RBUF_CAP(b) ((b) ? RBUF__HDR(b)->cap : 0) |
| 67 | +#define RBUF_END(b) ((b) + RBUF_LEN(b)) |
| 68 | +#define RBUF_SIZEOF(b) ((b) ? RBUF_LEN(b)*sizeof(*b) : 0) |
| 69 | + |
| 70 | +#define RBUF_FREE(b) ((b) ? (free(RBUF__HDR(b)), (b) = NULL) : 0) |
| 71 | +#define RBUF_FIT(b, n) ((size_t)(n) <= RBUF_CAP(b) ? 0 : (*(void**)(&(b)) = rbuf__grow((b), (n), sizeof(*(b))))) |
| 72 | +#define RBUF_PUSH(b, val) (RBUF_FIT((b), 1 + RBUF_LEN(b)), (b)[RBUF__HDR(b)->len++] = (val)) |
| 73 | +#define RBUF_POP(b) (b)[--RBUF__HDR(b)->len] |
| 74 | +#define RBUF_RESIZE(b, sz) (RBUF_FIT((b), (sz)), ((b) ? RBUF__HDR(b)->len = (sz) : 0)) |
| 75 | +#define RBUF_CLEAR(b) ((b) ? RBUF__HDR(b)->len = 0 : 0) |
| 76 | +#define RBUF_TRYFIT(b, n) (RBUF_FIT((b), (n)), (((b) && RBUF_CAP(b) >= (size_t)(n)) || !(n))) |
| 77 | + |
| 78 | +struct rbuf__hdr |
| 79 | +{ |
| 80 | + size_t len; |
| 81 | + size_t cap; |
| 82 | +}; |
| 83 | + |
| 84 | +static void *rbuf__grow(void *buf, |
| 85 | + size_t new_len, size_t elem_size) |
| 86 | +{ |
| 87 | + struct rbuf__hdr *new_hdr; |
| 88 | + size_t new_cap = MAX(2 * RBUF_CAP(buf), MAX(new_len, 16)); |
| 89 | + size_t new_size = sizeof(struct rbuf__hdr) + new_cap*elem_size; |
| 90 | + if (buf) |
| 91 | + { |
| 92 | + new_hdr = (struct rbuf__hdr *)realloc(RBUF__HDR(buf), new_size); |
| 93 | + if (!new_hdr) |
| 94 | + return buf; /* out of memory, return unchanged */ |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + new_hdr = (struct rbuf__hdr *)malloc(new_size); |
| 99 | + if (!new_hdr) |
| 100 | + return NULL; /* out of memory */ |
| 101 | + new_hdr->len = 0; |
| 102 | + } |
| 103 | + new_hdr->cap = new_cap; |
| 104 | + return new_hdr + 1; |
| 105 | +} |
| 106 | + |
| 107 | +#endif |
0 commit comments