FreeTDS API
macros.h
1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Brian Bruns
3  * Copyright (C) 2010-2017 Frediano Ziglio
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef _tdsguard_dWrEWKe6Aqdq6AyZsaKD0V_
22 #define _tdsguard_dWrEWKe6Aqdq6AyZsaKD0V_
23 
24 #ifndef _freetds_config_h_
25 #error should include config.h before
26 #endif
27 
28 #if HAVE_STDDEF_H
29 #include <stddef.h>
30 #endif /* HAVE_STDDEF_H */
31 
32 #define TDS_ZERO_FREE(x) do {free((x)); (x) = NULL;} while(0)
33 #define TDS_VECTOR_SIZE(x) (sizeof(x)/sizeof(x[0]))
34 
35 #ifdef offsetof
36 #define TDS_OFFSET(type, field) offsetof(type, field)
37 #else
38 #define TDS_OFFSET(type, field) (((char*)&((type*)0)->field)-((char*)0))
39 #endif
40 
41 #if ENABLE_EXTRA_CHECKS
42 # define TDS_EXTRA_CHECK(stmt) stmt
43 #else
44 # define TDS_EXTRA_CHECK(stmt)
45 #endif
46 
47 # if defined(__llvm__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
48 # define TDS_COMPILE_CHECK(name,check) \
49  _Static_assert(check,#name)
50 # else
51 # define TDS_COMPILE_CHECK(name,check) \
52  typedef char _tds_check_##name[(check)?1:-1] TDS_UNUSED
53 # endif
54 
55 #if defined(__GNUC__) && __GNUC__ >= 3
56 # define TDS_LIKELY(x) __builtin_expect(!!(x), 1)
57 # define TDS_UNLIKELY(x) __builtin_expect(!!(x), 0)
58 #else
59 # define TDS_LIKELY(x) (x)
60 # define TDS_UNLIKELY(x) (x)
61 #endif
62 
63 #if ENABLE_EXTRA_CHECKS && defined(__GNUC__) && __GNUC__ >= 4
64 #define TDS_WUR __attribute__ ((__warn_unused_result__))
65 #else
66 #define TDS_WUR
67 #endif
68 
69 #if defined(__GNUC__) && __GNUC__ >= 2
70 #define TDS_UNUSED __attribute__ ((unused))
71 #else
72 #define TDS_UNUSED
73 #endif
74 
75 #define TDS_INT2PTR(i) ((void*)(((char*)0)+((ptrdiff_t)(i))))
76 #define TDS_PTR2INT(p) ((int)(((char*)(p))-((char*)0)))
77 
78 #define TDS_MAX(a,b) ( (a) > (b) ? (a) : (b) )
79 #define TDS_MIN(a,b) ( (a) < (b) ? (a) : (b) )
80 #define TDS_CLAMP(x,a,b) ( (x) < (a) ? (a) : (x) > (b) ? (b) : (x) )
81 
82 #define tds_new(type, n) ((type *) malloc(sizeof(type) * (n)))
83 #define tds_new0(type, n) ((type *) calloc(n, sizeof(type)))
84 
85 #endif