[PATCH 2/3] Clean up membuffer internal structure names, add strip function

Linus Torvalds torvalds at linux-foundation.org
Sun Feb 9 09:56:18 UTC 2014


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Sun, 9 Feb 2014 09:40:49 -0800
Subject: [PATCH 2/3] Clean up membuffer internal structure names, add strip function

The "size" member was confusing - it's the size of the allocation, not
the size of the current string.  The size of the current string is the
member called "used".

This naming makes perfect sense for the internal implementation, but
it's confusing to users who actually do want to get the size of the
resulting string at the end.

So rename the fields to "alloc" and "len" - which is pretty clear.

This also adds a helper function to strip whitespace from the end:
"strip_mb()".

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

Trivial membuffer search-and-replace and helper function.

 membuffer.c | 24 +++++++++++++++---------
 membuffer.h | 13 ++++++++++++-
 save-xml.c  |  2 +-
 3 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/membuffer.c b/membuffer.c
index 30ba05fdbfc4..a258c424a88f 100644
--- a/membuffer.c
+++ b/membuffer.c
@@ -10,18 +10,24 @@ void free_buffer(struct membuffer *b)
 {
 	free(b->buffer);
 	b->buffer = NULL;
-	b->used = 0;
-	b->size = 0;
+	b->len = 0;
+	b->alloc = 0;
 }
 
 void flush_buffer(struct membuffer *b, FILE *f)
 {
-	if (b->used) {
-		fwrite(b->buffer, 1, b->used, f);
+	if (b->len) {
+		fwrite(b->buffer, 1, b->len, f);
 		free_buffer(b);
 	}
 }
 
+void strip_mb(struct membuffer *b)
+{
+	while (b->len && isspace(b->buffer[b->len-1]))
+		b->len--;
+}
+
 /*
  * Running out of memory isn't really an issue these days.
  * So rather than do insane error handling and making the
@@ -36,8 +42,8 @@ static void oom(void)
 
 static void make_room(struct membuffer *b, unsigned int size)
 {
-	unsigned int needed = b->used + size;
-	if (needed > b->size) {
+	unsigned int needed = b->len + size;
+	if (needed > b->alloc) {
 		char *n;
 		/* round it up to not reallocate all the time.. */
 		needed = needed * 9 / 8 + 1024;
@@ -45,15 +51,15 @@ static void make_room(struct membuffer *b, unsigned int size)
 		if (!n)
 			oom();
 		b->buffer = n;
-		b->size = needed;
+		b->alloc = needed;
 	}
 }
 
 void put_bytes(struct membuffer *b, const char *str, int len)
 {
 	make_room(b, len);
-	memcpy(b->buffer + b->used, str, len);
-	b->used += len;
+	memcpy(b->buffer + b->len, str, len);
+	b->len += len;
 }
 
 void put_string(struct membuffer *b, const char *str)
diff --git a/membuffer.h b/membuffer.h
index bef9e76bc0e9..17cfe6e8c0e8 100644
--- a/membuffer.h
+++ b/membuffer.h
@@ -1,8 +1,14 @@
 #ifndef MEMBUFFER_H
 #define MEMBUFFER_H
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <ctype.h>
+
 struct membuffer {
-	unsigned int size, used;
+	unsigned int len, alloc;
 	char *buffer;
 };
 
@@ -16,6 +22,7 @@ extern void free_buffer(struct membuffer *);
 extern void flush_buffer(struct membuffer *, FILE *);
 extern void put_bytes(struct membuffer *, const char *, int);
 extern void put_string(struct membuffer *, const char *);
+extern void strip_mb(struct membuffer *);
 extern __printf(2,0) void put_vformat(struct membuffer *, const char *, va_list);
 extern __printf(2,3) void put_format(struct membuffer *, const char *fmt, ...);
 
@@ -49,4 +56,8 @@ extern int put_duration(struct membuffer *, duration_t, const char *, const char
 extern int put_pressure(struct membuffer *, pressure_t, const char *, const char *);
 extern int put_salinity(struct membuffer *, int, const char *, const char *);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif
diff --git a/save-xml.c b/save-xml.c
index 7eba7165cabc..7bb2642bd74b 100644
--- a/save-xml.c
+++ b/save-xml.c
@@ -610,7 +610,7 @@ void export_dives_uddf(const char *filename, const bool selected)
 	 * transform it to UDDF format, finally dumping
 	 * the XML into a character buffer.
 	 */
-	doc = xmlReadMemory(buf.buffer, buf.used, "divelog", NULL, 0);
+	doc = xmlReadMemory(buf.buffer, buf.len, "divelog", NULL, 0);
 	free_buffer(&buf);
 	if (!doc) {
 		fprintf(stderr, "Failed to read XML memory\n");
-- 
1.9.0.rc3



More information about the subsurface mailing list