Skip to contentSkip to conversation

LVM2 ./WHATS_NEW_DM libdm/libdm-deptree.c

PATCH [email protected] 4 participants 7 messages Dec 11, 2008 – Dec 12, 2008

lore
Share & tools
lore
#0 lore raw

CVSROOT: /cvs/lvm2
Module name: LVM2
Changes by: zkabelac at sourceware.org 2008-12-11 16:25:51

Modified files:
. : WHATS_NEW_DM
libdm : libdm-deptree.c

Log message:
Replace _dm_snprintf with EMIT_PARAMS macro for creating target lines

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/WHATS_NEW_DM.diff?cvsroot=lvm2&r1=1.257&r2=1.258
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/libdm/libdm-deptree.c.diff?cvsroot=lvm2&r1=1.47&r2=1.48

WHATS_NEW_DM +1 -0
@@ -1,5 +1,6 @@
 Version 1.02.30 -
 ====================================
+  Replace _dm_snprintf with EMIT_PARAMS macro for creating target lines
 
 Version 1.02.29 - 10th November 2008
 ====================================
libdm/libdm-deptree.c +42 -106

208 lines not on this page · raw message

#1 lore raw
Quoted 5 lines
libdm/libdm-deptree.c +4 -0
+/* simplify string emiting code */
+#define EMIT_PARAMS(p, str...)\
+	do {\
+		const size_t bufsize = paramsize - (size_t)p;\

libdm/libdm-deptree.c line 1238

Quoted 10 lines
libdm/libdm-deptree.c +10 -0
+		int w;\
+		\
+		if ((w = snprintf(params + p, bufsize, str)) < 0\
+		    || ((size_t)w >= bufsize)) {\
+			stack; /* Out of space */\
+			return -1;\
+		}\
+		p += w;\
+	} while (0)
+

Do we have to do a macro here? Macros like this are harder to debug...

#6 lore raw
In reply to [email protected]

libdm/libdm-deptree.c line 139

Quoted 12 lines · zkabelac at sourceware.org

Changes by: zkabelac at sourceware.org 2008-12-11 16:25:51
Modified files:
. : WHATS_NEW_DM
libdm : libdm-deptree.c

Log message:
Replace _dm_snprintf with EMIT_PARAMS macro for creating target lines

+++ LVM2/libdm/libdm-deptree.c 2008/12/11 16:25:51 1.48

libdm/libdm-deptree.c +0 -2
-/* FIXME Consider exporting this */
-static int _dm_snprintf(char *buf, size_t bufsize, const char *format, ...)

We already did export it in libdm-string.c!

libdm/libdm-deptree.c line 1232

Quoted 8 lines
libdm/libdm-deptree.c +8 -0
+/* simplify string emiting code */
+#define EMIT_PARAMS(p, str...)\
+	do {\
+		const size_t bufsize = paramsize - (size_t)p;\
+		int w;\
+		\
+		if ((w = snprintf(params + p, bufsize, str)) < 0\
+		    || ((size_t)w >= bufsize)) {\

Can we use dm_snprintf() here now?
Also, a style point, we put || on the end of the previous line.

Quoted 11 lines
libdm/libdm-deptree.c +0 -2
 			    struct load_segment *seg, char *params,
 			    size_t paramsize, int *pos)
 {
 	struct seg_area *area;
 	char devbuf[DM_FORMAT_DEV_BUFSIZE];
-	int tw;
-	const char *prefix = "";
 
 	dm_list_iterate_items(area, &seg->areas) {
 		if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
 			return_0;

libdm/libdm-deptree.c line 1250

Quoted 10 lines
libdm/libdm-deptree.c +1 -8
 
-		if ((tw = _dm_snprintf(params + *pos, paramsize - *pos, "%s%s %" PRIu64,
-					prefix, devbuf, area->offset)) < 0) {
-                        stack;	/* Out of space */
-                        return -1;
-                }
-
-		prefix = " ";
-		*pos += tw;
+		EMIT_PARAMS(*pos, " %s %" PRIu64, devbuf, area->offset);

Has this changed the prefix behaviour by adding a space to the first one or does that cancel out somewhere else?
(Any changes to whitespace require thorough testing - there is code that relies on comparing these lines as strings against equivalent lines generated in-kernel.)

Alasdair

Signature · 1 line
agk at redhat.com
#2 lore raw
In reply to Dave Wysochanski
Quoted 6 lines · Dave Wysochanski

libdm/libdm-deptree.c line 1227

Quoted 4 lines
libdm/libdm-deptree.c +3 -0
+/* simplify string emiting code */
+#define EMIT_PARAMS(p, str...)\
+	do {\

Do we have to do a macro here? Macros like this are harder to debug...

Indeed - but sometimes there's stuff that's just too awkward to do as a function. I don't mind this one - we have a similar one in the kernel called DMEMIT - so long as it is correct:-)

Alasdair

Signature · 1 line
agk at redhat.com
#3 lore raw
In reply to Dave Wysochanski

Dave Wysochanski napsal(a):

Quoted 8 lines
Quoted 8 lines
libdm/libdm-deptree.c +8 -0
+/* simplify string emiting code */
+#define EMIT_PARAMS(p, str...)\
+	do {\
+		const size_t bufsize = paramsize - (size_t)p;\
+		int w;\
+		\
+		if ((w = snprintf(params + p, bufsize, str)) < 0\
+		    || ((size_t)w >= bufsize)) {\
Quoted 9 lines

libdm/libdm-deptree.c line 1238

Quoted 6 lines
libdm/libdm-deptree.c +6 -0
+			stack; /* Out of space */\
+			return -1;\
+		}\
+		p += w;\
+	} while (0)
+

Do we have to do a macro here? Macros like this are harder to debug...

I think it's actually minimizing the chance you will add a buggy code by some cut&paste operation when you add new string emitting line.

Also it makes the code more readable.

Do you think there is some potential debug problem in this code, that makes worth to keep the original emitting style?

(i.e. replicator emits string 11 times - without this macro it makes the code even less readable, and also using inline function and 'stack' macro is not going to work together, so it's hard to use gdb-friendly inline here :( )

Zdenek

#4 lore raw
Quoted 23 lines · Zdenek Kabelac

Dave Wysochanski napsal(a):

Quoted 8 lines
Quoted 8 lines
libdm/libdm-deptree.c +8 -0
+/* simplify string emiting code */
+#define EMIT_PARAMS(p, str...)\
+	do {\
+		const size_t bufsize = paramsize - (size_t)p;\
+		int w;\
+		\
+		if ((w = snprintf(params + p, bufsize, str)) < 0\
+		    || ((size_t)w >= bufsize)) {\
Quoted 9 lines

libdm/libdm-deptree.c line 1238

Quoted 6 lines
libdm/libdm-deptree.c +6 -0
+			stack; /* Out of space */\
+			return -1;\
+		}\
+		p += w;\
+	} while (0)
+

Do we have to do a macro here? Macros like this are harder to debug...

I think it's actually minimizing the chance you will add a buggy code by some cut&paste operation when you add new string emitting line.

Quoted 9 lines

Also it makes the code more readable.

Do you think there is some potential debug problem in this code, that makes worth to keep the original emitting style?

(i.e. replicator emits string 11 times - without this macro it makes the code even less readable, and also using inline function and 'stack' macro is not going to work together, so it's hard to use gdb-friendly inline here :( )

I agree something like this makes the code more readable - I was not suggesting keeping the original code. Was just wondering why you chose the macro vs a regular function. Is performance very critical here? Something to do with using va_list? Is there another reason for a macro I'm missing?

#5 lore raw

Dave Wysochanski napsal(a):

Quoted 32 lines · Zdenek Kabelac
Quoted 21 lines

Dave Wysochanski napsal(a):

Quoted 7 lines
Quoted 7 lines
libdm/libdm-deptree.c +7 -0
+/* simplify string emiting code */
+#define EMIT_PARAMS(p, str...)\
+	do {\
+		const size_t bufsize = paramsize - (size_t)p;\
+		int w;\
+		\
+		if ((w = snprintf(params + p, bufsize, str)) < 0\
Quoted 9 lines

libdm/libdm-deptree.c line 1238

Quoted 7 lines
libdm/libdm-deptree.c +7 -0
+		    || ((size_t)w >= bufsize)) {\
+			stack; /* Out of space */\
+			return -1;\
+		}\
+		p += w;\
+	} while (0)
+

Do we have to do a macro here? Macros like this are harder to debug...

I think it's actually minimizing the chance you will add a buggy code by some cut&paste operation when you add new string emitting line.

Quoted 9 lines

Also it makes the code more readable.

Do you think there is some potential debug problem in this code, that makes worth to keep the original emitting style?

(i.e. replicator emits string 11 times - without this macro it makes the code even less readable, and also using inline function and 'stack' macro is not going to work together, so it's hard to use gdb-friendly inline here :( )

Quoted 5 lines

I agree something like this makes the code more readable - I was not suggesting keeping the original code. Was just wondering why you chose the macro vs a regular function. Is performance very critical here? Something to do with using va_list? Is there another reason for a macro I'm missing?

Inline function would not resolve the 'if (emit() < 0) { stack; return -1;}' repeating part of the code. So with inline we would have actually saved only 'p += w;' line

Eventually we could use macro 'IF_MEM_OK(emit())' to only hide 'stack; return' and keep emit() as a static incline function, but then there is no big victory against the current EMIT_PARAMS macro.

Zdenek