instance_specific_extensions_int.h revision 13513
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 <tlm>
23
24namespace tlm_utils
25{
26
27class ispex_base;
28class instance_specific_extension_accessor;
29class instance_specific_extension_container;
30class instance_specific_extension_carrier;
31class instance_specific_extension_container_pool;
32
33} // namespace tlm_utils
34
35namespace tlm
36{
37
38extern template class tlm_array<tlm_utils::ispex_base *>;
39
40} // namespace tlm
41
42namespace tlm_utils
43{
44
45// The private extension base. Similar to normal extension base, but without
46// clone and free.
47class ispex_base
48{
49    friend class tlm::tlm_array<ispex_base*>;
50    void free() {} // Needed for explicit tlm_array instantiation.
51
52  public:
53    virtual ~ispex_base() {}
54
55  protected:
56    static unsigned int register_private_extension(const std::type_info &);
57};
58
59// This thing is basically a snippet of the generic_payload.
60// It contains all the extension specific code (the extension API so to speak)
61// the differences are:
62// - it calls back to its owner whenever a real (==non-NULL) extension gets
63// set for the first time.
64// - it calls back to its owner whenever a living (==non-NULL) extension gets
65// cleared.
66class instance_specific_extensions_per_accessor
67{
68  public:
69    typedef instance_specific_extension_container container_type;
70
71    explicit
72    instance_specific_extensions_per_accessor(container_type *container) :
73        m_container(container)
74    {}
75
76    template <typename T>
77    T *
78    set_extension(T *ext)
79    {
80        return static_cast<T *>(set_extension(T::priv_id, ext));
81    }
82
83    // Non-templatized version with manual index:
84    ispex_base *set_extension(unsigned int index, ispex_base *ext);
85
86    // Check for an extension, ext will be nullptr if not present.
87    template <typename T>
88    void get_extension(T *& ext) const
89    {
90        ext = static_cast<T *>(get_extension(T::priv_id));
91    }
92    // Non-templatized version:
93    ispex_base *get_extension(unsigned int index) const;
94
95    // Clear extension, the argument is needed to find the right index:
96    template <typename T>
97    void clear_extension(const T *)
98    {
99        clear_extension(T::priv_id);
100    }
101
102    // Non-templatized version with manual index
103    void clear_extension(unsigned int index);
104
105    // Make sure the extension array is large enough. Can be called once by
106    // an initiator module (before issuing the first transaction) to make
107    // sure that the extension array is of correct size. This is only needed
108    // if the initiator cannot guarantee that the generic payload object is
109    // allocated after C++ static construction time.
110    void resize_extensions();
111
112  private:
113    tlm::tlm_array<ispex_base *> m_extensions;
114    container_type* m_container;
115};
116
117// This thing contains the vector of extensions per accessor
118// which can be really large so this one should be pool allocated.
119// Therefore it keeps a use_count of itself to automatically free itself.
120// - to this end it provides callbacks to the extensions per accessor
121//   to increment and decrement the use_count.
122class instance_specific_extension_container
123{
124    friend class instance_specific_extension_accessor;
125    friend class instance_specific_extension_carrier;
126    friend class instance_specific_extension_container_pool;
127    friend class instance_specific_extensions_per_accessor;
128
129    typedef void release_fn(instance_specific_extension_carrier *, void *);
130
131    instance_specific_extension_container();
132    ~instance_specific_extension_container();
133
134    void resize();
135
136    void inc_use_count();
137    void dec_use_count();
138
139    static instance_specific_extension_container *create();
140    void attach_carrier(
141            instance_specific_extension_carrier *, void *txn, release_fn *);
142
143    instance_specific_extensions_per_accessor *
144        get_accessor(unsigned int index);
145
146    std::vector<instance_specific_extensions_per_accessor *>
147        m_ispex_per_accessor;
148    unsigned int use_count;
149    void *m_txn;
150    release_fn *m_release_fn;
151    instance_specific_extension_carrier *m_carrier;
152    instance_specific_extension_container *next; // For pooling.
153};
154
155// ----------------------------------------------------------------------------
156
157// This class 'hides' all the instance specific extension stuff from the user.
158// They instantiates one of those (e.g. instance_specific_extension_accessor
159// extAcc;) and can then access the private extensions.
160//    extAcc(txn).extensionAPIFnCall()
161// where extensionAPIFnCall is set_extension, get_extension,
162//      clear_extension,...
163class instance_specific_extension_accessor
164{
165  public:
166    instance_specific_extension_accessor();
167
168    // Implementation in instance_specific_extensions.h
169    template <typename T>
170    inline instance_specific_extensions_per_accessor &operator () (T &txn);
171
172  protected:
173    template<typename T>
174    static void release_carrier(
175            instance_specific_extension_carrier *, void * txn);
176
177    unsigned int m_index;
178};
179
180} // namespace tlm_utils
181
182#endif /* __SYSTEMC_EXT_TLM_UTILS_INSTANCE_SPECIFIC_EXTENSIONS_INT_H__ */
183