Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/make make: add types Substring and LazyBuf



details:   https://anonhg.NetBSD.org/src/rev/7d142f548688
branches:  trunk
changeset: 378363:7d142f548688
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Apr 11 12:06:53 2021 +0000

description:
make: add types Substring and LazyBuf

These will be used for making the string handling more efficient,
avoiding allocations, especially when evaluating variable expressions.

Since the string handling has grown quite a bit in the last months,
extract it into its own header file.

No functional change.

diffstat:

 usr.bin/make/make.h    |    5 +-
 usr.bin/make/nonints.h |  108 +-------------------
 usr.bin/make/str.h     |  269 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 273 insertions(+), 109 deletions(-)

diffs (truncated from 417 to 300 lines):

diff -r 35aa6b78b0be -r 7d142f548688 usr.bin/make/make.h
--- a/usr.bin/make/make.h       Sun Apr 11 11:41:27 2021 +0000
+++ b/usr.bin/make/make.h       Sun Apr 11 12:06:53 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: make.h,v 1.260 2021/04/04 10:05:08 rillig Exp $        */
+/*     $NetBSD: make.h,v 1.261 2021/04/11 12:06:53 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -149,10 +149,11 @@ typedef unsigned int Boolean;
 
 #include "lst.h"
 #include "enum.h"
+#include "make_malloc.h"
+#include "str.h"
 #include "hash.h"
 #include "config.h"
 #include "buf.h"
-#include "make_malloc.h"
 
 /*
  * The typical flow of states is:
diff -r 35aa6b78b0be -r 7d142f548688 usr.bin/make/nonints.h
--- a/usr.bin/make/nonints.h    Sun Apr 11 11:41:27 2021 +0000
+++ b/usr.bin/make/nonints.h    Sun Apr 11 12:06:53 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nonints.h,v 1.211 2021/04/04 11:56:43 rillig Exp $     */
+/*     $NetBSD: nonints.h,v 1.212 2021/04/11 12:06:53 rillig Exp $     */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -166,112 +166,6 @@ void Parse_SetInput(const char *, int, i
 void Parse_MainName(GNodeList *);
 int Parse_GetFatals(void);
 
-/* str.c */
-
-/* A read-only string that may need to be freed after use. */
-typedef struct FStr {
-       const char *str;
-       void *freeIt;
-} FStr;
-
-/* A modifiable string that may need to be freed after use. */
-typedef struct MFStr {
-       char *str;
-       void *freeIt;
-} MFStr;
-
-typedef struct Words {
-       char **words;
-       size_t len;
-       void *freeIt;
-} Words;
-
-#if __STDC_VERSION__ >= 199901L
-#  define FStr_Literal(str, freeIt) (FStr) { str, freeIt }
-#else
-MAKE_INLINE FStr
-FStr_Literal(const char *str, void *freeIt)
-{
-       FStr fstr;
-       fstr.str = str;
-       fstr.freeIt = freeIt;
-       return fstr;
-}
-#endif
-
-/* Return a string that is the sole owner of str. */
-MAKE_INLINE FStr
-FStr_InitOwn(char *str)
-{
-       return FStr_Literal(str, str);
-}
-
-/* Return a string that refers to the shared str. */
-MAKE_INLINE FStr
-FStr_InitRefer(const char *str)
-{
-       return FStr_Literal(str, NULL);
-}
-
-MAKE_INLINE void
-FStr_Done(FStr *fstr)
-{
-       free(fstr->freeIt);
-#ifdef CLEANUP
-       fstr->str = NULL;
-       fstr->freeIt = NULL;
-#endif
-}
-
-#if __STDC_VERSION__ >= 199901L
-#  define MFStr_Literal(str, freeIt) (MFStr) { str, freeIt }
-#else
-MAKE_INLINE MFStr
-MFStr_Literal(char *str, void *freeIt)
-{
-       MFStr mfstr;
-       mfstr.str = str;
-       mfstr.freeIt = freeIt;
-       return mfstr;
-}
-#endif
-
-/* Return a string that is the sole owner of str. */
-MAKE_INLINE MFStr
-MFStr_InitOwn(char *str)
-{
-       return MFStr_Literal(str, str);
-}
-
-/* Return a string that refers to the shared str. */
-MAKE_INLINE MFStr
-MFStr_InitRefer(char *str)
-{
-       return MFStr_Literal(str, NULL);
-}
-
-MAKE_INLINE void
-MFStr_Done(MFStr *mfstr)
-{
-       free(mfstr->freeIt);
-#ifdef CLEANUP
-       mfstr->str = NULL;
-       mfstr->freeIt = NULL;
-#endif
-}
-
-Words Str_Words(const char *, bool);
-MAKE_INLINE void
-Words_Free(Words w)
-{
-       free(w.words);
-       free(w.freeIt);
-}
-
-char *str_concat2(const char *, const char *);
-char *str_concat3(const char *, const char *, const char *);
-char *str_concat4(const char *, const char *, const char *, const char *);
-bool Str_Match(const char *, const char *);
 
 /* suff.c */
 void Suff_Init(void);
diff -r 35aa6b78b0be -r 7d142f548688 usr.bin/make/str.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/make/str.h        Sun Apr 11 12:06:53 2021 +0000
@@ -0,0 +1,269 @@
+/*     $NetBSD: str.h,v 1.1 2021/04/11 12:06:53 rillig Exp $   */
+
+/*
+ Copyright (c) 2021 Roland Illig <rillig%NetBSD.org@localhost>
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
+ BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+/*
+ * Memory-efficient string handling.
+ */
+
+
+/* A read-only string that may need to be freed after use. */
+typedef struct FStr {
+       const char *str;
+       void *freeIt;
+} FStr;
+
+/* A modifiable string that may need to be freed after use. */
+typedef struct MFStr {
+       char *str;
+       void *freeIt;
+} MFStr;
+
+/* A read-only range of a character array, NOT null-terminated. */
+typedef struct {
+       const char *start;
+       const char *end;
+} Substring;
+
+/*
+ * Builds a string, only allocating memory if the string is different from the
+ * expected string.
+ */
+typedef struct LazyBuf {
+       char *data;
+       size_t len;
+       size_t cap;
+       Substring expected;
+       void *freeIt;
+} LazyBuf;
+
+/* The result of splitting a string into words. */
+typedef struct Words {
+       char **words;
+       size_t len;
+       void *freeIt;
+} Words;
+
+
+MAKE_INLINE FStr
+FStr_Init(const char *str, void *freeIt)
+{
+       FStr fstr;
+       fstr.str = str;
+       fstr.freeIt = freeIt;
+       return fstr;
+}
+
+/* Return a string that is the sole owner of str. */
+MAKE_INLINE FStr
+FStr_InitOwn(char *str)
+{
+       return FStr_Init(str, str);
+}
+
+/* Return a string that refers to the shared str. */
+MAKE_INLINE FStr
+FStr_InitRefer(const char *str)
+{
+       return FStr_Init(str, NULL);
+}
+
+MAKE_INLINE void
+FStr_Done(FStr *fstr)
+{
+       free(fstr->freeIt);
+#ifdef CLEANUP
+       fstr->str = NULL;
+       fstr->freeIt = NULL;
+#endif
+}
+
+
+MAKE_INLINE MFStr
+MFStr_Init(char *str, void *freeIt)
+{
+       MFStr mfstr;
+       mfstr.str = str;
+       mfstr.freeIt = freeIt;
+       return mfstr;
+}
+
+/* Return a string that is the sole owner of str. */
+MAKE_INLINE MFStr
+MFStr_InitOwn(char *str)
+{
+       return MFStr_Init(str, str);
+}
+
+/* Return a string that refers to the shared str. */
+MAKE_INLINE MFStr
+MFStr_InitRefer(char *str)
+{
+       return MFStr_Init(str, NULL);
+}
+
+MAKE_INLINE void
+MFStr_Done(MFStr *mfstr)
+{
+       free(mfstr->freeIt);
+#ifdef CLEANUP
+       mfstr->str = NULL;
+       mfstr->freeIt = NULL;
+#endif
+}
+
+
+MAKE_INLINE Substring
+Substring_Init(const char *start, const char *end)
+{
+       Substring sub;
+
+       sub.start = start;
+       sub.end = end;
+       return sub;
+}
+
+MAKE_INLINE Substring



Home | Main Index | Thread Index | Old Index