我正在构建一个简单的粒子系统,并希望使用结构的单个数组缓冲区来管理我的粒子。也就是说,我找不到允许我从任意缓冲区进行 malloc() 和 free() 的 C 函数。这是一些伪代码来表明我的意图:
Particle* particles = (Particle*) malloc( sizeof(Particle) * numParticles );
Particle* firstParticle = <buffer_alloc>( particles );
initialize_particle( firstParticle );
// ... Some more stuff
if (firstParticle->life < 0)
<buffer_free>( firstParticle );
// @ program's end
free(particles);
哪里<buffer_alloc>
和<buffer_free>
是从任意指针分配和释放内存块的函数(可能带有附加元数据,例如缓冲区长度等)。这样的功能是否存在和/或是否有更好的方法来做到这一点?谢谢!
最佳答案
是的,你必须自己编写。它非常简单,确实很愚蠢,但与一直简单地使用 malloc() 和 free() 相比,它的性能会令人尖叫......
static const int maxParticles = 1000;
static Particle particleBuf[maxParticles]; // global static array
static Particle* headParticle;
void initParticleAllocator()
{
Particle* p = particleBuf;
Particle* pEnd = &particleBuf[maxParticles-1];
// create a linked list of unallocated Particles
while (p!=pEnd)
{
*((Particle**)p) = p+1;
++p;
}
*((Particle**)p) = NULL; // terminate the end of the list
headParticle = particleBuf; // point 'head' at the 1st unalloc'ed one
}
Particle* ParticleAlloc()
{
// grab the next unalloc'ed Particle from the list
Particle* ret = headParticle;
if (ret)
headParticle = *(Particle**)ret;
return ret; // will return NULL if no more available
}
void ParticleFree(Particle* p)
{
// return p to the list of unalloc'ed Particles
*((Particle**)p) = headParticle;
headParticle = p;
}
您可以修改上面的方法,根本不从任何全局静态数组开始,并在用户调用 ParticleAlloc() 时首先使用 malloc(),但当返回 Particles 时,不要调用 free() 而是调用将返回的粒子添加到未分配粒子的链表中。然后 ParticleAlloc() 的下一个调用者将从空闲粒子列表中获取一个,而不是使用 malloc()。任何时候空闲列表中不再有空闲列表,您的 ParticleAlloc() 函数就可以回退到 malloc()。或者混合使用这两种策略,这确实是两全其美:如果您知道您的用户几乎肯定会使用至少 1000 个粒子,但有时可能需要更多,您可以从 1000 个静态数组开始,然后如果你用完了,就调用 malloc() 。如果您这样做,则 malloc() 化的不需要特殊处理;当它们返回 ParticleFree() 时,只需将它们添加到未分配的粒子列表中即可。当你的程序退出时,你不需要费心对它们调用 free() ;操作系统将释放进程的整个内存空间,因此任何泄漏的内存都将在此时清除。
我应该提到,由于您的问题被标记为“C”而不是“C++”,所以我以 C 解决方案的形式回答了它。在 C++ 中,实现相同功能的最佳方法是将“operator new”和“operator delete”方法添加到 Particle 类中。它们将包含与我上面显示的基本相同的代码,但它们覆盖(而不是重载)全局“new”运算符,并且仅对于 Particle 类,定义一个专门的分配器来替换全局“new”。很酷的事情是 Particle 对象的用户甚至不必知道有一个特殊的分配器;他们只是像平常一样使用“new”和“delete”,并且幸福地没有意识到他们的 Particle 对象来自一个特殊的预分配池。
关于c - 从C中的缓冲区分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11181162/