Skip to content
Snippets Groups Projects
Commit b49bb2d7 authored by Rob Davies's avatar Rob Davies
Browse files

Avoid unintended macro expansion in KSORT_INIT_GENERIC

Netbsd's libc #defines uint16_t to __uint16_t (and similarly for
other stdint types).  This was expanded by KSORT_INIT_GENERIC()
resulting in functions being defined with slightly different names
compared to the ones produced by using KSORT_INIT() directly.
The names also no longer matched the results of expanding
ks_mergesort() and friends.

Fix this by adjusting where the underscore gets pasted into the
names.  This means KSORT_INIT_GENERIC can use the argument
in a token pasting operation, which prevents it from being
expanded.
parent 738c16d2
Branches
Tags
No related merge requests found
......@@ -82,8 +82,9 @@ typedef struct {
#define KSORT_SWAP(type_t, a, b) { register type_t t=(a); (a)=(b); (b)=t; }
#define KSORT_INIT(name, type_t, __sort_lt) \
void ks_mergesort_##name(size_t n, type_t array[], type_t temp[]) \
#define KSORT_INIT(name, type_t, __sort_lt) KSORT_INIT_(_ ## name, type_t, __sort_lt)
#define KSORT_INIT_(name, type_t, __sort_lt) \
void ks_mergesort##name(size_t n, type_t array[], type_t temp[]) \
{ \
type_t *a2[2], *a, *b; \
int curr, shift; \
......@@ -131,7 +132,7 @@ typedef struct {
} \
if (temp == 0) free(a2[1]); \
} \
void ks_heapadjust_##name(size_t i, size_t n, type_t l[]) \
void ks_heapadjust##name(size_t i, size_t n, type_t l[]) \
{ \
size_t k = i; \
type_t tmp = l[i]; \
......@@ -142,21 +143,21 @@ typedef struct {
} \
l[i] = tmp; \
} \
void ks_heapmake_##name(size_t lsize, type_t l[]) \
void ks_heapmake##name(size_t lsize, type_t l[]) \
{ \
size_t i; \
for (i = (lsize >> 1) - 1; i != (size_t)(-1); --i) \
ks_heapadjust_##name(i, lsize, l); \
ks_heapadjust##name(i, lsize, l); \
} \
void ks_heapsort_##name(size_t lsize, type_t l[]) \
void ks_heapsort##name(size_t lsize, type_t l[]) \
{ \
size_t i; \
for (i = lsize - 1; i > 0; --i) { \
type_t tmp; \
tmp = *l; *l = l[i]; l[i] = tmp; ks_heapadjust_##name(0, i, l); \
tmp = *l; *l = l[i]; l[i] = tmp; ks_heapadjust##name(0, i, l); \
} \
} \
static inline void __ks_insertsort_##name(type_t *s, type_t *t) \
static inline void __ks_insertsort##name(type_t *s, type_t *t) \
{ \
type_t *i, *j, swap_tmp; \
for (i = s + 1; i < t; ++i) \
......@@ -164,7 +165,7 @@ typedef struct {
swap_tmp = *j; *j = *(j-1); *(j-1) = swap_tmp; \
} \
} \
void ks_combsort_##name(size_t n, type_t a[]) \
void ks_combsort##name(size_t n, type_t a[]) \
{ \
const double shrink_factor = 1.2473309501039786540366528676643; \
int do_swap; \
......@@ -184,9 +185,9 @@ typedef struct {
} \
} \
} while (do_swap || gap > 2); \
if (gap != 1) __ks_insertsort_##name(a, a + n); \
if (gap != 1) __ks_insertsort##name(a, a + n); \
} \
void ks_introsort_##name(size_t n, type_t a[]) \
void ks_introsort##name(size_t n, type_t a[]) \
{ \
int d; \
ks_isort_stack_t *top, *stack; \
......@@ -204,7 +205,7 @@ typedef struct {
while (1) { \
if (s < t) { \
if (--d == 0) { \
ks_combsort_##name(t - s + 1, s); \
ks_combsort##name(t - s + 1, s); \
t = s; \
continue; \
} \
......@@ -231,7 +232,7 @@ typedef struct {
} else { \
if (top == stack) { \
free(stack); \
__ks_insertsort_##name(a, a+n); \
__ks_insertsort##name(a, a+n); \
return; \
} else { --top; s = (type_t*)top->left; t = (type_t*)top->right; d = top->depth; } \
} \
......@@ -239,7 +240,7 @@ typedef struct {
} \
/* This function is adapted from: http://ndevilla.free.fr/median/ */ \
/* 0 <= kk < n */ \
type_t ks_ksmall_##name(size_t n, type_t arr[], size_t kk) \
type_t ks_ksmall##name(size_t n, type_t arr[], size_t kk) \
{ \
type_t *low, *high, *k, *ll, *hh, *mid; \
low = arr; high = arr + n - 1; k = arr + kk; \
......@@ -266,7 +267,7 @@ typedef struct {
if (hh >= k) high = hh - 1; \
} \
} \
void ks_shuffle_##name(size_t n, type_t a[]) \
void ks_shuffle##name(size_t n, type_t a[]) \
{ \
int i, j; \
for (i = n; i > 1; --i) { \
......@@ -290,7 +291,7 @@ typedef struct {
typedef const char *ksstr_t;
#define KSORT_INIT_GENERIC(type_t) KSORT_INIT(type_t, type_t, ks_lt_generic)
#define KSORT_INIT_GENERIC(type_t) KSORT_INIT_(_ ## type_t, type_t, ks_lt_generic)
#define KSORT_INIT_STR KSORT_INIT(str, ksstr_t, ks_lt_str)
#ifdef __cplusplus
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment