michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "frontend/ParseMaps-inl.h" michael@0: michael@0: #include "jscntxt.h" michael@0: michael@0: #include "frontend/FullParseHandler.h" michael@0: #include "frontend/SyntaxParseHandler.h" michael@0: michael@0: using namespace js; michael@0: using namespace js::frontend; michael@0: michael@0: void michael@0: ParseMapPool::checkInvariants() michael@0: { michael@0: /* michael@0: * Having all values be of the same size permits us to easily reuse the michael@0: * allocated space for each of the map types. michael@0: */ michael@0: JS_STATIC_ASSERT(sizeof(Definition *) == sizeof(jsatomid)); michael@0: JS_STATIC_ASSERT(sizeof(Definition *) == sizeof(DefinitionList)); michael@0: JS_STATIC_ASSERT(sizeof(AtomDefnMap::Entry) == sizeof(AtomIndexMap::Entry)); michael@0: JS_STATIC_ASSERT(sizeof(AtomDefnMap::Entry) == sizeof(AtomDefnListMap::Entry)); michael@0: JS_STATIC_ASSERT(sizeof(AtomMapT::Entry) == sizeof(AtomDefnListMap::Entry)); michael@0: /* Ensure that the HasTable::clear goes quickly via memset. */ michael@0: JS_STATIC_ASSERT(mozilla::IsPod::value); michael@0: JS_STATIC_ASSERT(mozilla::IsPod::value); michael@0: JS_STATIC_ASSERT(mozilla::IsPod::value); michael@0: } michael@0: michael@0: void michael@0: ParseMapPool::purgeAll() michael@0: { michael@0: for (void **it = all.begin(), **end = all.end(); it != end; ++it) michael@0: js_delete(asAtomMap(*it)); michael@0: michael@0: all.clearAndFree(); michael@0: recyclable.clearAndFree(); michael@0: } michael@0: michael@0: void * michael@0: ParseMapPool::allocateFresh() michael@0: { michael@0: size_t newAllLength = all.length() + 1; michael@0: if (!all.reserve(newAllLength) || !recyclable.reserve(newAllLength)) michael@0: return nullptr; michael@0: michael@0: AtomMapT *map = js_new(); michael@0: if (!map) michael@0: return nullptr; michael@0: michael@0: all.infallibleAppend(map); michael@0: return (void *) map; michael@0: } michael@0: michael@0: DefinitionList::Node * michael@0: DefinitionList::allocNode(ExclusiveContext *cx, LifoAlloc &alloc, uintptr_t head, Node *tail) michael@0: { michael@0: Node *result = alloc.new_(head, tail); michael@0: if (!result) michael@0: js_ReportOutOfMemory(cx); michael@0: return result; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: template michael@0: void michael@0: AtomDecls::dump() michael@0: { michael@0: for (AtomDefnListRange r = map->all(); !r.empty(); r.popFront()) { michael@0: fprintf(stderr, "atom: "); michael@0: js_DumpAtom(r.front().key()); michael@0: const DefinitionList &dlist = r.front().value(); michael@0: for (DefinitionList::Range dr = dlist.all(); !dr.empty(); dr.popFront()) { michael@0: fprintf(stderr, " defn: %p\n", (void *) dr.front()); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: DumpAtomDefnMap(const AtomDefnMapPtr &map) michael@0: { michael@0: if (map->empty()) { michael@0: fprintf(stderr, "empty\n"); michael@0: return; michael@0: } michael@0: michael@0: for (AtomDefnRange r = map->all(); !r.empty(); r.popFront()) { michael@0: fprintf(stderr, "atom: "); michael@0: js_DumpAtom(r.front().key()); michael@0: fprintf(stderr, "defn: %p\n", (void *) r.front().value().get()); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: template michael@0: bool michael@0: AtomDecls::addShadow(JSAtom *atom, typename ParseHandler::DefinitionNode defn) michael@0: { michael@0: AtomDefnListAddPtr p = map->lookupForAdd(atom); michael@0: if (!p) michael@0: return map->add(p, atom, DefinitionList(ParseHandler::definitionToBits(defn))); michael@0: michael@0: return p.value().pushFront(cx, alloc, defn); michael@0: } michael@0: michael@0: void michael@0: frontend::InitAtomMap(frontend::AtomIndexMap *indices, HeapPtrAtom *atoms) michael@0: { michael@0: if (indices->isMap()) { michael@0: typedef AtomIndexMap::WordMap WordMap; michael@0: const WordMap &wm = indices->asMap(); michael@0: for (WordMap::Range r = wm.all(); !r.empty(); r.popFront()) { michael@0: JSAtom *atom = r.front().key(); michael@0: jsatomid index = r.front().value(); michael@0: JS_ASSERT(index < indices->count()); michael@0: atoms[index].init(atom); michael@0: } michael@0: } else { michael@0: for (const AtomIndexMap::InlineElem *it = indices->asInline(), *end = indices->inlineEnd(); michael@0: it != end; ++it) { michael@0: JSAtom *atom = it->key; michael@0: if (!atom) michael@0: continue; michael@0: JS_ASSERT(it->value < indices->count()); michael@0: atoms[it->value].init(atom); michael@0: } michael@0: } michael@0: } michael@0: michael@0: template class js::frontend::AtomDecls; michael@0: template class js::frontend::AtomDecls;