diff options
Diffstat (limited to 'firmware')
| -rw-r--r-- | firmware/export/general.h | 15 | ||||
| -rw-r--r-- | firmware/general.c | 37 | ||||
| -rw-r--r-- | firmware/kernel.c | 35 |
3 files changed, 53 insertions, 34 deletions
diff --git a/firmware/export/general.h b/firmware/export/general.h index 8da4a0a..16c8240 100644 --- a/firmware/export/general.h +++ b/firmware/export/general.h @@ -72,4 +72,19 @@ char *create_datetime_filename(char *buffer, const char *path, bool unique_time); #endif /* CONFIG_RTC */ +/*** + ** Compacted pointer lists + ** + ** N-length list requires N+1 elements to ensure NULL-termination. + **/ + +/* Find a pointer in a pointer array. Returns the addess of the element if + found or the address of the terminating NULL otherwise. This can be used + to bounds check and add items. */ +void ** find_array_ptr(void **arr, void *ptr); + +/* Remove a pointer from a pointer array if it exists. Compacts it so that + no gaps exist. Returns 0 on success and -1 if the element wasn't found. */ +int remove_array_ptr(void **arr, void *ptr); + #endif /* GENERAL_H */ diff --git a/firmware/general.c b/firmware/general.c index fa10254..20b0277 100644 --- a/firmware/general.c +++ b/firmware/general.c @@ -196,3 +196,40 @@ char *create_datetime_filename(char *buffer, const char *path, return buffer; } #endif /* CONFIG_RTC */ + +/*** + ** Compacted pointer lists + ** + ** N-length list requires N+1 elements to ensure NULL-termination. + **/ + +/* Find a pointer in a pointer array. Returns the addess of the element if + * found or the address of the terminating NULL otherwise. This can be used + * to bounds check and add items. */ +void ** find_array_ptr(void **arr, void *ptr) +{ + void *curr; + for (curr = *arr; curr != NULL && curr != ptr; curr = *(++arr)); + return arr; +} + +/* Remove a pointer from a pointer array if it exists. Compacts it so that + * no gaps exist. Returns 0 on success and -1 if the element wasn't found. */ +int remove_array_ptr(void **arr, void *ptr) +{ + void *curr; + arr = find_array_ptr(arr, ptr); + + if (*arr == NULL) + return -1; + + /* Found. Slide up following items. */ + do + { + void **arr1 = arr + 1; + *arr++ = curr = *arr1; + } + while (curr != NULL); + + return 0; +} diff --git a/firmware/kernel.c b/firmware/kernel.c index d8c67e8..fe40c35 100644 --- a/firmware/kernel.c +++ b/firmware/kernel.c @@ -27,6 +27,7 @@ #include "system.h" #include "panic.h" #include "debug.h" +#include "general.h" /* Make this nonzero to enable more elaborate checks on objects */ #if defined(DEBUG) || defined(SIMULATOR) @@ -62,40 +63,6 @@ static struct } all_queues SHAREDBSS_ATTR; /**************************************************************************** - * Common utilities - ****************************************************************************/ - -/* Find a pointer in a pointer array. Returns the addess of the element if - * found or the address of the terminating NULL otherwise. */ -static void ** find_array_ptr(void **arr, void *ptr) -{ - void *curr; - for(curr = *arr; curr != NULL && curr != ptr; curr = *(++arr)); - return arr; -} - -/* Remove a pointer from a pointer array if it exists. Compacts it so that - * no gaps exist. Returns 0 on success and -1 if the element wasn't found. */ -static int remove_array_ptr(void **arr, void *ptr) -{ - void *curr; - arr = find_array_ptr(arr, ptr); - - if(*arr == NULL) - return -1; - - /* Found. Slide up following items. */ - do - { - void **arr1 = arr + 1; - *arr++ = curr = *arr1; - } - while(curr != NULL); - - return 0; -} - -/**************************************************************************** * Standard kernel stuff ****************************************************************************/ void kernel_init(void) |