Skip to content

Commit 59f6cbc

Browse files
committed
[NFC] Extracted function for header/context size.
So that this functionality is available elsewhere, extract it from swift_task_create_commonImpl into a reusable static functions.
1 parent da45a67 commit 59f6cbc

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

stdlib/public/Concurrency/Task.cpp

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,36 @@ static inline bool taskIsDetached(TaskCreateFlags createFlags, JobFlags jobFlags
565565
return taskIsUnstructured(jobFlags) && !createFlags.copyTaskLocals();
566566
}
567567

568+
static std::pair<size_t, size_t> amountToAllocateForHeaderAndTask(
569+
const AsyncTask *parent, const TaskGroup *group,
570+
const Metadata *futureResultType, size_t initialContextSize) {
571+
// Figure out the size of the header.
572+
size_t headerSize = sizeof(AsyncTask);
573+
if (parent) {
574+
headerSize += sizeof(AsyncTask::ChildFragment);
575+
}
576+
if (group) {
577+
headerSize += sizeof(AsyncTask::GroupChildFragment);
578+
}
579+
if (futureResultType) {
580+
headerSize += FutureFragment::fragmentSize(headerSize, futureResultType);
581+
// Add the future async context prefix.
582+
headerSize += sizeof(FutureAsyncContextPrefix);
583+
} else {
584+
// Add the async context prefix.
585+
headerSize += sizeof(AsyncContextPrefix);
586+
}
587+
588+
headerSize = llvm::alignTo(headerSize, llvm::Align(alignof(AsyncContext)));
589+
// Allocate the initial context together with the job.
590+
// This means that we never get rid of this allocation.
591+
size_t amountToAllocate = headerSize + initialContextSize;
592+
593+
assert(amountToAllocate % MaximumAlignment == 0);
594+
595+
return {headerSize, amountToAllocate};
596+
}
597+
568598
/// Implementation of task creation.
569599
SWIFT_CC(swift)
570600
static AsyncTaskAndContext swift_task_create_commonImpl(
@@ -705,30 +735,9 @@ static AsyncTaskAndContext swift_task_create_commonImpl(
705735

706736
SWIFT_TASK_DEBUG_LOG("Task's base priority = %#zx", basePriority);
707737

708-
// Figure out the size of the header.
709-
size_t headerSize = sizeof(AsyncTask);
710-
if (parent) {
711-
headerSize += sizeof(AsyncTask::ChildFragment);
712-
}
713-
if (group) {
714-
headerSize += sizeof(AsyncTask::GroupChildFragment);
715-
}
716-
if (futureResultType) {
717-
headerSize += FutureFragment::fragmentSize(headerSize, futureResultType);
718-
// Add the future async context prefix.
719-
headerSize += sizeof(FutureAsyncContextPrefix);
720-
} else {
721-
// Add the async context prefix.
722-
headerSize += sizeof(AsyncContextPrefix);
723-
}
724-
725-
headerSize = llvm::alignTo(headerSize, llvm::Align(alignof(AsyncContext)));
726-
727-
// Allocate the initial context together with the job.
728-
// This means that we never get rid of this allocation.
729-
size_t amountToAllocate = headerSize + initialContextSize;
730-
731-
assert(amountToAllocate % MaximumAlignment == 0);
738+
size_t headerSize, amountToAllocate;
739+
std::tie(headerSize, amountToAllocate) = amountToAllocateForHeaderAndTask(
740+
parent, group, futureResultType, initialContextSize);
732741

733742
unsigned initialSlabSize = 512;
734743

0 commit comments

Comments
 (0)