xref: /aosp_15_r20/external/bcc/src/cc/table_storage.cc (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1*387f9dfdSAndroid Build Coastguard Worker /*
2*387f9dfdSAndroid Build Coastguard Worker  * Copyright (c) 2017 VMware, Inc.
3*387f9dfdSAndroid Build Coastguard Worker  *
4*387f9dfdSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*387f9dfdSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*387f9dfdSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*387f9dfdSAndroid Build Coastguard Worker  *
8*387f9dfdSAndroid Build Coastguard Worker  * http://www.apache.org/licenses/LICENSE-2.0
9*387f9dfdSAndroid Build Coastguard Worker  *
10*387f9dfdSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*387f9dfdSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*387f9dfdSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*387f9dfdSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*387f9dfdSAndroid Build Coastguard Worker  * limitations under the License.
15*387f9dfdSAndroid Build Coastguard Worker  */
16*387f9dfdSAndroid Build Coastguard Worker 
17*387f9dfdSAndroid Build Coastguard Worker #include <unistd.h>
18*387f9dfdSAndroid Build Coastguard Worker 
19*387f9dfdSAndroid Build Coastguard Worker #include <clang/AST/Type.h>
20*387f9dfdSAndroid Build Coastguard Worker 
21*387f9dfdSAndroid Build Coastguard Worker #include "table_storage_impl.h"
22*387f9dfdSAndroid Build Coastguard Worker 
23*387f9dfdSAndroid Build Coastguard Worker namespace ebpf {
24*387f9dfdSAndroid Build Coastguard Worker 
25*387f9dfdSAndroid Build Coastguard Worker using std::move;
26*387f9dfdSAndroid Build Coastguard Worker using std::string;
27*387f9dfdSAndroid Build Coastguard Worker using std::unique_ptr;
28*387f9dfdSAndroid Build Coastguard Worker 
29*387f9dfdSAndroid Build Coastguard Worker const string Path::DELIM = "/";
30*387f9dfdSAndroid Build Coastguard Worker 
TableStorage()31*387f9dfdSAndroid Build Coastguard Worker TableStorage::TableStorage() {}
~TableStorage()32*387f9dfdSAndroid Build Coastguard Worker TableStorage::~TableStorage() {}
Init(unique_ptr<TableStorageImpl> impl)33*387f9dfdSAndroid Build Coastguard Worker void TableStorage::Init(unique_ptr<TableStorageImpl> impl) { impl_ = move(impl); }
Find(const Path & path,TableStorage::iterator & result) const34*387f9dfdSAndroid Build Coastguard Worker bool TableStorage::Find(const Path &path, TableStorage::iterator &result) const {
35*387f9dfdSAndroid Build Coastguard Worker   return impl_->Find(path.to_string(), result);
36*387f9dfdSAndroid Build Coastguard Worker }
Insert(const Path & path,TableDesc && desc)37*387f9dfdSAndroid Build Coastguard Worker bool TableStorage::Insert(const Path &path, TableDesc &&desc) {
38*387f9dfdSAndroid Build Coastguard Worker   return impl_->Insert(path.to_string(), move(desc));
39*387f9dfdSAndroid Build Coastguard Worker }
Delete(const Path & path)40*387f9dfdSAndroid Build Coastguard Worker bool TableStorage::Delete(const Path &path) { return impl_->Delete(path.to_string()); }
DeletePrefix(const Path & path)41*387f9dfdSAndroid Build Coastguard Worker size_t TableStorage::DeletePrefix(const Path &path) {
42*387f9dfdSAndroid Build Coastguard Worker   size_t i = 0;
43*387f9dfdSAndroid Build Coastguard Worker   auto it = lower_bound(path);
44*387f9dfdSAndroid Build Coastguard Worker   auto upper = upper_bound(path);
45*387f9dfdSAndroid Build Coastguard Worker   while (it != upper) {
46*387f9dfdSAndroid Build Coastguard Worker     it = impl_->erase(*it.impl_);
47*387f9dfdSAndroid Build Coastguard Worker     ++i;
48*387f9dfdSAndroid Build Coastguard Worker   }
49*387f9dfdSAndroid Build Coastguard Worker   return i;
50*387f9dfdSAndroid Build Coastguard Worker }
51*387f9dfdSAndroid Build Coastguard Worker 
AddMapTypesVisitor(unique_ptr<MapTypesVisitor> visitor)52*387f9dfdSAndroid Build Coastguard Worker void TableStorage::AddMapTypesVisitor(unique_ptr<MapTypesVisitor> visitor) {
53*387f9dfdSAndroid Build Coastguard Worker   visitors_.push_back(move(visitor));
54*387f9dfdSAndroid Build Coastguard Worker }
VisitMapType(TableDesc & desc,clang::ASTContext & C,clang::QualType key_type,clang::QualType leaf_type)55*387f9dfdSAndroid Build Coastguard Worker void TableStorage::VisitMapType(TableDesc &desc, clang::ASTContext &C, clang::QualType key_type,
56*387f9dfdSAndroid Build Coastguard Worker                                 clang::QualType leaf_type) {
57*387f9dfdSAndroid Build Coastguard Worker   for (auto &v : visitors_)
58*387f9dfdSAndroid Build Coastguard Worker     v->Visit(desc, C, key_type, leaf_type);
59*387f9dfdSAndroid Build Coastguard Worker }
60*387f9dfdSAndroid Build Coastguard Worker 
begin()61*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator TableStorage::begin() { return impl_->begin(); }
end()62*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator TableStorage::end() { return impl_->end(); }
lower_bound(const Path & p)63*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator TableStorage::lower_bound(const Path &p) {
64*387f9dfdSAndroid Build Coastguard Worker   return impl_->lower_bound(p.to_string());
65*387f9dfdSAndroid Build Coastguard Worker }
upper_bound(const Path & p)66*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator TableStorage::upper_bound(const Path &p) {
67*387f9dfdSAndroid Build Coastguard Worker   return impl_->upper_bound(p.to_string() + "\x7f");
68*387f9dfdSAndroid Build Coastguard Worker }
69*387f9dfdSAndroid Build Coastguard Worker 
70*387f9dfdSAndroid Build Coastguard Worker /// TableStorage::iterator implementation
iterator()71*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator::iterator() {}
iterator(unique_ptr<TableStorageIteratorImpl> impl)72*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator::iterator(unique_ptr<TableStorageIteratorImpl> impl) : impl_(move(impl)) {}
iterator(const iterator & that)73*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator::iterator(const iterator &that) : impl_(that.impl_->clone()) {}
~iterator()74*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator::~iterator() {}
iterator(iterator && that)75*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator::iterator(iterator &&that) { *this = move(that); }
operator =(iterator && that)76*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator &TableStorage::iterator::operator=(iterator &&that) {
77*387f9dfdSAndroid Build Coastguard Worker   impl_ = move(that.impl_);
78*387f9dfdSAndroid Build Coastguard Worker   return *this;
79*387f9dfdSAndroid Build Coastguard Worker }
80*387f9dfdSAndroid Build Coastguard Worker 
operator ++()81*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator &TableStorage::iterator::operator++() {
82*387f9dfdSAndroid Build Coastguard Worker   ++*impl_;
83*387f9dfdSAndroid Build Coastguard Worker   return *this;
84*387f9dfdSAndroid Build Coastguard Worker }
operator ++(int)85*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator TableStorage::iterator::operator++(int) {
86*387f9dfdSAndroid Build Coastguard Worker   iterator tmp(*this);
87*387f9dfdSAndroid Build Coastguard Worker   operator++();
88*387f9dfdSAndroid Build Coastguard Worker   return tmp;
89*387f9dfdSAndroid Build Coastguard Worker }
operator ==(const iterator & rhs) const90*387f9dfdSAndroid Build Coastguard Worker bool TableStorage::iterator::operator==(const iterator &rhs) const {
91*387f9dfdSAndroid Build Coastguard Worker   // assumes that the underlying pair is stored in only one place
92*387f9dfdSAndroid Build Coastguard Worker   return &**impl_ == &**rhs.impl_;
93*387f9dfdSAndroid Build Coastguard Worker }
operator !=(const iterator & rhs) const94*387f9dfdSAndroid Build Coastguard Worker bool TableStorage::iterator::operator!=(const iterator &rhs) const {
95*387f9dfdSAndroid Build Coastguard Worker   return &**impl_ != &**rhs.impl_;
96*387f9dfdSAndroid Build Coastguard Worker }
operator *() const97*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator::reference TableStorage::iterator::operator*() const { return **impl_; }
operator ->() const98*387f9dfdSAndroid Build Coastguard Worker TableStorage::iterator::pointer TableStorage::iterator::operator->() const { return &**impl_; }
99*387f9dfdSAndroid Build Coastguard Worker 
100*387f9dfdSAndroid Build Coastguard Worker }  // namespace ebpf
101