My suggestion would be to mirror only the parts of those structs that are part of the public API. Then use unsafe_load / unsafe_store to read / modify / write the intended changes. The API authors were nice to put the public fields at the top so that you can do this (many libraries don't / can't do this). Then the "real" object will always be the Ptr to the one allocated by GMT, and you only have temporary copies of them alive at any time inside Julia.
On Mon, Aug 10, 2015 at 2:53 PM J Luis <[email protected]> wrote: > > >> usually, there's is one big toplevel distinction driven by who will free >> the memory: if C will be freeing the memory, then you must always have the >> C code also allocating the memory (hopefully the library author provide >> some sort of new / alloc / create function, although sometimes they rely on >> the user calling malloc). >> > > Yes, the GMT_Create_Data (that ccals a C function with the same name) does > that. > > https://github.com/joa-quim/GMT.jl/blob/master/src/gmt_main.jl#L852 > <https://github.com/joa-quim/GMT.jl/blob/master/src/gmt_main.jl#L852> > >> >> > Is that really impossible and I'm doomed to the path exemplified >> with that C GMT_blind_change_struct() function? >> >> ideally, C libraries that expect to manage a struct's memory also provide >> accessor methods to read any of the structs important fields so that you >> can always declare its type as Ptr{Void} in Julia. However, I realize this >> is true of many C libraries. >> > > > The pointers are ok, my real problem are those 'scalars' like the n_rows in > > > http://gmt.soest.hawaii.edu/projects/gmt/repository/entry/branches/5.2.0/src/gmt_resources.h#L517 > > I only know the number of rows after having read the data and than the > immut is already created > > >> >> >> On Mon, Aug 10, 2015 at 9:58 AM J Luis <[email protected]> wrote: >> >>> >>> >>> segunda-feira, 10 de Agosto de 2015 às 14:15:15 UTC+1, Isaiah escreveu: >>>> >>>> https://github.com/JuliaLang/julia/pull/12113 >>>> >>> >>> So there is hope. Thanks >>> >>> >>> >>>> >>>> >>>> On Mon, Aug 10, 2015 at 8:11 AM, J Luis <[email protected]> wrote: >>>> >>>>> Hi, >>>>> >>>>> Let me try to explain my need to fight with immutables. I made another >>>>> round to my GMT wrapper (which is hopefully almost ready) and struggled >>>>> again with the immutables. The point is that GMT API structs are deeply >>>>> nested and need to be mirrored with immutables (otherwise Julia crashes). >>>>> Namely this one >>>>> >>>>> >>>>> https://github.com/joa-quim/GMT.jl/blob/master/src/libgmt_h.jl#L1445 >>>>> >>>>> so I first went with the advised solution of creating and filling the >>>>> type is julia and replace the pointees of the immutable type, which I did >>>>> here (see the commented code): >>>>> >>>>> >>>>> https://github.com/joa-quim/GMT.jl/blob/master/src/gmt_main.jl#L874 >>>>> >>>>> but the problem now is that *TS* is a memory owned by Julia and that >>>>> will be fatal when GMT's own memory cleaning functions try to free it and >>>>> -> Julia crashes. >>>>> So if use a pure Julia solution I've no choice but to let memory leeks >>>>> and other house keeping to be done. Not good at all. >>>>> >>>>> For the time being I ended up with this ugly C trick of sending to C >>>>> the job of changing some members (non-pointers) that need absolutely to >>>>> changed after the type has been created >>>>> >>>>> >>>>> http://gmt.soest.hawaii.edu/projects/gmt/repository/entry/branches/5.2.0/src/gmt_api.c#L7376 >>>>> >>>>> but besides being ugly I've no guarantee that I can convince my >>>>> partners to let that backdoor go into the GMT lib. I much lovelier trick >>>>> would be if this chunk only of that backdoor function would work. >>>>> >>>>> else if (!strncmp (keyword, "API_POINTER_", 12U)) { /* >>>>> Blindly change a pointer to a scalar. Irritatingly Julia ignores this */ >>>>> if (!strcmp(&keyword[12], "UINT64")) >>>>> *(uint64_t *)ptr = *(uint64_t *)what; >>>>> } >>>>> >>>>> but the problem is that I am not able to get from Julia the pointers >>>>> address that would make that work. I tried with >>>>> >>>>> pointer(S0.n_rows) >>>>> >>>>> and >>>>> >>>>> pointer_from_objref(S0.n_rows) >>>>> >>>>> and others, but nothing. It seams to work in the C side (I can see it >>>>> with the debugger when I set in that piece of code) but than nothing >>>>> changes. The C struct doesn't change. >>>>> Is there a way to get the C pointer address from Julia? >>>>> >>>>> So, I guess my question is: >>>>> >>>>> How do we do to change immutable members that are not pointers? >>>>> Is that really impossible and I'm doomed to the path exemplified >>>>> with that C GMT_blind_change_struct() function? >>>>> >>>>> >>>>> >>>>> >>>>> >>>>
