Skip to content

Commit

Permalink
Merge pull request #16 from Karsten1987/expose_cdr
Browse files Browse the repository at this point in the history
reserve function to reserve initial space for raw buffer
  • Loading branch information
richiware authored May 21, 2018
2 parents 581bc96 + 0e102a0 commit e986a9e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/fastcdr/FastBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ namespace eprosima
return (iterator(m_buffer, m_bufferSize));
}

/*!
* @brief This function reserves memory for the internal raw buffer. It will only do so if the buffer is not yet allocated and is not externally set.
* @param size The size of the memory to be allocated.
* @return True if the allocation suceeded. False if the raw buffer was set externally or is already allocated.
*/
bool reserve(size_t size);

/*!
* @brief This function resizes the raw buffer. It will call the user's defined function for this purpose.
* @param minSizeInc The minimun growth expected of the current raw buffer.
Expand Down
13 changes: 13 additions & 0 deletions src/cpp/FastBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ FastBuffer::~FastBuffer()
}
}

bool FastBuffer::reserve(size_t size)
{
if (m_internalBuffer && m_buffer == NULL)
{
m_buffer = (char *)malloc(size);
if (m_buffer) {
m_bufferSize = size;
return true;
}
}
return false;
}

bool FastBuffer::resize(size_t minSizeInc)
{
size_t incBufferSize = BUFFER_START_LENGTH;
Expand Down
18 changes: 18 additions & 0 deletions test/ResizeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3820,3 +3820,21 @@ TEST(FastCDRResizeTests, Complete)
free(ldouble_seq_value);
free(c_string_value);
}

TEST(CDRResizeTests, ReserveBuffer)
{
FastBuffer buffer0;
EXPECT_EQ(true, buffer0.reserve(100));
EXPECT_EQ(100, buffer0.getBufferSize());

FastBuffer buffer1;
EXPECT_EQ(true, buffer1.resize(100));
EXPECT_EQ(200, buffer1.getBufferSize());
EXPECT_EQ(false, buffer1.reserve(300));
EXPECT_EQ(200, buffer1.getBufferSize());

char raw_buffer[10];
FastBuffer buffer2(&raw_buffer[0], 10);
EXPECT_EQ(false, buffer2.reserve(100));
EXPECT_EQ(10, buffer2.getBufferSize());
}

0 comments on commit e986a9e

Please sign in to comment.