radixsort.c.patch   [plain text]


--- radixsort.c.orig	2010-06-21 14:05:02.000000000 -0700
+++ radixsort.c	2010-06-21 14:26:55.000000000 -0700
@@ -53,6 +53,7 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
 #include <stdlib.h>
 #include <stddef.h>
 #include <errno.h>
+#include <pthread.h>
 
 typedef struct {
 	const u_char **sa;
@@ -60,11 +61,17 @@ typedef struct {
 } stack;
 
 static inline void simplesort
-(const u_char **, int, int, const u_char *, u_int);
+(const u_char **, int, int, const u_char *, u_int) __attribute__((always_inline));
 static void r_sort_a(const u_char **, int, int, const u_char *, u_int);
 static void r_sort_b(const u_char **, const u_char **, int, int,
     const u_char *, u_int);
 
+static int *r_sort_a_count;
+static int *r_sort_b_count;
+
+static void r_sort_count_allocate(void);
+static pthread_once_t r_sort_count_control = PTHREAD_ONCE_INIT;
+
 #define	THRESHOLD	20		/* Divert to simplesort(). */
 #define	SIZE		512		/* Default stack size. */
 
@@ -124,6 +131,12 @@ sradixsort(a, n, tab, endch)
 	return (0);
 }
 
+static void r_sort_count_allocate(void)
+{
+	r_sort_a_count = calloc(256, sizeof(int));
+	r_sort_b_count = calloc(256, sizeof(int));
+}
+
 #define empty(s)	(s >= sp)
 #define pop(a, n, i)	a = (--sp)->sa, n = sp->sn, i = sp->si
 #define push(a, n, i)	sp->sa = a, sp->sn = n, (sp++)->si = i
@@ -137,13 +150,19 @@ r_sort_a(a, n, i, tr, endch)
 	const u_char *tr;
 	u_int endch;
 {
-	static int count[256], nc, bmin;
+	static int *count, nc, bmin;
 	int c;
 	const u_char **ak, *r;
 	stack s[SIZE], *sp, *sp0, *sp1, temp;
 	int *cp, bigc;
 	const u_char **an, *t, **aj, **top[256];
 
+	if (pthread_once(&r_sort_count_control, r_sort_count_allocate)) {
+		return;
+	}
+
+	count = r_sort_a_count;
+
 	/* Set up stack. */
 	sp = s;
 	push(a, n, i);
@@ -239,13 +258,19 @@ r_sort_b(a, ta, n, i, tr, endch)
 	const u_char *tr;
 	u_int endch;
 {
-	static int count[256], nc, bmin;
+	static int *count, nc, bmin;
 	int c;
 	const u_char **ak, **ai;
 	stack s[512], *sp, *sp0, *sp1, temp;
 	const u_char **top[256];
 	int *cp, bigc;
 
+	if (pthread_once(&r_sort_count_control, r_sort_count_allocate)) {
+		return;
+	}
+
+	count = r_sort_b_count;
+
 	sp = s;
 	push(a, n, i);
 	while (!empty(s)) {