#ifndef TUPMACS_H
#define TUPMACS_H
#include "catalog/pg_type_d.h"
static inline bool
att_isnull(int ATT, const bits8 *BITS)
{
return !(BITS[ATT >> 3] & (1 << (ATT & 0x07)));
}
#ifndef FRONTEND
#define fetchatt(A,T) fetch_att(T, (A)->attbyval, (A)->attlen)
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
case sizeof(Datum):
return *((const Datum *) T);
#endif
default:
elog(ERROR, "unsupported byval length: %d", attlen);
return 0;
}
}
else
return PointerGetDatum(T);
}
#endif
#define att_align_datum(cur_offset, attalign, attlen, attdatum) \
( \
((attlen) == -1 && VARATT_IS_SHORT(DatumGetPointer(attdatum))) ? \
(uintptr_t) (cur_offset) : \
att_align_nominal(cur_offset, attalign) \
)
#define att_datum_alignby(cur_offset, attalignby, attlen, attdatum) \
( \
((attlen) == -1 && VARATT_IS_SHORT(DatumGetPointer(attdatum))) ? \
(uintptr_t) (cur_offset) : \
TYPEALIGN(attalignby, cur_offset))
#define att_align_pointer(cur_offset, attalign, attlen, attptr) \
( \
((attlen) == -1 && VARATT_NOT_PAD_BYTE(attptr)) ? \
(uintptr_t) (cur_offset) : \
att_align_nominal(cur_offset, attalign) \
)
#define att_pointer_alignby(cur_offset, attalignby, attlen, attptr) \
( \
((attlen) == -1 && VARATT_NOT_PAD_BYTE(attptr)) ? \
(uintptr_t) (cur_offset) : \
TYPEALIGN(attalignby, cur_offset))
#define att_align_nominal(cur_offset, attalign) \
( \
((attalign) == TYPALIGN_INT) ? INTALIGN(cur_offset) : \
(((attalign) == TYPALIGN_CHAR) ? (uintptr_t) (cur_offset) : \
(((attalign) == TYPALIGN_DOUBLE) ? DOUBLEALIGN(cur_offset) : \
( \
AssertMacro((attalign) == TYPALIGN_SHORT), \
SHORTALIGN(cur_offset) \
))) \
)
#define att_nominal_alignby(cur_offset, attalignby) \
TYPEALIGN(attalignby, cur_offset)
#define att_addlength_datum(cur_offset, attlen, attdatum) \
att_addlength_pointer(cur_offset, attlen, DatumGetPointer(attdatum))
#define att_addlength_pointer(cur_offset, attlen, attptr) \
( \
((attlen) > 0) ? \
( \
(cur_offset) + (attlen) \
) \
: (((attlen) == -1) ? \
( \
(cur_offset) + VARSIZE_ANY(attptr) \
) \
: \
( \
AssertMacro((attlen) == -2), \
(cur_offset) + (strlen((char *) (attptr)) + 1) \
)) \
)
#ifndef FRONTEND
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
case sizeof(Datum):
*(Datum *) T = newdatum;
break;
#endif
default:
elog(ERROR, "unsupported byval length: %d", attlen);
}
}
#endif
#endif