From: Jacek Piasecki <jacekx.piasecki@intel.com>
To: dev@dpdk.org
Cc: bruce.richardson@intel.com, deepak.k.jain@intel.com,
kubax.kozak@intel.com, michalx.k.jastrzebski@intel.com,
Jacek Piasecki <jacekx.piasecki@intel.com>
Subject: [dpdk-dev] [PATCH v4 2/5] cfgfile: change existing API functions
Date: Mon, 10 Jul 2017 14:44:14 +0200 [thread overview]
Message-ID: <1499690657-81150-3-git-send-email-jacekx.piasecki@intel.com> (raw)
In-Reply-To: <1499690657-81150-1-git-send-email-jacekx.piasecki@intel.com>
Change to flat arrays in cfgfile struct force slightly
diffrent data access for most of cfgfile functions.
This patch provides necessary changes in existing API.
Signed-off-by: Jacek Piasecki <jacekx.piasecki@intel.com>
---
lib/librte_cfgfile/rte_cfgfile.c | 120 +++++++++++++++++++--------------------
lib/librte_cfgfile/rte_cfgfile.h | 6 +-
2 files changed, 62 insertions(+), 64 deletions(-)
diff --git a/lib/librte_cfgfile/rte_cfgfile.c b/lib/librte_cfgfile/rte_cfgfile.c
index c6ae3e3..50fe37a 100644
--- a/lib/librte_cfgfile/rte_cfgfile.c
+++ b/lib/librte_cfgfile/rte_cfgfile.c
@@ -35,6 +35,7 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
+#include <errno.h>
#include <rte_common.h>
#include "rte_cfgfile.h"
@@ -42,13 +43,15 @@
struct rte_cfgfile_section {
char name[CFG_NAME_LEN];
int num_entries;
- struct rte_cfgfile_entry *entries[0];
+ int allocated_entries;
+ struct rte_cfgfile_entry *entries;
};
struct rte_cfgfile {
int flags;
int num_sections;
- struct rte_cfgfile_section *sections[0];
+ int allocated_sections;
+ struct rte_cfgfile_section *sections;
};
/** when we resize a file structure, how many extra entries
@@ -104,6 +107,19 @@ _strip(char *str, unsigned len)
return newlen;
}
+static struct rte_cfgfile_section *
+_get_section(struct rte_cfgfile *cfg, const char *sectionname)
+{
+ int i;
+
+ for (i = 0; i < cfg->num_sections; i++) {
+ if (strncmp(cfg->sections[i].name, sectionname,
+ sizeof(cfg->sections[0].name)) == 0)
+ return &cfg->sections[i];
+ }
+ return NULL;
+}
+
static int
rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
{
@@ -168,17 +184,17 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
if (flags & CFG_FLAG_GLOBAL_SECTION) {
curr_section = 0;
allocated_entries = CFG_ALLOC_ENTRY_BATCH;
- cfg->sections[curr_section] = malloc(
- sizeof(*cfg->sections[0]) +
- sizeof(cfg->sections[0]->entries[0]) *
+ cfg->sections = malloc(
+ sizeof(cfg->sections[0]) +
+ sizeof(cfg->sections[0].entries) *
allocated_entries);
- if (cfg->sections[curr_section] == NULL) {
+ if (cfg->sections == NULL) {
printf("Error - no memory for global section\n");
goto error1;
}
- snprintf(cfg->sections[curr_section]->name,
- sizeof(cfg->sections[0]->name), "GLOBAL");
+ snprintf(cfg->sections[curr_section].name,
+ sizeof(cfg->sections[0].name), "GLOBAL");
}
while (fgets(buffer, sizeof(buffer), f) != NULL) {
@@ -213,7 +229,7 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
/* close off old section and add start new one */
if (curr_section >= 0)
- cfg->sections[curr_section]->num_entries =
+ cfg->sections[curr_section].num_entries =
curr_entry + 1;
curr_section++;
@@ -235,17 +251,17 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
/* allocate space for new section */
allocated_entries = CFG_ALLOC_ENTRY_BATCH;
curr_entry = -1;
- cfg->sections[curr_section] = malloc(
- sizeof(*cfg->sections[0]) +
- sizeof(cfg->sections[0]->entries[0]) *
+ cfg->sections = malloc(
+ sizeof(cfg->sections[0]) +
+ sizeof(cfg->sections[0].entries) *
allocated_entries);
- if (cfg->sections[curr_section] == NULL) {
+ if (cfg->sections == NULL) {
printf("Error - no more memory\n");
goto error1;
}
- snprintf(cfg->sections[curr_section]->name,
- sizeof(cfg->sections[0]->name),
+ snprintf(cfg->sections[curr_section].name,
+ sizeof(cfg->sections[0].name),
"%s", &buffer[1]);
} else {
/* value line */
@@ -255,8 +271,7 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
goto error1;
}
- struct rte_cfgfile_section *sect =
- cfg->sections[curr_section];
+ struct rte_cfgfile_section *sect = cfg->sections;
char *split[2] = {NULL};
split[0] = buffer;
@@ -292,18 +307,17 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
printf("Error - no more memory\n");
goto error1;
}
- sect = cfg->sections[curr_section] = n_sect;
+ sect = cfg->sections = n_sect;
}
- sect->entries[curr_entry] = malloc(
- sizeof(*sect->entries[0]));
- if (sect->entries[curr_entry] == NULL) {
+ sect->entries = malloc(
+ sizeof(sect->entries[0]));
+ if (sect->entries == NULL) {
printf("Error - no more memory\n");
goto error1;
}
- struct rte_cfgfile_entry *entry = sect->entries[
- curr_entry];
+ struct rte_cfgfile_entry *entry = sect->entries;
snprintf(entry->name, sizeof(entry->name), "%s",
split[0]);
snprintf(entry->value, sizeof(entry->value), "%s",
@@ -319,42 +333,38 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
cfg->num_sections = curr_section + 1;
/* curr_section will still be -1 if we have an empty file */
if (curr_section >= 0)
- cfg->sections[curr_section]->num_entries = curr_entry + 1;
+ cfg->sections[curr_section].num_entries = curr_entry + 1;
return cfg;
error1:
cfg->num_sections = curr_section + 1;
if (curr_section >= 0)
- cfg->sections[curr_section]->num_entries = curr_entry + 1;
+ cfg->sections[curr_section].num_entries = curr_entry + 1;
rte_cfgfile_close(cfg);
error2:
fclose(f);
return NULL;
}
-
int rte_cfgfile_close(struct rte_cfgfile *cfg)
{
- int i, j;
+ int i;
if (cfg == NULL)
return -1;
- for (i = 0; i < cfg->num_sections; i++) {
- if (cfg->sections[i] != NULL) {
- if (cfg->sections[i]->num_entries) {
- for (j = 0; j < cfg->sections[i]->num_entries;
- j++) {
- if (cfg->sections[i]->entries[j] !=
- NULL)
- free(cfg->sections[i]->
- entries[j]);
- }
+ if (cfg->sections != NULL) {
+ for (i = 0; i < cfg->allocated_sections; i++) {
+ if (cfg->sections[i].entries != NULL) {
+ free(cfg->sections[i].entries);
+ cfg->sections[i].entries = NULL;
}
- free(cfg->sections[i]);
}
+ free(cfg->sections);
+ cfg->sections = NULL;
}
free(cfg);
+ cfg = NULL;
return 0;
}
@@ -366,7 +376,7 @@ size_t length)
int i;
int num_sections = 0;
for (i = 0; i < cfg->num_sections; i++) {
- if (strncmp(cfg->sections[i]->name, sectionname, length) == 0)
+ if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
num_sections++;
}
return num_sections;
@@ -380,23 +390,11 @@ rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
for (i = 0; i < cfg->num_sections && i < max_sections; i++)
snprintf(sections[i], CFG_NAME_LEN, "%s",
- cfg->sections[i]->name);
+ cfg->sections[i].name);
return i;
}
-static const struct rte_cfgfile_section *
-_get_section(struct rte_cfgfile *cfg, const char *sectionname)
-{
- int i;
- for (i = 0; i < cfg->num_sections; i++) {
- if (strncmp(cfg->sections[i]->name, sectionname,
- sizeof(cfg->sections[0]->name)) == 0)
- return cfg->sections[i];
- }
- return NULL;
-}
-
int
rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
{
@@ -409,7 +407,7 @@ rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
{
const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
if (s == NULL)
- return -1;
+ return -EINVAL;
return s->num_entries;
}
@@ -421,9 +419,9 @@ rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
int i;
const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
if (sect == NULL)
- return -1;
+ return -EINVAL;
for (i = 0; i < max_entries && i < sect->num_entries; i++)
- entries[i] = *sect->entries[i];
+ entries[i] = sect->entries[i];
return i;
}
@@ -436,12 +434,12 @@ rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
const struct rte_cfgfile_section *sect;
if (index < 0 || index >= cfg->num_sections)
- return -1;
+ return -EINVAL;
- sect = cfg->sections[index];
+ sect = &cfg->sections[index];
snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
for (i = 0; i < max_entries && i < sect->num_entries; i++)
- entries[i] = *sect->entries[i];
+ entries[i] = sect->entries[i];
return i;
}
@@ -454,9 +452,9 @@ rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
if (sect == NULL)
return NULL;
for (i = 0; i < sect->num_entries; i++)
- if (strncmp(sect->entries[i]->name, entryname, CFG_NAME_LEN)
- == 0)
- return sect->entries[i]->value;
+ if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
+ == 0)
+ return sect->entries[i].value;
return NULL;
}
diff --git a/lib/librte_cfgfile/rte_cfgfile.h b/lib/librte_cfgfile/rte_cfgfile.h
index fa10d40..4702110 100644
--- a/lib/librte_cfgfile/rte_cfgfile.h
+++ b/lib/librte_cfgfile/rte_cfgfile.h
@@ -178,7 +178,7 @@ int rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname);
* @param sectionname
* Section name
* @return
-* Number of entries in section on success, -1 otherwise
+* Number of entries in section on success, -EINVAL otherwise
*/
int rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
const char *sectionname);
@@ -199,7 +199,7 @@ int rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
* @param max_entries
* Maximum number of section entries to be stored in entries array
* @return
-* Number of entries populated on success, -1 otherwise
+* Number of entries populated on success, -EINVAL otherwise
*/
int rte_cfgfile_section_entries(struct rte_cfgfile *cfg,
const char *sectionname,
@@ -226,7 +226,7 @@ int rte_cfgfile_section_entries(struct rte_cfgfile *cfg,
* @param max_entries
* Maximum number of section entries to be stored in entries array
* @return
-* Number of entries populated on success, -1 otherwise
+* Number of entries populated on success, -EINVAL otherwise
*/
int rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg,
int index,
--
2.7.4
next prev parent reply other threads:[~2017-07-10 12:59 UTC|newest]
Thread overview: 70+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-30 8:30 [dpdk-dev] [PATCH 0/3] Add support for using a config file for DPDK Jacek Piasecki
2017-05-30 8:30 ` [dpdk-dev] [PATCH 1/3] cfgfile: add new API functions Jacek Piasecki
2017-05-31 14:20 ` Bruce Richardson
2017-05-31 14:22 ` Bruce Richardson
2017-06-26 10:59 ` [dpdk-dev] [PATCH v2 0/7] Add support for using a config file for DPDK Jacek Piasecki
2017-06-26 10:59 ` [dpdk-dev] [PATCH v2 1/7] cfgfile: remove EAL dependency Jacek Piasecki
2017-06-26 13:12 ` Dumitrescu, Cristian
2017-06-27 10:26 ` [dpdk-dev] [PATCH v3 0/4] Rework cfgfile API to enable apps config file support Jacek Piasecki
2017-06-27 10:26 ` [dpdk-dev] [PATCH v3 1/4] cfgfile: remove EAL dependency Jacek Piasecki
2017-06-30 9:44 ` Bruce Richardson
2017-06-30 11:16 ` Bruce Richardson
2017-06-27 10:26 ` [dpdk-dev] [PATCH v3 2/4] cfgfile: add new functions to API Jacek Piasecki
2017-06-30 9:55 ` Bruce Richardson
2017-06-30 10:28 ` Bruce Richardson
2017-06-27 10:26 ` [dpdk-dev] [PATCH v3 3/4] cfgfile: rework of load function Jacek Piasecki
2017-06-30 11:18 ` Bruce Richardson
2017-06-27 10:26 ` [dpdk-dev] [PATCH v3 4/4] test/cfgfile: add new unit test Jacek Piasecki
2017-06-30 11:20 ` [dpdk-dev] [PATCH v3 0/4] Rework cfgfile API to enable apps config file support Bruce Richardson
2017-06-26 10:59 ` [dpdk-dev] [PATCH v2 2/7] cfgfile: add new functions to API Jacek Piasecki
2017-06-26 10:59 ` [dpdk-dev] [PATCH v2 3/7] cfgfile: rework of load function Jacek Piasecki
2017-06-26 10:59 ` [dpdk-dev] [PATCH v2 4/7] test/cfgfile: add new unit test Jacek Piasecki
2017-06-26 10:59 ` [dpdk-dev] [PATCH v2 5/7] eal: add functions parsing EAL arguments Jacek Piasecki
2017-06-27 10:52 ` [dpdk-dev] [PATCH v3 0/3] EAL change for using a config file for DPDK Jacek Piasecki
2017-06-27 10:52 ` [dpdk-dev] [PATCH v3 1/3] eal: add functions parsing EAL arguments Jacek Piasecki
2017-06-30 16:04 ` Bruce Richardson
2017-07-10 12:44 ` [dpdk-dev] [PATCH v4 0/5] Rework cfgfile API to enable apps config file support Jacek Piasecki
2017-07-10 12:44 ` [dpdk-dev] [PATCH v4 1/5] cfgfile: remove EAL dependency Jacek Piasecki
2017-08-30 17:58 ` Bruce Richardson
2017-07-10 12:44 ` Jacek Piasecki [this message]
2017-08-30 20:07 ` [dpdk-dev] [PATCH v4 2/5] cfgfile: change existing API functions Bruce Richardson
2017-07-10 12:44 ` [dpdk-dev] [PATCH v4 3/5] cfgfile: add new functions to API Jacek Piasecki
2017-08-30 20:18 ` Bruce Richardson
2017-07-10 12:44 ` [dpdk-dev] [PATCH v4 4/5] cfgfile: rework of load function Jacek Piasecki
2017-08-30 20:24 ` Bruce Richardson
2017-07-10 12:44 ` [dpdk-dev] [PATCH v4 5/5] test/cfgfile: add new unit test Jacek Piasecki
2017-08-30 20:25 ` Bruce Richardson
2017-09-04 9:21 ` Bruce Richardson
2017-09-04 9:30 ` Bruce Richardson
2017-09-15 13:56 ` Thomas Monjalon
2017-09-18 13:49 ` Jastrzebski, MichalX K
2017-07-10 15:13 ` [dpdk-dev] [PATCH v4 0/5] Rework cfgfile API to enable apps config file support Thomas Monjalon
2017-07-20 21:48 ` Thomas Monjalon
2017-07-10 12:51 ` [dpdk-dev] [PATCH v4 0/3] EAL change for using a config file for DPDK Kuba Kozak
2017-07-10 12:51 ` [dpdk-dev] [PATCH v4 1/3] eal: add functions parsing EAL arguments Kuba Kozak
2017-07-13 9:26 ` [dpdk-dev] [PATCH v5 0/3] EAL change for using a config file for DPDK Kuba Kozak
2017-07-13 10:07 ` Kuba Kozak
2017-07-13 10:07 ` [dpdk-dev] [PATCH v5 1/3] eal: add functions parsing EAL arguments Kuba Kozak
2017-07-13 10:07 ` [dpdk-dev] [PATCH v5 2/3] app/testpmd: add parse options from cfg file Kuba Kozak
2017-07-13 10:07 ` [dpdk-dev] [PATCH v5 3/3] app/testpmd: add parse options from JSON " Kuba Kozak
2019-01-23 19:31 ` [dpdk-dev] [PATCH v5 0/3] EAL change for using a config file for DPDK Ferruh Yigit
2019-01-23 20:26 ` Thomas Monjalon
2019-01-24 13:54 ` Ferruh Yigit
2019-01-24 14:32 ` Thomas Monjalon
2019-01-24 14:46 ` Ferruh Yigit
2019-01-24 16:06 ` Thomas Monjalon
2019-01-24 16:18 ` Ferruh Yigit
2019-01-24 17:45 ` Thomas Monjalon
2019-01-28 14:43 ` Ferruh Yigit
2017-07-10 12:51 ` [dpdk-dev] [PATCH v4 2/3] app/testpmd: add parse options from cfg file Kuba Kozak
2017-07-10 12:51 ` [dpdk-dev] [PATCH v4 3/3] app/testpmd: add parse options from JSON " Kuba Kozak
2017-06-27 10:52 ` [dpdk-dev] [PATCH v3 2/3] app/testpmd: changed example to parse options from " Jacek Piasecki
2017-06-27 10:52 ` [dpdk-dev] [PATCH v3 3/3] app/testpmd: add parse arguments from JSON config file Jacek Piasecki
2017-07-05 0:00 ` [dpdk-dev] [PATCH v3 0/3] EAL change for using a config file for DPDK Thomas Monjalon
2017-06-26 10:59 ` [dpdk-dev] [PATCH v2 6/7] app/testpmd: changed example to parse options from cfg file Jacek Piasecki
2017-06-26 10:59 ` [dpdk-dev] [PATCH v2 7/7] app/testpmd: add parse arguments from JSON config file Jacek Piasecki
2017-05-30 8:30 ` [dpdk-dev] [PATCH 2/3] eal: add functions parsing EAL arguments Jacek Piasecki
2017-05-31 15:46 ` Bruce Richardson
2017-05-30 8:30 ` [dpdk-dev] [PATCH 3/3] app/testpmd: changed example to parse options from cfg file Jacek Piasecki
2017-06-20 2:13 ` Wu, Jingjing
2017-06-20 10:10 ` Kozak, KubaX
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1499690657-81150-3-git-send-email-jacekx.piasecki@intel.com \
--to=jacekx.piasecki@intel.com \
--cc=bruce.richardson@intel.com \
--cc=deepak.k.jain@intel.com \
--cc=dev@dpdk.org \
--cc=kubax.kozak@intel.com \
--cc=michalx.k.jastrzebski@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).