From 4e54c4b289d08bdc9f33978c200567217b87d81b Mon Sep 17 00:00:00 2001 From: Wang Huaqiang Date: Mon, 12 Nov 2018 21:31:40 +0800 Subject: [PATCH] util: Refactor virResctrlAllocSetID to set allocation ID Refactor virResctrlAllocSetID generating an error if an attempt is made to overwrite the existing value. Signed-off-by: Wang Huaqiang Reviewed-by: John Ferlan --- src/util/virresctrl.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c index 3a8b6e2cf0..90eda726f8 100644 --- a/src/util/virresctrl.c +++ b/src/util/virresctrl.c @@ -1361,17 +1361,32 @@ virResctrlAllocForeachMemory(virResctrlAllocPtr alloc, } +static int +virResctrlSetID(char **resctrlid, + const char *id) +{ + if (!id) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("New resctrl 'id' cannot be NULL")); + return -1; + } + + if (*resctrlid) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Attempt to overwrite resctrlid='%s' with id='%s'"), + *resctrlid, id); + return -1; + } + + return VIR_STRDUP(*resctrlid, id); +} + + int virResctrlAllocSetID(virResctrlAllocPtr alloc, const char *id) { - if (!id) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Resctrl allocation 'id' cannot be NULL")); - return -1; - } - - return VIR_STRDUP(alloc->id, id); + return virResctrlSetID(&alloc->id, id); }