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