Skip to content
Open
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
99 changes: 99 additions & 0 deletions src/fb-cpp/MessageBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* MIT License
*
* Copyright (c) 2026 F.D.Castel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef FBCPP_MESSAGE_BUFFER_H
#define FBCPP_MESSAGE_BUFFER_H

#include <cstddef>
#include <new>
#include <vector>


///
/// fb-cpp namespace.
///
namespace fbcpp
{
///
/// Alignment, in bytes, of the start of every message buffer and of every row inside
/// a RowSet buffer. It is enough for any field of a Firebird message.
///
inline constexpr std::size_t MESSAGE_BUFFER_ALIGNMENT = 16u;

namespace impl
{
template <typename T, std::size_t Alignment>
struct AlignedAllocator
{
using value_type = T;

template <typename U>
struct rebind
{
using other = AlignedAllocator<U, Alignment>;
};

AlignedAllocator() noexcept = default;

template <typename U>
AlignedAllocator(const AlignedAllocator<U, Alignment>&) noexcept
{
}

T* allocate(std::size_t n)
{
return static_cast<T*>(::operator new(n * sizeof(T), std::align_val_t{Alignment}));
}

void deallocate(T* p, std::size_t) noexcept
{
::operator delete(p, std::align_val_t{Alignment});
}

template <typename U>
bool operator==(const AlignedAllocator<U, Alignment>&) const noexcept
{
return true;
}
};

///
/// Rounds `length` up to a multiple of MESSAGE_BUFFER_ALIGNMENT.
///
constexpr unsigned alignMessageLength(unsigned length) noexcept
{
constexpr auto alignment = static_cast<unsigned>(MESSAGE_BUFFER_ALIGNMENT);
return (length + alignment - 1u) & ~(alignment - 1u);
}
} // namespace impl

///
/// Byte buffer holding Firebird messages, whose start is aligned to
/// MESSAGE_BUFFER_ALIGNMENT regardless of the global allocator.
///
using MessageBuffer = std::vector<std::byte, impl::AlignedAllocator<std::byte, MESSAGE_BUFFER_ALIGNMENT>>;
} // namespace fbcpp


#endif // FBCPP_MESSAGE_BUFFER_H
7 changes: 4 additions & 3 deletions src/fb-cpp/RowSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ RowSet::RowSet(Statement& statement, unsigned maxRows)

auto outMetadata = statement.getOutputMetadata();
messageLength = outMetadata->getMessageLength(&statusWrapper);
rowStride = alignMessageLength(messageLength);

buffer.resize(static_cast<std::size_t>(maxRows) * messageLength);
buffer.resize(static_cast<std::size_t>(maxRows) * rowStride);

auto resultSet = statement.getResultSetHandle();
auto* dest = buffer.data();
Expand All @@ -56,7 +57,7 @@ RowSet::RowSet(Statement& statement, unsigned maxRows)
auto& currentRow = statement.getOutputMessage();
assert(currentRow.size() == messageLength);
std::copy(currentRow.begin(), currentRow.end(), dest);
dest += messageLength;
dest += rowStride;
++count;
statement.clearCurrentRow();
}
Expand All @@ -72,7 +73,7 @@ RowSet::RowSet(Statement& statement, unsigned maxRows)
if (resultSet->fetchNext(&statusWrapper, dest) != fb::IStatus::RESULT_OK)
break;

dest += messageLength;
dest += rowStride;
++count;
}

Expand Down
22 changes: 19 additions & 3 deletions src/fb-cpp/RowSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "CalendarConverter.h"
#include "Descriptor.h"
#include "Exception.h"
#include "MessageBuffer.h"
#include <cassert>
#include <cstddef>
#include <span>
Expand Down Expand Up @@ -74,6 +75,7 @@ namespace fbcpp
: client{o.client},
count{o.count},
messageLength{o.messageLength},
rowStride{o.rowStride},
buffer{std::move(o.buffer)},
descriptors{std::move(o.descriptors)},
statusWrapper{std::move(o.statusWrapper)},
Expand All @@ -82,6 +84,7 @@ namespace fbcpp
{
o.count = 0;
o.messageLength = 0;
o.rowStride = 0;
}

RowSet& operator=(RowSet&& o) noexcept
Expand All @@ -91,13 +94,15 @@ namespace fbcpp
client = o.client;
count = o.count;
messageLength = o.messageLength;
rowStride = o.rowStride;
buffer = std::move(o.buffer);
descriptors = std::move(o.descriptors);
statusWrapper = std::move(o.statusWrapper);
numericConverter = std::move(o.numericConverter);
calendarConverter = std::move(o.calendarConverter);
o.count = 0;
o.messageLength = 0;
o.rowStride = 0;
}

return *this;
Expand All @@ -123,6 +128,15 @@ namespace fbcpp
return messageLength;
}

///
/// @brief Returns the distance (in bytes) between the starts of consecutive rows in the buffer.
/// It is the message length rounded up to MESSAGE_BUFFER_ALIGNMENT, so every row starts aligned.
///
unsigned getRowStride() const noexcept
{
return rowStride;
}

///
/// @brief Returns a Row view for typed access to the row at `index`.
/// @param index Zero-based row index (must be < getCount()).
Expand All @@ -140,14 +154,15 @@ namespace fbcpp
std::span<const std::byte> getRawRow(unsigned index) const
{
assert(index < count);
const auto* data = buffer.data() + static_cast<std::size_t>(index) * messageLength;
const auto* data = buffer.data() + static_cast<std::size_t>(index) * rowStride;
return {data, messageLength};
}

///
/// @brief Returns the entire contiguous buffer containing all fetched rows.
/// Row index starts at index * getRowStride().
///
const std::vector<std::byte>& getRawBuffer() const noexcept
const MessageBuffer& getRawBuffer() const noexcept
{
return buffer;
}
Expand All @@ -156,7 +171,8 @@ namespace fbcpp
Client* client;
unsigned count = 0;
unsigned messageLength = 0;
std::vector<std::byte> buffer;
unsigned rowStride = 0;
MessageBuffer buffer;
std::vector<Descriptor> descriptors;
impl::StatusWrapper statusWrapper;
impl::NumericConverter numericConverter;
Expand Down
4 changes: 2 additions & 2 deletions src/fb-cpp/Statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ Statement::Statement(
break;
}

const auto processMetadata = [&](FbRef<fb::IMessageMetadata>& metadata, std::vector<Descriptor>& descriptors,
std::vector<std::byte>& message)
const auto processMetadata =
[&](FbRef<fb::IMessageMetadata>& metadata, std::vector<Descriptor>& descriptors, MessageBuffer& message)
{
if (!metadata)
return;
Expand Down
11 changes: 6 additions & 5 deletions src/fb-cpp/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "NumericConverter.h"
#include "CalendarConverter.h"
#include "Descriptor.h"
#include "MessageBuffer.h"
#include "impl/ParameterSetter.h"
#include "SmartPtrs.h"
#include "Exception.h"
Expand Down Expand Up @@ -255,7 +256,7 @@ namespace fbcpp
///
/// @brief Provides direct access to the raw input message buffer.
///
std::vector<std::byte>& getInputMessage() noexcept
MessageBuffer& getInputMessage() noexcept
{
return inMessage;
}
Expand All @@ -271,7 +272,7 @@ namespace fbcpp
///
/// @brief Provides direct access to the raw output message buffer.
///
std::vector<std::byte>& getOutputMessage() noexcept
MessageBuffer& getOutputMessage() noexcept
{
return outMessage;
}
Expand Down Expand Up @@ -808,7 +809,7 @@ namespace fbcpp
return inDescriptors;
}

std::vector<std::byte>& getSetterMessage() noexcept
MessageBuffer& getSetterMessage() noexcept
{
return inMessage;
}
Expand Down Expand Up @@ -839,10 +840,10 @@ namespace fbcpp
FbRef<fb::IResultSet> resultSetHandle;
FbRef<fb::IMessageMetadata> inMetadata;
std::vector<Descriptor> inDescriptors;
std::vector<std::byte> inMessage;
MessageBuffer inMessage;
FbRef<fb::IMessageMetadata> outMetadata;
std::vector<Descriptor> outDescriptors;
std::vector<std::byte> outMessage;
MessageBuffer outMessage;
std::unique_ptr<Row> outRow;
StatementType type;
unsigned cursorFlags = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/fb-cpp/impl/ParameterSetter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "../Client.h"
#include "../Descriptor.h"
#include "../Exception.h"
#include "../MessageBuffer.h"
#include "../NumericConverter.h"
#include "../StructBinding.h"
#include "../types.h"
Expand Down Expand Up @@ -1181,7 +1182,7 @@ namespace fbcpp::impl
return owner().getSetterDescriptors();
}

std::vector<std::byte>& message() noexcept
MessageBuffer& message() noexcept
{
return owner().getSetterMessage();
}
Expand Down
5 changes: 3 additions & 2 deletions src/fb-cpp/request/RequestMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "../types.h"
#include "../Blob.h"
#include "../Descriptor.h"
#include "../MessageBuffer.h"
#include "../NumericConverter.h"
#include "../CalendarConverter.h"
#include "../impl/ParameterSetter.h"
Expand Down Expand Up @@ -185,7 +186,7 @@ namespace fbcpp::request
return descriptors;
}

std::vector<std::byte>& getSetterMessage() noexcept
MessageBuffer& getSetterMessage() noexcept
{
return message;
}
Expand All @@ -212,7 +213,7 @@ namespace fbcpp::request
private:
Client* client;
std::vector<Descriptor> descriptors;
std::vector<std::byte> message;
MessageBuffer message;
impl::StatusWrapper statusWrapper;
impl::NumericConverter numericConverter;
impl::CalendarConverter calendarConverter;
Expand Down
49 changes: 47 additions & 2 deletions src/test/RowSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "fb-cpp/Statement.h"
#include "fb-cpp/Transaction.h"
#include <cstddef>
#include <cstdint>
#include <vector>


Expand Down Expand Up @@ -62,7 +63,7 @@ BOOST_AUTO_TEST_CASE(fetchRowsIntoRowSet)
BOOST_CHECK_EQUAL(rowSet.getCount(), 5u);
BOOST_CHECK(rowSet.getMessageLength() > 0);
BOOST_CHECK_EQUAL(
rowSet.getRawBuffer().size(), static_cast<std::size_t>(rowSet.getCount()) * rowSet.getMessageLength());
rowSet.getRawBuffer().size(), static_cast<std::size_t>(rowSet.getCount()) * rowSet.getRowStride());

// Verify row data using typed Row access. The current execute() row is included.
for (unsigned i = 0; i < rowSet.getCount(); ++i)
Expand Down Expand Up @@ -100,7 +101,51 @@ BOOST_AUTO_TEST_CASE(fetchFewerRowsThanMaxRows)

BOOST_CHECK_EQUAL(rowSet.getCount(), 3u);
BOOST_CHECK_EQUAL(
rowSet.getRawBuffer().size(), static_cast<std::size_t>(rowSet.getCount()) * rowSet.getMessageLength());
rowSet.getRawBuffer().size(), static_cast<std::size_t>(rowSet.getCount()) * rowSet.getRowStride());
}

BOOST_AUTO_TEST_CASE(rowsAreAligned)
{
const auto database = getTempFile("RowSet-rowsAreAligned.fdb");

Attachment attachment{getClient(), database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};

const auto isAligned = [](const std::byte* ptr)
{ return reinterpret_cast<std::uintptr_t>(ptr) % MESSAGE_BUFFER_ALIGNMENT == 0u; };

// A message length that is not a multiple of the alignment: smallint + bigint + double + null flags.
Statement select{attachment, transaction,
"select cast(? as smallint), cast(n as bigint) * 1000000000000, cast(n as double precision) / 4 "
"from (select 1 n from rdb$database union all select 2 from rdb$database union all "
"select 3 from rdb$database)"};

BOOST_CHECK(isAligned(select.getInputMessage().data()));
BOOST_CHECK(isAligned(select.getOutputMessage().data()));

select.setInt16(0, 7);
BOOST_REQUIRE(select.execute(transaction));

RowSet rowSet{select, 10};

BOOST_REQUIRE_EQUAL(rowSet.getCount(), 3u);
BOOST_CHECK_NE(rowSet.getMessageLength() % MESSAGE_BUFFER_ALIGNMENT, 0u);
BOOST_CHECK_EQUAL(rowSet.getRowStride() % MESSAGE_BUFFER_ALIGNMENT, 0u);
BOOST_CHECK_GE(rowSet.getRowStride(), rowSet.getMessageLength());

for (unsigned i = 0; i < rowSet.getCount(); ++i)
{
const auto rawRow = rowSet.getRawRow(i);
BOOST_CHECK(isAligned(rawRow.data()));
BOOST_CHECK_EQUAL(rawRow.size(), rowSet.getMessageLength());

auto row = rowSet.getRow(i);
BOOST_CHECK_EQUAL(row.getInt16(0).value(), 7);
BOOST_CHECK_EQUAL(row.getInt64(1).value(), static_cast<std::int64_t>(i + 1) * 1000000000000);
BOOST_CHECK_EQUAL(row.getDouble(2).value(), (i + 1) / 4.0);
}
}

BOOST_AUTO_TEST_CASE(rowSetIsDisconnectedFromStatement)
Expand Down
Loading