按模板类划分的自动内存池功能
Automatic memory pool feature by template class
故事的延续。
考虑安全软件,其中不允许使用碎片进行动态分配。仅当类显式定义运算符new
和delete
以避免碎片时,才允许动态分配。
现在,我们有机会为我们想要获得的任何类优化运算符new
和delete
的显式定义。乍一看,任何类都可以从中继承的模板类是最终用户使用的最简单、最清晰的方法。
template<class T, unsigned size>
class MemoryPool
{
private:
struct Pool
{
bool allocated __attribute__ ((aligned (sizeof(void*))));
uint8_t memory[sizeof(T)] __attribute__ ((aligned (sizeof(void*))));
};
static std::array<uint8_t[sizeof(Pool)], size> memoryPool;
public:
void* operator new(std::size_t) noexcept
{
T* ret = nullptr;
for(auto it = memoryPool.begin(); it != memoryPool.end(); ++it)
{
/* ... */
}
return ret;
}
void operator delete(void* ptr) noexcept
{
for(auto it = memoryPool.begin(); it != memoryPool.end(); ++it)
{
/* ... */
}
}
};
如果这是一个童话故事,用记忆池宣布上课会很甜蜜。
class Interface
{/*...*/};
class Foo : public Interface, MemoryPool<Foo, 8>
{/*...*/};
class Bar : public Interface
{/*...*/};
和对象的声明:
Foo* foo = new Foo(); // goes to dedicated memory pool
Bar* bar = new Bar(); // fails on build
但即使static std::array<uint8_t[sizeof(Pool)], size> memoryPool;
是静态的并且会去类之外,所以它不会改变类的大小,编译器也会抱怨Foo
不完整,无法推断出Foo
的大小
src/metal/dynamic.hpp:14:24: error: invalid application of 'sizeof' to incomplete type 'Foo'
uint8_t memory[sizeof(T)] __attribute__ ((aligned (sizeof(void*))));
是否可以解决此"不完整类型"错误?
还是我应该完全重新设计解决方案?
问题是Foo
在实例化MemoryPool
时还不是完整的类型。
Clang 的错误消息非常具体:
main.cpp:10:24: error: invalid application of 'sizeof' to an incomplete type 'Foo'
uint8_t memory[sizeof(T)] __attribute__ ((aligned (sizeof(void*))));
^~~~~~~~~
main.cpp:13:31: note: in instantiation of member class 'MemoryPool<Foo, 8>::Pool' requested here
static std::array<uint8_t[sizeof(Pool)], size> memoryPool;
^
main.cpp:35:20: note: in instantiation of template class 'MemoryPool<Foo, 8>' requested here
class Foo : public MemoryPool<Foo, 8>
^
main.cpp:35:7: note: definition of 'Foo' is not complete until the closing '}'
class Foo : public MemoryPool<Foo, 8>
^
您可以通过将应用程序sizeof
延迟到Foo
是完整的类型来解决此问题。
这可以通过在静态成员函数访问memoryPool
来完成:
template<class T, unsigned size>
class MemoryPool
{
private:
struct Pool
{
bool allocated __attribute__ ((aligned (sizeof(void*))));
uint8_t memory[sizeof(T)] __attribute__ ((aligned (sizeof(void*))));
};
template <typename P>
static std::array<uint8_t[sizeof(P)], size>& getPool()
{
static std::array<uint8_t[sizeof(P)], size> memoryPool;
return memoryPool;
}
public:
void* operator new(std::size_t) noexcept
{
T* ret = nullptr;
for(auto it = getPool<Pool>().begin(); it != getPool<Pool>().end(); ++it)
{
/* ... */
}
return ret;
}
};
现场示例
C++14 允许稍微简单的实现:
template<class T, unsigned size>
class MemoryPool
{
private:
struct Pool
{
bool allocated __attribute__ ((aligned (sizeof(void*))));
uint8_t memory[sizeof(T)] __attribute__ ((aligned (sizeof(void*))));
};
static auto& getPool()
{
static std::array<uint8_t[sizeof(Pool)], size> memoryPool;
return memoryPool;
}
public:
void* operator new(std::size_t) noexcept
{
T* ret = nullptr;
for(auto it = getPool().begin(); it != getPool().end(); ++it)
{
/* ... */
}
return ret;
}
};
现场示例
相关文章:
- 带内存和隔离功能的SQLite
- 如何使用 MPI 的远程内存访问 (RMA) 功能并行化数据聚合?
- C++功能泄漏内存,我是C++新手,不确定如何解决
- OPENCL-如何使辅助功能返回阵列并将内存的部分从恒定内存空间转移到私有
- 是否可以在内存中修改功能
- 如何停止功能中的内存泄漏
- 将参数传递给const在内存中的副本的功能
- 有没有办法获得功能大小并分配内存以复制和执行
- 列出当前过程并使用未记录的当前功能编写内存的软件技巧
- 模板功能以操纵内存
- c++ std map 的擦除功能是否释放了指针键的内存?
- 按模板类划分的自动内存池功能
- 代码ARP数组,指针和内存分配(Windows IP功能)的不可理解的部分
- P/调用C 功能后清理内存
- 跟踪每个功能的内存分配
- 功能中的内存泄漏,返回指针
- 调整数组大小功能不会更改内存位置
- 删除在另一个功能中分配的内存
- 通过功能提升共享内存中的C++结构
- C++ 在 Windows 上:获取分配内存的功能