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