bug-gnulib
[Top][All Lists]
Advanced

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

[PATCH 2/2] dfa: struct dfamust now uses flexible array


From: Paul Eggert
Subject: [PATCH 2/2] dfa: struct dfamust now uses flexible array
Date: Thu, 19 Dec 2019 19:21:10 -0800

* lib/dfa.c: Include flexmember.h.
(dfamust, dfamustfree): Adjust to struct dfamust change.
This saves a call to malloc+free.
* lib/dfa.h (struct dfamust): Make the final member a
flexible array member.
* modules/dfa (Depends-on): Add flexmember.
---
 ChangeLog   | 8 ++++++++
 lib/dfa.c   | 7 ++++---
 lib/dfa.h   | 2 +-
 modules/dfa | 1 +
 4 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 6d0120331..b58c7b851 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2019-12-19  Paul Eggert  <address@hidden>
 
+       dfa: struct dfamust now uses flexible array
+       * lib/dfa.c: Include flexmember.h.
+       (dfamust, dfamustfree): Adjust to struct dfamust change.
+       This saves a call to malloc+free.
+       * lib/dfa.h (struct dfamust): Make the final member a
+       flexible array member.
+       * modules/dfa (Depends-on): Add flexmember.
+
        dfa: fast->small for array elements
        * lib/dfa.c (charclass_word): Use uint_least64_t not uint_fast64_t,
        since this type is used in arrays.  This change is more for
diff --git a/lib/dfa.c b/lib/dfa.c
index a7cd3e84f..734e74e29 100644
--- a/lib/dfa.c
+++ b/lib/dfa.c
@@ -24,6 +24,8 @@
 
 #include "dfa.h"
 
+#include "flexmember.h"
+
 #include <assert.h>
 #include <ctype.h>
 #include <stdint.h>
@@ -4272,11 +4274,11 @@ dfamust (struct dfa const *d)
   struct dfamust *dm = NULL;
   if (*result)
     {
-      dm = xmalloc (sizeof *dm);
+      dm = xmalloc (FLEXSIZEOF (struct dfamust, must, strlen (result) + 1));
       dm->exact = exact;
       dm->begline = begline;
       dm->endline = endline;
-      dm->must = xstrdup (result);
+      strcpy (dm->must, result);
     }
 
   while (mp)
@@ -4292,7 +4294,6 @@ dfamust (struct dfa const *d)
 void
 dfamustfree (struct dfamust *dm)
 {
-  free (dm->must);
   free (dm);
 }
 
diff --git a/lib/dfa.h b/lib/dfa.h
index 0da597fc9..56e2ee000 100644
--- a/lib/dfa.h
+++ b/lib/dfa.h
@@ -31,7 +31,7 @@ struct dfamust
   bool exact;
   bool begline;
   bool endline;
-  char *must;
+  char must[FLEXIBLE_ARRAY_MEMBER];
 };
 
 /* The dfa structure. It is completely opaque. */
diff --git a/modules/dfa b/modules/dfa
index ee5bec85d..f1b00fe1e 100644
--- a/modules/dfa
+++ b/modules/dfa
@@ -11,6 +11,7 @@ Depends-on:
 assert
 c99
 ctype
+flexmember
 intprops
 isblank
 locale
-- 
2.24.1




reply via email to

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