sync: refactor sync_fence_info

Split the sync_fence_info implementation into multiple functions. This
clarifies the logic, and allows the parts to be reused in the upcoming
sync_file_info function.

Test: sync-unit-tests on bullhead
Change-Id: I0ea37067dddf41b831670f08eb99e0b7fd52adce
diff --git a/libsync/sync.c b/libsync/sync.c
index 0e9ad43..6680f11 100644
--- a/libsync/sync.c
+++ b/libsync/sync.c
@@ -98,13 +98,11 @@
     return data.fence;
 }
 
-struct sync_fence_info_data *sync_fence_info(int fd)
+static struct sync_fence_info_data *legacy_sync_fence_info(int fd)
 {
     struct sync_fence_info_data *legacy_info;
     struct sync_pt_info *legacy_pt_info;
-    struct sync_file_info *info;
-    struct sync_fence_info *fence_info;
-    int err, num_fences, i;
+    int err;
 
     legacy_info = malloc(4096);
     if (legacy_info == NULL)
@@ -112,46 +110,57 @@
 
     legacy_info->len = 4096;
     err = ioctl(fd, SYNC_IOC_LEGACY_FENCE_INFO, legacy_info);
-    if (err < 0 && errno != ENOTTY) {
+    if (err < 0) {
         free(legacy_info);
         return NULL;
-    } else if (err == 0) {
-        return legacy_info;
     }
+    return legacy_info;
+}
 
-    info = calloc(1, sizeof(*info));
-    if (info == NULL)
-        goto free;
+static struct sync_file_info *modern_sync_file_info(int fd)
+{
+    struct sync_file_info local_info;
+    struct sync_file_info *info;
+    int err;
+
+    memset(&local_info, 0, sizeof(local_info));
+    err = ioctl(fd, SYNC_IOC_FILE_INFO, &local_info);
+    if (err < 0)
+        return NULL;
+
+    info = calloc(1, sizeof(struct sync_file_info) +
+                  local_info.num_fences * sizeof(struct sync_fence_info));
+    if (!info)
+        return NULL;
+    info->sync_fence_info = (__u64)(uintptr_t)(info + 1);
 
     err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
-    if (err < 0)
-        goto free;
-
-    num_fences = info->num_fences;
-
-    if (num_fences) {
-        info->flags = 0;
-        info->num_fences = num_fences;
-        info->sync_fence_info = (uint64_t) calloc(num_fences,
-                                        sizeof(struct sync_fence_info));
-        if ((void *)info->sync_fence_info == NULL)
-            goto free;
-
-        err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
-        if (err < 0) {
-            free((void *)info->sync_fence_info);
-            goto free;
-        }
+    if (err < 0) {
+        free(info);
+        return NULL;
     }
 
+    return info;
+}
+
+static struct sync_fence_info_data *sync_file_info_to_legacy_fence_info(
+    const struct sync_file_info *info)
+{
+    struct sync_fence_info_data *legacy_info;
+    struct sync_pt_info *legacy_pt_info;
+    const struct sync_fence_info *fence_info = sync_get_fence_info(info);
+    const uint32_t num_fences = info->num_fences;
+
+    legacy_info = malloc(4096);
+    if (legacy_info == NULL)
+        return NULL;
     legacy_info->len = sizeof(*legacy_info) +
                         num_fences * sizeof(struct sync_pt_info);
     strlcpy(legacy_info->name, info->name, sizeof(legacy_info->name));
     legacy_info->status = info->status;
 
     legacy_pt_info = (struct sync_pt_info *)legacy_info->pt_info;
-    fence_info = (struct sync_fence_info *)info->sync_fence_info;
-    for (i = 0 ; i < num_fences ; i++) {
+    for (uint32_t i = 0; i < num_fences; i++) {
         legacy_pt_info[i].len = sizeof(*legacy_pt_info);
         strlcpy(legacy_pt_info[i].obj_name, fence_info[i].obj_name,
                 sizeof(legacy_pt_info->obj_name));
@@ -161,14 +170,24 @@
         legacy_pt_info[i].timestamp_ns = fence_info[i].timestamp_ns;
     }
 
-    free((void *)info->sync_fence_info);
-    free(info);
     return legacy_info;
+}
 
-free:
-    free(legacy_info);
-    free(info);
-    return NULL;
+struct sync_fence_info_data *sync_fence_info(int fd)
+{
+    struct sync_fence_info_data *legacy_info;
+
+    legacy_info = legacy_sync_fence_info(fd);
+    if (legacy_info || errno != ENOTTY)
+        return legacy_info;
+
+    struct sync_file_info* file_info;
+    file_info = modern_sync_file_info(fd);
+    if (!file_info)
+        return NULL;
+    legacy_info = sync_file_info_to_legacy_fence_info(file_info);
+    sync_file_info_free(file_info);
+    return legacy_info;
 }
 
 struct sync_pt_info *sync_pt_info(struct sync_fence_info_data *info,
@@ -190,6 +209,11 @@
     free(info);
 }
 
+void sync_file_info_free(struct sync_file_info *info)
+{
+    free(info);
+}
+
 
 int sw_sync_timeline_create(void)
 {