Skip to content

Commit 99d442e

Browse files
authored
Merge pull request #1079 from rhenium/ky/dcompact
Implement dcompact
2 parents 5fe1b71 + 98acd95 commit 99d442e

2 files changed

Lines changed: 78 additions & 26 deletions

File tree

ext/openssl/ossl_ssl.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static void
5656
ossl_sslctx_mark(void *ptr)
5757
{
5858
SSL_CTX *ctx = ptr;
59-
rb_gc_mark((VALUE)SSL_CTX_get_ex_data(ctx, ossl_sslctx_ex_ptr_idx));
59+
rb_gc_mark_movable((VALUE)SSL_CTX_get_ex_data(ctx, ossl_sslctx_ex_ptr_idx));
6060
}
6161

6262
static void
@@ -65,12 +65,25 @@ ossl_sslctx_free(void *ptr)
6565
SSL_CTX_free(ptr);
6666
}
6767

68+
static void
69+
ossl_sslctx_compact(void *ptr)
70+
{
71+
SSL_CTX *ctx = ptr;
72+
VALUE self = (VALUE)SSL_CTX_get_ex_data(ctx, ossl_sslctx_ex_ptr_idx);
73+
if (self) {
74+
(void)SSL_CTX_set_ex_data(ctx, ossl_sslctx_ex_ptr_idx,
75+
(void *)rb_gc_location(self));
76+
}
77+
}
78+
6879
static const rb_data_type_t ossl_sslctx_type = {
69-
"OpenSSL/SSL/CTX",
70-
{
71-
ossl_sslctx_mark, ossl_sslctx_free,
80+
.wrap_struct_name = "OpenSSL/SSL/CTX",
81+
.function = {
82+
.dmark = ossl_sslctx_mark,
83+
.dfree = ossl_sslctx_free,
84+
.dcompact = ossl_sslctx_compact,
7285
},
73-
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
86+
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
7487
};
7588

7689
static VALUE
@@ -1567,7 +1580,7 @@ static void
15671580
ossl_ssl_mark(void *ptr)
15681581
{
15691582
SSL *ssl = ptr;
1570-
rb_gc_mark((VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx));
1583+
rb_gc_mark_movable((VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx));
15711584
}
15721585

15731586
static void
@@ -1576,12 +1589,25 @@ ossl_ssl_free(void *ssl)
15761589
SSL_free(ssl);
15771590
}
15781591

1592+
static void
1593+
ossl_ssl_compact(void *ptr)
1594+
{
1595+
SSL *ssl = ptr;
1596+
VALUE self = (VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx);
1597+
if (self) {
1598+
(void)SSL_set_ex_data(ssl, ossl_ssl_ex_ptr_idx,
1599+
(void *)rb_gc_location(self));
1600+
}
1601+
}
1602+
15791603
const rb_data_type_t ossl_ssl_type = {
1580-
"OpenSSL/SSL",
1581-
{
1582-
ossl_ssl_mark, ossl_ssl_free,
1604+
.wrap_struct_name = "OpenSSL/SSL",
1605+
.function = {
1606+
.dmark = ossl_ssl_mark,
1607+
.dfree = ossl_ssl_free,
1608+
.dcompact = ossl_ssl_compact,
15831609
},
1584-
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
1610+
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
15851611
};
15861612

15871613
static VALUE

ext/openssl/ossl_x509store.c

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,9 @@ static void
116116
ossl_x509store_mark(void *ptr)
117117
{
118118
X509_STORE *store = ptr;
119-
// Note: this reference is stored as @verify_callback so we don't need to mark it.
120-
// However we do need to ensure GC compaction won't move it, hence why
121-
// we call rb_gc_mark here.
122-
rb_gc_mark((VALUE)X509_STORE_get_ex_data(store, store_ex_verify_cb_idx));
119+
VALUE verify_cb =
120+
(VALUE)X509_STORE_get_ex_data(store, store_ex_verify_cb_idx);
121+
rb_gc_mark_movable(verify_cb);
123122
}
124123

125124
static void
@@ -128,12 +127,26 @@ ossl_x509store_free(void *ptr)
128127
X509_STORE_free(ptr);
129128
}
130129

130+
static void
131+
ossl_x509store_compact(void *ptr)
132+
{
133+
X509_STORE *store = ptr;
134+
VALUE verify_cb =
135+
(VALUE)X509_STORE_get_ex_data(store, store_ex_verify_cb_idx);
136+
if (verify_cb) {
137+
(void)X509_STORE_set_ex_data(store, store_ex_verify_cb_idx,
138+
(void *)rb_gc_location(verify_cb));
139+
}
140+
}
141+
131142
static const rb_data_type_t ossl_x509store_type = {
132-
"OpenSSL/X509/STORE",
133-
{
134-
ossl_x509store_mark, ossl_x509store_free,
143+
.wrap_struct_name = "OpenSSL/X509/STORE",
144+
.function = {
145+
.dmark = ossl_x509store_mark,
146+
.dfree = ossl_x509store_free,
147+
.dcompact = ossl_x509store_compact,
135148
},
136-
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
149+
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
137150
};
138151

139152
/*
@@ -570,10 +583,9 @@ static void
570583
ossl_x509stctx_mark(void *ptr)
571584
{
572585
X509_STORE_CTX *ctx = ptr;
573-
// Note: this reference is stored as @verify_callback so we don't need to mark it.
574-
// However we do need to ensure GC compaction won't move it, hence why
575-
// we call rb_gc_mark here.
576-
rb_gc_mark((VALUE)X509_STORE_CTX_get_ex_data(ctx, stctx_ex_verify_cb_idx));
586+
VALUE verify_cb =
587+
(VALUE)X509_STORE_CTX_get_ex_data(ctx, stctx_ex_verify_cb_idx);
588+
rb_gc_mark_movable(verify_cb);
577589
}
578590

579591
static void
@@ -585,12 +597,26 @@ ossl_x509stctx_free(void *ptr)
585597
X509_STORE_CTX_free(ctx);
586598
}
587599

600+
static void
601+
ossl_x509stctx_compact(void *ptr)
602+
{
603+
X509_STORE_CTX *ctx = ptr;
604+
VALUE verify_cb =
605+
(VALUE)X509_STORE_CTX_get_ex_data(ctx, stctx_ex_verify_cb_idx);
606+
if (verify_cb) {
607+
(void)X509_STORE_CTX_set_ex_data(ctx, stctx_ex_verify_cb_idx,
608+
(void *)rb_gc_location(verify_cb));
609+
}
610+
}
611+
588612
static const rb_data_type_t ossl_x509stctx_type = {
589-
"OpenSSL/X509/STORE_CTX",
590-
{
591-
ossl_x509stctx_mark, ossl_x509stctx_free,
613+
.wrap_struct_name = "OpenSSL/X509/STORE_CTX",
614+
.function = {
615+
.dmark = ossl_x509stctx_mark,
616+
.dfree = ossl_x509stctx_free,
617+
.dcompact = ossl_x509stctx_compact,
592618
},
593-
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
619+
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
594620
};
595621

596622
static VALUE

0 commit comments

Comments
 (0)