[libc-commits] [libc] [libc] Refactor qsort code (PR #198781)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Wed May 20 06:52:35 PDT 2026


================
@@ -0,0 +1,150 @@
+
+//===-- A template class for testing reentrant qsort functions --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "test/UnitTest/Test.h"
+
+template <typename QsortFnTy, typename SizeTy>
+class QsortReentrantTest : public LIBC_NAMESPACE::testing::Test {
+private:
+  static int int_compare_count(const void *l, const void *r, void *count_arg) {
+    int li = *static_cast<const int *>(l);
+    int ri = *static_cast<const int *>(r);
+    SizeTy *count = static_cast<SizeTy *>(count_arg);
+    *count = *count + 1;
+    if (li == ri)
+      return 0;
+    else if (li > ri)
+      return 1;
+    else
+      return -1;
+  }
+
+  struct PriorityVal {
+    int priority;
+    int size;
+  };
+
+  static int compare_priority_val(const PriorityVal *l, const PriorityVal *r) {
+    // Subtracting the priorities is unsafe, but it's fine for this test.
+    int priority_diff = l->priority - r->priority;
+    if (priority_diff != 0) {
+      return priority_diff;
+    }
+    if (l->size == r->size) {
+      return 0;
+    } else if (l->size > r->size) {
+      return 1;
+    } else {
+      return -1;
+    }
+  }
+
+  // The following test is intended to mimic the CPP library pattern of having a
+  // comparison function that takes a specific type, which is passed to a
+  // library that then needs to sort an array of that type. The library can't
+  // safely pass the comparison function to qsort because a function that takes
+  // const T* being cast to a function that takes const void* is undefined
+  // behavior. The safer pattern is to pass a type erased comparator that calls
+  // into the typed comparator to qsort_r.
+  template <typename T>
+  static int type_erased_comp(const void *l, const void *r,
+                              void *erased_func_ptr) {
+    typedef int (*TypedComp)(const T *, const T *);
----------------
kaladron wrote:

```suggestion
    using TypedComp = int (*)(const T *, const T *);
```

For idiomatic C++

https://github.com/llvm/llvm-project/pull/198781


More information about the libc-commits mailing list