u_arraylist_t *u_arraylist_create()
{
- u_arraylist_t *list = (u_arraylist_t *) OICMalloc(sizeof(u_arraylist_t));
+ u_arraylist_t *list = (u_arraylist_t *) OICCalloc(1, sizeof(u_arraylist_t));
if (!list)
{
OIC_LOG(DEBUG, TAG, "Out of memory");
*list = NULL;
}
-void u_arraylist_reserve(u_arraylist_t *list, size_t count)
+bool u_arraylist_reserve(u_arraylist_t *list, size_t count)
{
if (list && (count > list->capacity))
{
if (!tmp)
{
OIC_LOG(DEBUG, TAG, "Memory reallocation failed.");
- // Note that this is considered non-fatal.
+ return false;
}
else
{
list->capacity = count;
}
}
+ return true;
}
void u_arraylist_shrink_to_fit(u_arraylist_t *list)
return NULL;
}
+bool u_arraylist_get_index(const u_arraylist_t *list, const void *data, uint32_t *index)
+{
+ if (!list || !data)
+ {
+ return false;
+ }
+
+ for (uint32_t i = 0; i < list->length; i++)
+ {
+ if (data == list->data[i])
+ {
+ *index = i;
+ return true;
+ }
+ }
+
+ return false;
+}
+
bool u_arraylist_add(u_arraylist_t *list, void *data)
{
- static const double GROWTH_FACTOR = 1.5;
if (!list)
{
return false;
if (list->capacity <= list->length)
{
- uint32_t new_capacity = (list->capacity * GROWTH_FACTOR) + 0.5;
+ // Does a non-FP calcuation of the 1.5 growth factor. Helpful for
+ // certain limited platforms.
+ size_t new_capacity = ((list->capacity * 3) + 1) / 2;
+
// In case the re-alloc returns null, use a local variable to avoid
// losing the current block of memory.
void *tmp = OICRealloc(list->data,
list->data = (void **) tmp;
memset(list->data + list->capacity, 0,
(new_capacity - list->capacity) * sizeof(list->data[0]));
- list->capacity = new_capacity;
+ list->capacity = (uint32_t)new_capacity;
}
list->data[list->length] = data;