char* CoinMempool::alloc() { lock_mutex(); if (first_free_ == NULL) { unlock_mutex(); char* block = allocate_new_block(); lock_mutex(); #if (COIN_MEMPOOL_SAVE_BLOCKHEADS==1) // see if we can record another block head. If not, then resize // block_heads if (max_block_num_ == block_num_) { max_block_num_ = 2 * block_num_ + 10; char** old_block_heads = block_heads_; block_heads_ = (char**)malloc(max_block_num_ * sizeof(char*)); CoinMemcpyN( old_block_heads,block_num_,block_heads_); free(old_block_heads); } // save the new block block_heads_[block_num_++] = block; #endif // link in the new block *(char**)(block+((last_block_size_-1)*entry_size_)) = first_free_; first_free_ = block; } char* p = first_free_; first_free_ = *(char**)p; unlock_mutex(); return p; }
Block* get_block(uint32_t chunk_size) { const int index = GET_POOL_INDEX(chunk_size); assert(index >= 0 && index < sizeof(block_pools)/sizeof(*block_pools)); if(block_pools[index]) { return block_pools[index]; } // free memory from other threads and then try again. This requires a mutex // lock, but this code should be rarely reached. collect_memory_from_other_threads(); if(block_pools[index]) { return block_pools[index]; } Block* block = allocate_new_block(chunk_size); if(block == NULL) { return block; } add_block_to_pool(block); return block; }