00001 /* garbage.c 00002 * 00003 * Copyright (C) 2003, 2004 Eggheads Development Team 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #ifndef lint 00021 static const char rcsid[] = "$Id: garbage.c,v 1.5 2004-10-17 05:14:06 stdarg Exp $"; 00022 #endif 00023 00024 #include <eggdrop/eggdrop.h> 00025 00026 typedef struct garbage_list { 00027 struct garbage_list *next; 00028 garbage_proc_t garbage_proc; 00029 void *client_data; 00030 } garbage_list_t; 00031 00032 static garbage_list_t *garbage_list_head = NULL; 00033 00034 int garbage_add(garbage_proc_t garbage_proc, void *client_data, int flags) 00035 { 00036 garbage_list_t *g; 00037 00038 /* Make sure it's not a duplicate (helpful for cleanup functions). */ 00039 if (flags & GARBAGE_ONCE) { 00040 for (g = garbage_list_head; g; g = g->next) { 00041 if (g->garbage_proc == garbage_proc && g->client_data == client_data) return(0); 00042 } 00043 } 00044 00045 /* Create the entry. */ 00046 g = malloc(sizeof(*g)); 00047 g->garbage_proc = garbage_proc; 00048 g->client_data = client_data; 00049 00050 /* Add to list. */ 00051 g->next = garbage_list_head; 00052 garbage_list_head = g; 00053 00054 return(0); 00055 } 00056 00057 /* Should be called only from the main loop. */ 00058 int garbage_run() 00059 { 00060 garbage_list_t *g; 00061 00062 for (g = garbage_list_head; g; g = garbage_list_head) { 00063 garbage_list_head = g->next; 00064 (g->garbage_proc)(g->client_data); 00065 free(g); 00066 } 00067 return(0); 00068 }