Skip to content

Commit 9592867

Browse files
committed
GC实现 1.堆上分配空间
1 parent bbad000 commit 9592867

File tree

14 files changed

+292
-21
lines changed

14 files changed

+292
-21
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ADD_EXECUTABLE(pythonvm main.cpp
2525
#runtime/traceback.cpp
2626
#runtime/generator.cpp
2727
runtime/cellObject.cpp
28-
#memory/heap.cpp
28+
memory/heap.cpp
2929
#memory/oopClosure.cpp
3030
code/binaryFileParser.cpp
3131
code/codeObject.cpp)

memory/heap.cpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//
2+
// Created by Kosho on 2020/9/13.
3+
//
4+
#include <stdlib.h>
5+
6+
#include "runtime/universe.hpp"
7+
#include "memory/heap.hpp"
8+
9+
Space::Space(size_t size) {
10+
_size = size;
11+
_base = (char *) malloc(size);
12+
_end = _base + size;
13+
_top = (char *) (((long long) (_base + 15)) & -16); // 16字节对齐
14+
_capacity = _end - _top;
15+
}
16+
17+
Space::~Space() {
18+
if (_base) {
19+
free(_base);
20+
_base = 0;
21+
}
22+
23+
_top = 0;
24+
_end = 0;
25+
_capacity = 0;
26+
_size = 0;
27+
}
28+
29+
bool Space::can_alloc(size_t size) {
30+
return _capacity > size;
31+
}
32+
33+
bool Space::has_obj(char *obj) {
34+
return obj >= _base && _end > obj;
35+
}
36+
37+
void *Space::allocate(size_t size) {
38+
// 8字节对齐
39+
size = (size + 7) & -8;
40+
char *start = _top;
41+
_top += size;
42+
_capacity -= size;
43+
return start;
44+
}
45+
46+
void Space::clear() {
47+
memset(_base, 0, _size);
48+
_top = (char *) (((long long) (_base + 15)) & -16);
49+
_capacity = _end - _top;
50+
}
51+
52+
Heap *Heap::instance = NULL;
53+
size_t Heap::MAX_CAP = 2 * 1024 * 1024;
54+
55+
Heap *Heap::get_instance() {
56+
if (instance == NULL)
57+
instance = new Heap(MAX_CAP);
58+
59+
return instance;
60+
}
61+
62+
Heap::Heap(size_t size) {
63+
mem_1 = new Space(size);
64+
mem_2 = new Space(size);
65+
metaspace = new Space(size / 16);
66+
67+
mem_1->clear();
68+
mem_2->clear();
69+
metaspace->clear();
70+
71+
eden = mem_1;
72+
survivor = mem_2;
73+
}
74+
75+
Heap::~Heap() {
76+
if (mem_1) {
77+
delete mem_1;
78+
mem_1 = NULL;
79+
}
80+
81+
if (mem_2) {
82+
delete mem_2;
83+
mem_2 = NULL;
84+
}
85+
86+
if (metaspace) {
87+
delete metaspace;
88+
metaspace = NULL;
89+
}
90+
91+
eden = NULL;
92+
survivor = NULL;
93+
}
94+
95+
void *Heap::allocate(size_t size) {
96+
if (!eden->can_alloc(size)) {
97+
gc();
98+
}
99+
100+
return eden->allocate(size);
101+
}
102+
103+
void *Heap::allocate_meta(size_t size) {
104+
if (!metaspace->can_alloc(size)) {
105+
return NULL;
106+
}
107+
108+
return metaspace->allocate(size);
109+
}
110+
111+
void Heap::copy_live_objects() {
112+
// TODO
113+
}
114+
115+
void Heap::gc() {
116+
printf("gc starting...\n");
117+
printf(" befroe gc : \n");
118+
printf(" eden's capacity is %lu\n", eden->_capacity);
119+
copy_live_objects();
120+
121+
Space *temp = eden;
122+
eden = survivor;
123+
survivor = temp;
124+
125+
printf(" after gc : \n");
126+
printf(" eden's capacity is %lu\n", eden->_capacity);
127+
printf("gc end\n");
128+
129+
survivor->clear();
130+
}

memory/heap.hpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// Created by Kosho on 2020/9/13.
3+
//
4+
5+
#ifndef PYTHONVM_HEAP_HPP
6+
#define PYTHONVM_HEAP_HPP
7+
8+
#include <memory.h>
9+
10+
class HiObject;
11+
12+
/* base top end
13+
* ↓ ↓ ↓
14+
* |------------|--capacity--|
15+
* |-----------size----------|
16+
*/
17+
class Space {
18+
friend class Heap;
19+
20+
private:
21+
char *_base;
22+
char *_top;
23+
char *_end;
24+
size_t _size;
25+
size_t _capacity;
26+
27+
Space(size_t size);
28+
29+
~Space();
30+
31+
public:
32+
bool can_alloc(size_t size);
33+
34+
bool has_obj(char *obj);
35+
36+
void *allocate(size_t size);
37+
38+
void clear();
39+
};
40+
41+
class Heap {
42+
private:
43+
Space *mem_1;
44+
Space *mem_2;
45+
46+
Space *eden;
47+
Space *survivor;
48+
49+
Space *metaspace;
50+
51+
Heap(size_t size);
52+
53+
public:
54+
static size_t MAX_CAP;
55+
static Heap *instance;
56+
static Heap *get_instance();
57+
58+
~Heap();
59+
60+
void *allocate(size_t size);
61+
62+
void *allocate_meta(size_t size);
63+
64+
void copy_live_objects();
65+
66+
void gc();
67+
};
68+
69+
#endif //PYTHONVM_HEAP_HPP

object/hiObject.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "object/hiString.hpp"
1010
#include "runtime/universe.hpp"
1111
#include "runtime/functionObject.hpp"
12+
#include "memory/heap.hpp"
1213

1314
ObjectKlass *ObjectKlass::instance = NULL;
1415

@@ -110,6 +111,10 @@ HiObject *HiObject::len() {
110111
return klass()->len(this);
111112
}
112113

114+
void* HiObject::operator new(size_t size) {
115+
return Universe::heap->allocate(size);
116+
}
117+
113118
TypeKlass *TypeKlass::instance = NULL;
114119

115120
TypeKlass *TypeKlass::get_instance() {

object/hiObject.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class HiObject {
6161
HiDict *obj_dict() { return _obj_dict; }
6262
void set_obj_dict(HiDict *x) { _obj_dict = x; }
6363
void init_dict();
64+
65+
void *operator new(size_t size);
6466
};
6567

6668
/**

object/hiString.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
#include <string.h>
66
#include <stdio.h>
77

8-
#include "hiString.hpp"
9-
#include "hiInteger.hpp"
8+
#include "object/hiString.hpp"
9+
#include "object/hiInteger.hpp"
1010
#include "object/hiDict.hpp"
1111
#include "runtime/universe.hpp"
1212
#include "runtime/functionObject.hpp"
13+
#include "memory/heap.hpp"
1314

1415
StringKlass *StringKlass::instance = NULL;
1516

@@ -58,15 +59,17 @@ HiObject *StringKlass::equal(HiObject *x, HiObject *y) {
5859

5960
HiString::HiString(const char *x) {
6061
_length = strlen(x);
61-
_value = new char[_length];
62+
// _value = new char[_length];
63+
_value = (char*)Universe::heap->allocate(_length);
6264
strcpy(_value, x);
6365

6466
set_klass(StringKlass::get_instance());
6567
}
6668

6769
HiString::HiString(const char *x, const int length) {
6870
_length = length;
69-
_value = new char[length];
71+
// _value = new char[length];
72+
_value = (char*)Universe::heap->allocate(_length);
7073

7174
// do not use strcpy here, since '\0' is allowed.
7275
for (int i = 0; i < length; ++i) {
@@ -78,7 +81,8 @@ HiString::HiString(const char *x, const int length) {
7881

7982
HiString::HiString(const int length) {
8083
_length = length;
81-
_value = new char[length];
84+
// _value = new char[length];
85+
_value = (char*)Universe::heap->allocate(_length + 1);
8286
set_klass(StringKlass::get_instance());
8387
}
8488

object/klass.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
// Created by Kosho on 2020/8/16.
33
//
44

5-
#include <runtime/interpreter.hpp>
5+
#include "klass.hpp"
6+
#include "runtime/interpreter.hpp"
67
#include "runtime/stringTable.hpp"
78
#include "runtime/functionObject.hpp"
8-
#include "klass.hpp"
9+
#include "runtime/universe.hpp"
910
#include "object/hiObject.hpp"
1011
#include "object/hiInteger.hpp"
1112
#include "object/hiString.hpp"
1213
#include "object/hiDict.hpp"
1314
#include "object/hiList.hpp"
14-
#include "runtime/universe.hpp"
15+
#include "memory/heap.hpp"
1516

1617
#define ST(x) StringTable::get_instance()->STR(x)
1718
#define STR(x) x##_str
1819

1920
Klass::Klass() {
21+
Universe::klasses->add(this);
2022
_klass_dict = NULL;
2123
_name = NULL;
2224
_super = NULL;
@@ -237,3 +239,7 @@ void Klass::order_supers() {
237239
}
238240
printf("\n");
239241
}
242+
243+
void *Klass::operator new(size_t size) {
244+
return Universe::heap->allocate_meta(size);
245+
}

object/klass.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class Klass {
8080
virtual HiObject *iter (HiObject* x) { return 0; }
8181

8282
virtual HiObject *allocate_instance(HiObject* callable, ArrayList<HiObject *> *args);
83+
84+
void* operator new(size_t size);
8385
};
8486

8587
#endif //PYTHONVM_KLASS_HPP

runtime/universe.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@
33
//
44

55
#include "runtime/universe.hpp"
6+
#include "runtime/interpreter.hpp"
7+
#include "runtime/functionObject.hpp"
8+
#include "object/hiInteger.hpp"
9+
#include "object/hiObject.hpp"
610
#include "object/hiString.hpp"
7-
#include "object/hiDict.hpp"
811
#include "object/hiList.hpp"
9-
#include "functionObject.hpp"
12+
#include "object/hiDict.hpp"
13+
#include "memory/heap.hpp"
1014

1115
HiObject *Universe::HiTrue = NULL;
1216
HiObject *Universe::HiFalse = NULL;
1317

1418
HiObject *Universe::HiNone = NULL;
1519

20+
Heap *Universe::heap = NULL;
21+
22+
ArrayList<Klass *> *Universe::klasses = NULL;
23+
1624
void Universe::genesis() {
25+
heap = Heap::get_instance();
26+
klasses = new ArrayList<Klass *>();
27+
1728
HiTrue = new HiString("True");
1829
HiFalse = new HiString("False");
1930
HiNone = new HiString("None");

runtime/universe.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,27 @@
66
#define PYTHONVM_UNIVERSE_HPP
77

88
#include <stdio.h>
9-
#include <object/hiInteger.hpp>
109

1110
class Klass;
11+
class Heap;
1212
class HiInteger;
1313
class HiObject;
1414
class CodeObject;
1515

16+
template <typename T>
17+
class ArrayList;
18+
1619
class Universe {
1720
public:
1821
static HiObject *HiTrue;
1922
static HiObject *HiFalse;
2023

2124
static HiObject *HiNone;
2225

26+
static ArrayList<Klass*>* klasses;
27+
28+
static Heap *heap;
29+
2330
public:
2431
static void genesis();
2532

0 commit comments

Comments
 (0)