summaryrefslogtreecommitdiffstats
path: root/src/import/hwpf/fapi2/include/array.H
blob: e7bb8d95a5692d657386c3777def111035ffbf4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: hwpf/fapi2/include/array.H $                                  */
/*                                                                        */
/* IBM CONFIDENTIAL                                                       */
/*                                                                        */
/* EKB Project                                                            */
/*                                                                        */
/* COPYRIGHT 2012,2015                                                    */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* The source code for this program is not published or otherwise         */
/* divested of its trade secrets, irrespective of what has been           */
/* deposited with the U.S. Copyright Office.                              */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/**
 * @file array.H
 * @brief definitions for fapi2 arrays
 */

#ifndef __FAPI2_ARRAY__
#define __FAPI2_ARRAY__

#include <stdint.h>
#include <utility>
#include <assert.h>
#include <string.h>

namespace fapi2
{
///
/// @brief Class representing a FAPI2 array
/// FAPI2 arrays are defined to be very lightweight but support
/// c++ container operations (iterators, bounds checking, assignment, etc.)
/// To avoid the code-bloat associated with std::vector templates,
/// fapi2::array is presently limited to 64-bit elements.
///
/// To construct an array, you can either pass in an existing chunk
/// of memory, or let the container allocate memory for you:
///    fapi2::array foo(3, &PIB_MEM_BLOCK);
/// creates an array 3 x uit64_t in size, located at &PIB_MEM_BLOCK.
/// The memory pointed to by the address passed in is untouched
/// during creation. This allows for a light-weight overlay on top
/// of existing memory. It also means you need to initialize the space
/// yourself.
///    fapi2_array foo(3);
/// creates an array 3 x uint64_t in size, and that memory will be
/// allocated by the constructor and initiaized to 0's.
///
///
class array
{
    public:

        typedef uint64_t element_type;
        typedef element_type* iterator;
        typedef const element_type* const_iterator;

        ///
        /// @brief Create an array
        /// @param[in] i_size the size of the array
        /// @param[in] i_data a pointer to memory of appropriate size
        ///   defaults to nullptr which causes the platform to
        ///   allocate memory of size * element_type
        /// @warning fapi2::arrays, like arrays, can not be re-sized after
        /// creation.
        ///
        array(const uint32_t i_size, element_type* i_data = nullptr);

        ///
        /// @brief Destroy an array
        ///
        ~array(void);

        ///
        /// @brief operator[]
        /// @param[in] i_index the index of the element
        /// @return a reference to the element in question.
        /// @note array[0] = 0 works as well as foo = array[0]
        ///
        element_type& operator[](const uint32_t i_index);

        ///
        /// @brief operator=()
        /// @param[in] i_other the other array
        /// @return a reference to this, after the assignement
        ///
        array& operator=(const array& i_other);

        ///
        /// @brief move operator=()
        /// @note To use: new_array = std::move(old_array). old_array will be
        /// destroyed and no copy will be made (moved)
        ///
        array& operator=(array&& i_other);

        ///
        /// @brief operator==()
        ///
        bool operator==(const array& i_other);

        ///
        /// @brief operator!=()
        ///
        __attribute__ ((always_inline))
        bool operator!=(const array& i_other)
        {
            return ! operator==(i_other);
        }

        ///
        /// @brief Return an iterator the to beginning of the array
        /// @return An iterator to the beginning of the array
        ///
        __attribute__ ((always_inline))
        iterator begin(void)
        {
            return iv_data;
        }

        ///
        /// @brief Return an iterator the to end of the array
        /// @return An iterator to the end of the array
        ///
        __attribute__ ((always_inline))
        iterator end(void)
        {
            return iv_data + size();
        }

        ///
        /// @brief Return a const_iterator the to beginning of the array
        /// @return A const_iterator to the beginning of the array
        ///
        __attribute__ ((always_inline))
        const_iterator begin(void) const
        {
            return iv_data;
        }

        ///
        /// @brief Return a const_iterator the to end of the array
        /// @return A const_iterator to the end the array
        ///
        __attribute__ ((always_inline))
        const_iterator end(void) const
        {
            return iv_data + size();
        }

    private:

        enum
        {
            // Bit in iv_size representing whether we delete in the dtor
            delete_bit = 0x80000000,

            // The resulting size limit
            size_limit = 0x7FFFFFFF,
        };

        __attribute__ ((always_inline))
        uint32_t size(void)
        {
            return (iv_size & ~delete_bit);
        }

        __attribute__ ((always_inline))
        uint32_t size(void) const
        {
            return (iv_size & ~delete_bit);
        }

        uint32_t      iv_size;
        element_type* iv_data;
};
}

#endif
OpenPOWER on IntegriCloud