hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (C) 2008 Philippe Gerum <rpm@xenomai.org>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
 
#include <vxworks/errnoLib.h>
#include <vxworks/lstLib.h>
 
void lstExtract(LIST *lsrc, NODE *nstart, NODE *nend, LIST *ldst)
{
   struct pvholder *nholder = &nstart->link, *holder;
   struct NODE *n;
   int nitems = 0;
 
   do {    /* XXX: terribly inefficient, but plain simple... */
       holder = nholder;
       nholder = holder->next;
       pvlist_remove_init(holder);
       pvlist_append(holder, &ldst->list);
       n = container_of(holder, struct NODE, link);
       n->list = ldst;
       nitems++;
   } while (holder != &nend->link);
 
   lsrc->count -= nitems;
   ldst->count += nitems;
}
 
NODE *lstNth(LIST *l, int nodenum)
{
   struct pvholder *holder;
   int nth;
 
   if (l == NULL || nodenum <= 0 || pvlist_empty(&l->list))
       return NULL;
 
   if (nodenum <= l->count >> 2) { /* nodenum is 1-based. */
       nth = 1;
       pvlist_for_each(holder, &l->list)
           if (nodenum == nth++)
               return container_of(holder, struct NODE, link);
   } else {
       nth = l->count;
       pvlist_for_each_reverse(holder, &l->list)
           if (nodenum == nth--)
               return container_of(holder, struct NODE, link);
   }
 
   return NULL;
}
 
NODE *lstNStep(NODE *n, int steps)
{
   struct pvholder *holder = &n->link;
 
   if (steps == 0)
       return n;
 
   if (steps < 0) {
       do
           holder = holder->prev;
       while (holder->prev != &n->link && ++steps < 0);
   } else {
       do
           holder = holder->next;
       while (holder->next != &n->link && --steps > 0);
   }
 
   if (steps != 0)
       return NULL;
 
   return container_of(holder, struct NODE, link);
}
 
int lstFind(LIST *l, NODE *n)
{
   struct pvholder *holder;
   int nth = 1;
 
   if (l == NULL || pvlist_empty(&l->list))
       return ERROR;
 
   pvlist_for_each(holder, &l->list) {
       if (holder == &n->link)
           return nth;
       nth++;
   }
 
   return ERROR;
}
 
void lstConcat(LIST *ldst, LIST *lsrc)
{
   struct pvholder *holder;
   struct NODE *n;
 
   if (pvlist_empty(&lsrc->list))
       return;
 
   pvlist_for_each(holder, &lsrc->list) {
       n = container_of(holder, struct NODE, link);
       n->list = ldst;
   }
 
   pvlist_join(&lsrc->list, &ldst->list);
   ldst->count += lsrc->count;
   lsrc->count = 0;
}