美文网首页
13 UE5 TInlineAllocator介绍

13 UE5 TInlineAllocator介绍

作者: 游戏开发程序员 | 来源:发表于2024-02-24 11:07 被阅读0次

TInlineAllocator

  • 分配器存储大小为NumInlineElements的定长数组,当实际存储的元素数量高于NumInlineElements时,会从SecondaryAllocator申请分配内存,默认情况下为堆分配器。
  • 使用时机:非常了解数组中的最大数量元素
  • TArray<Shape*, TInlineAllocator<16>> MyShapeArray;
  • 前16个添加到数组的元素无动态分配
  • 在17个元素之后,所有元素都被移动到第二分配器(比如堆分配器)以供存储

TInlineAllocator的数组

image.png
  • 内部第二分配器地址为:0x0000043f828d1ce0。 查看内存里面为1 2 3 4


    image.png

TFixedAllocator 的数组

  • TFixedAllocator 没有第二分配器;
  • 如果固定存储没有空间了,会产生错误。


    image.png

内部的2个内存分配器

        // 数量少于NumInlineElements,使用此数组
        TTypeCompatibleBytes<ElementType> InlineData[NumInlineElements];
  
  // 数量超过NumInlineElements, 使用
        typename SecondaryAllocator::template ForElementType<ElementType> SecondaryData;

ResizeAllocation的代码实现

  • 针对不同情况使用InlineData 或 SecondaryData
  • SecondaryData 默认为FDefaultAllocator
        void ResizeAllocation(SizeType PreviousNumElements, SizeType NumElements,SIZE_T NumBytesPerElement)
        {
            // 检测数量是否满足.
            if(NumElements <= NumInlineElements)
            {
                // SecondaryData被使用的情况
                if(SecondaryData.GetAllocation())
                {
                    // 新数据存入InlineData
                    RelocateConstructItems<ElementType>(
                      (void*)InlineData, 
                      (ElementType*)SecondaryData.GetAllocation(), 
                      PreviousNumElements);

                    // 释放老的元素
                    SecondaryData.ResizeAllocation(0,0,NumBytesPerElement);
                }
            }
            else
            {
                // 数量超出时,SecondaryData未开启使用
                if(!SecondaryData.GetAllocation())
                {
                    // 扩容到NumElements
                    SecondaryData.ResizeAllocation(0,NumElements,NumBytesPerElement);

                    // inline数组元素移动到SecondaryData里
                    RelocateConstructItems<ElementType>((void*)SecondaryData.GetAllocation(), 
                      GetInlineElements(), PreviousNumElements);
                }
                else
                {
                    // SecondaryData已启用,扩容到NumElements
                    SecondaryData.ResizeAllocation(PreviousNumElements, NumElements, NumBytesPerElement);
                }
            }
        }

RelocateConstructItems

  • 破坏性的移动内存到自己内部
  • 1 内部判断按情况 直接内存移动
  • 2 执行新容器内的构造和老的析构
  • 2中注意内存的同步移动。++ 和 --
    if constexpr (UE::Core::Private::MemoryOps::TCanBitwiseRelocate<DestinationElementType, SourceElementType>::Value)
    {
        FMemory::Memmove(Dest, Source, sizeof(SourceElementType) * Count);
    }
    else
    {
        while (Count)
        {
            typedef SourceElementType RelocateConstructItemsElementTypeTypedef;

            new (Dest) DestinationElementType(*Source);
            ++(DestinationElementType*&)Dest;
            (Source++)->RelocateConstructItemsElementTypeTypedef::~RelocateConstructItemsElementTypeTypedef();
            --Count;
        }
    }

获取分配器函数

        // FContainerAllocatorInterface
        FORCEINLINE ElementType* GetAllocation() const
        {
            return IfAThenAElseB<ElementType>(SecondaryData.GetAllocation(),GetInlineElements());
        }

相关文章

  • 程序打包

    关于UE5打包问题[https://www.bilibili.com/read/cv11679358] UE5 P...

  • 目录、资产命名规范

    【UE5】目录、资产命名规范[https://zhuanlan.zhihu.com/p/484119115]

  • 地理坐标转换

    关联GIS:条条道路通UE5城[https://zhuanlan.zhihu.com/p/528244402] 关...

  • 通讯

    开源篇-WebSocket搭建UE5通信桥梁[https://zhuanlan.zhihu.com/p/54621...

  • 调试

    UE4/UE5的崩溃,卡死等问题处理[https://zhuanlan.zhihu.com/p/565680732]

  • 源代码

    从零开始:编译UE5 source code[https://www.jianshu.com/p/4a6b8603...

  • UE 命名规范

    资产命名表格链接:UE5项目命名规则[https://link.zhihu.com/?target=https%3...

  • UE5蓝图-动态创建和查找模型

    UE5蓝图-动态创建和查找模型,并控制其显隐性 蓝图 BP_RedEarth 蓝图 BP_CreateRedEar...

  • 【UE5】Nanite解析

    Epic外放的两大特性Nanite跟Lumen,构成了UE版本升级的基石,关于这两大技术,已经有了众多的分享,不过...

  • 【UE5】World Partition

    伯特兰·罗素说他人生由三大激情支配着:对爱的渴望、对知识的探求、对人类痛苦的怜悯 早两天,Epic发布了UE5 E...

网友评论

      本文标题:13 UE5 TInlineAllocator介绍

      本文链接:https://www.haomeiwen.com/subject/rpynadtx.html