There are a few lists in the nwfilter data structures that lend
themselves to using the new convenience macros.
Note that this patch incidentally fixes what appears to be a bug in
virNWFilterUnRegisterCallbackDriver - it was finding an entry to
delete, adjusting the array to get rid of it, then writing a 0 over
the item that had just been moved into the place previously occupied
by the now-deleted item (I think the intent was to 0 out the end of the
list, which also doesn't make sense, since the memory allocated to the
list had just been decreased in size).
---
src/conf/nwfilter_conf.c | 35 ++++++++++++++--------------------
src/conf/nwfilter_conf.h | 6 +++---
src/conf/nwfilter_params.c | 8 +-------
src/nwfilter/nwfilter_gentech_driver.c | 15 ++++++---------
4 files changed, 24 insertions(+), 40 deletions(-)
diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c
index 2d32bd3..3b15984 100644
--- a/src/conf/nwfilter_conf.c
+++ b/src/conf/nwfilter_conf.c
@@ -395,20 +395,19 @@ virNWFilterRuleDefAddString(virNWFilterRuleDefPtr nwf,
const char *string,
size_t maxstrlen)
{
- if (VIR_REALLOC_N(nwf->strings, nwf->nstrings+1) < 0) {
+ char *copy = strndup(string, maxstrlen);
+
+ if (!copy) {
virReportOOMError();
return NULL;
}
- nwf->strings[nwf->nstrings] = strndup(string, maxstrlen);
-
- if (!nwf->strings[nwf->nstrings]) {
+ if (VIR_APPEND_ELEMENT(nwf->strings, nwf->nstrings, ©) < 0) {
virReportOOMError();
+ VIR_FREE(copy);
return NULL;
}
- nwf->nstrings++;
-
return nwf->strings[nwf->nstrings-1];
}
@@ -2585,14 +2584,14 @@ virNWFilterDefParseXML(xmlXPathContextPtr ctxt) {
entry->include = virNWFilterIncludeParse(curr);
if (entry->rule || entry->include) {
- if (VIR_REALLOC_N(ret->filterEntries, ret->nentries+1) < 0) {
- VIR_FREE(entry);
+ if (VIR_APPEND_ELEMENT(ret->filterEntries, ret->nentries,
&entry) < 0) {
+ virNWFilterEntryFree(entry);
virReportOOMError();
goto cleanup;
}
- ret->filterEntries[ret->nentries++] = entry;
- } else
+ } else {
VIR_FREE(entry);
+ }
}
curr = curr->next;
}
@@ -2811,7 +2810,7 @@ virNWFilterDefLoopDetect(virConnectPtr conn,
return _virNWFilterDefLoopDetect(conn, nwfilters, def, def->name);
}
-int nCallbackDriver;
+size_t nCallbackDriver;
#define MAX_CALLBACK_DRIVER 10
static virNWFilterCallbackDriverPtr callbackDrvArray[MAX_CALLBACK_DRIVER];
@@ -2831,12 +2830,8 @@ virNWFilterUnRegisterCallbackDriver(virNWFilterCallbackDriverPtr
cbd)
while (i < nCallbackDriver && callbackDrvArray[i] != cbd)
i++;
- if (i < nCallbackDriver) {
- memmove(&callbackDrvArray[i], &callbackDrvArray[i+1],
- (nCallbackDriver - i - 1) * sizeof(callbackDrvArray[i]));
- callbackDrvArray[i] = 0;
- nCallbackDriver--;
- }
+ if (i < nCallbackDriver)
+ VIR_DELETE_ELEMENT_INPLACE(callbackDrvArray, i, nCallbackDriver);
}
void
@@ -3047,16 +3042,14 @@ virNWFilterObjAssignDef(virConnectPtr conn,
nwfilter->active = 0;
nwfilter->def = def;
- if (VIR_REALLOC_N(nwfilters->objs, nwfilters->count + 1) < 0) {
+ if (VIR_APPEND_ELEMENT(nwfilters->objs, nwfilters->count, &nwfilter) <
0) {
nwfilter->def = NULL;
virNWFilterObjUnlock(nwfilter);
virNWFilterObjFree(nwfilter);
virReportOOMError();
return NULL;
}
- nwfilters->objs[nwfilters->count++] = nwfilter;
-
- return nwfilter;
+ return nwfilters->objs[nwfilters->count - 1];
}
diff --git a/src/conf/nwfilter_conf.h b/src/conf/nwfilter_conf.h
index 4a2a9f3..893b345 100644
--- a/src/conf/nwfilter_conf.h
+++ b/src/conf/nwfilter_conf.h
@@ -476,7 +476,7 @@ struct _virNWFilterRuleDef {
size_t nVarAccess;
virNWFilterVarAccessPtr *varAccess;
- int nstrings;
+ size_t nstrings;
char **strings;
};
@@ -524,7 +524,7 @@ struct _virNWFilterDef {
char *chainsuffix;
virNWFilterChainPriority chainPriority;
- int nentries;
+ size_t nentries;
virNWFilterEntryPtr *filterEntries;
};
@@ -571,7 +571,7 @@ typedef virNWFilterTechDriver *virNWFilterTechDriverPtr;
typedef struct _virNWFilterRuleInst virNWFilterRuleInst;
typedef virNWFilterRuleInst *virNWFilterRuleInstPtr;
struct _virNWFilterRuleInst {
- int ndata;
+ size_t ndata;
void **data;
virNWFilterTechDriverPtr techdriver;
};
diff --git a/src/conf/nwfilter_params.c b/src/conf/nwfilter_params.c
index 7254519..0bd6ae4 100644
--- a/src/conf/nwfilter_params.c
+++ b/src/conf/nwfilter_params.c
@@ -269,13 +269,7 @@ virNWFilterVarValueDelNthValue(virNWFilterVarValuePtr val, unsigned
int pos)
case NWFILTER_VALUE_TYPE_ARRAY:
if (pos < val->u.array.nValues) {
VIR_FREE(val->u.array.values[pos]);
- val->u.array.nValues--;
-
- if (pos < val->u.array.nValues)
- memmove(&val->u.array.values[pos],
- &val->u.array.values[pos + 1],
- sizeof(val->u.array.values[0]) *
- (val->u.array.nValues - pos));
+ VIR_DELETE_ELEMENT_INPLACE(val->u.array.values, pos,
val->u.array.nValues);
return 0;
}
break;
diff --git a/src/nwfilter/nwfilter_gentech_driver.c
b/src/nwfilter/nwfilter_gentech_driver.c
index 572acf4..15d41cd 100644
--- a/src/nwfilter/nwfilter_gentech_driver.c
+++ b/src/nwfilter/nwfilter_gentech_driver.c
@@ -1,7 +1,7 @@
/*
* nwfilter_gentech_driver.c: generic technology driver
*
- * Copyright (C) 2011 Red Hat, Inc.
+ * Copyright (C) 2011, 2012 Red Hat, Inc.
* Copyright (C) 2010 IBM Corp.
* Copyright (C) 2010 Stefan Berger
*
@@ -109,11 +109,10 @@ int
virNWFilterRuleInstAddData(virNWFilterRuleInstPtr res,
void *data)
{
- if (VIR_REALLOC_N(res->data, res->ndata+1) < 0) {
+ if (VIR_APPEND_ELEMENT(res->data, res->ndata, &data) < 0) {
virReportOOMError();
return -1;
}
- res->data[res->ndata++] = data;
return 0;
}
@@ -387,7 +386,7 @@ _virNWFilterInstantiateRec(virNWFilterTechDriverPtr techdriver,
virNWFilterDefPtr filter,
const char *ifname,
virNWFilterHashTablePtr vars,
- int *nEntries,
+ size_t *nEntries,
virNWFilterRuleInstPtr **insts,
enum instCase useNewFilter, bool *foundNewFilter,
virNWFilterDriverStatePtr driver)
@@ -413,14 +412,12 @@ _virNWFilterInstantiateRec(virNWFilterTechDriverPtr techdriver,
break;
}
- if (VIR_REALLOC_N(*insts, (*nEntries)+1) < 0) {
+ if (VIR_APPEND_ELEMENT(*insts, *nEntries, &inst) < 0) {
virReportOOMError();
rc = -1;
break;
}
- (*insts)[(*nEntries)++] = inst;
-
} else if (inc) {
VIR_DEBUG("Instantiating filter %s", inc->filterref);
obj = virNWFilterObjFindByName(&driver->nwfilters,
inc->filterref);
@@ -595,7 +592,7 @@ virNWFilterDetermineMissingVarsRec(virNWFilterDefPtr filter,
static int
-virNWFilterRuleInstancesToArray(int nEntries,
+virNWFilterRuleInstancesToArray(size_t nEntries,
virNWFilterRuleInstPtr *insts,
void ***ptrs,
int *nptrs)
@@ -662,7 +659,7 @@ virNWFilterInstantiate(const unsigned char *vmuuid ATTRIBUTE_UNUSED,
{
int rc;
int j, nptrs;
- int nEntries = 0;
+ size_t nEntries = 0;
virNWFilterRuleInstPtr *insts = NULL;
void **ptrs = NULL;
int instantiate = 1;
--
1.7.11.7