Changed arraylist implementation to avoid floating point.
authorJon A. Cruz <jonc@osg.samsung.com>
Fri, 4 Sep 2015 18:33:53 +0000 (11:33 -0700)
committerJon A. Cruz <jonc@osg.samsung.com>
Wed, 9 Sep 2015 05:13:17 +0000 (05:13 +0000)
Updated calculation of u_arraylist growth to no longer use
floating point. This helps for limited platforms that might
not have native FP support, such as some Arduino boards.

Change-Id: I7d8923786ac31cab8eb3004c8a956d36d884ec29
Signed-off-by: Jon A. Cruz <jonc@osg.samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2382
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
Reviewed-by: Jaehong Jo <jaehong.jo@samsung.com>
Reviewed-by: jihwan seo <jihwan.seo@samsung.com>
resource/csdk/connectivity/common/src/uarraylist.c

index 8b175cd..b6aef02 100644 (file)
@@ -126,7 +126,6 @@ void *u_arraylist_get(const u_arraylist_t *list, uint32_t index)
 
 bool u_arraylist_add(u_arraylist_t *list, void *data)
 {
-    static const double GROWTH_FACTOR = 1.5;
     if (!list)
     {
         return false;
@@ -134,7 +133,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 +149,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;