RTI Connext DDS Micro
Version 2.4.8
|
RTI Connext DDS Micro, and DDS, uses IDL as the language to define data-types. One of the constructs in IDL is the sequence; a variable length vector where each element is of the same type. This document describes how to work with sequences and in particular the string sequence since it has special properties.
Logically a sequence can be viewed as a variable length vector with N elements as illustrated below. Note that sequences indices are 0 based.
+---+ 0 | T | +---+ 1 | T | +---+ 2 | T | +---+ | | +---+ N-1 | T | +---+
There are three types of sequences in RTI Connext DDS Micro:
The following built-in sequences exist (please refer to Reference Manuals for the complete API).
IDL Type | RTI Connext DDS Micro Type | RTI Connext DDS Micro Sequence |
---|---|---|
octet | DDS_Octet | DDS_OctetSeq |
char | DDS_Char | DDS_CharSeq |
boolean | DDS_Boolean | DDS_BooleanSeq |
short | DDS_Short | DDS_ShortSeq |
unsigned short | DDS_UnsignedShort | DDS_UnsignedShortSeq |
long | DDS_Long | DDS_LongSeq |
unsigned long | DDS_UnsignedLong | DDS_UnsignedLongSeq |
enum | DDS_Enum | DDS_EnumSeq |
wchar | DDS_Wchar | DDS_WcharSeq |
long long | DDS_LongLong | DDS_LongLongSeq |
unsigned long long | DDS_UnsignedLongLong | DDS_UnsignedLongLongSeq |
float | DDS_Float | DDS_FloatSeq |
double | DDS_Double | DDS_DoubleSeq |
long double | DDS_LongDouble | DDS_LongDoubleSeq |
string | DDS_String | DDS_StringSeq |
wstring | DDS_Wstring | DDS_WstringSeq |
The following are important properties of sequences to remember:
Sequences which originate from IDL are created when the IDL type they belong to is created. IDL sequences are always initialized with the maximum size specified in the IDL file. The maximum size of a type, and hence the sequence size, is used to calculate memory needs for serialization and deserialization buffers. Thus, changing the size of an IDL sequence can lead to hard to find memory corruption.
The string and wstring sequences are special in that not only is the maximum sequence size allocated, but because strings are also always of a finite maximum length, the maximum space needed for each string element is also allocated. This ensure that RTI Connext DDS Micro can prevent memory overruns and validate input.
Some typical scenarios with a long sequence and a string sequence defined in IDL is shown below:
Note that in this examples above the sequence length is set. The maximum size for each sequence is set when my_sample is allocated.
A special case is to copy a string sequence from a sample to a string sequence defined outside of the sample. This is possible, but care must be taken to ensure that the memory is allocated properly:
Conside the IDL type from the previous example. A string sequence of equal size can be allocated as follows:
If instead the following code was used, memory for the string in app_seq would be allocated as needed.
Application defined sequences work in the same way as sequences defined in IDL with two exceptions:
DDS_StringSet_set_maximum does not allocate space for the string pointers. The memory must be allocated on a per needed basis and calls to copy may reallocate memory as needed. Use DDS_StringSeq_set_maximum_w_max or DDS_StringSeq_ensure_length_w_max to also allocate pointers. In this case _copy will _not reallocate memory.
Note that it is not allowed to mix between calls that pass the max (ends in _w_max) and calls that do not. Doing so may cause memory leaks and/or memory corruption.
DDS_StringSeq_finalize automatically free memory pointed to by each element using DDS_String_free. All memory allocated to a string element should be allocated using a DDS_String function.
It is possible to assign any memory to a string sequence element if all elements are released manually first: