udevbe/jaccall

Mimic stack allocated memory, using Java's gc

Zubnix opened this issue · 0 comments

Currently all native allocated memory has to be manually freed. This causes issues when instantiating a struct type without permanent native allocated memory (through a call to new). If that struct type exposes a pointer (array) field, the resulting pointer field's address will be exposed, yet the memory it points to will be freed automatically as soon as the struct is gc.

This is in conflict with the current design, where all native allocated memory is considered non stack allocated. As such, the current behavior might lead to unexpected behavior.

The proposed fix is to mimic C's stack allocated memory by using Java's GC to free 'stack' memory.
This will require the introduction of heap and stack pointers, as well as the introduction of Pointer.ref(StructType structType), which consistently returns the same pointer used by the given struct instance. The old Pointer.nref(StructType structType) will be removed. The behavior of the other Pointer.nref(...)functions will remain the same with the difference that the returned Pointer will automatically free its memory as soon as the Java object is GC. The allocated non auto-free memory, Pointer.malloc(int size) can still be used (remains unchanged).