#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 GOOD_ALLOC(p, size) \
    if (((p) = ALLOC(size)) == 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 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 (512*1024)
void testHeapZero(u32 pageSize)
{
    IOSHeapId hid = 0;
    u32 *p=0, *q, *r;
    IOSError rv;
    int i, heapSize;
    int size;
    printf(" Testing allocation from heap id 0\n");
    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: ", pageSize);
    printf("Expect number of chunks: %d", (heapSize >> 5) - 2);
    p = 0;
    for (i = 0; i <  heapSize/32 - 2 ; ++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("\nTest passed\n\n");
}

void
main(u32 arg)
{
    testHeapZero(32);
    exit(0);
}
