1 | #include "globals.h"
|
---|
2 | #include "oscam-garbage.h"
|
---|
3 | #include "oscam-lock.h"
|
---|
4 | #include "oscam-string.h"
|
---|
5 | #include "oscam-time.h"
|
---|
6 |
|
---|
7 | #define HASH_BUCKETS 16
|
---|
8 |
|
---|
9 | struct cs_garbage
|
---|
10 | {
|
---|
11 | time_t time;
|
---|
12 | void *data;
|
---|
13 | #ifdef WITH_DEBUG
|
---|
14 | char *file;
|
---|
15 | uint16_t line;
|
---|
16 | #endif
|
---|
17 | struct cs_garbage *next;
|
---|
18 | };
|
---|
19 |
|
---|
20 | static struct cs_garbage *garbage_first[HASH_BUCKETS];
|
---|
21 | static struct cs_garbage *garbage_last[HASH_BUCKETS];
|
---|
22 | static CS_MUTEX_LOCK garbage_lock[HASH_BUCKETS];
|
---|
23 | static pthread_t garbage_thread;
|
---|
24 | static int32_t garbage_collector_active;
|
---|
25 | static int32_t garbage_debug;
|
---|
26 |
|
---|
27 | #ifdef WITH_DEBUG
|
---|
28 | void add_garbage_debug(void *data, char *file, uint16_t line)
|
---|
29 | {
|
---|
30 | #else
|
---|
31 | void add_garbage(void *data)
|
---|
32 | {
|
---|
33 | #endif
|
---|
34 | if(!data)
|
---|
35 | { return; }
|
---|
36 |
|
---|
37 | if(!garbage_collector_active || garbage_debug == 1)
|
---|
38 | {
|
---|
39 | NULLFREE(data);
|
---|
40 | return;
|
---|
41 | }
|
---|
42 |
|
---|
43 | int32_t bucket = (uintptr_t)data / 16 % HASH_BUCKETS;
|
---|
44 | struct cs_garbage *garbage;
|
---|
45 | if(!cs_malloc(&garbage, sizeof(struct cs_garbage)))
|
---|
46 | {
|
---|
47 | cs_log("*** MEMORY FULL -> FREEING DIRECT MAY LEAD TO INSTABILITY!!!! ***");
|
---|
48 | NULLFREE(data);
|
---|
49 | return;
|
---|
50 | }
|
---|
51 | garbage->time = time(NULL);
|
---|
52 | garbage->data = data;
|
---|
53 | #ifdef WITH_DEBUG
|
---|
54 | garbage->file = file;
|
---|
55 | garbage->line = line;
|
---|
56 | #endif
|
---|
57 | cs_writelock(&garbage_lock[bucket]);
|
---|
58 |
|
---|
59 | #ifdef WITH_DEBUG
|
---|
60 | if(garbage_debug == 2)
|
---|
61 | {
|
---|
62 | struct cs_garbage *garbagecheck = garbage_first[bucket];
|
---|
63 | while(garbagecheck)
|
---|
64 | {
|
---|
65 | if(garbagecheck->data == data)
|
---|
66 | {
|
---|
67 | cs_log("Found a try to add garbage twice. Not adding the element to garbage list...");
|
---|
68 | cs_log("Current garbage addition: %s, line %d.", file, line);
|
---|
69 | cs_log("Original garbage addition: %s, line %d.", garbagecheck->file, garbagecheck->line);
|
---|
70 | cs_writeunlock(&garbage_lock[bucket]);
|
---|
71 | NULLFREE(garbage);
|
---|
72 | return;
|
---|
73 | }
|
---|
74 | garbagecheck = garbagecheck->next;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | #endif
|
---|
78 |
|
---|
79 | if(garbage_last[bucket]) { garbage_last[bucket]->next = garbage; }
|
---|
80 | else { garbage_first[bucket] = garbage; }
|
---|
81 | garbage_last[bucket] = garbage;
|
---|
82 | cs_writeunlock(&garbage_lock[bucket]);
|
---|
83 | }
|
---|
84 |
|
---|
85 | static pthread_cond_t sleep_cond;
|
---|
86 | static pthread_mutex_t sleep_cond_mutex;
|
---|
87 |
|
---|
88 | static void garbage_collector(void)
|
---|
89 | {
|
---|
90 | int8_t i;
|
---|
91 | struct cs_garbage *garbage, *next, *prev, *first;
|
---|
92 | set_thread_name(__func__);
|
---|
93 | while(garbage_collector_active)
|
---|
94 | {
|
---|
95 |
|
---|
96 | for(i = 0; i < HASH_BUCKETS; ++i)
|
---|
97 | {
|
---|
98 | cs_writelock(&garbage_lock[i]);
|
---|
99 | first = garbage_first[i];
|
---|
100 | time_t deltime = time((time_t)0) - (2*cfg.ctimeout/1000 + 1);
|
---|
101 | for(garbage = first, prev = NULL; garbage; prev = garbage, garbage = garbage->next)
|
---|
102 | {
|
---|
103 | if(deltime < garbage->time) // all following elements are too new
|
---|
104 | {
|
---|
105 | if(prev)
|
---|
106 | {
|
---|
107 | garbage_first[i] = garbage;
|
---|
108 | prev->next = NULL;
|
---|
109 | }
|
---|
110 | break;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | if(!garbage && garbage_first[i]) // end of list reached and everything is to be cleaned
|
---|
114 | {
|
---|
115 | garbage = first;
|
---|
116 | garbage_first[i] = NULL;
|
---|
117 | garbage_last[i] = NULL;
|
---|
118 | }
|
---|
119 | else if(prev) { garbage = first; } // set back to beginning to cleanup all
|
---|
120 | else { garbage = NULL; } // garbage not old enough yet => nothing to clean
|
---|
121 | cs_writeunlock(&garbage_lock[i]);
|
---|
122 |
|
---|
123 | // list has been taken out before so we don't need a lock here anymore!
|
---|
124 | while(garbage)
|
---|
125 | {
|
---|
126 | next = garbage->next;
|
---|
127 | NULLFREE(garbage->data);
|
---|
128 | NULLFREE(garbage);
|
---|
129 | garbage = next;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | sleepms_on_cond(&sleep_cond_mutex, &sleep_cond, 1000);
|
---|
134 | }
|
---|
135 | pthread_exit(NULL);
|
---|
136 | }
|
---|
137 |
|
---|
138 | void start_garbage_collector(int32_t debug)
|
---|
139 | {
|
---|
140 |
|
---|
141 | garbage_debug = debug;
|
---|
142 | int8_t i;
|
---|
143 | for(i = 0; i < HASH_BUCKETS; ++i)
|
---|
144 | {
|
---|
145 | cs_lock_create(&garbage_lock[i], 5, "garbage_lock");
|
---|
146 |
|
---|
147 | garbage_first[i] = NULL;
|
---|
148 | }
|
---|
149 | cs_pthread_cond_init(&sleep_cond_mutex, &sleep_cond);
|
---|
150 |
|
---|
151 | pthread_attr_t attr;
|
---|
152 | pthread_attr_init(&attr);
|
---|
153 |
|
---|
154 | garbage_collector_active = 1;
|
---|
155 |
|
---|
156 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_SIZE);
|
---|
157 | int32_t ret = pthread_create(&garbage_thread, &attr, (void *)&garbage_collector, NULL);
|
---|
158 | if(ret)
|
---|
159 | {
|
---|
160 | cs_log("ERROR: can't create garbagecollector thread (errno=%d %s)", ret, strerror(ret));
|
---|
161 | pthread_attr_destroy(&attr);
|
---|
162 | cs_exit(1);
|
---|
163 | }
|
---|
164 | pthread_attr_destroy(&attr);
|
---|
165 | }
|
---|
166 |
|
---|
167 | void stop_garbage_collector(void)
|
---|
168 | {
|
---|
169 | if(garbage_collector_active)
|
---|
170 | {
|
---|
171 | int8_t i;
|
---|
172 |
|
---|
173 | garbage_collector_active = 0;
|
---|
174 | pthread_cond_signal(&sleep_cond);
|
---|
175 | pthread_join(garbage_thread, NULL);
|
---|
176 | for(i = 0; i < HASH_BUCKETS; ++i)
|
---|
177 | { cs_writelock(&garbage_lock[i]); }
|
---|
178 |
|
---|
179 | for(i = 0; i < HASH_BUCKETS; ++i)
|
---|
180 | {
|
---|
181 | while(garbage_first[i])
|
---|
182 | {
|
---|
183 | struct cs_garbage *next = garbage_first[i]->next;
|
---|
184 | NULLFREE(garbage_first[i]->data);
|
---|
185 | NULLFREE(garbage_first[i]);
|
---|
186 | garbage_first[i] = next;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|