-
Notifications
You must be signed in to change notification settings - Fork 101
/
test.c
110 lines (95 loc) · 2.04 KB
/
test.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "aoi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct alloc_cookie {
int count;
int max;
int current;
};
static void *
my_alloc(void * ud, void *ptr, size_t sz) {
struct alloc_cookie * cookie = ud;
if (ptr == NULL) {
void *p = malloc(sz);
++ cookie->count;
cookie->current += sz;
if (cookie->max < cookie->current) {
cookie->max = cookie->current;
}
// printf("%p + %u\n",p, sz);
return p;
}
-- cookie->count;
cookie->current -= sz;
// printf("%p - %u \n",ptr, sz);
free(ptr);
return NULL;
}
struct OBJECT {
float pos[3];
float v[3];
char mode[4];
};
static struct OBJECT OBJ[4];
static void
init_obj(uint32_t id, float x, float y, float vx, float vy, const char *mode) {
OBJ[id].pos[0] =x;
OBJ[id].pos[1] =y;
OBJ[id].pos[2] =0;
OBJ[id].v[0] =vx;
OBJ[id].v[1] =vy;
OBJ[id].v[2] =0;
strcpy(OBJ[id].mode, mode);
}
static void
update_obj(struct aoi_space *space, uint32_t id) {
int i;
for (i=0;i<3;i++) {
OBJ[id].pos[i] += OBJ[id].v[i];
if (OBJ[id].pos[i] < 0) {
OBJ[id].pos[i]+=100.0f;
} else if (OBJ[id].pos[i] > 100.0f) {
OBJ[id].pos[i]-=100.0f;
}
}
aoi_update(space, id, OBJ[id].mode, OBJ[id].pos);
}
static void
message(void *ud, uint32_t watcher, uint32_t marker) {
printf("%u (%f,%f) => %u (%f,%f)\n",
watcher, OBJ[watcher].pos[0], OBJ[watcher].pos[1],
marker, OBJ[marker].pos[0], OBJ[marker].pos[1]
);
}
static void
test(struct aoi_space * space) {
init_obj(0,40,0,0,1,"w");
init_obj(1,42,100,0,-1,"wm");
init_obj(2,0,40,1,0,"w");
init_obj(3,100,45,-1,0,"wm");
int i,j;
for (i=0;i<100;i++) {
if (i<50) {
for (j=0;j<4;j++) {
update_obj(space,j);
}
} else if (i==50) {
aoi_update(space, 3, "d", OBJ[3].pos);
} else {
for (j=0;j<3;j++) {
update_obj(space,j);
}
}
aoi_message(space, message, NULL);
}
}
int
main() {
struct alloc_cookie cookie = { 0,0,0 };
struct aoi_space * space = aoi_create(my_alloc , &cookie);
test(space);
aoi_release(space);
printf("max memory = %d, current memory = %d\n", cookie.max , cookie.current);
return 0;
}