asherkin@263: /* vim: set ts=4 sw=4 tw=99 et: asherkin@263: * asherkin@263: * Copyright (C) 2012 David Anderson asherkin@263: * asherkin@263: * This file is part of SourcePawn. asherkin@263: * asherkin@263: * SourcePawn is free software: you can redistribute it and/or modify it under asherkin@263: * the terms of the GNU General Public License as published by the Free asherkin@263: * Software Foundation, either version 3 of the License, or (at your option) asherkin@263: * any later version. asherkin@263: * asherkin@263: * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY asherkin@263: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS asherkin@263: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. asherkin@263: * asherkin@263: * You should have received a copy of the GNU General Public License along with asherkin@263: * SourcePawn. If not, see http://www.gnu.org/licenses/. asherkin@263: */ asherkin@263: #include "Zone.h" asherkin@263: #include "NativeTable.h" asherkin@263: #include "StringPool.h" asherkin@263: #include "Heap-inl.h" asherkin@263: asherkin@263: using namespace ke; asherkin@263: asherkin@263: NativeTable::NativeTable(Zone *zone) asherkin@263: : zone_(zone), asherkin@263: nativeMap_(ZoneAllocatorPolicy()) asherkin@263: { asherkin@263: } asherkin@263: asherkin@263: NativeTable::~NativeTable() asherkin@263: { asherkin@263: for (size_t i = 0; i < natives_.length(); i++) asherkin@263: delete natives_[i]; asherkin@263: } asherkin@263: asherkin@263: bool asherkin@263: NativeTable::initialize() asherkin@263: { asherkin@263: return nativeMap_.init(16); asherkin@263: } asherkin@263: asherkin@263: bool asherkin@263: NativeTable::add(Handle name, size_t *indexp) asherkin@263: { asherkin@263: // :TODO: assert no GC asherkin@263: CharsAndLength key(name->chars(), name->length()); asherkin@263: NativeMap::Insert p = nativeMap_.findForAdd(key); asherkin@263: if (p.found()) { asherkin@263: *indexp = (*p)->index; asherkin@263: return true; asherkin@263: } asherkin@263: asherkin@263: NativeInfo *info = new NativeInfo(name, NULL, natives_.length()); asherkin@263: if (!info || !natives_.append(info)) asherkin@263: return false; asherkin@263: asherkin@263: if (!nativeMap_.add(p, info)) asherkin@263: return false; asherkin@263: asherkin@263: *indexp = info->index; asherkin@263: asherkin@263: return true; asherkin@263: } asherkin@263: asherkin@263: bool asherkin@263: NativeTable::bind(const char *name, void *address) asherkin@263: { asherkin@263: CharsAndLength key(name, strlen(name)); asherkin@263: NativeMap::Insert p = nativeMap_.findForAdd(key); asherkin@263: if (p.found()) { asherkin@263: (*p)->address = address; asherkin@263: return true; asherkin@263: } asherkin@263: asherkin@263: Local str(zone_, zone_->symbols()->add(key.str(), key.length())); asherkin@263: if (!str) asherkin@263: return false; asherkin@263: asherkin@263: // :TODO: assert no GC asherkin@263: NativeInfo *info = new NativeInfo(str, address, natives_.length()); asherkin@263: if (!info || !natives_.append(info)) asherkin@263: return false; asherkin@263: asherkin@263: if (!nativeMap_.add(p, info)) asherkin@263: return false; asherkin@263: asherkin@263: return true; asherkin@263: } asherkin@263: asherkin@263: void asherkin@263: NativeTable::accept(VirtualObjectVisitor *visitor) asherkin@263: { asherkin@263: for (size_t i = 0; i < natives_.length(); i++) asherkin@263: visitor->visit(natives_[i]->name.address()); asherkin@263: }