Re: [Vala] malloc/free CCode attributes



* Arc Riley wrote, On 24/06/09 17:55:
While don't you write an example of what your proposal would look like to the list?  I honestly don't understand how your proposed solution would work for non-gobject bindings.
Off my head, with possibly wrong syntax, possibly abuse of abstract in interfaces:

/* somewhere in the bowels of vala */
interface IManagedMemory {
  abstract void ref_function();
  abstract void unref_function();
  abstract static void* alloc_function(int size);
  abstract void free_function();
}

/* in the vapi for my python-ish object */
interface IPythonManagedMemory : IManagedMemory {
  void ref_function() { Py_IncRef(this); }
  void unref_function() { Py_DecRef(this); }
  void* alloc_function(int size) { return PyObject_Malloc(size) }
  void free_function() { PyObject_Free(this); }
}

  [Compact]
  [CCode (lower_case_cprefix = "PyObject_"]
  public class Object : IPythonManagedMemory {
..

When the code generator wants to do any memory management operations, it casts to IManagedMemory (if it can) and then goes with that. Shame there is no IInterface to be involved as a cast helper, as with Microsofts interface model, but a way around this deficiency is to have an implementation if IManagedMemory which makes calls to the memory management library I referred to which tracks the allocating module based on the pointer value, so that the right referencer/de-referencer can be called.

That way python could create a bitmap, or libgd could create a bitmap and as long as they both used the memory management mallocer, it would always know based on pointer value who it belonged to.

Sam


On Wed, Jun 24, 2009 at 12:38 PM, Sam Liddicott <sam liddicott com> wrote:
Yes, but it is much cleaner and nicer to express it as an interface than a bunch of ccode attributes.

And not all structs of the same type are allocated by the same subsystem or want freeing with the same free function.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]