2007-06-07 05:16:20

by Mike Frysinger

[permalink] [raw]
Subject: [patch/rfc] implement memmem() locally in kallsyms.c

This patch basically copies the gnulib version of memmem() into
scripts/kallsyms.c. While a useful function, it isn't in POSIX so some
systems (like Darwin) choose to omit it. How do others feel ?

Signed-off-by: Mike Frysinger <[email protected]>
---
--- a/scripts/kallsyms.c
+++ b/scripts/kallsyms.c
@@ -26,8 +26,6 @@
*
*/

-#define _GNU_SOURCE
-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -56,6 +54,37 @@ int token_profit[0x10000];
unsigned char best_table[256][2];
unsigned char best_table_len[256];

+/* memmem(), while useful, is not in POSIX, so create a local version
+ * so we can compile on non-GNU systems (Darwin, *BSD, etc...)
+ */
+void *memmem(const void *haystack, size_t haystack_len,
+ const void *needle, size_t needle_len)
+{
+ const char *begin;
+ const char *const last_possible =
+ (const char *)haystack + haystack_len - needle_len;
+
+ /* The first occurrence of the empty string is deemed to occur at
+ * the beginning of the string.
+ */
+ if (needle_len == 0)
+ return (void *)haystack;
+
+ /* Sanity check, otherwise the loop might search through the whole
+ * memory.
+ */
+ if (haystack_len < needle_len)
+ return NULL;
+
+ for (begin = (const char *)haystack; begin <= last_possible; ++begin)
+ if (begin[0] == ((const char *)needle)[0] &&
+ !memcmp((const void *)&begin[1],
+ (const void *)((const char *)needle + 1),
+ needle_len - 1))
+ return (void *)begin;
+
+ return NULL;
+}

static void usage(void)
{


2007-06-07 09:37:11

by Jesper Juhl

[permalink] [raw]
Subject: Re: [patch/rfc] implement memmem() locally in kallsyms.c

On 07/06/07, Mike Frysinger <[email protected]> wrote:
> This patch basically copies the gnulib version of memmem() into
> scripts/kallsyms.c. While a useful function, it isn't in POSIX so some
> systems (like Darwin) choose to omit it. How do others feel ?
>

Do people actually build Linux kernels on Darwin & *BSD systems? If
they do then why?

What I'm getting at is; why do we care if it will build there?

--
Jesper Juhl <[email protected]>
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html

2007-06-07 15:44:43

by Mike Frysinger

[permalink] [raw]
Subject: Re: [patch/rfc] implement memmem() locally in kallsyms.c

On Thursday 07 June 2007, Jesper Juhl wrote:
> On 07/06/07, Mike Frysinger <[email protected]> wrote:
> > This patch basically copies the gnulib version of memmem() into
> > scripts/kallsyms.c. While a useful function, it isn't in POSIX so some
> > systems (like Darwin) choose to omit it. How do others feel ?
>
> Do people actually build Linux kernels on Darwin & *BSD systems? If
> they do then why?

in the embedded world, yes ... everything is being cross-compiled and
deployed on different hardware anyways, so the build env shouldnt matter

> What I'm getting at is; why do we care if it will build there?

that was the [rfc] part of the e-mail ... i got enough complaints from people
OS X people to put together the patch
-mike


Attachments:
(No filename) (741.00 B)
signature.asc (827.00 B)
This is a digitally signed message part.
Download all attachments

2007-06-08 12:27:47

by Paulo Marques

[permalink] [raw]
Subject: Re: [patch/rfc] implement memmem() locally in kallsyms.c

--- ./scripts/kallsyms.c.orig 2007-06-08 12:55:49.000000000 +0100
+++ ./scripts/kallsyms.c 2007-06-08 13:19:52.000000000 +0100
@@ -378,6 +378,17 @@ static void build_initial_tok_table(void
table_cnt = pos;
}

+static void *find_token(unsigned char *str, int len, unsigned char *token)
+{
+ int i;
+
+ for (i = 0; i < len - 1; i++) {
+ if (str[i] == token[0] && str[i+1] == token[1])
+ return &str[i];
+ }
+ return NULL;
+}
+
/* replace a given token in all the valid symbols. Use the sampled symbols
* to update the counts */
static void compress_symbols(unsigned char *str, int idx)
@@ -391,7 +402,7 @@ static void compress_symbols(unsigned ch
p1 = table[i].sym;

/* find the token on the symbol */
- p2 = memmem(p1, len, str, 2);
+ p2 = find_token(p1, len, str);
if (!p2) continue;

/* decrease the counts for this symbol's tokens */
@@ -410,7 +421,7 @@ static void compress_symbols(unsigned ch
if (size < 2) break;

/* find the token on the symbol */
- p2 = memmem(p1, size, str, 2);
+ p2 = find_token(p1, size, str);

} while (p2);


Attachments:
patch (1.06 kB)