R_object_helper.h 3.77 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
/*
2
* A simple wrapper for accessing data in R object.
Guolin Ke's avatar
Guolin Ke committed
3
* Due to license issue, we cannot include R's header file, so use this simple wrapper instead.
4
* However, if R changes the way it defines objects, this file will need to be updated as well.
Guolin Ke's avatar
Guolin Ke committed
5
6
7
8
9
*/
#ifndef R_OBJECT_HELPER_H_
#define R_OBJECT_HELPER_H_

#include <cstdint>
Guolin Ke's avatar
Guolin Ke committed
10
#include <cstdio>
Guolin Ke's avatar
Guolin Ke committed
11
12

#define TYPE_BITS 5
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// use .Internal(internalsID()) to uuid
#define R_INTERNALS_UUID "2fdf6c18-697a-4ba7-b8ef-11c0d92f1327"


#ifdef R_VER_ABOVE_35
#define NAMED_BITS 16
struct lgbm_sxpinfo {
  unsigned int type : 5;
  unsigned int scalar : 1;
  unsigned int obj : 1;
  unsigned int alt : 1;
  unsigned int gp : 16;
  unsigned int mark : 1;
  unsigned int debug : 1;
  unsigned int trace : 1;
  unsigned int spare : 1;
  unsigned int gcgen : 1;
  unsigned int gccls : 3;
  unsigned int named : NAMED_BITS;
  unsigned int extra : 32 - NAMED_BITS;
};

// 64bit pointer
#if INTPTR_MAX == INT64_MAX
Guolin Ke's avatar
Guolin Ke committed
37
typedef int64_t R_xlen_t;
38
39
40
41
42
#else
typedef int R_xlen_t;
#endif

#else
Guolin Ke's avatar
Guolin Ke committed
43
struct lgbm_sxpinfo {
Guolin Ke's avatar
Guolin Ke committed
44
45
46
47
48
49
50
51
52
53
54
55
  unsigned int type : 5;
  unsigned int obj : 1;
  unsigned int named : 2;
  unsigned int gp : 16;
  unsigned int mark : 1;
  unsigned int debug : 1;
  unsigned int trace : 1;
  unsigned int spare : 1;
  unsigned int gcgen : 1;
  unsigned int gccls : 3;
};

56
57
58
typedef int R_xlen_t;
#endif

Guolin Ke's avatar
Guolin Ke committed
59
struct lgbm_primsxp {
Guolin Ke's avatar
Guolin Ke committed
60
61
62
  int offset;
};

Guolin Ke's avatar
Guolin Ke committed
63
64
65
66
struct lgbm_symsxp {
  struct LGBM_SER *pname;
  struct LGBM_SER *value;
  struct LGBM_SER *internal;
Guolin Ke's avatar
Guolin Ke committed
67
68
};

Guolin Ke's avatar
Guolin Ke committed
69
70
71
72
struct lgbm_listsxp {
  struct LGBM_SER *carval;
  struct LGBM_SER *cdrval;
  struct LGBM_SER *tagval;
Guolin Ke's avatar
Guolin Ke committed
73
74
};

Guolin Ke's avatar
Guolin Ke committed
75
76
77
78
struct lgbm_envsxp {
  struct LGBM_SER *frame;
  struct LGBM_SER *enclos;
  struct LGBM_SER *hashtab;
Guolin Ke's avatar
Guolin Ke committed
79
80
};

Guolin Ke's avatar
Guolin Ke committed
81
82
83
84
struct lgbm_closxp {
  struct LGBM_SER *formals;
  struct LGBM_SER *body;
  struct LGBM_SER *env;
Guolin Ke's avatar
Guolin Ke committed
85
86
};

Guolin Ke's avatar
Guolin Ke committed
87
88
89
90
struct lgbm_promsxp {
  struct LGBM_SER *value;
  struct LGBM_SER *expr;
  struct LGBM_SER *env;
Guolin Ke's avatar
Guolin Ke committed
91
92
};

Guolin Ke's avatar
Guolin Ke committed
93
94
95
96
typedef struct LGBM_SER {
  struct lgbm_sxpinfo sxpinfo;
  struct LGBM_SER* attrib;
  struct LGBM_SER* gengc_next_node, *gengc_prev_node;
Guolin Ke's avatar
Guolin Ke committed
97
  union {
Guolin Ke's avatar
Guolin Ke committed
98
99
100
101
102
103
    struct lgbm_primsxp primsxp;
    struct lgbm_symsxp symsxp;
    struct lgbm_listsxp listsxp;
    struct lgbm_envsxp envsxp;
    struct lgbm_closxp closxp;
    struct lgbm_promsxp promsxp;
Guolin Ke's avatar
Guolin Ke committed
104
  } u;
Guolin Ke's avatar
Guolin Ke committed
105
} LGBM_SER, *LGBM_SE;
Guolin Ke's avatar
Guolin Ke committed
106

Guolin Ke's avatar
Guolin Ke committed
107
struct lgbm_vecsxp {
108
109
  R_xlen_t length;
  R_xlen_t truelength;
Guolin Ke's avatar
Guolin Ke committed
110
111
};

Guolin Ke's avatar
Guolin Ke committed
112
113
114
115
116
117
typedef struct VECTOR_SER {
  struct lgbm_sxpinfo sxpinfo;
  struct LGBM_SER* attrib;
  struct LGBM_SER* gengc_next_node, *gengc_prev_node;
  struct lgbm_vecsxp vecsxp;
} VECTOR_SER, *VECSE;
Guolin Ke's avatar
Guolin Ke committed
118

Guolin Ke's avatar
Guolin Ke committed
119
typedef union { VECTOR_SER s; double align; } SEXPREC_ALIGN;
Guolin Ke's avatar
Guolin Ke committed
120
121
122
123
124
125
126

#define DATAPTR(x)  (((SEXPREC_ALIGN *) (x)) + 1)

#define R_CHAR_PTR(x)     ((char *) DATAPTR(x))

#define R_INT_PTR(x)  ((int *) DATAPTR(x))

Guolin Ke's avatar
Guolin Ke committed
127
128
#define R_INT64_PTR(x)  ((int64_t *) DATAPTR(x))

Guolin Ke's avatar
Guolin Ke committed
129
130
131
132
#define R_REAL_PTR(x)     ((double *) DATAPTR(x))

#define R_AS_INT(x) (*((int *) DATAPTR(x)))

Guolin Ke's avatar
Guolin Ke committed
133
134
#define R_AS_INT64(x) (*((int64_t *) DATAPTR(x)))

Guolin Ke's avatar
Guolin Ke committed
135
#define R_IS_NULL(x) ((*(LGBM_SE)(x)).sxpinfo.type == 0)
Guolin Ke's avatar
Guolin Ke committed
136
137
138
139
140
141

// 64bit pointer
#if INTPTR_MAX == INT64_MAX

#define R_ADDR(x)  ((int64_t *) DATAPTR(x))

Guolin Ke's avatar
Guolin Ke committed
142
inline void R_SET_PTR(LGBM_SE x, void* ptr) {
Guolin Ke's avatar
Guolin Ke committed
143
144
145
146
147
148
149
  if (ptr == nullptr) {
    R_ADDR(x)[0] = (int64_t)(NULL);
  } else {
    R_ADDR(x)[0] = (int64_t)(ptr);
  }
}

Guolin Ke's avatar
Guolin Ke committed
150
inline void* R_GET_PTR(LGBM_SE x) {
Guolin Ke's avatar
Guolin Ke committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  if (R_IS_NULL(x)) {
    return nullptr;
  } else {
    auto ret = (void *)(R_ADDR(x)[0]);
    if (ret == NULL) {
      ret = nullptr;
    }
    return ret;
  }
}

#else

#define R_ADDR(x)  ((int32_t *) DATAPTR(x))

Guolin Ke's avatar
Guolin Ke committed
166
inline void R_SET_PTR(LGBM_SE x, void* ptr) {
Guolin Ke's avatar
Guolin Ke committed
167
168
169
170
171
172
173
  if (ptr == nullptr) {
    R_ADDR(x)[0] = (int32_t)(NULL);
  } else {
    R_ADDR(x)[0] = (int32_t)(ptr);
  }
}

Guolin Ke's avatar
Guolin Ke committed
174
inline void* R_GET_PTR(LGBM_SE x) {
Guolin Ke's avatar
Guolin Ke committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  if (R_IS_NULL(x)) {
    return nullptr;
  } else {
    auto ret = (void *)(R_ADDR(x)[0]);
    if (ret == NULL) {
      ret = nullptr;
    }
    return ret;
  }
}

#endif

#endif // R_OBJECT_HELPER_H_