Example #1
0
extern C void SECTION(".entry") _entry() 
{
    int ret, argc;
    char *arguments;
    char **argv;

    /* Setup the heap, C++ constructors and default mounts. */
    setupHeap();
    runConstructors();
    setupMappings();

    /* Allocate buffer for arguments. */
    argc = 0;
    argv = (char **) new char[ARGV_COUNT];
    arguments = (char *) ARGV_ADDR;

    /* Fill in arguments list. */
    while (argc < ARGV_COUNT && *arguments)
    {
        argv[argc] = arguments;
        arguments += ARGV_SIZE;
        argc++;
    }
    /* Pass control to the program. */
    ret = main(argc, argv);

    /* Terminate execution. */
    runDestructors();
    exit(ret);
}
Example #2
0
void* my_malloc(size_t size)
{
  	// calculate the needed size
	int total_size = size + sizeof(metadata_t);

	// if the requested size is beyond our largest block size
	if (total_size > SBRK_SIZE) {
		ERRNO = SINGLE_REQUEST_TOO_LARGE;
		return NULL;
	}
	// set up the heap if it is uninitialized
	if (!heap) {
		setupHeap();
		if (ERRNO == OUT_OF_MEMORY) {
			return NULL;
		}
	}
	// get index of the free list to allocate memory to the user
	int idx = getIdx(total_size);
	metadata_t *allocBlock = NULL;
	// if we have a memory block to allocate
	if (freelist[idx] != NULL) {
		// just simply return the first block
		allocBlock = freelist[idx];
		// if there is the next block
		if (allocBlock->next != NULL) {
			allocBlock->next->prev = NULL;
			freelist[idx] = allocBlock->next;
		} else {
			freelist[idx] = NULL;
		}
		// remove this block/
		allocBlock->in_use = 1;
		allocBlock->prev = NULL;
		allocBlock->next = NULL;

		// return the block
		return ((char*)allocBlock + sizeof(metadata_t));
	}
	// if we do not have a memory block to allocate
	// we split the second smallest blocks into blocks of needed size
	int splitIdx = idx + 1;
	while (freelist[splitIdx] == NULL) {
		splitIdx++;
	}
	// if there are no blocks to split
	if (splitIdx == 8) {
		// set up a new heap
		setupHeap();
		if (ERRNO == OUT_OF_MEMORY) {
			return NULL;
		}
	} else { // if a block can be split 
		splitBlock(splitIdx, idx, freelist[splitIdx]); // a recursive function
	}
	// call my_malloc() again to allocate memory
	allocBlock = my_malloc(size);
    // set the error code
    ERRNO = NO_ERROR;

  	return allocBlock;
}