Skip to content

Commit

Permalink
Cleanup builtin finalizers
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Phoenix committed Apr 4, 2010
1 parent ab16470 commit e2136c4
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 40 deletions.
6 changes: 3 additions & 3 deletions vm/builtin/data.cpp
Expand Up @@ -29,9 +29,9 @@ namespace rubinius {
return data;
}

void Data::finalize(STATE) {
FreeFunctor f = this->free();
if(f) f(data());
void Data::finalize(STATE, Data* data) {
FreeFunctor f = data->free();
if(f) f(data->data());
}

void Data::Info::mark(Object* t, ObjectMark& mark) {
Expand Down
2 changes: 1 addition & 1 deletion vm/builtin/data.hpp
Expand Up @@ -35,7 +35,7 @@ namespace rubinius {
/** New Data instance. */
static Data* create(STATE, void* data, MarkFunctor mark, FreeFunctor free);

void finalize(STATE);
static void finalize(STATE, Data* data);

RDataExposed* exposed() {
return &exposed_;
Expand Down
10 changes: 5 additions & 5 deletions vm/builtin/dir.cpp
Expand Up @@ -24,7 +24,7 @@ namespace rubinius {
Dir* d = state->new_object<Dir>(G(dir));
d->os_ = 0;

state->om->needs_finalization(d);
state->om->needs_finalization(d, (FinalizerFunction)&Dir::finalize);

return d;
}
Expand All @@ -39,10 +39,10 @@ namespace rubinius {
return dir;
}

void Dir::finalize(STATE) {
if(os_) {
closedir(os_);
os_ = 0;
void Dir::finalize(STATE, Dir* dir) {
if(dir->os_) {
closedir(dir->os_);
dir->os_ = 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion vm/builtin/dir.hpp
Expand Up @@ -29,7 +29,7 @@ namespace rubinius {

static Dir* create(STATE);

void finalize(STATE);
static void finalize(STATE, Dir* dir);

// Ruby.primitive :dir_allocate
static Dir* allocate(STATE, Object* self);
Expand Down
10 changes: 5 additions & 5 deletions vm/builtin/fiber.cpp
Expand Up @@ -37,7 +37,7 @@ namespace rubinius {
fib->stack_ = state->stack_start();
fib->context_ = new ucontext_t;

state->om->needs_finalization(fib);
state->om->needs_finalization(fib, (FinalizerFunction)&Fiber::finalize);

state->current_fiber.set(fib);
}
Expand Down Expand Up @@ -98,7 +98,7 @@ namespace rubinius {
fib->stack_ = malloc(stack_size);
fib->context_ = new ucontext_t;

state->om->needs_finalization(fib);
state->om->needs_finalization(fib, (FinalizerFunction)&Fiber::finalize);

ucontext_t* ctx = fib->ucontext();

Expand Down Expand Up @@ -189,10 +189,10 @@ namespace rubinius {
#endif
}

void Fiber::finalize(STATE) {
void Fiber::finalize(STATE, Fiber* fib) {
#ifdef FIBER_ENABLED
delete context_;
if(stack_ && !root_) free(stack_);
delete fib->context_;
if(fib->stack_ && !fib->root_) free(fib->stack_);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion vm/builtin/fiber.hpp
Expand Up @@ -92,7 +92,7 @@ namespace rubinius {
// Ruby.primitive :fiber_s_yield
static Object* s_yield(STATE, Arguments& args, CallFrame* calling_environment);

void finalize(STATE);
static void finalize(STATE, Fiber* fib);

public: /* TypeInfo */

Expand Down
12 changes: 6 additions & 6 deletions vm/builtin/io.cpp
Expand Up @@ -43,7 +43,7 @@ namespace rubinius {

// Don't bother to add finalization for stdio
if(fd >= 3) {
state->om->needs_finalization(io);
state->om->needs_finalization(io, (FinalizerFunction)&IO::finalize);
}

return io;
Expand All @@ -60,7 +60,7 @@ namespace rubinius {
// Ensure the instance's class is set (i.e. for subclasses of IO)
io->klass(state, as<Class>(self));

state->om->needs_finalization(io);
state->om->needs_finalization(io, (FinalizerFunction)&IO::finalize);

return io;
}
Expand Down Expand Up @@ -385,15 +385,15 @@ namespace rubinius {
mode(state, Fixnum::from((m & ~O_ACCMODE) | O_WRONLY));
}

void IO::finalize(STATE) {
if(descriptor_->nil_p()) return;
void IO::finalize(STATE, IO* io) {
if(io->descriptor()->nil_p()) return;

native_int fd = descriptor_->to_native();
native_int fd = io->descriptor()->to_native();

// don't close stdin, stdout, stderr (0, 1, 2)
if(fd >= 3) {
::close(fd);
descriptor(state, Fixnum::from(-1));
io->descriptor(state, Fixnum::from(-1));
}
}

Expand Down
2 changes: 1 addition & 1 deletion vm/builtin/io.hpp
Expand Up @@ -43,7 +43,7 @@ namespace rubinius {
void unsafe_set_descriptor(native_int fd);
void force_read_only(STATE);
void force_write_only(STATE);
void finalize(STATE);
static void finalize(STATE, IO* io);

/* Class primitives */

Expand Down
9 changes: 5 additions & 4 deletions vm/builtin/memorypointer.cpp
Expand Up @@ -58,16 +58,17 @@ namespace rubinius {
autorelease = val->true_p() ? true : false;

if(autorelease && !set_finalizer) {
state->om->needs_finalization(this);
state->om->needs_finalization(this,
(FinalizerFunction)&MemoryPointer::finalize);
set_finalizer = true;
}

return val;
}

void MemoryPointer::finalize(STATE) {
if(autorelease && pointer) {
::free(pointer);
void MemoryPointer::finalize(STATE, MemoryPointer* ptr) {
if(ptr->autorelease && ptr->pointer) {
::free(ptr->pointer);
}
}

Expand Down
2 changes: 1 addition & 1 deletion vm/builtin/memorypointer.hpp
Expand Up @@ -17,7 +17,7 @@ namespace rubinius {

static MemoryPointer* create(STATE, void* ptr);

void finalize(STATE);
static void finalize(STATE, MemoryPointer* ptr);

// Ruby.primitive :memorypointer_address
Integer* get_address(STATE);
Expand Down
4 changes: 4 additions & 0 deletions vm/gc/finalize.hpp
@@ -1,5 +1,7 @@
namespace rubinius {

typedef void (*FinalizerFunction)(STATE, Object*);

struct FinalizeObject {
public:
enum FinalizationStatus {
Expand All @@ -11,11 +13,13 @@ namespace rubinius {
public:
FinalizeObject()
: queue_count(0)
, finalizer(0)
{}

Object* object;
FinalizationStatus status;
int queue_count;
FinalizerFunction finalizer;

void queued() {
status = eQueued;
Expand Down
15 changes: 4 additions & 11 deletions vm/objectmemory.cpp
Expand Up @@ -452,10 +452,11 @@ namespace rubinius {
code_manager_.add_resource(cr);
}

void ObjectMemory::needs_finalization(Object* obj) {
void ObjectMemory::needs_finalization(Object* obj, FinalizerFunction func) {
FinalizeObject fi;
fi.object = obj;
fi.status = FinalizeObject::eLive;
fi.finalizer = func;

// Makes a copy of fi.
finalize_.push_back(fi);
Expand All @@ -466,16 +467,8 @@ namespace rubinius {
i != to_finalize_.end(); ) {
FinalizeObject* fi = *i;

if(IO* io = try_as<IO>(fi->object)) {
io->finalize(state);
} else if(Fiber* fib = try_as<Fiber>(fi->object)) {
fib->finalize(state);
} else if(MemoryPointer* ptr = try_as<MemoryPointer>(fi->object)) {
ptr->finalize(state);
} else if(Data* data = try_as<Data>(fi->object)) {
data->finalize(state);
} else if(Dir* dir = try_as<Dir>(fi->object)) {
dir->finalize(state);
if(fi->finalizer) {
(*fi->finalizer)(state, fi->object);
} else {
std::cerr << "Unsupported object to be finalized: "
<< fi->object->to_s(state)->c_str() << "\n";
Expand Down
2 changes: 1 addition & 1 deletion vm/objectmemory.hpp
Expand Up @@ -193,7 +193,7 @@ namespace rubinius {

int mature_bytes_allocated();

void needs_finalization(Object* obj);
void needs_finalization(Object* obj, FinalizerFunction func = 0);
void run_finalizers(STATE);

void find_referers(Object* obj, ObjectArray& result);
Expand Down

0 comments on commit e2136c4

Please sign in to comment.