7 Garbage *Garbage::garbage = 0;
10 GarbageObject::GarbageObject(char *title)
12 Garbage::garbage->add_object(this);
15 this->title = new char[strlen(title) + 1];
16 strcpy(this->title, title);
19 GarbageObject::~GarbageObject()
22 printf("GarbageObject::~GarbageObject: title=%s users=%d was not deleted by Garbage::delete_object\n", title, users);
27 void GarbageObject::add_user()
29 Garbage::garbage->lock->lock("GarbageObject::add_user");
31 Garbage::garbage->lock->unlock();
34 void GarbageObject::remove_user()
36 Garbage::garbage->lock->lock("GarbageObject::add_user");
38 if(users < 0) printf("GarbageObject::remove_user: users=%d Should be >= 0.\n", users);
39 Garbage::garbage->remove_expired();
40 // *this is now invalid
41 Garbage::garbage->lock->unlock();
50 lock = new Mutex("Garbage::lock", 1);
59 void Garbage::add_object(GarbageObject *ptr)
61 lock->lock("Garbage::add_object");
66 void Garbage::delete_object(GarbageObject *ptr)
68 Garbage *garbage = Garbage::garbage;
69 garbage->lock->lock("Garbage::delete_object");
72 // Remove expired objects here
74 garbage->lock->unlock();
77 void Garbage::remove_expired()
79 Garbage *garbage = Garbage::garbage;
80 for(int i = 0; i < garbage->objects.total; i++)
82 GarbageObject *ptr = garbage->objects.values[i];
83 if(ptr->users <= 0 && ptr->deleted)
85 // Must remove pointer to prevent recursive deletion of the same object.
86 // But i is still invalid.
87 garbage->objects.remove_number(i);