Allow allocation of changed paramaters to fail, then all param functions will return PARAM_INVALID or a count of 0

This commit is contained in:
David Sidrane 2015-06-01 15:06:44 -10:00 committed by Lorenz Meier
parent bcb3529490
commit 03bdf1e5f2
1 changed files with 36 additions and 29 deletions

View File

@ -47,7 +47,6 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <systemlib/err.h> #include <systemlib/err.h>
@ -116,7 +115,13 @@ get_param_info_count(void)
if (!param_changed_storage) { if (!param_changed_storage) {
size_param_changed_storage_bytes = (param_info_count / bits_per_allocation_unit) + 1; size_param_changed_storage_bytes = (param_info_count / bits_per_allocation_unit) + 1;
param_changed_storage = calloc(size_param_changed_storage_bytes, 1); param_changed_storage = calloc(size_param_changed_storage_bytes, 1);
ASSERT(param_changed_storage);
/* If the allocation fails we need to indicate failure in the
* API by returning PARAM_INVALID
*/
if (param_changed_storage == NULL) {
return 0;
}
} }
return param_info_count; return param_info_count;
@ -168,7 +173,8 @@ param_assert_locked(void)
static bool static bool
handle_in_range(param_t param) handle_in_range(param_t param)
{ {
return (param < get_param_info_count()); int count = get_param_info_count();
return (count && param < count);
} }
/** /**
@ -248,18 +254,19 @@ param_find_internal(const char *name, bool notification)
{ {
param_t param; param_t param;
/* perform a linear search of the known parameters */ /* perform a linear search of the known parameters */
for (param = 0; handle_in_range(param); param++) {
if (!strcmp(param_info_base[param].name, name)) {
if (notification) {
param_set_used_internal(param);
}
return param; for (param = 0; handle_in_range(param); param++) {
} if (!strcmp(param_info_base[param].name, name)) {
} if (notification) {
param_set_used_internal(param);
}
/* not found */ return param;
}
}
/* not found */
return PARAM_INVALID; return PARAM_INVALID;
} }
@ -284,38 +291,38 @@ param_count(void)
unsigned unsigned
param_count_used(void) param_count_used(void)
{ {
// ensure the allocation has been done unsigned count = 0;
get_param_info_count();
unsigned count = 0;
for (unsigned i = 0; i < size_param_changed_storage_bytes; i++) { // ensure the allocation has been done
for (unsigned j = 0; j < bits_per_allocation_unit; j++) { if (get_param_info_count()) {
if (param_changed_storage[i] & (1 << j)) {
count++;
}
}
}
return count; for (unsigned i = 0; i < size_param_changed_storage_bytes; i++) {
for (unsigned j = 0; j < bits_per_allocation_unit; j++) {
if (param_changed_storage[i] & (1 << j)) {
count++;
}
}
}
}
return count;
} }
param_t param_t
param_for_index(unsigned index) param_for_index(unsigned index)
{ {
if (index < get_param_info_count()) { unsigned count = get_param_info_count();
if (count && index < count) {
return (param_t)index; return (param_t)index;
} }
return PARAM_INVALID; return PARAM_INVALID;
} }
param_t param_t
param_for_used_index(unsigned index) param_for_used_index(unsigned index)
{ {
if (index < get_param_info_count()) { int count = get_param_info_count();
if (count && index < count) {
/* walk all params and count */ /* walk all params and count */
int count = 0;
for (unsigned i = 0; i < (unsigned)size_param_changed_storage_bytes; i++) { for (unsigned i = 0; i < (unsigned)size_param_changed_storage_bytes; i++) {
for (unsigned j = 0; j < bits_per_allocation_unit; j++) { for (unsigned j = 0; j < bits_per_allocation_unit; j++) {