SystemC Library API Reference Manual
Reference documentation for the Simics SystemC Library.
 
Loading...
Searching...
No Matches
transaction_data_buffer_pool.h
Go to the documentation of this file.
1/*
2 © 2025 Intel Corporation
3
4 This software and the related documents are Intel copyrighted materials, and
5 your use of them is governed by the express license under which they were
6 provided to you ("License"). Unless the License provides otherwise, you may
7 not use, modify, copy, publish, distribute, disclose or transmit this software
8 or the related documents without Intel's prior written permission.
9
10 This software and the related documents are provided as is, with no express or
11 implied warranties, other than those that are expressly stated in the License.
12*/
13
14#ifndef SIMICS_SYSTEMC_IFACE_TRANSACTION_DATA_BUFFER_POOL_H
15#define SIMICS_SYSTEMC_IFACE_TRANSACTION_DATA_BUFFER_POOL_H
16
17#include <simics/base/transaction.h>
18
19#include <cstring> // memset
20#include <stdexcept>
21#include <unordered_map>
22#include <vector>
23
24namespace simics {
25namespace systemc {
26namespace iface {
27
28/*
29 * The data buffer used for get/set the data from a transaction
30 */
32 public:
35 explicit TransactionDataBuffer(unsigned num_bytes)
36 : data_(num_bytes, 0) {
37 if (num_bytes == 0) {
38 throw std::invalid_argument {
39 "Cannot allocate an empty data buffer"
40 };
41 }
42 buffer_.data = &data_.front();
43 buffer_.len = num_bytes;
44 bytes_.data = &data_.front();
45 bytes_.len = num_bytes;
46 }
47
49 buffer_.data = nullptr;
50 bytes_.data = nullptr;
51 }
52
55 unsigned numBytes() const {
56 return data_.size();
57 }
58
60 unsigned char *dataPtr() const {
61 return buffer_.data;
62 }
63
64
66 void copyToBuffer(const transaction_t *transaction) {
67 auto size = SIM_transaction_size(transaction);
68 if (size > numBytes()) {
69 throw std::logic_error {
70 "The number of bytes for this data buffer ("
71 + std::to_string(numBytes())
72 + ") does not fit the size of the transaction ("
73 + std::to_string(size) + ")"
74 };
75 }
76 buffer_.len = size;
77 SIM_get_transaction_bytes(transaction, buffer_);
78 }
79
81 void copyFromBuffer(const transaction_t *transaction) {
82 auto size = SIM_transaction_size(transaction);
83 if (size > numBytes()) {
84 throw std::logic_error {
85 "The number of bytes for this data buffer ("
86 + std::to_string(numBytes())
87 + ") does not fit the size of the transaction ("
88 + std::to_string(size) + ")"
89 };
90 }
91 bytes_.len = size;
92 SIM_set_transaction_bytes(transaction, bytes_);
93 }
94
95 private:
96 std::vector<unsigned char> data_;
97 buffer_t buffer_;
98 bytes_t bytes_;
99};
100
101/*
102 * The pool for allocating/releasing TransactionDataBuffers
103 */
105 public:
107 for (auto it = pool_.begin(); it != pool_.end(); ++it) {
108 delete it->second;
109 }
110 }
111
113 TransactionDataBuffer *buf = nullptr;
114 auto roundup_size = roundUpSize(size);
115 auto it = pool_.find(roundup_size);
116 if (it != pool_.end()) {
117 buf = it->second;
118 // Remove only one buffer of this size
119 pool_.erase(it);
120 } else {
121 buf = new TransactionDataBuffer(roundup_size);
122 }
123 return buf;
124 }
125
127 pool_.emplace(buf->numBytes(), buf);
128 }
129
130 private:
131 // Round up requested size to the nearest size class:
132 // 8, 8K and 8M
133 static unsigned roundUpSize(unsigned size) {
134 if (size <= 8) {
135 return 8;
136 }
137 if (size <= 8192) {
138 return 8192;
139 }
140 if (size <= 8388608) {
141 return 8388608;
142 }
143 throw std::logic_error {
144 "The transaction data buffer does not support"
145 " data larger than 8M. Please report it."
146 };
147 }
148
149 std::unordered_multimap<unsigned, TransactionDataBuffer*> pool_;
150};
151
152} // namespace iface
153} // namespace systemc
154} // namespace simics
155
156#endif
Definition: transaction_data_buffer_pool.h:104
virtual ~TransactionDataBufferPool()
Definition: transaction_data_buffer_pool.h:106
void release(TransactionDataBuffer *buf)
Definition: transaction_data_buffer_pool.h:126
TransactionDataBuffer * acquire(unsigned size)
Definition: transaction_data_buffer_pool.h:112
Definition: transaction_data_buffer_pool.h:31
void copyToBuffer(const transaction_t *transaction)
Copy the bytes from transaction to buffer_.
Definition: transaction_data_buffer_pool.h:66
TransactionDataBuffer(unsigned num_bytes)
Definition: transaction_data_buffer_pool.h:35
void copyFromBuffer(const transaction_t *transaction)
Copy the bytes_ to transaction.
Definition: transaction_data_buffer_pool.h:81
virtual ~TransactionDataBuffer()
Definition: transaction_data_buffer_pool.h:48
unsigned char * dataPtr() const
Returns the data pointer to the data buffer.
Definition: transaction_data_buffer_pool.h:60
unsigned numBytes() const
Returns the maximum number of bytes in the data buffer, it is not affected by reSize and clear.
Definition: transaction_data_buffer_pool.h:55
Definition: adapter.h:79