ffmpeg versioning mods from Andrew
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / garbage.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcsignals.h"
23 #include "garbage.h"
24 #include "mutex.h"
25
26 #include <string.h>
27
28 //Garbage *Garbage::garbage = 0;
29
30
31 Garbage::Garbage(const char *title)
32 {
33 //      Garbage::garbage->add_object(this);
34         users = 1;
35         deleted = 0;
36         this->title = new char[strlen(title) + 1];
37         strcpy(this->title, title);
38         lock = new Mutex("Garbage::lock", 1);
39 }
40
41 Garbage::~Garbage()
42 {
43         if(!deleted)
44                 printf("Garbage::~Garbage: title=%s users=%d was not deleted by Garbage::remove_user\n",
45                         title,
46                         users);
47         delete [] title;
48         delete lock;
49 }
50
51
52 void Garbage::add_user()
53 {
54         lock->lock("Garbage::add_user");
55         users++;
56         lock->unlock();
57 }
58
59 int Garbage::remove_user()
60 {
61         void *This = this;
62         if(!This) return 0;
63
64 //printf("Garbage::remove_user %d lock=%p users=%d\n", __LINE__, lock, users);
65         if(users <= 0) {
66                 printf("Garbage::remove_user %d users=%d\n", __LINE__, users);
67                 return 0;
68         }
69         lock->lock("Garbage::delete_object");
70         if(--users > 0) {
71                 lock->unlock();
72                 return 1; // still has users
73         }
74         deleted = 1;
75         lock->unlock();
76         delete this; // this is now invalid.
77         return 0;
78 }
79
80
81 // void GarbageObject::remove_user()
82 // {
83 //      Garbage::garbage->lock->lock("GarbageObject::add_user");
84 //      users--;
85 //      if(users < 0) printf("GarbageObject::remove_user: users=%d Should be >= 0.\n", users);
86 //      Garbage::garbage->remove_expired();
87 // // *this is now invalid
88 //      Garbage::garbage->lock->unlock();
89 // }
90
91
92
93
94
95 // Garbage::Garbage()
96 // {
97 //      lock = new Mutex("Garbage::lock", 1);
98 // }
99 //
100 //
101 // Garbage::~Garbage()
102 // {
103 //      delete lock;
104 // }
105 //
106 // // void Garbage::add_object(GarbageObject *ptr)
107 // // {
108 // //   lock->lock("Garbage::add_object");
109 // //   objects.append(ptr);
110 // //   lock->unlock();
111 // // }
112 //
113 // void Garbage::delete_object(GarbageObject *ptr)
114 // {
115 //      if(!ptr) return;
116 //
117 //      Garbage *garbage = Garbage::garbage;
118 //      garbage->lock->lock("Garbage::delete_object");
119 //      ptr->users--;
120 //
121 //      if(ptr->users <= 0)
122 //      {
123 //              ptr->deleted = 1;
124 //              delete ptr;
125 //
126 // // Remove expired objects here
127 // //           remove_expired();
128 //      }
129 //      garbage->lock->unlock();
130 // }
131 //
132 // void Garbage::remove_expired()
133 // {
134 //      Garbage *garbage = Garbage::garbage;
135 //      for(int i = 0; i < garbage->objects.total; i++)
136 //      {
137 //              GarbageObject *ptr = garbage->objects.values[i];
138 //              if(ptr->users <= 0 && ptr->deleted)
139 //              {
140 // // Must remove pointer to prevent recursive deletion of the same object.
141 // // But i is still invalid.
142 //                      garbage->objects.remove_number(i);
143 //
144 //                      delete ptr;
145 //                      i--;
146 //              }
147 //      }
148 // }
149
150
151