StagingBuff: Move constructor into .cpp file
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace smo {
|
namespace smo {
|
||||||
namespace stim_buff {
|
namespace stim_buff {
|
||||||
@@ -87,7 +89,7 @@ void StagingBuffer::computeSlotStrideAndBufferSize()
|
|||||||
|
|
||||||
// Total size needed: alignment padding + slot area, then ensure minimum is met
|
// Total size needed: alignment padding + slot area, then ensure minimum is met
|
||||||
size_t rawSize = alignmentPadding + slotAreaSize;
|
size_t rawSize = alignmentPadding + slotAreaSize;
|
||||||
if (rawSize < minBufferSize)
|
if (rawSize < minBufferSize)
|
||||||
{ rawSize = minBufferSize; }
|
{ rawSize = minBufferSize; }
|
||||||
|
|
||||||
// Align up to the maximum alignment to ensure we can always find a valid offset
|
// Align up to the maximum alignment to ensure we can always find a valid offset
|
||||||
@@ -158,5 +160,65 @@ size_t StagingBuffer::calculateFirstSlotOffsetAndValidate(
|
|||||||
return firstSlotOffsetNBytes;
|
return firstSlotOffsetNBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StagingBuffer::StagingBuffer(
|
||||||
|
const IOEngineConstraints& inputEngineConstraints_,
|
||||||
|
const IOEngineConstraints& /*outputEngineConstraints*/,
|
||||||
|
size_t nDgramsPerFrame)
|
||||||
|
: buffer(nullptr, MmapDeleter(0)), bufferNBytes(0),
|
||||||
|
nDgramsPerFrame(nDgramsPerFrame), slotStrideNBytes(0),
|
||||||
|
firstSlotOffsetNBytes(0),
|
||||||
|
inputConstraints(inputEngineConstraints_),
|
||||||
|
assemblingFlag(false)
|
||||||
|
{
|
||||||
|
if (nDgramsPerFrame == 0)
|
||||||
|
{
|
||||||
|
throw std::invalid_argument(std::string(__func__)
|
||||||
|
+ ": StagingBuffer: nDgramsPerFrame must be > 0");
|
||||||
|
}
|
||||||
|
|
||||||
|
computeSlotStrideAndBufferSize();
|
||||||
|
|
||||||
|
/* Allocate buffer using mmap() for io_uring registration
|
||||||
|
* MAP_ANONYMOUS | MAP_PRIVATE creates anonymous, non-file-backed memory
|
||||||
|
*/
|
||||||
|
void* mmapped = mmap(
|
||||||
|
nullptr, bufferNBytes,
|
||||||
|
PROT_READ | PROT_WRITE,
|
||||||
|
MAP_ANONYMOUS | MAP_PRIVATE,
|
||||||
|
-1, 0);
|
||||||
|
|
||||||
|
if (mmapped == MAP_FAILED)
|
||||||
|
{
|
||||||
|
throw std::runtime_error(std::string(__func__)
|
||||||
|
+ ": StagingBuffer: mmap() failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = std::unique_ptr<uint8_t, MmapDeleter>(
|
||||||
|
static_cast<uint8_t*>(mmapped), MmapDeleter(bufferNBytes));
|
||||||
|
currentNBytes.store(0);
|
||||||
|
|
||||||
|
// Calculate offset and validate invariants (helper function in .cpp)
|
||||||
|
firstSlotOffsetNBytes = StagingBuffer::calculateFirstSlotOffsetAndValidate(
|
||||||
|
buffer.get(), bufferNBytes, nDgramsPerFrame,
|
||||||
|
slotStrideNBytes, inputConstraints);
|
||||||
|
|
||||||
|
// Build FrameAssemblyDesc once
|
||||||
|
std::vector<FrameAssemblyDesc::SlotDesc> slots;
|
||||||
|
slots.reserve(nDgramsPerFrame);
|
||||||
|
uint8_t *frameBase = buffer.get() + firstSlotOffsetNBytes;
|
||||||
|
for (size_t i = 0; i < nDgramsPerFrame; ++i)
|
||||||
|
{
|
||||||
|
size_t off = i * slotStrideNBytes;
|
||||||
|
FrameAssemblyDesc::SlotDesc s{
|
||||||
|
off, frameBase + off, inputConstraints.slotPadToNBytes};
|
||||||
|
|
||||||
|
slots.push_back(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
frameDesc = std::make_shared<FrameAssemblyDesc>(
|
||||||
|
nDgramsPerFrame, inputConstraints.slotPadToNBytes, bufferNBytes,
|
||||||
|
std::move(slots));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace stim_buff
|
} // namespace stim_buff
|
||||||
} // namespace smo
|
} // namespace smo
|
||||||
|
|||||||
@@ -3,15 +3,11 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <functional>
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <algorithm>
|
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/uio.h>
|
#include <sys/uio.h>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "frameAssemblyDesc.h"
|
#include "frameAssemblyDesc.h"
|
||||||
|
|
||||||
@@ -166,69 +162,6 @@ private:
|
|||||||
std::atomic<bool> assemblingFlag;
|
std::atomic<bool> assemblingFlag;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Inline implementations
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
inline StagingBuffer::StagingBuffer(
|
|
||||||
const IOEngineConstraints& inputEngineConstraints_,
|
|
||||||
const IOEngineConstraints& /*outputEngineConstraints*/,
|
|
||||||
size_t nDgramsPerFrame)
|
|
||||||
: buffer(nullptr, MmapDeleter(0)), bufferNBytes(0),
|
|
||||||
nDgramsPerFrame(nDgramsPerFrame), slotStrideNBytes(0),
|
|
||||||
firstSlotOffsetNBytes(0),
|
|
||||||
inputConstraints(inputEngineConstraints_),
|
|
||||||
assemblingFlag(false)
|
|
||||||
{
|
|
||||||
if (nDgramsPerFrame == 0)
|
|
||||||
{
|
|
||||||
throw std::invalid_argument(std::string(__func__)
|
|
||||||
+ ": StagingBuffer: nDgramsPerFrame must be > 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
computeSlotStrideAndBufferSize();
|
|
||||||
|
|
||||||
/* Allocate buffer using mmap() for io_uring registration
|
|
||||||
* MAP_ANONYMOUS | MAP_PRIVATE creates anonymous, non-file-backed memory
|
|
||||||
*/
|
|
||||||
void* mmapped = mmap(
|
|
||||||
nullptr, bufferNBytes,
|
|
||||||
PROT_READ | PROT_WRITE,
|
|
||||||
MAP_ANONYMOUS | MAP_PRIVATE,
|
|
||||||
-1, 0);
|
|
||||||
|
|
||||||
if (mmapped == MAP_FAILED)
|
|
||||||
{
|
|
||||||
throw std::runtime_error(std::string(__func__)
|
|
||||||
+ ": StagingBuffer: mmap() failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer = std::unique_ptr<uint8_t, MmapDeleter>(
|
|
||||||
static_cast<uint8_t*>(mmapped), MmapDeleter(bufferNBytes));
|
|
||||||
currentNBytes.store(0);
|
|
||||||
|
|
||||||
// Calculate offset and validate invariants (helper function in .cpp)
|
|
||||||
firstSlotOffsetNBytes = StagingBuffer::calculateFirstSlotOffsetAndValidate(
|
|
||||||
buffer.get(), bufferNBytes, nDgramsPerFrame,
|
|
||||||
slotStrideNBytes, inputConstraints);
|
|
||||||
|
|
||||||
// Build FrameAssemblyDesc once
|
|
||||||
std::vector<FrameAssemblyDesc::SlotDesc> slots;
|
|
||||||
slots.reserve(nDgramsPerFrame);
|
|
||||||
uint8_t *frameBase = buffer.get() + firstSlotOffsetNBytes;
|
|
||||||
for (size_t i = 0; i < nDgramsPerFrame; ++i)
|
|
||||||
{
|
|
||||||
size_t off = i * slotStrideNBytes;
|
|
||||||
FrameAssemblyDesc::SlotDesc s{
|
|
||||||
off, frameBase + off, inputConstraints.slotPadToNBytes};
|
|
||||||
|
|
||||||
slots.push_back(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
frameDesc = std::make_shared<FrameAssemblyDesc>(
|
|
||||||
nDgramsPerFrame, inputConstraints.slotPadToNBytes, bufferNBytes,
|
|
||||||
std::move(slots));
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace stim_buff
|
} // namespace stim_buff
|
||||||
} // namespace smo
|
} // namespace smo
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user