Medial Code Documentation
Loading...
Searching...
No Matches
R_object_helper.h
1/*
2* A simple wrapper for accessing data in R object.
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.
5*/
6#ifndef R_OBJECT_HELPER_H_
7#define R_OBJECT_HELPER_H_
8
9#include <cstdint>
10#include <cstdio>
11
12#define TYPE_BITS 5
13// use .Internal(internalsID()) to uuid
14#define R_INTERNALS_UUID "2fdf6c18-697a-4ba7-b8ef-11c0d92f1327"
15
16
17#ifdef R_VER_ABOVE_35
18#define NAMED_BITS 16
19struct lgbm_sxpinfo {
20 unsigned int type : 5;
21 unsigned int scalar : 1;
22 unsigned int obj : 1;
23 unsigned int alt : 1;
24 unsigned int gp : 16;
25 unsigned int mark : 1;
26 unsigned int debug : 1;
27 unsigned int trace : 1;
28 unsigned int spare : 1;
29 unsigned int gcgen : 1;
30 unsigned int gccls : 3;
31 unsigned int named : NAMED_BITS;
32 unsigned int extra : 32 - NAMED_BITS;
33};
34
35// 64bit pointer
36#if INTPTR_MAX == INT64_MAX
37typedef int64_t R_xlen_t;
38#else
39typedef int R_xlen_t;
40#endif
41
42#else
44 unsigned int type : 5;
45 unsigned int obj : 1;
46 unsigned int named : 2;
47 unsigned int gp : 16;
48 unsigned int mark : 1;
49 unsigned int debug : 1;
50 unsigned int trace : 1;
51 unsigned int spare : 1;
52 unsigned int gcgen : 1;
53 unsigned int gccls : 3;
54};
55
56typedef int R_xlen_t;
57#endif
58
60 int offset;
61};
62
64 struct LGBM_SER *pname;
65 struct LGBM_SER *value;
66 struct LGBM_SER *internal;
67};
68
70 struct LGBM_SER *carval;
71 struct LGBM_SER *cdrval;
72 struct LGBM_SER *tagval;
73};
74
76 struct LGBM_SER *frame;
77 struct LGBM_SER *enclos;
78 struct LGBM_SER *hashtab;
79};
80
82 struct LGBM_SER *formals;
83 struct LGBM_SER *body;
84 struct LGBM_SER *env;
85};
86
88 struct LGBM_SER *value;
89 struct LGBM_SER *expr;
90 struct LGBM_SER *env;
91};
92
93typedef struct LGBM_SER {
94 struct lgbm_sxpinfo sxpinfo;
95 struct LGBM_SER* attrib;
96 struct LGBM_SER* gengc_next_node, *gengc_prev_node;
97 union {
98 struct lgbm_primsxp primsxp;
99 struct lgbm_symsxp symsxp;
100 struct lgbm_listsxp listsxp;
101 struct lgbm_envsxp envsxp;
102 struct lgbm_closxp closxp;
103 struct lgbm_promsxp promsxp;
104 } u;
105} LGBM_SER, *LGBM_SE;
106
108 R_xlen_t length;
109 R_xlen_t truelength;
110};
111
112typedef struct VECTOR_SER {
113 struct lgbm_sxpinfo sxpinfo;
114 struct LGBM_SER* attrib;
115 struct LGBM_SER* gengc_next_node, *gengc_prev_node;
116 struct lgbm_vecsxp vecsxp;
117} VECTOR_SER, *VECSE;
118
119typedef union { VECTOR_SER s; double align; } SEXPREC_ALIGN;
120
121#define DATAPTR(x) (((SEXPREC_ALIGN *) (x)) + 1)
122
123#define R_CHAR_PTR(x) ((char *) DATAPTR(x))
124
125#define R_INT_PTR(x) ((int *) DATAPTR(x))
126
127#define R_INT64_PTR(x) ((int64_t *) DATAPTR(x))
128
129#define R_REAL_PTR(x) ((double *) DATAPTR(x))
130
131#define R_AS_INT(x) (*((int *) DATAPTR(x)))
132
133#define R_AS_INT64(x) (*((int64_t *) DATAPTR(x)))
134
135#define R_IS_NULL(x) ((*(LGBM_SE)(x)).sxpinfo.type == 0)
136
137// 64bit pointer
138#if INTPTR_MAX == INT64_MAX
139
140#define R_ADDR(x) ((int64_t *) DATAPTR(x))
141
142inline void R_SET_PTR(LGBM_SE x, void* ptr) {
143 if (ptr == nullptr) {
144 R_ADDR(x)[0] = (int64_t)(NULL);
145 } else {
146 R_ADDR(x)[0] = (int64_t)(ptr);
147 }
148}
149
150inline void* R_GET_PTR(LGBM_SE x) {
151 if (R_IS_NULL(x)) {
152 return nullptr;
153 } else {
154 auto ret = (void *)(R_ADDR(x)[0]);
155 if (ret == NULL) {
156 ret = nullptr;
157 }
158 return ret;
159 }
160}
161
162#else
163
164#define R_ADDR(x) ((int32_t *) DATAPTR(x))
165
166inline void R_SET_PTR(LGBM_SE x, void* ptr) {
167 if (ptr == nullptr) {
168 R_ADDR(x)[0] = (int32_t)(NULL);
169 } else {
170 R_ADDR(x)[0] = (int32_t)(ptr);
171 }
172}
173
174inline void* R_GET_PTR(LGBM_SE x) {
175 if (R_IS_NULL(x)) {
176 return nullptr;
177 } else {
178 auto ret = (void *)(R_ADDR(x)[0]);
179 if (ret == NULL) {
180 ret = nullptr;
181 }
182 return ret;
183 }
184}
185
186#endif
187
188#endif // R_OBJECT_HELPER_H_
Definition R_object_helper.h:93
Definition R_object_helper.h:112
Definition R_object_helper.h:81
Definition R_object_helper.h:75
Definition R_object_helper.h:69
Definition R_object_helper.h:59
Definition R_object_helper.h:87
Definition R_object_helper.h:43
Definition R_object_helper.h:63
Definition R_object_helper.h:107
Definition R_object_helper.h:119