is_aligned (size, align) returns true if size is aligned to align,
assuming that align is power of 2.
Suggested by Eric Blake in
https://www.redhat.com/archives/libguestfs/2018-August/msg00036.html
---
common/include/isaligned.h | 51 ++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+)
create mode 100644 common/include/isaligned.h
diff --git a/common/include/isaligned.h b/common/include/isaligned.h
new file mode 100644
index 0000000..e693820
--- /dev/null
+++ b/common/include/isaligned.h
@@ -0,0 +1,51 @@
+/* nbdkit
+ * Copyright (C) 2018 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef NBDKIT_ISALIGNED_H
+#define NBDKIT_ISALIGNED_H
+
+#include <assert.h>
+#include <stdbool.h>
+
+#include "ispowerof2.h"
+
+/* Return true if size is a multiple of align. align must be power of 2.
+ */
+static inline bool
+is_aligned (unsigned int size, unsigned int align)
+{
+ assert (is_power_of_2 (align));
+ return !(size & (align - 1));
+}
+
+#endif /* NBDKIT_ISALIGNED_H */
--
2.17.1