X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/fwd/blobdiff_plain/47a7631fc173b345182df04f67523d3b2fe4ba83..57ceb980e3c55306f3f70f92604703abf132cf27:/source.c diff --git a/source.c b/source.c index db18c56..f515ae2 100644 --- a/source.c +++ b/source.c @@ -7,24 +7,24 @@ /*----- Licensing notice --------------------------------------------------* * - * This file is part of the `fw' port forwarder. + * This file is part of the `fwd' port forwarder. * - * `fw' is free software; you can redistribute it and/or modify + * `fwd' is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * `fw' is distributed in the hope that it will be useful, + * `fwd' is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with `fw'; if not, write to the Free Software Foundation, + * along with `fwd'; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "fw.h" +#include "fwd.h" /*----- Static variables --------------------------------------------------*/ @@ -45,11 +45,15 @@ static source *sources = 0; void source_add(source *s) { + assert(s->ops->shutdown); + assert(!(s->f&SF_ACTIVE)); s->next = sources; s->prev = 0; if (sources) sources->prev = s; sources = s; + s->f |= SF_ACTIVE; + source_inc(s); } /* --- @source_remove@ --- * @@ -63,12 +67,45 @@ void source_add(source *s) void source_remove(source *s) { + assert(s->f&SF_ACTIVE); if (s->next) s->next->prev = s->prev; if (s->prev) s->prev->next = s->next; else sources = s->next; + s->f &= ~SF_ACTIVE; + source_dec(s); +} + +/* --- @source_inc@ --- * + * + * Arguments: @source *s@ = pointer to a source + * + * Returns: --- + * + * Use: Increments a source's refcount. + */ + +void source_inc(source *s) { s->ref++; } + +/* --- @source_dec@ --- * + * + * Arguments: @source *s@ = pointer to a source + * + * Returns: --- + * + * Use: Decrements a source's refcount, destroying it if necessary. + */ + +void source_dec(source *s) +{ + assert(s->ref > 0); + s->ref--; + if (!s->ref) { + if (s->f&SF_ACTIVE) s->ops->shutdown(s); + s->ops->destroy(s); + } } /* --- @source_killall@ --- * @@ -86,7 +123,7 @@ void source_killall(void) while (s) { source *ss = s; s = s->next; - ss->ops->destroy(ss); + ss->ops->shutdown(ss); } sources = 0; }