scc  2022.4.0
SystemC components library
tlm_extensions.h
1 /*******************************************************************************
2  * Copyright 2018-2022 MINRES Technologies GmbH
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 #ifndef SC_COMPONENTS_INCL_TLM_TLM_EXTENSIONS_H_
18 #define SC_COMPONENTS_INCL_TLM_TLM_EXTENSIONS_H_
19 
20 #ifdef CWR_SYSTEMC
21 #include <tlm_h/tlm_generic_payload/tlm_gp.h>
22 #else
23 #include <tlm_core/tlm_2/tlm_generic_payload/tlm_gp.h>
24 #endif
25 
27 namespace tlm {
29 namespace scc {
30 
31 template <typename T> struct tlm_unmanaged_extension : public tlm_extension<T> {
32  using type = T;
33 
34  tlm_extension_base* clone() const override { return new type(static_cast<const T&>(*this)); }
35 
36  void copy_from(tlm_extension_base const& other) override { this->operator=(static_cast<const type&>(other)); }
37 
38 protected:
40 };
41 
42 template <typename T> struct tlm_managed_extension {
43 
44  using type = T;
45 
46  template <typename... Args> static type* allocate(Args&&... args) {
47  auto* ret = new(pool::allocate()) type(std::forward<Args>(args)...);
48  ret->is_pooled = true;
49  return ret;
50  }
51 
52  static type* allocate() {
53  auto* ret = new(pool::allocate()) type();
54  ret->is_pooled = true;
55  return ret;
56  }
57 
58  tlm_extension_base* clone() const {
59  return allocate(); // Maybe static_cast<const T&>(*this)
60  }
61 
62  void copy_from(tlm_extension_base const& other) { this->operator=(static_cast<const type&>(other)); }
63 
64  void free() {
65  if(is_pooled) {
66  this->~type();
67  pool::dealllocate(this);
68  } else {
69  delete this;
70  }
71  }
72  struct pool {
73  static void* allocate() {
74  if(free_list.size() > 0) {
75  auto ret = free_list.back();
76  free_list.pop_back();
77  return ret;
78  } else
79  return calloc(1, sizeof(type));
80  }
81 
82  static void dealllocate(void* p) { free_list.push_back(p); }
83 
84  private:
85  static std::vector<void*> free_list;
86  };
87 
88 protected:
89  tlm_managed_extension() = default;
91  tlm_managed_extension& operator=(const tlm_managed_extension& other) { return *this; }
92 
93 private:
94  bool is_pooled{false};
95 };
96 
97 struct data_buffer : public tlm::tlm_extension<data_buffer> {
98 
99  tlm_extension_base* clone() const override {
100  data_buffer* ext = new data_buffer;
101  return ext;
102  }
103  void copy_from(tlm_extension_base const& from) override { buffer_ = static_cast<data_buffer const&>(from).buffer_; }
104 
105  void set_size(uint32_t size) { buffer_.resize(size); }
106  unsigned char* get_buf_ptr() { return buffer_.data(); }
107 
108 private:
109  std::vector<unsigned char> buffer_;
110 };
111 
112 } // namespace scc
113 } // namespace tlm
114 #endif /* SC_COMPONENTS_INCL_TLM_TLM_EXTENSIONS_H_ */
SCC SystemC utilities.
SystemC TLM.