Commit bbac08b9 authored by Niels Möller's avatar Niels Möller
Browse files

*** empty log message ***

Rev: src/resource.c:1.1
Rev: src/resource.h:1.1
parent 7ddca8c6
/* resource.c
*
* External resources associated with a connection, for instance
* processes and ports. Used to kill or release the resource in
* question when the connection dies.
*
* $Id$ */
/* lsh, an implementation of the ssh protocol
*
* Copyright (C) 1998 Niels Mller
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "resource.h"
#include "werror.h"
#include "xalloc.h"
/* Prototypes */
void do_mark_resources(struct resource_node *n,
void (*mark)(struct lsh_object *o));
void do_free_resources(struct resource_node *n);
void dont_free_live_resource(int alive);
#define CLASS_DEFINE
#include "resource.h.x"
#undef CLASS_DEFINE
#include "resource.c.x"
void do_mark_resources(struct resource_node *n,
void (*mark)(struct lsh_object *o))
{
for(; n; n = n->next)
mark(&n->resource->super);
}
void do_free_resources(struct resource_node *n)
{
while(n)
{
struct resource_node *old = n;
lsh_object_free(n->resource);
n = n->next;
lsh_space_free(old);
}
}
void dont_free_live_resource(int alive)
{
if (alive)
fatal("dont_free_live_resource: About to garbage collect a live resource!\n");
}
static struct resource_node *do_remember_resource(struct resource_list *self,
struct resource *r)
{
struct resource_node *n;
NEW_SPACE(n);
n->resource = r;
/* Add at head of list */
n->next = self->head;
n->prev = NULL;
self->head = n;
if (n->next)
n->next->prev = n;
else
self->tail = n;
return n;
}
static void do_kill_all(struct resource_list *self)
{
/* FIXME: Doesn't deallocate any nodes */
struct resource_node *n;
for (n = self->head; n; n = n->next)
KILL_RESOURCE(n->resource);
}
struct resource_list *empty_resource_list(void)
{
NEW(resource_list, self);
self->head = self->tail = NULL;
self->remember = do_remember_resource;
self->kill_all = do_kill_all;
/* self->kill = do_kill_resource; */
return self;
}
/* resource.h
*
* External resources associated with a connection, for instance
* processes and ports. Used to kill or release the resource in
* question when the connection dies.
*
* $Id$ */
/* lsh, an implementation of the ssh protocol
*
* Copyright (C) 1998 Niels Möller
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef LSH_RESOURCE_H_INCLUDED
#define LSH_RESOURCE_H_INCLUDED
#include "lsh_types.h"
#define CLASS_DECLARE
#include "resource.h.x"
#undef CLASS_DECLARE
/* CLASS:
(class
(name resource)
(vars
; Hack to check liveness before the resource gets gc:ed.
; Live resources should never be forgotten.
(alive special int #f dont_free_live_resource)
(kill method void)))
*/
#define KILL_RESOURCE(r) ((r)->kill((r)))
/* For the resource list. It is doubly linked to make removing
* elements easy. */
/* NOTE: No object header */
/* NOTE: This list is very similar to the packet queue in
* write_buffer.h. It may be a good idea to write a more general
* doubly linked list. */
struct resource_node
{
struct resource_node *next;
struct resource_node *prev;
struct resource *resource;
};
/* FIXME: Non-virtual methods would make sense for this class. Or
* perhaps we should use a struct rather than a class? */
/* CLASS:
(class
(name resource_list)
(vars
(head special "struct resource_node *"
do_mark_resources do_free_resources)
(tail simple "struct resource_node *")
; Returns the node.
; NOTE: This pointer should only be stored together with
; the resource list object pointer, as the nodes are not gc:ed
; individually.
(remember method "struct resource_node *" "struct resource *r")
; Kills the resource and unlinks and deallocates the node.
;; (kill_resource method void "struct resource_node *n")
(kill_all method void)))
*/
/* For now, don't use the value returned from remember. */
#define REMEMBER_RESOURCE(l, r) ((void) ((l)->remember((l), (r))))
#define KILL_RESOURCE_NODE(l, n) ((l)->kill_resource((l), (n)))
#define KILL_RESOURCE_LIST(l) ((l)->kill_all((l)))
/* Allcoates an empty list. */
struct resource_list *empty_resource_list(void);
#endif /* LSH_RESOURCE_H_INCLUDED */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment