Debian Codegraph @

lvgl/src/draw/nxp/g2d/lv_g2d_buf_map.c

raw symbols SCIP
1 /**
2 * @file lv_g2d_buf_map.c
3 *
4 */
5
6 /**
7 * Copyright 2024 NXP
8 *
9 * SPDX-License-Identifier: MIT
10 */
11
12 #include "lv_g2d_buf_map.h"
13
14 #if LV_USE_G2D
15 #if LV_USE_DRAW_G2D || LV_USE_ROTATE_G2D
16 #include <stdio.h>
17 #include "lv_g2d_utils.h"
18 #include "g2d.h"
19
20 /*********************
21 * DEFINES
22 *********************/
23
24 /**********************
25 * TYPEDEFS
26 **********************/
27
28 /**********************
29 * STATIC PROTOTYPES
30 **********************/
31
32 static unsigned long _map_hash_function(void * ptr);
33
34 static lv_map_item_t * _map_create_item(void * key, struct g2d_buf * value);
35
36 static void _map_free_item(lv_map_item_t * item);
37
38 static void _handle_collision(unsigned long index, lv_map_item_t * item);
39
40 static void _map_free_list(unsigned long index, lv_array_t * list);
41
42 /**********************
43 * STATIC VARIABLES
44 **********************/
45
46 static lv_buf_map_t * table;
47
48 /**********************
49 * MACROS
50 **********************/
51
52 /**********************
53 * GLOBAL FUNCTIONS
54 **********************/
55
56 void g2d_create_buf_map(void)
57 {
58 table = (lv_buf_map_t *) lv_malloc(sizeof(lv_buf_map_t));
59 table->size = LV_G2D_HASH_TABLE_SIZE;
60 table->count = 0;
61 table->items = (lv_map_item_t **) lv_malloc_zeroed(table->size * sizeof(lv_map_item_t *));
62 table->overflow_list = (lv_array_t **) lv_malloc_zeroed(table->size * sizeof(lv_array_t *));
63 }
64
65 void g2d_free_buf_map(void)
66 {
67 for(int i = 0; i < table->size; i++) {
68 if(table->overflow_list[i]) {
69 lv_array_deinit(table->overflow_list[i]);
70 lv_free(table->overflow_list[i]);
71 }
72
73 lv_map_item_t * item = table->items[i];
74 if(item != NULL)
75 _map_free_item(item);
76 }
77
78 lv_free(table->items);
79 lv_free(table->overflow_list);
80 lv_free(table);
81 }
82
83 void g2d_insert_buf_map(void * key, struct g2d_buf * value)
84 {
85 lv_map_item_t * item = _map_create_item(key, value);
86 int index = _map_hash_function(key);
87
88 if(table->count == table->size) {
89 /* Table is full. */
90 _map_free_item(item);
91 G2D_ASSERT_MSG(false, "Hash table is full. Increase LV_G2D_HASH_TABLE_SIZE.");
92 return;
93 }
94
95 if(table->items[index] == NULL) {
96 /* Key not found. Insert item. */
97 table->items[index] = item;
98 table->count++;
99 return;
100 }
101 else {
102 if(table->items[index]->key == key) {
103 /* Key already exists, update value. */
104 table->items[index]->value = value;
105 return;
106 }
107 else {
108 /* Handle the collision */
109 _handle_collision(index, item);
110 return;
111 }
112 }
113
114 }
115
116 struct g2d_buf * g2d_search_buf_map(void * key)
117 {
118 int index = _map_hash_function(key);
119 lv_map_item_t * item = table->items[index];
120 lv_array_t * list = (lv_array_t *)table->overflow_list[index];
121
122 if(item == NULL)
123 return NULL;
124
125 if(item->key == key)
126 return item->value;
127
128 if(list == NULL)
129 return NULL;
130
131 for(uint32_t i = 0; i < lv_array_size(list); i++) {
132 item = (lv_map_item_t *)lv_array_at(list, i);
133 if(item->key == key)
134 return item->value;
135 }
136
137 return NULL;
138 }
139
140 void g2d_free_item(void * key)
141 {
142 /* Delete an item from the table. */
143 int index = _map_hash_function(key);
144 lv_map_item_t * item = table->items[index];
145 lv_array_t * list = (lv_array_t *)table->overflow_list[index];
146
147 /* If there is no item, then nothing to do. */
148 if(item == NULL) {
149 return;
150 }
151 else if(item->key == key) {
152 /* Remove the item. */
153 table->items[index] = NULL;
154 _map_free_item(item);
155 table->count--;
156
157 /* If there is no collision chain, just return. */
158 if(list == NULL) {
159 return;
160 }
161
162 /* If a collision chain exists, promote the first item. */
163 lv_map_item_t * promoted_item = (lv_map_item_t *)lv_array_at(list, 0);
164 table->items[index] = _map_create_item(promoted_item->key, promoted_item->value);
165 lv_array_remove(list, 0);
166 if(lv_array_size(list) == 0) {
167 _map_free_list(index, list);
168 }
169 return;
170 }
171 /* If the item is not the one we are searching, and there is no list, then return. */
172 if(list == NULL) return;
173 else {
174 /* The item might be inside the list. */
175 for(uint32_t i = 0; i < lv_array_size(list); i++) {
176 item = (lv_map_item_t *)lv_array_at(list, i);
177 if(item->key == key) {
178 g2d_free(item->value);
179 lv_array_remove(list, i);
180 table->count--;
181 if(lv_array_size(list) == 0) {
182 _map_free_list(index, list);
183 }
184 return;
185 }
186 }
187 }
188 }
189
190 void g2d_print_table(void)
191 {
192 LV_LOG("\nHash Table\n-------------------\n");
193
194 for(int i = 0; i < table->size; i++) {
195 if(table->items[i]) {
196 LV_LOG("Index:%d, Key:%p, Value:%p\n", i, table->items[i] -> key, (void *)table->items[i]->value);
197 if(table->overflow_list[i]) {
198 for(uint32_t j = 0 ; j < lv_array_size(table->overflow_list[i]); j++) {
199 lv_map_item_t * item = (lv_map_item_t *)lv_array_at(table->overflow_list[i], j);
200 LV_LOG("Index:%d, Key:%p, Value:%p\n", i, item -> key, (void *)item->value);
201 }
202
203 }
204 }
205 }
206
207 LV_LOG("-------------------\n\n");
208 }
209
210 /**********************
211 * STATIC FUNCTIONS
212 **********************/
213
214 static unsigned long _map_hash_function(void * ptr)
215 {
216 unsigned long i = 0;
217 char str[64];
218 G2D_ASSERT_MSG(ptr, "Key is null.");
219 sprintf(str, "%p", ptr);
220
221 for(int j = 0; str[j]; j++)
222 i += str[j];
223
224 return i % LV_G2D_HASH_TABLE_SIZE;
225 }
226
227 static void _handle_collision(unsigned long index, lv_map_item_t * item)
228 {
229 if(table->overflow_list[index] == NULL) {
230 /* Create the list. */
231 lv_array_t * list = (lv_array_t *) lv_malloc(sizeof(lv_array_t));
232 lv_array_init(list, LV_ARRAY_DEFAULT_CAPACITY, sizeof(lv_map_item_t));
233 lv_array_push_back(list, item);
234 table->overflow_list[index] = list;
235 table->count++;
236 return;
237 }
238 else {
239 lv_array_t * list = (lv_array_t *)table->overflow_list[index];
240 for(uint32_t i = 0; i < lv_array_size(list); i++) {
241 lv_map_item_t * it = (lv_map_item_t *)lv_array_at(list, i);
242 if(it->key == item->key) {
243 /* Key exists, update value. */
244 it->value = item->value;
245 return;
246 }
247 }
248 /* Insert to the list. */
249 lv_array_push_back(table->overflow_list[index], item);
250 table->count++;
251 return;
252 }
253 }
254
255 static lv_map_item_t * _map_create_item(void * key, struct g2d_buf * value)
256 {
257 lv_map_item_t * item = (lv_map_item_t *) lv_malloc(sizeof(lv_map_item_t));
258 G2D_ASSERT_MSG(item, "Failed to alloc item.");
259 item->key = key;
260 item->value = value;
261 return item;
262 }
263
264 static void _map_free_item(lv_map_item_t * item)
265 {
266 /* Also free the g2d_buf. */
267 g2d_free(item->value);
268 item->key = NULL;
269 item->value = NULL;
270 lv_free(item);
271 item = NULL;
272 }
273
274 static void _map_free_list(unsigned long index, lv_array_t * list)
275 {
276 lv_array_deinit(list);
277 lv_free(list);
278 table->overflow_list[index] = NULL;
279 }
280
281 #endif /*LV_USE_DRAW_G2D || LV_USE_ROTATE_G2D*/
282 #endif /*LV_USE_G2D*/