-
Notifications
You must be signed in to change notification settings - Fork 986
/
gen_utils.h
348 lines (295 loc) · 7.33 KB
/
gen_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#ifndef __CLASS_PTR_ARRAY_H
#define __CLASS_PTR_ARRAY_H
#include <memory>
#include <queue>
#include "proxysql.h"
#include "sqlite3db.h"
#define MIN_ARRAY_LEN 8
#define MIN_ARRAY_DELETE_RATIO 8
static unsigned int l_near_pow_2 (unsigned int n) {
unsigned int i = 1;
while (i < n) i <<= 1;
return i ? i : n;
}
#ifndef def_fastrand
inline int fastrand() {
g_seed = (214013*g_seed+2531011);
return (g_seed>>16)&0x7FFF;
}
#define def_fastrand
#endif
class PtrArray {
private:
void expand(unsigned int more) {
if ( (len+more) > size ) {
unsigned int new_size=l_near_pow_2(len+more);
void *new_pdata=malloc(new_size*sizeof(void *));
memset(new_pdata,0,new_size*sizeof(void *));
if (pdata) {
memcpy(new_pdata,pdata,size*sizeof(void *));
free(pdata);
}
size=new_size;
pdata=(void **)new_pdata;
}
}
void shrink() {
unsigned int new_size=l_near_pow_2(len+1);
pdata=(void **)realloc(pdata,new_size*sizeof(void *));
size=new_size;
}
public:
unsigned int len;
unsigned int size;
void **pdata;
PtrArray(unsigned int __size=0) {
len=0;
pdata=NULL;
size=0;
if (__size) {
expand(__size);
}
size=__size;
}
~PtrArray() {
if (pdata) ( free(pdata) );
pdata=NULL;
}
void reset() {
len=0;
if (pdata) ( free(pdata) );
pdata=NULL;
size=0;
}
void *index(unsigned int i) { return pdata[i];}
void add(void *p) {
if (len==size) {
expand(1);
}
pdata[len++]=p;
}
bool remove(void *p) {
unsigned int i;
for (i=0; i<len; i++) {
if (pdata[i]==p) {
remove_index(i);
return true;
}
}
return false;
}
void * remove_index(unsigned int i) {
void *r=pdata[i];
if (i != (len-1)) {
memmove((void **)pdata+i,(void **)pdata+i+1,(len-i-1)*sizeof(void *));
}
len--;
if ( ( len>MIN_ARRAY_LEN ) && ( size > len*MIN_ARRAY_DELETE_RATIO ) ) {
shrink();
}
return r;
}
bool remove_fast(void *p) {
unsigned int i;
for (i=0; i<len; i++) {
if (pdata[i]==p) {
remove_index_fast(i);
return true;
}
}
return false;
}
void * remove_index_fast(unsigned int i) {
void *r=pdata[i];
if (i != (len-1))
pdata[i]=pdata[len-1];
len--;
if ( ( len>MIN_ARRAY_LEN ) && ( size > len*MIN_ARRAY_DELETE_RATIO ) ) {
//shrink(); // FIXME: when shrink is called, is r invalid ??
}
return r;
}
};
class PtrSizeArray {
private:
void expand(unsigned int);
void shrink();
public:
void * operator new(size_t);
void operator delete(void *);
unsigned int len;
unsigned int size;
PtrSize_t *pdata;
PtrSizeArray(unsigned int __size=0);
~PtrSizeArray();
void add(void *p, unsigned int s) {
if (len==size) {
expand(1);
}
pdata[len].ptr=p;
pdata[len].size=s;
len++;
//#ifdef DEBUG
// mysql_hdr *m=(mysql_hdr *)p;
// fprintf(stderr,"%u %u\n", m->pkt_id, m->pkt_length);
//#endif /* DEBUG */
};
void remove_index(unsigned int i, PtrSize_t *ps) {
if (ps) {
ps->ptr=pdata[i].ptr;
ps->size=pdata[i].size;
}
if (i != (len-1)) {
memmove(pdata+i,pdata+i+1,(len-i-1)*sizeof(PtrSize_t));
}
len--;
};
void remove_index_range(unsigned int i, unsigned int s) {
if (i != (len-s)) {
memmove(pdata+i,pdata+i+s,(len-i-s)*sizeof(PtrSize_t));
}
len-=s;
};
void remove_index_fast(unsigned int, PtrSize_t *);
void copy_add(PtrSizeArray *, unsigned int, unsigned int);
PtrSize_t * index(unsigned int i) {
return &pdata[i];
}
unsigned int total_size(unsigned int _min_size=0) {
unsigned int intsize=0;
unsigned int i=0;
for (i = 0 ; i < len ; i++) {
PtrSize_t *pts = index(i);
if (pts->size > _min_size) {
intsize += pts->size;
} else {
intsize += _min_size;
}
}
return intsize;
}
};
struct buffer_t {
void * data = nullptr;
size_t len = 0;
size_t capacity = 0;
};
class FixedSizeQueue : public std::queue<buffer_t> {
private:
using std::queue<buffer_t>::push;
using std::queue<buffer_t>::emplace;
using std::queue<buffer_t>::swap;
size_t _max_size = 0;
public:
FixedSizeQueue() = default;
FixedSizeQueue(size_t max_size) : _max_size(max_size) {}
~FixedSizeQueue() {
while (empty() == false) {
auto& node = front();
l_free(node.len, node.data);
pop();
}
}
inline
size_t get_max_size() const {
return _max_size;
}
void set_max_size(size_t max_size) {
if (_max_size == max_size)
return;
_max_size = max_size;
if (size() > max_size) {
while (size() != max_size) {
auto& node = front();
l_free(node.len, node.data);
pop();
}
}
}
// using template here to create compile-time separate definition of push, one for true and one for false
template<bool ALLOC_MEM = true>
void push(void* buff, size_t len) {
if (_max_size == 0) return;
assert(buff && len);
buffer_t mybuff;
if (size() == _max_size) {
mybuff = front();
pop();
}
if (ALLOC_MEM == true) {
if (mybuff.capacity < len) {
if (mybuff.data) free(mybuff.data);
mybuff.data = l_alloc(len);
mybuff.capacity = len;
}
memcpy(mybuff.data, buff, len);
mybuff.len = len;
} else {
if (mybuff.data) free(mybuff.data);
mybuff.data = buff;
mybuff.capacity = mybuff.len = len;
}
emplace(mybuff);
}
};
#endif /* __CLASS_PTR_ARRAY_H */
#ifndef __GEN_FUNCTIONS
#define __GEN_FUNCTIONS
#ifdef __APPLE__
#include <sys/types.h>
#include <sys/_types/_timespec.h>
#include <mach/mach.h>
#include <mach/clock.h>
#include <mach/mach_time.h>
#ifndef mach_time_h
#define mach_time_h
#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC SYSTEM_CLOCK
#endif // CLOCK_MONOTONIC
static void clock_gettime(int clk_id, struct timespec *tp) {
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
//retval = clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
tp->tv_sec = mts.tv_sec;
tp->tv_nsec = mts.tv_nsec;
}
#endif /* mach_time_t */
#endif /* __APPLE__ */
inline unsigned long long monotonic_time() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000);
}
inline unsigned long long realtime_time() {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000);
}
template<int FACTOR, typename T>
inline T overflow_safe_multiply(T val) {
static_assert(std::is_integral<T>::value, "T must be an integer type.");
static_assert(std::is_unsigned_v<T>, "T must be an unsigned integer type.");
static_assert(FACTOR > 0, "Negative factors are not supported.");
if constexpr (FACTOR == 0) return 0;
if (val == 0) return 0;
if (val > std::numeric_limits<T>::max() / FACTOR) return std::numeric_limits<T>::max();
return (val * FACTOR);
}
#endif /* __GEN_FUNCTIONS */
bool Proxy_file_exists(const char *);
bool Proxy_file_regular(const char *);
char *escape_string_single_quotes(char *input, bool free_it);
int remove_spaces(const char *);
char *trim_spaces_in_place(char *str);
char *trim_spaces_and_quotes_in_place(char *str);
bool mywildcmp(const char *p, const char *str);
std::string trim(const std::string& s);
/**
* @brief Helper function that converts a MYSQL_RES into a 'SQLite3_result'.
* @param resultset The resultset to be converted into a 'SQLite3_result'.
* @return An 'unique_ptr' holding the resulting 'SQLite3_result'.
*/
std::unique_ptr<SQLite3_result> get_SQLite3_resulset(MYSQL_RES* resultset);
std::vector<std::string> split_string(const std::string& str, char delimiter);