Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit eadd64f

Browse files
enh-googleGerrit Code Review
authored andcommitted
Merge "Add POSIX qsort_r()." into main
2 parents 8232cb9 + 5bae572 commit eadd64f

File tree

6 files changed

+50
-1
lines changed

6 files changed

+50
-1
lines changed

docs/status.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ list of POSIX functions implemented by glibc but not by bionic.
5555

5656
Current libc symbols: https://android.googlesource.com/platform/bionic/+/main/libc/libc.map.txt
5757

58+
New libc functions in API level 36:
59+
* `qsort_r` (new POSIX addition).
60+
5861
New libc functions in V (API level 35):
5962
* New `android_crash_detail_register`, `android_crash_detail_unregister`,
6063
`android_crash_detail_replace_name`, and `android_crash_detail_replace_data`

libc/Android.bp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ cc_library_static {
389389
"upstream-freebsd/lib/libc/stdlib/hdestroy_r.c",
390390
"upstream-freebsd/lib/libc/stdlib/hsearch_r.c",
391391
"upstream-freebsd/lib/libc/stdlib/qsort.c",
392+
"upstream-freebsd/lib/libc/stdlib/qsort_r.c",
392393
"upstream-freebsd/lib/libc/stdlib/quick_exit.c",
393394
"upstream-freebsd/lib/libc/string/wcpcpy.c",
394395
"upstream-freebsd/lib/libc/string/wcpncpy.c",

libc/include/stdlib.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,20 @@ int system(const char* _Nonnull __command);
114114
*/
115115
__wur void* _Nullable bsearch(const void* _Nonnull __key, const void* _Nullable __base, size_t __nmemb, size_t __size, int (* _Nonnull __comparator)(const void* _Nonnull __lhs, const void* _Nonnull __rhs));
116116

117-
void qsort(void* _Nullable __base, size_t __nmemb, size_t __size, int (* _Nonnull __comparator)(const void* _Nullable __lhs, const void* _Nullable __rhs));
117+
/**
118+
* [qsort(3)](http://man7.org/linux/man-pages/man3/qsort.3.html) sorts an array
119+
* of n elements each of the given size, using the given comparator.
120+
*/
121+
void qsort(void* _Nullable __array, size_t __n, size_t __size, int (* _Nonnull __comparator)(const void* _Nullable __lhs, const void* _Nullable __rhs));
122+
123+
/**
124+
* [qsort_r(3)](http://man7.org/linux/man-pages/man3/qsort_r.3.html) sorts an
125+
* array of n elements each of the given size, using the given comparator,
126+
* and passing the given context argument to the comparator.
127+
*
128+
* Available since API level 36.
129+
*/
130+
void qsort_r(void* _Nullable __array, size_t __n, size_t __size, int (* _Nonnull __comparator)(const void* _Nullable __lhs, const void* _Nullable __rhs, void* _Nullable __context), void* _Nullable __context) __INTRODUCED_IN(36);
118131

119132
uint32_t arc4random(void);
120133
uint32_t arc4random_uniform(uint32_t __upper_bound);

libc/libc.map.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ LIBC {
844844
pwrite;
845845
pwrite64;
846846
qsort;
847+
qsort_r; # introduced=36
847848
quick_exit;
848849
raise;
849850
rand;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
* This file is in the public domain. Originally written by Garrett
3+
* A. Wollman.
4+
*/
5+
#define I_AM_QSORT_R
6+
#include "qsort.c"

tests/stdlib_test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,31 @@ TEST(stdlib, qsort) {
431431
ASSERT_STREQ("charlie", entries[2].name);
432432
}
433433

434+
TEST(stdlib, qsort_r) {
435+
struct s {
436+
char name[16];
437+
static int comparator(const void* lhs, const void* rhs, void* context) {
438+
int* count_p = reinterpret_cast<int*>(context);
439+
*count_p += 1;
440+
return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
441+
}
442+
};
443+
s entries[3];
444+
strcpy(entries[0].name, "charlie");
445+
strcpy(entries[1].name, "bravo");
446+
strcpy(entries[2].name, "alpha");
447+
448+
int count;
449+
void* context = &count;
450+
451+
count = 0;
452+
qsort_r(entries, 3, sizeof(s), s::comparator, context);
453+
ASSERT_STREQ("alpha", entries[0].name);
454+
ASSERT_STREQ("bravo", entries[1].name);
455+
ASSERT_STREQ("charlie", entries[2].name);
456+
ASSERT_EQ(count, 3);
457+
}
458+
434459
static void* TestBug57421_child(void* arg) {
435460
pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
436461
pthread_join(main_thread, nullptr);

0 commit comments

Comments
 (0)