kcar RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: kcar-public@bogomips.org
Subject: [PATCH 5/5] TypedData C-API conversion
Date: Fri,  3 Mar 2017 20:53:07 +0000	[thread overview]
Message-ID: <20170303205307.2275-6-e@80x24.org> (raw)
In-Reply-To: <20170303205307.2275-1-e@80x24.org>

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/kcar/kcar.rl    | 36 ++++++++++++++++++++++++------------
 test/test_parser.rb |  8 ++++++++
 2 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/ext/kcar/kcar.rl b/ext/kcar/kcar.rl
index 1130b75..4c66fb5 100644
--- a/ext/kcar/kcar.rl
+++ b/ext/kcar/kcar.rl
@@ -409,29 +409,41 @@ post_exec: /* "_out:" also goes here */
   assert(hp->offset <= len && "offset longer than length");
 }
 
-static struct http_parser *data_get(VALUE self)
+static void kcar_mark(void *ptr)
 {
-  struct http_parser *hp;
+  struct http_parser *hp = ptr;
 
-  Data_Get_Struct(self, struct http_parser, hp);
-  assert(hp && "failed to extract http_parser struct");
-  return hp;
+  rb_gc_mark(hp->cont);
+  rb_gc_mark(hp->status);
 }
 
-static void mark(void *ptr)
+static size_t kcar_memsize(const void *ptr)
 {
-  struct http_parser *hp = ptr;
+  return sizeof(struct http_parser);
+}
 
-  rb_gc_mark(hp->cont);
-  rb_gc_mark(hp->status);
+static const rb_data_type_t kcar_type = {
+  "kcar_parser",
+  { kcar_mark, RUBY_TYPED_DEFAULT_FREE, kcar_memsize, /* reserved */ },
+  /* parent, data, [ flags ] */
+};
+
+static VALUE kcar_alloc(VALUE klass)
+{
+  struct http_parser *hp;
+  return TypedData_Make_Struct(klass, struct http_parser, &kcar_type, hp);
 }
 
-static VALUE alloc(VALUE klass)
+static struct http_parser *data_get(VALUE self)
 {
   struct http_parser *hp;
-  return Data_Make_Struct(klass, struct http_parser, mark, -1, hp);
+
+  TypedData_Get_Struct(self, struct http_parser, &kcar_type, hp);
+  assert(hp && "failed to extract http_parser struct");
+  return hp;
 }
 
+
 /**
  * call-seq:
  *    Kcar::Parser.new => parser
@@ -678,7 +690,7 @@ void Init_kcar_ext(void)
 
   eParserError = rb_define_class_under(mKcar, "ParserError", rb_eIOError);
 
-  rb_define_alloc_func(cParser, alloc);
+  rb_define_alloc_func(cParser, kcar_alloc);
   rb_define_method(cParser, "initialize", initialize, 0);
   rb_define_method(cParser, "reset", initialize, 0);
   rb_define_method(cParser, "headers", headers, 2);
diff --git a/test/test_parser.rb b/test/test_parser.rb
index dab2e77..c8eaa4d 100644
--- a/test/test_parser.rb
+++ b/test/test_parser.rb
@@ -289,4 +289,12 @@ class TestParser < Test::Unit::TestCase
     assert @hp.headers(env = {}, resp)
     assert_equal '', env['Host']
   end
+
+  def test_memsize
+    require 'objspace'
+    n = ObjectSpace.memsize_of(@hp)
+    assert_kind_of Integer, n
+  rescue LoadError
+    warn 'ObjectSpace not available'
+  end
 end
-- 
EW


      parent reply	other threads:[~2017-03-03 20:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-03 20:53 [PATCH 0/5] miscellaneous updates Eric Wong
2017-03-03 20:53 ` [PATCH 1/5] gemspec: use SPDX compatible terms for the license(s) Eric Wong
2017-03-03 20:53 ` [PATCH 2/5] archive/slrnpull.conf: add a note explaining the purpose Eric Wong
2017-03-03 20:53 ` [PATCH 3/5] drop rb_str_set_len compatibility replacement Eric Wong
2017-03-03 20:53 ` [PATCH 4/5] remove rb_str_modify workaround Eric Wong
2017-03-03 20:53 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/kcar/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170303205307.2275-6-e@80x24.org \
    --to=e@80x24.org \
    --cc=kcar-public@bogomips.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/kcar.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).