Diligent Engine API Reference: D:/GitHub/DiligentEngine/DiligentCore/Common/interface/AdaptiveFixedBlockAllocator.h Source File

Diligent Engine API Reference

AdaptiveFixedBlockAllocator.h
1 /* Copyright 2015-2018 Egor Yusov
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12 *
13 * In no event and under no legal theory, whether in tort (including negligence),
14 * contract, or otherwise, unless required by applicable law (such as deliberate
15 * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16 * liable for any damages, including any direct, indirect, special, incidental,
17 * or consequential damages of any character arising as a result of this License or
18 * out of the use or inability to use the software (including but not limited to damages
19 * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20 * all other commercial damages or losses), even if such Contributor has been advised
21 * of the possibility of such damages.
22 */
23
24 #pragma once
25
26 #include "MemoryAllocator.h"
27 #include "FixedBlockMemoryAllocator.h"
28 #include "DebugUtilities.h"
29
30 namespace Diligent
31 {
32 // Adaptive allocator that can function as a raw memory allocator or as
33 // fixed block memory allocator. In the fixed block memory allocator mode,
34 // the block size is determined by the size of the first allocation
35 class AdaptiveFixedBlockAllocator : public IMemoryAllocator
36 {
37 public:
38 AdaptiveFixedBlockAllocator(IMemoryAllocator &RawMemAllocator, Uint32 NumBlocksPerAllocation) :
39 m_RawMemAllocator(RawMemAllocator),
40 m_pFixedBlockAllocator(nullptr, STDDeleterRawMem<FixedBlockMemoryAllocator>(RawMemAllocator)),
41 m_NumBlocksPerAllocation(NumBlocksPerAllocation)
42 {
43 // Initialize allocator when we get the fist allocation request and know allocation size
44 }
45
46 // Allocates block of memory
47 virtual void* Allocate(size_t Size, const Char* dbgDescription, const char* dbgFileName, const Int32 dbgLineNumber)override final
48 {
49 if (m_NumBlocksPerAllocation > 1)
50 {
51 if( !m_pFixedBlockAllocator )
52 {
53 // Create fixed block allocator
54 auto *pRawMem = m_RawMemAllocator.Allocate(sizeof(FixedBlockMemoryAllocator), "Memory for FixedBlockMemoryAllocator", __FILE__, __LINE__);
55 m_pFixedBlockAllocator.reset( new(pRawMem) FixedBlockMemoryAllocator(m_RawMemAllocator, Size, m_NumBlocksPerAllocation) );
56 }
57
58 return m_pFixedBlockAllocator->Allocate(Size, dbgDescription, dbgFileName, dbgLineNumber);
59 }
60 else
61 {
62 // Use default raw allocator
63 return m_RawMemAllocator.Allocate(Size, dbgDescription, dbgFileName, dbgLineNumber);
64 }
65 }
66
67 // Releases memory
68 virtual void Free(void *Ptr)override final
69 {
70 if (m_NumBlocksPerAllocation > 1)
71 {
72 VERIFY_EXPR(m_pFixedBlockAllocator);
73 m_pFixedBlockAllocator->Free(Ptr);
74 }
75 else
76 {
77 VERIFY_EXPR(!m_pFixedBlockAllocator);
78 m_RawMemAllocator.Free(Ptr);
79 }
80 }
81
82 private:
83 IMemoryAllocator &m_RawMemAllocator;
84 Uint32 m_NumBlocksPerAllocation = 0;
85 std::unique_ptr<FixedBlockMemoryAllocator, STDDeleterRawMem<FixedBlockMemoryAllocator> > m_pFixedBlockAllocator;
86 };
87 }
Diligent
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34