replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / src / uarraylist.c
index 17fce9a..b795f10 100644 (file)
@@ -33,7 +33,7 @@
 
 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");
@@ -66,7 +66,7 @@ void u_arraylist_free(u_arraylist_t **list)
     *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))
     {
@@ -74,7 +74,7 @@ void u_arraylist_reserve(u_arraylist_t *list, size_t count)
         if (!tmp)
         {
             OIC_LOG(DEBUG, TAG, "Memory reallocation failed.");
-            // Note that this is considered non-fatal.
+            return false;
         }
         else
         {
@@ -82,6 +82,7 @@ void u_arraylist_reserve(u_arraylist_t *list, size_t count)
             list->capacity = count;
         }
     }
+    return true;
 }
 
 void u_arraylist_shrink_to_fit(u_arraylist_t *list)
@@ -124,9 +125,27 @@ void *u_arraylist_get(const u_arraylist_t *list, uint32_t index)
     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;
@@ -134,7 +153,10 @@ bool u_arraylist_add(u_arraylist_t *list, void *data)
 
     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,
@@ -147,7 +169,7 @@ bool u_arraylist_add(u_arraylist_t *list, void *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;