hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2020 Rockchip Electronics Co. Ltd.
 *
 * Author: Zorro Liu <zorro.liu@rock-chips.com>
 */
 
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/string.h>
 
#include "buf_list.h"
 
#define IS_NULL(ptr) (NULL == ptr)
 
int buf_list_init(buf_list_t **li, int maxelements)
{
   (*li) = (buf_list_t *)kmalloc(sizeof(buf_list_t), GFP_KERNEL);
   if ((*li) == NULL)
       return -ENOMEM;
 
   (*li)->nb_elt = 0;
   (*li)->array_elements = NULL;
   (*li)->maxelements = maxelements;
 
   (*li)->array_elements = (int **)kmalloc(sizeof(int *) * maxelements, GFP_KERNEL);
   if ((*li)->array_elements == NULL) {
       kfree(*li);
       return -ENOMEM;
   }
   memset((*li)->array_elements, 0, (sizeof(int *) * maxelements));
 
   return 0;
}
 
int buf_list_uninit(buf_list_t *li)
{
   if (!(IS_NULL(li))) {
       if (!(IS_NULL(li->array_elements))) {
           memset(li->array_elements, 0, (sizeof(int *) * (li->maxelements)));
           kfree(li->array_elements);
           li->array_elements = NULL;
       }
       if (li)
           kfree(li);
   }
 
   return 0;
}
 
int buf_list_eol(buf_list_t *li, int i)
{
   if (IS_NULL(li) || IS_NULL(li->array_elements))
       return 1;
 
   if ((i >= 0) && (i < li->nb_elt))
       return 0;
 
   /* end of list */
   return 1;
}
 
int *buf_list_get(buf_list_t *li, int pos)
{
   if ((IS_NULL(li)) || (IS_NULL(li->array_elements)) || (pos < 0) || (pos >= li->nb_elt))
       /* element does not exist */
       return NULL;
 
   return li->array_elements[pos];
}
 
int buf_list_remove(buf_list_t *li, int pos)
{
   int i = 0;
 
   if ((IS_NULL(li)) || (IS_NULL(li->array_elements)) || (pos < 0) || (pos >= li->nb_elt))
       /* element does not exist */
       return -1;
 
   /* exist because nb_elt > 0 */
   i = pos;
   while (i < li->nb_elt - 1) {
       li->array_elements[i] = li->array_elements[i + 1];
       i++;
   }
   li->nb_elt--;
 
   return li->nb_elt;
}
 
int buf_list_add(buf_list_t *li, int *el, int pos)
{
   int i = 0;
 
   if ((IS_NULL(li)) || (IS_NULL(li->array_elements)))
       return -1;
 
   if ((pos < 0) || (pos >= li->nb_elt)) {
       /* insert at the end  */
       pos = li->nb_elt;
   } else {
       i = (li->nb_elt - 1);
       while (i >= pos) {
           li->array_elements[i + 1] = li->array_elements[i];
           i--;
       }
   }
 
   if (pos >= (li->maxelements))
       return -1;
 
   li->array_elements[pos] = el;
   li->nb_elt++;
 
   return li->nb_elt;
}
 
int *buf_list_find(buf_list_t *list, int *node, int (*cmp_func)(int *, int *))
{
   int pos = 0;
   void *tmp = NULL;
 
   if ((IS_NULL(list)) || (IS_NULL(list->array_elements)))
       return NULL;
 
   while (pos < list->nb_elt) /*(!buf_list_eol(list, pos))*/ {
       int *node_;
#if 1
       node_ = list->array_elements[pos];
#else
       node_ = buf_list_get(list, pos);
#endif
       if (cmp_func(node, node_) == 0) {
           tmp = node_;
           break;
       }
       pos++;
   }
 
   return tmp;
}
 
int buf_list_get_pos(buf_list_t *list, int *node)
{
   int pos = 0;
 
   if ((IS_NULL(list)) || (IS_NULL(list->array_elements)) || (list->nb_elt <= 0))
       return -1;
 
   /* exist because nb_elt > 0 */
   pos = 0;
   while (pos < list->nb_elt) {
       if ((int *)(list->array_elements[pos]) == node)
           return pos;
       pos++;
   }
 
   return -1;
}
 
int buf_list_set(buf_list_t *li, int *el, int pos)
{
   if ((IS_NULL(li)) || (IS_NULL(li->array_elements)) || (pos < 0) || (pos >= li->nb_elt))
       /* element does not exist */
       return -1;
 
   /* exist because nb_elt > 0 */
   li->array_elements[pos] = el;
 
   return 0;
}