From 93e936d9a1586d25c338c6974027d6aa70960e1b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 14 Mar 2017 20:01:18 +0000 Subject: TypedData C-API conversion This provides some extra type safety if combined with other C extensions, as well as allowing us to account for memory usage of the HTTP parser in ObjectSpace. This requires Ruby 1.9.3+ and has remained a stable API since then. This will become officially supported when Ruby 2.3.0 is released later this month. This API has only been documented in doc/extension.rdoc (formerly README.EXT) in the Ruby source tree since April 2015, r50318 --- ext/raindrops/linux_tcp_info.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'ext/raindrops/linux_tcp_info.c') diff --git a/ext/raindrops/linux_tcp_info.c b/ext/raindrops/linux_tcp_info.c index 4693e47..83001a5 100644 --- a/ext/raindrops/linux_tcp_info.c +++ b/ext/raindrops/linux_tcp_info.c @@ -46,12 +46,22 @@ TCPI_ATTR_READER(rcv_rtt) TCPI_ATTR_READER(rcv_space) TCPI_ATTR_READER(total_retrans) +static size_t tcpi_memsize(const void *ptr) +{ + return sizeof(struct tcp_info); +} + +static const rb_data_type_t tcpi_type = { + "tcp_info", + { NULL, RUBY_TYPED_DEFAULT_FREE, tcpi_memsize, /* reserved */ }, + /* parent, data, [ flags ] */ +}; + static VALUE alloc(VALUE klass) { - struct tcp_info *info = xmalloc(sizeof(struct tcp_info)); + struct tcp_info *info; - /* Data_Make_Struct has an extra memset 0 which is so wasteful */ - return Data_Wrap_Struct(klass, NULL, -1, info); + return TypedData_Make_Struct(klass, struct tcp_info, &tcpi_type, info); } /* -- cgit v1.2.3-24-ge0c7