remove macros use in objects mofule
This commit is contained in:
parent
0fb9326c08
commit
7927226484
31 changed files with 362 additions and 386 deletions
|
|
@ -14,9 +14,7 @@ void obj::save_string(ArchiverOut& file, const std::string& string) {
|
|||
file.writeBytes(string.c_str(), string.size());
|
||||
}
|
||||
|
||||
ualni obj::save_string_size(const std::string& string) {
|
||||
return string.size() + sizeof(string.size());
|
||||
}
|
||||
ualni obj::save_string_size(const std::string& string) { return string.size() + sizeof(string.size()); }
|
||||
|
||||
void obj::load_string(ArchiverIn& file, std::string& out) {
|
||||
typeof(out.size()) size;
|
||||
|
|
@ -29,7 +27,7 @@ void obj::load_string(ArchiverIn& file, std::string& out) {
|
|||
}
|
||||
|
||||
Object* ObjectMemAllocate(const ObjectType* type);
|
||||
void ObjectMemDeallocate(Object* in);
|
||||
void ObjectMemDeallocate(Object* object);
|
||||
|
||||
objects_api* obj::NDO = nullptr;
|
||||
|
||||
|
|
@ -68,14 +66,14 @@ Object* objects_api::create(const std::string& name) {
|
|||
|
||||
setReferenceCount(obj_instance, 0);
|
||||
|
||||
NDO_CASTV(ClassObject, obj_instance, classobj);
|
||||
auto classobj = objects_api::cast<ClassObject>(obj_instance);
|
||||
|
||||
if (classobj) {
|
||||
auto idx = classobj->members->presents("__init__");
|
||||
DEBUG_ASSERT(idx)
|
||||
if (idx) {
|
||||
auto constructor_obj = classobj->members->getSlotVal(idx);
|
||||
NDO_CASTV(MethodObject, constructor_obj, constructor_method);
|
||||
auto constructor_method = objects_api::cast<MethodObject>(constructor_obj);
|
||||
if (constructor_method) {
|
||||
interp->execAll(constructor_method, classobj);
|
||||
}
|
||||
|
|
@ -102,7 +100,8 @@ bool objects_api::compare(Object* first, Object* second) {
|
|||
}
|
||||
|
||||
// raw data comparison
|
||||
return memCompare(first, second, first->type->size) == 0;
|
||||
// TODO : reconsider
|
||||
return memCompare(first->type, second->type, first->type->size - sizeof(Object) + sizeof(void*)) == 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
@ -110,7 +109,7 @@ bool objects_api::compare(Object* first, Object* second) {
|
|||
|
||||
Object* objects_api::instantiate(Object* in) {
|
||||
obj::Object* obj = NDO->create(in->type->name);
|
||||
NDO->copy(obj, in);
|
||||
tp::obj::objects_api::copy(obj, in);
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
|
@ -163,19 +162,18 @@ void objects_api::destroy(Object* in) const {
|
|||
return;
|
||||
}
|
||||
|
||||
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
|
||||
if (mh->refc > 1) {
|
||||
mh->refc--;
|
||||
if (in->refc > 1) {
|
||||
in->refc--;
|
||||
return;
|
||||
}
|
||||
|
||||
NDO_CASTV(ClassObject, in, classobj);
|
||||
auto classobj = objects_api::cast<ClassObject>(in);
|
||||
if (classobj) {
|
||||
auto idx = classobj->members->presents("__del__");
|
||||
DEBUG_ASSERT(idx)
|
||||
if (idx) {
|
||||
auto constructor_obj = classobj->members->getSlotVal(idx);
|
||||
NDO_CASTV(MethodObject, constructor_obj, constructor_method);
|
||||
auto constructor_method = objects_api::cast<MethodObject>(constructor_obj);
|
||||
if (constructor_method) {
|
||||
interp->execAll(constructor_method, classobj);
|
||||
}
|
||||
|
|
@ -191,22 +189,6 @@ void objects_api::destroy(Object* in) const {
|
|||
ObjectMemDeallocate(in);
|
||||
}
|
||||
|
||||
halni objects_api::getReferenceCount(Object* in) {
|
||||
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
|
||||
return (halni) mh->refc;
|
||||
}
|
||||
|
||||
void objects_api::increaseReferenceCount(Object* in) {
|
||||
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
|
||||
mh->refc++;
|
||||
}
|
||||
|
||||
void objects_api::setReferenceCount(Object* in, halni count) {
|
||||
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
|
||||
mh->refc = count;
|
||||
}
|
||||
|
||||
|
||||
void hierarchy_copy(Object* self, const Object* in, const ObjectType* type) {
|
||||
if (type->base) {
|
||||
hierarchy_copy(self, in, type->base);
|
||||
|
|
@ -226,14 +208,3 @@ void hierarchy_construct(Object* self, const ObjectType* type) {
|
|||
type->constructor(self);
|
||||
}
|
||||
}
|
||||
|
||||
Object* obj::ndo_cast(const Object* in, const ObjectType* to_type) {
|
||||
const ObjectType* typeIter = in->type;
|
||||
while (typeIter) {
|
||||
if (typeIter == to_type) {
|
||||
return (Object*) in;
|
||||
}
|
||||
typeIter = typeIter->base;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,60 +9,48 @@
|
|||
using namespace tp;
|
||||
using namespace obj;
|
||||
|
||||
ObjectMemHead* bottom = nullptr;
|
||||
Object* bottom = nullptr;
|
||||
ualni count = 0;
|
||||
|
||||
struct ObjectsFileHeader {
|
||||
char name[10] = { 0 };
|
||||
char version[10] = { 0 };
|
||||
|
||||
ObjectsFileHeader(bool default_val = true) {
|
||||
if (default_val) {
|
||||
memCopy(&name, "objects", 8);
|
||||
memCopy(&version, "0", 2);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Object* ObjectMemAllocate(const ObjectType* type) {
|
||||
ObjectMemHead* memh = (ObjectMemHead*) malloc(type->size + sizeof(ObjectMemHead));
|
||||
if (!memh) {
|
||||
return nullptr;
|
||||
auto object = (Object*) malloc(type->size);
|
||||
|
||||
if (!object) {
|
||||
printf("Cant allocate memory for an object");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
memh->down = nullptr;
|
||||
memh->flags = 0;
|
||||
object->down = nullptr;
|
||||
object->flags = 0;
|
||||
|
||||
memh->refc = (alni) 1;
|
||||
object->refc = (alni) 1;
|
||||
|
||||
if (bottom) {
|
||||
bottom->down = memh;
|
||||
bottom->down = object;
|
||||
}
|
||||
|
||||
memh->up = bottom;
|
||||
bottom = memh;
|
||||
object->up = bottom;
|
||||
bottom = object;
|
||||
|
||||
count++;
|
||||
|
||||
NDO_FROM_MEMH(memh)->type = type;
|
||||
return NDO_FROM_MEMH(memh);
|
||||
object->type = type;
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
void ObjectMemDeallocate(Object* in) {
|
||||
ObjectMemHead* memh = ((ObjectMemHead*) in) - 1;
|
||||
|
||||
if (memh->up) {
|
||||
memh->up->down = memh->down;
|
||||
void ObjectMemDeallocate(Object* object) {
|
||||
if (object->up) {
|
||||
object->up->down = object->down;
|
||||
}
|
||||
|
||||
if (memh->down) {
|
||||
memh->down->up = memh->up;
|
||||
if (object->down) {
|
||||
object->down->up = object->up;
|
||||
} else {
|
||||
bottom = memh->up;
|
||||
bottom = object->up;
|
||||
}
|
||||
|
||||
free(memh);
|
||||
|
||||
free(object);
|
||||
count--;
|
||||
}
|
||||
|
||||
|
|
@ -74,22 +62,34 @@ void obj::logTypeData(const ObjectType* type) {
|
|||
}
|
||||
}
|
||||
|
||||
ualni obj::getObjCount() { return count; }
|
||||
ualni obj::getObjCount() { return count; }
|
||||
|
||||
void obj::assertNoLeaks() {
|
||||
if (bottom) {
|
||||
printf("ERROR : not all objects are destroyed\n");
|
||||
ualni idx = 0;
|
||||
for (ObjectMemHead* memh = bottom; memh; memh = memh->up) {
|
||||
printf(" ===== Object - %i. Ref count - %i ===== \n", idx, memh->refc);
|
||||
logTypeData(NDO_FROM_MEMH(memh)->type);
|
||||
for (Object* object = bottom; object; object = object->up) {
|
||||
printf(" ===== Object - %llu. Ref count - %lli ===== \n", idx, object->refc);
|
||||
logTypeData(object->type);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ObjectsFileHeader {
|
||||
char name[10] = { 0 };
|
||||
char version[10] = { 0 };
|
||||
|
||||
explicit ObjectsFileHeader(bool default_val = true) {
|
||||
if (default_val) {
|
||||
memCopy(&name, "objects", 8);
|
||||
memCopy(&version, "0", 2);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct ObjectFileHead {
|
||||
Object* load_head_adress = 0;
|
||||
Object* load_head_adress = nullptr;
|
||||
halni refc = 0;
|
||||
};
|
||||
|
||||
|
|
@ -97,13 +97,11 @@ int1* loaded_file = nullptr;
|
|||
|
||||
void objects_api::clear_object_flags() {
|
||||
// clear all object flags
|
||||
for (ObjectMemHead* iter = bottom; iter; iter = iter->up) {
|
||||
for (Object* iter = bottom; iter; iter = iter->up) {
|
||||
iter->flags = -1;
|
||||
}
|
||||
}
|
||||
|
||||
alni& getObjectFlags(Object* in) { return NDO_MEMH_FROM_NDO(in)->flags; }
|
||||
|
||||
alni objsize_ram_util(Object* self, const ObjectType* type) {
|
||||
alni out = 0;
|
||||
|
||||
|
|
@ -128,11 +126,11 @@ alni objects_api::objsize_ram_recursive_util(Object* self, const ObjectType* typ
|
|||
alni out = 0;
|
||||
|
||||
if (different_object) {
|
||||
if (getObjectFlags(self) == 1) {
|
||||
if (self->flags == 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
getObjectFlags(self) = 1;
|
||||
self->flags = 1;
|
||||
}
|
||||
|
||||
if (type->allocated_size_recursive) {
|
||||
|
|
@ -177,7 +175,7 @@ alni objects_api::objsize_file(Object* self) { return objsize_file_util(self, se
|
|||
alni objsize_file_recursive_util(Object* self, const ObjectType* type) {
|
||||
alni out = 0;
|
||||
|
||||
getObjectFlags(self) = 1;
|
||||
self->flags = 1;
|
||||
|
||||
if (type->save_size) {
|
||||
out += type->save_size(self);
|
||||
|
|
@ -186,7 +184,7 @@ alni objsize_file_recursive_util(Object* self, const ObjectType* type) {
|
|||
if (type->childs_retrival) {
|
||||
Buffer<Object*> childs = type->childs_retrival(self);
|
||||
for (auto child : childs) {
|
||||
if (getObjectFlags(child.data()) != 1) {
|
||||
if (child.data()->flags != 1) {
|
||||
out += objsize_file_recursive_util(child.data(), child.data()->type);
|
||||
}
|
||||
}
|
||||
|
|
@ -217,8 +215,8 @@ void object_recursive_save(ArchiverOut& ndf, Object* self, const ObjectType* typ
|
|||
|
||||
alni objects_api::save(ArchiverOut& ndf, Object* in) {
|
||||
// if already saved return file_adress
|
||||
if (NDO_MEMH_FROM_NDO(in)->flags != -1) {
|
||||
return NDO_MEMH_FROM_NDO(in)->flags;
|
||||
if (in->flags != -1) {
|
||||
return in->flags;
|
||||
}
|
||||
|
||||
// save write adress for parent save function call
|
||||
|
|
@ -228,13 +226,13 @@ alni objects_api::save(ArchiverOut& ndf, Object* in) {
|
|||
alni save_adress = ndf.getFreeAddress();
|
||||
|
||||
// save file_adress in memhead
|
||||
NDO_MEMH_FROM_NDO(in)->flags = save_adress;
|
||||
in->flags = save_adress;
|
||||
|
||||
// update write adress
|
||||
ndf.setAddress(save_adress);
|
||||
|
||||
// save file object header
|
||||
ObjectFileHead ofh = { 0, getReferenceCount(in) };
|
||||
ObjectFileHead ofh = { 0, (halni) getReferenceCount(in) };
|
||||
ndf << ofh;
|
||||
|
||||
save_string(ndf, in->type->name);
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ alni ScriptSection::get_script_file_adress(Script* in) { return get_script_head_
|
|||
|
||||
alni ScriptSection::save_script_table_to_file_size(ScriptSection* self, ArchiverOut& file) {
|
||||
alni size = 0;
|
||||
size += 5; // header
|
||||
size += 5; // header
|
||||
size += sizeof(alni); // scripts length
|
||||
|
||||
for (auto iter : self->mScripts) {
|
||||
|
|
@ -84,7 +84,7 @@ alni ScriptSection::save_script_table_to_file_size(ScriptSection* self, Archiver
|
|||
}
|
||||
|
||||
size += sizeof(alni); // objects mem offset
|
||||
size += 5; // header
|
||||
size += 5; // header
|
||||
return size;
|
||||
}
|
||||
|
||||
|
|
@ -141,7 +141,7 @@ void load_constants(ScriptSection* self, ArchiverIn& file, alni start_addr) {
|
|||
file >> str_addr;
|
||||
|
||||
NDO->destroy(script->mReadable); // we already have string object in the script when creating script
|
||||
script->mReadable = NDO_CAST(obj::StringObject, obj::NDO->load(file, str_addr));
|
||||
script->mReadable = objects_api::cast<StringObject>(obj::NDO->load(file, str_addr));
|
||||
|
||||
file.setAddress(file.getAddress() + sizeof(alni)); // constants length
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue