[dpdk-dev,1/5] eal: refactor plugin list append from eal_parse_args() to a helper function

Message ID e18b068e2e25a82fb1d321a4bd4f32d41c6d83e6.1444996480.git.pmatilai@redhat.com (mailing list archive)
State Superseded, archived
Headers

Commit Message

Panu Matilainen Oct. 16, 2015, 11:58 a.m. UTC
  Signed-off-by: Panu Matilainen <pmatilai@redhat.com>
---
 lib/librte_eal/linuxapp/eal/eal.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)
  

Comments

Bruce Richardson Oct. 16, 2015, 12:57 p.m. UTC | #1
On Fri, Oct 16, 2015 at 02:58:13PM +0300, Panu Matilainen wrote:
> Signed-off-by: Panu Matilainen <pmatilai@redhat.com>
> ---
>  lib/librte_eal/linuxapp/eal/eal.c | 28 +++++++++++++++++++---------
>  1 file changed, 19 insertions(+), 9 deletions(-)
> 
> diff --git a/lib/librte_eal/linuxapp/eal/eal.c b/lib/librte_eal/linuxapp/eal/eal.c
> index 33e1067..cc66d9f 100644
> --- a/lib/librte_eal/linuxapp/eal/eal.c
> +++ b/lib/librte_eal/linuxapp/eal/eal.c
> @@ -530,6 +530,24 @@ eal_log_level_parse(int argc, char **argv)
>  	optind = 0; /* reset getopt lib */
>  }
>  
> +static int
> +eal_plugin_add(const char *path)
> +{
> +	struct shared_driver *solib;
> +
> +	solib = malloc(sizeof(*solib));
> +	if (solib == NULL) {
> +		RTE_LOG(ERR, EAL, "malloc(solib) failed\n");
> +		return -1;
> +	}
> +	memset(solib, 0, sizeof(*solib));
> +	strncpy(solib->name, path, PATH_MAX-1);
> +	solib->name[PATH_MAX-1] = 0;

I always prefer a one-line snprintf to the above two-line code. :-)

/Bruce
  
Panu Matilainen Oct. 16, 2015, 1:07 p.m. UTC | #2
On 10/16/2015 03:57 PM, Bruce Richardson wrote:
> On Fri, Oct 16, 2015 at 02:58:13PM +0300, Panu Matilainen wrote:
>> Signed-off-by: Panu Matilainen <pmatilai@redhat.com>
>> ---
>>   lib/librte_eal/linuxapp/eal/eal.c | 28 +++++++++++++++++++---------
>>   1 file changed, 19 insertions(+), 9 deletions(-)
>>
>> diff --git a/lib/librte_eal/linuxapp/eal/eal.c b/lib/librte_eal/linuxapp/eal/eal.c
>> index 33e1067..cc66d9f 100644
>> --- a/lib/librte_eal/linuxapp/eal/eal.c
>> +++ b/lib/librte_eal/linuxapp/eal/eal.c
>> @@ -530,6 +530,24 @@ eal_log_level_parse(int argc, char **argv)
>>   	optind = 0; /* reset getopt lib */
>>   }
>>
>> +static int
>> +eal_plugin_add(const char *path)
>> +{
>> +	struct shared_driver *solib;
>> +
>> +	solib = malloc(sizeof(*solib));
>> +	if (solib == NULL) {
>> +		RTE_LOG(ERR, EAL, "malloc(solib) failed\n");
>> +		return -1;
>> +	}
>> +	memset(solib, 0, sizeof(*solib));
>> +	strncpy(solib->name, path, PATH_MAX-1);
>> +	solib->name[PATH_MAX-1] = 0;
>
> I always prefer a one-line snprintf to the above two-line code. :-)

Me too (or asprintf, depending on situation), but the point of this 
patch is to move around existing code without changing it.
Certainly I can change it to sprintf if that's preferred.

	- Panu -
  

Patch

diff --git a/lib/librte_eal/linuxapp/eal/eal.c b/lib/librte_eal/linuxapp/eal/eal.c
index 33e1067..cc66d9f 100644
--- a/lib/librte_eal/linuxapp/eal/eal.c
+++ b/lib/librte_eal/linuxapp/eal/eal.c
@@ -530,6 +530,24 @@  eal_log_level_parse(int argc, char **argv)
 	optind = 0; /* reset getopt lib */
 }
 
+static int
+eal_plugin_add(const char *path)
+{
+	struct shared_driver *solib;
+
+	solib = malloc(sizeof(*solib));
+	if (solib == NULL) {
+		RTE_LOG(ERR, EAL, "malloc(solib) failed\n");
+		return -1;
+	}
+	memset(solib, 0, sizeof(*solib));
+	strncpy(solib->name, path, PATH_MAX-1);
+	solib->name[PATH_MAX-1] = 0;
+	TAILQ_INSERT_TAIL(&solib_list, solib, next);
+
+	return 0;
+}
+
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
@@ -538,7 +556,6 @@  eal_parse_args(int argc, char **argv)
 	char **argvopt;
 	int option_index;
 	char *prgname = argv[0];
-	struct shared_driver *solib;
 
 	argvopt = argv;
 
@@ -570,15 +587,8 @@  eal_parse_args(int argc, char **argv)
 
 		/* force loading of external driver */
 		case 'd':
-			solib = malloc(sizeof(*solib));
-			if (solib == NULL) {
-				RTE_LOG(ERR, EAL, "malloc(solib) failed\n");
+			if (eal_plugin_add(optarg) == -1)
 				return -1;
-			}
-			memset(solib, 0, sizeof(*solib));
-			strncpy(solib->name, optarg, PATH_MAX-1);
-			solib->name[PATH_MAX-1] = 0;
-			TAILQ_INSERT_TAIL(&solib_list, solib, next);
 			break;
 
 		/* long options */