RLP memory, types and NULL pointer

I searched around the forum for answers to these questions, but the search functionality is producing less than desirable results (read as I’m getting blasted in the face by results that don’t appear relevant to the search input, and the results are being presented oldest->recent instead of the reverse).

Firstly, I’m looking for some clarification about memory allocation.

The documentation states that the malloc(…) and free(…) functions handle memory on the heap that is shared between NETMF and RLP, while the malloc_CustomHeap(…) and free_CustomHeap(…) handle memory that is only for native code use.

  1. When (if ever) would you want to allocate memory on the shared heap for use within a native procedure?
  2. Is the transfer of data between managed and unmanaged code being handled on the shared heap?
  3. Does the garbage collector just manage the memory in the shared heap, or is it managing the memory in the custom heap as well?

Second, can types defined in the native code be used in the managed code? The method RLP.FindSymbol(…) seems to imply this is possible, but I’m not sure of the usage. Why I’m wondering is because I would like to send the contents of a data structure from the native code back to the managed code, instead of an address to the start of the contents of a byte array. I guess what is really comes down to, is can I share structure types and pass an instance of that structure type from the native code to the managed code?

Third, I see that there are definitions for true and false in the native code (RLP_TRUE and RLP_FALSE in RLP.h), but why isn’t there a definition for NULL? I like to use NULL in logic, because it helps to remind me that I’m checking a pointer. If I’m to define this myself ( I’m no C expert) it should be something like #define NULL (void*)0, correct?

Since custom heap is only used for very large images, which you probably do not have, then custom heap is not used by NETMF and I would suggest you would use it for your allocations. It is simpler and faster.
GC does not maintain either method in this case.

Not structures, not objects. But you can share a byte array that represent a structure, a simple form of serialization.

[quote]
Third, I see that there are definitions for true and false in the native code (RLP_TRUE and RLP_FALSE in RLP.h), but why isn’t there a definition for NULL? I like to use NULL in logic, because it helps to remind me that I’m checking a pointer. If I’m to define this myself ( I’m no C expert) it should be something like #define NULL (void*)0, correct?[/quote]

You can add NULL, it doesn’t effect anything else.

@ Gus -

  1. Got it, use the CustomHeap for everything in native.

I’m still confused. I see that the structure can be sent as a byte array back over to the managed code by casting it to an int and returning that value. But how do I then decode it when I get back into C#? Is that where the RLP.FindSymbol(…) comes in, or is that method irrelevant? Is there an example of something along these lines that you can point me to?

  1. I think GHI should consider adding a macro for NULL to the “standard” version of RLP.h. To me, it seems like the logical place for that macro to exist, and it would be an easy addition since its only one line of code.

You can’t pass pointers in C#, the way we normally do it is by sending a byte array down to RLP which gets fulled and sent back to C#.

right, I’m with ya about passing the byte arrays back and forth between native code and managed code. But how do you decode the structure that has been passed back to the managed code as a byte array? I just did a quick review of the RLP demos, and nothing on this particular topic is standing out. I don’t see a straightforward way to do it without knowing something about how the structure is put together at the native level.

You will need to parse them one by one, but using ExtractValuefromArray should help more. This should help GHI Electronics – Where Hardware Meets Software

Got it, thanks for pointing me to that link.