Skip to content

Commit

Permalink
compile C-level blocks in the autozone heap and emit a write barrier …
Browse files Browse the repository at this point in the history
…to the original ruby Proc object, to avoid premature garbage collection of the Proc when calling the C-level block

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@4971 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information
lrz committed Dec 3, 2010
1 parent 6ffb628 commit cc5676f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
9 changes: 5 additions & 4 deletions compiler.cpp
Expand Up @@ -5522,15 +5522,16 @@ RoxorCompiler::compile_conversion_to_c(const char *type, Value *val,
return funcptr;
}

// A C-level block. We allocate on the stack the literal
// A C-level block. We allocate on the auto heap the literal
// structure following the ABI, initialize it then pass
// a pointer to it.
Value *block_lit = new AllocaInst(BlockLiteralTy, "", bb);
Value *block_lit = compile_xmalloc(GET_CORE()->get_sizeof(BlockLiteralTy));
Value *args[] = {
block_lit,
funcptr
funcptr,
val
};
CallInst::Create(initBlockFunc, args, args + 2, "", bb);
CallInst::Create(initBlockFunc, args, args + 3, "", bb);
return new BitCastInst(block_lit, PtrTy, "", bb);
}
break;
Expand Down
15 changes: 9 additions & 6 deletions kernel.c
Expand Up @@ -1055,22 +1055,25 @@ struct ruby_block_literal {
int reserved;
void *imp;
struct ruby_block_descriptor *descriptor;
VALUE ruby_proc;
};

static struct ruby_block_descriptor ruby_block_descriptor_value = {
0, sizeof(struct ruby_block_literal)
};

#if MAC_OS_X_VERSION_MAX_ALLOWED < 1070
extern void *_NSConcreteStackBlock[32];
#endif
extern void *_NSConcreteAutoBlock[32];

#define __MR_BLOCK_IS_GC (1 << 27)
#define __MR_BLOCK_HAS_DESCRIPTOR (1 << 29)

PRIMITIVE void
vm_init_c_block(struct ruby_block_literal *b, void *imp)
vm_init_c_block(struct ruby_block_literal *b, void *imp, VALUE proc)
{
b->isa = &_NSConcreteStackBlock;
b->flags = (1 << 29);
b->isa = &_NSConcreteAutoBlock;
b->flags = __MR_BLOCK_IS_GC | __MR_BLOCK_HAS_DESCRIPTOR;
b->reserved = 0;
b->imp = imp;
b->descriptor = &ruby_block_descriptor_value;
GC_WB(&b->ruby_proc, proc);
}

0 comments on commit cc5676f

Please sign in to comment.