1 /* IOGarbageCollector.c - IOMultiplexer v2
2 * Copyright (C) 2014 Philipp Kreil (pk910)
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.
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.
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/>.
17 #define _IOHandler_internals
18 #include "IOInternal.h"
19 #include "IOHandler.h"
20 #include "IOGarbageCollector.h"
28 iogc_free *free_callback;
29 struct timeval timeout;
31 struct IOGCObject *next;
34 static int iogc_enabled = 1;
35 static struct timeval iogc_timeout;
36 static struct IOGCObject *first_object = NULL, *last_object = NULL;
39 iogc_timeout.tv_usec = 0;
40 iogc_timeout.tv_sec = 10;
44 void iohandler_set_gc(int enabled) {
51 void iogc_add(void *object) {
52 iogc_add_callback(object, NULL);
55 void iogc_add_callback(void *object, iogc_free *free_callback) {
58 free_callback(object);
63 struct IOGCObject *obj = malloc(sizeof(*obj));
65 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IOGCObject in %s:%d", __FILE__, __LINE__);
67 free_callback(object);
73 obj->free_callback = free_callback;
74 gettimeofday(&obj->timeout, NULL);
75 obj->timeout.tv_sec += IOGC_TIMEOUT;
79 last_object->next = obj;
87 gettimeofday(&now, NULL);
89 struct IOGCObject *obj, *next_obj;
90 for(obj = first_object; obj; obj = next_obj) {
91 if(timeval_is_smaler(obj->timeout, now)) {
93 if(obj->free_callback)
94 obj->free_callback(obj->object);