[java] [PATCH 3/6] JNA: simplify freeing memory for C strings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Make use of the CString class introduced recently and get rid of some
boilerplate code required to release memory at the calling side.
---
 src/main/java/org/libvirt/Connect.java        | 22 +++---------------
 src/main/java/org/libvirt/Domain.java         | 33 +++++++--------------------
 src/main/java/org/libvirt/DomainSnapshot.java |  8 +------
 src/main/java/org/libvirt/Interface.java      |  7 +-----
 src/main/java/org/libvirt/Network.java        | 14 ++----------
 src/main/java/org/libvirt/StorageVol.java     | 16 ++-----------
 src/main/java/org/libvirt/jna/Libvirt.java    | 26 ++++++++++-----------
 7 files changed, 30 insertions(+), 96 deletions(-)

diff --git a/src/main/java/org/libvirt/Connect.java b/src/main/java/org/libvirt/Connect.java
index d0bdc4d..437f423 100644
--- a/src/main/java/org/libvirt/Connect.java
+++ b/src/main/java/org/libvirt/Connect.java
@@ -978,12 +978,7 @@ public class Connect {
      *      description</a>
      */
     public String getCapabilities() throws LibvirtException {
-        Pointer ptr = processError(libvirt.virConnectGetCapabilities(VCP));
-        try {
-            return Library.getString(ptr);
-        } finally {
-            Library.free(ptr);
-        }
+        return processError(libvirt.virConnectGetCapabilities(VCP)).toString();
     }
 
     /**
@@ -1014,12 +1009,7 @@ public class Connect {
      * @throws LibvirtException
      */
     public String getHostName() throws LibvirtException {
-        Pointer ptr = processError(libvirt.virConnectGetHostname(VCP));
-        try {
-            return Library.getString(ptr);
-        } finally {
-            Library.free(ptr);
-        }
+        return processError(libvirt.virConnectGetHostname(VCP)).toString();
     }
 
     /**
@@ -1083,13 +1073,7 @@ public class Connect {
      * @since 1.5.2
      */
     public String getSysinfo() throws LibvirtException {
-        Pointer p = processError(libvirt.virConnectGetSysinfo(this.VCP, 0));
-
-        try {
-            return Library.getString(p);
-        } finally {
-            Library.free(p);
-        }
+        return processError(libvirt.virConnectGetSysinfo(this.VCP, 0)).toString();
     }
 
     /**
diff --git a/src/main/java/org/libvirt/Domain.java b/src/main/java/org/libvirt/Domain.java
index ab646de..087a06f 100644
--- a/src/main/java/org/libvirt/Domain.java
+++ b/src/main/java/org/libvirt/Domain.java
@@ -4,6 +4,7 @@ import java.nio.ByteBuffer;
 import java.util.concurrent.TimeUnit;
 
 import org.libvirt.event.IOErrorListener;
+import org.libvirt.jna.CString;
 import org.libvirt.jna.DomainPointer;
 import org.libvirt.jna.DomainSnapshotPointer;
 import org.libvirt.jna.Libvirt;
@@ -589,12 +590,7 @@ public class Domain {
      * @throws LibvirtException
      */
     public String getOSType() throws LibvirtException {
-        Pointer ptr = processError(libvirt.virDomainGetOSType(VDP));
-        try {
-            return Library.getString(ptr);
-        } finally {
-            Library.free(ptr);
-        }
+        return processError(libvirt.virDomainGetOSType(VDP)).toString();
     }
 
     /**
@@ -605,7 +601,7 @@ public class Domain {
      */
     public SchedParameter[] getSchedulerParameters() throws LibvirtException {
         IntByReference nParams = new IntByReference();
-        Library.free(processError(libvirt.virDomainGetSchedulerType(VDP, nParams)));
+        processError(libvirt.virDomainGetSchedulerType(VDP, nParams));
 
         int n = nParams.getValue();
 
@@ -637,12 +633,7 @@ public class Domain {
      * @throws LibvirtException
      */
     public String getSchedulerType() throws LibvirtException {
-        Pointer pScheduler = processError(libvirt.virDomainGetSchedulerType(VDP, null));
-        try {
-            return Library.getString(pScheduler);
-        } finally {
-            Library.free(pScheduler);
-        }
+        return processError(libvirt.virDomainGetSchedulerType(VDP, null)).toString();
     }
 
     /**
@@ -725,12 +716,7 @@ public class Domain {
      *      Description format </a>
      */
     public String getXMLDesc(int flags) throws LibvirtException {
-        Pointer ptr = processError(libvirt.virDomainGetXMLDesc(VDP, flags));
-        try {
-            return Library.getString(ptr);
-        } finally {
-            Library.free(ptr);
-        }
+        return processError(libvirt.virDomainGetXMLDesc(VDP, flags)).toString();
     }
 
     /**
@@ -1290,13 +1276,10 @@ public class Domain {
     }
 
     public String screenshot(Stream stream, int screen) throws LibvirtException {
-        Pointer ptr = processError(libvirt.virDomainScreenshot(this.VDP, stream.getVSP(), screen, 0));
+        CString mimeType = libvirt.virDomainScreenshot(this.VDP, stream.getVSP(), screen, 0);
+        processError(mimeType);
         stream.markReadable();
-        try {
-            return Library.getString(ptr);
-        } finally {
-            Library.free(ptr);
-        }
+        return mimeType.toString();
     }
 
     /**
diff --git a/src/main/java/org/libvirt/DomainSnapshot.java b/src/main/java/org/libvirt/DomainSnapshot.java
index 3013117..ad49724 100644
--- a/src/main/java/org/libvirt/DomainSnapshot.java
+++ b/src/main/java/org/libvirt/DomainSnapshot.java
@@ -73,12 +73,6 @@ public class DomainSnapshot {
      * @return the XML document
      */
     public String getXMLDesc() throws LibvirtException {
-        Pointer p = processError(libvirt.virDomainSnapshotGetXMLDesc(VDSP, 0));
-
-        try {
-            return Library.getString(p);
-        } finally {
-            Library.free(p);
-        }
+        return processError(libvirt.virDomainSnapshotGetXMLDesc(VDSP, 0)).toString();
     }
 }
diff --git a/src/main/java/org/libvirt/Interface.java b/src/main/java/org/libvirt/Interface.java
index 4c9670c..2c29a35 100644
--- a/src/main/java/org/libvirt/Interface.java
+++ b/src/main/java/org/libvirt/Interface.java
@@ -120,12 +120,7 @@ public class Interface {
      * @throws LibvirtException
      */
     public String getXMLDescription(int flags) throws LibvirtException {
-        Pointer xml = processError(libvirt.virInterfaceGetXMLDesc(VIP, flags));
-        try {
-            return Library.getString(xml);
-        } finally {
-            Library.free(xml);
-        }
+        return processError(libvirt.virInterfaceGetXMLDesc(VIP, flags)).toString();
     }
 
     /**
diff --git a/src/main/java/org/libvirt/Network.java b/src/main/java/org/libvirt/Network.java
index 2a77028..9f493f7 100644
--- a/src/main/java/org/libvirt/Network.java
+++ b/src/main/java/org/libvirt/Network.java
@@ -103,12 +103,7 @@ public class Network {
      * @throws LibvirtException
      */
     public String getBridgeName() throws LibvirtException {
-        final Pointer ptr = processError(libvirt.virNetworkGetBridgeName(VNP));
-        try {
-            return Library.getString(ptr);
-        } finally {
-            Library.free(ptr);
-        }
+        return processError(libvirt.virNetworkGetBridgeName(VNP)).toString();
     }
 
     /**
@@ -167,12 +162,7 @@ public class Network {
      * @throws LibvirtException
      */
     public String getXMLDesc(int flags) throws LibvirtException {
-        Pointer ptr = processError(libvirt.virNetworkGetXMLDesc(VNP, flags));
-        try {
-            return Library.getString(ptr);
-        } finally {
-            Library.free(ptr);
-        }
+        return processError(libvirt.virNetworkGetXMLDesc(VNP, flags)).toString();
     }
 
     /**
diff --git a/src/main/java/org/libvirt/StorageVol.java b/src/main/java/org/libvirt/StorageVol.java
index 47b79f6..3b1533e 100644
--- a/src/main/java/org/libvirt/StorageVol.java
+++ b/src/main/java/org/libvirt/StorageVol.java
@@ -163,13 +163,7 @@ public class StorageVol {
      * @throws LibvirtException
      */
     public String getPath() throws LibvirtException {
-        Pointer p = processError(libvirt.virStorageVolGetPath(VSVP));
-
-        try {
-            return Library.getString(p);
-        } finally {
-            Library.free(p);
-        }
+        return processError(libvirt.virStorageVolGetPath(VSVP)).toString();
     }
 
     /**
@@ -181,13 +175,7 @@ public class StorageVol {
      * @throws LibvirtException
      */
     public String getXMLDesc(int flags) throws LibvirtException {
-        Pointer p = processError(libvirt.virStorageVolGetXMLDesc(VSVP, flags));
-
-        try {
-            return Library.getString(p);
-        } finally {
-            Library.free(p);
-        }
+        return processError(libvirt.virStorageVolGetXMLDesc(VSVP, flags)).toString();
     }
 
     /**
diff --git a/src/main/java/org/libvirt/jna/Libvirt.java b/src/main/java/org/libvirt/jna/Libvirt.java
index 5a176b0..c6b7153 100644
--- a/src/main/java/org/libvirt/jna/Libvirt.java
+++ b/src/main/java/org/libvirt/jna/Libvirt.java
@@ -176,11 +176,11 @@ public interface Libvirt extends Library {
     int virConnectIsEncrypted(ConnectionPointer virConnectPtr) ;
     int virConnectIsSecure(ConnectionPointer virConnectPtr) ;
     CString virConnectFindStoragePoolSources(ConnectionPointer virConnectPtr, String type, String srcSpec, int flags);
-    Pointer virConnectGetCapabilities(ConnectionPointer virConnectPtr);
-    Pointer virConnectGetHostname(ConnectionPointer virConnectPtr);
+    CString virConnectGetCapabilities(ConnectionPointer virConnectPtr);
+    CString virConnectGetHostname(ConnectionPointer virConnectPtr);
     int virConnectGetLibVersion(ConnectionPointer virConnectPtr, LongByReference libVer);
     int virConnectGetMaxVcpus(ConnectionPointer virConnectPtr, String type);
-    Pointer virConnectGetSysinfo(ConnectionPointer virConnectPtr, int flags);
+    CString virConnectGetSysinfo(ConnectionPointer virConnectPtr, int flags);
     String virConnectGetType(ConnectionPointer virConnectPtr);
     CString virConnectGetURI(ConnectionPointer virConnectPtr);
     int virConnectGetVersion(ConnectionPointer virConnectPtr, LongByReference hvVer);
@@ -261,14 +261,14 @@ public interface Libvirt extends Library {
     NativeLong virDomainGetMaxMemory(DomainPointer virDomainPtr);
     int virDomainGetMaxVcpus(DomainPointer virDomainPtr);
     String virDomainGetName(DomainPointer virDomainPtr);
-    Pointer virDomainGetOSType(DomainPointer virDomainPtr);
+    CString virDomainGetOSType(DomainPointer virDomainPtr);
     int virDomainGetSchedulerParameters(DomainPointer virDomainPtr, virSchedParameter[] params,
             IntByReference nparams);
-    Pointer virDomainGetSchedulerType(DomainPointer virDomainPtr, IntByReference nparams);
+    CString virDomainGetSchedulerType(DomainPointer virDomainPtr, IntByReference nparams);
     int virDomainGetUUID(DomainPointer virDomainPtr, byte[] uuidString);
     int virDomainGetUUIDString(DomainPointer virDomainPtr, byte[] uuidString);
     int virDomainGetVcpus(DomainPointer virDomainPtr, virVcpuInfo[] info, int maxInfo, byte[] cpumaps, int maplen);
-    Pointer virDomainGetXMLDesc(DomainPointer virDomainPtr, int flags);
+    CString virDomainGetXMLDesc(DomainPointer virDomainPtr, int flags);
     int virDomainHasCurrentSnapshot(DomainPointer virDomainPtr, int flags);
     int virDomainHasManagedSaveImage(DomainPointer virDomainPtr, int flags);
     int virDomainInterfaceStats(DomainPointer virDomainPtr, String path, virDomainInterfaceStats stats, SizeT size);
@@ -303,7 +303,7 @@ public interface Libvirt extends Library {
     int virDomainResume(DomainPointer virDomainPtr);
     int virDomainSave(DomainPointer virDomainPtr, String to);
     int virDomainSendKey(DomainPointer virDomainPtr, int codeset, int holdtime, int[] keycodes, int nkeycodes, int flags);
-    Pointer virDomainScreenshot(DomainPointer virDomainPtr, StreamPointer virStreamPtr, int screen, int flags);
+    CString virDomainScreenshot(DomainPointer virDomainPtr, StreamPointer virStreamPtr, int screen, int flags);
     int virDomainSetAutostart(DomainPointer virDomainPtr, int autoStart);
     int virDomainSetMaxMemory(DomainPointer virDomainPtr, NativeLong maxMemory);
     int virDomainSetMemory(DomainPointer virDomainPtr, NativeLong maxMemory);
@@ -322,11 +322,11 @@ public interface Libvirt extends Library {
     int virNetworkDestroy(NetworkPointer virConnectPtr);
     int virNetworkFree(NetworkPointer virConnectPtr);
     int virNetworkGetAutostart(NetworkPointer virNetworkPtr, IntByReference value);
-    Pointer virNetworkGetBridgeName(NetworkPointer virNetworkPtr);
+    CString virNetworkGetBridgeName(NetworkPointer virNetworkPtr);
     String virNetworkGetName(NetworkPointer virNetworkPtr);
     int virNetworkGetUUID(NetworkPointer virNetworkPtr, byte[] uuidString);
     int virNetworkGetUUIDString(NetworkPointer virNetworkPtr, byte[] uuidString);
-    Pointer virNetworkGetXMLDesc(NetworkPointer virNetworkPtr, int flags);
+    CString virNetworkGetXMLDesc(NetworkPointer virNetworkPtr, int flags);
     int virNetworkIsActive(NetworkPointer virNetworkPtr);
     int virNetworkIsPersistent(NetworkPointer virNetworkPtr);
     NetworkPointer virNetworkLookupByName(ConnectionPointer virConnectPtr, String name);
@@ -393,8 +393,8 @@ public interface Libvirt extends Library {
     int virStorageVolGetInfo(StorageVolPointer storageVolPtr, virStorageVolInfo info);
     String virStorageVolGetKey(StorageVolPointer storageVolPtr);
     String virStorageVolGetName(StorageVolPointer storageVolPtr);
-    Pointer virStorageVolGetPath(StorageVolPointer storageVolPtr);
-    Pointer virStorageVolGetXMLDesc(StorageVolPointer storageVolPtr, int flags);
+    CString virStorageVolGetPath(StorageVolPointer storageVolPtr);
+    CString virStorageVolGetXMLDesc(StorageVolPointer storageVolPtr, int flags);
     StorageVolPointer virStorageVolLookupByKey(ConnectionPointer virConnectPtr, String name);
     StorageVolPointer virStorageVolLookupByName(StoragePoolPointer storagePoolPtr, String name);
     StorageVolPointer virStorageVolLookupByPath(ConnectionPointer virConnectPtr, String path);
@@ -408,7 +408,7 @@ public interface Libvirt extends Library {
     int virInterfaceFree(InterfacePointer virDevicePointer);
     String virInterfaceGetName(InterfacePointer virInterfacePtr);
     String virInterfaceGetMACString(InterfacePointer virInterfacePtr);
-    Pointer virInterfaceGetXMLDesc(InterfacePointer virInterfacePtr, int flags);
+    CString virInterfaceGetXMLDesc(InterfacePointer virInterfacePtr, int flags);
     int virInterfaceIsActive(InterfacePointer virDevicePointer);
     InterfacePointer virInterfaceLookupByMACString(ConnectionPointer virConnectPtr, String mac);
     InterfacePointer virInterfaceLookupByName(ConnectionPointer virConnectPtr, String name);
@@ -448,7 +448,7 @@ public interface Libvirt extends Library {
     DomainSnapshotPointer virDomainSnapshotCreateXML(DomainPointer virDomainPtr, String xmlDesc, int flags);
     DomainSnapshotPointer virDomainSnapshotCurrent(DomainPointer virDomainPtr, int flags);
     int virDomainSnapshotDelete(DomainSnapshotPointer virDomainSnapshotPtr, int flags);
-    Pointer virDomainSnapshotGetXMLDesc(DomainSnapshotPointer virDomainSnapshotPtr, int flags);
+    CString virDomainSnapshotGetXMLDesc(DomainSnapshotPointer virDomainSnapshotPtr, int flags);
     int virDomainSnapshotFree(DomainSnapshotPointer virDomainSnapshotPtr);
     int virDomainSnapshotListNames(DomainPointer virDomainPtr, Pointer[] names, int nameslen, int flags);
     DomainSnapshotPointer virDomainSnapshotLookupByName(DomainPointer virDomainPtr, String name, int flags);
-- 
2.2.2

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




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]