blob: 7a8a5b6a4cf69203ced648dcf693e60f4aa50424 (
plain) (
blame)
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
|
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "xwrap.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define RETRY_SEC 0
#define RETRY_NSEC 250000000
static const char fail_msg[] = "Fail to allocate memory. Retry ...\n";
static const size_t fail_len = STATIC_STRLEN (fail_msg);
int xatol (const char* str, long* result) {
int errno_save, rval;
long lres;
char* endptr;
errno_save = errno;
errno = 0, rval = 0;
lres = strtol (str, &endptr, 10);
if (str == endptr || errno != 0) {
rval = -1;
} else {
*result = lres;
}
errno = errno_save;
return rval;
}
void* xmalloc (size_t size) {
void* memptr;
while ((memptr = malloc (size)) == NULL) {
nanosleep (&(struct timespec) { RETRY_SEC, RETRY_NSEC }, NULL);
write (STDERR_FILENO, fail_msg, fail_len);
}
return memptr;
}
|