Skip to content

Commit cb8fc35

Browse files
committed
zstd: cherry-pick qsort commits
This change reflects cherry-picking of the following upstream commits from facebook/zstd#4312 and facebook/zstd#4336: * ebfa660b8297170fb0742ac8959aa64940292853 * 51b6e79f659f7ba18bf7c4bd2679ff58711aa34d * dcf675886bc138c03c3f157f7464798c4140829f * a9b8fef2e808c227058da4d0f41768c877cfba4c * 4d53e27144b5ad02c6c37c27a998300986a4adaa We need these commits so we can force musl <1.2 to use an appropriate qsort implementation. And the commits seem to shore up the feature detection and squash some bugs. So I'm fine carrying them before we would otherwise get them in 1.5.8.
1 parent aede8bd commit cb8fc35

1 file changed

Lines changed: 50 additions & 19 deletions

File tree

zstd/zstd.c

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47869,11 +47869,13 @@ size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx,
4786947869
/* qsort_r is an extension. */
4787047870
#if defined(__linux) || defined(__linux__) || defined(linux) || defined(__gnu_linux__) || \
4787147871
defined(__CYGWIN__) || defined(__MSYS__)
47872-
#if !defined(_GNU_SOURCE) && !defined(__ANDROID__) /* NDK doesn't ship qsort_r(). */
47873-
#define _GNU_SOURCE
47874-
#endif
47872+
# if !defined(_GNU_SOURCE) && !defined(__ANDROID__) /* NDK doesn't ship qsort_r(). */
47873+
# define _GNU_SOURCE
47874+
# endif
4787547875
#endif
4787647876

47877+
#define __STDC_WANT_LIB_EXT1__ 1 /* request C11 Annex K, which includes qsort_s() */
47878+
4787747879
#include <stdio.h> /* fprintf */
4787847880
#include <stdlib.h> /* malloc, free, qsort_r */
4787947881

@@ -47884,6 +47886,7 @@ size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx,
4788447886
# define ZDICT_STATIC_LINKING_ONLY
4788547887
#endif
4788647888

47889+
/**** skipping file: ../common/debug.h ****/
4788747890
/**** skipping file: ../common/mem.h ****/
4788847891
/**** skipping file: ../common/pool.h ****/
4788947892
/**** skipping file: ../common/threading.h ****/
@@ -48540,6 +48543,32 @@ void COVER_dictSelectionFree(COVER_dictSelection_t selection);
4854048543
#define COVER_MAX_SAMPLES_SIZE (sizeof(size_t) == 8 ? ((unsigned)-1) : ((unsigned)1 GB))
4854148544
#define COVER_DEFAULT_SPLITPOINT 1.0
4854248545

48546+
/**
48547+
* Select the qsort() variant used by cover
48548+
*/
48549+
#define ZDICT_QSORT_MIN 0
48550+
#define ZDICT_QSORT_C90 ZDICT_QSORT_MIN
48551+
#define ZDICT_QSORT_GNU 1
48552+
#define ZDICT_QSORT_APPLE 2
48553+
#define ZDICT_QSORT_MSVC 3
48554+
#define ZDICT_QSORT_C11 ZDICT_QSORT_MAX
48555+
#define ZDICT_QSORT_MAX 4
48556+
48557+
#ifndef ZDICT_QSORT
48558+
# if defined(__APPLE__)
48559+
# define ZDICT_QSORT ZDICT_QSORT_APPLE /* uses qsort_r() with a different order for parameters */
48560+
# elif defined(_GNU_SOURCE)
48561+
# define ZDICT_QSORT ZDICT_QSORT_GNU /* uses qsort_r() */
48562+
# elif defined(_WIN32) && defined(_MSC_VER)
48563+
# define ZDICT_QSORT ZDICT_QSORT_MSVC /* uses qsort_s() with a different order for parameters */
48564+
# elif defined(STDC_LIB_EXT1) && (STDC_LIB_EXT1 > 0) /* C11 Annex K */
48565+
# define ZDICT_QSORT ZDICT_QSORT_C11 /* uses qsort_s() */
48566+
# else
48567+
# define ZDICT_QSORT ZDICT_QSORT_C90 /* uses standard qsort() which is not re-entrant (requires global variable) */
48568+
# endif
48569+
#endif
48570+
48571+
4854348572
/*-*************************************
4854448573
* Console display
4854548574
***************************************/
@@ -48668,7 +48697,7 @@ static U32 *COVER_map_at(COVER_map_t *map, U32 key) {
4866848697
*/
4866948698
static void COVER_map_remove(COVER_map_t *map, U32 key) {
4867048699
U32 i = COVER_map_index(map, key);
48671-
COVER_map_pair_t *del = &map->data[i];
48700+
COVER_map_pair_t* del = &map->data[i];
4867248701
U32 shift = 1;
4867348702
if (del->value == MAP_EMPTY_VALUE) {
4867448703
return;
@@ -48721,8 +48750,8 @@ typedef struct {
4872148750
unsigned d;
4872248751
} COVER_ctx_t;
4872348752

48724-
#if !defined(_GNU_SOURCE) && !defined(__APPLE__) && !defined(_MSC_VER)
48725-
/* C90 only offers qsort() that needs a global context. */
48753+
#if ZDICT_QSORT == ZDICT_QSORT_C90
48754+
/* Use global context for non-reentrant sort functions */
4872648755
static COVER_ctx_t *g_coverCtx = NULL;
4872748756
#endif
4872848757

@@ -48768,9 +48797,9 @@ static int COVER_cmp8(COVER_ctx_t *ctx, const void *lp, const void *rp) {
4876848797
/**
4876948798
* Same as COVER_cmp() except ties are broken by pointer value
4877048799
*/
48771-
#if (defined(_WIN32) && defined(_MSC_VER)) || defined(__APPLE__)
48800+
#if (ZDICT_QSORT == ZDICT_QSORT_MSVC) || (ZDICT_QSORT == ZDICT_QSORT_APPLE)
4877248801
static int WIN_CDECL COVER_strict_cmp(void* g_coverCtx, const void* lp, const void* rp) {
48773-
#elif defined(_GNU_SOURCE)
48802+
#elif (ZDICT_QSORT == ZDICT_QSORT_GNU) || (ZDICT_QSORT == ZDICT_QSORT_C11)
4877448803
static int COVER_strict_cmp(const void *lp, const void *rp, void *g_coverCtx) {
4877548804
#else /* C90 fallback.*/
4877648805
static int COVER_strict_cmp(const void *lp, const void *rp) {
@@ -48784,9 +48813,9 @@ static int COVER_strict_cmp(const void *lp, const void *rp) {
4878448813
/**
4878548814
* Faster version for d <= 8.
4878648815
*/
48787-
#if (defined(_WIN32) && defined(_MSC_VER)) || defined(__APPLE__)
48816+
#if (ZDICT_QSORT == ZDICT_QSORT_MSVC) || (ZDICT_QSORT == ZDICT_QSORT_APPLE)
4878848817
static int WIN_CDECL COVER_strict_cmp8(void* g_coverCtx, const void* lp, const void* rp) {
48789-
#elif defined(_GNU_SOURCE)
48818+
#elif (ZDICT_QSORT == ZDICT_QSORT_GNU) || (ZDICT_QSORT == ZDICT_QSORT_C11)
4879048819
static int COVER_strict_cmp8(const void *lp, const void *rp, void *g_coverCtx) {
4879148820
#else /* C90 fallback.*/
4879248821
static int COVER_strict_cmp8(const void *lp, const void *rp) {
@@ -48803,26 +48832,28 @@ static int COVER_strict_cmp8(const void *lp, const void *rp) {
4880348832
* Hopefully when C11 become the norm, we will be able
4880448833
* to clean it up.
4880548834
*/
48806-
static void stableSort(COVER_ctx_t *ctx) {
48807-
#if defined(__APPLE__)
48835+
static void stableSort(COVER_ctx_t *ctx)
48836+
{
48837+
DEBUG_STATIC_ASSERT(ZDICT_QSORT_MIN <= ZDICT_QSORT && ZDICT_QSORT <= ZDICT_QSORT_MAX);
48838+
#if (ZDICT_QSORT == ZDICT_QSORT_APPLE)
4880848839
qsort_r(ctx->suffix, ctx->suffixSize, sizeof(U32),
4880948840
ctx,
4881048841
(ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp));
48811-
#elif defined(_GNU_SOURCE)
48842+
#elif (ZDICT_QSORT == ZDICT_QSORT_GNU)
4881248843
qsort_r(ctx->suffix, ctx->suffixSize, sizeof(U32),
4881348844
(ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp),
4881448845
ctx);
48815-
#elif defined(_WIN32) && defined(_MSC_VER)
48846+
#elif (ZDICT_QSORT == ZDICT_QSORT_MSVC)
48847+
qsort_s(ctx->suffix, ctx->suffixSize, sizeof(U32),
48848+
(ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp),
48849+
ctx);
48850+
#elif (ZDICT_QSORT == ZDICT_QSORT_C11)
4881648851
qsort_s(ctx->suffix, ctx->suffixSize, sizeof(U32),
4881748852
(ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp),
4881848853
ctx);
48819-
#elif defined(__OpenBSD__)
48820-
g_coverCtx = ctx;
48821-
mergesort(ctx->suffix, ctx->suffixSize, sizeof(U32),
48822-
(ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp));
4882348854
#else /* C90 fallback.*/
4882448855
g_coverCtx = ctx;
48825-
/* TODO(cavalcanti): implement a reentrant qsort() when is not available. */
48856+
/* TODO(cavalcanti): implement a reentrant qsort() when _r is not available. */
4882648857
qsort(ctx->suffix, ctx->suffixSize, sizeof(U32),
4882748858
(ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp));
4882848859
#endif

0 commit comments

Comments
 (0)