Initial import of some kind of API.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * JITCraft. If not, see http://www.gnu.org/licenses/.
19 #ifndef _include_jc_object_visitor_h_
20 #define _include_jc_object_visitor_h_
24 #include "FixedArray.h"
26 #include "Reference.h"
28 #include "Functions.h"
34 // An ObjectVisitor is used for traversing object references in the heap. Since
35 // this pattern must be lightning fast, we avoid virtual dispatch by forcing
36 // visitors to compose themselves with the visiting logic.
38 class ObjectVisitor : public T
41 void visitField(Object *obj, size_t offset) {
42 this->visit(reinterpret_cast<Object **>(Address(obj) + offset));
45 size_t iterateType(Object *obj) {
46 visitField(obj, Type::offsetOfContained());
47 visitField(obj, Type::offsetOfReturnType());
48 visitField(obj, Type::offsetOfParameters());
49 visitField(obj, Type::offsetOfTag());
53 size_t iterateFixedArray(FixedArray *array) {
54 unsigned length = array->length();
55 Object **ptr = reinterpret_cast<Object **>(array + 1);
56 for (unsigned i = 0; i < length; i++)
58 return FixedArray::SizeFor(length);
61 size_t iterateByteArray(ByteArray *array) {
62 return ByteArray::SizeFor(array->length());
65 size_t iterateCode(Object *obj) {
66 visitField(obj, Code::offsetOfModule());
67 visitField(obj, Code::offsetOfBytecode());
68 visitField(obj, Code::offsetOfLocals());
69 visitField(obj, Code::offsetOfObjects());
70 visitField(obj, Code::offsetOfSafepoints());
74 size_t iterateReference(Object *obj) {
75 visitField(obj, Reference::offsetOfBase());
76 return sizeof(Reference);
79 size_t iterateModule(Object *obj) {
80 visitField(obj, Module::offsetOfFunctions());
81 return sizeof(Module);
84 size_t iterateFunction(Object *obj) {
85 visitField(obj, Function::offsetOfModule());
86 visitField(obj, Function::offsetOfCode());
87 visitField(obj, Function::offsetOfType());
88 return sizeof(Function);
91 size_t iterateString(Object *obj) {
92 return String::SizeFor(String::cast(obj)->length());
95 size_t iterateArray(Array *array, ArrayMap *map) {
96 if (map->type()->isArray()) {
97 unsigned length = array->nelements();
98 Object **vec = array->objVec();
99 for (unsigned i = 0; i < length; i++)
100 this->visit(vec + i);
102 return Array::SizeOf(array);
105 size_t iterateArrayMap(ArrayMap *map) {
106 visitField(map, ArrayMap::offsetOfType());
107 visitField(map, ArrayMap::offsetOfContained());
108 return sizeof(ArrayMap);
112 size_t iterate(Object *obj) {
113 visitField(obj, Object::offsetOfMap());
114 return iterate(obj, obj->map());
117 size_t iterate(Object *obj, Map *map) {
118 switch (map->kind()) {
120 return iterateType(obj);
122 case MapKind_FixedArray:
123 return iterateFixedArray(FixedArray::cast(obj));
125 case MapKind_ByteArray:
126 return iterateByteArray(ByteArray::cast(obj));
129 return iterateCode(obj);
131 case MapKind_Reference:
132 return iterateReference(obj);
135 return iterateModule(obj);
137 case MapKind_Function:
138 return iterateFunction(obj);
141 return iterateString(obj);
144 return Align(iterateArray(Array::cast(obj), ArrayMap::cast(map)), kObjectAlignment);
146 case MapKind_MetaArray:
147 return iterateArrayMap(ArrayMap::cast(obj));
150 assert(map->kind() == MapKind_Meta);
157 SizeOfObject(Object *obj, Map *map)
159 switch (map->kind()) {
163 case MapKind_FixedArray:
164 return FixedArray::SizeFor(FixedArray::cast(obj)->length());
166 case MapKind_ByteArray:
167 return ByteArray::SizeFor(ByteArray::cast(obj)->length());
172 case MapKind_Reference:
173 return sizeof(Reference);
176 return sizeof(Module);
178 case MapKind_Function:
179 return sizeof(Function);
182 return String::SizeFor(String::cast(obj)->length());
185 return Align(Array::SizeOf(Array::cast(obj)), kObjectAlignment);
187 case MapKind_MetaArray:
188 return sizeof(ArrayMap);
190 case MapKind_FreeRegion:
191 return *reinterpret_cast<size_t *>(reinterpret_cast<Address>(obj + 1));
194 assert(map->kind() == MapKind_Meta);
199 class ObjectSizeEvaluator
202 size_t operator()(Object *obj, Map *map) {
203 return SizeOfObject(obj, map);
209 #endif // _include_jc_object_visitor_h_