Module Name:    src
Committed By:   sjg
Date:           Tue Apr 24 20:26:58 UTC 2012

Modified Files:
        src/usr.bin/make: buf.c buf.h var.c

Log Message:
Var* are generally very liberal with memory, with the expectation
that none of it persists for long.
This isn't always true - for example a long running .for loop.

Buf_DestroyCompact() is used by Var_Subst(), rather than Buf_Destroy().
If it looks like we can save BUF_COMPACT_LIMIT (128) or more bytes,
call realloc.  This can reduce memory consumption by about 20%
Setting BUF_COMPACT_LIMIT to 0 dissables this.


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/usr.bin/make/buf.c
cvs rdiff -u -r1.16 -r1.17 src/usr.bin/make/buf.h
cvs rdiff -u -r1.167 -r1.168 src/usr.bin/make/var.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/buf.c
diff -u src/usr.bin/make/buf.c:1.24 src/usr.bin/make/buf.c:1.25
--- src/usr.bin/make/buf.c:1.24	Sat Jan 17 13:29:37 2009
+++ src/usr.bin/make/buf.c	Tue Apr 24 20:26:58 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: buf.c,v 1.24 2009/01/17 13:29:37 dsl Exp $	*/
+/*	$NetBSD: buf.c,v 1.25 2012/04/24 20:26:58 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: buf.c,v 1.24 2009/01/17 13:29:37 dsl Exp $";
+static char rcsid[] = "$NetBSD: buf.c,v 1.25 2012/04/24 20:26:58 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)buf.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: buf.c,v 1.24 2009/01/17 13:29:37 dsl Exp $");
+__RCSID("$NetBSD: buf.c,v 1.25 2012/04/24 20:26:58 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -248,3 +248,44 @@ Buf_Destroy(Buffer *buf, Boolean freeDat
 
     return data;
 }
+
+
+/*-
+ *-----------------------------------------------------------------------
+ * Buf_DestroyCompact --
+ *	Nuke a buffer and return its data.
+ *
+ * Input:
+ *	buf		Buffer to destroy
+ *
+ * Results:
+ *	Data buffer
+ *
+ * Side Effects:
+ *	If the buffer size is much greater than its content,
+ *	a new buffer will be allocated and the old one freed.
+ *
+ *-----------------------------------------------------------------------
+ */
+#ifndef BUF_COMPACT_LIMIT
+# define BUF_COMPACT_LIMIT 128          /* worthwhile saving */
+#endif
+
+Byte *
+Buf_DestroyCompact(Buffer *buf)
+{
+#if BUF_COMPACT_LIMIT > 0
+    Byte *data;
+
+    if (buf->size - buf->count >= BUF_COMPACT_LIMIT) {
+	/* We trust realloc to be smart */
+	data = bmake_realloc(buf->buffer, buf->count + 1);
+	if (data) {
+	    data[buf->count] = 0;
+	    Buf_Destroy(buf, FALSE);
+	    return data;
+	}
+    }
+#endif
+    return Buf_Destroy(buf, FALSE);
+}

Index: src/usr.bin/make/buf.h
diff -u src/usr.bin/make/buf.h:1.16 src/usr.bin/make/buf.h:1.17
--- src/usr.bin/make/buf.h:1.16	Sat Jan 17 13:55:42 2009
+++ src/usr.bin/make/buf.h	Tue Apr 24 20:26:58 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: buf.h,v 1.16 2009/01/17 13:55:42 dsl Exp $	*/
+/*	$NetBSD: buf.h,v 1.17 2012/04/24 20:26:58 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -114,5 +114,6 @@ Byte *Buf_GetAll(Buffer *, int *);
 void Buf_Empty(Buffer *);
 void Buf_Init(Buffer *, int);
 Byte *Buf_Destroy(Buffer *, Boolean);
+Byte *Buf_DestroyCompact(Buffer *);
 
 #endif /* _BUF_H */

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.167 src/usr.bin/make/var.c:1.168
--- src/usr.bin/make/var.c:1.167	Fri Jun  3 21:10:42 2011
+++ src/usr.bin/make/var.c	Tue Apr 24 20:26:58 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.167 2011/06/03 21:10:42 sjg Exp $	*/
+/*	$NetBSD: var.c,v 1.168 2012/04/24 20:26:58 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.167 2011/06/03 21:10:42 sjg Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.168 2012/04/24 20:26:58 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.167 2011/06/03 21:10:42 sjg Exp $");
+__RCSID("$NetBSD: var.c,v 1.168 2012/04/24 20:26:58 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -4075,7 +4075,7 @@ Var_Subst(const char *var, const char *s
 	}
     }
 
-    return Buf_Destroy(&buf, FALSE);
+    return Buf_DestroyCompact(&buf);
 }
 
 /*-

Reply via email to