grep-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Grep-devel] [PATCH 1/4] grep: prepare search backends for thread-safety


From: Zev Weiss
Subject: [Grep-devel] [PATCH 1/4] grep: prepare search backends for thread-safety
Date: Sun, 25 Dec 2016 02:57:06 -0600

To facilitate removing mutable global state from search backends,
compile() functions will return an opaque pointer to backend-specific
data, which must then be passed back into the corresponding execute()
function.  This is merely a preparatory step changing function
signatures and call sites, so the pointers passed & returned are
dummies for now and not (yet) actually used.

* src/grep.c (compile_fp_t): Now returns an opaque pointer (the
compiled pattern).
(execute_fp_t): Now passed the pointer returned by a compile_fp_t.
All call sites updated accordingly.
(compiled_pattern): New static variable.
* src/dfasearch.c (GEAcompile): Return a void pointer (dummy NULL).
(EGexecute): Receive a void pointer argument (unused).
* src/kwsearch.c (Fcompile): Return a void pointer (dummy NULL).
(Fexecute): Receive a void pointer argument (unused).
* src/pcresearch.c (Pcompile): Return a void pointer (dummy NULL).
(Pexecute): Receive a void pointer argument (unused).
* src/search.h: Update compile/execute function prototypes.
---
 src/dfasearch.c  |  6 ++++--
 src/grep.c       | 21 +++++++++++++--------
 src/kwsearch.c   |  6 ++++--
 src/pcresearch.c |  6 ++++--
 src/search.h     | 12 ++++++------
 5 files changed, 31 insertions(+), 20 deletions(-)

diff --git a/src/dfasearch.c b/src/dfasearch.c
index 7f68907..25276fe 100644
--- a/src/dfasearch.c
+++ b/src/dfasearch.c
@@ -104,7 +104,7 @@ kwsmusts (void)
   dfamustfree (dm);
 }
 
-void
+void *
 GEAcompile (char const *pattern, size_t size, reg_syntax_t syntax_bits)
 {
   char *motif;
@@ -205,10 +205,12 @@ GEAcompile (char const *pattern, size_t size, 
reg_syntax_t syntax_bits)
   kwsmusts ();
 
   free (motif);
+
+  return NULL;
 }
 
 size_t
-EGexecute (char const *buf, size_t size, size_t *match_size,
+EGexecute (void *vdc, char const *buf, size_t size, size_t *match_size,
            char const *start_ptr)
 {
   char const *buflim, *beg, *end, *ptr, *match, *best_match, *mb_start;
diff --git a/src/grep.c b/src/grep.c
index c306ce0..9a89b5e 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -575,9 +575,11 @@ static bool seek_failed;
 static bool seek_data_failed;
 
 /* Functions we'll use to search. */
-typedef void (*compile_fp_t) (char const *, size_t, reg_syntax_t);
-typedef size_t (*execute_fp_t) (char const *, size_t, size_t *, char const *);
+typedef void *(*compile_fp_t) (char const *, size_t, reg_syntax_t);
+typedef size_t (*execute_fp_t) (void *, char const *, size_t, size_t *,
+                                char const *);
 static execute_fp_t execute;
+static void *compiled_pattern;
 
 static char const *
 input_filename (void)
@@ -1146,8 +1148,8 @@ print_line_middle (char *beg, char *lim,
 
   for (cur = beg;
        (cur < lim
-        && ((match_offset = execute (beg, lim - beg, &match_size, cur))
-            != (size_t) -1));
+        && ((match_offset = execute (compiled_pattern, beg, lim - beg,
+                                     &match_size, cur)) != (size_t) -1));
        cur = b + match_size)
     {
       b = beg + match_offset;
@@ -1291,7 +1293,7 @@ prpending (char const *lim)
       size_t match_size;
       --pending;
       if (outleft
-          || ((execute (lastout, nl + 1 - lastout,
+          || ((execute (compiled_pattern, lastout, nl + 1 - lastout,
                         &match_size, NULL) == (size_t) -1)
               == !out_invert))
         prline (lastout, nl + 1, SEP_CHAR_REJECTED);
@@ -1404,7 +1406,8 @@ grepbuf (char *beg, char const *lim)
   for (char *p = beg; p < lim; p = endp)
     {
       size_t match_size;
-      size_t match_offset = execute (p, lim - p, &match_size, NULL);
+      size_t match_offset = execute (compiled_pattern, p, lim - p,
+                                     &match_size, NULL);
       if (match_offset == (size_t) -1)
         {
           if (!out_invert)
@@ -2867,12 +2870,14 @@ main (int argc, char **argv)
     matcher = try_fgrep_pattern (matcher, keys, &keycc);
 
   execute = matchers[matcher].execute;
-  matchers[matcher].compile (keys, keycc, matchers[matcher].syntax);
+  compiled_pattern = matchers[matcher].compile (keys, keycc,
+                                                matchers[matcher].syntax);
   free (keys);
   /* We need one byte prior and one after.  */
   char eolbytes[3] = { 0, eolbyte, 0 };
   size_t match_size;
-  skip_empty_lines = ((execute (eolbytes + 1, 1, &match_size, NULL) == 0)
+  skip_empty_lines = ((execute (compiled_pattern, eolbytes + 1, 1,
+                                &match_size, NULL) == 0)
                       == out_invert);
 
   if ((argc - optind > 1 && !no_filenames) || with_filenames)
diff --git a/src/kwsearch.c b/src/kwsearch.c
index 7d11230..3b7474d 100644
--- a/src/kwsearch.c
+++ b/src/kwsearch.c
@@ -26,7 +26,7 @@
    any string matching the regexp. */
 static kwset_t kwset;
 
-void
+void *
 Fcompile (char const *pattern, size_t size, reg_syntax_t ignored)
 {
   size_t total = size;
@@ -68,10 +68,12 @@ Fcompile (char const *pattern, size_t size, reg_syntax_t 
ignored)
   while (p);
 
   kwsprep (kwset);
+
+  return NULL;
 }
 
 size_t
-Fexecute (char const *buf, size_t size, size_t *match_size,
+Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
           char const *start_ptr)
 {
   char const *beg, *end, *mb_start;
diff --git a/src/pcresearch.c b/src/pcresearch.c
index 245469c..54d9430 100644
--- a/src/pcresearch.c
+++ b/src/pcresearch.c
@@ -87,7 +87,7 @@ jit_exec (char const *subject, int search_bytes, int 
search_offset,
 static int empty_match[2];
 #endif
 
-void
+void *
 Pcompile (char const *pattern, size_t size, reg_syntax_t ignored)
 {
 #if !HAVE_LIBPCRE
@@ -175,10 +175,12 @@ Pcompile (char const *pattern, size_t size, reg_syntax_t 
ignored)
                                   PCRE_NOTBOL, sub, NSUB);
   empty_match[true] = pcre_exec (cre, extra, "", 0, 0, 0, sub, NSUB);
 #endif /* HAVE_LIBPCRE */
+
+  return NULL;
 }
 
 size_t
-Pexecute (char const *buf, size_t size, size_t *match_size,
+Pexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
           char const *start_ptr)
 {
 #if !HAVE_LIBPCRE
diff --git a/src/search.h b/src/search.h
index b700ed5..766b1f6 100644
--- a/src/search.h
+++ b/src/search.h
@@ -55,16 +55,16 @@ extern ptrdiff_t mb_goback (char const **, char const *, 
char const *);
 
 /* dfasearch.c */
 extern struct localeinfo localeinfo;
-extern void GEAcompile (char const *, size_t, reg_syntax_t);
-extern size_t EGexecute (char const *, size_t, size_t *, char const *);
+extern void *GEAcompile (char const *, size_t, reg_syntax_t);
+extern size_t EGexecute (void *, char const *, size_t, size_t *, char const *);
 
 /* kwsearch.c */
-extern void Fcompile (char const *, size_t, reg_syntax_t);
-extern size_t Fexecute (char const *, size_t, size_t *, char const *);
+extern void *Fcompile (char const *, size_t, reg_syntax_t);
+extern size_t Fexecute (void *, char const *, size_t, size_t *, char const *);
 
 /* pcresearch.c */
-extern void Pcompile (char const *, size_t, reg_syntax_t);
-extern size_t Pexecute (char const *, size_t, size_t *, char const *);
+extern void *Pcompile (char const *, size_t, reg_syntax_t);
+extern size_t Pexecute (void *, char const *, size_t, size_t *, char const *);
 
 /* Return the number of bytes in the character at the start of S, which
    is of size N.  N must be positive.  MBS is the conversion state.
-- 
2.11.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]