lkml.org 
[lkml]   [2018]   [Dec]   [21]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [for-next][PATCH 23/24] string.h: Add strncmp_prefix() helper macro
From
Date
On Fri, 2018-12-21 at 16:08 -0500, Steven Rostedt wrote:
> On Fri, 21 Dec 2018 21:58:32 +0100
> Andreas Schwab <schwab@linux-m68k.org> wrote:
>
>
> > > Well, perhaps I can just remove the ending ones. I get paranoid with
> > > macro variables, and tend to over do it so that there's no question.
> >
> > Why not make it an inline function?
>
> Matters if that removes the strlen(const) optimization. I could try it
> and see what happens.

Using

static inline bool str_has_prefix(const char *str, const char prefix[])
{
return !strncmp(str, prefix, strlen(prefix));
}

eliminates the strlen with gcc 4.8 (oldest I still have)

$ cat lib/test_module.c
* This module emits "Hello, world" on printk when loaded.
*
* It is designed to be used for basic evaluation of the module loading
* subsystem (for example when validating module signing/verification). It
* lacks any extra dependencies, and will not normally be loaded by the
* system unless explicitly requested by name.
*/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/init.h>
#include <linux/module.h>
#include <linux/printk.h>

static inline bool str_has_prefix(const char *str, const char prefix[])
{
return !strncmp(str, prefix, strlen(prefix));
}

bool test_str_has_prefix(const char *foo)
{
return str_has_prefix("TomJonesPrefix", foo);
}

bool test_str_has_prefix_TomJones(void)
{
return str_has_prefix("TomJonesPrefix", "TomJones");
}

$ make CC=/usr/bin/gcc-4.8 lib/test_module.o
$ objdump -d -s lib/test_module.o

lib/test_module.o: file format elf64-x86-64

Contents of section .text:
0000 534889fb e8000000 00b90f00 00004883 SH............H.
0010 f80f4889 df480f4e c848c7c6 00000000 ..H..H.N.H......
0020 4839c9f3 a65b0f94 c0c3660f 1f440000 H9...[....f..D..
0030 b8010000 00c3 ......
Contents of section .rodata.str1.1:
0000 546f6d4a 6f6e6573 50726566 697800 TomJonesPrefix.
Contents of section .comment:
0000 00474343 3a202855 62756e74 7520342e .GCC: (Ubuntu 4.
0010 382e352d 34756275 6e747539 2920342e 8.5-4ubuntu9) 4.
0020 382e3500 8.5.
Contents of section .orc_unwind_ip:
0000 00000000 00000000 00000000 00000000 ................
0010 00000000 00000000 ........
Contents of section .orc_unwind:
0000 08000000 05001000 00000500 08000000 ................
0010 05000000 00000000 08000000 05000000 ................
0020 00000000 ....

Disassembly of section .text:

0000000000000000 <test_str_has_prefix>:
0: 53 push %rbx
1: 48 89 fb mov %rdi,%rbx
4: e8 00 00 00 00 callq 9 <test_str_has_prefix+0x9>
9: b9 0f 00 00 00 mov $0xf,%ecx
e: 48 83 f8 0f cmp $0xf,%rax
12: 48 89 df mov %rbx,%rdi
15: 48 0f 4e c8 cmovle %rax,%rcx
19: 48 c7 c6 00 00 00 00 mov $0x0,%rsi
20: 48 39 c9 cmp %rcx,%rcx
23: f3 a6 repz cmpsb %es:(%rdi),%ds:(%rsi)
25: 5b pop %rbx
26: 0f 94 c0 sete %al
29: c3 retq
2a: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)

0000000000000030 <test_str_has_prefix_TomJones>:
30: b8 01 00 00 00 mov $0x1,%eax
35: c3 retq


\
 
 \ /
  Last update: 2018-12-21 23:21    [W:1.927 / U:0.012 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site