LBg/rbSFML-dev — Issue #3: Garbage Collected Allocations
Author: Groogy
Created: 2012-01-22 05:14:44+00:00
State: open

Well since Ruby does a fine job in pre-allocating a big heap maybe we should take advantage of it and jump in to the Ruby allocated heap and letting the garbage collector manage that memory.

The code would be pretty simple, the problem I see would be that we have to instead of providing only a free function pointer but also provide a mark function pointer.

```cpp
// rbSFML.hpp
template< typename T >
static inline VALUE ALLOCATE( VALUE aKlass )
{
T* memory = ALLOC( T );
if( memory == NULL ) rb_memerror();
T* object = new( memory ) T;
return ToRuby( object, aKlass );
}
```

Though problem is that I have no idea what is the free method in the C API so I don't know how to write the free function.
Something like this I guess:

```cpp
template< typename T >
static inline void FREE( void *anObject )
{
delete( anObject ) static_cast< T* >( anObject ); // "Call destructor but not release memory" delete-operator
SOME_RUBY_FREE_FUNC( anObject ); // The missing free function in the C API which I don't know.
}
```

Also the mark function, not entirely sure about that one either. There is some digging of information that is needed before implementing but I think we will gain a lot from this as the memory will be aligned with rest of ruby and allocations should be a bit faster as we have big chunks to pick from instead of doing constant system calls.