This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: Ping: [PATCH] Add inline bsearch expansion
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: Roland McGrath <roland at hack dot frob dot com>
- Cc: Andreas Jaeger <aj at suse dot com>, libc-alpha at sourceware dot org
- Date: Fri, 8 Feb 2013 09:55:54 +0100
- Subject: Re: Ping: [PATCH] Add inline bsearch expansion
- References: <20130105125130.GA2893@domone.kolej.mff.cuni.cz><20130105131937.GD26036@sunsite.ms.mff.cuni.cz><20130105152625.GA18344@domone.kolej.mff.cuni.cz><50E9D046.3030108@suse.com><20130106205715.GA3227@domone.kolej.mff.cuni.cz><20130201093632.GA7616@domone.kolej.mff.cuni.cz><20130207205620.675A32C096@topped-with-meat.com>
On Thu, Feb 07, 2013 at 12:56:20PM -0800, Roland McGrath wrote:
> Don't even bother asking for approval if you aren't including a proper
> ChangeLog entry.
>
> Use __compar_fn_t in the declaration like the existing header declaration
> does.
>
> We should avoid duplicating the code (even though clearly this code is
> never going to change). One way to do that would be to put the definition
> into a new file bits/stdlib-bsearch.h that stdlib.h includes under
> __USE_EXTERN_INLINES. Then stdlib/bsearch.c can do:
>
> #include <stdlib.h>
>
> #undef __extern_inline
> #define __extern_inline /* Empty, so we get a normal definition. */
> #include <bits/stdlib-bsearch.h>
>
>
> Thanks,
> Roland
Here is version with changelog entry.
2013-02-08 OndÅej BÃlka <neleai@seznam.cz>
* bits/stdlib-bsearch.h: New file.
* stdlib/bsearch.c: Include bits/stdlib-bsearch.h.
* stdlib/stdlib.h(bsearch): Add inline bsearch.
---
bits/stdlib-bsearch.h | 28 ++++++++++++++++++++++++++++
stdlib/bsearch.c | 34 +++++-----------------------------
stdlib/stdlib.h | 2 ++
3 files changed, 35 insertions(+), 29 deletions(-)
create mode 100644 bits/stdlib-bsearch.h
diff --git a/bits/stdlib-bsearch.h b/bits/stdlib-bsearch.h
new file mode 100644
index 0000000..0663204
--- /dev/null
+++ b/bits/stdlib-bsearch.h
@@ -0,0 +1,28 @@
+#ifdef __USE_EXTERN_INLINES
+__extern_inline
+void *
+bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size,
+ __compar_fn_t __compar)
+{
+ size_t __l, __u, __idx;
+ const void *__p;
+ int __comparison;
+
+ __l = 0;
+ __u = __nmemb;
+ while (__l < __u)
+ {
+ __idx = (__l + __u) / 2;
+ __p = (void *) (((const char *) __base) + (__idx * __size));
+ __comparison = (*__compar) (__key, __p);
+ if (__comparison < 0)
+ __u = __idx;
+ else if (__comparison > 0)
+ __l = __idx + 1;
+ else
+ return (void *) __p;
+ }
+
+ return NULL;
+}
+#endif
diff --git a/stdlib/bsearch.c b/stdlib/bsearch.c
index 55b4f37..04a9d9d 100644
--- a/stdlib/bsearch.c
+++ b/stdlib/bsearch.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991-2013 Free Software Foundation, Inc.
+/* Copyright (C) 2013 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -17,32 +17,8 @@
#include <stdlib.h>
-
-/* Perform a binary search for KEY in BASE which has NMEMB elements
- of SIZE bytes each. The comparisons are done by (*COMPAR)(). */
-void *
-bsearch (const void *key, const void *base, size_t nmemb, size_t size,
- int (*compar) (const void *, const void *))
-{
- size_t l, u, idx;
- const void *p;
- int comparison;
-
- l = 0;
- u = nmemb;
- while (l < u)
- {
- idx = (l + u) / 2;
- p = (void *) (((const char *) base) + (idx * size));
- comparison = (*compar) (key, p);
- if (comparison < 0)
- u = idx;
- else if (comparison > 0)
- l = idx + 1;
- else
- return (void *) p;
- }
-
- return NULL;
-}
+#define __USE_EXTERN_INLINES
+#undef __extern_inline
+#define __extern_inline /* Empty, so we get a normal definition. */
+#include <bits/stdlib-bsearch.h>
libc_hidden_def (bsearch)
diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h
index b49a41c..26a7bc4 100644
--- a/stdlib/stdlib.h
+++ b/stdlib/stdlib.h
@@ -756,6 +756,8 @@ extern void *bsearch (const void *__key, const void *__base,
size_t __nmemb, size_t __size, __compar_fn_t __compar)
__nonnull ((1, 2, 5)) __wur;
+#include <bits/stdlib-bsearch.h>
+
/* Sort NMEMB elements of BASE, of SIZE bytes each,
using COMPAR to perform the comparisons. */
extern void qsort (void *__base, size_t __nmemb, size_t __size,
--
1.7.4.4