forked from ximik777/kphp-kdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst-utils.h
122 lines (94 loc) · 3.51 KB
/
st-utils.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
/*
This file is part of VK/KittenPHP-DB-Engine Library.
VK/KittenPHP-DB-Engine Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
VK/KittenPHP-DB-Engine Library 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. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with VK/KittenPHP-DB-Engine Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2012 Vkontakte Ltd
2012 Sergey Kopeliovich <[email protected]>
2012 Anton Timofeev <[email protected]>
*/
/**
* Author: Sergey Kopeliovich ([email protected])
* Anton Timofeev ([email protected])
* Utility functions useful everywhere.
* Created: 16.03.2012
* Update: 01.04.2012
*/
#pragma once
#ifdef _WIN32
# define lrand48 rand
# define srand48 srand
#endif
/**
* Meta
*/
#define st_end }}
/**
* Debug
*/
#define ST_TERMINATE { st_printf ("$1Terminated at $3" __FILE__ ":%ld$1$^\n", (long)__LINE__); exit (0); }
/**
* Custom types
*/
typedef char bool;
typedef unsigned char byte;
#define TRUE 1
#define FALSE 0
/**
* Utility functions
*/
#define st_relax_min(minx, _x) { \
typeof (_x) t = (_x); \
if ((minx) > t) { \
(minx) = t; \
} \
} \
#define st_relax_max(maxx, _x) { \
typeof (_x) t = (_x); \
if ((maxx) < t) { \
(maxx) = t; \
} \
} \
#define DECLARE_TEMPLATE_MIN_MAX(type, suffix) \
inline static type st_min##suffix (type x, type y) { \
return x < y ? x : y; \
} \
inline static type st_max##suffix (type x, type y) { \
return x > y ? x : y; \
} \
DECLARE_TEMPLATE_MIN_MAX(int, )
DECLARE_TEMPLATE_MIN_MAX(long long, ll)
DECLARE_TEMPLATE_MIN_MAX(double, f)
#define st_sqr(x) ((x) * (x))
/* Colored printf version, writes formatted string into stderr stream:
* $[0-7] pattern to change font color,
* ^[0-7] pattern to change background color,
* $^ pattern to restore default coloring.
* Color ordering: BLACK(0), RED(1), GREEN(2), YELLOW(3), BLUE(4), MAGENTA(5), CYAN(6), GREY(7)
* Use cases:
* st_printf ("$1^2red on green $4 blue on green number: %d ^3 blue on yellow $^ normal\n", 30303);
*/
int st_printf (char const* format, ...);
/* Write time span provided in seconds in human readable format. */
void st_print_tspan (int sec);
/**
* Working with engine kernel wrappers
*/
// Try to change user, exit if failed
void st_try_change_user (void);
// result of read_in will always be a string with length of len
// __data must have size at least (len + 1)
struct netbuffer;
inline void st_safe_read_in (struct netbuffer *H, char *__data, int len);
struct connection;
inline int st_return_one_key_flags (struct connection *c, const char *key, char *val, int vlen, int flags);
// Try to init vars for 'st_local_uid' function,
// asserts if failed.
void st_try_init_local_uid (void);