Skip to contentSkip to conversation

isdn_bsdcomp.c - vfree() checking cleanups

PATCH In review [email protected] 3 participants 5 messages Mar 6, 2005 – Mar 7, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/8 isdn_bsdcomp.c - vfree() checking cleanups

isdn_bsdcomp.c vfree() checking cleanups.

Signed-off by: James Lamanna <[email protected]>

---

 kj-domen/drivers/isdn/i4l/isdn_bsdcomp.c |   12 ++++--------
 1 files changed, 4 insertions(+), 8 deletions(-)
drivers/isdn/i4l/isdn_bsdcomp.c renamed from drivers/isdn/i4l/isdn_bsdcomp.c~vfree-drivers_isdn_i4l_isdn_bsdcomp +4 -8
diff -puN drivers/isdn/i4l/isdn_bsdcomp.c~vfree-drivers_isdn_i4l_isdn_bsdcomp drivers/isdn/i4l/isdn_bsdcomp.c
@@ -283,18 +283,14 @@ static void bsd_free (void *state)
 		/*
 		 * Release the dictionary
 		 */
-		if (db->dict) {
-			vfree (db->dict);
-			db->dict = NULL;
-		}
+		vfree (db->dict);
+		db->dict = NULL;
 
 		/*
 		 * Release the string buffer
 		 */
-		if (db->lens) {
-			vfree (db->lens);
-			db->lens = NULL;
-		}
+		vfree (db->lens);
+		db->lens = NULL;
 
 		/*
 		 * Finally release the structure itself.

_

#1 lore raw

Hi Domen,

drivers/isdn/i4l/isdn_bsdcomp.c static void bsd_free (void *state) · line 287

Quoted 6 lines
drivers/isdn/i4l/isdn_bsdcomp.c renamed from drivers/isdn/i4l/isdn_bsdcomp.c~vfree-drivers_isdn_i4l_isdn_bsdcomp +2 -4
-		if (db->dict) {
-			vfree (db->dict);
-			db->dict = NULL;
-		}
+		vfree (db->dict);
+		db->dict = NULL;

Is it really worth always calling vfree() which calls __vunmap() before db->dict is determined to be NULL in order to turn three lines into two? Plus the write to db->dict which might otherwise not be needed. The old code was clear, clean, and fast, no?

Cheers,

Ralph.

#2 lore raw
Quoted 12 lines · Ralph Corderoy

Hi Domen,

drivers/isdn/i4l/isdn_bsdcomp.c static void bsd_free (void *state) · line 287

Quoted 6 lines
drivers/isdn/i4l/isdn_bsdcomp.c renamed from drivers/isdn/i4l/isdn_bsdcomp.c~vfree-drivers_isdn_i4l_isdn_bsdcomp +2 -4
-		if (db->dict) {
-			vfree (db->dict);
-			db->dict = NULL;
-		}
+		vfree (db->dict);
+		db->dict = NULL;

Is it really worth always calling vfree() which calls __vunmap() before db->dict is determined to be NULL in order to turn three lines into two?

Four lines into two :-)

Quoted 2 lines

Plus the write to db->dict which might otherwise not be needed. The old code was clear, clean, and fast, no?

Shorter and more readable code is always better, right? And speed really doesn't seem to be an issue here.

Domen

#3 lore raw
Quoted 14 lines · Domen Puncer
Quoted 12 lines · Ralph Corderoy

Hi Domen,

drivers/isdn/i4l/isdn_bsdcomp.c static void bsd_free (void *state) · line 287

Quoted 6 lines
drivers/isdn/i4l/isdn_bsdcomp.c renamed from drivers/isdn/i4l/isdn_bsdcomp.c~vfree-drivers_isdn_i4l_isdn_bsdcomp +2 -4
-		if (db->dict) {
-			vfree (db->dict);
-			db->dict = NULL;
-		}
+		vfree (db->dict);
+		db->dict = NULL;

Is it really worth always calling vfree() which calls __vunmap() before db->dict is determined to be NULL in order to turn three lines into two?

Quoted 8 lines

Four lines into two :-)

Quoted 2 lines

Plus the write to db->dict which might otherwise not be needed. The old code was clear, clean, and fast, no?

Shorter and more readable code is always better, right? And speed really doesn't seem to be an issue here.

I also prefer the old code, since it make clear, that you must be careful here, since the function can be called with already freed db->dict, and for me this version is not better readable as the old one.

Signature · 3 lines
Karsten Keil
SuSE Labs
ISDN development
#4 lore raw
In reply to Domen Puncer

Hi Domen,

Quoted 12 lines · Ralph Corderoy
Quoted 10 lines

drivers/isdn/i4l/isdn_bsdcomp.c static void bsd_free (void *state) · line 287

Quoted 6 lines
drivers/isdn/i4l/isdn_bsdcomp.c renamed from drivers/isdn/i4l/isdn_bsdcomp.c~vfree-drivers_isdn_i4l_isdn_bsdcomp +2 -4
-		if (db->dict) {
-			vfree (db->dict);
-			db->dict = NULL;
-		}
+		vfree (db->dict);
+		db->dict = NULL;

Is it really worth always calling vfree() which calls __vunmap() before db->dict is determined to be NULL in order to turn three lines into two?

Quoted 6 lines

Four lines into two :-)

Quoted 2 lines

Plus the write to db->dict which might otherwise not be needed. The old code was clear, clean, and fast, no?

Shorter and more readable code is always better, right?

No. Let me try and persuade you.

There's three cases.

1. foo will always be NULL at the line in question so no need to `vfree(foo); foo = NULL;'.

2. foo will never be NULL at the line in question so `vfree(foo); foo = NULL;' is mandatory.

3. foo will sometimes be NULL, sometimes not.

In that third case, seeing

    if (foo) {
        vfree(foo);
        foo = NULL;
    }

tells the reader that we're dealing with the `foo *maybe* NULL' case whereas

    vfree(foo);
    foo = NULL;

*suggests* the `foo is never NULL' case. The reader has to remember that vfree(NULL) is valid and bear that in mind.

If the reader is about to modify the preceeding lines, knowing the foo may sometimes be NULL helps. So I prefer the longer code here because it better shows the intent of the coder and is more telling about the state of db->dict.

If you're really keen to save lines, please axe the superfluous three-line comments. We read C here, they're unnecessary.

Quoted 12 lines
drivers/isdn/i4l/isdn_bsdcomp.c renamed from drivers/isdn/i4l/isdn_bsdcomp.c~vfree-drivers_isdn_i4l_isdn_bsdcomp +2 -4
 		/*
 		 * Release the dictionary
 		 */
-		if (db->dict) {
-			vfree (db->dict);
-			db->dict = NULL;
-		}
+		vfree (db->dict);
+		db->dict = NULL;
 
 		/*
 		 * Release the string buffer

drivers/isdn/i4l/isdn_bsdcomp.c static void bsd_free (void *state) · line 296

Quoted 10 lines
drivers/isdn/i4l/isdn_bsdcomp.c renamed from drivers/isdn/i4l/isdn_bsdcomp.c~vfree-drivers_isdn_i4l_isdn_bsdcomp +2 -4
 		 */
-		if (db->lens) {
-			vfree (db->lens);
-			db->lens = NULL;
-		}
+		vfree (db->lens);
+		db->lens = NULL;
 
 		/*
 		 * Finally release the structure itself.

Cheers,

Ralph.