osDef.h 5.7 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _TD_OS_DEF_H_
#define _TD_OS_DEF_H_

#ifdef __cplusplus
extern "C" {
#endif

S
Shengliang Guan 已提交
23
#if defined(_TD_DARWIN_64)
H
Hongze Cheng 已提交
24
// specific
wafwerar's avatar
wafwerar 已提交
25 26
#ifndef __COMPAR_FN_T
#define __COMPAR_FN_T
H
Hongze Cheng 已提交
27
typedef int (*__compar_fn_t)(const void *, const void *);
wafwerar's avatar
wafwerar 已提交
28
#endif
S
Shengliang Guan 已提交
29

H
Hongze Cheng 已提交
30 31 32 33
// for send function in tsocket.c
#if defined(MSG_NOSIGNAL)
#undef MSG_NOSIGNAL
#endif
S
Shengliang Guan 已提交
34

H
Hongze Cheng 已提交
35
#define MSG_NOSIGNAL 0
S
Shengliang Guan 已提交
36

H
Hongze Cheng 已提交
37 38 39
#define SO_NO_CHECK  0x1234
#define SOL_TCP      0x1234
#define TCP_KEEPIDLE 0x1234
S
Shengliang Guan 已提交
40

H
Hongze Cheng 已提交
41 42 43
#ifndef PTHREAD_MUTEX_RECURSIVE_NP
#define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
#endif
S
Shengliang Guan 已提交
44 45 46
#endif

#if defined(_ALPINE)
wafwerar's avatar
wafwerar 已提交
47 48
#ifndef __COMPAR_FN_T
#define __COMPAR_FN_T
H
Hongze Cheng 已提交
49 50 51 52 53
typedef int (*__compar_fn_t)(const void *, const void *);
#endif
void error(int, int, const char *);
#ifndef PTHREAD_MUTEX_RECURSIVE_NP
#define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
wafwerar's avatar
wafwerar 已提交
54
#endif
S
Shengliang Guan 已提交
55 56
#endif

57
#if defined(WINDOWS)
H
Hongze Cheng 已提交
58 59
char *stpcpy(char *dest, const char *src);
char *stpncpy(char *dest, const char *src, int n);
S
Shengliang Guan 已提交
60

H
Hongze Cheng 已提交
61
// specific
wafwerar's avatar
wafwerar 已提交
62 63
#ifndef __COMPAR_FN_T
#define __COMPAR_FN_T
H
Hongze Cheng 已提交
64 65 66 67 68 69 70 71 72 73 74
typedef int (*__compar_fn_t)(const void *, const void *);
#endif
#define ssize_t int
#define _SSIZE_T_
#define bzero(ptr, size) memset((ptr), 0, (size))
#define strcasecmp       _stricmp
#define strncasecmp      _strnicmp
#define wcsncasecmp      _wcsnicmp
#define strtok_r         strtok_s
// #define snprintf _snprintf
#define in_addr_t unsigned long
75
//  #define socklen_t int
S
Shengliang Guan 已提交
76

H
Hongze Cheng 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
char *strsep(char **stringp, const char *delim);
char *getpass(const char *prefix);
char *strndup(const char *s, int n);

// for send function in tsocket.c
#define MSG_NOSIGNAL 0
#define SO_NO_CHECK  0x1234
#define SOL_TCP      0x1234

#define SHUT_RDWR SD_BOTH
#define SHUT_RD   SD_RECEIVE
#define SHUT_WR   SD_SEND

#define LOCK_EX 1
#define LOCK_NB 2
#define LOCK_UN 3

#ifndef PATH_MAX
#define PATH_MAX 256
#endif

typedef struct {
  int   we_wordc;
  char *we_wordv[1];
  int   we_offs;
  char  wordPos[1025];
} wordexp_t;
int  wordexp(char *words, wordexp_t *pwordexp, int flags);
void wordfree(wordexp_t *pwordexp);

#define openlog(a, b, c)
#define closelog()
#define LOG_ERR  0
#define LOG_INFO 1
void syslog(int unused, const char *format, ...);
#endif  // WINDOWS

H
Hongze Cheng 已提交
114
#ifndef WINDOWS
H
Hongze Cheng 已提交
115 116 117
#ifndef O_BINARY
#define O_BINARY 0
#endif
H
Hongze Cheng 已提交
118 119
#endif

H
Hongze Cheng 已提交
120 121
#define POINTER_SHIFT(p, b)      ((void *)((char *)(p) + (b)))
#define POINTER_DISTANCE(p1, p2) ((char *)(p1) - (char *)(p2))
S
Shengliang Guan 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

#ifndef NDEBUG
#define ASSERT(x) assert(x)
#else
#define ASSERT(x)
#endif

#ifndef UNUSED
#define UNUSED(x) ((void)(x))
#endif

#ifdef UNUSED_FUNC
#undefine UNUSED_FUNC
#endif

#ifdef UNUSED_PARAM
#undef UNUSED_PARAM
#endif

#if defined(__GNUC__)
#define UNUSED_PARAM(x) _UNUSED##x __attribute__((unused))
H
Hongze Cheng 已提交
143
#define UNUSED_FUNC     __attribute__((unused))
S
Shengliang Guan 已提交
144 145 146 147 148 149 150 151 152 153
#else
#define UNUSED_PARAM(x) x
#define UNUSED_FUNC
#endif

#ifdef tListLen
#undefine tListLen
#endif
#define tListLen(x) (sizeof(x) / sizeof((x)[0]))

H
refact  
Hongze Cheng 已提交
154 155 156 157 158 159
#if defined(__GNUC__)
#define FORCE_INLINE inline __attribute__((always_inline))
#else
#define FORCE_INLINE
#endif

S
Shengliang Guan 已提交
160 161 162 163 164 165 166 167 168 169 170
#define DEFAULT_UNICODE_ENCODEC "UCS-4LE"

#define DEFAULT_COMP(x, y)       \
  do {                           \
    if ((x) == (y)) {            \
      return 0;                  \
    } else {                     \
      return (x) < (y) ? -1 : 1; \
    }                            \
  } while (0)

H
Haojun Liao 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
#define DEFAULT_DOUBLE_COMP(x, y)         \
  do {                                    \
    if (isnan(x) && isnan(y)) {           \
      return 0;                           \
    }                                     \
    if (isnan(x)) {                       \
      return -1;                          \
    }                                     \
    if (isnan(y)) {                       \
      return 1;                           \
    }                                     \
    if (fabs((x) - (y)) <= DBL_EPSILON) { \
      return 0;                           \
    } else {                              \
      return (x) < (y) ? -1 : 1;          \
    }                                     \
S
Shengliang Guan 已提交
187 188 189 190 191 192 193 194 195 196 197
  } while (0)

#define DEFAULT_FLOAT_COMP(x, y) DEFAULT_DOUBLE_COMP(x, y)

#define ALIGN_NUM(n, align) (((n) + ((align)-1)) & (~((align)-1)))

// align to 8bytes
#define ALIGN8(n) ALIGN_NUM(n, 8)

#undef threadlocal
#ifdef _ISOC11_SOURCE
H
Hongze Cheng 已提交
198
#define threadlocal _Thread_local
S
Shengliang Guan 已提交
199
#elif defined(__APPLE__)
H
Hongze Cheng 已提交
200
#define threadlocal __thread
S
Shengliang Guan 已提交
201
#elif defined(__GNUC__) && !defined(threadlocal)
H
Hongze Cheng 已提交
202
#define threadlocal __thread
S
Shengliang Guan 已提交
203
#else
H
Hongze Cheng 已提交
204
#define threadlocal __declspec(thread)
S
Shengliang Guan 已提交
205 206
#endif

wafwerar's avatar
wafwerar 已提交
207
#ifdef WINDOWS
H
Hongze Cheng 已提交
208
#define PRIzu "ld"
S
Shengliang Guan 已提交
209
#else
H
Hongze Cheng 已提交
210
#define PRIzu "zu"
S
Shengliang Guan 已提交
211
#endif
212

213
#if !defined(WINDOWS)
H
Hongze Cheng 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
#if defined(_TD_DARWIN_64)
// MacOS
#if !defined(_GNU_SOURCE)
#define setThreadName(name)     \
  do {                          \
    pthread_setname_np((name)); \
  } while (0)
#else
// pthread_setname_np not defined
#define setThreadName(name)
#endif
#else
// Linux, length of name must <= 16 (the last '\0' included)
#define setThreadName(name)     \
  do {                          \
    prctl(PR_SET_NAME, (name)); \
  } while (0)
#endif
232
#else
H
Hongze Cheng 已提交
233 234
// Windows
#define setThreadName(name)
235 236
#endif

S
Shengliang Guan 已提交
237
#if defined(_WIN32)
H
Hongze Cheng 已提交
238
#define TD_DIRSEP "\\"
S
Shengliang Guan 已提交
239
#else
H
Hongze Cheng 已提交
240
#define TD_DIRSEP "/"
S
Shengliang Guan 已提交
241 242
#endif

S
os  
Shengliang Guan 已提交
243 244 245 246
#define TD_LOCALE_LEN   64
#define TD_CHARSET_LEN  64
#define TD_TIMEZONE_LEN 96

H
refact  
Hongze Cheng 已提交
247 248 249 250
#ifdef __cplusplus
}
#endif

251
#endif /*_TD_OS_DEF_H_*/