-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.c
47 lines (38 loc) · 1.11 KB
/
platform.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
#include <stdlib.h>
#include "fixes.h"
#include <string.h>
#include "util.h"
/* Assert calloc */
void* acalloc(size_t nmemb, size_t size, char* name) {
void* res = calloc(nmemb, size);
if (nmemb * size == 0) {
/* wa_warn("acalloc: %s requests allocating 0 bytes.\n", name);*/
} else if (res == NULL) {
FATAL("Could not allocate %lu bytes for %s", nmemb * size, name);
}
return res;
}
/* Assert realloc/calloc */
void* arecalloc(void* ptr,
size_t old_nmemb,
size_t nmemb,
size_t size,
char* name) {
void* res = realloc(ptr, nmemb * size);
if (res == NULL) {
FATAL("Could not allocate %lu bytes for %s", nmemb * size, name);
}
/* Initialize new memory */
memset((void*)((size_t)res + old_nmemb * size), 0,
(nmemb - old_nmemb) * size);
return res;
}
double fmax(double left, double right) {
return (left > right) ? left : right;
}
double fmin(double left, double right) {
return (left < right) ? left : right;
}
double trunc(double d) {
return (d > 0) ? floor(d) : ceil(d);
}