Skip to content

Instantly share code, notes, and snippets.

@makotoshimazu
Created January 7, 2014 06:15
Show Gist options
  • Save makotoshimazu/8295325 to your computer and use it in GitHub Desktop.
Save makotoshimazu/8295325 to your computer and use it in GitHub Desktop.
arenaが思ったとおりに動作しているか確認するためのコード。 2013/12/27時点でcloneしたmrubyでは正常動作しなかったが、#1637のプルリクで1/3に直っている。 この例だとarenaに保護されているようだが、いつ放置されるのかよくわかっていない。
//! make arenaoverflow.bin
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <mruby.h>
#include <mruby/compile.h>
#include <mruby/value.h>
mrb_value create( mrb_state *mrb, mrb_value factory )
{
return mrb_funcall( mrb, factory, "create", 0 );
}
void mrb_state_print( mrb_state *mrb )
{
printf( "----------------\n"
"mrb_state stats\n"
"\n"
"%20s = %10d\n"
"%20s = %10d\n",
"mrb->arena_idx", mrb->arena_idx,
"mrb->arena_capa", mrb->arena_capa
);
}
int main( void )
{
mrb_state *mrb = mrb_open();
/* load class */
FILE *fp = fopen( "factory.rb", "r" );
if ( fp == NULL ) {
perror( "fopen" );
exit( 1 );
}
mrb_load_file( mrb, fp );
/* factory: FactoryTest class */
mrb_value factory =
mrb_obj_value( mrb_class_get( mrb, "FactoryTest" ) );
/* (a) people is alloced on heap region */
mrb_value *people = mrb_malloc( mrb, sizeof(mrb_value)*1E6 );
/* /\* (b) people is alloced on stack *\/ */
/* mrb_value people[1000000]; */
mrb_state_print( mrb );
printf( "---------------\n" );
printf( "person create\n" );
for ( int i = 0; i < 1E6; i++ ) {
/* (1) method call on another func */
people[i] = create( mrb, factory );
/* /\* (2) method call on toplevel *\/ */
/* people[i] = mrb_funcall( mrb, factory, "create", 0 ); */
}
mrb_state_print( mrb );
printf( "---------------\n" );
printf( "person no\n" );
/* some errors will occur if gc collect the object */
for ( int i = 0; i < 1E6; i++ ) {
mrb_funcall( mrb, people[i], "no", 0 );
}
mrb_state_print( mrb );
mrb_close( mrb );
return 0;
}
class Person
@@total = 0
def initialize
# p "person.new no#{@@total}"
@n = @@total
@@total += 1
end
def no
# p "#{@n}"
end
end
class FactoryTest
def initialize
p "Factory Test"
end
def self.create
Person.new
end
end
MRUBYPATH := /home/amiq/.opt/src/mruby
CFLAGS += -I$(MRUBYPATH)/include
LDFLAGS += -L$(MRUBYPATH)/build/host/lib -lmruby -lm
CFLAGS += -std=gnu99 -O0 -g
all: arenaoverflow.bin
%.bin: %.c
$(CC) -o $@ $< $(CFLAGS) $(LDFLAGS)
.PHONY: clean
clean:
rm -f *.bin *.o
.PHONY: check-syntax
check-syntax:
$(CC) $(CHK_SOURCES) $(CFLAGS) $(LDFLAGS) -fsyntax-only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment