1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
* Copyright (C) 2016-2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <Block.h>
#include <utility>
#include <wtf/Assertions.h>
#include <wtf/StdLibExtras.h>
#if __has_include(<ptrauth.h>)
#include <ptrauth.h>
#ifdef __ptrauth_block_copy_helper
#define WTF_COPY_FUNCTION_POINTER_QUALIFIER __ptrauth_block_copy_helper
#else
#define WTF_COPY_FUNCTION_POINTER_QUALIFIER
#endif
#ifdef __ptrauth_block_destroy_helper
#define WTF_DISPOSE_FUNCTION_POINTER_QUALIFIER __ptrauth_block_destroy_helper
#else
#define WTF_DISPOSE_FUNCTION_POINTER_QUALIFIER
#endif
#ifdef __ptrauth_block_invocation_pointer
#define WTF_INVOKE_FUNCTION_POINTER_QUALIFIER __ptrauth_block_invocation_pointer
#else
#define WTF_INVOKE_FUNCTION_POINTER_QUALIFIER
#endif
#ifdef __ptrauth_objc_isa_pointer
#define WTF_ISA_POINTER_QUALIFIER __ptrauth_objc_isa_pointer
#else
#define WTF_ISA_POINTER_QUALIFIER
#endif
#else
#define WTF_COPY_FUNCTION_POINTER_QUALIFIER
#define WTF_DISPOSE_FUNCTION_POINTER_QUALIFIER
#define WTF_INVOKE_FUNCTION_POINTER_QUALIFIER
#define WTF_ISA_POINTER_QUALIFIER
#endif
// Because ARC enablement is a compile-time choice, and we compile this header
// both ways, we need a separate copy of our code when ARC is enabled.
#if __has_feature(objc_arc)
#define BlockPtr BlockPtrArc
#define makeBlockPtr makeBlockPtrArc
#endif
namespace WTF {
extern "C" void* _NSConcreteMallocBlock[32];
template<typename> class BlockPtr;
template<typename R, typename... Args>
class BlockPtr<R (Args...)> {
public:
using BlockType = R (^)(Args...);
template<typename F>
static BlockPtr fromCallable(F function)
{
struct Descriptor {
uintptr_t reserved;
uintptr_t size;
void (*WTF_COPY_FUNCTION_POINTER_QUALIFIER copy)(void *dst, const void *src);
void (*WTF_DISPOSE_FUNCTION_POINTER_QUALIFIER dispose)(const void *);
};
struct Block {
void* WTF_ISA_POINTER_QUALIFIER isa;
int32_t flags;
int32_t reserved;
R (*WTF_INVOKE_FUNCTION_POINTER_QUALIFIER invoke)(void *, Args...);
const struct Descriptor* descriptor;
F f;
};
static const Descriptor descriptor {
0,
sizeof(Block),
// We keep the copy function null - the block is already on the heap
// so it should never be copied.
nullptr,
[](const void* ptr) {
static_cast<Block*>(const_cast<void*>(ptr))->f.~F();
}
};
Block* block = static_cast<Block*>(malloc(sizeof(Block)));
block->isa = _NSConcreteMallocBlock;
enum {
BLOCK_NEEDS_FREE = (1 << 24),
BLOCK_HAS_COPY_DISPOSE = (1 << 25),
};
const unsigned retainCount = 1;
block->flags = BLOCK_HAS_COPY_DISPOSE | BLOCK_NEEDS_FREE | (retainCount << 1);
block->reserved = 0;
block->invoke = [](void *ptr, Args... args) -> R {
return static_cast<Block*>(ptr)->f(std::forward<Args>(args)...);
};
block->descriptor = &descriptor;
new (&block->f) F { std::move(function) };
#if __has_feature(objc_arc)
return BlockPtr { (__bridge_transfer BlockType)block };
#else
BlockPtr blockPtr;
blockPtr.m_block = reinterpret_cast<BlockType>(block);
return blockPtr;
#endif
}
BlockPtr()
: m_block(nullptr)
{
}
BlockPtr(BlockType block)
#if __has_feature(objc_arc)
: m_block(block)
#else
: m_block(Block_copy(block))
#endif
{
}
BlockPtr(const BlockPtr& other)
#if __has_feature(objc_arc)
: m_block(other.m_block)
#else
: m_block(Block_copy(other.m_block))
#endif
{
}
BlockPtr(BlockPtr&& other)
: m_block(std::exchange(other.m_block, nullptr))
{
}
~BlockPtr()
{
#if !__has_feature(objc_arc)
Block_release(m_block);
#endif
}
BlockPtr& operator=(const BlockPtr& other)
{
#if __has_feature(objc_arc)
m_block = other.m_block;
#else
if (this != &other) {
Block_release(m_block);
m_block = Block_copy(other.m_block);
}
#endif
return *this;
}
BlockPtr& operator=(BlockPtr&& other)
{
ASSERT(this != &other);
#if !__has_feature(objc_arc)
Block_release(m_block);
#endif
m_block = std::exchange(other.m_block, nullptr);
return *this;
}
BlockType get() const { return m_block; }
explicit operator bool() const { return m_block; }
bool operator!() const { return !m_block; }
R operator()(Args... arguments) const
{
ASSERT(m_block);
return m_block(std::forward<Args>(arguments)...);
}
private:
BlockType m_block;
};
template<typename R, typename... Args>
inline BlockPtr<R (Args...)> makeBlockPtr(R (^block)(Args...))
{
return BlockPtr<R (Args...)>(block);
}
template<typename F, typename Class, typename R, typename... Args>
inline auto makeBlockPtr(F&& function, R (Class::*)(Args...) const)
{
return BlockPtr<R (Args...)>::fromCallable(std::forward<F>(function));
}
template<typename F, typename Class, typename R, typename... Args>
inline auto makeBlockPtr(F&& function, R (Class::*)(Args...))
{
return BlockPtr<R (Args...)>::fromCallable(std::forward<F>(function));
}
template<typename F>
inline auto makeBlockPtr(F&& function)
{
return makeBlockPtr(std::forward<F>(function), &F::operator());
}
}
using WTF::BlockPtr;
using WTF::makeBlockPtr;
|