1 /* Copyright (C) 2017 the mpv developers 2 * 3 * Permission to use, copy, modify, and/or distribute this software for any 4 * purpose with or without fee is hereby granted, provided that the above 5 * copyright notice and this permission notice appear in all copies. 6 * 7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 */ 15 16 #ifndef TA_TALLOC_H_ 17 #define TA_TALLOC_H_ 18 19 #include <string.h> 20 21 #include "ta.h" 22 23 // Note: all talloc wrappers are wired to the "x" functions, which abort on OOM. 24 // libtalloc doesn't do that, but the mplayer2 internal copy of it did. 25 26 #define talloc ta_xnew 27 #define talloc_zero ta_xznew 28 29 #define talloc_array ta_xnew_array 30 #define talloc_zero_array ta_xznew_array 31 32 #define talloc_array_size ta_xnew_array_size 33 #define talloc_realloc ta_xrealloc 34 #define talloc_ptrtype ta_xnew_ptrtype 35 #define talloc_array_ptrtype ta_xnew_array_ptrtype 36 37 #define talloc_steal ta_steal 38 #define talloc_realloc_size ta_xrealloc_size 39 #define talloc_new ta_xnew_context 40 #define talloc_set_destructor ta_set_destructor 41 #define talloc_enable_leak_report ta_enable_leak_report 42 #define talloc_size ta_xalloc_size 43 #define talloc_zero_size ta_xzalloc_size 44 #define talloc_get_size ta_get_size 45 #define talloc_free_children ta_free_children 46 #define talloc_free ta_free 47 #define talloc_dup ta_xdup 48 #define talloc_memdup ta_xmemdup 49 #define talloc_strdup ta_xstrdup 50 #define talloc_strndup ta_xstrndup 51 #define talloc_asprintf ta_xasprintf 52 #define talloc_vasprintf ta_xvasprintf 53 54 // Don't define linker-level symbols, as that would clash with real libtalloc. 55 #define talloc_strdup_append ta_talloc_strdup_append 56 #define talloc_strdup_append_buffer ta_talloc_strdup_append_buffer 57 #define talloc_strndup_append ta_talloc_strndup_append 58 #define talloc_strndup_append_buffer ta_talloc_strndup_append_buffer 59 #define talloc_vasprintf_append ta_talloc_vasprintf_append 60 #define talloc_vasprintf_append_buffer ta_talloc_vasprintf_append_buffer 61 #define talloc_asprintf_append ta_talloc_asprintf_append 62 #define talloc_asprintf_append_buffer ta_talloc_asprintf_append_buffer 63 64 char *ta_talloc_strdup(void *t, const char *p); 65 char *ta_talloc_strdup_append(char *s, const char *a); 66 char *ta_talloc_strdup_append_buffer(char *s, const char *a); 67 68 char *ta_talloc_strndup(void *t, const char *p, size_t n); 69 char *ta_talloc_strndup_append(char *s, const char *a, size_t n); 70 char *ta_talloc_strndup_append_buffer(char *s, const char *a, size_t n); 71 72 char *ta_talloc_vasprintf_append(char *s, const char *fmt, va_list ap) TA_PRF(2, 0); 73 char *ta_talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) TA_PRF(2, 0); 74 75 char *ta_talloc_asprintf_append(char *s, const char *fmt, ...) TA_PRF(2, 3); 76 char *ta_talloc_asprintf_append_buffer(char *s, const char *fmt, ...) TA_PRF(2, 3); 77 78 // mpv specific stuff - should be made part of proper TA API 79 80 #define TA_FREEP(pctx) do {talloc_free(*(pctx)); *(pctx) = NULL;} while(0) 81 82 // Return number of allocated entries in typed array p[]. 83 #define MP_TALLOC_AVAIL(p) (talloc_get_size(p) / sizeof((p)[0])) 84 85 // Resize array p so that p[count-1] is the last valid entry. ctx as ta parent. 86 #define MP_RESIZE_ARRAY(ctx, p, count) \ 87 do { \ 88 (p) = ta_xrealloc_size(ctx, p, \ 89 ta_calc_array_size(sizeof((p)[0]), count)); \ 90 } while (0) 91 92 // Resize array p so that p[nextidx] is accessible. Preallocate additional 93 // space to make appending more efficient, never shrink. ctx as ta parent. 94 #define MP_TARRAY_GROW(ctx, p, nextidx) \ 95 do { \ 96 size_t nextidx_ = (nextidx); \ 97 if (nextidx_ >= MP_TALLOC_AVAIL(p)) \ 98 MP_RESIZE_ARRAY(ctx, p, ta_calc_prealloc_elems(nextidx_)); \ 99 } while (0) 100 101 // Append the last argument to array p (with count idxvar), basically: 102 // p[idxvar++] = ...; ctx as ta parent. 103 #define MP_TARRAY_APPEND(ctx, p, idxvar, ...) \ 104 do { \ 105 MP_TARRAY_GROW(ctx, p, idxvar); \ 106 (p)[(idxvar)] = (__VA_ARGS__); \ 107 (idxvar)++; \ 108 } while (0) 109 110 // Insert the last argument at p[at] (array p with count idxvar), basically: 111 // for(idxvar-1 down to at) p[n+1] = p[n]; p[at] = ...; idxvar++; 112 // ctx as ta parent. Required: at >= 0 && at <= idxvar. 113 #define MP_TARRAY_INSERT_AT(ctx, p, idxvar, at, ...)\ 114 do { \ 115 size_t at_ = (at); \ 116 assert(at_ <= (idxvar)); \ 117 MP_TARRAY_GROW(ctx, p, idxvar); \ 118 memmove((p) + at_ + 1, (p) + at_, \ 119 ((idxvar) - at_) * sizeof((p)[0])); \ 120 (idxvar)++; \ 121 (p)[at_] = (__VA_ARGS__); \ 122 } while (0) 123 124 // Given an array p with count idxvar, insert c elements at p[at], so that 125 // p[at] to p[at+c-1] can be accessed. The elements at p[at] and following 126 // are shifted up by c before insertion. The new entries are uninitialized. 127 // ctx as ta parent. Required: at >= 0 && at <= idxvar. 128 #define MP_TARRAY_INSERT_N_AT(ctx, p, idxvar, at, c)\ 129 do { \ 130 size_t at_ = (at); \ 131 assert(at_ <= (idxvar)); \ 132 size_t c_ = (c); \ 133 MP_TARRAY_GROW(ctx, p, (idxvar) + c_); \ 134 memmove((p) + at_ + c_, (p) + at_, \ 135 ((idxvar) - at_) * sizeof((p)[0])); \ 136 (idxvar) += c_; \ 137 } while (0) 138 139 // Remove p[at] from array p with count idxvar (inverse of MP_TARRAY_INSERT_AT()). 140 // Doesn't actually free any memory, or do any other talloc calls. 141 #define MP_TARRAY_REMOVE_AT(p, idxvar, at) \ 142 do { \ 143 size_t at_ = (at); \ 144 assert(at_ <= (idxvar)); \ 145 memmove((p) + at_, (p) + at_ + 1, \ 146 ((idxvar) - at_ - 1) * sizeof((p)[0])); \ 147 (idxvar)--; \ 148 } while (0) 149 150 // Returns whether or not there was any element to pop. 151 #define MP_TARRAY_POP(p, idxvar, out) \ 152 ((idxvar) > 0 \ 153 ? (*(out) = (p)[--(idxvar)], true) \ 154 : false \ 155 ) 156 157 #endif 158