Skip to content

Commit b717c7d

Browse files
committed
Prepare for a more real task-local alloocator implementation.
1 parent c18331c commit b717c7d

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

stdlib/public/Concurrency/TaskAlloc.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,50 @@
1717
//
1818
//===----------------------------------------------------------------------===//
1919

20+
#include "TaskPrivate.h"
2021
#include "swift/Runtime/Concurrency.h"
2122
#include <stdlib.h>
2223

2324
using namespace swift;
2425

26+
namespace {
27+
28+
class TaskAllocator {
29+
public:
30+
void *alloc(size_t size) {
31+
return malloc(size);
32+
}
33+
34+
void dealloc(void *ptr) {
35+
free(ptr);
36+
}
37+
};
38+
39+
static_assert(sizeof(TaskAllocator) <= sizeof(AsyncTask::AllocatorPrivate),
40+
"task allocator must fit in allocator-private slot");
41+
42+
static_assert(alignof(TaskAllocator) <= alignof(decltype(AsyncTask::AllocatorPrivate)),
43+
"task allocator must not be more aligned than "
44+
"allocator-private slot");
45+
46+
} // end anonymous namespace
47+
48+
void swift::_swift_task_alloc_initialize(AsyncTask *task) {
49+
new (task->AllocatorPrivate) TaskAllocator();
50+
}
51+
52+
static TaskAllocator &allocator(AsyncTask *task) {
53+
return reinterpret_cast<TaskAllocator &>(task->AllocatorPrivate);
54+
}
55+
56+
void swift::_swift_task_alloc_destroy(AsyncTask *task) {
57+
allocator(task).~TaskAllocator();
58+
}
59+
2560
void *swift::swift_task_alloc(AsyncTask *task, size_t size) {
26-
return malloc(size);
61+
return allocator(task).alloc(size);
2762
}
2863

2964
void swift::swift_task_dealloc(AsyncTask *task, void *ptr) {
30-
free(ptr);
65+
return allocator(task).dealloc(ptr);
3166
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===--- TaskPrivate.h - Concurrency library internal interface -*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Internal functions for the concurrency library.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_CONCURRENCY_TASKPRIVATE_H
18+
#define SWIFT_CONCURRENCY_TASKPRIVATE_H
19+
20+
namespace swift {
21+
22+
class AsyncTask;
23+
24+
/// Initialize the task-local allocator in the given task.
25+
void _swift_task_alloc_initialize(AsyncTask *task);
26+
27+
/// Destsroy the task-local allocator in the given task.
28+
void _swift_task_alloc_destroy(AsyncTask *task);
29+
30+
} // end namespace swift
31+
32+
#endif

0 commit comments

Comments
 (0)