2 c memory-management memory-pool
假设,出于问题的目的,我们有一个内存池,最初分配了n 个块。然而,当达到容量时,池想要增长并变为原来大小的两倍 ( 2n )。
现在可以在 C 中完成此调整大小操作realloc
,但是函数本身可能返回指向不同内存的指针(复制了旧数据)。
这意味着内存池分配器返回的指针可能不再有效(因为内存可能已被移动)。
克服这个问题的好方法是什么?或者说这根本有可能吗?
从多个不连续的内存池中分配。当一个池已满时,分配第二个池,使其位于虚拟地址空间中的其他位置。
Then the problem is one of keeping track of where your pools are. Typically you'd use some of the space in each pool for bookkeeping. For example, you might reserve one pointer's worth of space to keep a simple linear linked list of all the pools. More sophisticated allocators tend to require more bookkeeping overhead.