提交 7d7a7e29 编写于 作者: E Eric Blake 提交者: Daniel Veillard

xen: use typical allocations

The next patch will add a syntax check that flags this usage in xen
as awkward - while it was valid memory management, it was very hard
to maintain.  Swapping to a more traditional allocation may be a bit
slower, but easier to understand.

* src/xen/xend_internal.c (xenDaemonListDomainsOld): Use two-level
allocation, rather than abusing allocation function.
(xenDaemonLookupByUUID): Update caller.
上级 08c4de59
...@@ -753,12 +753,10 @@ xend_wait_for_devices(virConnectPtr xend, const char *name) ...@@ -753,12 +753,10 @@ xend_wait_for_devices(virConnectPtr xend, const char *name)
char ** char **
xenDaemonListDomainsOld(virConnectPtr xend) xenDaemonListDomainsOld(virConnectPtr xend)
{ {
size_t extra = 0;
struct sexpr *root = NULL; struct sexpr *root = NULL;
char **ret = NULL; char **ret = NULL;
int count = 0; int count = 0;
int i; int i;
char *ptr;
struct sexpr *_for_i, *node; struct sexpr *_for_i, *node;
root = sexpr_get(xend, "/xend/domain"); root = sexpr_get(xend, "/xend/domain");
...@@ -769,32 +767,22 @@ xenDaemonListDomainsOld(virConnectPtr xend) ...@@ -769,32 +767,22 @@ xenDaemonListDomainsOld(virConnectPtr xend)
_for_i = _for_i->u.s.cdr, node = _for_i->u.s.car) { _for_i = _for_i->u.s.cdr, node = _for_i->u.s.car) {
if (node->kind != SEXPR_VALUE) if (node->kind != SEXPR_VALUE)
continue; continue;
extra += strlen(node->u.value) + 1;
count++; count++;
} }
/* if (VIR_ALLOC_N(ret, count + 1) < 0) {
* We can'tuse the normal allocation routines as we are mixing virReportOOMError();
* an array of char * at the beginning followed by an array of char
* ret points to the NULL terminated array of char *
* ptr points to the current string after that array but in the same
* allocated block
*/
if (virAlloc((void *)&ptr,
(count + 1) * sizeof(char *) + extra * sizeof(char)) < 0)
goto error; goto error;
}
ret = (char **) ptr;
ptr += sizeof(char *) * (count + 1);
i = 0; i = 0;
for (_for_i = root, node = root->u.s.car; _for_i->kind == SEXPR_CONS; for (_for_i = root, node = root->u.s.car; _for_i->kind == SEXPR_CONS;
_for_i = _for_i->u.s.cdr, node = _for_i->u.s.car) { _for_i = _for_i->u.s.cdr, node = _for_i->u.s.car) {
if (node->kind != SEXPR_VALUE) if (node->kind != SEXPR_VALUE)
continue; continue;
ret[i] = ptr; ret[i] = strdup(node->u.value);
strcpy(ptr, node->u.value); if (!ret[i])
ptr += strlen(node->u.value) + 1; goto no_memory;
i++; i++;
} }
...@@ -803,6 +791,12 @@ xenDaemonListDomainsOld(virConnectPtr xend) ...@@ -803,6 +791,12 @@ xenDaemonListDomainsOld(virConnectPtr xend)
error: error:
sexpr_free(root); sexpr_free(root);
return ret; return ret;
no_memory:
for (i = 0; i < count; i++)
VIR_FREE(ret[i]);
VIR_FREE(ret);
goto error;
} }
...@@ -2493,16 +2487,18 @@ xenDaemonLookupByUUID(virConnectPtr conn, const unsigned char *uuid) ...@@ -2493,16 +2487,18 @@ xenDaemonLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
id = xenDaemonDomainLookupByName_ids(conn, *tmp, &ident[0]); id = xenDaemonDomainLookupByName_ids(conn, *tmp, &ident[0]);
if (id >= 0) { if (id >= 0) {
if (!memcmp(uuid, ident, VIR_UUID_BUFLEN)) { if (!memcmp(uuid, ident, VIR_UUID_BUFLEN)) {
name = strdup(*tmp); name = *tmp;
if (name == NULL)
virReportOOMError();
break; break;
} }
} }
tmp++; tmp++;
} }
tmp = names;
while (*tmp) {
if (*tmp != name)
VIR_FREE(*tmp);
tmp++;
}
VIR_FREE(names); VIR_FREE(names);
} else { /* New approach for xen >= 3.0.4 */ } else { /* New approach for xen >= 3.0.4 */
char *domname = NULL; char *domname = NULL;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册