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
20#ifndef __SYSTEMC_EXT_TLM_CORE_2_GENERIC_PAYLOADS_ARRAY_HH__
21#define __SYSTEMC_EXT_TLM_CORE_2_GENERIC_PAYLOADS_ARRAY_HH__
22
23#include <vector>
24
25namespace tlm
26{
27
28// This implements a lean and fast array class that supports array expansion on
29// request. The class is primarily used in the tlm_generic_payload class for
30// storing the pointers to the extensions.
31//
32// Individual array elements can be accessed through the [] operators, and the
33// array length is returned by the size() method.
34//
35// The size can be dynamically expanded using the expand(uint) method. There
36// is no shrinking mechanism implemented, because the extension mechanism
37// does not require this feature. Bear in mind that calling the expand method
38// may invalidate all direct pointers into the array.
39
40
41// The tlm_array shall always be used with T=tlm_extension_base*.
42template <typename T>
43class tlm_array : private std::vector<T>
44{
45  private:
46    typedef std::vector<T> base_type;
47    typedef typename base_type::size_type size_type;
48
49  public:
50    tlm_array(size_type size=0) : base_type(size), m_entries() {}
51
52    // Operators for dereferencing:
53    using base_type::operator [];
54
55    // Array size:
56    using base_type::size;
57
58    // Expand the array if needed:
59    void
60    expand(size_type new_size)
61    {
62        if (new_size > size())
63            base_type::resize(new_size);
64    }
65
66    static const char *const kind_string;
67    const char *kind() const { return kind_string; }
68
69    // This function shall get a pointer to an array slot
70    // it stores this slot in a cache of active slots
71    void insert_in_cache(T *p) { m_entries.push_back(p - &(*this)[0]); }
72
73    // This functions clears all active slots of the array.
74    void
75    free_entire_cache()
76    {
77        while (m_entries.size()) {
78            // We make sure no one cleared the slot manually.
79            if ((*this)[m_entries.back()]) {
80                // ...and then we call free on the content of the slot
81                (*this)[m_entries.back()]->free();
82            }
83            // Afterwards we set the slot to NULL
84            (*this)[m_entries.back()] = nullptr;
85            m_entries.pop_back();
86        }
87    }
88
89  protected:
90    std::vector<size_type> m_entries;
91};
92
93template <typename T>
94const char *const tlm_array<T>::kind_string = "tlm_array";
95
96} // namespace tlm
97
98#endif /* __SYSTEMC_EXT_TLM_CORE_2_GENERIC_PAYLOADS_ARRAY_HH__ */
99