File: allocator.cpp

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 (392 lines) | stat: -rw-r--r-- 9,772 bytes parent folder | download
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

// Includes for instrumentation
#if defined ( _DEBUG ) || defined ( _INTERNAL )
    #include <cstring>
    #include <stdlib.h>
    #include "IGC/common/igc_debug.h"
    #include "common/Stats.hpp"
#endif

#if defined( _WIN32 )
#include "3d/common/iStdLib/types.h"
#include <new>
#include <cstdint>

#include <intrin.h>
#include <wtypes.h>
#include <WinBase.h>

using namespace std;

#endif // _WIN32

#include "Probe/Assertion.h"

#if ( defined ( _DEBUG ) || defined ( _INTERNAL ) )
/*****************************************************************************\

Class:
CAllocator

Description:
Default memory allocator class

\*****************************************************************************/
class CAllocator
{
public:
    static void*   Allocate(size_t size);
    static void    Deallocate(void* ptr);

    static void*   AlignedAllocate(size_t size, size_t alignment);
    static void    AlignedDeallocate(void* ptr);

protected:
    static void*   Malloc(size_t size);
    static void    Free(void* ptr);

    struct SAllocDesc
    {
        void*   alloc_ptr;
    };
};

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

Function:
CAllocator::Allocate

Description:
Allocates memory from system memory pool

Input:
size_t size

Output:
void*

\*****************************************************************************/
inline void* CAllocator::Allocate(size_t size)
{
#if GET_MEM_STATS
    if(IGC::Debug::GetDebugFlag(IGC::Debug::DebugFlag::MEM_STATS))
    {
        void* ret = NULL;
        unsigned* instrPtr = NULL;

        ret = CAllocator::Malloc(size + 8);

        instrPtr = (unsigned*)ret;
        *instrPtr++ = reinterpret_cast<int&>(size);
        *instrPtr++ = 0xf00ba3;

#ifndef IGC_STANDALONE
        CMemoryReport::MallocMemInstrumentation(size);
#endif

        ret = instrPtr;

        return ret;
    }
    else
    {
        return CAllocator::Malloc(size);
    }
#else
    return CAllocator::Malloc(size);
#endif
}

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

Function:
CAllocator::Deallocate

Description:
Deallocates memory from system memory pool

Input:
void* ptr

Output:
none

\*****************************************************************************/
inline void CAllocator::Deallocate(void* ptr)
{
#if GET_MEM_STATS
    if(IGC::Debug::GetDebugFlag(IGC::Debug::DebugFlag::MEM_STATS))
    {
        if(ptr)
        {
            bool blockValid = true;
            size_t size = 0;

            unsigned* instrPtr = (unsigned*)ptr;

            blockValid = (instrPtr[-1] == 0xf00ba3);
            if(blockValid)
            {
                size = instrPtr[-2];
                ptr = instrPtr - 2;
            }
#ifndef IGC_STANDALONE
            CMemoryReport::FreeMemInstrumentation(size);
#endif
            CAllocator::Free(ptr);
        }
    }
    else
    {
        CAllocator::Free(ptr);
    }
#else
    CAllocator::Free(ptr);
#endif
}

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

Function:
CAllocator::AlignedAllocate

Description:
Allocates aligned memory from system memory pool

Input:
size_t size
size_t alignment

Output:
void*

Notes:

|<-------------------(alloc_size)------------------->|
----------------------------------------------------
|///////|               |                            |
|///////|  SAllocDesc   |<----------(size)---------->|
|///////|               |                            |
----------------------------------------------------
^                       ^
|                       |
alloc_ptr               ptr (aligned)

\*****************************************************************************/
inline void* CAllocator::AlignedAllocate(size_t size, size_t alignment)
{
    void* ptr = NULL;

    // Allocate enough space for the data, the alignment,
    // and storage for the descriptor
    size_t allocSize = size + alignment + sizeof(SAllocDesc);

    void* alloc_ptr = CAllocator::Malloc(allocSize);

    if(alloc_ptr)
    {
        // Ensure there is at least enough space to store the descriptor
        // before performing the alignment
        ptr = (BYTE*)alloc_ptr + sizeof(SAllocDesc);

        // Determine the number of bytes to offset for an aligned pointer
        const size_t offset = (alignment)
            ? alignment - ((size_t)ptr % alignment)
            : 0;

        // Align the pointer
        ptr = (BYTE*)ptr + offset;

        // Store the descriptor for the allocation inside the allocation
        // in the space before the aligned pointer
        SAllocDesc* alloc_desc = (SAllocDesc*)((BYTE*)ptr - sizeof(SAllocDesc));
        alloc_desc->alloc_ptr = alloc_ptr;
    }

    return ptr;
}

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

Function:
CAllocator::AlignedDeallocate

Description:
Deallocates aligned memory from system memory pool

Input:
void* ptr

Output:
none

\*****************************************************************************/
inline void CAllocator::AlignedDeallocate(void* ptr)
{
    if(ptr)
    {
        // Extract the descriptor for the allocation
        SAllocDesc* alloc_desc = (SAllocDesc*)((BYTE*)ptr - sizeof(SAllocDesc));
        void* alloc_ptr = alloc_desc->alloc_ptr;

        CAllocator::Free(alloc_ptr);
    }
}

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

Function:
CAllocator::Malloc

Description:
Abstraction for "malloc"

Input:
size_t size

Output:
void*

\*****************************************************************************/
inline void* CAllocator::Malloc(size_t size)
{
    void* const ptr = malloc(size);

#ifdef _DEBUG
    if(nullptr != ptr)
    {
        std::memset(ptr, 0xcc, size);
    }
#endif
#ifdef _WIN32
    IGC_ASSERT_EXIT_MESSAGE(nullptr != ptr, "Could not allocate the required memory to storage");
#endif
    return ptr;
}

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

Function:
CAllocator::Free

Description:
Abstraction for "free"

Input:
void* ptr

Output:
none

\*****************************************************************************/
inline void CAllocator::Free(void* ptr)
{
    if(ptr)
    {
       free( ptr );
    }
}

#endif //defined( _WIN32 ) || ( defined ( _DEBUG ) || defined ( _INTERNAL ) )

/*****************************************************************************\
locally visible new & delete for Linux
\*****************************************************************************/
#if defined ( _DEBUG ) || defined ( _INTERNAL )
#if defined __GNUC__

#if !defined __clang__
/*
    The clang compiler is relatively strict with regard to warnings.
    This is a particular issue when building with -Werror.

    In the case addressed by this change, clang is being strict about
    redefinition of operators new, new[], delete, and delete[].

    The clang compiler will warn:
    - If a new declaration does not match a previous declaration
      - with respect to inline vs extern
      - with respect to exception specification (e.g., "noexcept")
      - with respect to parameter list (e.g., "std::size_t" vs "size_t").
    Warnings that may be seen are:
    - -Winline-new-delete
    - -Wimplicit-exception-spec-mismatch
    - -Wmicrosoft-exception-spec

    Because of the above, this special debug code (normally built for
    build_type == release-internal or debug) is disabled for clang for now.
    */

// TODO: Throw exception if the allocation fails.
inline void* operator new(size_t size)
{
    void* storage = CAllocator::Allocate(size);
    IGC_ASSERT_EXIT_MESSAGE(nullptr != storage, "Could not allocate the required memory to storage");
    return storage;
}

inline void operator delete(void* ptr)
{
    CAllocator::Deallocate(ptr);
}

// TODO: Throw exception if the allocation fails.
inline void* operator new[](size_t size)
{
    return ::operator new(size);
}

inline void operator delete[](void* ptr)
{
    CAllocator::Deallocate(ptr);
}
#endif // !defined __clang__
#endif//defined __GNUC__
#endif //defined ( _DEBUG ) || defined ( _INTERNAL )

#ifndef __GNUC__
#define __NOTAGNUC__
#endif // __GNUC__

#if ( ( defined ( _DEBUG ) || defined ( _INTERNAL ) ) && (defined __NOTAGNUC__ ) )
/*****************************************************************************\
 operator new
\*****************************************************************************/
void* __cdecl operator new( size_t size )
{
    void* storage = CAllocator::Allocate(size);
    IGC_ASSERT_EXIT_MESSAGE(nullptr != storage, "Could not allocate the required memory to storage");
    return storage;
}

/*****************************************************************************\
 operator delete
\*****************************************************************************/
void __cdecl operator delete( void* ptr )
{
    CAllocator::Deallocate( ptr );
}

/*****************************************************************************\
 operator new[]
\*****************************************************************************/
void* __cdecl operator new[]( size_t size )
{
    return ::operator new(size);
}

/*****************************************************************************\
 operator delete[]
\*****************************************************************************/
void __cdecl operator delete[]( void* ptr )
{
    CAllocator::Deallocate( ptr );
}
#endif // ( ( defined ( _DEBUG ) || defined ( _INTERNAL ) ) && (defined __NOTAGNUC__) )