提交 6f46bf7f 编写于 作者: J Jiri Denemark

cpu_x86: Use array of models in CPU map

There's no reason for keeping the models in a linked list. Especially
when we know upfront the total number of models we are loading.

As a nice side effect, this fixes x86GetModels to always return a
NULL-terminated list of models.
Signed-off-by: NJiri Denemark <jdenemar@redhat.com>
上级 d25a3051
...@@ -97,8 +97,6 @@ struct _virCPUx86Model { ...@@ -97,8 +97,6 @@ struct _virCPUx86Model {
char *name; char *name;
virCPUx86VendorPtr vendor; virCPUx86VendorPtr vendor;
virCPUx86Data *data; virCPUx86Data *data;
virCPUx86ModelPtr next;
}; };
typedef struct _virCPUx86Map virCPUx86Map; typedef struct _virCPUx86Map virCPUx86Map;
...@@ -106,7 +104,8 @@ typedef virCPUx86Map *virCPUx86MapPtr; ...@@ -106,7 +104,8 @@ typedef virCPUx86Map *virCPUx86MapPtr;
struct _virCPUx86Map { struct _virCPUx86Map {
virCPUx86VendorPtr vendors; virCPUx86VendorPtr vendors;
virCPUx86FeaturePtr features; virCPUx86FeaturePtr features;
virCPUx86ModelPtr models; size_t nmodels;
virCPUx86ModelPtr *models;
virCPUx86FeaturePtr migrate_blockers; virCPUx86FeaturePtr migrate_blockers;
}; };
...@@ -859,14 +858,11 @@ static virCPUx86ModelPtr ...@@ -859,14 +858,11 @@ static virCPUx86ModelPtr
x86ModelFind(virCPUx86MapPtr map, x86ModelFind(virCPUx86MapPtr map,
const char *name) const char *name)
{ {
virCPUx86ModelPtr model; size_t i;
model = map->models;
while (model) {
if (STREQ(model->name, name))
return model;
model = model->next; for (i = 0; i < map->nmodels; i++) {
if (STREQ(map->models[i]->name, name))
return map->models[i];
} }
return NULL; return NULL;
...@@ -1094,9 +1090,8 @@ x86ModelLoad(xmlXPathContextPtr ctxt, ...@@ -1094,9 +1090,8 @@ x86ModelLoad(xmlXPathContextPtr ctxt,
goto cleanup; goto cleanup;
} }
model->next = map->models; if (VIR_APPEND_ELEMENT(map->models, map->nmodels, model) < 0)
map->models = model; goto cleanup;
model = NULL;
ret = 0; ret = 0;
...@@ -1111,6 +1106,8 @@ x86ModelLoad(xmlXPathContextPtr ctxt, ...@@ -1111,6 +1106,8 @@ x86ModelLoad(xmlXPathContextPtr ctxt,
static void static void
x86MapFree(virCPUx86MapPtr map) x86MapFree(virCPUx86MapPtr map)
{ {
size_t i;
if (!map) if (!map)
return; return;
...@@ -1120,11 +1117,9 @@ x86MapFree(virCPUx86MapPtr map) ...@@ -1120,11 +1117,9 @@ x86MapFree(virCPUx86MapPtr map)
x86FeatureFree(feature); x86FeatureFree(feature);
} }
while (map->models) { for (i = 0; i < map->nmodels; i++)
virCPUx86ModelPtr model = map->models; x86ModelFree(map->models[i]);
map->models = model->next; VIR_FREE(map->models);
x86ModelFree(model);
}
while (map->vendors) { while (map->vendors) {
virCPUx86VendorPtr vendor = map->vendors; virCPUx86VendorPtr vendor = map->vendors;
...@@ -1592,7 +1587,7 @@ x86Decode(virCPUDefPtr cpu, ...@@ -1592,7 +1587,7 @@ x86Decode(virCPUDefPtr cpu,
virCPUx86Data *features = NULL; virCPUx86Data *features = NULL;
const virCPUx86Data *cpuData = NULL; const virCPUx86Data *cpuData = NULL;
virCPUx86VendorPtr vendor; virCPUx86VendorPtr vendor;
size_t i; ssize_t i;
int rc; int rc;
virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES | virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES |
...@@ -1603,7 +1598,11 @@ x86Decode(virCPUDefPtr cpu, ...@@ -1603,7 +1598,11 @@ x86Decode(virCPUDefPtr cpu,
vendor = x86DataToVendor(data, map); vendor = x86DataToVendor(data, map);
for (candidate = map->models; candidate; candidate = candidate->next) { /* Walk through the CPU models in reverse order to check newest
* models first.
*/
for (i = map->nmodels - 1; i >= 0; i--) {
candidate = map->models[i];
if (!cpuModelIsAllowed(candidate->name, models, nmodels)) { if (!cpuModelIsAllowed(candidate->name, models, nmodels)) {
if (preferred && STREQ(candidate->name, preferred)) { if (preferred && STREQ(candidate->name, preferred)) {
if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) { if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) {
...@@ -2211,34 +2210,22 @@ static int ...@@ -2211,34 +2210,22 @@ static int
x86GetModels(char ***models) x86GetModels(char ***models)
{ {
virCPUx86MapPtr map; virCPUx86MapPtr map;
virCPUx86ModelPtr model; size_t i;
char *name;
size_t nmodels = 0;
if (!(map = virCPUx86GetMap())) if (!(map = virCPUx86GetMap()))
return -1; return -1;
if (models && VIR_ALLOC_N(*models, 0) < 0) if (models) {
goto error; if (VIR_ALLOC_N(*models, map->nmodels + 1) < 0)
goto error;
model = map->models;
while (model) {
if (models) {
if (VIR_STRDUP(name, model->name) < 0)
goto error;
if (VIR_APPEND_ELEMENT(*models, nmodels, name) < 0) { for (i = 0; i < map->nmodels; i++) {
VIR_FREE(name); if (VIR_STRDUP((*models)[i], map->models[i]->name) < 0)
goto error; goto error;
}
} else {
nmodels++;
} }
model = model->next;
} }
return nmodels; return map->nmodels;
error: error:
if (models) { if (models) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册