MarkedBlock.h 11.8 KB
Newer Older
1 2 3
/*
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
ggaren@apple.com's avatar
ggaren@apple.com committed
4
 *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011 Apple Inc. All rights reserved.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifndef MarkedBlock_h
#define MarkedBlock_h

#include <wtf/Bitmap.h>
26
#include <wtf/DoublyLinkedList.h>
27
#include <wtf/HashFunctions.h>
28
#include <wtf/PageAllocationAligned.h>
29
#include <wtf/StdLibExtras.h>
30

31 32 33 34 35 36 37 38 39 40 41
// Set to log state transitions of blocks.
#define HEAP_LOG_BLOCK_STATE_TRANSITIONS 0

#if HEAP_LOG_BLOCK_STATE_TRANSITIONS
#define HEAP_DEBUG_BLOCK(block) do {                                    \
        printf("%s:%d %s: block %s = %p\n",                             \
               __FILE__, __LINE__, __FUNCTION__, #block, (block));      \
    } while (false)
#else
#define HEAP_DEBUG_BLOCK(block) ((void)0)
#endif
42

43 44
namespace JSC {
    
45
    class Heap;
46
    class JSCell;
47

48 49 50
    typedef uintptr_t Bits;

    static const size_t KB = 1024;
51
    
52 53 54 55 56 57 58
    // A marked block is a page-aligned container for heap-allocated objects.
    // Objects are allocated within cells of the marked block. For a given
    // marked block, all cells have the same size. Objects smaller than the
    // cell size may be allocated in the marked block, in which case the
    // allocation suffers from internal fragmentation: wasted space whose
    // size is equal to the difference between the cell size and the object
    // size.
59

60 61
    class MarkedBlock : public DoublyLinkedListNode<MarkedBlock> {
        friend class WTF::DoublyLinkedListNode<MarkedBlock>;
62
    public:
63 64 65
        // Ensure natural alignment for native types whilst recognizing that the smallest
        // object the heap will commonly allocate is four words.
        static const size_t atomSize = 4 * sizeof(void*);
66 67 68 69 70
        static const size_t blockSize = 16 * KB;

        static const size_t atomsPerBlock = blockSize / atomSize; // ~1.5% overhead
        static const size_t ownerSetsPerBlock = 8; // ~2% overhead.

71 72
        struct FreeCell {
            FreeCell* next;
73 74 75 76 77 78 79
            
            void setNoObject()
            {
                // This relies on FreeCell not having a vtable, and the next field
                // falling exactly where a vtable would have been.
                next = 0;
            }
80 81
        };
        
82 83 84 85 86
        struct VoidFunctor {
            typedef void ReturnType;
            void returnValue() { }
        };

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        class OwnerSet {
        public:
            OwnerSet();
            
            void add(const JSCell*);
            void clear();

            size_t size();
            bool didOverflow();
            
            const JSCell** owners();

        private:
            static const size_t capacity = 5;
            unsigned char m_size;
            const JSCell* m_owners[capacity];
        };
104

105
        static MarkedBlock* create(Heap*, size_t cellSize);
106 107
        static void destroy(MarkedBlock*);

108
        static bool isAtomAligned(const void*);
109
        static MarkedBlock* blockFor(const void*);
110
        static size_t firstAtom();
111 112
        
        Heap* heap() const;
113 114 115
        
        bool inNewSpace();
        void setInNewSpace(bool);
116

117
        void* allocate();
118
        void sweep();
119
        
120 121 122 123
        // This invokes destructors on all cells that are not marked, marks
        // them, and returns a linked list of those cells.
        FreeCell* lazySweep();
        
124 125 126
        // Notify the block that destructors may have to be called again.
        void notifyMayHaveFreshFreeCells();
        
127 128
        void initForCellSize(size_t cellSize);
        
129 130 131 132 133 134
        // These should be called immediately after a block is created.
        // Blessing for fast path creates a linked list, while blessing for
        // slow path creates dummy cells.
        FreeCell* blessNewBlockForFastPath();
        void blessNewBlockForSlowPath();
        
135 136
        void reset();
        
137 138 139 140
        // This unmarks all cells on the free list, and allocates dummy JSCells
        // in their place.
        void canonicalizeBlock(FreeCell* firstFreeCell);
        
141 142 143 144
        bool isEmpty();

        void clearMarks();
        size_t markCount();
145

146 147
        size_t cellSize();

148 149
        size_t size();
        size_t capacity();
150

151 152
        bool isMarked(const void*);
        bool testAndSetMarked(const void*);
153
        bool testAndClearMarked(const void*);
154
        void setMarked(const void*);
155 156 157

        void addOldSpaceOwner(const JSCell* owner, const JSCell*);

158
        template <typename Functor> void forEachCell(Functor&);
159

160
    private:
161 162
        static const size_t blockMask = ~(blockSize - 1); // blockSize must be a power of two.
        static const size_t atomMask = ~(atomSize - 1); // atomSize must be a power of two.
163 164
        
        enum DestructorState { FreeCellsDontHaveObjects, SomeFreeCellsStillHaveObjects, AllFreeCellsHaveObjects };
165 166 167

        typedef char Atom[atomSize];

168
        MarkedBlock(const PageAllocationAligned&, Heap*, size_t cellSize);
169
        Atom* atoms();
170

171 172
        size_t atomNumber(const void*);
        size_t ownerSetNumber(const JSCell*);
173 174
        
        template<DestructorState destructorState>
175
        void callDestructor(JSCell*, void* jsFinalObjectVPtr);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        
        template<DestructorState destructorState>
        void specializedReset();
        
        template<DestructorState destructorState>
        void specializedSweep();
        
        template<DestructorState destructorState>
        MarkedBlock::FreeCell* produceFreeList();
        
        void setDestructorState(DestructorState destructorState)
        {
            m_destructorState = static_cast<int8_t>(destructorState);
        }
        
        DestructorState destructorState()
        {
            return static_cast<DestructorState>(m_destructorState);
        }
        
196
        size_t m_endAtom; // This is a fuzzy end. Always test for < m_endAtom.
197
        size_t m_atomsPerCell;
198
        WTF::Bitmap<blockSize / atomSize> m_marks;
199
        bool m_inNewSpace;
200
        int8_t m_destructorState; // use getters/setters for this, particularly since we may want to compact this (effectively log(3)/log(2)-bit) field into other fields
201
        OwnerSet m_ownerSets[ownerSetsPerBlock];
202
        PageAllocationAligned m_allocation;
203
        Heap* m_heap;
204 205
        MarkedBlock* m_prev;
        MarkedBlock* m_next;
206 207
    };

208
    inline size_t MarkedBlock::firstAtom()
209
    {
210
        return WTF::roundUpToMultipleOf<atomSize>(sizeof(MarkedBlock)) / atomSize;
211 212
    }

213
    inline MarkedBlock::Atom* MarkedBlock::atoms()
214
    {
215
        return reinterpret_cast<Atom*>(this);
216 217
    }

218
    inline bool MarkedBlock::isAtomAligned(const void* p)
219
    {
220
        return !(reinterpret_cast<Bits>(p) & ~atomMask);
221 222
    }

223
    inline MarkedBlock* MarkedBlock::blockFor(const void* p)
224
    {
225
        return reinterpret_cast<MarkedBlock*>(reinterpret_cast<Bits>(p) & blockMask);
226
    }
227

228 229 230 231
    inline Heap* MarkedBlock::heap() const
    {
        return m_heap;
    }
232

233 234 235 236 237 238 239 240 241
    inline bool MarkedBlock::inNewSpace()
    {
        return m_inNewSpace;
    }
    
    inline void MarkedBlock::setInNewSpace(bool inNewSpace)
    {
        m_inNewSpace = inNewSpace;
    }
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    
    inline void MarkedBlock::notifyMayHaveFreshFreeCells()
    {
        HEAP_DEBUG_BLOCK(this);
        
        // This is called at the beginning of GC. If this block is
        // AllFreeCellsHaveObjects, then it means that we filled up
        // the block in this collection. If it's in any other state,
        // then this collection will potentially produce new free
        // cells; new free cells always have objects. Hence if the
        // state currently claims that there are no objects in free
        // cells then we need to bump it over. Otherwise leave it be.
        // This all crucially relies on the collector canonicalizing
        // blocks before doing anything else, as canonicalizeBlocks
        // will correctly set SomeFreeCellsStillHaveObjects for
        // blocks that were only partially filled during this
        // mutation cycle.
        
        if (destructorState() == FreeCellsDontHaveObjects)
            setDestructorState(SomeFreeCellsStillHaveObjects);
    }
263

264 265
    inline bool MarkedBlock::isEmpty()
    {
266
        return m_marks.isEmpty();
267 268 269 270
    }

    inline void MarkedBlock::clearMarks()
    {
271
        m_marks.clearAll();
272 273 274 275
    }
    
    inline size_t MarkedBlock::markCount()
    {
276
        return m_marks.count();
277 278
    }

279 280 281 282 283
    inline size_t MarkedBlock::cellSize()
    {
        return m_atomsPerCell * atomSize;
    }

284 285
    inline size_t MarkedBlock::size()
    {
286
        return markCount() * cellSize();
287 288 289 290
    }

    inline size_t MarkedBlock::capacity()
    {
291
        return m_allocation.size();
292 293
    }

294
    inline size_t MarkedBlock::atomNumber(const void* p)
295
    {
296
        return (reinterpret_cast<Bits>(p) - reinterpret_cast<Bits>(this)) / atomSize;
297 298
    }

299
    inline bool MarkedBlock::isMarked(const void* p)
300
    {
301
        return m_marks.get(atomNumber(p));
302 303
    }

304
    inline bool MarkedBlock::testAndSetMarked(const void* p)
305
    {
306
        return m_marks.testAndSet(atomNumber(p));
307 308
    }

309 310 311 312 313
    inline bool MarkedBlock::testAndClearMarked(const void* p)
    {
        return m_marks.testAndClear(atomNumber(p));
    }

314
    inline void MarkedBlock::setMarked(const void* p)
315
    {
316
        m_marks.set(atomNumber(p));
317 318
    }

319
    template <typename Functor> inline void MarkedBlock::forEachCell(Functor& functor)
320
    {
321
        for (size_t i = firstAtom(); i < m_endAtom; i += m_atomsPerCell) {
322
            if (!m_marks.get(i))
323
                continue;
324
            functor(reinterpret_cast<JSCell*>(&atoms()[i]));
325 326
        }
    }
327
    
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    inline size_t MarkedBlock::ownerSetNumber(const JSCell* cell)
    {
        return (reinterpret_cast<Bits>(cell) - reinterpret_cast<Bits>(this)) * ownerSetsPerBlock / blockSize;
    }

    inline void MarkedBlock::addOldSpaceOwner(const JSCell* owner, const JSCell* cell)
    {
        OwnerSet& ownerSet = m_ownerSets[ownerSetNumber(cell)];
        ownerSet.add(owner);
    }

    inline MarkedBlock::OwnerSet::OwnerSet()
        : m_size(0)
    {
    }

    inline void MarkedBlock::OwnerSet::add(const JSCell* owner)
    {
        if (m_size < capacity) {
            m_owners[m_size++] = owner;
            return;
        }
        m_size = capacity + 1; // Signals overflow.
    }

    inline void MarkedBlock::OwnerSet::clear()
    {
        m_size = 0;
    }

    inline size_t MarkedBlock::OwnerSet::size()
    {
        return m_size;
    }

    inline bool MarkedBlock::OwnerSet::didOverflow()
    {
        return m_size > capacity;
    }

    inline const JSCell** MarkedBlock::OwnerSet::owners()
    {
        return m_owners;
    }

373 374
} // namespace JSC

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
namespace WTF {

    struct MarkedBlockHash : PtrHash<JSC::MarkedBlock*> {
        static unsigned hash(JSC::MarkedBlock* const& key)
        {
            // Aligned VM regions tend to be monotonically increasing integers,
            // which is a great hash function, but we have to remove the low bits,
            // since they're always zero, which is a terrible hash function!
            return reinterpret_cast<JSC::Bits>(key) / JSC::MarkedBlock::blockSize;
        }
    };

    template<> struct DefaultHash<JSC::MarkedBlock*> {
        typedef MarkedBlockHash Hash;
    };

} // namespace WTF

393
#endif // MarkedBlock_h