kernel/sys/process.c
/**
 * @brief Allocate a new file descriptor.
 *
 * Adds a new entry to the file descriptor table for @p proc
 * pointing to the file @p node. The file descriptor's offset
 * and file modes must be set by the caller afterwards.
 *
 * @param proc Process whose file descriptor should be modified.
 * @param node VFS object to add a reference to.
 * @returns the new file descriptor index
 */
@brief Allocate a new file descriptor.
Adds a new entry to the file descriptor table for @p proc
pointing to the file @p node. The file descriptor's offset
and file modes must be set by the caller afterwards.

@param proc Process whose file descriptor should be modified.
@param node VFS object to add a reference to.
@returns the new file descriptor index
@brief Allocate a new file descriptor . Adds a new entry to the file descriptor table for @p proc pointing to the file @p node . The file descriptor ' s offset and file modes must be set by the caller afterwards . @param proc Process whose file descriptor should be modified . @param node VFS object to add a reference to . @returns the new file descriptor index
unsigned long process_append_fd(process_t * proc, fs_node_t * node) {
	spin_lock(proc->fds->lock);
	for (unsigned long i = 0; i < proc->fds->length; ++i) {
		if (!proc->fds->entries[i]) {
			proc->fds->entries[i] = node;
			proc->fds->modes[i] = 0;
			proc->fds->offsets[i] = 0;
			spin_unlock(proc->fds->lock);
			return i;
		}
	}
	if (proc->fds->length == proc->fds->capacity) {
		proc->fds->capacity *= 2;
		proc->fds->entries = realloc(proc->fds->entries, sizeof(fs_node_t *) * proc->fds->capacity);
		proc->fds->modes   = realloc(proc->fds->modes,   sizeof(int) * proc->fds->capacity);
		proc->fds->offsets = realloc(proc->fds->offsets, sizeof(uint64_t) * proc->fds->capacity);
	}
	proc->fds->entries[proc->fds->length] = node;
	proc->fds->modes[proc->fds->length] = 0;
	proc->fds->offsets[proc->fds->length] = 0;
	proc->fds->length++;
	spin_unlock(proc->fds->lock);
	return proc->fds->length-1;
}
unsigned long process_append_fd ( process_t * proc , fs_node_t * node ) { spin_lock ( proc -> fds -> lock ) ; for ( unsigned long i = 0 ; i < proc -> fds -> length ; ++ i ) { if ( ! proc -> fds -> entries [ i ] ) { proc -> fds -> entries [ i ] = node ; proc -> fds -> modes [ i ] = 0 ; proc -> fds -> offsets [ i ] = 0 ; spin_unlock ( proc -> fds -> lock ) ; return i ; } } if ( proc -> fds -> length == proc -> fds -> capacity ) { proc -> fds -> capacity *= 2 ; proc -> fds -> entries = realloc ( proc -> fds -> entries , sizeof ( fs_node_t * ) * proc -> fds -> capacity ) ; proc -> fds -> modes = realloc ( proc -> fds -> modes , sizeof ( int ) * proc -> fds -> capacity ) ; proc -> fds -> offsets = realloc ( proc -> fds -> offsets , sizeof ( uint64_t ) * proc -> fds -> capacity ) ; } proc -> fds -> entries [ proc -> fds -> length ] = node ; proc -> fds -> modes [ proc -> fds -> length ] = 0 ; proc -> fds -> offsets [ proc -> fds -> length ] = 0 ; proc -> fds -> length ++ ; spin_unlock ( proc -> fds -> lock ) ; return proc -> fds -> length - 1 ; }
@brief Allocate a new file descriptor.
@brief Allocate a new file descriptor .
/* Fill gaps */ /* modes, offsets must be set by caller */ /* No gaps, expand */ /* modes, offsets must be set by caller */