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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "util.h"
char* copystring(const char *s){
size_t len=strlen(s);
char *s2=malloc(len+1,char);
memcpy(s2,s,len+1);
return s2;
}
char* copybufasstring(const char *b,int length){
char *s=malloc(length+1,char);
memcpy(s,b,length);
s[length]='\0';
return s;
}
__attribute__((noreturn)) void outofmem(void){
fprintf(stderr,"OUT OF MEMORY!\n");
exit(2);
}
double floatmod(double a,double b){
if(b==0)return nan("");
int sa=a<0?-1:1;
a=fabs(a); b=fabs(b);
return sa*(a-b*floor(a/b));
}
void* malloccheck(size_t n){
void *p=mallocreal(n);
if(!p)outofmem();
return p;
}
void* calloccheck(size_t n,size_t s){
void *p=callocreal(n,s);
if(!p)outofmem();
return p;
}
void* realloccheck(void *p,size_t n){
p=reallocreal(p,n);
if(!p)outofmem();
return p;
}
#undef malloc
#undef calloc
#undef realloc
void* mallocreal(size_t n){
return malloc(n);
}
void* callocreal(size_t n,size_t s){
return calloc(n,s);
}
void* reallocreal(void *p,size_t n){
return realloc(p,n);
}
|