1 /* 2 * Copyright (C) 2020-2021 Jo-Philipp Wich <jo@mein.io> 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #ifndef UCODE_UTIL_H 18 #define UCODE_UTIL_H 19 20 #include <stdio.h> 21 #include <stddef.h> 22 #include <stdlib.h> 23 #include <stdbool.h> 24 #include <stdarg.h> /* va_start(), va_end(), va_list */ 25 #include <string.h> /* strdup() */ 26 #include <json-c/json.h> 27 28 29 /* alignment & array size */ 30 31 #ifndef ALIGN 32 #define ALIGN(x) (((x) + sizeof(size_t) - 1) & -sizeof(size_t)) 33 #endif 34 35 #ifndef ARRAY_SIZE 36 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) 37 #endif 38 39 40 /* vector macros */ 41 42 #define UC_VECTOR_CHUNK_SIZE 8 43 44 #define uc_declare_vector(name, type) \ 45 typedef struct { \ 46 size_t count; \ 47 type *entries; \ 48 } name 49 50 #define uc_vector_grow(vec) \ 51 do { \ 52 if (((vec)->count % UC_VECTOR_CHUNK_SIZE) == 0) { \ 53 (vec)->entries = xrealloc((vec)->entries, sizeof((vec)->entries[0]) * ((vec)->count + UC_VECTOR_CHUNK_SIZE)); \ 54 memset(&(vec)->entries[(vec)->count], 0, sizeof((vec)->entries[0]) * UC_VECTOR_CHUNK_SIZE); \ 55 } \ 56 } while(0) 57 58 #define uc_vector_clear(vec) \ 59 do { \ 60 free((vec)->entries); \ 61 (vec)->entries = NULL; \ 62 (vec)->count = 0; \ 63 } while(0) 64 65 #define uc_vector_first(vec) \ 66 (&((vec)->entries[0])) 67 68 #define uc_vector_last(vec) \ 69 (&((vec)->entries[(vec)->count - 1])) 70 71 #define uc_vector_push(vec, val) do { \ 72 uc_vector_grow(vec); \ 73 (vec)->entries[(vec)->count++] = (val); \ 74 } while(0) 75 76 77 /* "failsafe" utility functions */ 78 79 static inline void *xcalloc(size_t size, size_t nmemb) { 80 void *ptr = calloc(size, nmemb); 81 82 if (!ptr) { 83 fprintf(stderr, "Out of memory\n"); 84 abort(); 85 } 86 87 return ptr; 88 } 89 90 static inline void *xalloc(size_t size) { 91 return xcalloc(1, size); 92 } 93 94 static inline void *xrealloc(void *ptr, size_t size) { 95 ptr = realloc(ptr, size); 96 97 if (!ptr) { 98 fprintf(stderr, "Out of memory\n"); 99 abort(); 100 } 101 102 return ptr; 103 } 104 105 static inline char *xstrdup(const char *s) { 106 char *ptr = strdup(s); 107 108 if (!ptr) { 109 fprintf(stderr, "Out of memory\n"); 110 abort(); 111 } 112 113 return ptr; 114 } 115 116 static inline struct json_tokener *xjs_new_tokener(void) { 117 struct json_tokener *tok = json_tokener_new(); 118 119 if (!tok) { 120 fprintf(stderr, "Out of memory\n"); 121 abort(); 122 } 123 124 return tok; 125 } 126 127 __attribute__((format(printf, 2, 0))) 128 static inline int xasprintf(char **strp, const char *fmt, ...) { 129 va_list ap; 130 int len; 131 132 va_start(ap, fmt); 133 len = vasprintf(strp, fmt, ap); 134 va_end(ap); 135 136 if (len == -1) { 137 fprintf(stderr, "Out of memory\n"); 138 abort(); 139 } 140 141 return len; 142 } 143 144 __attribute__((format(printf, 2, 0))) 145 static inline int xvasprintf(char **strp, const char *fmt, va_list ap) { 146 int len = vasprintf(strp, fmt, ap); 147 148 if (len == -1) { 149 fprintf(stderr, "Out of memory\n"); 150 abort(); 151 } 152 153 return len; 154 } 155 156 static inline struct printbuf *xprintbuf_new(void) { 157 struct printbuf *pb = printbuf_new(); 158 159 if (!pb) { 160 fprintf(stderr, "Out of memory\n"); 161 abort(); 162 } 163 164 return pb; 165 } 166 167 #endif /* UCODE_UTIL_H */ 168
This page was automatically generated by LXR 0.3.1. • OpenWrt