svn commit: samba r25014 - in branches/SAMBA_4_0: . source/lib/util source/param

jelmer at samba.org jelmer at samba.org
Sat Sep 8 00:38:27 GMT 2007


Author: jelmer
Date: 2007-09-08 00:38:22 +0000 (Sat, 08 Sep 2007)
New Revision: 25014

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=25014

Log:
Use talloc for allocating values as well.
Modified:
   branches/SAMBA_4_0/
   branches/SAMBA_4_0/source/lib/util/util.h
   branches/SAMBA_4_0/source/lib/util/util_str.c
   branches/SAMBA_4_0/source/param/loadparm.c


Changeset:

Property changes on: branches/SAMBA_4_0
___________________________________________________________________
Name: bzr:revision-info
...skipped...
Name: bzr:revision-id:v3-trunk0
...skipped...

Modified: branches/SAMBA_4_0/source/lib/util/util.h
===================================================================
--- branches/SAMBA_4_0/source/lib/util/util.h	2007-09-08 00:07:50 UTC (rev 25013)
+++ branches/SAMBA_4_0/source/lib/util/util.h	2007-09-08 00:38:22 UTC (rev 25014)
@@ -371,15 +371,10 @@
 _PUBLIC_ char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len);
 
 /**
- Free a string value.
-**/
-_PUBLIC_ void string_free(char **s);
-
-/**
  Set a string value, deallocating any existing space, and allocing the space
  for the string
 **/
-_PUBLIC_ bool string_set(char **dest, const char *src);
+_PUBLIC_ bool string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src);
 
 /**
  Substitute a string for a pattern in another string. Make sure there is 

Modified: branches/SAMBA_4_0/source/lib/util/util_str.c
===================================================================
--- branches/SAMBA_4_0/source/lib/util/util_str.c	2007-09-08 00:07:50 UTC (rev 25013)
+++ branches/SAMBA_4_0/source/lib/util/util_str.c	2007-09-08 00:38:22 UTC (rev 25014)
@@ -260,39 +260,26 @@
 }
 
 /**
- Set a string value, allocing the space for the string
+ Set a string value, deallocating any existing space, and allocing the space
+ for the string
 **/
-static bool string_init(char **dest,const char *src)
+_PUBLIC_ bool string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
 {
-	if (!src) src = "";
+	talloc_free(*dest);
 
-	(*dest) = strdup(src);
+	if (src == NULL) 
+		src = "";
+
+	*dest = talloc_strdup(mem_ctx, src);
 	if ((*dest) == NULL) {
 		DEBUG(0,("Out of memory in string_init\n"));
 		return false;
 	}
+
 	return true;
 }
 
 /**
- Free a string value.
-**/
-_PUBLIC_ void string_free(char **s)
-{
-	if (s) SAFE_FREE(*s);
-}
-
-/**
- Set a string value, deallocating any existing space, and allocing the space
- for the string
-**/
-_PUBLIC_ bool string_set(char **dest, const char *src)
-{
-	string_free(dest);
-	return string_init(dest,src);
-}
-
-/**
  Substitute a string for a pattern in another string. Make sure there is 
  enough room!
 
@@ -304,7 +291,7 @@
  use of len==0 which was for no length checks to be done.
 **/
 
-_PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t len)
+_PUBLIC_ void string_sub(char *s, const char *pattern, const char *insert, size_t len)
 {
 	char *p;
 	ssize_t ls,lp,li, i;

Modified: branches/SAMBA_4_0/source/param/loadparm.c
===================================================================
--- branches/SAMBA_4_0/source/param/loadparm.c	2007-09-08 00:07:50 UTC (rev 25013)
+++ branches/SAMBA_4_0/source/param/loadparm.c	2007-09-08 00:38:22 UTC (rev 25014)
@@ -588,7 +588,8 @@
 		     parm_table[i].type == P_USTRING) &&
 		    parm_table[i].ptr &&
 		    !(parm_table[i].flags & FLAG_CMDLINE)) {
-			string_set(parm_table[i].ptr, "");
+			string_set(talloc_autofree_context(), 
+				   parm_table[i].ptr, "");
 		}
 	}
 
@@ -1180,6 +1181,14 @@
 	return pservice;
 }
 
+void string_free(char **str)
+{
+	if (str) {
+		talloc_free(*str);
+		*str = NULL;
+	}
+}
+
 /***************************************************************************
  Free the dynamically allocated parts of a service struct.
 ***************************************************************************/
@@ -1250,10 +1259,8 @@
 			/* They will be added during parsing again */
 			data = ServicePtrs[i]->param_opt;
 			while (data) {
-				string_free(&data->key);
-				string_free(&data->value);
 				pdata = data->next;
-				SAFE_FREE(data);
+				talloc_free(data);
 				data = pdata;
 			}
 			ServicePtrs[i]->param_opt = NULL;
@@ -1291,7 +1298,7 @@
 	}
 	copy_service(ServicePtrs[i], &tservice, NULL);
 	if (name != NULL)
-		string_set(&ServicePtrs[i]->szService, name);
+		string_set(ServicePtrs[i], &ServicePtrs[i]->szService, name);
 	return i;
 }
 
@@ -1319,13 +1326,13 @@
 		string_sub(newHomedir,"%H", pszHomedir, sizeof(newHomedir)); 
 	}
 
-	string_set(&ServicePtrs[i]->szPath, newHomedir);
+	string_set(ServicePtrs[i], &ServicePtrs[i]->szPath, newHomedir);
 
 	if (!(*(ServicePtrs[i]->comment))) {
-		pstring comment;
-		slprintf(comment, sizeof(comment) - 1,
+		char *comment = talloc_asprintf(ServicePtrs[i], 
 			 "Home directory of %s", user);
-		string_set(&ServicePtrs[i]->comment, comment);
+		string_set(ServicePtrs[i], &ServicePtrs[i]->comment, comment);
+		talloc_free(comment);
 	}
 	ServicePtrs[i]->bAvailable = sDefault.bAvailable;
 	ServicePtrs[i]->bBrowseable = sDefault.bBrowseable;
@@ -1357,15 +1364,15 @@
 	if (i < 0)
 		return false;
 
-	string_set(&ServicePtrs[i]->szPath, tmpdir());
+	string_set(ServicePtrs[i], &ServicePtrs[i]->szPath, tmpdir());
 
 	asprintf(&comment, "%s Service (%s)", fstype, Globals.szServerString);
 	if (comment == NULL)
 		return false;
 
-	string_set(&ServicePtrs[i]->comment, comment);
+	string_set(ServicePtrs[i], &ServicePtrs[i]->comment, comment);
 	SAFE_FREE(comment);
-	string_set(&ServicePtrs[i]->fstype, fstype);
+	string_set(ServicePtrs[i], &ServicePtrs[i]->fstype, fstype);
 	ServicePtrs[i]->iMaxConnections = -1;
 	ServicePtrs[i]->bAvailable = true;
 	ServicePtrs[i]->bRead_only = true;
@@ -1399,8 +1406,9 @@
 	/* entry (if/when the 'available' keyword is implemented!).    */
 
 	/* the printer name is set to the service name. */
-	string_set(&ServicePtrs[i]->szPrintername, pszPrintername);
-	string_set(&ServicePtrs[i]->comment, comment);
+	string_set(ServicePtrs[i], &ServicePtrs[i]->szPrintername, 
+		   pszPrintername);
+	string_set(ServicePtrs[i], &ServicePtrs[i]->comment, comment);
 	ServicePtrs[i]->bBrowseable = sDefault.bBrowseable;
 	/* Printers cannot be read_only. */
 	ServicePtrs[i]->bRead_only = False;
@@ -1514,12 +1522,12 @@
 					break;
 
 				case P_STRING:
-					string_set(dest_ptr,
+					string_set(pserviceDest, dest_ptr,
 						   *(char **)src_ptr);
 					break;
 
 				case P_USTRING:
-					string_set(dest_ptr,
+					string_set(pserviceDest, dest_ptr,
 						   *(char **)src_ptr);
 					strupper(*(char **)dest_ptr);
 					break;
@@ -1549,18 +1557,19 @@
 			/* If we already have same option, override it */
 			if (strcmp(pdata->key, data->key) == 0) {
 				string_free(&pdata->value);
-				pdata->value = strdup(data->value);
-				not_added = False;
+				pdata->value = talloc_reference(pdata, 
+							     data->value);
+				not_added = false;
 				break;
 			}
 			pdata = pdata->next;
 		}
 		if (not_added) {
-			paramo = malloc_p(struct param_opt);
-			if (!paramo)
+			paramo = talloc(pserviceDest, struct param_opt);
+			if (paramo == NULL)
 				smb_panic("OOM");
-			paramo->key = strdup(data->key);
-			paramo->value = strdup(data->value);
+			paramo->key = talloc_reference(paramo, data->key);
+			paramo->value = talloc_reference(paramo, data->value);
 			DLIST_ADD(pserviceDest->param_opt, paramo);
 		}
 		data = data->next;
@@ -1694,7 +1703,7 @@
 
 	add_to_file_list(pszParmValue, fname);
 
-	string_set(ptr, fname);
+	string_set(talloc_autofree_context(), ptr, fname);
 
 	if (file_exist(fname))
 		return pm_process(fname, do_section, do_parameter, NULL);
@@ -1714,7 +1723,7 @@
 	int iTemp;
 	service *serviceTemp;
 
-	string_set(ptr, pszParmValue);
+	string_set(talloc_autofree_context(), ptr, pszParmValue);
 
 	serviceTemp = init_service(talloc_autofree_context());
 
@@ -1778,6 +1787,7 @@
 {
 	struct param_opt *paramo, *data;
 	char *name;
+	TALLOC_CTX *mem_ctx;
 
 	while (isspace((unsigned char)*pszParmName)) {
 		pszParmName++;
@@ -1790,8 +1800,10 @@
 
 	if (snum < 0) {
 		data = Globals.param_opt;
+		mem_ctx = talloc_autofree_context();
 	} else {
 		data = ServicePtrs[snum]->param_opt;
+		mem_ctx = ServicePtrs[snum];
 	}
 
 	/* Traverse destination */
@@ -1804,19 +1816,19 @@
 				return True;
 			}
 
-			free(paramo->value);
-			paramo->value = strdup(pszParmValue);
+			talloc_free(paramo->value);
+			paramo->value = talloc_strdup(paramo, pszParmValue);
 			paramo->flags = flags;
 			free(name);
 			return True;
 		}
 	}
 
-	paramo = malloc_p(struct param_opt);
+	paramo = talloc(mem_ctx, struct param_opt);
 	if (!paramo)
 		smb_panic("OOM");
-	paramo->key = strdup(name);
-	paramo->value = strdup(pszParmValue);
+	paramo->key = talloc_strdup(paramo, name);
+	paramo->value = talloc_strdup(paramo, pszParmValue);
 	paramo->flags = flags;
 	if (snum < 0) {
 		DLIST_ADD(Globals.param_opt, paramo);
@@ -1826,18 +1838,19 @@
 
 	free(name);
 	
-	return True;
+	return true;
 }
 
 /***************************************************************************
  Process a parameter for a particular service number. If snum < 0
  then assume we are in the globals.
 ***************************************************************************/
-BOOL lp_do_parameter(int snum, const char *pszParmName, const char *pszParmValue)
+bool lp_do_parameter(int snum, const char *pszParmName, const char *pszParmValue)
 {
 	int parmnum, i;
 	void *parm_ptr = NULL;	/* where we are going to store the result */
 	void *def_ptr = NULL;
+	TALLOC_CTX *mem_ctx;
 
 	parmnum = map_parameter(pszParmName);
 
@@ -1865,6 +1878,7 @@
 	/* we might point at a service, the default service or a global */
 	if (snum < 0) {
 		parm_ptr = def_ptr;
+		mem_ctx = talloc_autofree_context();
 	} else {
 		if (parm_table[parmnum].class == P_GLOBAL) {
 			DEBUG(0,
@@ -1875,6 +1889,7 @@
 		parm_ptr =
 			((char *)ServicePtrs[snum]) + PTR_DIFF(def_ptr,
 							    &sDefault);
+		mem_ctx = ServicePtrs[snum];
 	}
 
 	if (snum >= 0) {
@@ -1931,16 +1946,16 @@
 		}
 
 		case P_LIST:
-			*(const char ***)parm_ptr = str_list_make(talloc_autofree_context(), 
+			*(const char ***)parm_ptr = str_list_make(mem_ctx, 
 								  pszParmValue, NULL);
 			break;
 
 		case P_STRING:
-			string_set(parm_ptr, pszParmValue);
+			string_set(mem_ctx, parm_ptr, pszParmValue);
 			break;
 
 		case P_USTRING:
-			string_set(parm_ptr, pszParmValue);
+			string_set(mem_ctx, parm_ptr, pszParmValue);
 			strupper(*(char **)parm_ptr);
 			break;
 
@@ -2481,10 +2496,8 @@
 		for (data=Globals.param_opt; data; data=next) {
 			next = data->next;
 			if (data->flags & FLAG_CMDLINE) continue;
-			free(data->key);
-			free(data->value);
 			DLIST_REMOVE(Globals.param_opt, data);
-			free(data);
+			talloc_free(data);
 		}
 	}
 	



More information about the samba-cvs mailing list