main.cpp

来自「一个内存池的allocator,参考stl源码的内存池实现」· C++ 代码 · 共 46 行

CPP
46
字号

#include "memory_pool.hpp"
#include <iostream>
#include "shm_alloc.hpp"
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;
using namespace happycode;

template< class T >
void print(T i) {cout << i << "  ";};

int main()
{
	char *heap_mem = new char[10485760];

	MemoryPool<256,512> mempool;

	try {
	mempool.init_mem(heap_mem,10485760);
	} catch(mempool_init_error &e) {
		cerr << e.what() << endl;
	}


	shm_allocator<int,256,512>::mempoolptr = &mempool;

	vector< int,shm_allocator<int,256,512> > test(40);
	test.push_back(1);
	test.push_back(10);
	test.push_back(5);
	test.push_back(3);
	cout << mempool.dump() << endl;  //use 256 block 4
	
	sort(test.begin(),test.end());
	for_each(test.begin(),test.end(),print<int>);

	cout << mempool.dump() << endl;  //use 256 block 4
	
	return 0;
}



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?