1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-02-12 19:22:37 +00:00
ton/crypto/vm/atom.cpp
birydrad 72020c04c4
celldb in-memory mode, stats for actors, perf counters, minor fix in rldp2 (#1164)
* getactorstats query for validator-engine-console

* celldb in-memory mode (--celldb-in-memory option)

* rldp2: bugfix - do not estimate speed while nothing is sent

* add simple ed25519 benchmark

* fix compilation errors of different platforms and move to c++20

* fix some warnings

* turn on TON_USE_ABSEIL for glibc 2.27 nix build

---------

Co-authored-by: birydrad <>
2024-09-23 17:34:37 +03:00

97 lines
2.7 KiB
C++

/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#include "atom.h"
namespace vm {
using td::Ref;
std::atomic<Atom*> Atom::hashtable[hashtable_size] = {};
std::atomic<int> Atom::atoms_defined{0};
std::atomic<int> Atom::anon_atoms{0};
void Atom::print_to(std::ostream& os) const {
if (name_.empty()) {
os << "atom#" << index_;
} else {
os << name_;
}
}
std::string Atom::make_name() const {
char buffer[16];
snprintf(buffer, sizeof(buffer), "atom#%d", index_);
return buffer;
}
std::ostream& operator<<(std::ostream& os, const Atom& atom) {
atom.print_to(os);
return os;
}
std::ostream& operator<<(std::ostream& os, Ref<Atom> atom_ref) {
atom_ref->print_to(os);
return os;
}
std::pair<unsigned, unsigned> Atom::compute_hash(td::Slice name) {
unsigned h1 = 1, h2 = 1;
for (std::size_t i = 0; i < name.size(); i++) {
h1 = (239 * h1 + (unsigned char)name[i]) % hashtable_size;
h2 = (17 * h2 + (unsigned char)name[i]) % (hashtable_size - 1);
}
return std::make_pair(h1, h2 + 1);
}
Ref<Atom> Atom::find(td::Slice name, bool create) {
auto hash = compute_hash(name);
while (true) {
auto& pos = hashtable[hash.first];
Atom* ptr = pos.load(std::memory_order_acquire);
if (ptr) {
if (ptr->name_as_slice() == name) {
return Ref<Atom>(ptr);
}
} else if (!create) {
return {};
} else {
Atom* p2 = new Atom(name.str(), hash.first);
Atom* p1 = nullptr;
if (pos.compare_exchange_strong(p1, p2)) {
atoms_defined.fetch_add(1, std::memory_order_relaxed);
return Ref<Atom>(p2);
}
delete p2;
CHECK(p1);
if (p1->name_as_slice() == name) {
return Ref<Atom>(p1);
}
}
hash.first += hash.second;
if (hash.first >= hashtable_size) {
hash.first -= hashtable_size;
}
}
}
Ref<Atom> Atom::anon() {
int c = anon_atoms.fetch_add(1, std::memory_order_relaxed);
return Ref<Atom>{true, "", ~c};
}
} // namespace vm