-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathputil.c
197 lines (168 loc) · 3.75 KB
/
putil.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
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
/* util.c -- (C) Geoffrey Reynolds, March 2009.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
*/
//#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "stdint.h"
#include <string.h>
#ifndef __GNUC__
#include <intrin.h>
#endif
#include "putil.h"
#ifdef USE_BOINC
#ifdef __APPLE__
#include "boinc_api.h"
#else
#include "BOINC/boinc_api.h"
#endif
#endif
void *xmalloc(size_t size)
{
void *ret;
if ((ret = malloc(size)) == NULL)
{
perror("malloc");
#ifdef USE_BOINC
boinc_finish(EXIT_FAILURE);
#else
exit(EXIT_FAILURE);
#endif
}
return ret;
}
void *xrealloc(void *mem, size_t size)
{
void *ret;
if ((ret = realloc(mem,size)) == NULL)
{
perror("realloc");
#ifdef USE_BOINC
boinc_finish(EXIT_FAILURE);
#else
exit(EXIT_FAILURE);
#endif
}
return ret;
}
char *xstrdup(const char *str)
{
if (str == NULL)
return NULL;
else
return strcpy(xmalloc(strlen(str)+1),str);
}
/* Returns 0 if successful, -1 if cannot parse, -2 if out of range.
*/
int parse_uint(unsigned int *result, const char *str,
unsigned int lo, unsigned int hi)
{
uint64_t result64;
int status;
status = parse_uint64(&result64,str,lo,hi);
if (status == 0)
*result = result64;
return status;
}
/* Returns 0 if successful, -1 if cannot parse, -2 if out of range.
*/
int parse_uint64(uint64_t *result, const char *str,
uint64_t lo, uint64_t hi)
{
uint64_t num;
unsigned int expt;
char *tail;
expt = 0;
errno = 0;
num = strtoull(str,&tail,0);
if (errno != 0 || num > hi)
return -2;
switch (*tail)
{
case 'P': expt += 3;
case 'T': expt += 3;
case 'G': expt += 3;
case 'M': expt += 3;
case 'K': expt += 3;
if (tail[1] != '\0')
return -1;
for ( ; expt > 0; expt -= 3)
if (num > hi/1000)
return -2;
else
num *= 1000;
break;
case 'e':
case 'E':
expt = strtoul(tail+1,&tail,0);
if (errno != 0)
return -2;
if (*tail != '\0')
return -1;
while (expt-- > 0)
if (num > hi/10)
return -2;
else
num *= 10;
break;
case 'p': expt += 10;
case 't': expt += 10;
case 'g': expt += 10;
case 'm': expt += 10;
case 'k': expt += 10;
if (tail[1] != '\0')
return -1;
if (num > (hi>>expt))
return -2;
num <<= expt;
break;
case 'b':
case 'B':
expt = strtoul(tail+1,&tail,0);
if (errno != 0)
return -2;
if (*tail != '\0')
return -1;
while (expt-- > 0)
if (num > (hi>>1))
return -2;
else
num <<= 1;
break;
case '\0':
break;
default:
return -1;
}
if (num < lo)
return -2;
*result = num;
return 0;
}
#ifndef _GNU_SOURCE
int asprintf(char **out, const char *fmt, const char *str) {
*out = (char *)xmalloc(strlen(fmt)+strlen(str)-1);
return sprintf(*out, fmt, str);
}
#endif
// Allocate and copy string.
char* astrcpy(char **dest, const char *src) {
*dest = (char *)xmalloc(strlen(src)+1);
return strcpy(*dest, src);
}
#ifndef __GNUC__
unsigned long __builtin_ctzll(unsigned __int64 i) {
unsigned long res;
if((unsigned int)i) {
_BitScanForward(&res, (unsigned int)i);
return res;
} else {
_BitScanForward(&res,(unsigned int)(i>>32));
return res+32;
}
}
#endif