-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_nolibc.c
84 lines (62 loc) · 1.36 KB
/
main_nolibc.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
/* Try sbrk and mmap */
#include <unistd.h>
#include <sys/mman.h>
void* mymalloc(unsigned long sz)
{
return sbrk(sz);
/*
long pagesize = sysconf(_SC_PAGESIZE);
return mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_ANONYMOUS, -1, 0);
*/
}
void myfree(void* addr)
{
/* no-op for sbrk based system */
/*
Too lazy to bother for a compilation test
return munmap(NULL, sz);
*/
}
void* myrealloc(void* addr, unsigned long sz)
{
unsigned long i;
unsigned char* to, *from;
void* newaddr = mymalloc(sz);
if (!newaddr) {
return NULL;
}
to = newaddr;
from = addr;
for (i=0; i<sz; i++) {
to[i] = from[i];
}
myfree(addr);
return newaddr;
}
#define CVEC_MALLOC(sz) mymalloc(sz)
#define CVEC_REALLOC(p, sz) myrealloc(p, sz)
#define CVEC_FREE(p) myfree(p)
#define CVEC_SIZE_T long
#define CVECTOR_IMPLEMENTATION
#include "cvector.h"
int main(int argc, char** argv)
{
cvector_i veci;
cvector_d vecd;
cvector_str vecstr;
cvector_void vecvoid;
cvec_i(&veci, 0, 10);
cvec_d(&vecd, 0, 10);
cvec_str(&vecstr, 0, 10);
cvec_void(&vecvoid, 0, 10, sizeof(long double), NULL, NULL);
/*
THIS IS REALLY JUST A compilation test to see/prove it really doesn't
depend on the C stdlib
functionality testing is done in main.c and cvector_tests.c
*/
cvec_free_i(&veci);
cvec_free_d(&vecd);
cvec_free_str(&vecstr);
cvec_free_void(&vecvoid);
return 0;
}