2017-03-02 54 views
1

电子艺界EASTL图书馆的east::allocator要求用户实施特殊的new操作符(如此sample所示)。这个new运营商有一个const char* name,这应该是用来记录有关内存分配应用程序特定信息执行EASTL容器。该EASTL Best Practices指南还提到,一个人应该“名称集装箱跟踪内存使用情况。如何使用EASTL跟踪内存使用情况?

然而,似乎没有要通过eastl::allocator::deallocate传递任何相应const char* name,所以不会出现是一个内置登录内存的方式取消分配

打算如何使用EASTL调试功能?我想我一定会错过一些东西。 EA软件工程师在2015年也提供了一个相关的presentation,在那里他展示了EA开发的一些用于内存调试的内部工具,似乎表明他们跟踪分配和释放(例如,以确保“游戏级别1“在”游戏等级2“开始时被释放)。

+0

例如,分配器可以分配一个大于请求的块,在开始处存储名称(和/或任何附加信息),并返回指向块剩余部分的指针。然后解除分配器会找到这个信息的偏移量与它给出的指针的负偏移量。 –

回答

0

EASTL的分配器是有状态的并绑定到容器的实例;这意味着他们在实例级别定义:

什么EASTL所做的是用更熟悉的内存分配模式 其中只有一个allocator类接口,它是由 使用的所有容器。此外EASTL容器允许你访问他们 分配器和查询它们,为它们命名,改变他们,等

EASTL却偏偏让分配器不能集装箱交换期间容器 之间复制和分配操作。这意味着如果 容器A与容器B交换其内容,则两个容器 都保留其原始分配器。同样,将容器A分配给 容器B会导致容器B保留其原始分配器。 相当的容器应该通过运算符==报告;如果分配器相等,EASTL 将执行智能交换,否则将进行蛮力交换 。

https://github.com/questor/eastl/blob/master/doc/EASTL%20Design.html

所以我会成员添加到allocator类和跟踪内存计数里面,像以下:

#ifndef EASTL_CUSTOM_ALLOCATOR_H_ 
#define EASTL_CUSTOM_ALLOCATOR_H_ 

#include "new_implementation.hpp" 
#include <EASTL/list.h> 
#include <iostream> 

#define DEBUG_MACRO 

class EASTL_CustomAllocator { 
public: 
    EASTL_CustomAllocator(const char* pName = EASTL_NAME_VAL(EASTL_ALLOCATOR_DEFAULT_NAME)) 
     : m_pName(pName), m_totalAmountOfBytesAllocated(0) { 
#ifdef DEBUG_MACRO 
     std::cout << m_pName << ": default construct allocator" << std::endl; 
#endif 
    } 
    EASTL_CustomAllocator(const EASTL_CustomAllocator& x) 
     : m_pName(x.m_pName), 
      m_totalAmountOfBytesAllocated(x.m_totalAmountOfBytesAllocated) { 
#ifdef DEBUG_MACRO 
     std::cout << m_pName << ": copy construct allocator" << std::endl; 
#endif 
    } 
    EASTL_CustomAllocator(const EASTL_CustomAllocator& x, const char* pName) 
     : m_pName(pName), 
      m_totalAmountOfBytesAllocated(x.m_totalAmountOfBytesAllocated) { 
#ifdef DEBUG_MACRO 
     std::cout << m_pName << ": copy construct allocator" << std::endl; 
#endif 
    } 

    EASTL_CustomAllocator& operator=(const EASTL_CustomAllocator& x) { 
#ifdef DEBUG_MACRO 
     std::cout << m_pName << ": copy assignment" << std::endl; 
#endif 
     m_pName = x.m_pName; 
     m_totalAmountOfBytesAllocated = x.m_totalAmountOfBytesAllocated; 
     return *this; 
    } 

    void* allocate(size_t num_of_bytes, int flags = 0) { 
     m_totalAmountOfBytesAllocated += num_of_bytes; 
     void* p = ::new((char*)0, flags, 0, (char*)0,  0) char[num_of_bytes]; 
#ifdef DEBUG_MACRO 
     std::cout << m_pName << ": allocate " << num_of_bytes << " bytes" << " at: " << (void*) p << std::endl; 
#endif 
     return p; 
    } 
    void* allocate(size_t num_of_bytes, size_t alignment, size_t offset, int flags = 0) { 
     m_totalAmountOfBytesAllocated += num_of_bytes; 
     void* p = ::new(alignment, offset, (char*)0, flags, 0, (char*)0,  0) char[num_of_bytes]; 
#ifdef DEBUG_MACRO 
     std::cout << m_pName << ": allocate " << num_of_bytes << " bytes" << " at: " << (void*) p << std::endl; 
#endif 
     return p; 
    } 
    void deallocate(void* p, size_t num_of_bytes) { 
     m_totalAmountOfBytesAllocated -= num_of_bytes; 
#ifdef DEBUG_MACRO 
     std::cout << m_pName << ": deallocate " << num_of_bytes << " bytes" << " at: " << (void*) p << std::endl; 
#endif 
     delete[](char*)p; 
    } 

    const char* get_name() const { 
     return m_pName; 
    } 
    void  set_name(const char* pName) { 
     m_pName = pName; 
    } 
    size_t get_totalAmountOfBytesAllocated() const { 
     return m_totalAmountOfBytesAllocated; 
    } 

protected: 
    const char* m_pName; // Debug name, used to track memory. 
    size_t m_totalAmountOfBytesAllocated; // keeps track of the memory currently allocated 
}; 


bool operator==(const EASTL_CustomAllocator& a, const EASTL_CustomAllocator& b) { 
    if (&a == &b) { 
     return true; // allocator a and b are equal if they are the same 
    } 
    else { 
     return false; // otherwhise, return false, because the state m_totalAmountOfBytesAllocated needs to be increased/decreased on splice and swap 
    } 
} 
bool operator!=(const EASTL_CustomAllocator& a, const EASTL_CustomAllocator& b) { 
    return false; 
} 


#endif /* EASTL_CUSTOM_ALLOCATOR_H_ */ 

传递自定义分配器类型作为模板参数一个像下面这样的eastl容器(你也可以在构造时用一个用户定义的名字来设置一个实例,甚至以后可以通过set_allocator()):

eastl::list<int, EASTL_CustomAllocator> 
list(EASTL_CustomAllocator("EASTL Some Name")); 

但我不确定打算如何使用调试功能。

相关问题