#include <ios.h>
#include <hw.h>
#include <ioslibc.h>

#define STACK_SIZE 1024
const u8 _initStack[STACK_SIZE];
const u32 _initStackSize = sizeof(_initStack);
const u32 _initPriority = 10;
int i, j, k;

#define ALLOC(s) IOS_Alloc(hid, s)
#define ALLOC_ALIGN(s, a) IOS_AllocAligned(hid, s, a)

#define GOOD_ALLOC(p, size) \
    if (((p) = ALLOC(size)) == 0) { \
        printf("Alloc of %d bytes failed\n", (size)); \
        exit(-1); \
    }

#define GOOD_ALLOC_ALIGN(p, size, a) \
    if (((p) = ALLOC_ALIGN(size, a)) == 0 || \
        (((u32)(p) & ((a)-1)) != 0)) { \
        printf("Alloc of %d bytes failed\n", (size)); \
        exit(-1); \
    }

#define BAD_ALLOC(p, size) \
    if (((p) = ALLOC(size)) != 0) { \
        printf("Alloc of %d bytes succeeded\n", (size)); \
        exit(-1); \
    }

#define BAD_ALLOC_ALIGN(p, size, a) \
    if (((p) = ALLOC_ALIGN(size, a)) != 0) { \
        printf("Alloc of %d bytes succeeded\n", (size)); \
        exit(-1); \
    }

#define GOOD_FREE(p) \
    if ((rv = IOS_Free(hid, p)) != IOS_ERROR_OK) { \
        printf("Free failed: %d\n", rv); \
        exit(-1); \
    }

#define BAD_FREE(p) \
    if ((rv = IOS_Free(hid, p)) == IOS_ERROR_OK) { \
        printf("Free succeeded: %d\n", rv); \
        exit(-1); \
    }

#define HEAP_SIZE (64*1024)
u8 heap[HEAP_SIZE] __attribute__((aligned(32)));

void testAllocator(u32 pageSize)
{
    u32 *p=0, *q, *r;
    int i, heapSize;
    int size;
    IOSError rv;

    printf("\nAlloc Test: %d byte pages\n\n", pageSize);

    printf("Create unaligned heap\n");
    rv = IOS_CreateHeap(heap+1, sizeof(heap));
    if (rv != IOS_ERROR_INVALID) {
        printf("Able to create heap: %d\n", rv);
        exit(-1);
    }
    printf("OK\n");

    printf("Create heap over unowned buffer\n");
    rv = IOS_CreateHeap(0, 256);
    if (rv != IOS_ERROR_ACCESS) {
        printf("Able to create heap: %d\n", rv);
        printf("XXX No access controls for now\n");
    }
    printf("OK\n");

    IOSHeapId hid = IOS_CreateHeap(heap, sizeof(heap));
    if (hid < 0) {
        printf("Unable to create heap: %d\n", hid);
        exit(-1);
    }
    printf("Heap id: %d\n", hid);
    for (i = HEAP_SIZE; i > 0; i -= pageSize) {
        if ((p = ALLOC(i))) {
            break;
        }
    }
    heapSize = i;
    if (p) {
        printf("Heap size: %d bytes @ %08x\n", heapSize, p);
    } else {
        printf("Unable to allocate any memory\n");
        exit(-1);
    }

    printf("Attempt to allocate even more memory: ");
    BAD_ALLOC(q, pageSize);
    printf("OK (allocation failed)\n");

    printf("Free block @ %08x: ", p);
    GOOD_FREE(p);
    printf("OK\n");

    printf("Free block again @ %08x: ", p);
    BAD_FREE(p);
    printf("OK\n");
    
    printf("Free bad address @ %08x: ", p+1);
    BAD_FREE(p+1);
    printf("OK\n");
    
    printf("Free 0: ");
    BAD_FREE(0);
    printf("OK\n");

    printf("Allocate 0 bytes: ");
    BAD_ALLOC(p, 0);
    printf("OK\n");
    printf("Allocate entire heap again (%d bytes): ", heapSize);
    GOOD_ALLOC(p, heapSize);
    printf("OK\n");

    printf("Free block @ %08x: ", p);
    GOOD_FREE(p);
    printf("OK\n");

    printf("Allocate entire heap in two chunks (%d bytes): ", heapSize/2-pageSize);
    GOOD_ALLOC(p, heapSize/2 - pageSize);
    GOOD_ALLOC(q, heapSize/2 - pageSize);
    printf("OK\n");

    printf("Free blocks @ %08x, %08x: ", p, q);
    GOOD_FREE(p);
    GOOD_FREE(q);
    printf("OK\n");

    size = (heapSize + pageSize)/3 - 2*pageSize;
    printf("Allocate entire heap in three chunks (%d bytes): ", size);
    GOOD_ALLOC(p, size);
    GOOD_ALLOC(q, size);
    GOOD_ALLOC(r, size);
    printf("OK\n");

    printf("Free blocks @ %08x, %08x, %08x: ", p, q, r);
    GOOD_FREE(p);
    GOOD_FREE(q);
    GOOD_FREE(r);
    printf("OK\n");
    
    printf("Build a linked list using the entire heap %d bytes at a time:\n", pageSize);
    printf("Expect number of chunks: %d\n", (heapSize /(pageSize+32)));
    p = 0;
    for (i = 0; i <  heapSize/(pageSize+32); ++i) {
        GOOD_ALLOC(q, pageSize);
        *q = 0;
        if (p) {
           *p = (u32)q;
        } else {
            r = q;
        }
        p = q;
    }
    printf("OK\n");

    printf("Free linked list @ %08x: ", r);
    p = r;
    while (p) {
        q = (u32 *)(*p);
        GOOD_FREE(p);
        p = q;
    } 
    printf("OK\n");

    printf("Allocate entire heap again (%d bytes): ", heapSize);
    GOOD_ALLOC(p, heapSize);
    printf("OK\n");

    printf("Free block @ %08x: ", p);
    GOOD_FREE(p);
    printf("OK\n");

    printf("Destroy heap: ");
    rv = IOS_DestroyHeap(hid);
    if (rv != IOS_ERROR_OK) {
        printf("Unable to destroy heap: %d\n", rv);
        exit(-1);
    }
    printf("OK\n");


    printf("Destroy invalid heap: ");
    rv = IOS_DestroyHeap(6);
    if (rv != IOS_ERROR_INVALID) {
        printf("Able to destroy invalid heap: %d\n", rv);
        exit(-1);
    }
    printf("OK\n");

    printf("Attempt to allocate from a destroyed heap: ");
    BAD_ALLOC(q, pageSize);
    printf("OK (allocation failed)\n");

    printf("\nTest passed\n\n");
}

void testAlignedAllocator(u32 pageSize, u32 align)
{
    u32 *p=0, *q, *r;
    u32 *ch[10];
    int i, heapSize;
    int size;
    IOSError rv;

    printf("\nAligned Alloc Test: %d byte pages (%d byte alignemnt)\n\n", pageSize, align);

    printf("Create heap\n");
    IOSHeapId hid = IOS_CreateHeap(heap, sizeof(heap));
    if (hid < 0) {
        printf("Unable to create heap: %d\n", hid);
        exit(-1);
    }
    printf("heap id: %d\n", hid);

    printf("Compute heap size: ");
    for (i = HEAP_SIZE; i > 0; i -= pageSize) {
        if (pageSize != 16)
            printf("allocating %d bytes\n", i);
        if ((p = ALLOC_ALIGN(i, align))) {
            break;
        }
    }
    heapSize = i;
    if (p) {
        printf("%d bytes @ %08x\n", heapSize, p);
    } else {
        printf("Unable to allocate any memory\n");
        exit(-1);
    }

    printf("Attempt to allocate even more memory: ");
    BAD_ALLOC_ALIGN(q, pageSize, align);
    printf("OK (allocation failed)\n");

    printf("Free block @ %08x: ", p);
    GOOD_FREE(p);
    printf("OK\n");

    printf("Free block again @ %08x: ", p);
    BAD_FREE(p);
    printf("OK\n");
    
    printf("Free bad address @ %08x: ", p+1);
    BAD_FREE(p+1);
    printf("OK\n");
    
    printf("Free 0: ");
    BAD_FREE(0);
    printf("OK\n");

    printf("Allocate 0 bytes: ");
    BAD_ALLOC_ALIGN(p, 0, align);
    printf("OK\n");

    printf("Allocate entire heap again (%d bytes): ", heapSize);
    GOOD_ALLOC_ALIGN(p, heapSize, align);
    printf("OK\n");

    printf("Free block @ %08x: ", p);
    GOOD_FREE(p);
    printf("OK\n");

    printf("Allocate entire heap in two chunks (%d bytes): ", heapSize/2-pageSize);
    GOOD_ALLOC_ALIGN(p, heapSize/2 - align, align);
    GOOD_ALLOC_ALIGN(q, heapSize/2 - align, align);
    printf("OK\n");

    printf("Free blocks @ %08x, %08x: ", p, q);
    GOOD_FREE(p);
    GOOD_FREE(q);
    printf("OK\n");

    size = (heapSize + pageSize)/3 - 2*align;
    printf("Allocate entire heap in three chunks (%d bytes): ", size);
    GOOD_ALLOC_ALIGN(p, size, align);
    GOOD_ALLOC_ALIGN(q, size, align);
    GOOD_ALLOC_ALIGN(r, size, align);
    printf("OK\n");

    printf("Free blocks @ %08x, %08x, %08x: ", p, q, r);
    GOOD_FREE(p);
    GOOD_FREE(q);
    GOOD_FREE(r);
    printf("OK\n");

    printf("Build a linked list using the entire heap %d bytes at a time: ", pageSize);
    p = 0;
    for (i = 0; i <  heapSize/(pageSize+align); ++i) {
        GOOD_ALLOC_ALIGN(q, pageSize, align);
        *q = 0;
        if (p) {
           *p = (u32)q;
        } else {
            r = q;
        }
        p = q;
    }
    printf("OK\n");

    printf("Free linked list @ %08x: ", r);
    p = r;
    while (p) {
        q = (u32 *)(*p);
        GOOD_FREE(p);
        p = q;
    } 
    printf("OK\n");

    printf("Allocate entire heap again (%d bytes): ", heapSize);
    GOOD_ALLOC_ALIGN(p, heapSize, align);
    printf("OK\n");

    printf("Free block @ %08x: ", p);
    GOOD_FREE(p);
    printf("OK\n");

    size = (heapSize + pageSize)/10 - 2*align;
    printf("Allocate entire heap in 10 chunks (%d bytes): ", size);
    for (i = 0; i < 10; ++i) {
        GOOD_ALLOC_ALIGN(ch[i], size, align);
    }
    printf("OK\n");

    printf("Free 10 chunks in random order: ");
    size = 6;
    for (i = 0; i < 10; ++i) {
        GOOD_FREE(ch[size%10]);
        size += 3;
    }
    printf("OK\n");

    printf("Allocate entire heap again (%d bytes): ", heapSize);
    GOOD_ALLOC_ALIGN(p, heapSize, align);
    printf("OK\n");

    printf("Free block @ %08x: ", p);
    GOOD_FREE(p);
    printf("OK\n");

    printf("Destroy heap: ");
    rv = IOS_DestroyHeap(hid);
    if (rv != IOS_ERROR_OK) {
        printf("Unable to destroy heap: %d\n", rv);
        exit(-1);
    }
    printf("OK\n");

    printf("\nTest passed\n\n");
}/* Thread created by idle process */
u8 testthrStack[1024]; /* 1024 is hardwired as stack size for this thread
                        * when the idle thread starts it. Please change
                        * it if you change this.
                        */
void
main(u32 arg)
{
    testAllocator(32);
    testAlignedAllocator(32, 128);
    testAlignedAllocator(32, 1024);
    exit(0);
}
