1 #include "git-compat-util.h"
4 * A merge sort implementation, simplified from the qsort implementation
5 * by Mike Haertel, which is a part of the GNU C Library.
8 static void msort_with_tmp(void *b
, size_t n
, size_t s
,
9 int (*cmp
)(const void *, const void *),
22 b2
= (char *)b
+ (n1
* s
);
24 msort_with_tmp(b1
, n1
, s
, cmp
, t
);
25 msort_with_tmp(b2
, n2
, s
, cmp
, t
);
29 while (n1
> 0 && n2
> 0) {
30 if (cmp(b1
, b2
) <= 0) {
43 memcpy(tmp
, b1
, n1
* s
);
44 memcpy(b
, t
, (n
- n2
) * s
);
47 void git_stable_qsort(void *b
, size_t n
, size_t s
,
48 int (*cmp
)(const void *, const void *))
50 const size_t size
= st_mult(n
, s
);
53 if (size
< sizeof(buf
)) {
54 /* The temporary array fits on the small on-stack buffer. */
55 msort_with_tmp(b
, n
, s
, cmp
, buf
);
57 /* It's somewhat large, so malloc it. */
58 char *tmp
= xmalloc(size
);
59 msort_with_tmp(b
, n
, s
, cmp
, tmp
);