LevelDB 之 arena

粒度比较大,实现简洁清晰明了。
对比nginx的,nginx从小到大各种尺寸都有,适用性更好一些。相对要精细很多。

Arena.h
//z 2014-06-05 10:48:50 L.209'47470 BG57IV3 T1840949363.K.F1370514324[T6,L108,R4,V118]
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#ifndef STORAGE_LEVELDB_UTIL_ARENA_H_
#define STORAGE_LEVELDB_UTIL_ARENA_H_

#include <cstddef>
#include <vector>
#include <assert.h>
#include <stdint.h>

namespace leveldb
{
/*//z
思路:
先在上一次分配的内存块中查找是否剩余空间能否容纳当前申请的空间;如果足以容纳,直接使用剩余空间
否则视其大小重新分配一块空间:如果大于1024,直接分配一块新空间,大小与申请空间大小同
如果小于1024,说明上一块空间剩余空间小于1024,那么重新分配一块4096大小的空间,使用该空间来存放待申请的空间
*/
class Arena
{
public:
	Arena();
	~Arena();

	// Return a pointer to a newly allocated memory block of "bytes" bytes.
	char* Allocate(size_t bytes);

	// Allocate memory with the normal alignment guarantees provided by malloc
	char* AllocateAligned(size_t bytes);

	// Returns an estimate of the total memory usage of data allocated
	// by the arena (including space allocated but not yet used for user
	// allocations).
	size_t MemoryUsage() const
	{
		return blocks_memory_ + blocks_.capacity() * sizeof(char*);
	}

private:
	char* AllocateFallback(size_t bytes);
	char* AllocateNewBlock(size_t block_bytes);

	// Allocation state
	char* alloc_ptr_;
	size_t alloc_bytes_remaining_;

	// Array of new[] allocated memory blocks
	std::vector<char*> blocks_;

	// Bytes of memory in blocks allocated so far
	size_t blocks_memory_;

	// No copying allowed
	Arena(const Arena&);
	void operator=(const Arena&);
};

inline char* Arena::Allocate(size_t bytes)
{
	// The semantics of what to return are a bit messy if we allow
	// 0-byte allocations, so we disallow them here (we don't need
	// them for our internal use).
	assert(bytes > 0);
	//z 在一块直接分配好的内存上移动一下指针即可;事实上可能会存在很多的浪费。
	if (bytes <= alloc_bytes_remaining_)
	{
		char* result = alloc_ptr_;
		alloc_ptr_ += bytes;
		alloc_bytes_remaining_ -= bytes;
		return result;
	}

	//z 如果剩余控件不足与容纳,那么根据bytes大小决定是否重新分配一块标准大小的内存(4096),或者要是bytes大于1024,直接
	//z 分配其大小的内存,原标准块内存仍旧有用。
	//z 如果小于 1024 ,说明原标准块剩余内存不足以容纳1024,新分配一块标准块内存,用此来为bytes分配对应内存。
	return AllocateFallback(bytes);
}

}

#endif  // STORAGE_LEVELDB_UTIL_ARENA_H_

//z 2014-06-05 10:48:50 L.209'47470 BG57IV3 T1840949363.K.F1370514324[T6,L108,R4,V118]
arena.cc
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include "util/arena.h"
#include <assert.h>

namespace leveldb {
	//z 常量变量名k开头
	static const int kBlockSize = 4096;

	//z 初始化
	Arena::Arena() {
		blocks_memory_ = 0;
		alloc_ptr_ = NULL;  // First allocation will allocate a block
		alloc_bytes_remaining_ = 0;
	}

	Arena::~Arena() {
		//z 删除申请的内存
		for (size_t i = 0; i < blocks_.size(); i++) {
			delete[] blocks_[i];
		}
	}

	char* Arena::AllocateFallback(size_t bytes) {
		//z 如果申请的bytes > 1024
		if (bytes > kBlockSize / 4) {
			// Object is more than a quarter of our block size.  Allocate it separately
			// to avoid wasting too much space in leftover bytes.
			char* result = AllocateNewBlock(bytes);
			return result;
		}

		// We waste the remaining space in the current block.
		//z 不大于1024时,分配一块标准大小的内存
		alloc_ptr_ = AllocateNewBlock(kBlockSize);
		alloc_bytes_remaining_ = kBlockSize;

		//z 指定返回的位置
		char* result = alloc_ptr_;
		//z 移动指针位置至空闲内存开始的地方
		alloc_ptr_ += bytes;
		//z 记录还剩下多少内存
		alloc_bytes_remaining_ -= bytes;
		return result;
	}

	char* Arena::AllocateAligned(size_t bytes) {
		//z 这个值是固定的,不用每次都求一次?但是代价非常小,求一次也无所为?
		const int align = sizeof(void*);    // We'll align to pointer size
		assert((align & (align-1)) == 0);   // Pointer size should be a power of 2
		//z 求的其余数
		size_t current_mod = reinterpret_cast<uintptr_t>(alloc_ptr_) & (align-1);
		size_t slop = (current_mod == 0 ? 0 : align - current_mod);
		size_t needed = bytes + slop;
		char* result;
		//z 如果剩余的空间足以容纳所需要的内存
		if (needed <= alloc_bytes_remaining_) {
			//z 对齐返回地址
			result = alloc_ptr_ + slop;
			alloc_ptr_ += needed;
			alloc_bytes_remaining_ -= needed;
		} else {
			// AllocateFallback always returned aligned memory
			//z 否则直接分配一块新的内存
			//z 在这种情况下这块内存可能很小
			result = AllocateFallback(bytes);
		}
		//z 确保返回地址是对齐的
		assert((reinterpret_cast<uintptr_t>(result) & (align-1)) == 0);
		return result;
	}
	
	//z 在不小于一个page的时候,直接采用这种方式
	char* Arena::AllocateNewBlock(size_t block_bytes) {
		char* result = new char[block_bytes];
		blocks_memory_ += block_bytes;
		blocks_.push_back(result);
		return result;
	}

}
//z 2014-06-05 10:48:50 L.209'47470 BG57IV3 T1840949363.K.F1370514324[T6,L108,R4,V118]

posted @ 2014-06-05 10:45  BiG5  阅读(233)  评论(0编辑  收藏  举报