summaryrefslogtreecommitdiffstats
path: root/src/include/usr/util/utilmem.H
blob: 66a35ddc60f00431650fe71a7a75ceb162fd5b60 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/include/usr/util/utilmem.H $                              */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2003,2014              */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#ifndef UTILMEM_H
#define UTILMEM_H

/**
 * @file utilmem.H
 *
 * @brief Memory Stream manipulation
 *
 * Used for creating and manipulating memory streams
 *
 */

/*****************************************************************************/
// I n c l u d e s
/*****************************************************************************/

// Utility Includes
#include <util/utilstream.H>

/*****************************************************************************/
// Forwards
/*****************************************************************************/
class UtilFile;

//*****************************************************************************/
// C o n s t a n t s
/*****************************************************************************/
/*****************************************************************************/
// Memory Stream class
/*****************************************************************************/
/**
 *  @brief     Utility Memory Stream class
 *
 *  Used for memory streams
 *
 */
class UtilMem : public UtilStream
{
public:

    /**
     *  @brief Default constructor
     *
     *  Initializes internals of the memory stream object, setting autogrow
     *  to true.
     *
     */
    UtilMem();

    /**
     *  @brief Constructor with size parameter
     *
     *  Initializes internals of the memory stream object and allocates a
     *  stream of the specified size.
     *
     */
    UtilMem(uint32_t i_size);

    /**
     *  @brief Constructor with buffer pointer and size parameter
     *
     *  Initializes internals of the memory stream object.
     *
     */
    UtilMem(void * i_buffer, uint32_t i_size);

    /**
     *  @brief Assignment operator
     *
     */
    UtilMem & operator = ( const UtilMem & i_right );

    /**
     *  @brief UtilFile assignment
     *
     *  Easy way to transfer data between a file and a memory object
     *
     *  The file object is read from the current position up to the
     *  size of the mem object.
     *
     *  @return UtilMem reference
     *
     *  @note Errors are reported by getLastError()
     *
     */
    UtilMem & operator = ( UtilFile & i_right );

    /**
     *  @brief Default destructor
     *
     *
     */
    virtual ~UtilMem();

    /**
     *  @brief Reads data from current position of the stream
     *
     *  Reads data from the current postion of the stream for the
     *  specified number of bytes.
     *
     */
    uint32_t read(
        void *        o_buffer, ///< Buffer data is read into
        uint32_t      i_size    ///< Size in bytes of data to be read
        );

    /**
     *  @brief Writes data to current position of the stream
     *
     *  Writes data to the current postion of the stream for the
     *  specified number of bytes.
     *
     */
    uint32_t write(
        const void *i_buffer,   ///< Source buffer data is written from
        uint32_t    i_size      ///< Size in bytes of data to be written
        );

    /**
     *  @brief Seek to the specified position in the stream
     *
     *  Performs a seek based on the specified position and offset.
     *
     */
    uint32_t seek(
        int         i_pos,      ///< Seek offset in bytes
        whence    i_whence      ///< Position to seek from
        );

    /**
     *  @brief Return the current size of the stream
     *
     *  Returns the current size of the stream.
     *
     */
    uint32_t size() const
    {
        return iv_size;
    }

    /**
     *  @brief Change size
     *
     *  Change the size of the memory object to the requested
     *  size.  The data is maintained.
     *
     */
    void changeSize( uint32_t i_size );

    /**
     *  @brief Set autoGrow flag as specified
     *
     *  Sets the autoGrow flag as specified.
     *
     */
    void autoGrow(bool i_flag);

    /**
     *  @brief Query the autoGrow flag
     *
     *  Queries the autoGrow flag.
     *
     */
    bool autoGrow();

    /**
     *  @brief Base pointer access
     *
     *  Obtains a constant pointer to the data
     *
     */
    const void * base() const
    {
        return iv_memStart;
    }

    /**
     *  @brief Reset the the object
     *
     *  1. Clears the memory to the specified value
     *  2. Sets the offest back to 0
     *  3. Deletes any pending errors
     */
    void reset( int i_c = 0 );

private:

    uint8_t *   iv_memStart;
    uint32_t    iv_offset;
    uint32_t    iv_size;
    bool        iv_autoGrow;
    bool        iv_autoCleanup;

};

/*****************************************************************************/
// I n l i n e s
/*****************************************************************************/

/*****************************************************************************/
// Set the autoGrow flag
/*****************************************************************************/
inline void UtilMem::autoGrow(bool i_flag)
{
    if ( iv_autoCleanup )
    {
        // Can only autogrow if the memory is ours
        iv_autoGrow = i_flag;
    }
}

/*****************************************************************************/
// Query the autoGrow flag
/*****************************************************************************/
inline bool UtilMem::autoGrow()
{
    return iv_autoGrow;
}

#endif //UTILMEM_H
OpenPOWER on IntegriCloud