1/*
2 * Copyright (C) 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#pragma once
27
28#include "AllocationFailureMode.h"
29#include "FreeList.h"
30#include "MarkedBlock.h"
31#include <wtf/Noncopyable.h>
32
33namespace JSC {
34
35class BlockDirectory;
36class GCDeferralContext;
37class Heap;
38
39class LocalAllocator : public BasicRawSentinelNode<LocalAllocator> {
40 WTF_MAKE_NONCOPYABLE(LocalAllocator);
41
42public:
43 LocalAllocator(BlockDirectory*);
44 ~LocalAllocator();
45
46 void* allocate(Heap&, GCDeferralContext*, AllocationFailureMode);
47
48 unsigned cellSize() const { return m_freeList.cellSize(); }
49
50 void stopAllocating();
51 void prepareForAllocation();
52 void resumeAllocating();
53 void stopAllocatingForGood();
54
55 static ptrdiff_t offsetOfFreeList();
56 static ptrdiff_t offsetOfCellSize();
57
58 bool isFreeListedCell(const void*) const;
59
60private:
61 friend class BlockDirectory;
62
63 void reset();
64 JS_EXPORT_PRIVATE void* allocateSlowCase(Heap&, GCDeferralContext*, AllocationFailureMode);
65 void didConsumeFreeList();
66 void* tryAllocateWithoutCollecting();
67 void* tryAllocateIn(MarkedBlock::Handle*);
68 void* allocateIn(MarkedBlock::Handle*);
69 ALWAYS_INLINE void doTestCollectionsIfNeeded(Heap&, GCDeferralContext*);
70
71 BlockDirectory* m_directory;
72 FreeList m_freeList;
73
74 MarkedBlock::Handle* m_currentBlock { nullptr };
75 MarkedBlock::Handle* m_lastActiveBlock { nullptr };
76
77 // After you do something to a block based on one of these cursors, you clear the bit in the
78 // corresponding bitvector and leave the cursor where it was.
79 size_t m_allocationCursor { 0 }; // Points to the next block that is a candidate for allocation.
80};
81
82inline ptrdiff_t LocalAllocator::offsetOfFreeList()
83{
84 return OBJECT_OFFSETOF(LocalAllocator, m_freeList);
85}
86
87inline ptrdiff_t LocalAllocator::offsetOfCellSize()
88{
89 return OBJECT_OFFSETOF(LocalAllocator, m_freeList) + FreeList::offsetOfCellSize();
90}
91
92} // namespace JSC
93
94