Home | History | Annotate | Download | only in platform
      1 /*
      2  * Copyright (C) 2011 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 COMPUTER, 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 COMPUTER, 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 PODFreeListArena_h
     27 #define PODFreeListArena_h
     28 
     29 #include "platform/PODArena.h"
     30 
     31 namespace blink {
     32 
     33 template <class T>
     34 class PODFreeListArena : public RefCounted<PODFreeListArena<T> > {
     35 public:
     36     static PassRefPtr<PODFreeListArena> create()
     37     {
     38         return adoptRef(new PODFreeListArena);
     39     }
     40 
     41     // Creates a new PODFreeListArena configured with the given Allocator.
     42     static PassRefPtr<PODFreeListArena> create(PassRefPtr<PODArena::Allocator> allocator)
     43     {
     44         return adoptRef(new PODFreeListArena(allocator));
     45     }
     46 
     47     // Allocates an object from the arena.
     48     T* allocateObject()
     49     {
     50         void* ptr = allocateFromFreeList();
     51 
     52         if (ptr) {
     53             // Use placement operator new to allocate a T at this location.
     54             new(ptr) T();
     55             return static_cast<T*>(ptr);
     56         }
     57 
     58         // PODArena::allocateObject calls T's constructor.
     59         return static_cast<T*>(m_arena->allocateObject<T>());
     60     }
     61 
     62     template<class Argument1Type> T* allocateObject(const Argument1Type& argument1)
     63     {
     64         void* ptr = allocateFromFreeList();
     65 
     66         if (ptr) {
     67             // Use placement operator new to allocate a T at this location.
     68             new(ptr) T(argument1);
     69             return static_cast<T*>(ptr);
     70         }
     71 
     72         // PODArena::allocateObject calls T's constructor.
     73         return static_cast<T*>(m_arena->allocateObject<T>(argument1));
     74     }
     75 
     76     void freeObject(T* ptr)
     77     {
     78         FixedSizeMemoryChunk* oldFreeList = m_freeList;
     79 
     80         m_freeList = reinterpret_cast<FixedSizeMemoryChunk*>(ptr);
     81         m_freeList->next = oldFreeList;
     82     }
     83 
     84 private:
     85     PODFreeListArena()
     86         : m_arena(PODArena::create()), m_freeList(0) { }
     87 
     88     explicit PODFreeListArena(PassRefPtr<PODArena::Allocator> allocator)
     89         : m_arena(PODArena::create(allocator)), m_freeList(0) { }
     90 
     91     ~PODFreeListArena() { }
     92 
     93     void* allocateFromFreeList()
     94     {
     95         if (m_freeList) {
     96             void* memory = m_freeList;
     97             m_freeList = m_freeList->next;
     98             return memory;
     99         }
    100         return 0;
    101     }
    102 
    103     int getFreeListSizeForTesting() const
    104     {
    105         int total = 0;
    106         for (FixedSizeMemoryChunk* cur = m_freeList; cur; cur = cur->next) {
    107             total++;
    108         }
    109         return total;
    110     }
    111 
    112     RefPtr<PODArena> m_arena;
    113 
    114     // This free list contains pointers within every chunk that's been allocated so
    115     // far. None of the individual chunks can be freed until the arena is
    116     // destroyed.
    117     struct FixedSizeMemoryChunk {
    118         FixedSizeMemoryChunk* next;
    119     };
    120     FixedSizeMemoryChunk* m_freeList;
    121 
    122     COMPILE_ASSERT(sizeof(T) >= sizeof(FixedSizeMemoryChunk), PODFreeListArena_type_should_be_larger);
    123 
    124     friend class WTF::RefCounted<PODFreeListArena>;
    125     friend class PODFreeListArenaTest;
    126 };
    127 
    128 } // namespace blink
    129 
    130 #endif
    131