summaryrefslogtreecommitdiff
path: root/firmware
diff options
context:
space:
mode:
Diffstat (limited to 'firmware')
-rw-r--r--firmware/export/general.h15
-rw-r--r--firmware/general.c37
-rw-r--r--firmware/kernel.c35
3 files changed, 53 insertions, 34 deletions
diff --git a/firmware/export/general.h b/firmware/export/general.h
index 8da4a0ad59..16c824086b 100644
--- a/firmware/export/general.h
+++ b/firmware/export/general.h
@@ -72,4 +72,19 @@ char *create_datetime_filename(char *buffer, const char *path,
72 bool unique_time); 72 bool unique_time);
73#endif /* CONFIG_RTC */ 73#endif /* CONFIG_RTC */
74 74
75/***
76 ** Compacted pointer lists
77 **
78 ** N-length list requires N+1 elements to ensure NULL-termination.
79 **/
80
81/* Find a pointer in a pointer array. Returns the addess of the element if
82 found or the address of the terminating NULL otherwise. This can be used
83 to bounds check and add items. */
84void ** find_array_ptr(void **arr, void *ptr);
85
86/* Remove a pointer from a pointer array if it exists. Compacts it so that
87 no gaps exist. Returns 0 on success and -1 if the element wasn't found. */
88int remove_array_ptr(void **arr, void *ptr);
89
75#endif /* GENERAL_H */ 90#endif /* GENERAL_H */
diff --git a/firmware/general.c b/firmware/general.c
index fa1025492b..20b0277c09 100644
--- a/firmware/general.c
+++ b/firmware/general.c
@@ -196,3 +196,40 @@ char *create_datetime_filename(char *buffer, const char *path,
196 return buffer; 196 return buffer;
197} 197}
198#endif /* CONFIG_RTC */ 198#endif /* CONFIG_RTC */
199
200/***
201 ** Compacted pointer lists
202 **
203 ** N-length list requires N+1 elements to ensure NULL-termination.
204 **/
205
206/* Find a pointer in a pointer array. Returns the addess of the element if
207 * found or the address of the terminating NULL otherwise. This can be used
208 * to bounds check and add items. */
209void ** find_array_ptr(void **arr, void *ptr)
210{
211 void *curr;
212 for (curr = *arr; curr != NULL && curr != ptr; curr = *(++arr));
213 return arr;
214}
215
216/* Remove a pointer from a pointer array if it exists. Compacts it so that
217 * no gaps exist. Returns 0 on success and -1 if the element wasn't found. */
218int remove_array_ptr(void **arr, void *ptr)
219{
220 void *curr;
221 arr = find_array_ptr(arr, ptr);
222
223 if (*arr == NULL)
224 return -1;
225
226 /* Found. Slide up following items. */
227 do
228 {
229 void **arr1 = arr + 1;
230 *arr++ = curr = *arr1;
231 }
232 while (curr != NULL);
233
234 return 0;
235}
diff --git a/firmware/kernel.c b/firmware/kernel.c
index d8c67e8485..fe40c35002 100644
--- a/firmware/kernel.c
+++ b/firmware/kernel.c
@@ -27,6 +27,7 @@
27#include "system.h" 27#include "system.h"
28#include "panic.h" 28#include "panic.h"
29#include "debug.h" 29#include "debug.h"
30#include "general.h"
30 31
31/* Make this nonzero to enable more elaborate checks on objects */ 32/* Make this nonzero to enable more elaborate checks on objects */
32#if defined(DEBUG) || defined(SIMULATOR) 33#if defined(DEBUG) || defined(SIMULATOR)
@@ -62,40 +63,6 @@ static struct
62} all_queues SHAREDBSS_ATTR; 63} all_queues SHAREDBSS_ATTR;
63 64
64/**************************************************************************** 65/****************************************************************************
65 * Common utilities
66 ****************************************************************************/
67
68/* Find a pointer in a pointer array. Returns the addess of the element if
69 * found or the address of the terminating NULL otherwise. */
70static void ** find_array_ptr(void **arr, void *ptr)
71{
72 void *curr;
73 for(curr = *arr; curr != NULL && curr != ptr; curr = *(++arr));
74 return arr;
75}
76
77/* Remove a pointer from a pointer array if it exists. Compacts it so that
78 * no gaps exist. Returns 0 on success and -1 if the element wasn't found. */
79static int remove_array_ptr(void **arr, void *ptr)
80{
81 void *curr;
82 arr = find_array_ptr(arr, ptr);
83
84 if(*arr == NULL)
85 return -1;
86
87 /* Found. Slide up following items. */
88 do
89 {
90 void **arr1 = arr + 1;
91 *arr++ = curr = *arr1;
92 }
93 while(curr != NULL);
94
95 return 0;
96}
97
98/****************************************************************************
99 * Standard kernel stuff 66 * Standard kernel stuff
100 ****************************************************************************/ 67 ****************************************************************************/
101void kernel_init(void) 68void kernel_init(void)