Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reserve function to reserve initial space for raw buffer #16

Merged
merged 1 commit into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}