Skip to content

Instantly share code, notes, and snippets.

@larskanis
Created February 5, 2020 16:43
Show Gist options
  • Save larskanis/d95455fa71deb4cc6b03005cb9c505b0 to your computer and use it in GitHub Desktop.
Save larskanis/d95455fa71deb4cc6b03005cb9c505b0 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <ruby.h>
static VALUE alloc(VALUE self)
{
return rb_str_new_cstr("abcdef");
}
static struct st_table* string_table;
static VALUE reuse(VALUE self)
{
const char *str = "abcdef";
VALUE rb_str;
if( !st_lookup(string_table, (st_data_t) str, (st_data_t *) &rb_str) ){
rb_str = rb_str_new_cstr(str);
rb_gc_register_mark_object(rb_str);
st_insert(string_table, (st_data_t) str, rb_str);
}
return rb_str;
}
void Init_string_alloc_test(void)
{
string_table = st_init_strtable();
VALUE mod = rb_define_module("StringAllocTest");
rb_define_module_function(mod, "c_alloc", alloc, 0);
rb_define_module_function(mod, "c_reuse", reuse, 0);
}
@larskanis
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment