コード例 #1
0
ファイル: malloc537.c プロジェクト: ningrassia/malloc537
/*
 * Functions similarly to realloc, but does checking on the input
 * pointer, and stores output pointer data in our hash table
*/
void *realloc537(void *ptr, size_t size)
{
	void * return_pointer;
	node * remove_node = NULL;

	/* If the pointer is null, this is just a malloc! let malloc537 handle it.*/
	if(ptr == NULL)
	{
		return malloc537(size);
	}
	/* If the size is null, it's just a free.*/
	else if(size == 0)
	{
		free537(ptr);
		return NULL;
	}
	else
	{
		/*HERE WE DO A REMOVE/mark as unused/whatever*/
		node * temp;
		temp = lookup(ptr);
		temp->free = 1;
	}
	
	return_pointer = realloc(ptr, size);

	/* Before we insert, remove any nodes that will be overlapped.*/
	/*
	 * Need to find all nodes within range base+1 to size, and delete them.
	 */
			
	remove_node = contained_lookup(return_pointer, size);
	while(remove_node != NULL)
	{
		delete_node(remove_node->base);	
		remove_node = contained_lookup(return_pointer, size);
	}

	insert(ptr, size);
	/*
	print(root, 0);
	printf("\n");
	*/
	
	return return_pointer;
}
コード例 #2
0
ファイル: getProcesses.c プロジェクト: mberberet/CS537-prog-3
int *getProcesses()
{
    const char* proc = "/proc";
    int *pid;
    int pidSize;
    DIR *handle;
    /*Get DIR struct for /proc*/
    struct dirent processes;
    /*Struct for each directory in /proc*/
    struct dirent *p;
    int x;
    int i;
    int isPID;
    handle = opendir(proc);
    if(!handle)
    {
    	return NULL;
    }
    x = 0;
    pidSize = 100;
    pid = (int *) malloc537(pidSize * sizeof(int));
    if(!pid)
    {
    	free537(pid);
        closedir(handle);
	return NULL;
    }
    p = readdir(handle);

    while(p)
    {
    	processes = *p;
    	i = 0;
	isPID = 1;
	/* Make sure the directory name is an integer*/
	while(processes.d_name[i] != '\0' && isPID)
	{
		if(!isdigit((int)(processes.d_name[i])))
		{
			isPID = 0;
		}
		i++;
	}
	/* If UID is correct, add it to the array*/
	if(isPID && hasUID(processes.d_name))
	{
		memcheck537(&pid[x], sizeof(int));
		pid[x] = atoi(processes.d_name);
		if(pid[x] == 0)
		{
			free537(pid);
            closedir(handle);
			return NULL;
		}
		x++;
		/*Double the size of the array if we hit the end */
		if(x >= pidSize)
		{
			pidSize*=2;
			pid = (int *) realloc537(pid, pidSize*sizeof(int));
			if(!pid)
			{
				free537(pid);
                closedir(handle);
				return NULL;
			}
		}
	}
	p = readdir(handle);
    }
    /*if there isn't an error, return the array*/
    if(errno == 0)
    {
	memcheck537(pid + x, sizeof(int));
	*(pid + x) = 0;
    closedir(handle);
	return pid;
    }
    else
    {
 	free537(pid);
    closedir(handle);
 	return NULL;
    }
}