Skip to content

Instantly share code, notes, and snippets.

@yewton
Created May 27, 2012 09:56
Show Gist options
  • Save yewton/2803145 to your computer and use it in GitHub Desktop.
Save yewton/2803145 to your computer and use it in GitHub Desktop.
Pool
#include "A.hpp"
#include <iostream>
A::A() { }
A::~A() { }
void A::execute() { }
Pool<A> A::merePool(sizeof(A));
#ifndef A_H_
#define A_H_
#include "Pool.hpp"
class A {
public:
A ();
~A ();
void execute();
static void * operator new(size_t size) {
return merePool.alloc(size);
}
static void operator delete(void *p, size_t size) {
merePool.free(p, size);
}
private:
static Pool<A> merePool;
};
#endif
#include "B.hpp"
#include <iostream>
B::B() { }
B::~B() { }
void B::execute() { }
#ifndef B_H_
#define B_H_
class B {
public:
B ();
~B ();
void execute();
};
#endif
#include "A.hpp"
#include "B.hpp"
#include <iostream>
#include <vector>
#include <sys/time.h>
using namespace std;
inline double get_dtime(void){
struct timeval tv;
gettimeofday(&tv, NULL);
return ((double)(tv.tv_sec) + (double)(tv.tv_usec) * 0.001 * 0.001);
}
int main() {
vector<A*> va;
double start, end;
start = get_dtime();
for(int i = 0; i < 100000; i++) {
va.push_back(new A);
}
while(va.begin() != va.end()) {
delete va.back();
va.pop_back();
}
for(int i = 0; i < 100000; i++) {
va.push_back(new A);
}
while(va.begin() != va.end()) {
delete va.back();
va.pop_back();
}
end = get_dtime();
cout << (end - start) << endl;
vector<B*> vb;
start = get_dtime();
for(int i = 0; i < 100000; i++) {
vb.push_back(new B);
}
while(vb.begin() != vb.end()) {
delete vb.back();
vb.pop_back();
}
for(int i = 0; i < 100000; i++) {
vb.push_back(new B);
}
while(vb.begin() != vb.end()) {
delete vb.back();
vb.pop_back();
}
end = get_dtime();
cout << (end - start) << endl;
return 0;
}
#ifndef POOL_H_
#define POOL_H_
#include <cstddef>
#include <vector>
#include <iostream>
template<class T>
class Pool {
public:
Pool(std::size_t n);
void * alloc(std::size_t n);
void free(void *p, std::size_t n);
~Pool();
private:
std::size_t obj_size;
static std::vector<T*> freeList;
static const int BLOCK_SIZE;
};
template<class T>
Pool<T>::Pool(std::size_t n) : obj_size(n) {
freeList.reserve(BLOCK_SIZE);
}
template<class T>
void * Pool<T>::alloc(std::size_t n) {
if (n != obj_size)
return ::operator new(n);
T* p;
if (!freeList.empty()) {
p = freeList.back();
freeList.pop_back();
} else {
T *newBlock =
static_cast<T*>(::operator new(BLOCK_SIZE * sizeof(T)));
for(int i = 1; i < BLOCK_SIZE; ++i) {
freeList.push_back(&newBlock[i]);
}
p = newBlock;
}
return p;
}
template<class T>
void Pool<T>::free(void *p, std::size_t n) {
if (p == 0) return;
if (n != obj_size) {
::operator delete(p);
return;
}
T *carcass =
static_cast<T*>(p);
freeList.push_back(carcass);
}
template<class T>
Pool<T>::~Pool() { }
template<class T>
std::vector<T*> Pool<T>::freeList;
template<class T>
const int Pool<T>::BLOCK_SIZE = 512;
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment