this repo has no description
at trunk 41 lines 1.2 kB view raw
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) 2#include "symbols.h" 3 4#include "runtime.h" 5#include "visitor.h" 6 7namespace py { 8 9// clang-format off 10static const char* kPredefinedSymbols[] = { 11#define DEFINE_SYMBOL(symbol) #symbol, 12 FOREACH_SYMBOL(DEFINE_SYMBOL) 13#undef DEFINE_SYMBOL 14}; 15// clang-format on 16 17Symbols::Symbols(Runtime* runtime) { 18 auto num_symbols = static_cast<uword>(SymbolId::kMaxId); 19 uword symbol_size = sizeof(*symbols_); 20 symbols_ = static_cast<RawObject*>(std::calloc(num_symbols, symbol_size)); 21 CHECK(symbols_ != nullptr, "could not allocate memory for symbol table"); 22 for (uword i = 0; i < num_symbols; i++) { 23 symbols_[i] = runtime->newStrFromCStr(kPredefinedSymbols[i]); 24 } 25} 26 27Symbols::~Symbols() { std::free(symbols_); } 28 29void Symbols::visit(PointerVisitor* visitor) { 30 for (word i = 0; i < static_cast<int>(SymbolId::kMaxId); i++) { 31 visitor->visitPointer(&symbols_[i], PointerKind::kRuntime); 32 } 33} 34 35const char* Symbols::predefinedSymbolAt(SymbolId id) { 36 int index = static_cast<int>(id); 37 DCHECK_INDEX(index, static_cast<int>(SymbolId::kMaxId)); 38 return kPredefinedSymbols[index]; 39} 40 41} // namespace py