void testAllocHandlesOutOfMemory(void){
	long i;
	int dealtWithOutOfMemory = 0;
	
	printf(" => testAllocHandlesOutOfMemory\n");

	REAL_TASK_COUNT = 0;
	for(i = FAIR_TASK_COUNT; i <= MAX_TASK_COUNT && dealtWithOutOfMemory == 0; i++){
		/* Point test slot i to the actual task_alloc() result. */
		tasks[i] = NULL;
		tasks[i] = task_alloc();

		if (tasks[i] != NULL){
			/* Slots must be in the allocated space */
			assert( (unsigned long)tasks[i] <= ( (unsigned long)get_MEM_BLOCK_START() + MEM_BLOCK_SIZE ) );
		} else {
			/* If the allocation was out of memory, NULL may have been returned, which passes the test */
			dealtWithOutOfMemory = 1;
			REAL_TASK_COUNT = i;
		}
	}

	assert(REAL_TASK_COUNT >= FAIR_TASK_COUNT);
	assert(REAL_TASK_COUNT <= MAX_TASK_COUNT);
	assert( dealtWithOutOfMemory );
}
void testAllocUniqueFreeMemory(void){
	int i;
	task_t *prev_task = NULL;

	printf(" => testAllocUniqueFreeMemory\n");

	for(i = 0; i < FAIR_TASK_COUNT; i++){
		/* Point test slot i to the actual task_alloc() result. */
		tasks[i] = task_alloc();
		/* Slots must be allocated */
		assert(tasks[i] != NULL);
		/* Slots must be in the allocated space */
		assert( (unsigned long)tasks[i] <= ( (unsigned long)get_MEM_BLOCK_START() + MEM_BLOCK_SIZE ) );

		/* Try and set some data */
		tasks[i]->id = i;
		
		/* Compare and set prev_task */
		if (prev_task != NULL){
			/* Slots must be unique in pointer reference */
			assert(tasks[i] != prev_task);
			/* Slots must thus be unique in value */
			assert(tasks[i]->id != prev_task->id);
		}
		prev_task = tasks[i];
	}
}
void task_free(void *ptr) {
	/* Check to see if the ptr is within our memory block */
	if(ptr != NULL && ptr >= get_MEM_BLOCK_START() && (unsigned long)ptr < (unsigned long)get_MEM_BLOCK_START()+MEM_BLOCK_SIZE) {
		/* If so, create a new empty_space struct to store in the memory slot of the free'd task */
		struct empty_space *newspace = ptr;
		newspace->this_space = ptr;
		newspace->next_space = lastempty;
		lastempty = newspace;
	}
}
void testFreeHandlesWrongPointers(void){

	printf(" => testFreeHandlesWrongPointers\n");

	/* This should simply succeed. Pointers to out of bound addresses must be ignored. */	
	task_free((task_t*) ((unsigned long) get_MEM_BLOCK_START() + MEM_BLOCK_SIZE + 1024));

	/* This should simply succeed. Pointers to out of bound addresses must be ignored. */	
	task_free((task_t*) 0xFFFFFFFF); /* Pointing to address at 4 GB of memory */
}
void *task_alloc(void) {

	printf("HIHIHIHIHIHIHIHIHI\n");
	
	if (initialized == 0) {
		printf("HI DOES THIS HAPPEN AT ALL\n");
		curr = (node *)((unsigned int)get_MEM_BLOCK_START() + MEM_BLOCK_SIZE - sizeof(task_t));
		prev = NULL;
		curr->next = (node *)((unsigned int)get_MEM_BLOCK_START());

		printf("I have a small hunch this doesn't f**k things up\n");
	
		for (; (unsigned int)curr >= (unsigned int)get_MEM_BLOCK_START() + 2*sizeof(task_t); prev=curr, curr = (node *)((unsigned int)curr - sizeof(task_t))) {
			/* printf("%p \n", main_pointer); */
/*		curr->next = (node *)((unsigned int)get_MEM_BLOCK_START());*/	
		curr->next = prev;
/*	printf("%p %p %p\n", curr, curr->next, prev);*/
			/*curr->next = (node *)prev;*/
/*			printf("%p -> %p\n", head, head->next);*/ 
		}

		printf("Finished looping yo \n");

		printf("HI HIHIHIHIHI\n");
		head = (node *)((unsigned int)get_MEM_BLOCK_START());
	
		printf("Einde: %p\n", head);	
		initialized = 1;
	} 

	printf("%p \n", curr);
	printf("%p \n", head);

	
	curr = head->next;
	/*if ((unsigned int)curr == (unsigned int)NULL) {
		head->next = curr->next;
	}*/

	return curr;		
}
/** The test program */
int main(int argc, char *argv[]){
	void* start;
	printf("= as2_t1 =\n");
	printf("This unit test program is provided for Assignment 2, Task 1 of the course Operating Systems, University of Twente, 2011.\n\n");
	printf("Your solution will be tested on a number of points. Afterwards, \"[End of Test]\" will be shown. IF this is not so, the program crashed and you're not done yet.\n");
	printf("Please note: this program may detect some flaws, but may not detect all of them. Do *not* assume correctness of your solution on passing this test alone.\n\n");


	printf(" || Memory Block Start Address    || 0x%8x       ||\n", (unsigned int) get_MEM_BLOCK_START());
	printf(" || Memory Block Size             || %10lu Bytes ||\n", MEM_BLOCK_SIZE);
	printf(" || task_t Slot Size              || %10u Bytes ||\n", sizeof(task_t));
	printf(" || task_t Max Slot Count         || %10lu slots ||\n", MAX_TASK_COUNT);
	printf(" || task_t Fair Slot Count        || %10i slots ||\n", FAIR_TASK_COUNT);
	printf("\n");
	
	runAllTests();
	
	start = get_MEM_BLOCK_START();
	if( NULL != start )
		free( start );
}
void *task_alloc() {
	/* Do some initialization the first time */
	if(!pointerSet) {
		topPointer = get_MEM_BLOCK_START();
		pointerSet = 1;
	}
	
	/* If there is a previous released memory slot, use it */
	if(lastempty != NULL) {
		void *result = lastempty->this_space;
		lastempty = lastempty->next_space;
		return result;
	}else if(topPointer - (task_t*)get_MEM_BLOCK_START() > MEM_BLOCK_SIZE / sizeof(task_t) - 1) {
		/* If no more memory is available, return a NULL pointer, similar to how malloc handles this */
		return NULL;
	}else{
		/* If all goes well we can just return the topPointer and increment it */
		void *result = topPointer;
		topPointer++;
		return result;
	}
}
void task_free(void *ptr) {
	if (!(ptr == NULL) && !((unsigned long)&ptr < (unsigned long)get_MEM_BLOCK_START())) {
		
	}	
}