-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
74 lines (62 loc) · 1.3 KB
/
util.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
/* See LICENSE file for copyright and license details. */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#include "util.h"
void *
ecalloc(size_t nmemb, size_t size)
{
void *p;
p = calloc(nmemb, size);
return p;
if(p) return p;
/* exit if calloc failed Likely OS is out of memory */
DEBUG("%s","FATAL: FAILED TO CALLOC MEMORY");
exit(1);
}
char *
smprintf(char *fmt, ...)
{
va_list fmtargs;
char *ret;
int len;
va_start(fmtargs, fmt);
len = vsnprintf(NULL, 0, fmt, fmtargs);
va_end(fmtargs);
ret = malloc(++len);
if(!ret)
{
perror("smprintf Failed: ");
return "NULL";
}
va_start(fmtargs, fmt);
vsnprintf(ret, len, fmt, fmtargs);
va_end(fmtargs);
return ret;
}
void
debug(char *fmt, ...)
{
#ifdef ENABLE_DEBUG
static char str[1024];
memset(str, 0, sizeof(char) * 1024);
va_list args;
va_start(args, fmt);
vsnprintf(str, vsnprintf(NULL, 0, fmt, args), fmt, args);
va_end(args);
perror(str);
fprintf(stdout, "%s\n", str);
#endif
}
double
functime(void (*_timefunction)(void))
{
clock_t start, end;
start = clock();
_timefunction();
end = clock();
return ((double)(end - start)) / CLOCKS_PER_SEC;
}