| 1 | /* |
| 2 | * Copyright (C) 2014-2018 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #ifndef SmallPage_h |
| 27 | #define SmallPage_h |
| 28 | |
| 29 | #include "BAssert.h" |
| 30 | #include "List.h" |
| 31 | #include "Mutex.h" |
| 32 | #include "VMAllocate.h" |
| 33 | #include <mutex> |
| 34 | |
| 35 | namespace bmalloc { |
| 36 | |
| 37 | class SmallLine; |
| 38 | |
| 39 | class SmallPage : public ListNode<SmallPage> { |
| 40 | public: |
| 41 | void ref(std::unique_lock<Mutex>&); |
| 42 | bool deref(std::unique_lock<Mutex>&); |
| 43 | unsigned refCount(std::unique_lock<Mutex>&) { return m_refCount; } |
| 44 | |
| 45 | size_t sizeClass() { return m_sizeClass; } |
| 46 | void setSizeClass(size_t sizeClass) { m_sizeClass = sizeClass; } |
| 47 | |
| 48 | bool hasFreeLines(std::unique_lock<Mutex>&) const { return m_hasFreeLines; } |
| 49 | void setHasFreeLines(std::unique_lock<Mutex>&, bool hasFreeLines) { m_hasFreeLines = hasFreeLines; } |
| 50 | |
| 51 | bool hasPhysicalPages() { return m_hasPhysicalPages; } |
| 52 | void setHasPhysicalPages(bool hasPhysicalPages) { m_hasPhysicalPages = hasPhysicalPages; } |
| 53 | |
| 54 | bool usedSinceLastScavenge() { return m_usedSinceLastScavenge; } |
| 55 | void clearUsedSinceLastScavenge() { m_usedSinceLastScavenge = false; } |
| 56 | void setUsedSinceLastScavenge() { m_usedSinceLastScavenge = true; } |
| 57 | |
| 58 | SmallLine* begin(); |
| 59 | |
| 60 | unsigned char slide() const { return m_slide; } |
| 61 | void setSlide(unsigned char slide) { m_slide = slide; } |
| 62 | |
| 63 | private: |
| 64 | unsigned char m_hasFreeLines: 1; |
| 65 | unsigned char m_hasPhysicalPages: 1; |
| 66 | unsigned char m_usedSinceLastScavenge: 1; |
| 67 | unsigned char m_refCount: 7; |
| 68 | unsigned char m_sizeClass; |
| 69 | unsigned char m_slide; |
| 70 | |
| 71 | static_assert( |
| 72 | sizeClassCount <= std::numeric_limits<decltype(m_sizeClass)>::max(), |
| 73 | "Largest size class must fit in SmallPage metadata" ); |
| 74 | }; |
| 75 | |
| 76 | using LineCache = std::array<List<SmallPage>, sizeClassCount>; |
| 77 | |
| 78 | inline void SmallPage::ref(std::unique_lock<Mutex>&) |
| 79 | { |
| 80 | BASSERT(!m_slide); |
| 81 | ++m_refCount; |
| 82 | BASSERT(m_refCount); |
| 83 | } |
| 84 | |
| 85 | inline bool SmallPage::deref(std::unique_lock<Mutex>&) |
| 86 | { |
| 87 | BASSERT(!m_slide); |
| 88 | BASSERT(m_refCount); |
| 89 | --m_refCount; |
| 90 | return !m_refCount; |
| 91 | } |
| 92 | |
| 93 | } // namespace bmalloc |
| 94 | |
| 95 | #endif // SmallPage_h |
| 96 | |