// (c) 2003-2007 Electronic Arts Inc.

#include    "spit.h"
#include    "falloc.h"
#include    "stdgimex.h"

#include    "malloc.h"

#if defined(_MSC_VER)
#pragma warning(push,1)
#include <windows.h>
#pragma warning(pop)
#endif

__forceinline __int64 GetCycle64(void) { __asm rdtsc }

#define USE_MALLOC 1
#define USE_ALLOCA 0

main()
{
    FAlloc FHeap;

    const int count = 100;

//  int sbh = _set_sbh_threshold(1016);

#if defined(_MSC_VER)
    SetThreadPriority(0, THREAD_PRIORITY_ABOVE_NORMAL);
#endif

    __int64 t;

    for (int pass=0; pass<2; ++pass)
    {
        t = GetCycle64();

        for (int i=0; i<count; ++i)
        {
            size_t BlockSize=(i*1234)&255;
    #if USE_MALLOC
            void *p = malloc(BlockSize);
    #elif USE_ALLOCA
            void *p = alloca(BlockSize);
    #else
            void *p = FHeap.Alloc(BlockSize);
    #endif

    #ifdef DEBUG
            printf("%x[%d] ",p,BlockSize);
    #endif

    #if USE_MALLOC
            free(p);
    #else
            FHeap.Free(p);
    #endif

        }

        t = GetCycle64()-t;
    }
    printf("took %I64d %.2f \n",t,(double)t/count);

    void *p = FHeap.Alloc(40000);

#ifdef DEBUG
    printf("\nFAlloc GetCount %d\n",FHeap.GetCount());
    FHeap.Dump();

#endif
}

