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

Re: [Libguestfs] [PATCH v2 3/4] common: Add isaligned helper module



On 08/03/2018 02:28 PM, Nir Soffer wrote:
is_aligned (size, align) returns true if size is aligned to align,
assuming that align is power of 2.

Suggested by Eric in
https://www.redhat.com/archives/libguestfs/2018-August/msg00036.html
---
  common/include/isaligned.h | 50 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 50 insertions(+)
  create mode 100644 common/include/isaligned.h


+/* Return true if size is a multiple of align. align must be power of 2.
+ *
+ * Suggested by Eric Blake.  See:
+ * https://www.redhat.com/archives/libguestfs/2018-August/msg00036.html

Do we need this comment in the code? I'm fine if it is just in the commit message.

+ */
+static inline bool
+is_aligned (unsigned int size, unsigned int align)
+{
+  return !(size & (align - 1));
+}

Should we assert() that align is indeed a power of 2, to make it harder for callers to misuse this function? Otherwise, looks okay to me.


--
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org


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