#ifndef PODArena_h
#define PODArena_h
#include <stdint.h>
#include "wtf/Assertions.h"
#include "wtf/FastMalloc.h"
#include "wtf/Noncopyable.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/RefCounted.h"
#include "wtf/Vector.h"
namespace WebCore {
class PODArena FINAL : public RefCounted<PODArena> {
public:
class Allocator : public RefCounted<Allocator> {
public:
virtual void* allocate(size_t size) = 0;
virtual void free(void* ptr) = 0;
protected:
virtual ~Allocator() { }
friend class WTF::RefCounted<Allocator>;
};
class FastMallocAllocator : public Allocator {
public:
static PassRefPtr<FastMallocAllocator> create()
{
return adoptRef(new FastMallocAllocator);
}
virtual void* allocate(size_t size) OVERRIDE { return fastMalloc(size); }
virtual void free(void* ptr) OVERRIDE { fastFree(ptr); }
protected:
FastMallocAllocator() { }
};
static PassRefPtr<PODArena> create()
{
return adoptRef(new PODArena);
}
static PassRefPtr<PODArena> create(PassRefPtr<Allocator> allocator)
{
return adoptRef(new PODArena(allocator));
}
template<class T> T* allocateObject()
{
return new (allocateBase<T>()) T();
}
template<class T, class Argument1Type> T* allocateObject(const Argument1Type& argument1)
{
return new (allocateBase<T>()) T(argument1);
}
enum {
DefaultChunkSize = 16384
};
protected:
friend class WTF::RefCounted<PODArena>;
PODArena()
: m_allocator(FastMallocAllocator::create())
, m_current(0)
, m_currentChunkSize(DefaultChunkSize) { }
explicit PODArena(PassRefPtr<Allocator> allocator)
: m_allocator(allocator)
, m_current(0)
, m_currentChunkSize(DefaultChunkSize) { }
template <class T> static size_t minAlignment()
{
return WTF_ALIGN_OF(T);
}
template<class T> void* allocateBase()
{
void* ptr = 0;
size_t roundedSize = roundUp(sizeof(T), minAlignment<T>());
if (m_current)
ptr = m_current->allocate(roundedSize);
if (!ptr) {
if (roundedSize > m_currentChunkSize)
m_currentChunkSize = roundedSize;
m_chunks.append(adoptPtr(new Chunk(m_allocator.get(), m_currentChunkSize)));
m_current = m_chunks.last().get();
ptr = m_current->allocate(roundedSize);
}
return ptr;
}
size_t roundUp(size_t size, size_t alignment)
{
ASSERT(!(alignment % 2));
return (size + alignment - 1) & ~(alignment - 1);
}
class Chunk FINAL {
WTF_MAKE_NONCOPYABLE(Chunk);
public:
Chunk(Allocator* allocator, size_t size)
: m_allocator(allocator)
, m_size(size)
, m_currentOffset(0)
{
m_base = static_cast<uint8_t*>(m_allocator->allocate(size));
}
~Chunk()
{
m_allocator->free(m_base);
}
void* allocate(size_t size)
{
if (m_currentOffset + size < m_currentOffset)
return 0;
if (m_currentOffset + size > m_size)
return 0;
void* result = m_base + m_currentOffset;
m_currentOffset += size;
return result;
}
protected:
Allocator* m_allocator;
uint8_t* m_base;
size_t m_size;
size_t m_currentOffset;
};
RefPtr<Allocator> m_allocator;
Chunk* m_current;
size_t m_currentChunkSize;
Vector<OwnPtr<Chunk> > m_chunks;
};
}
#endif