-
Notifications
You must be signed in to change notification settings - Fork 375
/
util.h
144 lines (121 loc) · 4.42 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (C) 2010 Mail.RU
* Copyright (C) 2010 Yuriy Vostrikov
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef TARANTOOL_UTIL_H
#define TARANTOOL_UTIL_H
#include <unistd.h>
#ifndef MAX
# define MAX(a, b) (a) > (b) ? (a) : (b)
# define MIN(a, b) (a) < (b) ? (a) : (b)
#endif
/* Macros to define enum and corresponding strings. */
#define ENUM0_MEMBER(s, ...) s,
#define ENUM_MEMBER(s, v, ...) s = v,
#define ENUM_STRS_MEMBER(s, v, ...) [s] = #s,
#define ENUM0(enum_name, enum_members) enum enum_name {enum_members(ENUM0_MEMBER) enum_name##_MAX}
#define ENUM(enum_name, enum_members) enum enum_name {enum_members(ENUM_MEMBER) enum_name##_MAX}
#define STRS(enum_name, enum_members) \
char *enum_name##_strs[enum_name##_MAX + 1] = {enum_members(ENUM_STRS_MEMBER) '\0'}
// Macros for printf functions
#include <inttypes.h>
#ifdef __x86_64__
#define PRI_SZ "lu"
#define PRI_SSZ "ld"
#define PRI_OFFT "lu"
#define PRI_XFFT "lx"
#else
#define PRI_SZ "u"
#define PRI_SSZ "d"
#define PRI_OFFT "llu"
#define PRI_XFFT "llx"
#endif
#define nelem(x) (sizeof((x))/sizeof((x)[0]))
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
#define field_sizeof(compound_type, field) sizeof(((compound_type *)NULL)->field)
#ifndef offsetof
#define offsetof(type, member) ((size_t) &((type *)0)->member)
#endif
#ifndef __offsetof
#define __offsetof offsetof
#endif
#ifndef lengthof
#define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
#endif
#ifndef TYPEALIGN
#define TYPEALIGN(ALIGNVAL,LEN) \
(((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
#define SHORTALIGN(LEN) TYPEALIGN(sizeof(int16_t), (LEN))
#define INTALIGN(LEN) TYPEALIGN(sizeof(int32_t), (LEN))
#define MAXALIGN(LEN) TYPEALIGN(sizeof(int64_t), (LEN))
#define PTRALIGN(LEN) TYPEALIGN(sizeof(void*), (LEN))
#define CACHEALIGN(LEN) TYPEALIGN(32, (LEN))
#endif
#define __packed__ __attribute__((packed))
#define __noinline__ __attribute__((noinline))
#define __unused__ __attribute__((unused))
#define __cleanup__(f) __attribute__((cleanup (f)))
#define __regparm2__ __attribute__((regparm(2)))
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
#define CRLF "\r\n"
#ifdef GCC
# define FORMAT_PRINTF gnu_printf
#else
# define FORMAT_PRINTF printf
#endif
extern int forked;
pid_t tfork();
void close_all_xcpt(int fdc, ...);
void coredump(int dump_interval);
void *xrealloc(void *ptr, size_t size);
void __gcov_flush();
extern void *__libc_stack_end;
#ifdef ENABLE_BACKTRACE
char *backtrace(void *frame, void *stack, size_t stack_size);
#endif /* ENABLE_BACKTRACE */
#ifdef HAVE_BFD
struct symbol {
void *addr;
const char *name;
void *end;
};
struct symbol *addr2symbol(void *addr);
void load_symbols(const char *name);
#endif /* HAVE_BFD */
#ifdef NDEBUG
# define assert(pred) (void)(0)
#else
# define assert(pred) ((pred) ? (void)(0) : assert_fail (#pred, __FILE__, __LINE__, __FUNCTION__))
void assert_fail(const char *assertion, const char *file,
unsigned int line, const char *function) __attribute__ ((noreturn));
#endif
#endif