Exemple #1
0
StringData *StringData::GetStaticString(const StringData *str) {
  if (UNLIKELY(!s_stringDataMap)) {
    StringDataMap::Config config;
    config.growthFactor = 1;
    s_stringDataMap =
      new StringDataMap(RuntimeOption::EvalInitialStaticStringTableSize,
                        config);
  }
  if (str->isStatic()) {
    assert(checkStaticStr(str));
    return const_cast<StringData*>(str);
  }
  StringDataMap::const_iterator it = s_stringDataMap->find(str);
  if (it != s_stringDataMap->end()) {
    return const_cast<StringData*>(it->first);
  }
  // Lookup failed, so do the hard work of creating a StringData with its own
  // copy of the key string, so that the atomic insert() has a permanent key.
  StringData *sd = (StringData*)Util::low_malloc(sizeof(StringData));
  new (sd) StringData(str->data(), str->size(), CopyMalloc);
  sd->setStatic();
  auto pair = s_stringDataMap->insert(sd, 0);
  if (!pair.second) {
    sd->~StringData();
    Util::low_free(sd);
  }
  assert(pair.first->first != nullptr);
  return const_cast<StringData*>(pair.first->first);
}