Created
February 5, 2020 16:43
-
-
Save larskanis/d95455fa71deb4cc6b03005cb9c505b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://gist.github.com/larskanis/c1ef84c3e058e819786f5fd20bc7c207