libvirt/src/util
Laine Stump 85b22f528f util: add VIR_(APPEND|INSERT|DELETE)_ELEMENT
I noticed when writing the backend functions for virNetworkUpdate that
I was repeating the same sequence of memmove, VIR_REALLOC, nXXX-- (and
messed up the args to memmove at least once), and had seen the same
sequence in a lot of other places, so I decided to write a few
utility functions/macros - see the .h file for full documentation.

The intent is to reduce the number of lines of code, but more
importantly to eliminate the need to check the element size and
element count arithmetic every time we need to do this (I *always*
make at least one mistake.)

VIR_INSERT_ELEMENT: insert one element at an arbitrary index within an
  array of objects. The size of each object is determined
  automatically by the macro using sizeof(*array). The new element's
  contents are copied into the inserted space, then the original copy
  of contents are 0'ed out (if everything else was
  successful). Compile-time assignment and size compatibility between
  the array and the new element is guaranteed (see explanation below
  [*])

VIR_INSERT_ELEMENT_COPY: identical to VIR_INSERT_ELEMENT, except that
  the original contents of newelem are not cleared to 0 (i.e. a copy
  is made).

VIR_APPEND_ELEMENT: This is just a special case of VIR_INSERT_ELEMENT
  that "inserts" one past the current last element.

VIR_APPEND_ELEMENT_COPY: identical to VIR_APPEND_ELEMENT, except that
  the original contents of newelem are not cleared to 0 (i.e. a copy
  is made).

VIR_DELETE_ELEMENT: delete one element at an arbitrary index within an
  array of objects. It's assumed that the element being deleted is
  already saved elsewhere (or cleared, if that's what is appropriate).

All five of these macros have an _INPLACE variant, which skips the
memory re-allocation of the array, assuming that the caller has
already done it (when inserting) or will do it later (when deleting).

Note that VIR_DELETE_ELEMENT* can return a failure, but only if an
invalid index is given (index + amount to delete is > current array
size), so in most cases you can safely ignore the return (that's why
the helper function virDeleteElementsN isn't declared with
ATTRIBUTE_RETURN_CHECK). A warning is logged if this ever happens,
since it is surely a coding error.

[*] One initial problem with the INSERT and APPEND macros was that,
due to both the array pointer and newelem pointer being cast to void*
when passing to virInsertElementsN(), any chance of type-checking was
lost. If we were going to move in newelem with a memmove anyway, we
would be no worse off for this. However, most current open-coded
insert/append operations use direct struct assignment to move the new
element into place (or just populate the new element directly) - thus
use of the new macros would open a possibility for new usage errors
that didn't exist before (e.g. accidentally sending &newelemptr rather
than newelemptr - I actually did this quite a lot in my test
conversions of existing code).

But thanks to Eric Blake's clever thinking, I was able to modify the
INSERT and APPEND macros so that they *do* check for both assignment
and size compatibility of *ptr (an element in the array) and newelem
(the element being copied into the new position of the array). This is
done via clever use of the C89-guaranteed fact that the sizeof()
operator must have *no* side effects (so an assignment inside sizeof()
is checked for validity, but not actually evaluated), and the fact
that virInsertElementsN has a "# of new elements" argument that we
want to always be 1.
2012-12-11 05:49:44 -05:00
..
bitmap.c bitmap: fix typo to use UL type of integer constant in virBitmapIsAllSet 2012-11-28 18:30:28 +08:00
bitmap.h bitmap: add virBitmapCountBits 2012-10-25 11:19:23 -06:00
buf.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
buf.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
cgroup.c Turn some dual-state int parameters into booleans 2012-11-29 16:14:43 +00:00
cgroup.h Turn some dual-state int parameters into booleans 2012-11-29 16:14:43 +00:00
command.c Remove spurious whitespace between function name & open brackets 2012-11-02 13:36:49 +00:00
command.h Move virProcess{Kill,Abort,TranslateStatus} into virprocess.{c,h} 2012-09-26 10:09:57 +01:00
conf.c Remove spurious whitespace between function name & open brackets 2012-11-02 13:36:49 +00:00
conf.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
dnsmasq.c Revert "dnsmasq: Fix parsing of the version number" 2012-12-10 14:00:02 +01:00
dnsmasq.h util: capabilities detection for dnsmasq 2012-11-29 15:02:39 -05:00
ebtables.c Remove spurious whitespace between function name & open brackets 2012-11-02 13:36:49 +00:00
ebtables.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
event_poll.c Skip deleted timers when calculting next timeout 2012-11-23 10:11:55 +00:00
event_poll.h Fix indentions 2012-12-03 09:58:57 +08:00
event.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
event.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
hooks.c hooks: let virCommand do the error reporting 2012-10-09 08:41:53 -06:00
hooks.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
hostusb.c qemu: Add option to treat missing USB devices as success 2012-10-11 15:11:41 +02:00
hostusb.h qemu: Add option to treat missing USB devices as success 2012-10-11 15:11:41 +02:00
iohelper.c iohelper: Don't report errors on special FDs 2012-11-05 16:55:42 +01:00
iptables.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
iptables.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
json.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
json.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
keymaps.csv Rsync keymaps.csv file with GTK-VNC 2012-01-12 20:44:55 +00:00
logging.c Use helper functions to format the journal iov array 2012-11-14 20:20:02 -07:00
logging.h Add metadata to virLogOutputFunc 2012-11-14 19:14:07 -07:00
memory.c util: add VIR_(APPEND|INSERT|DELETE)_ELEMENT 2012-12-11 05:49:44 -05:00
memory.h util: add VIR_(APPEND|INSERT|DELETE)_ELEMENT 2012-12-11 05:49:44 -05:00
pci.c pci: Fix building of 32bit PCI command array 2012-12-05 14:04:54 +01:00
pci.h util: Slightly refactor PCI list functions 2012-12-05 13:45:34 +01:00
processinfo.c util: fix virBitmap allocation in virProcessInfoGetAffinity 2012-11-29 10:10:08 -07:00
processinfo.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
sexpr.c Convert (nearly) all files in src/util/ to use virReportError() 2012-07-18 16:31:35 +01:00
sexpr.h build: fix PROBE() usage of intptr_t 2012-08-09 15:40:42 -06:00
stats_linux.c Remove spurious whitespace between function name & open brackets 2012-11-02 13:36:49 +00:00
stats_linux.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
storage_file.c qemu: Stop recursive detection of image chains when an image is missing 2012-11-22 16:04:17 +01:00
storage_file.h storage: let format probing work on root-squash NFS 2012-10-22 09:04:57 -06:00
sysinfo.c Remove spurious whitespace between function name & open brackets 2012-11-02 13:36:49 +00:00
sysinfo.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
threadpool.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
threadpool.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
threads-pthread.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
threads-pthread.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
threads-win32.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
threads-win32.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
threads.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
threads.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
util.c qemu: Allow the user to specify vendor and product for disk 2012-12-07 16:53:27 +08:00
util.h qemu: Allow the user to specify vendor and product for disk 2012-12-07 16:53:27 +08:00
uuid.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
uuid.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
viratomic.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
viratomic.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
viraudit.c Add a metadata parameter to virLog{, V}Message 2012-11-14 19:08:31 -07:00
viraudit.h Change logging category parameter into an enum 2012-09-28 10:39:28 +01:00
virauth.c Fix crash accessing a NULL URI when looking up auth credentials 2012-09-21 10:13:53 +01:00
virauth.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virauthconfig.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virauthconfig.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virdbus.c virdbus: Add virDBusGetSessionBus helper 2012-11-27 19:37:00 +00:00
virdbus.h virdbus: Add virDBusGetSessionBus helper 2012-11-27 19:37:00 +00:00
virfile.c util: check for NULL parameter in virFileWrapperFdCatchError 2012-11-29 00:00:39 +08:00
virfile.h qemu: Report errors from iohelper 2012-10-29 17:04:26 +01:00
virhash.c Fix some typos in messages, docs and comments. 2012-08-22 15:34:07 -06:00
virhash.h Implement virHashRemoveAll function 2012-04-19 10:21:43 -04:00
virhashcode.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virhashcode.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virinitctl.c Add APIs for talking to init via /dev/initctl 2012-11-30 19:17:30 +00:00
virinitctl.h Add APIs for talking to init via /dev/initctl 2012-11-30 19:17:30 +00:00
virkeycode-mapgen.py Fix keymap used to talk with QEMU 2011-08-26 14:18:57 +01:00
virkeycode.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virkeycode.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virkeyfile.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virkeyfile.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virlockspace.c util: fix index when building lock owners array 2012-11-14 12:43:49 -05:00
virlockspace.h Add JSON serialization of virLockSpacePtr objects for process re-exec() 2012-10-16 15:45:55 +01:00
virmacaddr.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virmacaddr.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnetdev.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnetdev.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnetdevbandwidth.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnetdevbandwidth.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnetdevbridge.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnetdevbridge.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnetdevmacvlan.c Specify name of target interface with macvlan error 2012-11-27 17:02:22 +00:00
virnetdevmacvlan.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnetdevopenvswitch.c openvswitch: Add utility functions for getting and setting Open vSwitch per-port data 2012-10-23 15:26:04 -04:00
virnetdevopenvswitch.h openvswitch: Add utility functions for getting and setting Open vSwitch per-port data 2012-10-23 15:26:04 -04:00
virnetdevtap.c network: fix dnsmasq/radvd binding to IPv6 on recent kernels 2012-09-27 11:17:52 -06:00
virnetdevtap.h network: fix dnsmasq/radvd binding to IPv6 on recent kernels 2012-09-27 11:17:52 -06:00
virnetdevveth.c Fix error reporting in virNetDevVethDelete 2012-11-27 17:59:28 +00:00
virnetdevveth.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnetdevvlan.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnetdevvlan.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnetdevvportprofile.c Correct checking of virStrcpyStatic() return value 2012-10-03 21:17:13 -04:00
virnetdevvportprofile.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnetlink.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnetlink.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnodesuspend.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virnodesuspend.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virobject.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virobject.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virpidfile.c Don't assume pid_t is the same size as an int 2012-11-01 09:16:04 +00:00
virpidfile.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virprocess.c Don't assume pid_t is the same size as an int 2012-11-01 09:16:04 +00:00
virprocess.h Move most of qemuProcessKill into virProcessKillPainfully 2012-09-27 10:11:44 +01:00
virrandom.c maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virrandom.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virsocketaddr.c util: new virSocketAddrIsPrivate function 2012-11-29 15:02:39 -05:00
virsocketaddr.h util: new virSocketAddrIsPrivate function 2012-11-29 15:02:39 -05:00
virstring.c Introduce APIs for splitting/joining strings 2012-11-30 20:05:43 +00:00
virstring.h Introduce APIs for splitting/joining strings 2012-11-30 20:05:43 +00:00
virterror_internal.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virterror.c Add APIs for talking to init via /dev/initctl 2012-11-30 19:17:30 +00:00
virtime.c Remove spurious whitespace between function name & open brackets 2012-11-02 13:36:49 +00:00
virtime.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
virtypedparam.c maint: Misc whitespace cleanups 2012-12-03 15:13:32 +01:00
virtypedparam.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
viruri.c Remove spurious whitespace between function name & open brackets 2012-11-02 13:36:49 +00:00
viruri.h Fix crash accessing a NULL URI when looking up auth credentials 2012-09-21 10:13:53 +01:00
xml.c Remove spurious whitespace between function name & open brackets 2012-11-02 13:36:49 +00:00
xml.h xml: print uuids in the warning 2012-10-29 14:38:43 +01:00