• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/include/ucode/util.h

  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 #ifndef __hidden
 30 #define __hidden __attribute__((visibility("hidden")))
 31 #endif
 32 
 33 
 34 /* alignment & array size */
 35 
 36 #ifndef ALIGN
 37 #define ALIGN(x) (((x) + sizeof(size_t) - 1) & -sizeof(size_t))
 38 #endif
 39 
 40 #ifndef ARRAY_SIZE
 41 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
 42 #endif
 43 
 44 
 45 /* vector macros */
 46 
 47 #define UC_VECTOR_CHUNK_SIZE 8
 48 
 49 #define uc_declare_vector(name, type) \
 50         typedef struct { \
 51                 size_t count; \
 52                 type *entries; \
 53         } name
 54 
 55 #define uc_vector_grow(vec) \
 56         do { \
 57                 if (((vec)->count % UC_VECTOR_CHUNK_SIZE) == 0) { \
 58                         (vec)->entries = xrealloc((vec)->entries, sizeof((vec)->entries[0]) * ((vec)->count + UC_VECTOR_CHUNK_SIZE)); \
 59                         memset(&(vec)->entries[(vec)->count], 0, sizeof((vec)->entries[0]) * UC_VECTOR_CHUNK_SIZE); \
 60                 } \
 61         } while(0)
 62 
 63 #define uc_vector_clear(vec) \
 64         do { \
 65                 free((vec)->entries); \
 66                 (vec)->entries = NULL; \
 67                 (vec)->count = 0; \
 68         } while(0)
 69 
 70 #define uc_vector_first(vec) \
 71         (&((vec)->entries[0]))
 72 
 73 #define uc_vector_last(vec) \
 74         (&((vec)->entries[(vec)->count - 1]))
 75 
 76 #define uc_vector_push(vec, val) do { \
 77         uc_vector_grow(vec); \
 78         (vec)->entries[(vec)->count++] = (val); \
 79 } while(0)
 80 
 81 
 82 /* "failsafe" utility functions */
 83 
 84 static inline void *xcalloc(size_t size, size_t nmemb) {
 85         void *ptr = calloc(size, nmemb);
 86 
 87         if (!ptr) {
 88                 fprintf(stderr, "Out of memory\n");
 89                 abort();
 90         }
 91 
 92         return ptr;
 93 }
 94 
 95 static inline void *xalloc(size_t size) {
 96         return xcalloc(1, size);
 97 }
 98 
 99 static inline void *xrealloc(void *ptr, size_t size) {
100         ptr = realloc(ptr, size);
101 
102         if (!ptr) {
103                 fprintf(stderr, "Out of memory\n");
104                 abort();
105         }
106 
107         return ptr;
108 }
109 
110 static inline char *xstrdup(const char *s) {
111         char *ptr = strdup(s);
112 
113         if (!ptr) {
114                 fprintf(stderr, "Out of memory\n");
115                 abort();
116         }
117 
118         return ptr;
119 }
120 
121 static inline struct json_tokener *xjs_new_tokener(void) {
122         struct json_tokener *tok = json_tokener_new();
123 
124         if (!tok) {
125                 fprintf(stderr, "Out of memory\n");
126                 abort();
127         }
128 
129         return tok;
130 }
131 
132 __attribute__((format(printf, 2, 0)))
133 static inline int xasprintf(char **strp, const char *fmt, ...) {
134         va_list ap;
135         int len;
136 
137         va_start(ap, fmt);
138         len = vasprintf(strp, fmt, ap);
139         va_end(ap);
140 
141         if (len == -1) {
142                 fprintf(stderr, "Out of memory\n");
143                 abort();
144         }
145 
146         return len;
147 }
148 
149 __attribute__((format(printf, 2, 0)))
150 static inline int xvasprintf(char **strp, const char *fmt, va_list ap) {
151         int len = vasprintf(strp, fmt, ap);
152 
153         if (len == -1) {
154                 fprintf(stderr, "Out of memory\n");
155                 abort();
156         }
157 
158         return len;
159 }
160 
161 static inline struct printbuf *xprintbuf_new(void) {
162         struct printbuf *pb = printbuf_new();
163 
164         if (!pb) {
165                 fprintf(stderr, "Out of memory\n");
166                 abort();
167         }
168 
169         return pb;
170 }
171 
172 #endif /* UCODE_UTIL_H */
173 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt