[libvirt] [PATCH v4 1/3] interface: Introduce virInterfaceObjNew

John Ferlan posted 3 patches 7 years, 11 months ago
[libvirt] [PATCH v4 1/3] interface: Introduce virInterfaceObjNew
Posted by John Ferlan 7 years, 11 months ago
Create/use a helper to perform the object allocation

Signed-off-by: John Ferlan <jferlan@redhat.com>
---
 src/conf/virinterfaceobj.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/src/conf/virinterfaceobj.c b/src/conf/virinterfaceobj.c
index 8bd8094..1e3f25c 100644
--- a/src/conf/virinterfaceobj.c
+++ b/src/conf/virinterfaceobj.c
@@ -46,6 +46,27 @@ struct _virInterfaceObjList {
 
 /* virInterfaceObj manipulation */
 
+static virInterfaceObjPtr
+virInterfaceObjNew(void)
+{
+    virInterfaceObjPtr obj;
+
+    if (VIR_ALLOC(obj) < 0)
+        return NULL;
+
+    if (virMutexInit(&obj->lock) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "%s", _("cannot initialize mutex"));
+        VIR_FREE(obj);
+        return NULL;
+    }
+
+    virInterfaceObjLock(obj);
+
+    return obj;
+}
+
+
 void
 virInterfaceObjLock(virInterfaceObjPtr obj)
 {
@@ -230,18 +251,12 @@ virInterfaceObjListAssignDef(virInterfaceObjListPtr interfaces,
         return obj;
     }
 
-    if (VIR_ALLOC(obj) < 0)
-        return NULL;
-    if (virMutexInit(&obj->lock) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       "%s", _("cannot initialize mutex"));
-        VIR_FREE(obj);
+    if (!(obj = virInterfaceObjNew()))
         return NULL;
-    }
-    virInterfaceObjLock(obj);
 
     if (VIR_APPEND_ELEMENT_COPY(interfaces->objs,
                                 interfaces->count, obj) < 0) {
+        virInterfaceObjUnlock(obj);
         virInterfaceObjFree(obj);
         return NULL;
     }
-- 
2.9.4

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v4 1/3] interface: Introduce virInterfaceObjNew
Posted by Erik Skultety 7 years, 11 months ago
On Sat, Jun 03, 2017 at 06:53:21AM -0400, John Ferlan wrote:
> Create/use a helper to perform the object allocation
>
> Signed-off-by: John Ferlan <jferlan@redhat.com>
> ---
>  src/conf/virinterfaceobj.c | 31 +++++++++++++++++++++++--------
>  1 file changed, 23 insertions(+), 8 deletions(-)
>
> diff --git a/src/conf/virinterfaceobj.c b/src/conf/virinterfaceobj.c
> index 8bd8094..1e3f25c 100644
> --- a/src/conf/virinterfaceobj.c
> +++ b/src/conf/virinterfaceobj.c
> @@ -46,6 +46,27 @@ struct _virInterfaceObjList {
>
>  /* virInterfaceObj manipulation */
>
> +static virInterfaceObjPtr
> +virInterfaceObjNew(void)
> +{
> +    virInterfaceObjPtr obj;
> +
> +    if (VIR_ALLOC(obj) < 0)
> +        return NULL;
> +
> +    if (virMutexInit(&obj->lock) < 0) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR,
> +                       "%s", _("cannot initialize mutex"));
> +        VIR_FREE(obj);
> +        return NULL;
> +    }
> +
> +    virInterfaceObjLock(obj);

Nothing in the function name suggests that the returned object is going to be
locked already, but we do the same thing for domains, secrets, and NWFilter, so
let's just say I'm okay with such design.

ACK.
Erik

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list