当前位置: 首页>>代码示例>>C++>>正文


C++ MemoryContext::resetToSavepoint方法代码示例

本文整理汇总了C++中MemoryContext::resetToSavepoint方法的典型用法代码示例。如果您正苦于以下问题:C++ MemoryContext::resetToSavepoint方法的具体用法?C++ MemoryContext::resetToSavepoint怎么用?C++ MemoryContext::resetToSavepoint使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MemoryContext的用法示例。


在下文中一共展示了MemoryContext::resetToSavepoint方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: testMemoryContext

void MemoryContextTestCase::testMemoryContext() {
	MemoryContext *mc = new MemoryContext(4096, 1);

	u64 firstSavepoint = mc->setSavepoint();

	int chunkSize = 700;
	void *first = mc->alloc(chunkSize);
	memset(first, 0, chunkSize);	// 便于valgrind检测有无越界写问题
	for (int i = 0; i < 12; i++) {
		void *chunk = mc->alloc(chunkSize);
		memset(chunk, 0, chunkSize);
	}
	u64 savepoint = mc->setSavepoint();
	void *firstAfterSavepoint = mc->alloc(chunkSize);
	for (int i = 0; i < 12; i++) {
		void *chunk = mc->alloc(chunkSize);
		memset(chunk, 0, chunkSize);
	}
	// 分配超出页面大小的块
	for (int i = 0; i < 3; i++)
		mc->alloc(8000);
	mc->resetToSavepoint(savepoint);
	CPPUNIT_ASSERT(mc->setSavepoint() == savepoint);
	CPPUNIT_ASSERT(mc->alloc(chunkSize) == firstAfterSavepoint);

	mc->reset();
	CPPUNIT_ASSERT(mc->setSavepoint() == firstSavepoint);
	CPPUNIT_ASSERT(mc->alloc(chunkSize) == first);	// 第一页始终保持因此地址不变
	mc->resetToSavepoint(0);
	CPPUNIT_ASSERT(mc->setSavepoint() == firstSavepoint);
	CPPUNIT_ASSERT(mc->alloc(chunkSize) == first);

	// 测试getMemUsage
	{
		mc->reset();
		for (int i = 0; i < 12; i++)
			mc->alloc(chunkSize);
		CPPUNIT_ASSERT(mc->getMemUsage() > chunkSize * 12);
		u64 muBak = mc->getMemUsage();
		u64 savepoint = mc->setSavepoint();
		for (int i = 0; i < 12; i++)
			mc->alloc(chunkSize);
		CPPUNIT_ASSERT(mc->getMemUsage() > chunkSize * 24);
		mc->resetToSavepoint(savepoint);
		CPPUNIT_ASSERT(mc->getMemUsage() == muBak);
		mc->reset();
		CPPUNIT_ASSERT(mc->getMemUsage() < muBak);
	}
	delete mc;
}
开发者ID:github188,项目名称:mysql-5.5.30,代码行数:50,代码来源:TestMemoryContext.cpp

示例2: testMemoryContextUsePool

void MemoryContextTestCase::testMemoryContextUsePool() {
	uint poolSize = 4096;
	uint reservedPages = 1;
	PagePool pool(1, NTSE_PAGE_SIZE);
	CommonMemPool commonMemPool(poolSize, &pool);
	pool.registerUser(&commonMemPool);
	pool.init();

	MemoryContext *mc = new MemoryContext(&commonMemPool, reservedPages);

	u64 firstSavepoint = mc->setSavepoint();

	int chunkSize = 700;
	void *first = mc->alloc(chunkSize);
	memset(first, 0, chunkSize);	// 便于valgrind检测有无越界写问题
	for (int i = 0; i < 12; i++) {
		void *chunk = mc->alloc(chunkSize);
		memset(chunk, 0, chunkSize);
	}
	u64 savepoint = mc->setSavepoint();
	void *firstAfterSavepoint = mc->alloc(chunkSize);
	for (int i = 0; i < 12; i++) {
		void *chunk = mc->alloc(chunkSize);
		memset(chunk, 0, chunkSize);
	}
	// 分配超出页面大小的块
	for (int i = 0; i < 3; i++)
		NTSE_ASSERT(NULL == mc->alloc(NTSE_PAGE_SIZE + 1));
	mc->resetToSavepoint(savepoint);
	CPPUNIT_ASSERT(mc->setSavepoint() == savepoint);
	CPPUNIT_ASSERT(mc->alloc(chunkSize) == firstAfterSavepoint);

	// 测试内存池内存不足
	mc->reset();
	for (int i = 0; i < poolSize; i++) {
		int cs = NTSE_PAGE_SIZE / 2 + 100;
		void *data = mc->alloc(cs);
		NTSE_ASSERT(NULL != data);
		memset(data, 0, cs);
	}

	NTSE_ASSERT(poolSize == commonMemPool.getCurrentSize());
	for (int i = 0; i < 10; i++) {
		int cs = NTSE_PAGE_SIZE / 2 + 100;
		void *data = mc->alloc(cs);
		NTSE_ASSERT(NULL == data);
		NTSE_ASSERT(poolSize == commonMemPool.getCurrentSize());
	}

	mc->reset();
	CPPUNIT_ASSERT(mc->setSavepoint() == firstSavepoint);
	CPPUNIT_ASSERT(mc->alloc(chunkSize) == first);	// 第一页始终保持因此地址不变
	mc->resetToSavepoint(0);
	CPPUNIT_ASSERT(mc->setSavepoint() == firstSavepoint);
	CPPUNIT_ASSERT(mc->alloc(chunkSize) == first);

	// 测试getMemUsage
	{
		mc->reset();
		for (int i = 0; i < 12; i++)
			mc->alloc(chunkSize);
		CPPUNIT_ASSERT(mc->getMemUsage() > chunkSize * 12);
		u64 muBak = mc->getMemUsage();
		u64 savepoint = mc->setSavepoint();
		for (int i = 0; i < 12; i++)
			mc->alloc(chunkSize);
		CPPUNIT_ASSERT(mc->getMemUsage() > chunkSize * 24);
		mc->resetToSavepoint(savepoint);
		CPPUNIT_ASSERT(mc->getMemUsage() == muBak);
		mc->reset();
		CPPUNIT_ASSERT(mc->getMemUsage() < muBak);
	}
	delete mc;
	mc = NULL;

	// 测试预留页面数
	for (uint i = 1; i <= 10; i++) {	
		mc = new MemoryContext(&commonMemPool, i);

		uint lastUsedPages = commonMemPool.getCurrentSize();
		
		for (uint j = 0; j < i; j++) {
			int cs = NTSE_PAGE_SIZE / 2 + 100;
			void *data = mc->alloc(cs);
			memset(data, 0, cs);
		}

		NTSE_ASSERT(lastUsedPages == commonMemPool.getCurrentSize());

		mc->reset();
		delete mc;
		mc = NULL;
	}
}
开发者ID:github188,项目名称:mysql-5.5.30,代码行数:94,代码来源:TestMemoryContext.cpp


注:本文中的MemoryContext::resetToSavepoint方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。