Skip to content

Commit

Permalink
CheckedPtr should require FastMalloc, Part 1
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=271950

Reviewed by Ryosuke Niwa.

This will enable zombie-mode CheckedPtr.

It's also just good performance hygiene.

* Source/WTF/wtf/CheckedPtr.h:
(WTF::CheckedPtr::refIfNotNull):
(WTF::CheckedPtr::derefIfNotNull): Use static_assert to verify use of FastMalloc.
This is a good spot because it provides a complete type definition for T and
it's our lowest level funnel. (Not enabled yet because there are a few complex
cases left to work through.)

(WTF::CheckedPtr::operator UnspecifiedBoolType const): Deleted.
(WTF::CheckedPtr::unspecifiedBoolTypeInstance const): Deleted. Took the
opportunity to remove these. 'explicit operator bool()' obsoletes them.

* Source/WTF/wtf/FastMalloc.h: Capitalized our type name since that's WebKit
style. Renamed to WTFIsFastAllocated to match the macro WTF_MAKE_FAST_ALLOCATED.

* Source/WTF/wtf/StdLibExtras.h:
(WTF::makeUnique):
(WTF::makeUniqueWithoutRefCountedCheck):
(WTF::makeUniqueWithoutFastMallocCheck): Adopted new typename for
WTFIsFastAllocated and added a little more instruction to the error message.

* Source/WTF/wtf/UniqueRef.h:
(WTF::makeUniqueRef): Ditto

* Source/bmalloc/bmalloc/IsoHeap.h:
* Source/bmalloc/bmalloc/IsoHeapInlines.h:
* Source/bmalloc/bmalloc/TZoneHeap.h:
* Source/bmalloc/bmalloc/TZoneHeapInlines.h: Ditto

* Source/WebCore/platform/ScrollableArea.cpp:
* Source/WebCore/platform/graphics/TextRun.cpp:
* Source/WebCore/rendering/LegacyRootInlineBox.cpp:
* Source/WebCore/rendering/MarkedText.h:
* Source/WebCore/rendering/RegionContext.h: Adopt FastMalloc.

* Source/WebCore/rendering/RenderLayerCompositor.cpp:
(WebCore::RenderLayerCompositor::adjustOverflowScrollbarContainerLayers): Fixed
a type safety bug uncovered by removing the UnspecifiedBoolType operator. This
code certainly did something before, but probably not what the author expected! :P

* Source/WebCore/rendering/RenderObject.cpp:
* Source/WebKit/Shared/WebPreferencesStore.h: Adopt FastMalloc.

Canonical link: https://commits.webkit.org/276869@main
  • Loading branch information
geoffreygaren committed Apr 1, 2024
1 parent 4563c83 commit 1bcc994
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 20 deletions.
12 changes: 6 additions & 6 deletions Source/WTF/wtf/CheckedPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ class CheckedPtr {

bool isHashTableDeletedValue() const { return PtrTraits::isHashTableDeletedValue(m_ptr); }

// This conversion operator allows implicit conversion to bool but not to other integer types.
using UnspecifiedBoolType = void (CheckedPtr::*)() const;
operator UnspecifiedBoolType() const { return m_ptr ? &CheckedPtr::unspecifiedBoolTypeInstance : nullptr; }

ALWAYS_INLINE explicit operator bool() const { return PtrTraits::unwrap(m_ptr); }

ALWAYS_INLINE T* get() const { return PtrTraits::unwrap(m_ptr); }
Expand Down Expand Up @@ -233,16 +229,20 @@ class CheckedPtr {
private:
template<typename OtherType, typename OtherPtrTraits> friend class CheckedPtr;

void unspecifiedBoolTypeInstance() const { }

ALWAYS_INLINE void refIfNotNull()
{
// FIXME: Enable this assertion once all clients conform.
// static_assert(std::is_same<typename T::WTFIsFastAllocated, int>::value, "Objects that use CheckedPtr must use FastMalloc (WTF_MAKE_FAST_ALLOCATED)");

if (T* ptr = PtrTraits::unwrap(m_ptr); LIKELY(ptr))
ptr->incrementPtrCount();
}

ALWAYS_INLINE void derefIfNotNull()
{
// FIXME: Enable this assertion once all clients conform.
// static_assert(std::is_same<typename T::WTFIsFastAllocated, int>::value, "Objects that use CheckedPtr must use FastMalloc (WTF_MAKE_FAST_ALLOCATED)");

if (T* ptr = PtrTraits::unwrap(m_ptr); LIKELY(ptr))
ptr->decrementPtrCount();
}
Expand Down
6 changes: 3 additions & 3 deletions Source/WTF/wtf/FastMalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ using WTF::fastCompactAlignedMalloc;
{ \
::WTF::fastFree(p); \
} \
using webkitFastMalloced = int; \
using WTFIsFastAllocated = int; \

#define WTF_MAKE_FAST_COMPACT_ALLOCATED_IMPL \
WTF_ALLOW_COMPACT_POINTERS_IMPL; \
Expand Down Expand Up @@ -483,7 +483,7 @@ using WTF::fastCompactAlignedMalloc;
{ \
::WTF::fastFree(p); \
} \
using webkitFastMalloced = int; \
using WTFIsFastAllocated = int; \

// FIXME: WTF_MAKE_FAST_ALLOCATED should take class name so that we can create malloc_zone per this macro.
// https://bugs.webkit.org/show_bug.cgi?id=205702
Expand Down Expand Up @@ -541,7 +541,7 @@ using __thisIsHereToForceASemicolonAfterThisMacro UNUSED_TYPE_ALIAS = int
{ \
classname##Malloc::free(p); \
} \
using webkitFastMalloced = int; \
using WTFIsFastAllocated = int; \

#define WTF_MAKE_FAST_ALLOCATED_WITH_HEAP_IDENTIFIER(classname) \
public: \
Expand Down
8 changes: 4 additions & 4 deletions Source/WTF/wtf/StdLibExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -597,22 +597,22 @@ template<typename T> class TypeHasRefMemberFunction {
template<class T, class... Args>
ALWAYS_INLINE decltype(auto) makeUnique(Args&&... args)
{
static_assert(std::is_same<typename T::webkitFastMalloced, int>::value, "T is FastMalloced");
static_assert(!TypeHasRefMemberFunction<T>::value, "T should not be refcounted");
static_assert(std::is_same<typename T::WTFIsFastAllocated, int>::value, "T sould use FastMalloc (WTF_MAKE_FAST_ALLOCATED)");
static_assert(!TypeHasRefMemberFunction<T>::value, "T should not be RefCounted");
return std::make_unique<T>(std::forward<Args>(args)...);
}

template<class T, class... Args>
ALWAYS_INLINE decltype(auto) makeUniqueWithoutRefCountedCheck(Args&&... args)
{
static_assert(std::is_same<typename T::webkitFastMalloced, int>::value, "T is FastMalloced");
static_assert(std::is_same<typename T::WTFIsFastAllocated, int>::value, "T sould use FastMalloc (WTF_MAKE_FAST_ALLOCATED)");
return std::make_unique<T>(std::forward<Args>(args)...);
}

template<class T, class... Args>
ALWAYS_INLINE decltype(auto) makeUniqueWithoutFastMallocCheck(Args&&... args)
{
static_assert(!TypeHasRefMemberFunction<T>::value, "T should not be refcounted");
static_assert(!TypeHasRefMemberFunction<T>::value, "T should not be RefCounted");
return std::make_unique<T>(std::forward<Args>(args)...);
}

Expand Down
2 changes: 1 addition & 1 deletion Source/WTF/wtf/UniqueRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ UniqueRef<T> makeUniqueRefWithoutFastMallocCheck(Args&&... args)
template<typename T, class... Args>
UniqueRef<T> makeUniqueRef(Args&&... args)
{
static_assert(std::is_same<typename T::webkitFastMalloced, int>::value, "T is FastMalloced");
static_assert(std::is_same<typename T::WTFIsFastAllocated, int>::value, "T sould use FastMalloc (WTF_MAKE_FAST_ALLOCATED)");
return makeUniqueRefWithoutFastMallocCheck<T>(std::forward<Args>(args)...);
}

Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/platform/ScrollableArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
namespace WebCore {

struct SameSizeAsScrollableArea : public CanMakeWeakPtr<SameSizeAsScrollableArea>, public CanMakeCheckedPtr {
WTF_MAKE_STRUCT_FAST_ALLOCATED;

~SameSizeAsScrollableArea() { }
SameSizeAsScrollableArea() { }
void* pointer[3];
Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/platform/graphics/TextRun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
namespace WebCore {

struct ExpectedTextRunSize : public CanMakeCheckedPtr {
WTF_MAKE_STRUCT_FAST_ALLOCATED;

String text;
TabSize tabSize;
float float1;
Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/rendering/LegacyRootInlineBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(LegacyRootInlineBox);

struct SameSizeAsLegacyRootInlineBox : LegacyInlineFlowBox, CanMakeWeakPtr<LegacyRootInlineBox>, CanMakeCheckedPtr {
WTF_MAKE_STRUCT_FAST_ALLOCATED;

int layoutUnits[4];
};

Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/rendering/MarkedText.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class RenderedDocumentMarker;
struct TextBoxSelectableRange;

struct MarkedText : public CanMakeCheckedPtr {
WTF_MAKE_STRUCT_FAST_ALLOCATED;

// Sorted by paint order
enum class Type : uint8_t {
Unmarked,
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/rendering/RegionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace WebCore {
class Path;

class RegionContext : public CanMakeCheckedPtr {
WTF_MAKE_FAST_ALLOCATED;
public:
virtual ~RegionContext() = default;

Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/rendering/RenderLayerCompositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ void RenderLayerCompositor::adjustOverflowScrollbarContainerLayers(RenderLayer&

if (lastDescendantLayerIndex && scrollerLayerIndex) {
auto insertionIndex = std::max(lastDescendantLayerIndex.value() + 1, scrollerLayerIndex.value() + 1);
LOG_WITH_STREAM(Compositing, stream << "Moving overflow controls layer for " << overflowScrollingLayer << " to appear after " << lastContainedDescendant);
LOG_WITH_STREAM(Compositing, stream << "Moving overflow controls layer for " << *overflowScrollingLayer << " to appear after " << *lastContainedDescendant);
layerChildren.insert(insertionIndex, *overflowContainerLayer);
}

Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/rendering/RenderObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ RenderObject::SetLayoutNeededForbiddenScope::~SetLayoutNeededForbiddenScope()
#endif

struct SameSizeAsRenderObject : public CachedImageClient, public CanMakeCheckedPtr {
WTF_MAKE_STRUCT_FAST_ALLOCATED;

virtual ~SameSizeAsRenderObject() = default; // Allocate vtable pointer.
#if ASSERT_ENABLED
unsigned m_debugBitfields : 2;
Expand Down
2 changes: 2 additions & 0 deletions Source/WebKit/Shared/WebPreferencesStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
namespace WebKit {

struct WebPreferencesStore : public CanMakeCheckedPtr {
WTF_MAKE_STRUCT_FAST_ALLOCATED;

using Value = std::variant<String, bool, uint32_t, double>;
using ValueMap = MemoryCompactRobinHoodHashMap<String, Value>;

Expand Down
4 changes: 2 additions & 2 deletions Source/bmalloc/bmalloc/IsoHeap.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public: \
\
exportMacro static void freeAfterDestruction(void*); \
\
using webkitFastMalloced = int; \
using WTFIsFastAllocated = int; \
private: \
using __makeBisoMallocedMacroSemicolonifier BUNUSED_TYPE_ALIAS = int

Expand All @@ -218,7 +218,7 @@ public: \
\
exportMacro static void freeAfterDestruction(void*); \
\
using webkitFastMalloced = int; \
using WTFIsFastAllocated = int; \
private: \
using __makeBisoMallocedMacroSemicolonifier BUNUSED_TYPE_ALIAS = int

Expand Down
2 changes: 1 addition & 1 deletion Source/bmalloc/bmalloc/IsoHeapInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public: \
bisoHeap().deallocate(p); \
} \
\
using webkitFastMalloced = int; \
using WTFIsFastAllocated = int; \
private: \
using __makeBisoMallocedInlineMacroSemicolonifier BUNUSED_TYPE_ALIAS = int

Expand Down
2 changes: 1 addition & 1 deletion Source/bmalloc/bmalloc/TZoneHeap.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public: \
} \
exportMacro static void freeAfterDestruction(void*); \
\
using webkitFastMalloced = int; \
using WTFIsFastAllocated = int; \
private: \
using __makeTZoneMallocedMacroSemicolonifier BUNUSED_TYPE_ALIAS = int

Expand Down
2 changes: 1 addition & 1 deletion Source/bmalloc/bmalloc/TZoneHeapInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public: \
btzoneHeap().deallocate(p); \
} \
\
using webkitFastMalloced = int; \
using WTFIsFastAllocated = int; \
private: \
using __makeBtzoneMallocedInlineMacroSemicolonifier BUNUSED_TYPE_ALIAS = int

Expand Down

0 comments on commit 1bcc994

Please sign in to comment.