#ifndef _SYS_MODEL_H
#define _SYS_MODEL_H
#ifdef __cplusplus
extern "C" {
#endif
#if defined(_KERNEL) && !defined(_ASM)
#include <sys/debug.h>
#endif
#include <sys/isa_defs.h>
#if defined(_KERNEL) || defined(_FAKE_KERNEL) || defined(_KMEMUSER)
#define DATAMODEL_MASK 0x0FF00000
#define DATAMODEL_ILP32 0x00100000
#define DATAMODEL_LP64 0x00200000
#define DATAMODEL_NONE 0
#if defined(_LP64)
#define DATAMODEL_NATIVE DATAMODEL_LP64
#elif defined(_ILP32)
#define DATAMODEL_NATIVE DATAMODEL_ILP32
#else
#error "No DATAMODEL_NATIVE specified"
#endif
#endif
#ifndef _ASM
typedef unsigned int model_t;
#endif
#if defined(_KERNEL) && !defined(_ASM)
#if defined(_LP64)
#define STRUCT_HANDLE(struct_type, handle) \
struct { \
union { \
struct struct_type##32 *m32; \
struct struct_type *m64; \
} ptr; \
model_t model; \
} handle = { NULL, DATAMODEL_ILP32 }
#define STRUCT_DECL(struct_type, handle) \
struct struct_type __##handle##_buf; \
STRUCT_HANDLE(struct_type, handle)
#define STRUCT_SET_HANDLE(handle, umodel, addr) \
(handle).model = (model_t)(umodel) & DATAMODEL_MASK; \
ASSERT(((umodel) & DATAMODEL_MASK) != DATAMODEL_NONE); \
((handle).ptr.m64) = (addr)
#define STRUCT_INIT(handle, umodel) \
STRUCT_SET_HANDLE(handle, umodel, &__##handle##_buf)
#define STRUCT_SIZE(handle) \
((handle).model == DATAMODEL_ILP32 ? \
sizeof (*(handle).ptr.m32) : \
sizeof (*(handle).ptr.m64))
#define STRUCT_FADDR(handle, field) \
((handle).model == DATAMODEL_ILP32 ? \
(void *)&(handle).ptr.m32->field : \
&(handle).ptr.m64->field)
#define STRUCT_FGET(handle, field) \
(((handle).model == DATAMODEL_ILP32) ? \
(handle).ptr.m32->field : \
(handle).ptr.m64->field)
#define STRUCT_FGETP(handle, field) \
((handle).model == DATAMODEL_ILP32 ? \
(void *)(uintptr_t)(handle).ptr.m32->field : \
(handle).ptr.m64->field)
#define STRUCT_FSET(handle, field, val) \
((handle).model == DATAMODEL_ILP32 ? \
((handle).ptr.m32->field = (val)) : \
((handle).ptr.m64->field = (val)))
#define STRUCT_FSETP(handle, field, val) \
((handle).model == DATAMODEL_ILP32 ? \
(void) ((handle).ptr.m32->field = (caddr32_t)(uintptr_t)(val)) : \
(void) ((handle).ptr.m64->field = (val)))
#define STRUCT_BUF(handle) ((handle).ptr.m64)
#define SIZEOF_PTR(umodel) \
(((umodel) & DATAMODEL_MASK) == DATAMODEL_ILP32 ? \
sizeof (caddr32_t) : \
sizeof (caddr_t))
#define SIZEOF_STRUCT(struct_type, umodel) \
(((umodel) & DATAMODEL_MASK) == DATAMODEL_ILP32 ? \
sizeof (struct struct_type##32) : \
sizeof (struct struct_type))
#else
#define STRUCT_HANDLE(struct_type, handle) \
struct { \
struct struct_type *ptr; \
model_t model; \
} handle = { NULL, DATAMODEL_ILP32 }
#define STRUCT_DECL(struct_type, handle) \
struct struct_type __##handle##_buf; \
STRUCT_HANDLE(struct_type, handle)
#define STRUCT_SET_HANDLE(handle, umodel, addr) \
(handle).model = (model_t)(umodel) & DATAMODEL_MASK; \
ASSERT(((umodel) & DATAMODEL_MASK) == DATAMODEL_ILP32); \
(handle).ptr = (addr)
#define STRUCT_INIT(handle, umodel) \
STRUCT_SET_HANDLE(handle, umodel, &__##handle##_buf)
#define STRUCT_SIZE(handle) (sizeof (*(handle).ptr))
#define STRUCT_FADDR(handle, field) (&(handle).ptr->field)
#define STRUCT_FGET(handle, field) ((handle).ptr->field)
#define STRUCT_FGETP STRUCT_FGET
#define STRUCT_FSET(handle, field, val) ((handle).ptr->field = (val))
#define STRUCT_FSETP STRUCT_FSET
#define STRUCT_BUF(handle) ((handle).ptr)
#define SIZEOF_PTR(umodel) sizeof (caddr_t)
#define SIZEOF_STRUCT(struct_type, umodel) sizeof (struct struct_type)
#endif
#if defined(_LP64) || defined(__lint)
struct _klwp;
extern model_t lwp_getdatamodel(struct _klwp *);
extern model_t get_udatamodel(void);
#else
#define lwp_getdatamodel(t) DATAMODEL_ILP32
#define get_udatamodel() DATAMODEL_ILP32
#endif
#endif
#ifdef __cplusplus
}
#endif
#endif