fixed small memory leak in timeq.c
[NeonServV5.git] / src / timeq.c
1 /* timeq.c - NeonServ v5.4
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "timeq.h"
19
20 static struct timeq_entry *timeq_events;
21 #ifdef HAVE_THREADS
22 static pthread_mutex_t synchronized;
23 #endif
24
25 void init_timeq() {
26     THREAD_MUTEX_INIT(synchronized);
27 }
28
29 void timeq_tick() {
30     SYNCHRONIZE(synchronized);
31     struct timeq_entry *entry, *next;
32     struct timeval now;
33     gettimeofday(&now, NULL);
34     for(entry = timeq_events; entry; entry = next) {
35         if(!timeval_is_bigger(entry->execute, now)) {
36             next = entry->next;
37             if(timeq_events == entry)
38                 timeq_events = next;
39             entry->callback(entry->data);
40             if(entry->name)
41                 free(entry->name);
42             free(entry);
43         } else
44             break;
45     }
46     DESYNCHRONIZE(synchronized);
47 }
48
49 struct timeq_entry* timeq_add(int seconds, int module_id, timeq_callback_t *callback, void *data) {
50     return timeq_uadd(seconds * 1000, module_id, callback, data);
51 }
52
53 struct timeq_entry* timeq_uadd(int useconds, int module_id, timeq_callback_t *callback, void *data) {
54     struct timeval now;
55     gettimeofday(&now, NULL);
56     struct timeq_entry *entry = malloc(sizeof(*entry));
57     if (!entry)
58     {
59         perror("malloc() failed");
60         return NULL;
61     }
62     SYNCHRONIZE(synchronized);
63     now.tv_usec += (useconds % 1000);
64     now.tv_sec += (useconds / 1000);
65     entry->execute = now;
66     entry->module_id = module_id;
67     entry->callback = callback;
68     entry->data = data;
69     entry->name = NULL;
70     struct timeq_entry *next, *prev = NULL;
71     for(next = timeq_events; next; next = next->next) {
72         if(timeval_is_bigger(next->execute, now))
73             break;
74         else
75             prev = next;
76     }
77     if(prev == NULL) {
78         entry->next = timeq_events;
79         timeq_events = entry;
80     } else {
81         entry->next = next;
82         prev->next = entry;
83     }
84     DESYNCHRONIZE(synchronized);
85     return entry;
86 }
87
88 struct timeq_entry* timeq_add_name(char *name, int seconds, int module_id, timeq_callback_t *callback, void *data) {
89     return timeq_uadd_name(name, seconds * 1000, module_id, callback, data);
90 }
91
92 struct timeq_entry* timeq_uadd_name(char *name, int useconds, int module_id, timeq_callback_t *callback, void *data) {
93     SYNCHRONIZE(synchronized);
94     struct timeq_entry *entry = timeq_uadd(useconds, module_id, callback, data);
95     entry->name = strdup(name);
96     DESYNCHRONIZE(synchronized);
97     return entry;
98 }
99
100 int timeq_del(struct timeq_entry* entry) {
101     SYNCHRONIZE(synchronized);
102     struct timeq_entry *centry, *last = NULL;
103     for(centry = timeq_events; centry; centry = centry->next) {
104         if(centry == entry) {
105             if(last)
106                 last->next = centry->next;
107             else
108                 timeq_events = centry->next;
109             if(centry->name)
110                 free(centry->name);
111             free(centry);
112             DESYNCHRONIZE(synchronized);
113             return 1;
114         } else {
115             last = centry;
116         }
117     }
118     DESYNCHRONIZE(synchronized);
119     return 0;
120 }
121
122 int timeq_del_name(char *name) {
123     SYNCHRONIZE(synchronized);
124     struct timeq_entry *centry, *last = NULL;
125     for(centry = timeq_events; centry; centry = centry->next) {
126         if(centry->name && !stricmp(centry->name, name)) {
127             if(last)
128                 last->next = centry->next;
129             else
130                 timeq_events = centry->next;
131             free(centry->name);
132             free(centry);
133             DESYNCHRONIZE(synchronized);
134             return 1;
135         } else {
136             last = centry;
137         }
138     }
139     DESYNCHRONIZE(synchronized);
140     return 0;
141 }
142
143 int timeq_name_exists(char *name) {
144     SYNCHRONIZE(synchronized);
145     struct timeq_entry *centry;
146     for(centry = timeq_events; centry; centry = centry->next) {
147         if(centry->name && !stricmp(centry->name, name)) {
148             DESYNCHRONIZE(synchronized);
149             return 1;
150         }
151     }
152     DESYNCHRONIZE(synchronized);
153     return 0;
154 }
155
156 void unregister_module_timers(int module_id) {
157     SYNCHRONIZE(synchronized);
158     struct timeq_entry *centry, *next, *last = NULL;
159     for(centry = timeq_events; centry; centry = next) {
160         next = centry->next;
161         if(centry->module_id == module_id) {
162             if(last)
163                 last->next = centry->next;
164             else
165                 timeq_events = centry->next;
166             free(centry->name);
167             free(centry);
168         } else
169             last = centry;
170     }
171     DESYNCHRONIZE(synchronized);
172 }