File: Stream.h

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (420 lines) | stat: -rw-r--r-- 11,099 bytes parent folder | download | duplicates (3)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*========================== begin_copyright_notice ============================

Copyright (C) 2019-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#pragma once

#include "Array.h"
#include "Buffer.h"
#include "utility.h"
#include "MemCopy.h"

namespace iSTD
{

/*****************************************************************************\

Class:
    CStream

Description:
    Allocates and manages a system memory buffer of unknown size

\*****************************************************************************/
template<class CAllocatorType>
class CStream : public CObject<CAllocatorType>
{
public:

    CStream( void );
    virtual ~CStream( void );

    void*   GetSpace( const DWORD dwSize );
    void*   GetLinearAddress( void );
    DWORD   GetUsedSpace( void ) const;
    void    SetUsedSpace( DWORD currentSizeUsed );
    void    PutAllSpace( void );

    void*   GetSegmentAddress( const DWORD offset, const DWORD size ) const;

protected:

    CDynamicArray<CBuffer<CAllocatorType>*,CAllocatorType> m_StreamArray;

    CBuffer<CAllocatorType>*    m_pCurrentBuffer;
    DWORD                       m_StreamBufferCount;
    CBuffer<CAllocatorType>*    m_pStreamBuffer;
    DWORD                       m_dwSizeUsed;
};

/*****************************************************************************\

Function:
    CStream Constructor

Description:
    Initializes internal data

Input:
    none

Output:
    none

\*****************************************************************************/
template<class CAllocatorType>
inline CStream<CAllocatorType>::CStream( void )
    : CObject<CAllocatorType>(),
      m_StreamArray(0)
{
    m_pCurrentBuffer = NULL;
    m_StreamBufferCount = 0;
    m_pStreamBuffer = NULL;
    m_dwSizeUsed = 0;
}

/*****************************************************************************\

Function:
    CStream Destructor

Description:
    Deletes internal data

Input:
    none

Output:
    none

\*****************************************************************************/
template<class CAllocatorType>
inline CStream<CAllocatorType>::~CStream( void )
{
    const DWORD dwCount = m_StreamArray.GetSize();
    for( DWORD i = 0; i < dwCount; i++ )
    {
        CBuffer<CAllocatorType>* pBuffer = m_StreamArray.GetElement(i);
        SafeDelete( pBuffer );
    }

    SafeDelete( m_pStreamBuffer );
}

/*****************************************************************************\

Function:
    CStream::GetSpace

Description:
    Gets space from the stream

Input:
    const DWORD dwSize - size in bytes

Output:
    void* - linear address of space

\*****************************************************************************/
template<class CAllocatorType>
inline void* CStream<CAllocatorType>::GetSpace( const DWORD dwSize )
{
    // If this is the first GetSpace call, or
    if( ( !m_pCurrentBuffer ) ||
        // the size requested is larger than the size available
        ( dwSize > m_pCurrentBuffer->GetAvailableSpace() ) )
    {
        // Create a new buffer large enough for the requested size
        m_pCurrentBuffer = new CBuffer<CAllocatorType>();
        ASSERT( m_pCurrentBuffer );

        if( m_pCurrentBuffer &&
            m_pCurrentBuffer->Allocate( Max( (DWORD)0x1000, dwSize ), sizeof(DWORD) ) )
        {
            // Add the buffer to the array of buffers
            if( m_StreamArray.SetElement( m_StreamBufferCount, m_pCurrentBuffer ) )
            {
                // Keep track of the number of buffers in the array
                m_StreamBufferCount++;
            }
            else
            {
                ASSERT(0);
                SafeDelete( m_pCurrentBuffer );
                m_pCurrentBuffer = NULL;
            }
        }
        else
        {
            ASSERT(0);
            SafeDelete( m_pCurrentBuffer );
            m_pCurrentBuffer = NULL;
        }
    }

    if( m_pCurrentBuffer )
    {
        // Keep track of the total size of all buffers
        m_dwSizeUsed += dwSize;
        return m_pCurrentBuffer->GetSpace( dwSize );
    }
    else
    {
        return NULL;
    }
}

/*****************************************************************************\

Function:
    CStream::GetLinearAddress

Description:
    Gets the contiguous linear address of the entire stream

Input:
    none

Output:
    void* - linear address

\*****************************************************************************/
template<class CAllocatorType>
inline void* CStream<CAllocatorType>::GetLinearAddress( void )
{
    // If there are buffers in the array that have not yet been combined
    if( m_StreamBufferCount )
    {
        // Create a new stream buffer large enough for all buffers
        CBuffer<CAllocatorType>* pStreamBuffer = new CBuffer<CAllocatorType>();
        ASSERT( pStreamBuffer );

        if( pStreamBuffer &&
            pStreamBuffer->Allocate( m_dwSizeUsed, sizeof(DWORD) ) )
        {
            // If there already exists another stream buffer
            if( m_pStreamBuffer )
            {
                // Add the contents of the previous buffer to the new one
                MemCopy(
                    pStreamBuffer->GetSpace( m_pStreamBuffer->GetUsedSpace() ),
                    m_pStreamBuffer->GetLinearAddress(),
                    m_pStreamBuffer->GetUsedSpace() );

                SafeDelete( m_pStreamBuffer );
            }

            // For each buffer in the buffer array
            for( DWORD i = 0; i < m_StreamBufferCount; i++ )
            {
                CBuffer<CAllocatorType>* pBuffer = m_StreamArray.GetElement(i);

                if( pBuffer )
                {
                    // Append the contents of the buffers to the stream buffer
                    MemCopy(
                        pStreamBuffer->GetSpace( pBuffer->GetUsedSpace() ),
                        pBuffer->GetLinearAddress(),
                        pBuffer->GetUsedSpace() );

                    SafeDelete( pBuffer );

                    // Remove the buffer from the array
                    m_StreamArray.SetElement( i, NULL );
                }
            }

            m_pCurrentBuffer = NULL;
            m_StreamBufferCount = 0;
            m_pStreamBuffer = pStreamBuffer;
        }
        else
        {
            ASSERT(0);
            SafeDelete( pStreamBuffer );
            pStreamBuffer = NULL;

            // If we fail to recombine the buffers into a single linear allocation
            // then return NULL to indicate the failure
            return NULL;
        }
    }

    return ( m_pStreamBuffer )
           ? m_pStreamBuffer->GetLinearAddress()
           : NULL;
}

/*****************************************************************************\

Function:
    CStream::GetUsedSpace

Description:
    Gets the current size of the stream

Input:
    none

Output:
    DWORD - size in bytes

\*****************************************************************************/
template<class CAllocatorType>
inline DWORD CStream<CAllocatorType>::GetUsedSpace( void ) const
{
    return m_dwSizeUsed;
}

/*****************************************************************************\

Function:
    CStream::SetUsedSpace

Description:
    Sets the current size of the stream.
    Function should be used only to sets smaller size.

Input:
    DWORD - size in bytes

Output:
    none

\*****************************************************************************/
template<class CAllocatorType>
inline void CStream<CAllocatorType>::SetUsedSpace( DWORD currentSizeUsed )
{
    ASSERT( currentSizeUsed == 0 );
    ASSERT( currentSizeUsed < m_dwSizeUsed );

    m_dwSizeUsed = currentSizeUsed;
}

/*****************************************************************************\

Function:
    CStream::PutAllSpace

Description:
    Puts all space back into the CStream.  All space becomes unused.

Input:
    none

Output:
    none

\*****************************************************************************/
template<class CAllocatorType>
inline void CStream<CAllocatorType>::PutAllSpace( void )
{
    const DWORD dwCount = m_StreamArray.GetSize();

    for( DWORD i = 0; i < dwCount; i++ )
    {
        CBuffer<CAllocatorType>* pBuffer = m_StreamArray.GetElement(i);
        if( pBuffer )
        {
            pBuffer->PutAllSpace();
        }
    }
    m_dwSizeUsed = 0;
    
    if( m_pStreamBuffer )
    {
        m_pStreamBuffer->PutAllSpace();
    }
}

/*****************************************************************************\

Function:
    CStream::GetSegmentAddress

Description:
    Gets the contiguous linear address a segment of the stream

Input:
    const DWORD offset - the offset in the stream of the segment
    const DWORD size - the size in bytes of the segment

Output:
    void* - linear address

Notes:
    The address returned is only valid for the segment requested. It should
    not be expected that memory access outside the returned segment is valid.

    If a segment straddles more than one stream buffer, then the request will
    fail.  This condition will not happen as long as the user accesses segments
    in the same location and size in which they were added to the stream using
    GetSpace calls.

\*****************************************************************************/
template<class CAllocatorType>
inline void* CStream<CAllocatorType>::GetSegmentAddress(
    const DWORD offset,
    const DWORD size ) const
{
    DWORD currentOffset = offset;

    if( m_pStreamBuffer )
    {
        if( currentOffset < m_pStreamBuffer->GetUsedSpace() )
        {
            if( ( currentOffset + size ) <= m_pStreamBuffer->GetUsedSpace() )
            {
                return (BYTE*)m_pStreamBuffer->GetLinearAddress() + offset;
            }
            else
            {
                // The offset is within the buffer, but the size requested
                // is too large for a contiguous address
                ASSERT(0);
                return NULL;
            }
        }
        else
        {
            currentOffset -= m_pStreamBuffer->GetUsedSpace();
        }
    }

    for( DWORD i = 0; i < m_StreamBufferCount; i++ )
    {
        CBuffer<CAllocatorType>* pBuffer = m_StreamArray.GetElement(i);

        if( pBuffer )
        {
            if( currentOffset < pBuffer->GetUsedSpace() )
            {
                if( ( currentOffset + size ) <= pBuffer->GetUsedSpace() )
                {
                    return (BYTE*)pBuffer->GetLinearAddress() + currentOffset;
                }
                else
                {
                    // The offset is within the buffer, but the size requested
                    // is too large for a contiguous address
                    ASSERT(0);
                    return NULL;
                }
            }
            else
            {
                currentOffset -= pBuffer->GetUsedSpace();
            }
        }
    }

    // offset not found
    ASSERT(0);
    return NULL;
}

} // iSTD