[ccan] [PATCH 3/3] memfuncs: Implement memrchr()

David Gibson david at gibson.dropbear.id.au
Wed Oct 22 07:52:43 AEDT 2014


On Tue, Oct 21, 2014 at 11:18:00AM +1030, Paul 'Rusty' Russell wrote:
> Rusty Russell <rusty at rustcorp.com.au> writes:
> > David Gibson <david at gibson.dropbear.id.au> writes:
> >> On Sun, Oct 19, 2014 at 05:00:45PM +0300, Ran Benita wrote:
> >>> On Sun, Oct 19, 2014 at 02:07:46PM +0200, David Gibson wrote:
> >>> > The memrchr() function, which works like memchr(), but searches from the
> >>> > back of the region to the front is implemented in the GNU C library, but
> >>> > isn't standard.
> >>> > 
> >>> > This patch adds an implementation of the function to the memfuncs module,
> >>> > when it's not available in the system C library.
> >>> > 
> >>> > Signed-off-by: David Gibson <david at gibson.dropbear.id.au>
> >>> > ---
> >>> >  ccan/memfuncs/memfuncs.c          | 14 ++++++++++++++
> >>> >  ccan/memfuncs/memfuncs.h          |  4 ++++
> >>> >  ccan/memfuncs/test/run.c          | 16 +++++++++++++++-
> >>> >  tools/configurator/configurator.c |  6 ++++++
> >>> >  4 files changed, 39 insertions(+), 1 deletion(-)
> >>> > 
> >>> > diff --git a/ccan/memfuncs/memfuncs.c b/ccan/memfuncs/memfuncs.c
> >>> > index c80b862..2674f41 100644
> >>> > --- a/ccan/memfuncs/memfuncs.c
> >>> > +++ b/ccan/memfuncs/memfuncs.c
> >>> > @@ -25,3 +25,17 @@ void *memmem(const void *haystack, size_t haystacklen,
> >>> >  	return NULL;
> >>> >  }
> >>> >  #endif
> >>> > +
> >>> > +#if !HAVE_MEMRCHR
> >>> > +void *memrchr(const void *s, int c, size_t n)
> >>> > +{
> >>> > +	unsigned char *p = (unsigned char *)s;
> >>> > +	size_t i;
> >>> 
> >>> This needs to be ssize_t, otherwise the loop doesn't end.
> >>
> >> Good point.  More importantly, the fact that my testcases did
> >> terminate indicates that they're not actually testing the local
> >> implementations :(.
> >
> > Yes, that's weird.  ccanlint's "tests_pass_without_features" *should*
> > have caught this.
> >
> > Let me check...
> > Rusty.
> 
> Ah, fascinating.  You got the builtin memrchr because your run.c
> included the header only, not the .c file.
> 
> You could rename run.c to api.c, or "#include <ccan/memfuncs/memfuncs.c>"
> instead of "#include <ccan/memfuncs/memfuncs.h>".

Hmm.. so I tried both of those, and it still doesn't fail..

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.ozlabs.org/pipermail/ccan/attachments/20141021/57932af4/attachment.sig>


More information about the ccan mailing list