Skip to content

Commit 33766ef

Browse files
committed
Use TypedData
1 parent 5baa708 commit 33766ef

File tree

4 files changed

+63
-34
lines changed

4 files changed

+63
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## (unreleased)
22

33
* Use freetds v1.5.1 and OpenSSL v3.5.0 for Windows and Linux builds.
4+
* Use `TypedData` in C-Land.
45

56
## 3.2.1
67

ext/tiny_tds/client.c

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,54 @@ static ID intern_source_eql, intern_severity_eql, intern_db_error_number_eql, in
88
static ID intern_new, intern_dup, intern_transpose_iconv_encoding, intern_local_offset, intern_gsub, intern_call;
99
VALUE opt_escape_regex, opt_escape_dblquote;
1010

11+
static void rb_tinytds_client_mark(void *ptr)
12+
{
13+
tinytds_client_wrapper *cwrap = (tinytds_client_wrapper *)ptr;
14+
15+
if (cwrap) {
16+
rb_gc_mark(cwrap->charset);
17+
}
18+
}
19+
20+
static void rb_tinytds_client_free(void *ptr)
21+
{
22+
tinytds_client_wrapper *cwrap = (tinytds_client_wrapper *)ptr;
23+
24+
if (cwrap->login) {
25+
dbloginfree(cwrap->login);
26+
}
27+
28+
if (cwrap->client && !cwrap->closed) {
29+
dbclose(cwrap->client);
30+
cwrap->client = NULL;
31+
cwrap->closed = 1;
32+
cwrap->userdata->closed = 1;
33+
}
34+
35+
xfree(cwrap->userdata);
36+
xfree(ptr);
37+
}
38+
39+
static size_t tinytds_client_wrapper_size(const void* data)
40+
{
41+
return sizeof(tinytds_client_wrapper);
42+
}
43+
44+
static const rb_data_type_t tinytds_client_wrapper_type = {
45+
.wrap_struct_name = "tinytds_client_wrapper",
46+
.function = {
47+
.dmark = rb_tinytds_client_mark,
48+
.dfree = rb_tinytds_client_free,
49+
.dsize = tinytds_client_wrapper_size,
50+
},
51+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
52+
};
1153

1254
// Lib Macros
1355

1456
#define GET_CLIENT_WRAPPER(self) \
1557
tinytds_client_wrapper *cwrap; \
16-
Data_Get_Struct(self, tinytds_client_wrapper, cwrap)
58+
TypedData_Get_Struct(self, tinytds_client_wrapper, &tinytds_client_wrapper_type, cwrap)
1759

1860
#define REQUIRE_OPEN_CLIENT(cwrap) \
1961
if (cwrap->closed || cwrap->userdata->closed) { \
@@ -244,39 +286,11 @@ static void rb_tinytds_client_reset_userdata(tinytds_client_userdata *userdata)
244286
userdata->nonblocking_errors_size = 0;
245287
}
246288

247-
static void rb_tinytds_client_mark(void *ptr)
248-
{
249-
tinytds_client_wrapper *cwrap = (tinytds_client_wrapper *)ptr;
250-
251-
if (cwrap) {
252-
rb_gc_mark(cwrap->charset);
253-
}
254-
}
255-
256-
static void rb_tinytds_client_free(void *ptr)
257-
{
258-
tinytds_client_wrapper *cwrap = (tinytds_client_wrapper *)ptr;
259-
260-
if (cwrap->login) {
261-
dbloginfree(cwrap->login);
262-
}
263-
264-
if (cwrap->client && !cwrap->closed) {
265-
dbclose(cwrap->client);
266-
cwrap->client = NULL;
267-
cwrap->closed = 1;
268-
cwrap->userdata->closed = 1;
269-
}
270-
271-
xfree(cwrap->userdata);
272-
xfree(ptr);
273-
}
274-
275289
static VALUE allocate(VALUE klass)
276290
{
277291
VALUE obj;
278292
tinytds_client_wrapper *cwrap;
279-
obj = Data_Make_Struct(klass, tinytds_client_wrapper, rb_tinytds_client_mark, rb_tinytds_client_free, cwrap);
293+
obj = TypedData_Make_Struct(klass, tinytds_client_wrapper, &tinytds_client_wrapper_type, cwrap);
280294
cwrap->closed = 1;
281295
cwrap->charset = Qnil;
282296
cwrap->userdata = malloc(sizeof(tinytds_client_userdata));

ext/tiny_tds/result.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ rb_encoding *binaryEncoding;
3535

3636

3737
// Lib Backend (Memory Management)
38-
3938
static void rb_tinytds_result_mark(void *ptr)
4039
{
4140
tinytds_result_wrapper *rwrap = (tinytds_result_wrapper *)ptr;
@@ -54,11 +53,26 @@ static void rb_tinytds_result_free(void *ptr)
5453
xfree(ptr);
5554
}
5655

56+
static size_t tinytds_result_wrapper_size(const void* data)
57+
{
58+
return sizeof(tinytds_result_wrapper);
59+
}
60+
61+
const rb_data_type_t tinytds_result_wrapper_type = {
62+
.wrap_struct_name = "tinytds_result_wrapper",
63+
.function = {
64+
.dmark = rb_tinytds_result_mark,
65+
.dfree = rb_tinytds_result_free,
66+
.dsize = tinytds_result_wrapper_size,
67+
},
68+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
69+
};
70+
5771
VALUE rb_tinytds_new_result_obj(tinytds_client_wrapper *cwrap)
5872
{
5973
VALUE obj;
6074
tinytds_result_wrapper *rwrap;
61-
obj = Data_Make_Struct(cTinyTdsResult, tinytds_result_wrapper, rb_tinytds_result_mark, rb_tinytds_result_free, rwrap);
75+
obj = TypedData_Make_Struct(cTinyTdsResult, tinytds_result_wrapper, &tinytds_result_wrapper_type, rwrap);
6276
rwrap->cwrap = cwrap;
6377
rwrap->client = cwrap->client;
6478
rwrap->local_offset = Qnil;

ext/tiny_tds/result.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ typedef struct {
1919
unsigned long number_of_rows;
2020
} tinytds_result_wrapper;
2121

22+
extern const rb_data_type_t tinytds_result_wrapper_type;
2223

2324
// Lib Macros
24-
2525
#define GET_RESULT_WRAPPER(self) \
2626
tinytds_result_wrapper *rwrap; \
27-
Data_Get_Struct(self, tinytds_result_wrapper, rwrap)
27+
TypedData_Get_Struct(self, tinytds_result_wrapper, &tinytds_result_wrapper_type, rwrap)
2828

2929

3030

0 commit comments

Comments
 (0)