1/*
2 * Copyright (C) 2017-2019 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#pragma once
27
28#include "Bits.h"
29#include "EligibilityResult.h"
30#include "IsoPage.h"
31#include "Vector.h"
32
33namespace bmalloc {
34
35template<typename Config> class IsoHeapImpl;
36
37class IsoDirectoryBaseBase {
38public:
39 IsoDirectoryBaseBase() { }
40 virtual ~IsoDirectoryBaseBase() { }
41
42 virtual void didDecommit(unsigned index) = 0;
43};
44
45template<typename Config>
46class IsoDirectoryBase : public IsoDirectoryBaseBase {
47public:
48 IsoDirectoryBase(IsoHeapImpl<Config>&);
49
50 IsoHeapImpl<Config>& heap() { return m_heap; }
51
52 virtual void didBecome(IsoPage<Config>*, IsoPageTrigger) = 0;
53
54protected:
55 IsoHeapImpl<Config>& m_heap;
56};
57
58template<typename Config, unsigned passedNumPages>
59class IsoDirectory : public IsoDirectoryBase<Config> {
60public:
61 static constexpr unsigned numPages = passedNumPages;
62
63 IsoDirectory(IsoHeapImpl<Config>&);
64
65 // Find the first page that is eligible for allocation and return it. May return null if there is no
66 // such thing. May allocate a new page if we have an uncommitted page.
67 EligibilityResult<Config> takeFirstEligible();
68
69 void didBecome(IsoPage<Config>*, IsoPageTrigger) override;
70
71 // This gets called from a bulk decommit function in the Scavenger, so no locks are held. This function
72 // needs to get the heap lock.
73 void didDecommit(unsigned index) override;
74
75 // Iterate over all empty and committed pages, and put them into the vector. This also records the
76 // pages as being decommitted. It's the caller's job to do the actual decommitting.
77 void scavenge(Vector<DeferredDecommit>&);
78
79 template<typename Func>
80 void forEachCommittedPage(const Func&);
81
82private:
83 void scavengePage(size_t, Vector<DeferredDecommit>&);
84
85 // NOTE: I suppose that this could be two bitvectors. But from working on the GC, I found that the
86 // number of bitvectors does not matter as much as whether or not they make intuitive sense.
87 Bits<numPages> m_eligible;
88 Bits<numPages> m_empty;
89 Bits<numPages> m_committed;
90 std::array<IsoPage<Config>*, numPages> m_pages;
91 unsigned m_firstEligibleOrDecommitted { 0 };
92};
93
94} // namespace bmalloc
95
96