[libvirt] [PATCH 1/3] Added VIR_ALLOC_VAR macro

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

 



Allocates a buffer containing a struct with variable sized array as the last member, useful for some ioctl and other APIs that return data in this way.
---
 src/util/memory.h |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/src/util/memory.h b/src/util/memory.h
index fc9e6c1..e7effd6 100644
--- a/src/util/memory.h
+++ b/src/util/memory.h
@@ -76,6 +76,24 @@ void virFree(void *ptrptr);
 #define VIR_ALLOC_N(ptr, count) virAllocN(&(ptr), sizeof(*(ptr)), (count))

 /**
+ * VIR_ALLOC_VAR:
+ * @ptr: pointer to hold address of allocated memory
+ * @type: element type of trailing array
+ * @count: number of array elements to allocate
+ *
+ * Allocate sizeof(*ptr) bytes plus an array of 'count' elements, each
+ * sizeof('type').  This sort of allocation is useful for receiving
+ * the data of certain ioctls and other APIs which return a struct in
+ * which the last element is an array of undefined length.  The caller
+ * of this type of API is expected to know the length of the array
+ * that will be returned and allocate a suitable buffer to contain the
+ * returned data.
+ *
+ * Returns -1 on failure, 0 on success
+ */
+#define VIR_ALLOC_VAR(ptr, type, count) virAlloc(&(ptr), sizeof(*(ptr)) + (sizeof(type) * count))
+
+/**
  * VIR_REALLOC_N:
  * @ptr: pointer to hold address of allocated memory
  * @count: number of elements to allocate
-- 
1.6.5.5

--
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]