1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2020 Rockchip Electronics Co. Ltd.
4 *
5 * Author: Zorro Liu <zorro.liu@rock-chips.com>
6 */
7
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/string.h>
11
12 #include "buf_list.h"
13
14 #define IS_NULL(ptr) (NULL == ptr)
15
buf_list_init(buf_list_t ** li,int maxelements)16 int buf_list_init(buf_list_t **li, int maxelements)
17 {
18 (*li) = (buf_list_t *)kmalloc(sizeof(buf_list_t), GFP_KERNEL);
19 if ((*li) == NULL)
20 return -ENOMEM;
21
22 (*li)->nb_elt = 0;
23 (*li)->array_elements = NULL;
24 (*li)->maxelements = maxelements;
25
26 (*li)->array_elements = (int **)kmalloc(sizeof(int *) * maxelements, GFP_KERNEL);
27 if ((*li)->array_elements == NULL) {
28 kfree(*li);
29 return -ENOMEM;
30 }
31 memset((*li)->array_elements, 0, (sizeof(int *) * maxelements));
32
33 return 0;
34 }
35
buf_list_uninit(buf_list_t * li)36 int buf_list_uninit(buf_list_t *li)
37 {
38 if (!(IS_NULL(li))) {
39 if (!(IS_NULL(li->array_elements))) {
40 memset(li->array_elements, 0, (sizeof(int *) * (li->maxelements)));
41 kfree(li->array_elements);
42 li->array_elements = NULL;
43 }
44 if (li)
45 kfree(li);
46 }
47
48 return 0;
49 }
50
buf_list_eol(buf_list_t * li,int i)51 int buf_list_eol(buf_list_t *li, int i)
52 {
53 if (IS_NULL(li) || IS_NULL(li->array_elements))
54 return 1;
55
56 if ((i >= 0) && (i < li->nb_elt))
57 return 0;
58
59 /* end of list */
60 return 1;
61 }
62
buf_list_get(buf_list_t * li,int pos)63 int *buf_list_get(buf_list_t *li, int pos)
64 {
65 if ((IS_NULL(li)) || (IS_NULL(li->array_elements)) || (pos < 0) || (pos >= li->nb_elt))
66 /* element does not exist */
67 return NULL;
68
69 return li->array_elements[pos];
70 }
71
buf_list_remove(buf_list_t * li,int pos)72 int buf_list_remove(buf_list_t *li, int pos)
73 {
74 int i = 0;
75
76 if ((IS_NULL(li)) || (IS_NULL(li->array_elements)) || (pos < 0) || (pos >= li->nb_elt))
77 /* element does not exist */
78 return -1;
79
80 /* exist because nb_elt > 0 */
81 i = pos;
82 while (i < li->nb_elt - 1) {
83 li->array_elements[i] = li->array_elements[i + 1];
84 i++;
85 }
86 li->nb_elt--;
87
88 return li->nb_elt;
89 }
90
buf_list_add(buf_list_t * li,int * el,int pos)91 int buf_list_add(buf_list_t *li, int *el, int pos)
92 {
93 int i = 0;
94
95 if ((IS_NULL(li)) || (IS_NULL(li->array_elements)))
96 return -1;
97
98 if ((pos < 0) || (pos >= li->nb_elt)) {
99 /* insert at the end */
100 pos = li->nb_elt;
101 } else {
102 i = (li->nb_elt - 1);
103 while (i >= pos) {
104 li->array_elements[i + 1] = li->array_elements[i];
105 i--;
106 }
107 }
108
109 if (pos >= (li->maxelements))
110 return -1;
111
112 li->array_elements[pos] = el;
113 li->nb_elt++;
114
115 return li->nb_elt;
116 }
117
buf_list_find(buf_list_t * list,int * node,int (* cmp_func)(int *,int *))118 int *buf_list_find(buf_list_t *list, int *node, int (*cmp_func)(int *, int *))
119 {
120 int pos = 0;
121 void *tmp = NULL;
122
123 if ((IS_NULL(list)) || (IS_NULL(list->array_elements)))
124 return NULL;
125
126 while (pos < list->nb_elt) /*(!buf_list_eol(list, pos))*/ {
127 int *node_;
128 #if 1
129 node_ = list->array_elements[pos];
130 #else
131 node_ = buf_list_get(list, pos);
132 #endif
133 if (cmp_func(node, node_) == 0) {
134 tmp = node_;
135 break;
136 }
137 pos++;
138 }
139
140 return tmp;
141 }
142
buf_list_get_pos(buf_list_t * list,int * node)143 int buf_list_get_pos(buf_list_t *list, int *node)
144 {
145 int pos = 0;
146
147 if ((IS_NULL(list)) || (IS_NULL(list->array_elements)) || (list->nb_elt <= 0))
148 return -1;
149
150 /* exist because nb_elt > 0 */
151 pos = 0;
152 while (pos < list->nb_elt) {
153 if ((int *)(list->array_elements[pos]) == node)
154 return pos;
155 pos++;
156 }
157
158 return -1;
159 }
160
buf_list_set(buf_list_t * li,int * el,int pos)161 int buf_list_set(buf_list_t *li, int *el, int pos)
162 {
163 if ((IS_NULL(li)) || (IS_NULL(li->array_elements)) || (pos < 0) || (pos >= li->nb_elt))
164 /* element does not exist */
165 return -1;
166
167 /* exist because nb_elt > 0 */
168 li->array_elements[pos] = el;
169
170 return 0;
171 }
172