// final/test.cpp

#include <assert.h>
#include <stdlib.h>
#include <time.h>
#include <new>
#include <memory>
#include <iostream>
#include "static_mem_pool.h"

#if defined(_STLP_INTERNAL_ALLOC_H)
#define alloc _Node_alloc
#define single_client_alloc __node_alloc<false,0>
#elif defined(__GNUC__)
#if __GNUC__ >= 3
#define alloc __alloc
#define single_client_alloc __single_client_alloc
#endif
#else
#error "STL implementation not recognized"
#endif

#ifndef LOOPS
#define LOOPS 50000000
#endif

#ifndef BLKSIZE
#define BLKSIZE 100
#endif

using namespace std;

void* volatile ptr;

int main()
{
    clock_t t;
    int i;

    t = clock();
    for (i = 0; i < LOOPS; ++i) {
        ptr = alloc::allocate(BLKSIZE);
        alloc::deallocate((void*)ptr, BLKSIZE);
    }
    cout << (clock() - t) * 1.0 / CLOCKS_PER_SEC << endl;

    t = clock();
    for (i = 0; i < LOOPS; ++i) {
        ptr = single_client_alloc::allocate(BLKSIZE);
        single_client_alloc::deallocate((void*)ptr, BLKSIZE);
    }
    cout << (clock() - t) * 1.0 / CLOCKS_PER_SEC << endl;

    t = clock();
    static_mem_pool<BLKSIZE>::instance();
    for (i = 0; i < LOOPS; ++i) {
        ptr = static_mem_pool<BLKSIZE>::instance_known().allocate();
        static_mem_pool<BLKSIZE>::instance_known().deallocate((void*)ptr);
    }
    cout << (clock() - t) * 1.0 / CLOCKS_PER_SEC << endl;

    t = clock();
    static_mem_pool<BLKSIZE, 0>::instance();
    for (i = 0; i < LOOPS; ++i) {
        ptr = static_mem_pool<BLKSIZE, 0>::instance_known().allocate();
        static_mem_pool<BLKSIZE, 0>::instance_known().deallocate((void*)ptr);
    }
    cout << (clock() - t) * 1.0 / CLOCKS_PER_SEC << endl;

    return 0;
}