~ubuntu-branches/ubuntu/precise/openarena/precise

« back to all changes in this revision

Viewing changes to code/tools/lcc/src/list.c

  • Committer: Bazaar Package Importer
  • Author(s): Bruno "Fuddl" Kleinert
  • Date: 2007-01-20 12:28:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070120122809-2yza5ojt7nqiyiam
Tags: upstream-0.6.0
ImportĀ upstreamĀ versionĀ 0.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "c.h"
 
2
 
 
3
 
 
4
static List freenodes;          /* free list nodes */
 
5
 
 
6
/* append - append x to list, return new list */
 
7
List append(void *x, List list) {
 
8
        List new;
 
9
 
 
10
        if ((new = freenodes) != NULL)
 
11
                freenodes = freenodes->link;
 
12
        else
 
13
                NEW(new, PERM);
 
14
        if (list) {
 
15
                new->link = list->link;
 
16
                list->link = new;
 
17
        } else
 
18
                new->link = new;
 
19
        new->x = x;
 
20
        return new;
 
21
}
 
22
 
 
23
/* length - # elements in list */
 
24
int length(List list) {
 
25
        int n = 0;
 
26
 
 
27
        if (list) {
 
28
                List lp = list;
 
29
                do
 
30
                        n++;
 
31
                while ((lp = lp->link) != list);
 
32
        }
 
33
        return n;
 
34
}
 
35
 
 
36
/* ltov - convert list to an NULL-terminated vector allocated in arena */
 
37
void *ltov(List *list, unsigned arena) {
 
38
        int i = 0;
 
39
        void **array = newarray(length(*list) + 1, sizeof array[0], arena);
 
40
 
 
41
        if (*list) {
 
42
                List lp = *list;
 
43
                do {
 
44
                        lp = lp->link;
 
45
                        array[i++] = lp->x;
 
46
                } while (lp != *list);
 
47
#ifndef PURIFY
 
48
                lp = (*list)->link;
 
49
                (*list)->link = freenodes;
 
50
                freenodes = lp;
 
51
#endif
 
52
        }
 
53
        *list = NULL;
 
54
        array[i] = NULL;
 
55
        return array;
 
56
}