// 内存块的链表组织结构 // Blocks are variable length malloc-ed objects. The following structure // describes the common header for all blocks. structBlock { void* owner; // &ThreadCache of thread that owns this block, or // &this->owner if not yet owned by a thread. Block* next; // Next block in arena (may have different owner) // ((char*) &block) + pos is next available byte. It is always // aligned at a multiple of 8 bytes. size_t pos; size_t size; // total size of the block. GOOGLE_ATTRIBUTE_ALWAYS_INLINE size_tavail()const{ return size - pos; } // data follows };
// Add first unowned block to list. Block* first_block = reinterpret_cast<Block*>(options_.initial_block); first_block->size = options_.initial_block_size; first_block->pos = kHeaderSize; first_block->next = NULL; // Thread which calls Init() owns the first block. This allows the // single-threaded case to allocate on the first block without taking any // locks. first_block->owner = &thread_cache(); SetThreadCacheBlock(first_block); AddBlockInternal(first_block); owns_first_block_ = false; }