How to manage memory with loan_continguous

The following methods (provided in the FooSeq class) will help you manage RTI Connext sequences:

  • FooSeq::loan_contiguous() for C++
  • FooSeq::loan_discontiguous() for C++
  • FooSeq_loan_contiguous() for C
  • FooSeq_loan_discontiguous() for C
  • FooSeq::unloan() for C++
  • FooSeq_unloan() for C
  • for Java and C# (Note:Only contiguous available.)
  • Sequence.unloan() for Java and C#

It is good to use loan_xxxx() when your application already has allocated memory and you want RTI Connext to use part of that memory for the creation of sequences of objects. Without loan_xxxx()RTI Connext will allocate memory whenever you increase the length of a sequence.

If you are using loan_contiguous(), you can only loan memory to a sequence that doesn't already have memory allocated to it (i.e., its maximum() is 0). The FooSeq() constructor defaults to an initial maximum of 0, so that FooSeq.maximum() returns 0.

If you want to operate on a sample's data sequence whose memory is managed by the application, the following code snippet could apply.

instance->string_data.maximum(0); // fooSequence is a sequence of type
                                  // Foo contained in the topic instance.
char *Buffer[5]; // Allocate memory
instance->string_data.loan_contiguous(Buffer, 0, 5); // Length 0, Maximum 5
instance->string_data.length(3); // Set current length to 3.
// Populate sequence data
instance->string_data[0] = DDS_String_dup("x");
instance->string_data[1] = DDS_String_dup("y");
instance->string_data[2] = DDS_String_dup("z");
// Output topic sample.
sequences_writer->write(*instance, instance_handle);
Programming Language: