1. 16 3月, 2019 1 次提交
    • E
      object: Add sanity check on correct parent class · 1c560052
      Eric Blake 提交于
      Checking that the derived class is larger than the requested parent
      class saves us from some obvious mistakes, but as written, it does not
      catch all the cases; in particular, it is easy to forget to update a
      VIR_CLASS_NEW when changing the 'parent' member from virObject to
      virObjectLockabale, but where the size checks don't catch that.  Add a
      parameter for one more layer of sanity checking.
      
      It would be cool if we could get gcc to stringize typeof(parent) into
      the string name of that type, so that we could confirm that the
      precise parent class is in use rather than just a struct that happens
      to have the same size as the parent class.  But sizeof checks are
      better than nothing.
      
      Note that I did NOT change the fact that we require derived classes to
      be larger (as the difference in size makes it easy to tell classes
      apart), which means that even if a derived class has no functionality
      to add (but rather exists for compiler-enforced type-safety), it must
      still include a dummy member.  But I did fix the wording of the error
      message to match the code.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Reviewed-by: NJán Tomko <jtomko@redhat.com>
      Reviewed-by: NDaniel P. Berrangé <berrange@redhat.com>
      1c560052
  2. 15 3月, 2019 1 次提交
    • E
      virobject: Improve documentation · 632ac8f8
      Eric Blake 提交于
      I had to inspect the code to learn whether a final virObjectUnref()
      calls ALL dispose callbacks in child-to-parent order (akin to C++
      destructors), or whether I manually had to call a parent-class dispose
      when writing a child class dispose method.  The answer is the
      former. (Thankfully, since VIR_FREE wipes out pointers for safety,
      even if I had guessed wrong, I probably would not have tripped over a
      double-free fault when the parent dispose ran for the second time).  I
      also had to read the code to learn if a dispose method was even
      mandatory (it is not, although getting NULL through VIR_CLASS_NEW
      requires a macro).  While at it, the VIR_CLASS_NEW macro requires that
      the virObject component at offset 0 be reached through the name
      'parent', not 'object'.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      Reviewed-by: NErik Skultety <eskultet@redhat.com>
      632ac8f8
  3. 18 2月, 2019 1 次提交
  4. 14 12月, 2018 2 次提交
  5. 18 4月, 2018 2 次提交
  6. 15 8月, 2017 3 次提交
    • J
      util: Introduce and use virObjectRWUnlock · 045d712c
      John Ferlan 提交于
      Rather than overload virObjectUnlock as commit id '77f4593b' has
      done, create a separate virObjectRWUnlock API that will force the
      consumers to make the proper decision regarding unlocking the
      RWLock's. Similar to the RWLockRead and RWLockWrite, use the
      virObjectGetRWLockableObj helper. This restores the virObjectUnlock
      code to using the virObjectGetLockableObj.
      Signed-off-by: NJohn Ferlan <jferlan@redhat.com>
      045d712c
    • J
      util: Introduce and use virObjectRWLockWrite · 908b3364
      John Ferlan 提交于
      Instead of making virObjectLock be the entry point for two
      different types of locks, let's create a virObjectRWLockWrite API
      which will only handle the virObjectRWLockableClass objects.
      
      Use the new virObjectRWLockWrite for the virdomainobjlist code
      in order to handle the Add, Remove, Rename, and Load operations
      that need to be very synchronous.
      Signed-off-by: NJohn Ferlan <jferlan@redhat.com>
      908b3364
    • J
      util: Rename virObjectLockRead to virObjectRWLockRead · 99a72b3e
      John Ferlan 提交于
      Since the class it represents is based on virObjectRWLockableClass
      and in order to make sure we differentiate just in case anyone somehow
      believes they could use virObjectLockRead for a virObjectLockableClass,
      let's rename the API to use the RW in the name. Besides the RW locks
      refer to pthread_rwlock_{init|rdlock|wrlock|unlock|destroy} while the
      other locks refer to pthread_mutex_{init|lock|unlock|destroy}.
      Signed-off-by: NJohn Ferlan <jferlan@redhat.com>
      99a72b3e
  7. 24 7月, 2017 1 次提交
    • M
      virobject: Introduce virObjectRWLockable · 77f4593b
      Michal Privoznik 提交于
      Up until now we only had virObjectLockable which uses mutexes for
      mutually excluding each other in critical section. Well, this is
      not enough. Future work will require RW locks so we might as well
      have virObjectRWLockable which is introduced here.
      
      Moreover, polymorphism is introduced to our code for the first
      time. Yay! More specifically, virObjectLock will grab a write
      lock, virObjectLockRead will grab a read lock then (what a
      surprise right?). This has great advantage that an object can be
      made derived from virObjectRWLockable in a single line and still
      continue functioning properly (mutexes can be viewed as grabbing
      write locks only). Then just those critical sections that can
      grab a read lock need fixing. Therefore the resulting change is
      going to be way smaller.
      
      In order to avoid writer starvation, the object initializes RW
      lock that prefers writers.
      Signed-off-by: NMichal Privoznik <mprivozn@redhat.com>
      Reviewed-by: NPavel Hrdina <phrdina@redhat.com>
      77f4593b
  8. 22 6月, 2017 1 次提交
  9. 11 5月, 2015 1 次提交
    • P
      util: Make the virDomainListFree helper more universal · a5e89ae1
      Peter Krempa 提交于
      Extend it to a universal helper used for clearing lists of any objects.
      Note that the argument type is specifically void * to allow implicit
      typecasting.
      
      Additionally add a helper that works on non-NULL terminated arrays once
      we know the length.
      a5e89ae1
  10. 07 4月, 2014 1 次提交
    • E
      hash: add common utility functions · 09567144
      Eric Blake 提交于
      I almost wrote a hash value free function that just called
      VIR_FREE, then realized I couldn't be the first person to
      do that.  Sure enough, it was worth factoring into a common
      helper routine.
      
      * src/util/virhash.h (virHashValueFree): New function.
      * src/util/virhash.c (virHashValueFree): Implement it.
      * src/util/virobject.h (virObjectFreeHashData): New function.
      * src/libvirt_private.syms (virhash.h, virobject.h): Export them.
      * src/nwfilter/nwfilter_learnipaddr.c (virNWFilterLearnInit): Use
      common function.
      * src/qemu/qemu_capabilities.c (virQEMUCapsCacheNew): Likewise.
      * src/qemu/qemu_command.c (qemuDomainCCWAddressSetCreate):
      Likewise.
      * src/qemu/qemu_monitor.c (qemuMonitorGetBlockInfo): Likewise.
      * src/qemu/qemu_process.c (qemuProcessWaitForMonitor): Likewise.
      * src/util/virclosecallbacks.c (virCloseCallbacksNew): Likewise.
      * src/util/virkeyfile.c (virKeyFileParseGroup): Likewise.
      * tests/qemumonitorjsontest.c
      (testQemuMonitorJSONqemuMonitorJSONGetBlockInfo): Likewise.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      09567144
  11. 13 12月, 2013 1 次提交
    • E
      object: require maximal alignment in base class · fca4f233
      Eric Blake 提交于
      Recent changes to events (commit 8a29ffcf) resulted in new compile
      failures on some targets (such as ARM OMAP5):
      conf/domain_event.c: In function 'virDomainEventDispatchDefaultFunc':
      conf/domain_event.c:1198:30: error: cast increases required alignment of
      target type [-Werror=cast-align]
      conf/domain_event.c:1314:34: error: cast increases required alignment of
      target type [-Werror=cast-align]
      cc1: all warnings being treated as errors
      
      The error is due to alignment; the base class is merely aligned
      to the worst of 'int' and 'void*', while the child class must
      be aligned to a 'long long'.  The solution is to include a
      'long long' (and for good measure, a function pointer) in the
      base class to ensure correct alignment regardless of what a
      child class may add, but to wrap the inclusion in a union so
      as to not incur any wasted space.  On a typical x86_64 platform,
      the base class remains 16 bytes; on i686, the base class remains
      12 bytes; and on the impacted ARM platform, the base class grows
      from 12 bytes to 16 bytes due to the increase of alignment from
      4 to 8 bytes.
      
      Reported by Michele Paolino and others.
      
      * src/util/virobject.h (_virObject): Use a union to ensure that
      subclasses never have stricter alignment than the parent.
      * src/util/virobject.c (virObjectNew, virObjectUnref)
      (virObjectRef): Adjust clients.
      * src/libvirt.c (virConnectRef, virDomainRef, virNetworkRef)
      (virInterfaceRef, virStoragePoolRef, virStorageVolRef)
      (virNodeDeviceRef, virSecretRef, virStreamRef, virNWFilterRef)
      (virDomainSnapshotRef): Likewise.
      * src/qemu/qemu_monitor.c (qemuMonitorOpenInternal)
      (qemuMonitorClose): Likewise.
      Signed-off-by: NEric Blake <eblake@redhat.com>
      fca4f233
  12. 23 1月, 2013 1 次提交
    • E
      build: allow virObject to have no parent · 682c79c4
      Eric Blake 提交于
      When building with static analysis enabled, we turn on attribute
      nonnull checking.  However, this caused the build to fail with:
      
      ../../src/util/virobject.c: In function 'virObjectOnceInit':
      ../../src/util/virobject.c:55:40: error: null argument where non-null required (argument 1) [-Werror=nonnull]
      
      Creation of the virObject class is the one instance where the
      parent class is allowed to be NULL.  Making things conditional
      will let us keep static analysis checking for all other .c file
      callers, without breaking the build on this one exception.
      
      * src/util/virobject.c: Define witness.
      * src/util/virobject.h (virClassNew): Use it to force most callers
      to pass non-null parameter.
      682c79c4
  13. 16 1月, 2013 2 次提交
  14. 21 9月, 2012 1 次提交
  15. 07 8月, 2012 1 次提交
    • D
      Add a generic reference counted virObject type · 784a99f7
      Daniel P. Berrange 提交于
      This introduces a fairly basic reference counted virObject type
      and an associated virClass type, that use atomic operations for
      ref counting.
      
      In a global initializer (recommended to be invoked using the
      virOnceInit API), a virClass type must be allocated for each
      object type. This requires a class name, a "dispose" callback
      which will be invoked to free memory associated with the object's
      fields, and the size in bytes of the object struct.
      
      eg,
      
         virClassPtr  connclass = virClassNew("virConnect",
                                              sizeof(virConnect),
                                              virConnectDispose);
      
      The struct for the object, must include 'virObject' as its
      first member
      
      eg
      
        struct _virConnect {
          virObject object;
      
          virURIPtr uri;
        };
      
      The 'dispose' callback is only responsible for freeing
      fields in the object, not the object itself. eg a suitable
      impl for the above struct would be
      
        void virConnectDispose(void *obj) {
           virConnectPtr conn = obj;
           virURIFree(conn->uri);
        }
      
      There is no need to reset fields to 'NULL' or '0' in the
      dispose callback, since the entire object will be memset
      to 0, and the klass pointer & magic integer fields will
      be poisoned with 0xDEADBEEF before being free()d
      
      When creating an instance of an object, one needs simply
      pass the virClassPtr eg
      
         virConnectPtr conn = virObjectNew(connclass);
         if (!conn)
            return NULL;
         conn->uri = virURIParse("foo:///bar")
      
      Object references can be manipulated with
      
         virObjectRef(conn)
         virObjectUnref(conn)
      
      The latter returns a true value, if the object has been
      freed (ie its ref count hit zero)
      Signed-off-by: NDaniel P. Berrange <berrange@redhat.com>
      784a99f7