| 261 | } |
| 262 | |
| 263 | Result AsyncBuffersPool::createChildView(AsyncBufferView::ID parentBufferID, size_t offset, size_t length, |
| 264 | AsyncBufferView::ID& outChildBufferID) |
| 265 | { |
| 266 | AsyncBufferView* parent = getBuffer(parentBufferID); |
| 267 | SC_TRY_MSG(parent != nullptr, "AsyncBuffersPool::createChildView - Invalid parent bufferID"); |
| 268 | |
| 269 | AsyncBufferView::ID rootParentID = parentBufferID; |
| 270 | size_t rootOffset = parent->offset + offset; |
| 271 | |
| 272 | // Flatten nested child views |
| 273 | while (parent->type == AsyncBufferView::Type::Child) |
| 274 | { |
| 275 | rootParentID = parent->parentID; |
| 276 | parent = getBuffer(rootParentID); |
| 277 | SC_TRY_MSG(parent != nullptr, "AsyncBuffersPool::createChildView - Internal Error (Invalid parent ID)"); |
| 278 | } |
| 279 | |
| 280 | Span<const char> rootFullData; |
| 281 | // We need the full data of the root parent to check bounds |
| 282 | switch (parent->type) |
| 283 | { |
| 284 | case AsyncBufferView::Type::Writable: rootFullData = parent->writableData; break; |
| 285 | case AsyncBufferView::Type::ReadOnly: rootFullData = parent->readonlyData; break; |
| 286 | case AsyncBufferView::Type::Growable: { |
| 287 | AsyncBufferView::GrowableStorage storage; |
| 288 | |
| 289 | auto da = parent->getGrowableBuffer(storage, true)->getDirectAccess(); |
| 290 | rootFullData = {static_cast<const char*>(da.data), da.sizeInBytes}; |
| 291 | (void)parent->getGrowableBuffer(storage, false); // destruct |
| 292 | break; |
| 293 | } |
| 294 | default: return Result::Error("AsyncBuffersPool::createChildView - Invalid root parent type"); |
| 295 | } |
| 296 | |
| 297 | SC_TRY_MSG(rootOffset + length <= rootFullData.sizeInBytes(), |
| 298 | "AsyncBuffersPool::createChildView - Offset and length out of bounds"); |
| 299 | |
| 300 | for (size_t idx = 0; idx < buffers.sizeInElements(); ++idx) |
| 301 | { |
| 302 | if (buffers[idx].type == AsyncBufferView::Type::Empty) |
| 303 | { |
| 304 | AsyncBufferView& child = buffers[idx]; |
| 305 | child.type = AsyncBufferView::Type::Child; |
| 306 | child.parentID = rootParentID; |
| 307 | child.offset = rootOffset; |
| 308 | child.length = length; |
| 309 | child.refs = 1; |
| 310 | // Pre-calculate data span for debugging visibility |
| 311 | if (parent->type == AsyncBufferView::Type::ReadOnly) |
| 312 | { |
| 313 | (void)rootFullData.sliceStartLength(rootOffset, length, child.readonlyData); |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | // Root is Writable or Growable (treated as writable here) |
| 318 | Span<char> rootWritableData = {const_cast<char*>(rootFullData.data()), rootFullData.sizeInBytes()}; |
| 319 | (void)rootWritableData.sliceStartLength(rootOffset, length, child.writableData); |
| 320 | } |
no test coverage detected