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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
/*
* Copyright (C) 2015-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. ``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
* 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 <wtf/Assertions.h>
#include <wtf/FastMalloc.h>
#include <wtf/StdLibExtras.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC { namespace DFG {
class StructureAbstractValue;
} } // namespace JSC::DFG
namespace WTF {
// FIXME: This currently only works for types that are pointer-like: they should have the size
// of a pointer and like a pointer they should not have assignment operators, copy constructors,
// non-trivial default constructors, and non-trivial destructors. It may be possible to lift all
// of these restrictions. If we succeeded then this should be renamed to just TinySet.
// https://bugs.webkit.org/show_bug.cgi?id=145741
template<typename T>
class TinyPtrSet {
WTF_MAKE_FAST_ALLOCATED;
static_assert(sizeof(T) == sizeof(void*), "It's in the title of the class.");
public:
TinyPtrSet()
: m_pointer(0)
{
setEmpty();
}
TinyPtrSet(T element)
: m_pointer(0)
{
set(element);
}
ALWAYS_INLINE TinyPtrSet(const TinyPtrSet& other)
: m_pointer(0)
{
copyFrom(other);
}
ALWAYS_INLINE TinyPtrSet& operator=(const TinyPtrSet& other)
{
if (this == &other)
return *this;
deleteListIfNecessary();
copyFrom(other);
return *this;
}
~TinyPtrSet()
{
deleteListIfNecessary();
}
void clear()
{
deleteListIfNecessary();
setEmpty();
}
// Returns the only entry if the array has exactly one entry.
T onlyEntry() const
{
if (isThin())
return singleEntry();
OutOfLineList* list = this->list();
if (list->m_length != 1)
return T();
return list->list()[0];
}
bool isEmpty() const
{
bool result = isThin() && !singleEntry();
if (result)
ASSERT(m_pointer != reservedValue);
return result;
}
// Returns true if the value was added, or false if the value was already there.
ALWAYS_INLINE bool add(T value)
{
ASSERT(value);
if (isThin()) {
if (singleEntry() == value)
return false;
if (!singleEntry()) {
set(value);
return true;
}
OutOfLineList* list = OutOfLineList::create(defaultStartingSize);
list->m_length = 2;
list->list()[0] = singleEntry();
list->list()[1] = value;
set(list);
return true;
}
return addOutOfLine(value);
}
bool remove(T value)
{
if (isThin()) {
if (singleEntry() == value) {
setEmpty();
return true;
}
return false;
}
OutOfLineList* list = this->list();
for (unsigned i = 0; i < list->m_length; ++i) {
if (list->list()[i] != value)
continue;
list->list()[i] = list->list()[--list->m_length];
if (!list->m_length) {
OutOfLineList::destroy(list);
setEmpty();
}
return true;
}
return false;
}
bool contains(T value) const
{
if (isThin())
return singleEntry() == value;
return containsOutOfLine(value);
}
ALWAYS_INLINE bool merge(const TinyPtrSet& other)
{
if (other.isThin()) {
if (other.singleEntry())
return add(other.singleEntry());
return false;
}
return mergeOtherOutOfLine(other);
}
void forEach(NOESCAPE const Invocable<void(const T&)> auto& functor) const
{
if (isThin()) {
if (!singleEntry())
return;
functor(singleEntry());
return;
}
OutOfLineList* list = this->list();
for (unsigned i = 0; i < list->m_length; ++i)
functor(list->list()[i]);
}
void genericFilter(NOESCAPE const Invocable<bool(const T&)> auto& functor)
{
if (isThin()) {
if (!singleEntry())
return;
if (functor(singleEntry()))
return;
clear();
return;
}
OutOfLineList* list = this->list();
for (unsigned i = 0; i < list->m_length; ++i) {
if (functor(list->list()[i]))
continue;
list->list()[i--] = list->list()[--list->m_length];
}
if (!list->m_length)
clear();
}
void filter(const TinyPtrSet& other)
{
if (other.isThin()) {
if (!other.singleEntry() || !contains(other.singleEntry()))
clear();
else {
clear();
set(other.singleEntry());
}
return;
}
genericFilter([&] (T value) { return other.containsOutOfLine(value); });
}
void exclude(const TinyPtrSet& other)
{
if (other.isThin()) {
if (other.singleEntry())
remove(other.singleEntry());
return;
}
genericFilter([&] (T value) { return !other.containsOutOfLine(value); });
}
bool isSubsetOf(const TinyPtrSet& other) const
{
if (isThin()) {
if (!singleEntry())
return true;
return other.contains(singleEntry());
}
if (other.isThin()) {
if (!other.singleEntry())
return false;
OutOfLineList* list = this->list();
if (list->m_length >= 2)
return false;
if (list->list()[0] == other.singleEntry())
return true;
return false;
}
OutOfLineList* list = this->list();
for (unsigned i = 0; i < list->m_length; ++i) {
if (!other.containsOutOfLine(list->list()[i]))
return false;
}
return true;
}
bool isSupersetOf(const TinyPtrSet& other) const
{
return other.isSubsetOf(*this);
}
bool overlaps(const TinyPtrSet& other) const
{
if (isThin()) {
if (!singleEntry())
return false;
return other.contains(singleEntry());
}
if (other.isThin()) {
if (!other.singleEntry())
return false;
return containsOutOfLine(other.singleEntry());
}
OutOfLineList* list = this->list();
for (unsigned i = 0; i < list->m_length; ++i) {
if (other.containsOutOfLine(list->list()[i]))
return true;
}
return false;
}
size_t size() const
{
if (isThin())
return !!singleEntry();
return list()->m_length;
}
T at(size_t i) const
{
if (isThin()) {
ASSERT(!i);
ASSERT(singleEntry());
return singleEntry();
}
ASSERT(i < list()->m_length);
return list()->list()[i];
}
T operator[](size_t i) const { return at(i); }
T last() const
{
if (isThin()) {
ASSERT(singleEntry());
return singleEntry();
}
return list()->list()[list()->m_length - 1];
}
class iterator {
public:
iterator()
: m_set(nullptr)
, m_index(0)
{
}
iterator(const TinyPtrSet* set, size_t index)
: m_set(set)
, m_index(index)
{
}
T operator*() const { return m_set->at(m_index); }
iterator& operator++()
{
m_index++;
return *this;
}
bool operator==(const iterator& other) const { return m_index == other.m_index; }
private:
const TinyPtrSet* m_set;
size_t m_index;
};
iterator begin() const { return iterator(this, 0); }
iterator end() const { return iterator(this, size()); }
bool operator==(const TinyPtrSet& other) const
{
if (size() != other.size())
return false;
return isSubsetOf(other);
}
private:
friend class JSC::DFG::StructureAbstractValue;
static constexpr uintptr_t fatFlag = 1;
static constexpr uintptr_t reservedFlag = 2;
static constexpr uintptr_t flags = fatFlag | reservedFlag;
static constexpr uintptr_t reservedValue = 4;
static constexpr unsigned defaultStartingSize = 4;
NEVER_INLINE bool addOutOfLine(T value)
{
OutOfLineList* list = this->list();
for (unsigned i = 0; i < list->m_length; ++i) {
if (list->list()[i] == value)
return false;
}
if (list->m_length < list->m_capacity) {
list->list()[list->m_length++] = value;
return true;
}
OutOfLineList* newList = OutOfLineList::create(list->m_capacity * 2);
newList->m_length = list->m_length + 1;
for (unsigned i = list->m_length; i--;)
newList->list()[i] = list->list()[i];
newList->list()[list->m_length] = value;
OutOfLineList::destroy(list);
set(newList);
return true;
}
NEVER_INLINE bool mergeOtherOutOfLine(const TinyPtrSet& other)
{
OutOfLineList* list = other.list();
if (list->m_length >= 2) {
if (isThin()) {
OutOfLineList* myNewList = OutOfLineList::create(
list->m_length + !!singleEntry());
if (singleEntry()) {
myNewList->m_length = 1;
myNewList->list()[0] = singleEntry();
}
set(myNewList);
}
bool changed = false;
for (unsigned i = 0; i < list->m_length; ++i)
changed |= addOutOfLine(list->list()[i]);
return changed;
}
ASSERT(list->m_length);
return add(list->list()[0]);
}
bool containsOutOfLine(T value) const
{
OutOfLineList* list = this->list();
for (unsigned i = 0; i < list->m_length; ++i) {
if (list->list()[i] == value)
return true;
}
return false;
}
ALWAYS_INLINE void copyFrom(const TinyPtrSet& other)
{
if (other.isThin() || other.m_pointer == reservedValue) {
bool value = getReservedFlag();
m_pointer = other.m_pointer;
setReservedFlag(value);
return;
}
copyFromOutOfLine(other);
}
NEVER_INLINE void copyFromOutOfLine(const TinyPtrSet& other)
{
ASSERT(!other.isThin() && other.m_pointer != reservedValue);
OutOfLineList* otherList = other.list();
OutOfLineList* myList = OutOfLineList::create(otherList->m_length);
myList->m_length = otherList->m_length;
for (unsigned i = otherList->m_length; i--;)
myList->list()[i] = otherList->list()[i];
set(myList);
}
class OutOfLineList {
public:
static OutOfLineList* create(unsigned capacity)
{
return new (NotNull, fastMalloc(sizeof(OutOfLineList) + capacity * sizeof(T))) OutOfLineList(0, capacity);
}
static void destroy(OutOfLineList* list)
{
fastFree(list);
}
T* list() { return std::bit_cast<T*>(this + 1); }
OutOfLineList(unsigned length, unsigned capacity)
: m_length(length)
, m_capacity(capacity)
{
}
unsigned m_length;
unsigned m_capacity;
};
ALWAYS_INLINE void deleteListIfNecessary()
{
if (!isThin()) {
ASSERT(m_pointer != reservedValue);
OutOfLineList::destroy(list());
}
}
bool isThin() const { return !(m_pointer & fatFlag); }
void* pointer() const
{
return std::bit_cast<void*>(m_pointer & ~flags);
}
T singleEntry() const
{
ASSERT(isThin());
return std::bit_cast<T>(pointer());
}
OutOfLineList* list() const
{
ASSERT(!isThin());
return static_cast<OutOfLineList*>(pointer());
}
void set(T value)
{
set(std::bit_cast<uintptr_t>(value), true);
}
void set(OutOfLineList* list)
{
set(std::bit_cast<uintptr_t>(list), false);
}
void setEmpty()
{
set(0, true);
}
void set(uintptr_t pointer, bool singleEntry)
{
m_pointer = pointer | (singleEntry ? 0 : fatFlag) | (m_pointer & reservedFlag);
}
bool getReservedFlag() const { return m_pointer & reservedFlag; }
void setReservedFlag(bool value)
{
if (value)
m_pointer |= reservedFlag;
else
m_pointer &= ~reservedFlag;
}
uintptr_t m_pointer;
};
} // namespace WTF
using WTF::TinyPtrSet;
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
|