-
Notifications
You must be signed in to change notification settings - Fork 128
/
garbage_collect.c
158 lines (132 loc) · 3.64 KB
/
garbage_collect.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* garbage_collect.c - an sample memroy garbage collection.
*
* (c) wzt 2014 http://www.cloud-sec.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wvm.h"
#include "type.h"
#include "list.h"
#include "log.h"
#include "vm_error.h"
#include "garbage_collect.h"
JVM_OBJECT *create_new_obj(void *addr, int size)
{
JVM_OBJECT *new_obj = NULL;
new_obj = (JVM_OBJECT *)calloc(1, sizeof(JVM_OBJECT));
if (!new_obj) {
error("calloc failed.\n");
return NULL;
}
new_obj->addr = (void *)addr;
new_obj->size = size;
new_obj->ref_count = 0;
list_add_tail(&(new_obj->list), &jvm_obj_list_head);
return new_obj;
}
void free_jvm_obj(JVM_OBJECT *obj)
{
if (obj)
free((void *)obj);
}
void show_jvm_obj(struct list_head *list_head)
{
JVM_OBJECT *s;
struct list_head *p;
list_for_each(p, list_head) {
s = list_entry(p, JVM_OBJECT, list);
if (s) {
printf("addr: 0x%x\tsize: %d\tref_count: %d\n",
s->addr, s->size, s->ref_count);
}
}
}
void del_jvm_obj(struct list_head *list_head, void *addr)
{
JVM_OBJECT *s;
struct list_head *p, *q;
list_for_each_safe(p, q, list_head) {
s = list_entry(p, JVM_OBJECT, list);
if (s && s->addr == addr) {
printf("found addr: 0x%x\tsize: %d\tref_count: %d\n",
s->addr, s->size, s->ref_count);
list_del(p);
free_jvm_obj(s);
}
}
}
void free_all_jvm_obj(struct list_head *list_head, void *addr)
{
JVM_OBJECT *s;
struct list_head *p, *q;
list_for_each_safe(p, q, list_head) {
s = list_entry(p, JVM_OBJECT, list);
if (s) {
list_del(p);
free_jvm_obj(s);
}
}
}
JVM_OBJECT *search_obj_by_addr(void *addr, struct list_head *list_head)
{
JVM_OBJECT *s;
struct list_head *p;
list_for_each(p, list_head) {
s = list_entry(p, JVM_OBJECT, list);
if (s && s->addr == addr) {
printf("found addr: 0x%x\tsize: %d\tref_count: %d\n",
s->addr, s->size, s->ref_count);
return s;
}
}
printf("not found addr: 0x%x\n", addr);
return NULL;
}
int inc_obj_ref(void *addr, struct list_head *list_head)
{
JVM_OBJECT *obj;
obj = search_obj_by_addr(addr, list_head);
if (!obj)
return -1;
obj->ref_count++;
return 0;
}
int dec_obj_ref(void *addr, struct list_head *list_head)
{
JVM_OBJECT *obj;
obj = search_obj_by_addr(addr, list_head);
if (!obj)
return -1;
obj->ref_count--;
return 0;
}
void start_gc(struct list_head *list_head)
{
JVM_OBJECT *s;
struct list_head *p, *q;
list_for_each_safe(p, q, list_head) {
s = list_entry(p, JVM_OBJECT, list);
if (s && s->ref_count == 0) {
printf("free addr: 0x%x\tsize: %d\tref_count: %d\n",
s->addr, s->size, s->ref_count);
list_del(p);
free_jvm_obj(s);
}
}
}