Ejemplo n.º 1
0
Archivo: arrays.c Proyecto: Vuzi/vuziks
return_code arrays_array(Object* o, Linked_list *args, Variable* eval_value, int as_constructor) {

    (void)o;
    (void)args;

    if(!as_constructor) {
        err_add(E_ERROR, OP_IMPOSSIBLE, "Can't use this built-in function (arrays.array) as a function");
        return RC_ERROR;
    }

    // Nouvel objet array
    eval_value->type = T_OBJECT;
    eval_value->value.v_obj = var_new_object(NULL);

    // Nom d'objet : array
    eval_value->value.v_obj->name = "array";
    eval_value->value.v_obj->name_h = ARRAY_HASH;

    // Array en lui même
    Array* ar = xcalloc(1, sizeof(Array));

    eval_value->value.v_obj->data = (void*)ar;

    // Initialisation de l'array
    array_init(ar);

    // Fonction des strings

    Variable *v = var_new("add", str_hash("add"), T_FUNCTION_BUILTIN);
    v->value.v_func_builtin = arrays_array_add;
    v->container = &eval_value->value.v_obj->ec;
    v->container->object = eval_value->value.v_obj;

    linked_list_push(&(eval_value->value.v_obj->ec.variables), LLT_VARIABLE, (void*)v);

    v = var_new("get", str_hash("get"), T_FUNCTION_BUILTIN);
    v->value.v_func_builtin = arrays_array_get;
    v->container = &eval_value->value.v_obj->ec;
    v->container->object = eval_value->value.v_obj;

    linked_list_push(&(eval_value->value.v_obj->ec.variables), LLT_VARIABLE, (void*)v);

    v = var_new("pop", str_hash("pop"), T_FUNCTION_BUILTIN);
    v->value.v_func_builtin = arrays_array_pop;
    v->container = &eval_value->value.v_obj->ec;
    v->container->object = eval_value->value.v_obj;

    linked_list_push(&(eval_value->value.v_obj->ec.variables), LLT_VARIABLE, (void*)v);

    v = var_new("length", str_hash("length"), T_FUNCTION_BUILTIN);
    v->value.v_func_builtin = arrays_array_length;
    v->container = &eval_value->value.v_obj->ec;
    v->container->object = eval_value->value.v_obj;

    linked_list_push(&(eval_value->value.v_obj->ec.variables), LLT_VARIABLE, (void*)v);

    return RC_OK;
}
Ejemplo n.º 2
0
void tiered_priority_queue_push(TieredPriorityQueue_t *queue, int priority, int front, void *data) {
	if (priority >= TIERED_PRIORITY_QUEUE_LEVELS) priority = TIERED_PRIORITY_QUEUE_LEVELS-1;
	if (priority < 0) priority = 0;
	pthread_mutex_lock(&queue->mutex);
	linked_list_push(&queue->lists[priority], front, data);
	pthread_mutex_unlock(&queue->mutex);
}
Ejemplo n.º 3
0
int init_user_list(
	struct linked_list **curr
) {
	char directory_name[NAME_LEN];
	struct linked_list *head = NULL;
	struct linked_list *activity_list;
	if (list_fs_elements(CONF_DATASET_DIRECTORY, DT_DIR, &activity_list))
		return EXIT_FAILURE;
	while (activity_list) {
		struct linked_list *subdir_list;
		snprintf(directory_name, NAME_LEN, "%s/%s", CONF_DATASET_DIRECTORY, activity_list->name);
		if (list_fs_elements(directory_name, DT_DIR, &subdir_list))
			return EXIT_FAILURE;
		while (subdir_list) {
			if (linked_list_contains(head, subdir_list->name) == -1)
				linked_list_push(&head, subdir_list->name);
			subdir_list = subdir_list->next;
		}
		linked_list_free(subdir_list);
		activity_list = activity_list->next;
	}
	linked_list_free(activity_list);
	*curr = head;
	return EXIT_SUCCESS;
}
Ejemplo n.º 4
0
int list_fs_elements(
	char const *const directory,
	unsigned char d_type,
	struct linked_list **list
) {
	int ret = -1;
	DIR *dir = NULL;
	struct dirent *ent;
	struct linked_list *head = NULL;
	dir = opendir(directory);
	if (dir == NULL) {
		write_log("unable to open directory", LOG_ERROR);
		goto ERROR;
	}
	while ((ent = readdir(dir)) != NULL) {
		if (ent->d_type != d_type)
			continue;
		if (strcmp(ent->d_name, ".") == 0)
			continue;
		if (strcmp(ent->d_name, "..") == 0)
			continue;
		linked_list_push(&head, ent->d_name);
	}
	*list = head;
	ret = 0;
	ERROR:
		if (dir != NULL)
			closedir(dir);
		return ret;
}
Ejemplo n.º 5
0
static void
push_token_onto_stack(struct linked_list *stack,
                      enum ctache_token_type token_type)
{
    enum ctache_token_type *token_type_ptr;
    token_type_ptr = malloc(sizeof(*token_type_ptr));
    *token_type_ptr = token_type;
    linked_list_push(stack, token_type_ptr);
}
Ejemplo n.º 6
0
return_code strings_string(Object* o, Linked_list *args, Variable* eval_value, int as_constructor) {

    (void)o;
    (void)args;

    if(!as_constructor) {
        err_add(E_ERROR, OP_IMPOSSIBLE, "Can't use this built-in function (strings.string) as a function");
        return RC_ERROR;
    }

    // Nouvel objet string
    eval_value->type = T_OBJECT;
    eval_value->value.v_obj = var_new_object(NULL);

    // Nom d'objet : string
    eval_value->value.v_obj->name = "string";
    eval_value->value.v_obj->name_h = STRING_HASH;

    // String en lui même
    VK_String* vks = xcalloc(1, sizeof(VK_String));

    eval_value->value.v_obj->data = (void*)vks;

    // Fonction des strings
    Variable *v = var_new("append", str_hash("append"), T_FUNCTION_BUILTIN);
    v->value.v_func_builtin = strings_string_append;
    v->container = &eval_value->value.v_obj->ec;
    v->container->object = eval_value->value.v_obj;

    linked_list_push(&(eval_value->value.v_obj->ec.variables), LLT_VARIABLE, (void*)v);

    v = var_new("length", str_hash("length"), T_FUNCTION_BUILTIN);
    v->value.v_func_builtin = strings_string_length;
    v->container = &eval_value->value.v_obj->ec;
    v->container->object = eval_value->value.v_obj;

    linked_list_push(&(eval_value->value.v_obj->ec.variables), LLT_VARIABLE, (void*)v);

    return RC_OK;
}
Ejemplo n.º 7
0
Archivo: arrays.c Proyecto: Vuzi/vuziks
// Initialisation de l'objet built-in string
Object* arrays_init(Exec_context* ec_obj) {
    // Création de l'objet
    Object* o = var_new_object(NULL);

    (void)ec_obj;

    // Définition du type
    o->name = "arrays";
    o->name_h = str_hash(o->name);

    // Ajout des fonctions
    Variable* v = var_new("array", str_hash("array"), T_FUNCTION_BUILTIN);
    v->value.v_func_builtin = arrays_array;
    v->container = &o->ec; v->container->object = o;
    linked_list_push(&(o->ec.variables), LLT_VARIABLE, (void*)v);

    return o;
}
Ejemplo n.º 8
0
/*
 * osprd_ioctl(inode, filp, cmd, arg)
 *   Called to perform an ioctl on the named file.
 */
int osprd_ioctl(struct inode *inode, struct file *filp,
		unsigned int cmd, char *passwd)
{
	osprd_info_t *d = file2osprd(filp);	// device info
	int r = 0;			// return value: initially 0

	// is file open for writing?
	int filp_writable = (filp->f_mode & FMODE_WRITE) != 0;

	// This line avoids compiler warnings; you may remove it.
	(void) filp_writable, (void) d;

	// Set 'r' to the ioctl's return value: 0 on success, negative on error
	//eprintk("cmd: %d\n", cmd);

	if (cmd == OSPRDIOCACQUIRE) {

		// EXERCISE: Lock the ramdisk.
		//
		// If *filp is open for writing (filp_writable), then attempt
		// to write-lock the ramdisk; otherwise attempt to read-lock
		// the ramdisk.
		//
                // This lock request must block using 'd->blockq' until:
		// 1) no other process holds a write lock;
		// 2) either the request is for a read lock, or no other process
		//    holds a read lock; and
		// 3) lock requests should be serviced in order, so no process
		//    that blocked earlier is still blocked waiting for the
		//    lock.
		//
		// If a process acquires a lock, mark this fact by setting
		// 'filp->f_flags |= F_OSPRD_LOCKED'.  You also need to
		// keep track of how many read and write locks are held:
		// change the 'osprd_info_t' structure to do this.
		//
		// Also wake up processes waiting on 'd->blockq' as needed.
		//
		// If the lock request would cause a deadlock, return -EDEADLK.
		// If the lock request blocks and is awoken by a signal, then
		// return -ERESTARTSYS.
		// Otherwise, if we can grant the lock request, return 0.

		// 'd->ticket_head' and 'd->ticket_tail' should help you
		// service lock requests in order.  These implement a ticket
		// order: 'ticket_tail' is the next ticket, and 'ticket_head'
		// is the ticket currently being served.  You should set a local
		// variable to 'd->ticket_head' and increment 'd->ticket_head'.
		// Then, block at least until 'd->ticket_tail == local_ticket'.
		// (Some of these operations are in a critical section and must
		// be protected by a spinlock; which ones?)

		// Your code here (instead of the next two lines).
		//eprintk("Attempting to acquire\n");
		unsigned my_ticket;
		// check deadlock protection
		osp_spin_lock(&d->mutex);
		if (d->write_locking_pid == current->pid) {
			osp_spin_unlock(&d->mutex);
			return -EDEADLK;
		}
		my_ticket = d->ticket_head;
		d->ticket_head++;
		osp_spin_unlock(&d->mutex);
		//eprintk("pid = %d\n", current->pid);

		if (filp_writable) {
			//eprintk("write %d\n", d->write_locking_pid);
			// write lock
			if (wait_event_interruptible(d->blockq,
				d->ticket_tail == my_ticket
				&& d->write_locking_pid == 0
				&& d->read_locking_pids.size == 0
				)) {
				//eprintk("wait_event_interruptible: %d\n", current->pid);
				//osp_spin_lock(&d->mutex);
				// if blocked
				if(d->ticket_tail == my_ticket) {
					// this process is being served
					d->ticket_tail = return_valid_ticket(
						&d->invalid_tickets, d->ticket_tail);
					wake_up_all(&d->blockq);
				}
				else {
					// not being served

					// add spin lock for good measure
					osp_spin_lock(&d->mutex);
					linked_list_push(&d->invalid_tickets, my_ticket);
					osp_spin_unlock(&d->mutex);
				}
				//osp_spin_unlock(&d->mutex);
				return -ERESTARTSYS;
			}
			else {
				// acquire the lock
				//eprintk("acquire write lock\n");
				osp_spin_lock(&d->mutex);
				filp->f_flags |= F_OSPRD_LOCKED;
				d->write_locking_pid = current->pid;
				d->ticket_tail = return_valid_ticket(
					&d->invalid_tickets, d->ticket_tail);
				osp_spin_unlock(&d->mutex);
				wake_up_all(&d->blockq);
				return 0;
			}
		}
		else {
			//read lock
			//eprintk("read\n");
			if (wait_event_interruptible(d->blockq,
				d->ticket_tail == my_ticket
				&& d->write_locking_pid == 0)) {
				//osp_spin_lock(&d->mutex);
				// if blocked
				if(d->ticket_tail == my_ticket) {
					// this process is being served
					d->ticket_tail = return_valid_ticket(
						&d->invalid_tickets, d->ticket_tail);
					wake_up_all(&d->blockq);
				}
				else {
					// not being served

					// add spin lock for good measure
					osp_spin_lock(&d->mutex);
					linked_list_push(&d->invalid_tickets, my_ticket);
					osp_spin_unlock(&d->mutex);
				}
				//osp_spin_unlock(&d->mutex);
				return -ERESTARTSYS;
			}
			else {
				// acquire the lock
				//eprintk("read lock\n");
				osp_spin_lock(&d->mutex);
				filp->f_flags |= F_OSPRD_LOCKED;
				linked_list_push(&d->read_locking_pids, current->pid);
				d->ticket_tail = return_valid_ticket(
					&d->invalid_tickets, d->ticket_tail);
				osp_spin_unlock(&d->mutex);
				wake_up_all(&d->blockq);
				return 0;
			}
		}

	} else if (cmd == OSPRDIOCTRYACQUIRE) {

		// EXERCISE: ATTEMPT to lock the ramdisk.
		//
		// This is just like OSPRDIOCACQUIRE, except it should never
		// block.  If OSPRDIOCACQUIRE would block or return deadlock,
		// OSPRDIOCTRYACQUIRE should return -EBUSY.
		// Otherwise, if we can grant the lock request, return 0.

		// Your code here (instead of the next two lines).
		//eprintk("Attempting to try acquire\n");
		if (filp_writable) {
			osp_spin_lock(&d->mutex);
			if (d->write_locking_pid != 0 || d->read_locking_pids.size != 0) {
				osp_spin_unlock(&d->mutex);
				return -EBUSY;
			}
			filp->f_flags |= F_OSPRD_LOCKED;
			d->write_locking_pid = current->pid;
			osp_spin_unlock(&d->mutex);
			return 0;
		}
		else {
			osp_spin_lock(&d->mutex);
			if (d->write_locking_pid != 0) {
				osp_spin_unlock(&d->mutex);
				return -EBUSY;
			}
			filp->f_flags |= F_OSPRD_LOCKED;
			linked_list_push(&d->read_locking_pids, current->pid);
			osp_spin_unlock(&d->mutex);
			return 0;
		}

	} else if (cmd == OSPRDIOCRELEASE) {
		d->passwd_hash = 0;

		// EXERCISE: Unlock the ramdisk.
		//
		// If the file hasn't locked the ramdisk, return -EINVAL.
		// Otherwise, clear the lock from filp->f_flags, wake up
		// the wait queue, perform any additional accounting steps
		// you need, and return 0.

		// Your code here (instead of the next line).
		if (!(filp->f_flags & F_OSPRD_LOCKED)) {
			// ramdisk isn't locked
			return -EINVAL;
		}
		if (filp_writable) {
			// see if this process has write lock
			osp_spin_lock(&d->mutex);
			if (d->write_locking_pid != current->pid) {
				return -EINVAL;
			}
			//eprintk("release\n");
			d->write_locking_pid = 0;
			if (d->read_locking_pids.size == 0) {
				//eprintk("unsetting flag\n");
				filp->f_flags ^= F_OSPRD_LOCKED;
			}
			osp_spin_unlock(&d->mutex);
			wake_up_all(&d->blockq);
			return 0;
		}
		else {
			//eprintk("!!!!!reached\n");
			if (d->read_locking_pids.size == 0) {
				return -EINVAL;
			}
			osp_spin_lock(&d->mutex);
			int removeStatus = linked_list_remove(&d->read_locking_pids, current->pid);
			//eprintk("%d\n", removeStatus);
			if (removeStatus) {
				if (d->read_locking_pids.size == 0 && d->write_locking_pid == 0) {
					filp->f_flags ^= F_OSPRD_LOCKED;
				}
				wake_up_all(&d->blockq);
				//return 0;
			}
			osp_spin_unlock(&d->mutex);
			return removeStatus ? 0 : -EINVAL;
		}

	}
	else if (cmd == OSPRDIOCPASSWD) {
		char *buf = (char*)kmalloc(20, GFP_ATOMIC);
		if (copy_from_user(buf, (const char __user*) passwd, 20)) {
			kfree(buf);
			return -EFAULT;
		}
		d->passwd_hash = jenkins_hash(buf);
		//eprintk("OSPRDIOCPASSWD: %d\n", d->passwd_hash);
		return 0;
	}
	else {
		r = -ENOTTY; /* unknown command */
		//eprintk("not recognized\n");
	}
	return r;
}