-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.c
83 lines (69 loc) · 1.68 KB
/
utils.c
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
#include "mig_mon.h"
/* Return 0 when succeed, 1 for retry, assert on error */
void fd_write(int fd, void *buffer, size_t size)
{
int ret;
retry:
ret = write(fd, buffer, size);
if (ret < 0)
ret = -errno;
if (ret == -EAGAIN || ret == -EINTR)
goto retry;
assert((size_t)ret == size);
}
/* Return 0 when succeed, 1 for retry, assert on error */
void fd_read(int fd, void *buffer, size_t size)
{
int ret;
retry:
ret = read(fd, buffer, size);
if (ret < 0)
ret = -errno;
if (ret == -EAGAIN || ret == -EINTR)
goto retry;
assert((size_t)ret == size);
}
void socket_set_fast_reuse(int fd)
{
int val = 1, ret;
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
(const char *)&val, sizeof(val));
assert(ret == 0);
}
void pthread_set_name(pthread_t thread, const char *name)
{
#ifdef __linux__
int ret = pthread_setname_np(thread, name);
assert(ret == 0);
#endif
}
/* Parse some number like "2G", if no unit, using "M" by default. */
uint64_t parse_size_to_mega(const char *str)
{
uint64_t value, n = 1;
char *endptr;
value = strtoul(str, &endptr, 10);
if (value == 0 || endptr == NULL) {
printf("Unknown size string: '%s'\n", str);
exit(-1);
}
switch (*endptr) {
case 't':
case 'T':
n *= 1024;
/* fall through */
case 'g':
case 'G':
n *= 1024;
/* fall through */
case 'm':
case 'M':
case '\0': /* This means, no unit, so MB by default */
break;
default:
printf("Unknown unit '%c', try something else (MB/GB/...)\n", *endptr);
exit(-1);
break;
}
return value * n;
}