Convert macros to static inline functions (tupmacs.h)
authorPeter Eisentraut <peter@eisentraut.org>
Mon, 18 Jul 2022 05:43:39 +0000 (07:43 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Mon, 18 Jul 2022 06:01:27 +0000 (08:01 +0200)
Reviewed-by: Amul Sul <sulamul@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/5b558da8-99fb-0a99-83dd-f72f05388517%40enterprisedb.com

src/include/access/itup.h
src/include/access/tupmacs.h

index 7458bc2fda60e4bb0cc0f614f937dee1a75dcafd..8bd852b15db397b4e0044317fa3a9a87ea74e53f 100644 (file)
@@ -114,7 +114,7 @@ typedef IndexAttributeBitMapData * IndexAttributeBitMap;
    ) \
    : \
    ( \
-       (att_isnull((attnum)-1, (char *)(tup) + sizeof(IndexTupleData))) ? \
+       (att_isnull((attnum)-1, (bits8 *)(tup) + sizeof(IndexTupleData))) ? \
        ( \
            *(isnull) = true, \
            (Datum)NULL \
index 16c74a581e4c71c69dc86aa7cebbd86e1c37da2b..8b24cd365869efedfc5503408f6b1c4eaa16c4cc 100644 (file)
  * Note that a 0 in the null bitmap indicates a null, while 1 indicates
  * non-null.
  */
-#define att_isnull(ATT, BITS) (!((BITS)[(ATT) >> 3] & (1 << ((ATT) & 0x07))))
+static inline bool
+att_isnull(int ATT, const bits8 *BITS)
+{
+   return !(BITS[ATT >> 3] & (1 << (ATT & 0x07)));
+}
 
+#ifndef FRONTEND
 /*
  * Given a Form_pg_attribute and a pointer into a tuple's data area,
  * return the correct value or pointer.
 /*
  * Same, but work from byval/len parameters rather than Form_pg_attribute.
  */
+static inline Datum
+fetch_att(const void *T, bool attbyval, int attlen)
+{
+   if (attbyval)
+   {
+       switch (attlen)
+       {
+           case sizeof(char):
+               return CharGetDatum(*((const char *) T));
+           case sizeof(int16):
+               return Int16GetDatum(*((const int16 *) T));
+           case sizeof(int32):
+               return Int32GetDatum(*((const int32 *) T));
 #if SIZEOF_DATUM == 8
-
-#define fetch_att(T,attbyval,attlen) \
-( \
-   (attbyval) ? \
-   ( \
-       (attlen) == (int) sizeof(Datum) ? \
-           *((Datum *)(T)) \
-       : \
-     ( \
-       (attlen) == (int) sizeof(int32) ? \
-           Int32GetDatum(*((int32 *)(T))) \
-       : \
-       ( \
-           (attlen) == (int) sizeof(int16) ? \
-               Int16GetDatum(*((int16 *)(T))) \
-           : \
-           ( \
-               AssertMacro((attlen) == 1), \
-               CharGetDatum(*((char *)(T))) \
-           ) \
-       ) \
-     ) \
-   ) \
-   : \
-   PointerGetDatum((char *) (T)) \
-)
-#else                          /* SIZEOF_DATUM != 8 */
-
-#define fetch_att(T,attbyval,attlen) \
-( \
-   (attbyval) ? \
-   ( \
-       (attlen) == (int) sizeof(int32) ? \
-           Int32GetDatum(*((int32 *)(T))) \
-       : \
-       ( \
-           (attlen) == (int) sizeof(int16) ? \
-               Int16GetDatum(*((int16 *)(T))) \
-           : \
-           ( \
-               AssertMacro((attlen) == 1), \
-               CharGetDatum(*((char *)(T))) \
-           ) \
-       ) \
-   ) \
-   : \
-   PointerGetDatum((char *) (T)) \
-)
-#endif                         /* SIZEOF_DATUM == 8 */
+           case sizeof(Datum):
+               return *((const Datum *) T);
+#endif
+           default:
+               elog(ERROR, "unsupported byval length: %d", attlen);
+               return 0;
+       }
+   }
+   else
+       return PointerGetDatum(T);
+}
+#endif                         /* FRONTEND */
 
 /*
  * att_align_datum aligns the given offset as needed for a datum of alignment
    )) \
 )
 
+#ifndef FRONTEND
 /*
  * store_att_byval is a partial inverse of fetch_att: store a given Datum
  * value into a tuple data area at the specified address.  However, it only
  * handles the byval case, because in typical usage the caller needs to
- * distinguish by-val and by-ref cases anyway, and so a do-it-all macro
+ * distinguish by-val and by-ref cases anyway, and so a do-it-all function
  * wouldn't be convenient.
  */
+static inline void
+store_att_byval(void *T, Datum newdatum, int attlen)
+{
+   switch (attlen)
+   {
+       case sizeof(char):
+           *(char *) T = DatumGetChar(newdatum);
+           break;
+       case sizeof(int16):
+           *(int16 *) T = DatumGetInt16(newdatum);
+           break;
+       case sizeof(int32):
+           *(int32 *) T = DatumGetInt32(newdatum);
+           break;
 #if SIZEOF_DATUM == 8
-
-#define store_att_byval(T,newdatum,attlen) \
-   do { \
-       switch (attlen) \
-       { \
-           case sizeof(char): \
-               *(char *) (T) = DatumGetChar(newdatum); \
-               break; \
-           case sizeof(int16): \
-               *(int16 *) (T) = DatumGetInt16(newdatum); \
-               break; \
-           case sizeof(int32): \
-               *(int32 *) (T) = DatumGetInt32(newdatum); \
-               break; \
-           case sizeof(Datum): \
-               *(Datum *) (T) = (newdatum); \
-               break; \
-           default: \
-               elog(ERROR, "unsupported byval length: %d", \
-                    (int) (attlen)); \
-               break; \
-       } \
-   } while (0)
-#else                          /* SIZEOF_DATUM != 8 */
-
-#define store_att_byval(T,newdatum,attlen) \
-   do { \
-       switch (attlen) \
-       { \
-           case sizeof(char): \
-               *(char *) (T) = DatumGetChar(newdatum); \
-               break; \
-           case sizeof(int16): \
-               *(int16 *) (T) = DatumGetInt16(newdatum); \
-               break; \
-           case sizeof(int32): \
-               *(int32 *) (T) = DatumGetInt32(newdatum); \
-               break; \
-           default: \
-               elog(ERROR, "unsupported byval length: %d", \
-                    (int) (attlen)); \
-               break; \
-       } \
-   } while (0)
-#endif                         /* SIZEOF_DATUM == 8 */
-
+       case sizeof(Datum):
+           *(Datum *) T = newdatum;
+           break;
 #endif
+       default:
+           elog(ERROR, "unsupported byval length: %d", attlen);
+   }
+}
+#endif                         /* FRONTEND */
+
+#endif                         /* TUPMACS_H */