1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19#ifndef __SYSTEMC_EXT_TLM_UTILS_INSTANCE_SPECIFIC_EXTENSIONS_INT_H__
20#define __SYSTEMC_EXT_TLM_UTILS_INSTANCE_SPECIFIC_EXTENSIONS_INT_H__
21
22#include <vector>
23
24#include "../tlm_core/2/generic_payload/array.hh"
25
26namespace tlm_utils
27{
28
29class ispex_base;
30class instance_specific_extension_accessor;
31class instance_specific_extension_container;
32class instance_specific_extension_carrier;
33class instance_specific_extension_container_pool;
34
35} // namespace tlm_utils
36
37namespace tlm
38{
39
40extern template class tlm_array<tlm_utils::ispex_base *>;
41
42} // namespace tlm
43
44namespace tlm_utils
45{
46
47// The private extension base. Similar to normal extension base, but without
48// clone and free.
49class ispex_base
50{
51    friend class tlm::tlm_array<ispex_base*>;
52    void free() {} // Needed for explicit tlm_array instantiation.
53
54  public:
55    virtual ~ispex_base() {}
56
57  protected:
58    static unsigned int register_private_extension(const std::type_info &);
59};
60
61// This thing is basically a snippet of the generic_payload.
62// It contains all the extension specific code (the extension API so to speak)
63// the differences are:
64// - it calls back to its owner whenever a real (==non-NULL) extension gets
65// set for the first time.
66// - it calls back to its owner whenever a living (==non-NULL) extension gets
67// cleared.
68class instance_specific_extensions_per_accessor
69{
70  public:
71    typedef instance_specific_extension_container container_type;
72
73    explicit
74    instance_specific_extensions_per_accessor(container_type *container) :
75        m_container(container)
76    {}
77
78    template <typename T>
79    T *
80    set_extension(T *ext)
81    {
82        return static_cast<T *>(set_extension(T::priv_id, ext));
83    }
84
85    // Non-templatized version with manual index:
86    ispex_base *set_extension(unsigned int index, ispex_base *ext);
87
88    // Check for an extension, ext will be nullptr if not present.
89    template <typename T>
90    void get_extension(T *& ext) const
91    {
92        ext = static_cast<T *>(get_extension(T::priv_id));
93    }
94    // Non-templatized version:
95    ispex_base *get_extension(unsigned int index) const;
96
97    // Clear extension, the argument is needed to find the right index:
98    template <typename T>
99    void clear_extension(const T *)
100    {
101        clear_extension(T::priv_id);
102    }
103
104    // Non-templatized version with manual index
105    void clear_extension(unsigned int index);
106
107    // Make sure the extension array is large enough. Can be called once by
108    // an initiator module (before issuing the first transaction) to make
109    // sure that the extension array is of correct size. This is only needed
110    // if the initiator cannot guarantee that the generic payload object is
111    // allocated after C++ static construction time.
112    void resize_extensions();
113
114  private:
115    tlm::tlm_array<ispex_base *> m_extensions;
116    container_type* m_container;
117};
118
119// This thing contains the vector of extensions per accessor
120// which can be really large so this one should be pool allocated.
121// Therefore it keeps a use_count of itself to automatically free itself.
122// - to this end it provides callbacks to the extensions per accessor
123//   to increment and decrement the use_count.
124class instance_specific_extension_container
125{
126    friend class instance_specific_extension_accessor;
127    friend class instance_specific_extension_carrier;
128    friend class instance_specific_extension_container_pool;
129    friend class instance_specific_extensions_per_accessor;
130
131    typedef void release_fn(instance_specific_extension_carrier *, void *);
132
133    instance_specific_extension_container();
134    ~instance_specific_extension_container();
135
136    void resize();
137
138    void inc_use_count();
139    void dec_use_count();
140
141    static instance_specific_extension_container *create();
142    void attach_carrier(
143            instance_specific_extension_carrier *, void *txn, release_fn *);
144
145    instance_specific_extensions_per_accessor *
146        get_accessor(unsigned int index);
147
148    std::vector<instance_specific_extensions_per_accessor *>
149        m_ispex_per_accessor;
150    unsigned int use_count;
151    void *m_txn;
152    release_fn *m_release_fn;
153    instance_specific_extension_carrier *m_carrier;
154    instance_specific_extension_container *next; // For pooling.
155};
156
157// ----------------------------------------------------------------------------
158
159// This class 'hides' all the instance specific extension stuff from the user.
160// They instantiates one of those (e.g. instance_specific_extension_accessor
161// extAcc;) and can then access the private extensions.
162//    extAcc(txn).extensionAPIFnCall()
163// where extensionAPIFnCall is set_extension, get_extension,
164//      clear_extension,...
165class instance_specific_extension_accessor
166{
167  public:
168    instance_specific_extension_accessor();
169
170    // Implementation in instance_specific_extensions.h
171    template <typename T>
172    inline instance_specific_extensions_per_accessor &operator () (T &txn);
173
174  protected:
175    template<typename T>
176    static void release_carrier(
177            instance_specific_extension_carrier *, void * txn);
178
179    unsigned int m_index;
180};
181
182} // namespace tlm_utils
183
184#endif /* __SYSTEMC_EXT_TLM_UTILS_INSTANCE_SPECIFIC_EXTENSIONS_INT_H__ */
185