forked from cloudwu/bpa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestbpa.c
More file actions
71 lines (60 loc) · 1.32 KB
/
Copy pathtestbpa.c
File metadata and controls
71 lines (60 loc) · 1.32 KB
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
#include "bpa.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
static void
set_id(struct bpa_heap *H, id uid, int v, size_t s) {
char * buf = bpa_get_ptr(H, uid);
memset(buf, v, s);
}
static void
dump_id(struct bpa_heap *H, id uid, size_t s) {
printf("id = %x\n", uid);
char * buf = bpa_get_ptr(H, uid);
if (buf == NULL) {
printf("not exist\n");
return;
}
size_t i;
for (i=0;i<s;i++) {
printf("%x",buf[i]);
}
printf("\n");
}
int
main() {
void * buffer = malloc(1024*1024);
struct bpa_heap *H = bpa_init(buffer, 1024*1024);
assert(H == buffer);
bpa_dump(H);
id id1 = bpa_create(H, 10000);
id id2 = bpa_create(H, 20000);
id id3 = bpa_create(H, 30000);
bpa_create(H, 4000);
assert(bpa_get_id(H, bpa_get_ptr(H,id1)) == id1);
assert(bpa_get_id(H, bpa_get_ptr(H,id2)) == id2);
assert(bpa_get_id(H, bpa_get_ptr(H,id3)) == id3);
set_id(H, id1, 1, 10000);
set_id(H, id2, 2, 20000);
set_id(H, id3, 3, 30000);
dump_id(H, id1, 100);
dump_id(H, id2, 100);
dump_id(H, id3, 100);
dump_id(H, id1 + 0x10000, 100);
bpa_dump(H);
bpa_grab(H, id1);
bpa_grab(H, id3);
bpa_collect(H,1);
bpa_dump(H);
dump_id(H, id1, 100);
dump_id(H, id2, 100);
dump_id(H, id3, 100);
bpa_release(H,id1);
bpa_collect(H,0);
bpa_dump(H);
bpa_release(H,id3);
bpa_collect(H,0);
bpa_dump(H);
return 0;
}