Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

89 lines (71 sloc) 2.433 kb
#ifndef RBX_SYMBOLTABLE_HPP
#define RBX_SYMBOLTABLE_HPP
#include "oop.hpp"
#include "prelude.hpp"
#include "util/thread.hpp"
#include <string>
#include <vector>
#include <tr1/unordered_map>
/* SymbolTable provides a one-to-one map between a symbol ID
* and a string. The hashing algorithm used to generate a
* hashval from a string is not perfect. For instance, the
* strings "__uint_fast64_t" and "TkIF_MOD" generate the same
* hashval. SymbolTable handles this by first looking up a
* symbol ID for a string by the string's hashval and then
* comparing the candidate string with the stored string that
* corresponds to the possible symbol ID.
*
* When a symbol ID is generated, the string corresponding to
* that symbol ID is stored in a vector. The symbol ID is the
* index of that string in the vector. The symbol ID becomes
* a Symbol* by adding the tag value for symbols. (See builtin
* class Symbol::from_index and oop.hpp.)
*/
namespace rubinius {
class Array;
class String;
class Symbol;
typedef std::vector<std::string> SymbolStrings;
typedef std::vector<std::size_t> SymbolIds;
typedef std::tr1::unordered_map<hashval, SymbolIds> SymbolMap;
class SymbolTable {
public: // Types
// We encode in the symbol some information about it, mostly
// what kind of prefix it has. This lets us ask questions
// easily about it without having to get the string representation
// and perform comparisons against the char data.
enum Kind {
Normal,
Constant,
IVar,
CVar,
System
};
typedef std::vector<Kind> SymbolKinds;
private:
SymbolMap symbols;
SymbolStrings strings;
SymbolKinds kinds;
thread::SpinLock lock_;
size_t bytes_used_;
public:
size_t& bytes_used() {
return bytes_used_;
}
Symbol* lookup(std::string str);
Symbol* lookup(STATE, std::string str);
Symbol* lookup(const char* str, size_t length);
Symbol* lookup(STATE, const char* str, size_t length);
Symbol* lookup(STATE, String* str);
String* lookup_string(STATE, const Symbol* sym);
std::string& lookup_cppstring(const Symbol* sym);
std::string lookup_debug_string(const Symbol* sym);
size_t size();
Array* all_as_array(STATE);
Kind kind(STATE, const Symbol* sym);
int byte_size();
size_t add(std::string str);
Kind detect_kind(const char* str, int size);
};
};
#endif
Jump to Line
Something went wrong with that request. Please try again.