scc 2025.09
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#include "tlm_gp_shared.h"
26
28namespace tlm {
30namespace scc {
38template <typename T> struct tlm_unmanaged_extension : public tlm_extension<T> {
39 using type = T;
40
41 tlm_extension_base* clone() const override { return new type(static_cast<const T&>(*this)); }
42
43 void copy_from(tlm_extension_base const& other) override { this->operator=(static_cast<const type&>(other)); }
44
45protected:
46 tlm_unmanaged_extension(){};
47};
48
55template <typename T> struct tlm_managed_extension : public tlm_extension<T> {
56
57 using type = T;
58
59 template <typename... Args> static type* allocate(Args&&... args) {
60 auto* ret = new(pool::allocate()) type(std::forward<Args>(args)...);
61 ret->is_pooled = true;
62 return ret;
63 }
64
65 static type* allocate() {
66 auto* ret = new(pool::allocate()) type();
67 ret->is_pooled = true;
68 return ret;
69 }
70
71 tlm_extension_base* clone() const {
72 return allocate(); // Maybe static_cast<const T&>(*this)
73 }
74
75 void copy_from(tlm_extension_base const& other) { this->operator=(static_cast<const type&>(other)); }
76
77 void free() {
78 if(is_pooled) {
79 this->~type();
80 pool::dealllocate(this);
81 } else {
82 delete this;
83 }
84 }
85 struct pool {
86 static void* allocate() {
87 if(free_list.size() > 0) {
88 auto ret = free_list.back();
89 free_list.pop_back();
90 return ret;
91 } else
92 return calloc(1, sizeof(type));
93 }
94
95 static void dealllocate(void* p) { free_list.push_back(p); }
96
97 private:
98 static std::vector<void*> free_list;
99 };
100
101protected:
102 tlm_managed_extension() = default;
103 tlm_managed_extension(const tlm_managed_extension&) = default;
104 tlm_managed_extension& operator=(const tlm_managed_extension& other) { return *this; }
105
106private:
107 bool is_pooled{false};
108};
109
117struct data_buffer : public tlm::tlm_extension<data_buffer> {
118
119 tlm_extension_base* clone() const override {
120 data_buffer* ext = new data_buffer;
121 return ext;
122 }
123 void copy_from(tlm_extension_base const& from) override { buffer_ = static_cast<data_buffer const&>(from).buffer_; }
124
125 void set_size(uint32_t size) { buffer_.resize(size); }
126 unsigned char* get_buf_ptr() { return buffer_.data(); }
127
128private:
129 std::vector<unsigned char> buffer_;
130};
131
138struct tlm_payload_extension : public tlm::tlm_extension<tlm_payload_extension> {
139
140 tlm_extension_base* clone() const override {
142 return ext;
143 }
144
145 void copy_from(tlm_extension_base const& from) override { gp = static_cast<tlm_payload_extension const&>(from).gp; }
146
147 tlm::scc::tlm_gp_shared_ptr gp;
148};
149
150} // namespace scc
151} // namespace tlm
152#endif /* SC_COMPONENTS_INCL_TLM_TLM_EXTENSIONS_H_ */
SCC TLM utilities.
Definition axis_tlm.h:56
SystemC TLM.
Definition dmi_mgr.h:19
Extension for data buffering.
Extension for a TLM payload.