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

New garbage-collectible list type.

Rev: src/list.c:1.1
Rev: src/list.h:1.1
Rev: src/xalloc.c:1.13
Rev: src/xalloc.h:1.13
parent d674cd09
/* list.h
*
* Variable length lists of atoms (or other integers).
*
* $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.
*/
#include "list.h"
#define CLASS_DEFINE
#include "list.h.x"
#undef CLASS_DEFINE
#include "xalloc.h"
struct lsh_list *make_list(unsigned n, ...);
{
int i;
va_list args;
struct lsh_list *l = lsh_list_alloc(n);
va_start(args, n);
for (i=0; i<n; i++)
{
int atom = va_arg(args, int);
assert(atom >= 0);
LIST(l)[i] = atom;
}
assert(va_arg(args, int) == -1);
return l;
}
/* list.h
*
* Variable length lists of atoms (or other integers).
*
* $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_LIST_H_INCLUDED
#define LSH_LIST_H_INCLUDED
#include "lsh_types.h"
#define CLASS_DECLARE
#include "list.h.x"
#undef CLASS_DECLARE
/* CLASS:
(class
(name lsh_list)
(vars
(length simple unsigned)
; This is really of variable size
(elements array int 1)))
*/
#define LIST(x) ((x)->elements)
struct lsh_list *make_list(unsigned length, ...);
#endif /* LSH_LIST_H_INCLUDED */
......@@ -24,6 +24,7 @@
*/
#include "xalloc.h"
#include "werror.h"
#include <stdlib.h>
......@@ -127,6 +128,21 @@ struct lsh_object *lsh_object_alloc(struct lsh_class *class)
return instance;
}
struct lsh_list *lsh_list_alloc(unsigned length)
{
struct lsh_class *class = &CLASS(lsh_list);
struct lsh_list *list = xalloc(class->size
+ sizeof(int) * (length - 1));
list->super.isa = class;
list->super.alloc_method = LSH_ALLOC_HEAP;
list->length = length;
gc_register(&list->super);
return list;
}
/* Should be called *only* by the gc */
void lsh_object_free(struct lsh_object *o)
{
......
......@@ -26,7 +26,8 @@
#ifndef LSH_XALLOC_H_INCLUDED
#define LSH_XALLOC_H_INCLUDED
#include "lsh_types.h"
#include "list.h"
#include <stdlib.h>
/* Allocation */
......@@ -53,6 +54,7 @@ void lsh_object_free(struct lsh_object *o);
void *lsh_space_alloc(size_t size);
void lsh_space_free(void *p);
struct lsh_list *lsh_list_alloc(unsigned length);
#ifdef DEBUG_ALLOC
......
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