]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
make distribution_cmp private and change the return value type
authorSvetlana Shmidt <sshmidt@google.com>
Mon, 7 Sep 2020 14:23:30 +0000 (14:23 +0000)
committerSvetlana Shmidt <sshmidt@google.com>
Mon, 7 Sep 2020 14:23:30 +0000 (14:23 +0000)
src/daemon/distribution.c
src/daemon/distribution.h

index c402431e89e1f3921da419f8455ac8c603014764..53cdb04db3f6fd742c448d387fc9ae63d9850fc8 100644 (file)
@@ -344,46 +344,57 @@ static int compare_longint(uint64_t a, uint64_t b) {
   return a == b ? 0 : a < b ? -1 : 1;
 }
 
-int distribution_cmp(distribution_t *d1, distribution_t *d2) {
+/** return a pointer to array int[2].
+ * The first value is error code or 0 if succeed.
+ * The second value is result of comparison
+ */
+static int *distribution_cmp(distribution_t *d1, distribution_t *d2) {
+  int *comparison = calloc(2, sizeof(*comparison));
+  if (comparison == NULL) {
+    return NULL;
+  }
   if (d1 == NULL || d2 == NULL) {
-    return EINVAL;
+    comparison[0] = EINVAL;
+    return comparison;
   }
-  if (d1-> num_buckets != d2->num_buckets) {
-    return EINVAL;
+  if (d1->num_buckets != d2->num_buckets) {
+    comparison[0] = EINVAL;
+    return comparison;
   }
   for (size_t i = 0; i < tree_size(d1->num_buckets); i++) {
     if (d1->tree[i].maximum != d2->tree[i].maximum) { // will it work with doubles?
-      return EINVAL;
+      comparison[0] = EINVAL;
+      return comparison;
     }
   }
 
-  pthread_mutex_lock(&d1->mutex);
-  pthread_mutex_unlock(&d2->mutex);
-  int comparison = compare_longint(d1->tree[0].bucket_counter, d2->tree[0].bucket_counter);
+  int res = compare_longint(d1->tree[0].bucket_counter, d2->tree[0].bucket_counter);
   for (size_t i = 1; i < tree_size(d1->num_buckets); i++) {
-    if (comparison != compare_longint(d1->tree[i].bucket_counter, d2->tree[i].bucket_counter)) {
-      comparison = ERANGE;
+    if (res != compare_longint(d1->tree[i].bucket_counter, d2->tree[i].bucket_counter)) {
+      comparison[0] = ERANGE;
       break;
     }
   }
-  pthread_mutex_unlock(&d2->mutex);
-  pthread_mutex_unlock(&d1->mutex);
+  comparison[1] = res;
   return comparison;
 }
 
 /* TODO(bkjg): add tests for this function */
 int distribution_sub(distribution_t *d1, distribution_t *d2) {
-
-  int cmp_status = distribution_cmp(d1, d2);
-  if (cmp_status != 1 && cmp_status != 0) { // i.e. d1 < d2 or can't compare
-    if (cmp_status == -1)
-      cmp_status = ERANGE;
-    return cmp_status;
-  }
-
   pthread_mutex_lock(&d1->mutex);
   pthread_mutex_lock(&d2->mutex);
 
+  int *cmp_status = distribution_cmp(d1, d2);
+  if (cmp_status[0] != 0 || cmp_status[1] == -1) { // i.e. d1 < d2 or can't compare
+    int status = cmp_status[0];
+    if (cmp_status[1] == -1)
+      status = ERANGE;
+    pthread_mutex_unlock(&d2->mutex);
+    pthread_mutex_unlock(&d1->mutex);
+    free(cmp_status);
+    return status;
+  }
+
   d1->total_sum -= d2->total_sum;
   d1->total_square_sum -= d2->total_square_sum;
   for (size_t i = 0; i < tree_size(d1->num_buckets); i++) {
index 37b6619bf1da5eb8f5202e1180338a6e3d51a633..28d6d1454b01a216fe29a204ba35292f719a9781 100644 (file)
@@ -118,14 +118,6 @@ double distribution_squared_deviation_sum(distribution_t *dist);
 
 void destroy_buckets_array(buckets_array_t buckets_array);
 
-/** return EINVAL if bucket boundaries are not the same or NULL pointer received
- *  return -1 if d1 < d2
- *  return 0  if d1 == d2
- *  return 1  if d1 > d2
- *  return ERANGE if distributions can't be compared
- */
-int distribution_cmp(distribution_t *d1, distribution_t *d2);
-
 /* TODO(bkjg): add description */
 int distribution_sub(distribution_t *d1, distribution_t *d2);
 #endif // COLLECTD_DISTRIBUTION_H
\ No newline at end of file