Bug 1883861 - Part 1: Move visitMemoryBarrier into the common CodeGenerator file...
[gecko.git] / layout / style / CachedInheritingStyles.h
blobcfe86594ca500cb829c5714ddedf21805c1dbd28
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_CachedInheritingStyles_h
8 #define mozilla_CachedInheritingStyles_h
10 #include "nsAtom.h"
11 #include "nsCOMPtr.h"
12 #include "nsTArray.h"
14 class nsWindowSizes;
16 namespace mozilla {
18 enum class PseudoStyleType : uint8_t;
19 class ComputedStyle;
21 // Cache of anonymous box and lazy pseudo styles that inherit from a given
22 // style.
24 // To minimize memory footprint, the cache is word-sized with a tagged pointer
25 // If there is only one entry, it's stored inline. If there are more, they're
26 // stored in an out-of-line buffer. See bug 1429126 comment 0 and comment 1 for
27 // the measurements and rationale that influenced the design.
28 class CachedInheritingStyles {
29 public:
30 void Insert(ComputedStyle* aStyle);
31 ComputedStyle* Lookup(PseudoStyleType) const;
33 CachedInheritingStyles() : mBits(0) {}
34 ~CachedInheritingStyles() {
35 if (IsIndirect()) {
36 delete AsIndirect();
37 } else if (!IsEmpty()) {
38 RefPtr<ComputedStyle> ref = dont_AddRef(AsDirect());
42 void AddSizeOfIncludingThis(nsWindowSizes& aSizes, size_t* aCVsSize) const;
44 private:
45 // See bug 1429126 comment 1 for the choice of four here.
46 typedef AutoTArray<RefPtr<ComputedStyle>, 4> IndirectCache;
48 bool IsEmpty() const { return !mBits; }
49 bool IsIndirect() const { return (mBits & 1); }
51 ComputedStyle* AsDirect() const {
52 MOZ_ASSERT(!IsIndirect());
53 return reinterpret_cast<ComputedStyle*>(mBits);
56 IndirectCache* AsIndirect() const {
57 MOZ_ASSERT(IsIndirect());
58 return reinterpret_cast<IndirectCache*>(mBits & ~1);
61 uintptr_t mBits;
64 } // namespace mozilla
66 #endif // mozilla_CachedInheritingStyles_h