-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
907a97f
commit 5a97441
Showing
4 changed files
with
161 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (c) 2021-2022 NVIDIA Corporation | ||
* | ||
* Licensed under the Apache License Version 2.0 with LLVM Exceptions | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* https://llvm.org/LICENSE.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#pragma once | ||
|
||
#include "__execution_fwd.hpp" | ||
#include "__scope.hpp" | ||
|
||
#include <memory> | ||
#include <memory_resource> | ||
|
||
namespace stdexec { | ||
namespace __mem { | ||
|
||
template <class _Alloc> | ||
struct __allocator { | ||
using __value_type = typename std::allocator_traits<_Alloc>::value_type; | ||
|
||
explicit __allocator(_Alloc __alloc) | ||
: __alloc_(std::move(__alloc)) {} | ||
|
||
auto __allocate() { | ||
return std::allocator_traits<_Alloc>::allocate(__alloc_, 1); | ||
} | ||
|
||
void __destroy(__value_type* __ptr) noexcept { | ||
return std::allocator_traits<_Alloc>::destroy(__alloc_, __ptr); | ||
} | ||
|
||
void __deallocate(__value_type* __ptr) noexcept { | ||
return std::allocator_traits<_Alloc>::deallocate(__alloc_, __ptr, 1); | ||
} | ||
|
||
private: | ||
STDEXEC_ATTRIBUTE((no_unique_address)) _Alloc __alloc_; | ||
}; | ||
|
||
template <class _Data, auto _GetAllocator> | ||
struct __with_allocator_provider { | ||
// _GetAllocator returns an allocator for the derived type. | ||
using _Derived = typename __result_of<_GetAllocator, _Data&>::__value_type; | ||
|
||
union { | ||
_Data __data_; | ||
}; | ||
|
||
// The __data_ field is already initialized; don't touch it. | ||
constexpr __with_allocator_provider() noexcept {} | ||
|
||
static void* operator new(std::size_t size, _Data&& __data) { | ||
auto __alloc = _GetAllocator(__data); | ||
_Derived* __ptr = __alloc.__allocate(); | ||
[[maybe_unused]] __scope_guard __g{[&]() noexcept { __alloc.__deallocate(__ptr); }}; | ||
::new(&__ptr->__data_) _Data((_Data&&) __data); | ||
__g.__dismiss(); | ||
return __ptr; | ||
} | ||
|
||
static void operator delete(void* __self, _Data&&) { | ||
_Derived* __ptr = static_cast<_Derived*>(__self); | ||
auto __alloc = _GetAllocator(__ptr->__data_); | ||
__ptr->__data_.~_Data(); | ||
__alloc.__deallocate(__ptr); | ||
} | ||
|
||
static void operator delete(__with_allocator_provider* __self, std::destroying_delete_t) noexcept { | ||
_Derived* __ptr = static_cast<_Derived*>(__self); | ||
auto __alloc = _GetAllocator(__ptr->__data_); | ||
__ptr->__data_.~_Data(); | ||
__alloc.__destroy(__ptr); | ||
__alloc.__deallocate(__ptr); | ||
} | ||
}; | ||
|
||
template <class _Alloc, class _Ty> | ||
using __alloc_rebind_t = typename std::allocator_traits<_Alloc>::template rebind_alloc<_Ty>; | ||
|
||
template <class _Derived> | ||
inline constexpr auto __get_allocator_for = [](auto& __data) noexcept { | ||
auto __alloc = query_or(get_allocator, get_env(__data), std::allocator<void>()); | ||
return __allocator{__alloc_rebind_t<decltype(__alloc), _Derived>{__alloc}}; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters