>From 9fdad64c895fd8ad96310f7dac6dee0928b574c5 Mon Sep 17 00:00:00 2001
From: Michal Zidek <mzidek@redhat.com>
Date: Mon, 26 Aug 2013 13:58:37 +0200
Subject: [PATCH] Rename SAFEALIGN macros

The new SAFEALIGN macros name turned to be inappropriate because
they do not reflect what the macros really do.
---
 src/util/util_safealign.h | 70 +++++++++++++++++++++++++++--------------------
 1 file changed, 40 insertions(+), 30 deletions(-)

diff --git a/src/util/util_safealign.h b/src/util/util_safealign.h
index d1d7a48..c2ff33f 100644
--- a/src/util/util_safealign.h
+++ b/src/util/util_safealign.h
@@ -45,66 +45,76 @@ safealign_memcpy(void *dest, const void *src, size_t n, size_t *counter)
     }
 }
 
-#define SAFEALIGN_VAR2BUF(dest, value, type, pctr) do { \
+#define SAFEALIGN_SETMEM_VALUE(dest, value, type, pctr) do { \
     type CV_MACRO_val = (type)(value); \
     safealign_memcpy(dest, &CV_MACRO_val, sizeof(type), pctr); \
 } while(0)
 
-#define SAFEALIGN_BUF2VAR_INT64(dest, src, pctr) \
+/* SAFEALIGN_COPY_INT64(void *dest, void *src, size_t *pctr)
+ * Will copy sizeof(int64_t) bytes from memory at address pointed by src
+ * to memory at address pointed by dest. If pctr is not NULL, the value
+ * of *pctr will be raised by the number of copied bytes. */
+#define SAFEALIGN_COPY_INT64(dest, src, pctr) \
     safealign_memcpy(dest, src, sizeof(int64_t), pctr)
 
-#define SAFEALIGN_VAR2BUF_INT64(dest, value, pctr) \
-    SAFEALIGN_VAR2BUF(dest, value, int64_t, pctr)
+/* SAFEALIGN_SETMEM_INT64(void *dest, int64_t value, size_t *pctr)
+ * Will store store given value to memory at address pointed by dest.
+ * If pctr is not NULL, the value of *pctr will be raised by the
+ * number of stored bytes (in this case sizeof(int64_t)). */
+#define SAFEALIGN_SETMEM_INT64(dest, value, pctr) \
+    SAFEALIGN_SETMEM_VALUE(dest, value, int64_t, pctr)
 
-#define SAFEALIGN_BUF2VAR_UINT32(dest, src, pctr) \
+/* SAFEALIGN_COPY_UINT32(void *dest, void *src, size_t *pctr) */
+#define SAFEALIGN_COPY_UINT32(dest, src, pctr) \
     safealign_memcpy(dest, src, sizeof(uint32_t), pctr)
 
-#define SAFEALIGN_VAR2BUF_UINT32(dest, value, pctr) \
-    SAFEALIGN_VAR2BUF(dest, value, uint32_t, pctr)
+/* SAFEALIGN_SETMEM_UINT32(void *dest, uint32_t value, size_t *pctr) */
+#define SAFEALIGN_SETMEM_UINT32(dest, value, pctr) \
+    SAFEALIGN_SETMEM_VALUE(dest, value, uint32_t, pctr)
 
-#define SAFEALIGN_BUF2VAR_INT32(dest, src, pctr) \
+/* SAFEALIGN_COPY_INT32(void *dest, void *src, size_t *pctr) */
+#define SAFEALIGN_COPY_INT32(dest, src, pctr) \
     safealign_memcpy(dest, src, sizeof(int32_t), pctr)
 
-#define SAFEALIGN_VAR2BUF_INT32(dest, value, pctr) \
-    SAFEALIGN_VAR2BUF(dest, value, int32_t, pctr)
+/* SAFEALIGN_SETMEM_INT32(void *dest, int32_t value, size_t *pctr) */
+#define SAFEALIGN_SETMEM_INT32(dest, value, pctr) \
+    SAFEALIGN_SETMEM_VALUE(dest, value, int32_t, pctr)
 
-#define SAFEALIGN_BUF2VAR_UINT16(dest, src, pctr) \
+/* SAFEALIGN_COPY_UINT16(void *dest, void *src, size_t *pctr) */
+#define SAFEALIGN_COPY_UINT16(dest, src, pctr) \
     safealign_memcpy(dest, src, sizeof(uint16_t), pctr)
 
-#define SAFEALIGN_VAR2BUF_UINT16(dest, value, pctr) \
-    SAFEALIGN_VAR2BUF(dest, value, uint16_t, pctr)
+/* SAFEALIGN_SETMEM_UINT16(void *dest, uint16_t value, size_t *pctr) */
+#define SAFEALIGN_SETMEM_UINT16(dest, value, pctr) \
+    SAFEALIGN_SETMEM_VALUE(dest, value, uint16_t, pctr)
 
-#define SAFEALIGN_BUF2VAR_UINT32_CHECK(dest, src, len, pctr) do { \
+/* These macros are the same as their equivalents without _CHECK suffix,
+ * but additionally make the caller return EINVAL immediatelly if *pctr
+ * would excceed len. */
+#define SAFEALIGN_COPY_UINT32_CHECK(dest, src, len, pctr) do { \
     if ((*(pctr) + sizeof(uint32_t)) > (len) || \
         SIZE_T_OVERFLOW(*(pctr), sizeof(uint32_t))) return EINVAL; \
     safealign_memcpy(dest, src, sizeof(uint32_t), pctr); \
 } while(0)
 
-#define SAFEALIGN_BUF2VAR_INT32_CHECK(dest, src, len, pctr) do { \
+#define SAFEALIGN_COPY_INT32_CHECK(dest, src, len, pctr) do { \
     if ((*(pctr) + sizeof(int32_t)) > (len) || \
         SIZE_T_OVERFLOW(*(pctr), sizeof(int32_t))) return EINVAL; \
     safealign_memcpy(dest, src, sizeof(int32_t), pctr); \
 } while(0)
 
-#define SAFEALIGN_BUF2VAR_UINT16_CHECK(dest, src, len, pctr) do { \
+#define SAFEALIGN_COPY_UINT16_CHECK(dest, src, len, pctr) do { \
     if ((*(pctr) + sizeof(uint16_t)) > (len) || \
         SIZE_T_OVERFLOW(*(pctr), sizeof(uint16_t))) return EINVAL; \
     safealign_memcpy(dest, src, sizeof(uint16_t), pctr); \
 } while(0)
 
-
-/* Do not use these aliases in new code. Use the macros above instead. */
-#define SAFEALIGN_SET_VALUE         SAFEALIGN_VAR2BUF
-#define SAFEALIGN_COPY_INT64        SAFEALIGN_BUF2VAR_INT64
-#define SAFEALIGN_SET_INT64         SAFEALIGN_VAR2BUF_INT64
-#define SAFEALIGN_COPY_UINT32       SAFEALIGN_BUF2VAR_UINT32
-#define SAFEALIGN_SET_UINT32        SAFEALIGN_VAR2BUF_UINT32
-#define SAFEALIGN_COPY_INT32        SAFEALIGN_BUF2VAR_INT32
-#define SAFEALIGN_SET_INT32         SAFEALIGN_VAR2BUF_INT32
-#define SAFEALIGN_COPY_UINT16       SAFEALIGN_BUF2VAR_UINT16
-#define SAFEALIGN_SET_UINT16        SAFEALIGN_VAR2BUF_UINT16
-#define SAFEALIGN_COPY_UINT32_CHECK SAFEALIGN_BUF2VAR_UINT32_CHECK
-#define SAFEALIGN_COPY_INT32_CHECK  SAFEALIGN_BUF2VAR_INT32_CHECK
-#define SAFEALIGN_COPY_UINT16_CHECK SAFEALIGN_BUF2VAR_UINT16_CHECK
+/* Aliases for backward compatibility. */
+#define SAFEALIGN_SET_VALUE SAFEALIGN_SETMEM_VALUE
+#define SAFEALIGN_SET_INT64 SAFEALIGN_SETMEM_INT64
+#define SAFEALIGN_SET_UINT32 SAFEALIGN_SETMEM_UINT32
+#define SAFEALIGN_SET_INT32 SAFEALIGN_SETMEM_INT32
+#define SAFEALIGN_SET_UINT16 SAFEALIGN_SETMEM_UINT16
+#define SAFEALIGN_SET_UINT32 SAFEALIGN_SETMEM_UINT32
 
 #endif /* _UTIL_SAFEALIGN_H */
-- 
1.7.11.2

