1 /* 2 * Copyright (c) 2020, Rockchip Electronics Co., Ltd 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 #ifndef J2S_COMMON_H 16 #define J2S_COMMON_H 17 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 22 #include <fcntl.h> 23 #include <stdbool.h> 24 #include <stdint.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <unistd.h> 29 30 #define J2S_VERSION "1.0.0~20201229" 31 32 //#define DEBUG 33 34 #ifndef unlikely 35 #define unlikely(x) __builtin_expect((x), 0) 36 #endif 37 38 #ifndef MSG 39 #define MSG(level, fmt, args...) \ 40 fprintf(stderr, #level ": %s(%d) [%s]: " fmt, __FILE__, __LINE__, \ 41 __func__, ##args) 42 #endif 43 44 #ifndef DBG 45 #if defined(NDEBUG) 46 #define DBG(fmt, args...) 47 #elif defined(DEBUG) 48 #define DBG(fmt, args...) MSG(D, fmt, ##args) 49 #else 50 #define DBG(fmt, args...) \ 51 if (getenv("J2S_DEBUG")) \ 52 MSG(D, fmt, ##args) 53 #endif 54 #endif 55 56 #ifndef INFO 57 #define INFO(fmt, args...) MSG(I, fmt, ##args) 58 #endif 59 60 #ifndef WARN 61 #define WARN(fmt, args...) MSG(W, fmt, ##args) 62 #endif 63 64 #ifndef ERR 65 #define ERR(fmt, args...) { \ 66 MSG(E, fmt, ##args); \ 67 exit(-1); \ 68 } 69 #endif 70 71 #ifndef DASSERT 72 #define DASSERT(b, action) \ 73 do { \ 74 if (unlikely(!(b))) { \ 75 ERR("debug assertion failure (%s)\n", #b); \ 76 action; \ 77 } \ 78 } while (0) 79 #endif 80 81 #ifndef DASSERT_MSG 82 #define DASSERT_MSG(b, action, fmt, args...) \ 83 do { \ 84 if (unlikely(!(b))) { \ 85 ERR(fmt, ##args); \ 86 action; \ 87 } \ 88 } while (0) 89 #endif 90 91 #define MAX_LINE 8192 92 #define MAX_NAME 32 93 94 #define J2S_DESC_MAGIC "@desc:" 95 96 typedef enum { 97 J2S_TYPE_INT_8 = 1, 98 J2S_TYPE_UINT_8, 99 J2S_TYPE_INT_16, 100 J2S_TYPE_UINT_16, 101 J2S_TYPE_INT_32, 102 J2S_TYPE_UINT_32, 103 J2S_TYPE_INT_64, 104 J2S_TYPE_UINT_64, 105 J2S_TYPE_FLOAT, 106 J2S_TYPE_DOUBLE, 107 J2S_TYPE_STRING, /* For char *|char [] */ 108 J2S_TYPE_STRUCT, 109 } j2s_type; 110 111 typedef enum { 112 J2S_FLAG_ARRAY = 1 << 0, 113 J2S_FLAG_POINTER = 1 << 1, 114 J2S_FLAG_DEP_ARRAY = 1 << 2, /* int array[1][2] */ 115 J2S_FLAG_DEP_POINTER = 1 << 3, /* char ** */ 116 J2S_FLAG_ARRAY_POINTER = 1 << 4, /* typedef int (*array_ptr_int)[4] */ 117 } j2s_flag; 118 119 #define J2S_IS_SIMPLE_STRING(obj) \ 120 ((obj)->type == J2S_TYPE_STRING && ((obj)->flags == J2S_FLAG_ARRAY || (obj)->flags == J2S_FLAG_POINTER)) 121 122 #define J2S_IS_ARRAY(obj) \ 123 ((obj)->flags & J2S_FLAG_ARRAY && !((obj)->flags & J2S_FLAG_ARRAY_POINTER)) 124 125 #define J2S_IS_POINTER(obj) \ 126 ((obj)->flags & J2S_FLAG_POINTER && !J2S_IS_ARRAY(obj)) 127 128 #ifdef __cplusplus 129 } 130 #endif 131 132 #endif // J2S_COMMON_H 133