21
21
#include < iostream>
22
22
#include < cstdint>
23
23
#include < cassert>
24
+ #include < algorithm>
25
+ #include < list>
24
26
#include < fcntl.h>
25
27
#include < unistd.h>
26
28
@@ -89,10 +91,22 @@ class stdout_redirect {
89
91
bool redirecting;
90
92
};
91
93
92
- const char *out_data_copy (std::string msg)
94
+ struct cstore_obj {
95
+ Cstore *cstore;
96
+ std::list<char *> output;
97
+ };
98
+
99
+ static struct cstore_obj * create_handle () {
100
+ struct cstore_obj *h = new cstore_obj;
101
+ h->cstore = Cstore::createCstore (false );
102
+ return h;
103
+ };
104
+
105
+ const char *out_data_copy (std::string msg, cstore_obj *o)
93
106
{
94
107
size_t len = msg.length ();
95
108
char *out_data = (char *) malloc (len + 1 );
109
+ o->output .push_back (out_data);
96
110
msg.copy (out_data, len);
97
111
out_data[len] = ' \0 ' ;
98
112
return out_data;
@@ -109,36 +123,41 @@ static void *voidptr_of_uint(uint64_t v)
109
123
return (void *)(uintptr_t )(v & ~1 );
110
124
}
111
125
112
- static Cstore * cstore_of_handle (uint64_t handle)
126
+ static cstore_obj * cstore_obj_of_handle (uint64_t handle)
113
127
{
114
- return (Cstore *) voidptr_of_uint (handle);
128
+ return (cstore_obj *) voidptr_of_uint (handle);
115
129
}
116
130
117
131
uint64_t
118
132
vy_cstore_init (void )
119
133
{
120
- Cstore *handle = Cstore::createCstore ( false );
134
+ cstore_obj *handle = create_handle ( );
121
135
return uint_of_voidptr (handle);
122
136
}
123
137
124
138
void
125
139
vy_cstore_free (uint64_t handle)
126
140
{
127
- Cstore *h = cstore_of_handle (handle);
141
+ cstore_obj *h = cstore_obj_of_handle (handle);
142
+ for (char * x: h->output ) {
143
+ free (x);
144
+ }
145
+ delete h->cstore ;
128
146
delete h;
129
147
}
130
148
131
149
int
132
150
vy_in_session (uint64_t handle)
133
151
{
134
- Cstore *h = cstore_of_handle (handle);
152
+ Cstore *h = cstore_obj_of_handle (handle)-> cstore ;
135
153
return h->inSession () ? 1 : 0 ;
136
154
}
137
155
138
156
const char *
139
157
vy_validate_path (uint64_t handle, const void ** path_ptr, size_t len)
140
158
{
141
- Cstore *cstore = cstore_of_handle (handle);
159
+ cstore_obj *obj = cstore_obj_of_handle (handle);
160
+ Cstore *cstore = obj->cstore ;
142
161
const char **path = (const char **) path_ptr;
143
162
Cpath path_comps = Cpath (path, len);
144
163
const char *out_data;
@@ -154,15 +173,16 @@ vy_validate_path(uint64_t handle, const void** path_ptr, size_t len)
154
173
out_str.append (redirect.get_redirected_output ());
155
174
}
156
175
157
- out_data = out_data_copy (out_str);
176
+ out_data = out_data_copy (out_str, obj );
158
177
out_stream = NULL ;
159
178
return out_data;
160
179
}
161
180
162
181
const char *
163
182
vy_set_path (uint64_t handle, const void ** path_ptr, size_t len)
164
183
{
165
- Cstore *cstore = cstore_of_handle (handle);
184
+ cstore_obj *obj = cstore_obj_of_handle (handle);
185
+ Cstore *cstore = obj->cstore ;
166
186
const char **path = (const char **) path_ptr;
167
187
Cpath path_comps = Cpath (path, len);
168
188
const char *out_data;
@@ -178,15 +198,16 @@ vy_set_path(uint64_t handle, const void** path_ptr, size_t len)
178
198
out_str.append (redirect.get_redirected_output ());
179
199
}
180
200
181
- out_data = out_data_copy (out_str);
201
+ out_data = out_data_copy (out_str, obj );
182
202
out_stream = NULL ;
183
203
return out_data;
184
204
}
185
205
186
206
const char *
187
207
vy_legacy_set_path (uint64_t handle, const void ** path_ptr, size_t len)
188
208
{
189
- Cstore *cstore = cstore_of_handle (handle);
209
+ cstore_obj *obj = cstore_obj_of_handle (handle);
210
+ Cstore *cstore = obj->cstore ;
190
211
const char **path = (const char **) path_ptr;
191
212
Cpath path_comps = Cpath (path, len);
192
213
const char *out_data;
@@ -210,15 +231,16 @@ vy_legacy_set_path(uint64_t handle, const void** path_ptr, size_t len)
210
231
}
211
232
212
233
out:
213
- out_data = out_data_copy (out_str);
234
+ out_data = out_data_copy (out_str, obj );
214
235
out_stream = NULL ;
215
236
return out_data;
216
237
}
217
238
218
239
const char *
219
240
vy_delete_path (uint64_t handle, const void ** path_ptr, size_t len)
220
241
{
221
- Cstore *cstore = cstore_of_handle (handle);
242
+ cstore_obj *obj = cstore_obj_of_handle (handle);
243
+ Cstore *cstore = obj->cstore ;
222
244
const char **path = (const char **) path_ptr;
223
245
Cpath path_comps = Cpath (path, len);
224
246
const char *out_data;
@@ -234,7 +256,7 @@ vy_delete_path(uint64_t handle, const void** path_ptr, size_t len)
234
256
out_str.append (redirect.get_redirected_output ());
235
257
}
236
258
237
- out_data = out_data_copy (out_str);
259
+ out_data = out_data_copy (out_str, obj );
238
260
out_stream = NULL ;
239
261
return out_data;
240
262
}
0 commit comments