PyObject *
PyWeakref_NewRef(PyObject *ob, PyObject *callback)
{
    PyWeakReference *result = NULL;
    PyWeakReference **list;
    PyWeakReference *ref, *proxy;

    if (!PyType_SUPPORTS_WEAKREFS(ob->ob_type)) {
        PyErr_Format(PyExc_TypeError,
		     "cannot create weak reference to '%s' object",
                     ob->ob_type->tp_name);
        return NULL;
    }
    list = GET_WEAKREFS_LISTPTR(ob);
    get_basic_refs(*list, &ref, &proxy);
    if (callback == Py_None)
        callback = NULL;
    if (callback == NULL)
        /* return existing weak reference if it exists */
        result = ref;
    if (result != NULL)
        Py_INCREF(result);
    else {
        /* Note: new_weakref() can trigger cyclic GC, so the weakref
           list on ob can be mutated.  This means that the ref and
           proxy pointers we got back earlier may have been collected,
           so we need to compute these values again before we use
           them. */
        result = new_weakref(ob, callback);
        if (result != NULL) {
            get_basic_refs(*list, &ref, &proxy);
            if (callback == NULL) {
                if (ref == NULL)
                    insert_head(result, list);
                else {
                    /* Someone else added a ref without a callback
                       during GC.  Return that one instead of this one
                       to avoid violating the invariants of the list
                       of weakrefs for ob. */
                    Py_DECREF(result);
                    Py_INCREF(ref);
                    result = ref;
                }
            }
            else {
                PyWeakReference *prev;

                prev = (proxy == NULL) ? ref : proxy;
                if (prev == NULL)
                    insert_head(result, list);
                else
                    insert_after(result, prev);
            }
        }
    }
    return (PyObject *) result;
}
static PyObject *
weakref___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
    PyWeakReference *self = NULL;
    PyObject *ob, *callback = NULL;

    if (parse_weakref_init_args("__new__", args, kwargs, &ob, &callback)) {
        PyWeakReference *ref, *proxy;
        PyWeakReference **list;

        if (!PyType_SUPPORTS_WEAKREFS(ob->ob_type)) {
            PyErr_Format(PyExc_TypeError,
                         "cannot create weak reference to '%s' object",
                         ob->ob_type->tp_name);
            return NULL;
        }
        if (callback == Py_None)
            callback = NULL;
        list = GET_WEAKREFS_LISTPTR(ob);
        get_basic_refs(*list, &ref, &proxy);
        if (callback == NULL && type == &_PyWeakref_RefType) {
            if (ref != NULL) {
                /* We can re-use an existing reference. */
                Py_INCREF(ref);
                return (PyObject *)ref;
            }
        }
        /* We have to create a new reference. */
        /* Note: the tp_alloc() can trigger cyclic GC, so the weakref
           list on ob can be mutated.  This means that the ref and
           proxy pointers we got back earlier may have been collected,
           so we need to compute these values again before we use
           them. */
        self = (PyWeakReference *) (type->tp_alloc(type, 0));
        if (self != NULL) {
            init_weakref(self, ob, callback);
            if (callback == NULL && type == &_PyWeakref_RefType) {
                insert_head(self, list);
            }
            else {
                PyWeakReference *prev;

                get_basic_refs(*list, &ref, &proxy);
                prev = (proxy == NULL) ? ref : proxy;
                if (prev == NULL)
                    insert_head(self, list);
                else
                    insert_after(self, prev);
            }
        }
    }
    return (PyObject *)self;
}
Ejemplo n.º 3
0
Ret buddy_alloc(Buddy* thiz, size_t size, size_t* offset)
{
	return_val_if_fail(thiz!=NULL && size>0 && size<=thiz->size && offset!=NULL, RET_FAIL);

	Node* iter = NULL;

	size_t i;
	for(i=size; i<=thiz->size; i++)
	{
		if(thiz->free_list[i] != NULL)
		{
			iter = thiz->free_list[i];	
			break;
		}
	}

	if(iter == NULL)
	{
		return RET_FAIL;
	}

	size_t found_size = i+1;

	if(found_size > size)
	{
		//split.
		for(;;)
		{
			size_t new_size = size - 1;

			//buddy.
			Node* n = node_new(new_size, iter->offset + 1<<new_size);
			insert_head(&(thiz->free_list[new_size-1]), n);

			if(new_size == size)
			{
				Node* nn = node_new(new_size, iter->offset);
				insert_head(&(thiz->alloc_list[new_size-1]), nn);
				break;
			}
		}
	}
	else
	{
		Node* nnn = node_new(iter->size, iter->offset);
		insert_head(&(thiz->alloc_list[size-1]), nnn);
	}

	*offset = iter->offset;
	return RET_OK;
}
Ejemplo n.º 4
0
Archivo: btree.c Proyecto: taysom/tau
int insert (tree_s *tree, u64 key, void *rec, unint len)
{
	void	*root;
FN;
	root = grow_tree(tree, len);
	return insert_head(tree, root, key, rec, len);
}
Ejemplo n.º 5
0
Archivo: btree.c Proyecto: taysom/tau
static int insert_branch (
	tree_s		*tree,
	branch_s	*parent,
	u64		key,
	void		*rec,
	unint		len)
{
	void		*child;
	int		k;	/* Critical that this be signed */
FN;
	for (;;) {
		do {
			k = binary_search_branch(key, parent->br_key,
							parent->br_num);
			child = bget(bdev(parent), parent->br_key[k].k_block);
			if (!child) {
				bput(parent);
				return qERR_NOT_FOUND;
			}
		} while (split(child, parent, k, len));
		bput(parent);
		if (type(child) != BRANCH) {
			return insert_head(tree, child, key, rec, len);
		}
		parent = child;
	}
}
Ejemplo n.º 6
0
Ret buddy_free(Buddy* thiz, size_t offset)
{
	return_val_if_fail(thiz!=NULL && (offset%2)==0, RET_FAIL);
	
	int i;
	for(i=0; i<thiz->size; i++)
	{
		Node* iter = foreach_list(thiz->alloc_list[i], (void* )offset, match_block_callback);
		if(iter == NULL) continue;

		delete_from_list(&(thiz->alloc_list[i]), iter);
		free(iter); iter = NULL;

		Node* buddy_iter = foreach_list(thiz->free_list[i], (void* )offset, match_block_callback);
		if(buddy_iter == NULL)
		{
			return RET_OK;
		}

		delete_from_list(&(thiz->free_list[i]), buddy_iter);
		buddy_iter->size = buddy_iter->size + 1;
		insert_head(&(thiz->free_list[buddy_iter->size - 1]), buddy_iter);
		//TODO merge should be recurive.
	}
	
	return RET_FAIL;
}
Ejemplo n.º 7
0
struct l1 *insert(int key) {
  if(head == NULL) {
    head = (struct l1*) malloc(sizeof(struct l1));
    head->key = key;
    return head;
  }
  struct l1 *p = head;
  struct l1 *prev = NULL;
  while((p->key < key) && (p->next != NULL)) {
    prev = p;
    p = p->next;
  }
  if(prev == NULL) {
    struct l1*n = insert_head(key);
    return n;
  }
  if(p->next == NULL) {
    struct l1 *n = insert_tail(key);
    return n;    
  }
  struct l1 *n = (struct l1*) malloc(sizeof(struct l1));
  n->key = key;
  prev->next = n;
  n->next = p;
  return n;
}
Ejemplo n.º 8
0
node_t* add_list(node_t *l1, node_t *l2)
{
	node_t *ret = NULL;
	reverse_list_iter(&l1);
	reverse_list_iter(&l2);
	node_t *t1 = l1;
	node_t *t2 = l2;
	int carry = 0;
	int sum;
	int digit;
	int v1;
	int v2;

	while(t1 || t2)
	{
		v1 = 0;
		v2 = 0;
		if(t1)
		{
			v1 = t1->val;
			t1 = t1->next;
		}

		if(t2)
		{
			v2 = t2->val;
			t2 = t2->next;
		}

		sum = v1 + v2 + carry;
		digit = sum % 10;
		carry = sum / 10;

		node_t *n = (node_t *)malloc(sizeof(node_t));
		n->val = digit;
		insert_head(&ret, n);
	}

	if(carry > 0)
	{
		node_t *n = (node_t *)malloc(sizeof(node_t));
		n->val = carry;
		insert_head(&ret, n);
	}

	return ret;
}
Ejemplo n.º 9
0
int main() {
  insert_head(3);
  insert_tail(4);
  insert(2);
  insert(1);
  insert(5);
  print_l1();
  return 0;
}
Ejemplo n.º 10
0
void reverse_list_iter(node_t **list)
{
    node_t *revl = NULL;
    node_t *tmp = *list;
    node_t *n;
    while(tmp)
    {
        n = tmp;
        tmp = tmp->next;
        insert_head(&revl, n);
    }
    *list = revl;
}
Ejemplo n.º 11
0
rstatus_t coro_sent_parallel(coroutine *c)
{
    ASSERT(c);
    ASSERT(c->need_parallel);
    if(pthread_mutex_trylock(&g_mutex) == 0) {
        c->sched = g_parallelsched;
        insert_head(&c->sched->wait_sched_queue, c);
        pthread_mutex_unlock(&g_mutex); 
        return M_OK;
    }
    c->sched = g_mastersched;
    return M_ERR; 
}
Ejemplo n.º 12
0
node_t* merge_list(node_t *l1, node_t *l2)
{
    node_t *t1 = l1;
    node_t *t2 = l2;
    node_t *list = NULL;
    node_t *n;
    while(t1 && t2)
    {
        if(t1->val <= t2->val)
        {
            n = t1;
            t1 = t1->next;
        }
        else
        {
            n = t2;
            t2 = t2->next;
        }
        insert_head(&list, n);
    }
    
    while(t1)
    {
        n = t1;
        t1 = t1->next;
        insert_head(&list, n);
    }
    
    while(t2)
    {
        n = t2;
        t2 = t2->next;
        insert_head(&list, n);
    }
    reverse_list_iter(&list);
    
    return list;
}
Ejemplo n.º 13
0
PyObject *
PyWeakref_NewRef(PyObject *ob, PyObject *callback)
{
    PyWeakReference *result = NULL;
    PyWeakReference **list;
    PyWeakReference *ref, *proxy;

    if (!PyType_SUPPORTS_WEAKREFS(ob->ob_type)) {
        PyErr_Format(PyExc_TypeError,
		     "cannot create weak reference to '%s' object",
                     ob->ob_type->tp_name);
        return NULL;
    }
    list = GET_WEAKREFS_LISTPTR(ob);
    get_basic_refs(*list, &ref, &proxy);
    if (callback == NULL || callback == Py_None)
        /* return existing weak reference if it exists */
        result = ref;
    if (result != NULL)
        Py_XINCREF(result);
    else {
        result = new_weakref(ob, callback);
        if (result != NULL) {
            if (callback == NULL) {
                insert_head(result, list);
            }
            else {
                PyWeakReference *prev = (proxy == NULL) ? ref : proxy;

                if (prev == NULL)
                    insert_head(result, list);
                else
                    insert_after(result, prev);
            }
        }
    }
    return (PyObject *) result;
}
Ejemplo n.º 14
0
/*  Return: (List) list reversed
	Data: l (List) list to reverse
	Process: travers the list to change each next pointer value */
List reverse(List l)
{
	List tmp = NULL;
	Elem *p = NULL;

	if(l != NULL)
	{
		tmp = l;
		p = l;
		while(p->next != NULL)
		{
			tmp = insert_head(p, p->data);
			p = p->next;
		}
		return tmp;
	}
	return l;
}
Ejemplo n.º 15
0
/*  Return: (List) list with one more element at the tail of the list
	Data: l (List) list which will receive the new tail
		  d (void*) the data of the new element
	Process: travers the list to reach its end, then add the new element */
List insert_tail(List l, void* d)
{
	Elem *p = NULL, *q = NULL;

	if(l == NULL)
		return insert_head(l, d);
	else
	{
		p = (Elem*) calloc(1, sizeof(Elem));
		if(p != NULL)
		{
			p->data = d;
			p->next = NULL;
			q = l;
			while(q->next != NULL)
				q = q->next;
			q->next = p;
		}
	}
	return l;
}
Ejemplo n.º 16
0
int main(int argc, char *argv[]) {
	printf("\n");
	/*
	 * testing the creation of lists and appending
	 */
	printf("%s\n\n", "Commands Test 1");
	LTICKET ref = create_list("Adewale");
	append_to_list(ref, "Olufemi");
	append_to_list(ref, "Nneka");
	append_to_list(ref, "Jacob");
	append_to_list(ref, "Adetola");
	append_to_list(ref, "Kemi");
	visit_nodes(ref);
	delete_list(ref);

	/*
	 * testing the head and tail removal as well as head insertion
	 */
	printf("%s\n\n", "Commands Test 2");
	LTICKET ref2 = create_list("Adewale");
	append_to_list(ref2, "Olufemi");
	append_to_list(ref2, "Nneka");
	append_to_list(ref2, "Jacob");
	append_to_list(ref2, "Adetola");
	append_to_list(ref2, "Kemi");
	visit_nodes(ref2);
	remove_head(ref2);
	visit_nodes(ref2);
	insert_head(ref2, "Chiamaka");
	visit_nodes(ref2);
	remove_tail(ref2);
	visit_nodes(ref2);
	delete_list(ref2);

	// int check = visit_nodes(ref);
	// if(LE_ISERROR(check))
	// 	printf("%s\n", le_errbuf);
	return 0;
}
Ejemplo n.º 17
0
node_t*  reverse_k_nodes(node_t *list, int k)
{
	if(list == NULL || k == 0)
		return;

	node_t *tmp = list;
	node_t *head = list;
	node_t *revl = NULL;;

	int count;
	int iter;

	count = 0;
	while(tmp && count < k)
	{
		count++;
		node_t *n = tmp;
		tmp = tmp->next;
		insert_head(&revl, n);
	}

	head->next = reverse_k_nodes(tmp, k);
	return revl;
}
Ejemplo n.º 18
0
struct node * append(pid_t pid, int childrv, char**cmd, struct node *head)	{
	//printf("in append \n");
	struct node *new_node = (struct node *)malloc(sizeof(struct node));
	if(head == NULL)
		{
			new_node = insert_head(pid, childrv, cmd, head);
		}
	else	{
    	struct node * current = head;
    	while (current->next != NULL) { //current = last node in list
        current = current->next;
    	}
	

		
		current->next = new_node;
		new_node->pid = pid;
		new_node->next = NULL;
		new_node->cmd = cmd;
		new_node->childrv = childrv;
		
	}
	return new_node;
}
Ejemplo n.º 19
0
void init_menu_insert(AList_WS* list) {
	for (int i = 0; i < 3; ++i)
		printf("%s\n", MAIN_MENU[i]);

	printf("\ta. Insert at Head\n");
	printf("\tb. Insert at Tail\n");
	printf("\tc. Insert at position Next to an index\n");
	printf("\td. Insert at position Prev to an index\n");
	printf("\te. Go Back\n");

	for (int i = 3; i < 8; ++i)
		printf("%s\n", MAIN_MENU[i]);

	printf("\vPLease enter option: ");

	char* input = (char*)(malloc(100));
	memset(input, '-', 100);
	scanf("%s", input);
	char ch = input[0];

	if (isdigit(input[0]) && isdigit(input[1]))
		ch = 'X';

	switch (ch) {

	case '1':
	case 'a':
	case 'H':
	{
		printf("Please enter the value you want to insert: ");
		double data;
		scanf("%lf", &data);
		insert_head(list, data);
		break;
	}

	case '2':
	case 'b':
	case 'T':
	{
		printf("Please enter the value you want to insert: ");
		double data;
		scanf("%lf", &data);
		insert_tail(list, data);
		break;
	}

	case '3':
	case 'c':
	case 'N':
	{
		printf("Please enter the value you want to insert: ");
		double data;
		scanf("%lf", &data);
		printf("Please enter the position");
		size_t pos;
		scanf("%lu", pos);
		insert_next(list,pos,data);
		break;
	}

	case '4':
	case 'd':
	case 'P':
	{
		printf("Please enter the value you want to insert: ");
		double data;
		scanf("%lf", &data);
		printf("Please enter the position");
		size_t pos;
		scanf("%lu", pos);
		insert_prev(list,pos,data);
		break;
	}

	case '5':
	case 'e':
	case 'B':
		break;

	default:
		printf("Please enter a valid choice.\n\n");
		init_menu_insert(list);
	}
}
Ejemplo n.º 20
0
AList_WS* new_list_data(double data) {
	AList_WS* new_list = new_list_size(1);
	insert_head(new_list, data);
	return new_list;
}
Ejemplo n.º 21
0
	void insert_head(list &_list) { insert_head(&_list); }
Ejemplo n.º 22
0
	void relink_head(list *_list) { erase(); insert_head(_list); }
Ejemplo n.º 23
0
PyObject *
PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
{
	PyWeakReference *result = NULL;
	PyWeakReference **list;
	PyWeakReference *ref, *proxy;

	if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
		PyErr_Format(PyExc_TypeError,
				"cannot create weak reference to '%s' object",
				Py_TYPE(ob)->tp_name);
		return NULL;
	}
	list = GET_WEAKREFS_LISTPTR(ob);
	get_basic_refs(*list, &ref, &proxy);
	if (callback == Py_None)
		callback = NULL;
	if (callback == NULL)
		/* attempt to return an existing weak reference if it exists */
		result = proxy;
	if (result != NULL)
		Py_INCREF(result);
	else {
		/* Note: new_weakref() can trigger cyclic GC, so the weakref
		   list on ob can be mutated.  This means that the ref and
		   proxy pointers we got back earlier may have been collected,
		   so we need to compute these values again before we use
		   them. */
		if (PyCallable_Check(ob))
			result = new_weakref(ob, callback, &_PyWeakref_CallableProxyType);
		else
			result = new_weakref(ob, callback, &_PyWeakref_ProxyType);

		if (result != NULL) {
			PyWeakReference *prev;

			//JyNI-note: These lines mess up JyNI-initialization.
			//In new_weakref _PyObject_GC_New(&_PyWeakref_RefType) is
			//called, which caches the tme for _PyWeakref_RefType in
			//jy->jy. Now the type is altered afterwards leading to
			//a wrong tme. Shall we fix it here, or implement a
			//lookup in unconsistent case?
			//(Better fix it here)
//			if (PyCallable_Check(ob))
//				Py_TYPE(result) = &_PyWeakref_CallableProxyType;
//			else
//				Py_TYPE(result) = &_PyWeakref_ProxyType;
			get_basic_refs(*list, &ref, &proxy);
			if (callback == NULL) {
				if (proxy != NULL) {
					/* Someone else added a proxy without a callback
					   during GC.  Return that one instead of this one
					   to avoid violating the invariants of the list
					   of weakrefs for ob. */
					Py_DECREF(result);
					Py_INCREF(result = proxy);
					goto skip_insert;
				}
				prev = ref;
			}
			else
				prev = (proxy == NULL) ? ref : proxy;

			if (prev == NULL)
				insert_head(result, list);
			else
				insert_after(result, prev);
		skip_insert:
			;
		}
	}
	return (PyObject *) result;
}
Ejemplo n.º 24
0
void coro_ready_immediatly(coroutine* coro) 
{
    ASSERT(coro->status == M_FREE || coro->status == M_RUN);
    coro->status = M_READY;
    insert_head(&coro->sched->wait_sched_queue, coro);
}
Ejemplo n.º 25
0
int main(int argc, char **argv) {
  int choix=0;
  int value=0;

  printf("%s",menu);
  fflush(stdout);

  while(1) {
    fflush(stdin);
    choix = getchar();
    printf("\n");

    switch(choix) {
    case 'T' :
    case 't' :
      printf("Valeur du nouvel element ? ");
      scanf("%d",&value);
      if (insert_head(&la_liste,value)!=0) {
	printf("Erreur : impossible d'ajouter l'element %d\n",value);
      };
      break;

    case 'Q' :
    case 'q' :
      printf("Valeur du nouvel element ? ");
      scanf("%d",&value);
      if (insert_tail(&la_liste,value)!=0) {
		printf("Erreur : impossible d'ajouter l'element %d\n",value);
      };
      break;


    case 'F' :
    case 'f' :
		printf("Index de l'element a rechercher ? ");
		scanf("%d", &value);
		list_elem_t* result = find_element(la_liste, value);
		if(result != NULL)
			printf("La valeur de l'element numero %d est %d\n", value, result->value);
		else
			printf("Erreur, l'element numero %d n'existe pas\n", value);
      break;



    case 's' :
    case 'S' :
      printf("Valeur de l'element a supprimer ? ");
	  scanf("%d", &value);
	  if(remove_element(&la_liste, value) != 0)
		printf("Erreur, aucun element ne possede la valeur %d\n", value);
	  else
		printf("L'element de valeur %d a ete supprime avec succes\n", value);	
      break;


    case 'r' :
    case 'R' :
      printf("Renversement des elements de la liste.\n");
	  reverse_list(&la_liste);
      break;


    case 'x' :
    case 'X' :
      return 0;

    default:
      break;
    }
    print_list(la_liste);

    if (nb_malloc!=list_size(la_liste)) {
	printf("\nAttention : il y a une fuite memoire dans votre programme !\n");
	printf("la liste contient %d element, or il y a %d element vivant en memoire !\n",list_size(la_liste),nb_malloc);
    }
    getchar(); // pour consommer un RC et eviter un double affichage du menu !
    printf("%s\n",menu);
  }
  return 0;
}