// Iterate ’i" through all chunks in the memory block for (char * i = iter.begin(); i != iter.end(); i += partition_size) { // If this chunk is free if (i == freed_iter) { // Increment freed_iter to point to next in free list freed_iter = nextof(freed_iter);
// Continue searching chunks in the memory block continue; }
// This chunk is not free (allocated), so call its destructor static_cast<T *>(static_cast<void *>(i))->~T(); // and continue searching chunks in the memory block }
// increment iter iter = next; } while (iter.valid());
// Make the block list empty so that the inherited destructor doesn’t try to // free it again. this->list.invalidate(); }这段代码不难理解,object_pool遍历所有申请的内存块(MemBlock),并遍历其中所有结点(Node),如果该结点不出现在自由内存结点(FreeNode)的列表(FreeNodeList)中,那么,它就是用户未主动释放的结点,需要进行相应的析构操作。现在你明白了,ordered_malloc是为了让MemBlockList中的MemBlock有序,ordered_free是为了让FreeNodeList中的所有FreeNode有序。而MemBlockList, FreeNodeList有序,是为了更快地检测Node是自由的还是被使用的(这实际上是一个集合求交的流程,建议你看看std::set_intersection,它定义在STL的<algorithm>中)。