提交 f8fa2b3e 编写于 作者: E Eric Blake

maint: fix awkward typing of virDomainChrGetDomainPtrs

virDomainChrGetDomainPtrs() required 4 levels of pointers (taking
a parameter that will be used as an output variable to return the
address of another variable that contains an array of pointers).
This is rather complex to reason about, especially when outside
of the domain_conf file, no other caller should be modifying
the resulting array of pointers directly.  Changing the public
signature gives something is easier to reason with, and actually
make const-correct; which is important as it was the only function
that was blocking virDomainDeviceDefCopy from treating its source
as const.

* src/conf/domain_conf.h (virDomainChrGetDomainPtrs): Use simpler
types, and make const-correct for external users.
* src/conf/domain_conf.c (virDomainChrGetDomainPtrs): Split...
(virDomainChrGetDomainPtrsInternal): ...into an internal version
that lets us modify terms, vs. external form that is read-only.
(virDomainDeviceDefPostParseInternal, virDomainChrFind)
(virDomainChrInsert): Adjust callers.
* src/qemu/qemu_command.c (qemuGetNextChrDevIndex): Adjust caller.
(qemuDomainDeviceAliasIndex): Make const-correct.
Signed-off-by: NEric Blake <eblake@redhat.com>
上级 d3f9d851
...@@ -2807,10 +2807,10 @@ virDomainDeviceDefPostParseInternal(virDomainDeviceDefPtr dev, ...@@ -2807,10 +2807,10 @@ virDomainDeviceDefPostParseInternal(virDomainDeviceDefPtr dev,
{ {
if (dev->type == VIR_DOMAIN_DEVICE_CHR) { if (dev->type == VIR_DOMAIN_DEVICE_CHR) {
virDomainChrDefPtr chr = dev->data.chr; virDomainChrDefPtr chr = dev->data.chr;
virDomainChrDefPtr **arrPtr; const virDomainChrDef **arrPtr;
size_t i, *cnt; size_t i, cnt;
virDomainChrGetDomainPtrs(def, chr, &arrPtr, &cnt); virDomainChrGetDomainPtrs(def, chr->deviceType, &arrPtr, &cnt);
if (chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE && if (chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE &&
chr->targetType == VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_NONE) chr->targetType == VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_NONE)
...@@ -2822,9 +2822,9 @@ virDomainDeviceDefPostParseInternal(virDomainDeviceDefPtr dev, ...@@ -2822,9 +2822,9 @@ virDomainDeviceDefPostParseInternal(virDomainDeviceDefPtr dev,
chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE)) { chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE)) {
int maxport = -1; int maxport = -1;
for (i = 0; i < *cnt; i++) { for (i = 0; i < cnt; i++) {
if ((*arrPtr)[i]->target.port > maxport) if (arrPtr[i]->target.port > maxport)
maxport = (*arrPtr)[i]->target.port; maxport = arrPtr[i]->target.port;
} }
chr->target.port = maxport + 1; chr->target.port = maxport + 1;
...@@ -2834,8 +2834,8 @@ virDomainDeviceDefPostParseInternal(virDomainDeviceDefPtr dev, ...@@ -2834,8 +2834,8 @@ virDomainDeviceDefPostParseInternal(virDomainDeviceDefPtr dev,
chr->info.addr.vioserial.port == 0) { chr->info.addr.vioserial.port == 0) {
int maxport = 0; int maxport = 0;
for (i = 0; i < *cnt; i++) { for (i = 0; i < cnt; i++) {
virDomainChrDefPtr thischr = (*arrPtr)[i]; const virDomainChrDef *thischr = arrPtr[i];
if (thischr->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_SERIAL && if (thischr->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_SERIAL &&
thischr->info.addr.vioserial.controller == chr->info.addr.vioserial.controller && thischr->info.addr.vioserial.controller == chr->info.addr.vioserial.controller &&
thischr->info.addr.vioserial.bus == chr->info.addr.vioserial.bus && thischr->info.addr.vioserial.bus == chr->info.addr.vioserial.bus &&
...@@ -10277,26 +10277,31 @@ virDomainChrDefPtr ...@@ -10277,26 +10277,31 @@ virDomainChrDefPtr
virDomainChrFind(virDomainDefPtr def, virDomainChrFind(virDomainDefPtr def,
virDomainChrDefPtr target) virDomainChrDefPtr target)
{ {
virDomainChrDefPtr chr, **arrPtr; virDomainChrDefPtr chr;
size_t i, *cntPtr; const virDomainChrDef **arrPtr;
size_t i, cnt;
virDomainChrGetDomainPtrs(def, target, &arrPtr, &cntPtr); virDomainChrGetDomainPtrs(def, target->deviceType, &arrPtr, &cnt);
for (i = 0; i < *cntPtr; i++) { for (i = 0; i < cnt; i++) {
chr = (*arrPtr)[i]; /* Cast away const */
chr = (virDomainChrDefPtr) arrPtr[i];
if (virDomainChrEquals(chr, target)) if (virDomainChrEquals(chr, target))
return chr; return chr;
} }
return NULL; return NULL;
} }
void
virDomainChrGetDomainPtrs(virDomainDefPtr vmdef, /* Return the address within vmdef to be modified when working with a
virDomainChrDefPtr chr, * chrdefptr of the given type. */
virDomainChrDefPtr ***arrPtr, static void
size_t **cntPtr) virDomainChrGetDomainPtrsInternal(virDomainDefPtr vmdef,
enum virDomainChrDeviceType type,
virDomainChrDefPtr ***arrPtr,
size_t **cntPtr)
{ {
switch ((enum virDomainChrDeviceType) chr->deviceType) { switch (type) {
case VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL: case VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL:
*arrPtr = &vmdef->parallels; *arrPtr = &vmdef->parallels;
*cntPtr = &vmdef->nparallels; *cntPtr = &vmdef->nparallels;
...@@ -10324,6 +10329,29 @@ virDomainChrGetDomainPtrs(virDomainDefPtr vmdef, ...@@ -10324,6 +10329,29 @@ virDomainChrGetDomainPtrs(virDomainDefPtr vmdef,
} }
} }
/* Return the array within vmdef that can contain a chrdefptr of the
* given type. */
void
virDomainChrGetDomainPtrs(const virDomainDef *vmdef,
enum virDomainChrDeviceType type,
const virDomainChrDef ***arrPtr,
size_t *cntPtr)
{
virDomainChrDef ***arrVar;
size_t *cntVar;
/* Cast away const; we add it back in the final assignment. */
virDomainChrGetDomainPtrsInternal((virDomainDefPtr) vmdef, type,
&arrVar, &cntVar);
if (arrVar) {
*arrPtr = (const virDomainChrDef **) *arrVar;
*cntPtr = *cntVar;
} else {
*arrPtr = NULL;
*cntPtr = 0;
}
}
int int
virDomainChrInsert(virDomainDefPtr vmdef, virDomainChrInsert(virDomainDefPtr vmdef,
virDomainChrDefPtr chr) virDomainChrDefPtr chr)
...@@ -10331,7 +10359,7 @@ virDomainChrInsert(virDomainDefPtr vmdef, ...@@ -10331,7 +10359,7 @@ virDomainChrInsert(virDomainDefPtr vmdef,
virDomainChrDefPtr **arrPtr; virDomainChrDefPtr **arrPtr;
size_t *cntPtr; size_t *cntPtr;
virDomainChrGetDomainPtrs(vmdef, chr, &arrPtr, &cntPtr); virDomainChrGetDomainPtrsInternal(vmdef, chr->deviceType, &arrPtr, &cntPtr);
return VIR_APPEND_ELEMENT(*arrPtr, *cntPtr, chr); return VIR_APPEND_ELEMENT(*arrPtr, *cntPtr, chr);
} }
...@@ -10343,7 +10371,7 @@ virDomainChrRemove(virDomainDefPtr vmdef, ...@@ -10343,7 +10371,7 @@ virDomainChrRemove(virDomainDefPtr vmdef,
virDomainChrDefPtr ret, **arrPtr; virDomainChrDefPtr ret, **arrPtr;
size_t i, *cntPtr; size_t i, *cntPtr;
virDomainChrGetDomainPtrs(vmdef, chr, &arrPtr, &cntPtr); virDomainChrGetDomainPtrsInternal(vmdef, chr->deviceType, &arrPtr, &cntPtr);
for (i = 0; i < *cntPtr; i++) { for (i = 0; i < *cntPtr; i++) {
ret = (*arrPtr)[i]; ret = (*arrPtr)[i];
......
...@@ -2439,10 +2439,11 @@ virDomainLeaseRemove(virDomainDefPtr def, ...@@ -2439,10 +2439,11 @@ virDomainLeaseRemove(virDomainDefPtr def,
virDomainLeaseDefPtr lease); virDomainLeaseDefPtr lease);
void void
virDomainChrGetDomainPtrs(virDomainDefPtr vmdef, virDomainChrGetDomainPtrs(const virDomainDef *vmdef,
virDomainChrDefPtr chr, enum virDomainChrDeviceType type,
virDomainChrDefPtr ***arrPtr, const virDomainChrDef ***arrPtr,
size_t **cntPtr); size_t *cntPtr)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);
virDomainChrDefPtr virDomainChrDefPtr
virDomainChrFind(virDomainDefPtr def, virDomainChrFind(virDomainDefPtr def,
virDomainChrDefPtr target); virDomainChrDefPtr target);
......
...@@ -584,7 +584,7 @@ cleanup: ...@@ -584,7 +584,7 @@ cleanup:
return ret; return ret;
} }
static int qemuDomainDeviceAliasIndex(virDomainDeviceInfoPtr info, static int qemuDomainDeviceAliasIndex(const virDomainDeviceInfo *info,
const char *prefix) const char *prefix)
{ {
int idx; int idx;
...@@ -911,8 +911,8 @@ qemuGetNextChrDevIndex(virDomainDefPtr def, ...@@ -911,8 +911,8 @@ qemuGetNextChrDevIndex(virDomainDefPtr def,
virDomainChrDefPtr chr, virDomainChrDefPtr chr,
const char *prefix) const char *prefix)
{ {
virDomainChrDefPtr **arrPtr; const virDomainChrDef **arrPtr;
size_t *cntPtr; size_t cnt;
size_t i; size_t i;
ssize_t idx = 0; ssize_t idx = 0;
const char *prefix2 = NULL; const char *prefix2 = NULL;
...@@ -920,13 +920,13 @@ qemuGetNextChrDevIndex(virDomainDefPtr def, ...@@ -920,13 +920,13 @@ qemuGetNextChrDevIndex(virDomainDefPtr def,
if (chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE) if (chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE)
prefix2 = "serial"; prefix2 = "serial";
virDomainChrGetDomainPtrs(def, chr, &arrPtr, &cntPtr); virDomainChrGetDomainPtrs(def, chr->deviceType, &arrPtr, &cnt);
for (i = 0; i < *cntPtr; i++) { for (i = 0; i < cnt; i++) {
ssize_t thisidx; ssize_t thisidx;
if (((thisidx = qemuDomainDeviceAliasIndex(&(*arrPtr)[i]->info, prefix)) < 0) && if (((thisidx = qemuDomainDeviceAliasIndex(&arrPtr[i]->info, prefix)) < 0) &&
(prefix2 && (prefix2 &&
(thisidx = qemuDomainDeviceAliasIndex(&(*arrPtr)[i]->info, prefix2)) < 0)) { (thisidx = qemuDomainDeviceAliasIndex(&arrPtr[i]->info, prefix2)) < 0)) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to determine device index for character device")); _("Unable to determine device index for character device"));
return -1; return -1;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册