/* vim: set ts=4 sw=4 tw=99 et:
 *
 * Copyright (C) 2012 David Anderson
 *
 * This file is part of JITCraft.
 *
 * JITCraft is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 * 
 * Foobar 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * JITCraft. If not, see http://www.gnu.org/licenses/.
 */
#include "Zone.h"
#include "StringPool.h"
#include "TypeManager.h"
#include "AllocatorPolicies.h"

using namespace ke;

Zone *ke::SingleThreadedZone;

Zone::Zone()
  : heap_(this),
    types_(NULL),
    lastLocalRoot_(NULL)
{
}

Zone::~Zone()
{
    delete symbols_;
    delete types_;
}

bool
Zone::initialize()
{
    if (!stack_.initialize())
        return false;
    if (!heap_.initialize())
        return false;

    symbols_ = new StringPool(this);
    if (!symbols_ || !symbols_->initialize())
        return false;

    maps_[MapKind_Meta] = heap_.metaMap();
    if ((maps_[MapKind_Type] = allocateMap(MapKind_Type, sizeof(Map))) == NULL)
        return false;
    if ((maps_[MapKind_FixedArray] = allocateMap(MapKind_FixedArray, sizeof(Map))) == NULL)
        return false;
    if ((maps_[MapKind_ByteArray] = allocateMap(MapKind_ByteArray, sizeof(Map))) == NULL)
        return false;
    if ((maps_[MapKind_Code] = allocateMap(MapKind_Code, sizeof(Map))) == NULL)
        return false;
    if ((maps_[MapKind_Reference] = allocateMap(MapKind_Reference, sizeof(Map))) == NULL)
        return false;
    if ((maps_[MapKind_MetaArray] = allocateMap(MapKind_MetaArray, sizeof(Map))) == NULL)
        return false;
    if ((maps_[MapKind_Module] = allocateMap(MapKind_Module, sizeof(Map))) == NULL)
        return false;
    if ((maps_[MapKind_Function] = allocateMap(MapKind_Function, sizeof(Map))) == NULL)
        return false;
    if ((maps_[MapKind_String] = allocateMap(MapKind_String, sizeof(Map))) == NULL)
        return false;
    if ((maps_[MapKind_FreeRegion] = allocateMap(MapKind_FreeRegion, sizeof(Map))) == NULL)
        return false;

    types_ = new TypeManager(this);
    if (!types_ || !types_->initialize())
        return false;

    return true;
}

Map *
Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
{
    assert(maps_[parent]);
    Map *map = heap_.allocateMap(kind, bytes);
    if (!map)
        return NULL;
    map->init(maps_[parent]);
    return map;
}

void
Zone::accept(VirtualObjectVisitor *visitor)
{
    for (unsigned i = 0; i < MapKinds_Total; i++)
        visitor->visit(maps_[i].address());
    types()->accept(visitor);

    Local<Object> *local = lastLocalRoot_;
    while (local) {
        visitor->visit(local->address());
        local = local->prev();
    }

    rootScopes_.accept(visitor);

    stack_.accept(visitor);
}

String *
Zone::makeSymbol(const char *str, size_t length)
{
    return symbols_->add(str, length);
}

String *
Zone::makeSymbol(const char *str)
{
    return makeSymbol(str, strlen(str));
}

void
ZoneAllocatorPolicy::reportOutOfMemory()
{
    // :TODO: report
    assert(false);
}

void
ZoneAllocatorPolicy::reportAllocationOverflow()
{
    // :TODO: report
    assert(false);
}
