eclipse-cyclonedds / cyclonedds

Eclipse Cyclone DDS project
https://projects.eclipse.org/projects/iot.cyclonedds
Other
799 stars 349 forks source link

Why use double "_" for alloc? #2000

Closed tooyoungtoosimpo closed 4 days ago

tooyoungtoosimpo commented 1 month ago

image I wonder to know why use "__" for alloc like "HelloWorldData_Msg_alloc()", but use "" for "HelloWorldData_Msg_free()".

eboasson commented 1 month ago

Good question ...

I think the double underscore is because Cyclone IDL compiler follows the mapping from IDL to C specified by the OMG (https://www.omg.org/spec/C/1.0/PDF from 1997 (!)) which states in section 1.8:

For types whose parameter passing modes require heap allocation, an ORB implementation will provide allocation functions. These types include variable-length struct, variable-length union, sequence, any, string, wstring and array of a variable-length type. The return value of these allocation functions must be freed using CORBA_free(). For one of these listed types T, the ORB implementation will provide the following type-specific allocation function:

T *T__alloc();

The functions are defined at global scope using the fully-scoped name of T converted into a C language name (as described in “Scoped Names” on page 1-5) followed by the suffix “__alloc” (note the double underscore). For any, string, and wstring, the allocation functions are:

CORBA_any *CORBA_any_alloc();
char *CORBA_string_alloc();
CORBA_wchar* CORBA_wstring_alloc(CORBA_unsigned_long len);

The free function in that spec is always CORBA_free (independent of the type). The DDS spec doesn't seem to say anything about how allocation and freeing is supposed to work, but the implication of the structure of all the specs is that it would be as prescribed by the spec I just quoted. So it should be CORBA_free! I know OpenSplice uses DDS_free, I have no clue what the others do.

In any case having a typed free function is therefore not following the spec, and it probably should have been a double underscore. For Cyclone, this behaviour was inherited from something way older, and I think changing it isn't worth the bother. Lost in the mists of time ...

(I usually don't bother using them. I usually just do a heap- or stack-allocation and memset it to 0, and call dds_sample_free(ptr, &topic_desc, DDS_FREE_CONTENTS_BIT) and then call free myself if I allocated with malloc.)

tooyoungtoosimpo commented 1 month ago

Thanks for your answer, it helps me a lot.