mirror of
https://github.com/facebook/zstd.git
synced 2025-07-25 01:42:11 +03:00
add support for C11 Annex K qsort_s()
standard defined re-entrant variant of qsort(). Unfortunately, Annex K is optional.
This commit is contained in:
2
.github/workflows/dev-short-tests.yml
vendored
2
.github/workflows/dev-short-tests.yml
vendored
@ -693,7 +693,7 @@ jobs:
|
|||||||
- name: Install musl-tools
|
- name: Install musl-tools
|
||||||
run: |
|
run: |
|
||||||
sudo apt install -y musl-tools
|
sudo apt install -y musl-tools
|
||||||
- name: Compile the project with musl-gcc
|
- name: Compile with musl-gcc and test-zstd
|
||||||
run: |
|
run: |
|
||||||
CC=musl-gcc CFLAGS="-Werror -O3" CPPFLAGS=-DZDICT_QSORT=ZDICT_QSORT_C90 make -j -C tests test-zstd V=1
|
CC=musl-gcc CFLAGS="-Werror -O3" CPPFLAGS=-DZDICT_QSORT=ZDICT_QSORT_C90 make -j -C tests test-zstd V=1
|
||||||
|
|
||||||
|
@ -29,6 +29,8 @@
|
|||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define __STDC_WANT_LIB_EXT1__ 1 /* request C11 Annex K, which includes qsort_s() */
|
||||||
|
|
||||||
#include <stdio.h> /* fprintf */
|
#include <stdio.h> /* fprintf */
|
||||||
#include <stdlib.h> /* malloc, free, qsort_r */
|
#include <stdlib.h> /* malloc, free, qsort_r */
|
||||||
|
|
||||||
@ -68,8 +70,9 @@
|
|||||||
#define ZDICT_QSORT_C90 ZDICT_QSORT_MIN
|
#define ZDICT_QSORT_C90 ZDICT_QSORT_MIN
|
||||||
#define ZDICT_QSORT_GNU 1
|
#define ZDICT_QSORT_GNU 1
|
||||||
#define ZDICT_QSORT_APPLE 2
|
#define ZDICT_QSORT_APPLE 2
|
||||||
#define ZDICT_QSORT_MSVC ZDICT_QSORT_MAX
|
#define ZDICT_QSORT_MSVC 3
|
||||||
#define ZDICT_QSORT_MAX 3
|
#define ZDICT_QSORT_C11 ZDICT_QSORT_MAX
|
||||||
|
#define ZDICT_QSORT_MAX 4
|
||||||
|
|
||||||
#ifndef ZDICT_QSORT
|
#ifndef ZDICT_QSORT
|
||||||
# if defined(__APPLE__)
|
# if defined(__APPLE__)
|
||||||
@ -77,7 +80,9 @@
|
|||||||
# elif defined(_GNU_SOURCE)
|
# elif defined(_GNU_SOURCE)
|
||||||
# define ZDICT_QSORT ZDICT_QSORT_GNU /* uses qsort_r() */
|
# define ZDICT_QSORT ZDICT_QSORT_GNU /* uses qsort_r() */
|
||||||
# elif defined(_WIN32) && defined(_MSC_VER)
|
# elif defined(_WIN32) && defined(_MSC_VER)
|
||||||
# define ZDICT_QSORT ZDICT_QSORT_MSVC /* uses qsort_s() */
|
# define ZDICT_QSORT ZDICT_QSORT_MSVC /* uses qsort_s() with a different order for parameters */
|
||||||
|
# elif defined(STDC_LIB_EXT1) && (STDC_LIB_EXT1 > 0) /* C11 Annex K */
|
||||||
|
# define ZDICT_QSORT ZDICT_QSORT_C11 /* uses qsort_s() */
|
||||||
# else
|
# else
|
||||||
# define ZDICT_QSORT ZDICT_QSORT_C90 /* uses standard qsort() which is not re-entrant (requires global variable) */
|
# define ZDICT_QSORT ZDICT_QSORT_C90 /* uses standard qsort() which is not re-entrant (requires global variable) */
|
||||||
# endif
|
# endif
|
||||||
@ -204,7 +209,7 @@ static U32 *COVER_map_at(COVER_map_t *map, U32 key) {
|
|||||||
*/
|
*/
|
||||||
static void COVER_map_remove(COVER_map_t *map, U32 key) {
|
static void COVER_map_remove(COVER_map_t *map, U32 key) {
|
||||||
U32 i = COVER_map_index(map, key);
|
U32 i = COVER_map_index(map, key);
|
||||||
COVER_map_pair_t *del = &map->data[i];
|
COVER_map_pair_t* del = &map->data[i];
|
||||||
U32 shift = 1;
|
U32 shift = 1;
|
||||||
if (del->value == MAP_EMPTY_VALUE) {
|
if (del->value == MAP_EMPTY_VALUE) {
|
||||||
return;
|
return;
|
||||||
@ -307,7 +312,7 @@ static int COVER_cmp8(COVER_ctx_t *ctx, const void *lp, const void *rp) {
|
|||||||
*/
|
*/
|
||||||
#if (ZDICT_QSORT == ZDICT_QSORT_MSVC) || (ZDICT_QSORT == ZDICT_QSORT_APPLE)
|
#if (ZDICT_QSORT == ZDICT_QSORT_MSVC) || (ZDICT_QSORT == ZDICT_QSORT_APPLE)
|
||||||
static int WIN_CDECL COVER_strict_cmp(void* g_coverCtx, const void* lp, const void* rp) {
|
static int WIN_CDECL COVER_strict_cmp(void* g_coverCtx, const void* lp, const void* rp) {
|
||||||
#elif (ZDICT_QSORT == ZDICT_QSORT_GNU)
|
#elif (ZDICT_QSORT == ZDICT_QSORT_GNU) || (ZDICT_QSORT == ZDICT_QSORT_C11)
|
||||||
static int COVER_strict_cmp(const void *lp, const void *rp, void *g_coverCtx) {
|
static int COVER_strict_cmp(const void *lp, const void *rp, void *g_coverCtx) {
|
||||||
#else /* C90 fallback.*/
|
#else /* C90 fallback.*/
|
||||||
static int COVER_strict_cmp(const void *lp, const void *rp) {
|
static int COVER_strict_cmp(const void *lp, const void *rp) {
|
||||||
@ -323,7 +328,7 @@ static int COVER_strict_cmp(const void *lp, const void *rp) {
|
|||||||
*/
|
*/
|
||||||
#if (ZDICT_QSORT == ZDICT_QSORT_MSVC) || (ZDICT_QSORT == ZDICT_QSORT_APPLE)
|
#if (ZDICT_QSORT == ZDICT_QSORT_MSVC) || (ZDICT_QSORT == ZDICT_QSORT_APPLE)
|
||||||
static int WIN_CDECL COVER_strict_cmp8(void* g_coverCtx, const void* lp, const void* rp) {
|
static int WIN_CDECL COVER_strict_cmp8(void* g_coverCtx, const void* lp, const void* rp) {
|
||||||
#elif (ZDICT_QSORT == ZDICT_QSORT_GNU)
|
#elif (ZDICT_QSORT == ZDICT_QSORT_GNU) || (ZDICT_QSORT == ZDICT_QSORT_C11)
|
||||||
static int COVER_strict_cmp8(const void *lp, const void *rp, void *g_coverCtx) {
|
static int COVER_strict_cmp8(const void *lp, const void *rp, void *g_coverCtx) {
|
||||||
#else /* C90 fallback.*/
|
#else /* C90 fallback.*/
|
||||||
static int COVER_strict_cmp8(const void *lp, const void *rp) {
|
static int COVER_strict_cmp8(const void *lp, const void *rp) {
|
||||||
@ -355,6 +360,10 @@ static void stableSort(COVER_ctx_t *ctx)
|
|||||||
qsort_s(ctx->suffix, ctx->suffixSize, sizeof(U32),
|
qsort_s(ctx->suffix, ctx->suffixSize, sizeof(U32),
|
||||||
(ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp),
|
(ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp),
|
||||||
ctx);
|
ctx);
|
||||||
|
#elif (ZDICT_QSORT == ZDICT_QSORT_C11)
|
||||||
|
qsort_s(ctx->suffix, ctx->suffixSize, sizeof(U32),
|
||||||
|
(ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp),
|
||||||
|
ctx);
|
||||||
#elif defined(__OpenBSD__)
|
#elif defined(__OpenBSD__)
|
||||||
/* On OpenBSD, qsort() is not guaranteed to be stable, their mergesort() is.
|
/* On OpenBSD, qsort() is not guaranteed to be stable, their mergesort() is.
|
||||||
* Note(@cyan): qsort() is never guaranteed to be stable,
|
* Note(@cyan): qsort() is never guaranteed to be stable,
|
||||||
|
Reference in New Issue
Block a user