+CAPTIVE_ROUND_DOWN{,64}()
authorshort <>
Fri, 31 Jan 2003 19:40:54 +0000 (19:40 +0000)
committershort <>
Fri, 31 Jan 2003 19:40:54 +0000 (19:40 +0000)
+CAPTIVE_ROUND_DOWN_EXCEEDING{,64}()
+CAPTIVE_ROUND_UP{,64}()

src/libcaptive/include/captive/macros.h

index a6c82e1..d9ac486 100644 (file)
@@ -276,6 +276,68 @@ gsize r;
                (const gchar *)(_captive_strdup_alloca_string); \
                })
 
+
+/**
+ * CAPTIVE_ROUND_DOWN:
+ * @pointer: Arbitrary pointer type.
+ * @fragment: Amount of 'sizeof(char)' to align @pointer down to.
+ * This size will be typically a power of 2.
+ * Value less or equal to %0 is forbidden.
+ *
+ * General pointer down-rounding macro. Already aligned pointer is left as is.
+ *
+ * glib NOTE: YOU MAY NOT STORE POINTERS IN INTEGERS.
+ *
+ * @Returns: Down-rounded @pointer to the integer multiple of @fragment.
+ * Resulting pointer has the same type as @pointer.
+ */
+#define CAPTIVE_ROUND_DOWN(pointer,fragment) \
+               ((typeof(pointer))(((guint32)(pointer))-CAPTIVE_ROUND_DOWN_EXCEEDING((pointer),(fragment))))
+#define CAPTIVE_ROUND_DOWN64(pointer,fragment) \
+               ((typeof(pointer))(((guint64)(pointer))-CAPTIVE_ROUND_DOWN_EXCEEDING64((pointer),(fragment))))
+
+
+/**
+ * CAPTIVE_ROUND_DOWN_EXCEEDING:
+ * @pointer: Arbitrary pointer type.
+ * @fragment: Amount of 'sizeof(char)' to detect down-alignment amount of @pointer for.
+ * This size will be typically a power of 2.
+ * Value less or equal to %0 is forbidden.
+ *
+ * Detects current non-aligned amount of data exceeding over integer multiple of @fragment.
+ * It will return value %0 for an aligned pointer.
+ *
+ * glib NOTE: YOU MAY NOT STORE POINTERS IN INTEGERS.
+ *
+ * @Returns: #gsize typed number of bytes exceeding over integer multiple of @fragment.
+ */
+#define CAPTIVE_ROUND_DOWN_EXCEEDING(pointer,fragment) \
+               ((gsize)(((guint32)(pointer))%(fragment)))
+#define CAPTIVE_ROUND_DOWN_EXCEEDING64(pointer,fragment) \
+               ((gsize)(((guint64)(pointer))%(fragment)))
+
+
+/**
+ * CAPTIVE_ROUND_UP:
+ * @pointer: Arbitrary pointer type.
+ * @fragment: Amount of 'sizeof(char)' to align @pointer up to.
+ * This size will be typically a power of 2.
+ * Value less or equal to %0 is forbidden.
+ *
+ * General pointer up-rounding macro. Already aligned pointer is left as is.
+ *
+ * glib NOTE: YOU MAY NOT STORE POINTERS IN INTEGERS.
+ *
+ * @Returns: Up-rounded @pointer to the integer multiple of @fragment.
+ * Resulting pointer has the same type as @pointer.
+ */
+#define CAPTIVE_ROUND_UP(pointer,fragment) \
+               /* new retyping as 'pointer' gets passed as 'char *' to CAPTIVE_ROUND_DOWN(). */ \
+               ((typeof(pointer))(CAPTIVE_ROUND_DOWN(((guint32)(pointer))+(fragment)-1,(fragment))))
+#define CAPTIVE_ROUND_UP64(pointer,fragment) \
+               /* new retyping as 'pointer' gets passed as 'char *' to CAPTIVE_ROUND_DOWN(). */ \
+               ((typeof(pointer))(CAPTIVE_ROUND_DOWN64(((guint64)(pointer))+(fragment)-1,(fragment))))
+
 G_END_DECLS