[PATCH] Two simplifications to loadparm

Volker Lendecke Volker.Lendecke at SerNet.DE
Fri Aug 21 09:00:20 UTC 2015


Hi!

Review&push appreciated!

Thanks,

Volker

-- 
SerNet GmbH, Bahnhofsallee 1b, 37081 Göttingen
phone: +49-551-370000-0, fax: +49-551-370000-9
AG Göttingen, HRB 2816, GF: Dr. Johannes Loxen
http://www.sernet.de, mailto:kontakt at sernet.de
-------------- next part --------------
From d711d72dde1b8278e3857e75f6a7a513dd2ed31a Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Mon, 17 Aug 2015 21:07:37 +0200
Subject: [PATCH 1/2] param: Simplify set_param_opt()

"not_added" is not a very good boolean flag concept... An early
return serves the same purpose just as well.

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/param/loadparm.c | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/lib/param/loadparm.c b/lib/param/loadparm.c
index 0a1c29a..c62e0de 100644
--- a/lib/param/loadparm.c
+++ b/lib/param/loadparm.c
@@ -802,10 +802,8 @@ void set_param_opt(TALLOC_CTX *mem_ctx,
 		   unsigned priority)
 {
 	struct parmlist_entry *new_opt, *opt;
-	bool not_added;
 
 	opt = *opt_list;
-	not_added = true;
 
 	/* Traverse destination */
 	while (opt) {
@@ -821,31 +819,29 @@ void set_param_opt(TALLOC_CTX *mem_ctx,
 			TALLOC_FREE(opt->list);
 			opt->value = talloc_strdup(opt, opt_value);
 			opt->priority = priority;
-			not_added = false;
-			break;
+			return;
 		}
 		opt = opt->next;
 	}
-	if (not_added) {
-		new_opt = talloc(mem_ctx, struct parmlist_entry);
-		if (new_opt == NULL) {
-			smb_panic("OOM");
-		}
 
-		new_opt->key = talloc_strdup(new_opt, opt_name);
-		if (new_opt->key == NULL) {
-			smb_panic("talloc_strdup failed");
-		}
+	new_opt = talloc(mem_ctx, struct parmlist_entry);
+	if (new_opt == NULL) {
+		smb_panic("OOM");
+	}
 
-		new_opt->value = talloc_strdup(new_opt, opt_value);
-		if (new_opt->value == NULL) {
-			smb_panic("talloc_strdup failed");
-		}
+	new_opt->key = talloc_strdup(new_opt, opt_name);
+	if (new_opt->key == NULL) {
+		smb_panic("talloc_strdup failed");
+	}
 
-		new_opt->list = NULL;
-		new_opt->priority = priority;
-		DLIST_ADD(*opt_list, new_opt);
+	new_opt->value = talloc_strdup(new_opt, opt_value);
+	if (new_opt->value == NULL) {
+		smb_panic("talloc_strdup failed");
 	}
+
+	new_opt->list = NULL;
+	new_opt->priority = priority;
+	DLIST_ADD(*opt_list, new_opt);
 }
 
 /**
-- 
2.4.6


From 4d7d17088aed95451e53c968d7cdc7ef98b90cc9 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Mon, 17 Aug 2015 21:12:56 +0200
Subject: [PATCH 2/2] param: Use talloc_pooled_object

Reduce memory fragmentation a bit and obsolete NULL checks

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/param/loadparm.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/lib/param/loadparm.c b/lib/param/loadparm.c
index c62e0de..a0700a9 100644
--- a/lib/param/loadparm.c
+++ b/lib/param/loadparm.c
@@ -824,20 +824,14 @@ void set_param_opt(TALLOC_CTX *mem_ctx,
 		opt = opt->next;
 	}
 
-	new_opt = talloc(mem_ctx, struct parmlist_entry);
+	new_opt = talloc_pooled_object(
+		mem_ctx, struct parmlist_entry,
+		2, strlen(opt_name) + 1 + strlen(opt_value) + 1);
 	if (new_opt == NULL) {
 		smb_panic("OOM");
 	}
-
 	new_opt->key = talloc_strdup(new_opt, opt_name);
-	if (new_opt->key == NULL) {
-		smb_panic("talloc_strdup failed");
-	}
-
 	new_opt->value = talloc_strdup(new_opt, opt_value);
-	if (new_opt->value == NULL) {
-		smb_panic("talloc_strdup failed");
-	}
 
 	new_opt->list = NULL;
 	new_opt->priority = priority;
-- 
2.4.6



More information about the samba-technical mailing list