예제 #1
0
파일: vmm.c 프로젝트: jefjin/ucore
// insert_vma_struct -insert vma in mm's rb tree link & list link
void
insert_vma_struct(struct mm_struct *mm, struct vma_struct *vma) {
    assert(vma->vm_start < vma->vm_end);
    list_entry_t *list = &(mm->mmap_list);
    list_entry_t *le_prev = list, *le_next;
    if (mm->mmap_tree != NULL) {
        struct vma_struct *mmap_prev;
        insert_vma_rb(mm->mmap_tree, vma, &mmap_prev);
        if (mmap_prev != NULL) {
            le_prev = &(mmap_prev->list_link);
        }
    }
    else {
        list_entry_t *le = list;
        while ((le = list_next(le)) != list) {
            struct vma_struct *mmap_prev = le2vma(le, list_link);
            if (mmap_prev->vm_start > vma->vm_start) {
                break;
            }
            le_prev = le;
        }
    }

    le_next = list_next(le_prev);

    /* check overlap */
    if (le_prev != list) {
        check_vma_overlap(le2vma(le_prev, list_link), vma);
    }
    if (le_next != list) {
        check_vma_overlap(vma, le2vma(le_next, list_link));
    }

    vma->vm_mm = mm;
    list_add_after(le_prev, &(vma->list_link));

    mm->map_count ++;
    if (mm->mmap_tree == NULL && mm->map_count >= RB_MIN_MAP_COUNT) {

        /* try to build red-black tree now, but may fail. */
        mm->mmap_tree = rb_tree_create(vma_compare);

        if (mm->mmap_tree != NULL) {
            list_entry_t *list = &(mm->mmap_list), *le = list;
            while ((le = list_next(le)) != list) {
                insert_vma_rb(mm->mmap_tree, le2vma(le, list_link), NULL);
            }
        }
    }
}
예제 #2
0
파일: main.c 프로젝트: godspeed1989/misc
int main()
{
	int i;
	rb_tree_t tree;
	rb_node_t *node;
	rb_tree_create(&tree, NULL);

	for (i = 0; i < MAX; i++)
		rb_insert(&tree, i, NULL);
	rb_clear(&tree);
	for (i = 0; i < MAX; i+=2)
		rb_insert(&tree, i, NULL);
	for (i = 0; i < MAX; i+=4)
	{
		node = rb_find(&tree, i);
		rb_delete(&tree, node, FALSE);
	}

	rb_tree_destroy(&tree);
	return 0;
}
예제 #3
0
파일: rb_tree.c 프로젝트: 121786404/liunix
void
check_rb_tree(void) {
    rb_tree *tree = rb_tree_create(check_compare1);
    assert(tree != NULL);

    rb_node *nil = tree->nil, *root = tree->root;
    assert(!nil->red && root->left == nil);

    int total = 1000;
    struct check_data **all = check_safe_kmalloc(sizeof(struct check_data *) * total);

    long i;
    for (i = 0; i < total; i ++) {
        all[i] = check_safe_kmalloc(sizeof(struct check_data));
        all[i]->data = i;
    }

    int *mark = check_safe_kmalloc(sizeof(int) * total);
    memset(mark, 0, sizeof(int) * total);

    for (i = 0; i < total; i ++) {
        mark[all[i]->data] = 1;
    }
    for (i = 0; i < total; i ++) {
        assert(mark[i] == 1);
    }

    for (i = 0; i < total; i ++) {
        int j = (rand() % (total - i)) + i;
        struct check_data *z = all[i];
        all[i] = all[j];
        all[j] = z;
    }

    memset(mark, 0, sizeof(int) * total);
    for (i = 0; i < total; i ++) {
        mark[all[i]->data] = 1;
    }
    for (i = 0; i < total; i ++) {
        assert(mark[i] == 1);
    }

    for (i = 0; i < total; i ++) {
        rb_insert(tree, &(all[i]->rb_link));
        check_tree(tree, root->left);
    }

    rb_node *node;
    for (i = 0; i < total; i ++) {
        node = rb_search(tree, check_compare2, (void *)(all[i]->data));
        assert(node != NULL && node == &(all[i]->rb_link));
    }

    for (i = 0; i < total; i ++) {
        node = rb_search(tree, check_compare2, (void *)i);
        assert(node != NULL && rbn2data(node)->data == i);
        rb_delete(tree, node);
        check_tree(tree, root->left);
    }

    assert(!nil->red && root->left == nil);

    long max = 32;
    if (max > total) {
        max = total;
    }

    for (i = 0; i < max; i ++) {
        all[i]->data = max;
        rb_insert(tree, &(all[i]->rb_link));
        check_tree(tree, root->left);
    }

    for (i = 0; i < max; i ++) {
        node = rb_search(tree, check_compare2, (void *)max);
        assert(node != NULL && rbn2data(node)->data == max);
        rb_delete(tree, node);
        check_tree(tree, root->left);
    }

    assert(rb_tree_empty(tree));

    for (i = 0; i < total; i ++) {
        rb_insert(tree, &(all[i]->rb_link));
        check_tree(tree, root->left);
    }

    rb_tree_destroy(tree);

    for (i = 0; i < total; i ++) {
        kfree(all[i]);
    }

    kfree(mark);
    kfree(all);
}
예제 #4
0
파일: extstruct.c 프로젝트: drpicox/mcxx
void extensible_struct_init(extensible_struct_t** extensible_struct)
{
    (*extensible_struct) = xcalloc(1, sizeof((*extensible_struct)));
    (*extensible_struct)->hash = rb_tree_create(strcmp_vptr, null_dtor_func, null_dtor_func);
}
int main() {
//  stk_stack* enumResult;
  int option=0;
  int newKey,newKey2;
  int* newInt;
  red_black_node_struct* newNode;
  red_black_tree_struct* tree;

  tree=rb_tree_create(IntComp,IntDest,InfoDest,IntPrint,InfoPrint);
  while(option!=8) {
    printf("choose one of the following:\n");
    printf("(1) add to tree\n(2) delete from tree\n(3) query\n");
    printf("(4) find predecessor\n(5) find sucessor\n(6) enumerate\n");
    printf("(7) print tree\n(8) quit\n");
    do option=fgetc(stdin); while(-1 != option && isspace(option));
    option-='0';
    switch(option)
      {
      case 1:
	{
	  printf("type key for new node\n");
	  scanf("%i",&newKey);
	  newInt=(int*) malloc(sizeof(int));
	  *newInt=newKey;
	  rb_tree_insert(tree,newInt,0);
	}
	break;
	
      case 2:
	{
	  printf("type key of node to remove\n");
	  scanf("%i",&newKey);
	  if ( ( newNode=rb_exact_query(tree,&newKey ) ) ) rb_delete(tree,newNode);/*assignment*/
	  else printf("key not found in tree, no action taken\n");
	}
	break;

      case 3:
	{
	  printf("type key of node to query for\n");
	  scanf("%i",&newKey);
	  if ( ( newNode = rb_exact_query(tree,&newKey) ) ) {/*assignment*/
	    printf("data found in tree at location %i\n",(int)newNode);
	  } else {
	    printf("data not in tree\n");
	  }
	}
	break;
      case 4:
	{
	  printf("type key of node to find predecessor of\n");
	  scanf("%i",&newKey);
	  if ( ( newNode = rb_exact_query(tree,&newKey) ) ) {/*assignment*/
	    newNode=tree_predecessor(tree,newNode);
	    if(tree->nil == newNode) {
	      printf("there is no predecessor for that node (it is a minimum)\n");
	    } else {
	      printf("predecessor has key %i\n",*(int*)newNode->key);
	    }
	  } else {
	    printf("data not in tree\n");
	  }
	}
	break;
      case 5:
	{
	  printf("type key of node to find successor of\n");
	  scanf("%i",&newKey);
	  if ( (newNode = rb_exact_query(tree,&newKey) ) ) {
	    newNode=tree_successor(tree,newNode);
	    if(tree->nil == newNode) {
	      printf("there is no successor for that node (it is a maximum)\n");
	    } else {
	      printf("successor has key %i\n",*(int*)newNode->key);
	    }
	  } else {
	    printf("data not in tree\n");
	  }
	}
	break;
      case 6:
	{
	  printf("type low and high keys to see all keys between them\n");
/* 	  scanf("%i %i",&newKey,&newKey2);
	  enumResult=rbEnumerate(tree,&newKey,&newKey2);	  
	  while ( (newNode = StackPop(enumResult)) ) {
	    tree->PrintKey(newNode->key);
	    printf("\n");
	  }
	  free(enumResult);
	}*/
	break;
      case 7:
	{
	  rb_tree_print(tree);
	}
	break;
      case 8:
	{
		red_black_node_struct *ss ;//= (red_black_node_struct*)start( tree);
		for ( ss= start( tree) ; ss != NULL ; ss = next(tree) ){
			printf("%d\n",*(int*)ss->key);

		}
	  rb_tree_destroy(tree);
	  return 0;
	}
	break;
      default:
	printf("Invalid input; Please try again.\n");
      }
  }
}
  return 0;
}
예제 #6
0
status_t memory_pool_create(
    size_t chunkSize,
    size_t minReserve,
    size_t maxReserve,
    size_t maxBytes,
    useconds_t periodMicroseconds,
    memory_pool_handle_t* p_handle)
{
    status_t err = NO_ERROR;
    int pthreadErr = 0;
    pthread_attr_t pthreadAttr;
    memory_pool_t* pool = NULL;
    size_t chunkCount = 0;

    /* check input variables */
    if (
        (chunkSize < 1) ||
        (minReserve > maxReserve) ||
        (maxReserve < 1) ||
        (maxBytes < (maxReserve * chunkSize)) ||
        (periodMicroseconds <= 0))
    {
        return ERR_INVALID_ARGUMENT;
    }

    /* alloc/init struct */
    pool = (memory_pool_t*)malloc(sizeof(memory_pool_t));
    if (NULL == pool) {
        return ERR_FAILED_ALLOC;
    }

    pool->chunk_size = chunkSize;
    pool->min_reserve_chunks = minReserve;
    pool->max_reserve_chunks = maxReserve;
    pool->max_chunks = maxBytes / chunkSize;
    pool->generated_chunks = 0;
    pool->period_microseconds = periodMicroseconds;
    pool->claimed = NULL;
    pool->generated = NULL;
    pool->available = NULL;
    pool->thread = NULL;
    pool->kill_thread = 0;

    /* create chunk containers */
    err = stack_create(maxReserve, &(pool->generated));
    if (NO_ERROR != err) {
        memory_pool_release(pool);
        return err;
    }

    err = stack_create(maxBytes / chunkSize, &(pool->available));
    if (NO_ERROR != err) {
        memory_pool_release(pool);
        return err;
    }

    err = rb_tree_create(
              &_rb_tree_compare,
              &_rb_tree_alloc_copy_key,
              &_rb_tree_release_key,
              &_rb_tree_alloc_copy_data,
              &_rb_tree_release_data,
              &(pool->claimed));
    if (NO_ERROR != err) {
        memory_pool_release(pool);
        return err;
    }

    /* alloc minimum amount of data before creating thread */
    while ((chunkCount < pool->min_reserve_chunks) && (err == NO_ERROR)) {
        err = _memory_pool_generate_chunk(pool, pool->available);
        chunkCount++;
    }

    /* create generator thread */
    pthreadErr = pthread_mutex_init(&(pool->mutex), NULL);
    if (pthreadErr) {
        memory_pool_release(pool);
        return ERR_FAILED_THREAD_CREATE;
    }

    pthreadErr = pthread_attr_init(&pthreadAttr);
    if (pthreadErr) {
        memory_pool_release(pool);
        return ERR_FAILED_THREAD_CREATE;
    }

    pthreadErr = pthread_attr_setdetachstate(
                     &pthreadAttr,
                     PTHREAD_CREATE_JOINABLE);
    if (pthreadErr) {
        pthread_attr_destroy(&pthreadAttr);
        memory_pool_release(pool);
        return ERR_FAILED_THREAD_CREATE;
    }

    pthreadErr = pthread_create(
                     &(pool->thread),
                     &pthreadAttr,
                     _memory_pool_thread,
                     (void*)pool);
    pthread_attr_destroy(&pthreadAttr);
    if (pthreadErr) {
        memory_pool_release(pool);
        return ERR_FAILED_THREAD_CREATE;
    }

    *p_handle = pool;

    return NO_ERROR;
}