[ccan] [PATCH 1/3] mem: add mem helper functions

Cody P Schafer dev at codyps.com
Thu Aug 20 10:20:41 AEST 2015


On Wed, Aug 19, 2015 at 5:01 PM, David Gibson
<david at gibson.dropbear.id.au> wrote:
> On Tue, Aug 18, 2015 at 10:54:52PM -0400, Cody P Schafer wrote:
>> On Tue, Aug 18, 2015 at 7:08 PM, David Gibson
>> <david at gibson.dropbear.id.au> wrote:
>> > On Mon, Aug 17, 2015 at 08:33:29PM -0400, Cody P Schafer wrote:
>> >> Signed-off-by: Cody P Schafer <dev at codyps.com>
>> >
>> > Concept looks fine.  A number of minor nits.
>> >
>> >> ---
>> >> +/**
>> >> + * memstarts - determine if @data starts with @prefix
>> >> + * @data: does this begin with @prefix?
>> >> + * @data_len: bytes in @data
>> >> + * @prefix: does @data begin with these bytes?
>> >> + * @prefix_len: bytes in @prefix
>> >> + *
>> >> + * Returns true if @data starts with @prefix, otherwise return false.
>> >> + *
>> >> + * Example:
>> >> + *   if (memstarts(somebytes, bytes_len, otherbytes, otherbytes_len)) {
>> >> + *           printf("somebytes starts with otherbytes!\n");
>> >> + *   }
>> >> + */
>> >> +static inline bool memstarts(void const *data, size_t data_len,
>> >> +             void const *prefix, size_t prefix_len)
>> >> +{
>> >> +     if (prefix_len > data_len)
>> >> +             return false;
>> >> +     return !memcmp(data, prefix, prefix_len);
>> >
>> > I'd prefer to see the order changed, and use memeq() here.
>>
>> I don't think memeq() fits here, as it includes the length comparison,
>> which is required when checking if 2 blocks of memory are equal, but
>> serves no purpose here.
>>
>> I'd need to either:
>>  - pass prefix_len twice (ugly)
>
> I think this is correct, and I don't see it as that ugly.

I suppose I just don't see the point in writing code that discards
information (in this case, that we already know the lengths are equal)
when we could trivially write code that doesn't discard it (by calling
memcmp directly).

My updated patchset makes the change you requested, however.


More information about the ccan mailing list