summaryrefslogtreecommitdiff
path: root/lib/list.c
blob: 8dcca723b037b1b317092bf1293f6a2e8c612e3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* list.c: simple list (represented as arrays) manipulation.

Copyright (C) 1992 Free Software Foundation, Inc.

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, 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 "config.h"

#include "list.h"


list_type
list_init ()
{
  list_type answer;

  LIST_DATA (answer) = NULL;
  LIST_SIZE (answer) = 0;

  return answer;
}


/* Free the memory for both the elements and the list itself.  */

void
list_free (list_type *list)
{
  if (list != NULL && LIST_DATA (*list) != NULL)
    {
      unsigned e;
      
      for (e = 0; e < LIST_SIZE (*list); e++)
        free (LIST_ELT (*list, e));
      
      free (LIST_DATA (*list));
    }
}

/* The list consists entirely of pointers to objects.  We allocate the
   space for the objects pointed to here, though, and return a pointer to
   the newly-created final element in the list.  */

address
list_append (list_type *list, unsigned element_size)
{
  LIST_SIZE (*list)++;
  XRETALLOC (LIST_DATA (*list), LIST_SIZE (*list), address);
  LIST_LAST_ELT (*list) = xmalloc (element_size);

  return LIST_LAST_ELT (*list);
}