12 #ifndef HPP_SERVICE_DYNAMIC_BUFFER_HPP_
13 #define HPP_SERVICE_DYNAMIC_BUFFER_HPP_
18 #include <dds/core/macros.hpp>
20 #include "osapi/osapi_heap.h"
21 #include "routingservice/routingservice_log.h"
22 #include "routingservice/routingservice_remote_config.h"
24 #include <dds/core/Value.hpp>
25 #include <dds/core/types.hpp>
26 #include <rtiboost/assert.hpp>
27 #include <rti/service/ServiceException.hpp>
29 namespace rti {
namespace service {
31 class DynamicBufferProperty {
34 DynamicBufferProperty(int32_t min_size,
bool trim_to_size)
35 :min_size_(min_size), trim_to_size_(trim_to_size)
39 DynamicBufferProperty(
const DynamicBufferProperty &property)
40 :min_size_(property.min_size_), trim_to_size_(property.trim_to_size_)
44 DynamicBufferProperty& min_size(int32_t min_size)
49 int32_t min_size()
const
54 DynamicBufferProperty& trim_to_size(
bool trim_to_size)
56 trim_to_size_ = trim_to_size;
60 bool trim_to_size()
const
74 DynamicBuffer(
const DynamicBufferProperty &property)
75 :property_(property), buffer_(NULL), buffer_length_(0)
77 ensure_length(property.min_size());
82 if (buffer_ != NULL) {
83 RTIOsapiHeap_freeArray(buffer_);
89 void ensure_length(int32_t new_length)
91 if (new_length > buffer_length_) {
92 if (!RTIOsapiHeap_reallocateArray(&buffer_, new_length,
char)) {
93 RTI_THROW_SERVICE_EXCEPTION(
94 &RTI_OSAPI_MEMORY_LOG_OUT_OF_HEAP_STRING_d,
97 buffer_length_ = new_length;
103 if (property_.trim_to_size()) {
104 if (!RTIOsapiHeap_reallocateArray(
106 property_.min_size(),
108 RTI_THROW_SERVICE_EXCEPTION(
109 &RTI_OSAPI_MEMORY_LOG_OUT_OF_HEAP_STRING_d,
110 property_.min_size());
112 buffer_length_ = property_.min_size();
116 void release(
char *&buffer, int32_t& length)
119 length = buffer_length_;
124 void own(
char *buffer, int32_t length)
127 buffer_length_ = length;
137 return buffer_length_;
141 const DynamicBufferProperty property_;
143 int32_t buffer_length_;