Example #1
0
int main(void){
	struct tree tr;
	tr.root = NULL;
	tr.cmpfunc = intcmpfunc;

	insert_int(&tr, 15);
	insert_int(&tr, 5);
	insert_int(&tr, 3);
	insert_int(&tr, 12);
	insert_int(&tr, 10);
	insert_int(&tr, 6);
	insert_int(&tr, 7);
	insert_int(&tr, 16);
	insert_int(&tr, 20);
	insert_int(&tr, 18);
	insert_int(&tr, 23);

	findthree(&tr);

	traverse(tr.root, print_data);

	print_successor(tr.root->left);

	rb_delete(&tr, tr.root->left);
	rb_delete(&tr, tr.root->left->right);
	rb_delete(&tr, tr.root->left->left);
	/*left_rotate(&tr, tr.root->right);
	right_rotate(&tr, tr.root->right);*/

	traverse(tr.root, print_data);

	destroy_node(tr.root);

	return 0;
}
Example #2
0
void resman_stop_watch(struct resman *rman, struct resource *res)
{
    int i, sz;
    struct watch_dir *wdir;

    if(!res->watch_path) {
        return;
    }

    if(!(wdir = rb_find(rman->watchdirs, res->watch_path))) {
        return;
    }

    /* if there is no other reference to this watch dir, destroy it */
    if(--wdir->nref <= 0) {
        /* find the handle in the watch_handles array and remove it */
        sz = dynarr_size(rman->watch_handles);
        for(i=0; i<sz; i++) {
            if(rman->watch_handles[i] == wdir->handle) {
                /* swap the end for it and pop */
                rman->watch_handles[i] = rman->watch_handles[sz - 1];
                rman->watch_handles[sz - 1] = 0;
                dynarr_pop(rman->watch_handles);
                break;
            }
        }

        rb_delete(rman->wdirbyev, wdir->over.hEvent);
        rb_delete(rman->watchdirs, wdir->watch_path);

        CancelIo(wdir->handle);
        CloseHandle(wdir->handle);
        CloseHandle(wdir->over.hEvent);
        free(wdir->watch_path);
        free(wdir);

        res->watch_path = 0;
    } else {
        /* just remove this watch item */
        if(wdir->items && wdir->items->res == res) {
            struct watch_item *tmp = wdir->items;
            wdir->items = wdir->items->next;
            free(tmp);
        } else {
            struct watch_item *wprev = wdir->items;
            struct watch_item *witem = wprev->next;

            while(witem) {
                if(witem->res == res) {
                    struct watch_item *tmp = witem;
                    wprev->next = witem->next;
                    break;
                }
                witem = witem->next;
            }
        }
    }
}
Example #3
0
/*
 * mm_malloc - Allocate a block
 *
 * If there exists a free block where the request fits, get the smallest one, segment it and allocate.
 * If there is no such block, increase brk.
 */
void* mm_malloc(size_t size)
{
    size_t block_size, next_block_size;
    void *free_block, *next_block;
    
    block_size = ALIGN(HEADER_SIZE + size);
    block_size = block_size < MIN_BLOCK_SIZE ? MIN_BLOCK_SIZE : block_size;

    free_block = rb_find(block_size);
    if(free_block == rb_null){ // proper free block not found
        /* set free_block to the end of last block in heap */
        free_block = mem_heap_hi() - 3;
        if(PREV_FREE(free_block)){ // if the last block is free
            /* set free_block to the last block */
            free_block -= PREV_SIZE_MASKED(free_block);
            if(IS_IN_RB(free_block)){
                rb_delete(free_block);
            }
            /* this block is smaller than request, so increase brk */
            mem_sbrk(block_size - CUR_SIZE_MASKED(free_block));
        }else{ // if the last block is not free
            mem_sbrk(block_size);
        }
    }else{
        /* will be allocated, so delete from tree first */
        rb_delete(free_block);
        /* if the block is bigger than request, segment it */
        if((next_block_size = CUR_SIZE_MASKED(free_block) - block_size) > 0){
            next_block = NEXT_BLOCK(free_block, block_size);
            CUR_SIZE(next_block) = PREV_SIZE(NEXT_BLOCK(next_block, next_block_size)) = next_block_size | 1;
            if(IS_IN_RB(next_block)){
                rb_insert(next_block);
            }
        }
    }
    CUR_SIZE(free_block) = PREV_SIZE(NEXT_BLOCK(free_block, block_size)) = block_size;

#ifdef DEBUG
    printf("mm_malloc(%u) called\n", size);
    printf("free_block = %p\n", free_block);
    rb_print_preorder();
    printf("\n");
#endif /* DEBUG */

#ifdef CHECK
    if(!mm_check()){
        rb_print_preorder();
        exit(0);
    }
#endif /* CHECK */

    return USER_BLOCK(free_block);
}
Example #4
0
void m_remove(MTrace * m, void * pdata){
    if (m->cmp_fn(pdata, m->left_max) > 0){
        rb_delete(m->rb2, pdata);
        m->cnt2 -= 1;
        m->right_min = find_right_min(m);
    }
    else{
        rb_delete(m->rb1, pdata);
        m->cnt1 -= 1;
        m->left_max = find_left_max(m);
    }
    balance(m);
}
Example #5
0
static void balance(MTrace * m){
    if (m->cnt1 > m->cnt2 + 1){
        m_insert_right(m, m->left_max);
        rb_delete(m->rb1, m->left_max);
        m->cnt1 -= 1;
        m->left_max = find_left_max(m);
    }
    else if (m->cnt1 + 1 < m->cnt2){
        m_insert_left(m, m->right_min);
        rb_delete(m->rb2, m->right_min);
        m->cnt2 -= 1;
        m->right_min = find_right_min(m);
    }
}
Example #6
0
void delete_CLRS(NODE * t, int v)
{
	NODE * to_del = rb_search(t, v);
	assert(to_del);

	if (dbg_level == 0) {
		fprintf(stderr, "%s~%d%s, ", RED, v, NOCOLOR);
		rb_delete(t, to_del);
		verify_rbtree(t->L, 0);
	} else {
		printf("%sdelete %d%s\n", RED, v, NOCOLOR);
		rb_delete(t, to_del);
		______________________________("./fig/", t, t, "deleted %d", v);
	}
}
Example #7
0
int
main(void)
{
	rbt root;
	int i;
	int key[] = { 1, 2, 4, 5, 7, 8, 11, 14, 15, 39, 29, 73, 24, 18, 18, 18, 18};
	int key2[] = {15, 14, 11, 8, 7, 5, 4, 2, 1, 18, 73, 24, 39, 29, 18, 18, 18};
	/*int key[] = {41, 38, 31, 12, 19, 8};
	int key2[] = { 8, 12, 19, 31, 38, 41};*/
	rbn nil = { BLACK, 0, NULL, NULL, NULL};
	root.nil = &nil;
	root.root = &nil;
	rbn *tmp;
	for (i = 0; i < sizeof(key2) / sizeof(int); i++) {
		tmp = malloc(sizeof(rbn));
		tmp->key = key2[i];
		rb_insert(&root, tmp);
		printf("insert:%d\n", tmp->key);
	}
	for (i = 0; i < sizeof(key) / sizeof(int); i++) {
		tmp = rb_search(root.root, key[i]);
		rb_delete(&root, tmp);
		printf("deleted:%d\n", tmp->key);
	}
	printf("\n");
	return 0;
}
Example #8
0
void BankRBTree::mergeAccount(const string& id1, const string& passwd1, const string& id2, const string& passwd2){
  //find account1's position, if not exist return |NULL|
  string now_id1 = id1;
  DataNode la1 = DataNode(&now_id1, NULL);
  DataNode* res1 = (DataNode*)rb_find(rb_tree, &la1);

  if(res1 == NULL){
    cout << "ID " << id1 << " not found\n";
    return;
  }
  //find account2's position, if not exist return |NULL|
  string now_id2 = id2;
  DataNode la2 = DataNode(&now_id2, NULL);
  DataNode* res2 = (DataNode*)rb_find(rb_tree, &la2);
  
  if(res2 == NULL){
    cout << "ID " << id2 << " not found\n";
    return;
  }
  
  //verify passwd1
  if(res1->second->verifyPassword(passwd1) == false){
    cout << "wrong password1\n";
    return;
  }
  //verify passwd2
  if(res2->second->verifyPassword(passwd2) == false){
    cout << "wrong password2\n";
    return;
  }

  res1->second->mergeAccount(*(res2->second));
  rb_delete(rb_tree, res2);
}
Example #9
0
int main(int argc, char *argv[])
{
	struct rb_node sentinel;
	struct rb_tree rbtree;
	struct rb_node node[1000];
	int i, j;

	rb_init(&rbtree, &sentinel);
	srand(time(0));

	for (i = 0; i < 10; i++) {
		for (;;) {
			node[i].key = rand()%10000;
			for (j = 0; j < i; j++) {
				if (node[i].key == node[j].key)
					break;
			}
			if (j < i)
				continue;
			else
				break;
		}
		rb_insert(&rbtree, &node[i]);
	}
	for (i = 0; i < 10; i++)
		rb_delete(&rbtree, &node[i]);

	exit(0);
}
Example #10
0
/* Add a file to the list. Return the index of the item. */
int plist_add (struct plist *plist, const char *file_name)
{
	assert (plist != NULL);
	assert (plist->items != NULL);

	if (plist->allocated == plist->num) {
		plist->allocated *= 2;
		plist->items = (struct plist_item *)xrealloc (plist->items,
				sizeof(struct plist_item) * plist->allocated);
	}

	plist->items[plist->num].file = xstrdup (file_name);
	plist->items[plist->num].type = file_name ? file_type (file_name)
		: F_OTHER;
	plist->items[plist->num].deleted = 0;
	plist->items[plist->num].title_file = NULL;
	plist->items[plist->num].title_tags = NULL;
	plist->items[plist->num].tags = NULL;
	plist->items[plist->num].mtime = (file_name ? get_mtime(file_name)
			: (time_t)-1);
	plist->items[plist->num].queue_pos = 0;

	if (file_name) {
		rb_delete (plist->search_tree, file_name);
		rb_insert (plist->search_tree, (void *)(intptr_t)plist->num);
	}

	plist->num++;
	plist->not_deleted++;

	return plist->num - 1;
}
Example #11
0
void *
rbthash_remove (rbthash_table_t *tbl, void *key, int keylen)
{
        struct rbthash_bucket   *bucket = NULL;
        rbthash_entry_t         *entry = NULL;
        rbthash_entry_t         searchentry = {0, };
        void                    *dataref = NULL;

        if ((!tbl) || (!key))
                return NULL;

        bucket = rbthash_key_bucket (tbl, key, keylen);
        if (!bucket) {
                gf_log (GF_RBTHASH, GF_LOG_ERROR, "Failed to get bucket");
                return NULL;
        }

        searchentry.key = key;
        searchentry.keylen = keylen;

        LOCK (&bucket->bucketlock);
        {
                entry = rb_delete (bucket->bucket, &searchentry);
        }
        UNLOCK (&bucket->bucketlock);

        if (!entry)
                return NULL;

        GF_FREE (entry->key);
        dataref = entry->data;
        mem_put (tbl->entrypool, entry);

        return dataref;
}
Example #12
0
/*
 * Extract next item (in order) from search queue
 *
 * Returns a GISTSearchItem or NULL.  Caller must pfree item when done with it.
 *
 * NOTE: on successful return, so->curTreeItem is the GISTSearchTreeItem that
 * contained the result item.  Callers can use so->curTreeItem->distances as
 * the distances value for the item.
 */
static GISTSearchItem *
getNextGISTSearchItem(GISTScanOpaque so)
{
	for (;;)
	{
		GISTSearchItem *item;

		/* Update curTreeItem if we don't have one */
		if (so->curTreeItem == NULL)
		{
			so->curTreeItem = (GISTSearchTreeItem *) rb_leftmost(so->queue);
			/* Done when tree is empty */
			if (so->curTreeItem == NULL)
				break;
		}

		item = so->curTreeItem->head;
		if (item != NULL)
		{
			/* Delink item from chain */
			so->curTreeItem->head = item->next;
			if (item == so->curTreeItem->lastHeap)
				so->curTreeItem->lastHeap = NULL;
			/* Return item; caller is responsible to pfree it */
			return item;
		}

		/* curTreeItem is exhausted, so remove it from rbtree */
		rb_delete(so->queue, (RBNode *) so->curTreeItem);
		so->curTreeItem = NULL;
	}

	return NULL;
}
Example #13
0
/* Swap the first item on the playlist with the item with file fname. */
void plist_swap_first_fname (struct plist *plist, const char *fname)
{
	int i;

	assert (plist != NULL);
	assert (fname != NULL);

	i = plist_find_fname (plist, fname);

	if (i != -1 && i != 0) {
		rb_delete (plist->search_tree, fname);
		rb_delete (plist->search_tree, plist->items[0].file);
		plist_swap (plist, 0, i);
		rb_insert (plist->search_tree, NULL);
		rb_insert (plist->search_tree, (void *)(intptr_t)i);
	}
}
Example #14
0
void test_strings(void)
{
    int i;
    char str[512];
    struct rb_node *node;

    struct rb_tree *tree = rb_create(string_cmp,
				     string_free, string_free,
				     string_print, string_print);

    assert( rb_isempty(tree) == 1 );

    srand(4545);
    for (i = 0; i < 10000; i++)
    {
	snprintf(str, sizeof(str), "test%d", rand() % 1000000);

	rb_insert(tree, strdup(str), strdup("value"));
    }

    srand(4545);
    for (i = 0; i < 10000; i++)
    {
	snprintf(str, sizeof(str), "test%d", rand() % 1000000);

	node = rb_find(tree, str);
	assert(node);
    }    

    {
	node = rb_find(tree, "test46554A");
	assert(node == NULL);
    }

    assert( rb_isempty(tree) == 0 );
    assert( rb_size(tree) == 10000 );

    srand(4545);
    for (i = 0; i < 10000; i++)
    {
	snprintf(str, sizeof(str), "test%d", rand() % 1000000);

	node = rb_find(tree, str);
	assert(node);

	rb_delete(tree, node);
    }

    assert( rb_isempty(tree) == 1 );
    assert( rb_size(tree) == 0 );

    rb_destroy(tree);
}
Example #15
0
File: glue.c Project: chazu/btmux
void
zap_unneccessary_hcode(void)
{
	for (;;) {
		zappable_node = -1;
		rb_walk(xcode_tree, WALK_INORDER, zap_check, NULL);
		if(zappable_node >= 0)
			rb_delete(xcode_tree, (void *)zappable_node);
		else
			break;
	}
}
Example #16
0
void
node_delete(RB_TREE *T, int key)
{
    RB_NODE     *node;

    if ((node = rbtree_search(T, T->root, key)) != T->nil) {
        rb_delete(T, node);
        free(node);
    }
    else
        fprintf(stderr, "can't find %d\n", key);
}
Example #17
0
void test_integers(void)
{
    int i;
    intptr_t val;
    struct rb_node *node;

    struct rb_tree *tree = rb_create(integer_cmp,
				     integer_free, integer_free,
				     integer_print, integer_print);

    assert( rb_isempty(tree) == 1 );

    srand(4545);
    for (i = 0; i < 10000; i++)
    {
	val = rand() % 1000000;

	rb_insert(tree, (void*)val, (void*)val);
    }

    srand(4545);
    for (i = 0; i < 10000; i++)
    {
	val = rand() % 1000000;

	node = rb_find(tree, (void*)val);
	assert(node);
    }    

    {
	node = rb_find(tree, (void*)234324);
	assert(node == NULL);
    }

    assert( rb_isempty(tree) == 0 );
    assert( rb_size(tree) == 10000 );

    srand(4545);
    for (i = 0; i < 10000; i++)
    {
	val = rand() % 1000000;

	node = rb_find(tree, (void*)val);
	assert(node);

	rb_delete(tree, node);
    }

    assert( rb_isempty(tree) == 1 );
    assert( rb_size(tree) == 0 );

    rb_destroy(tree);
}
Example #18
0
File: rbtree.c Project: nasa/QuIP
int rb_delete_key(qrb_tree *tree_p, const char *key)
{
	qrb_node *n_p;

	n_p = rb_find(tree_p,key);
	if( n_p == NULL ){
		fprintf(stderr,"rb_delete_key:  failed to find key \"%s\"!?\n",key);
		return -1;
	}
	rb_delete(tree_p,n_p);	// rb_delete frees the memory...
	return 0;
}
Example #19
0
File: vmm.c Project: jefjin/ucore
// remove_vma_struct - remove vma from mm's rb tree link & list link
static int
remove_vma_struct(struct mm_struct *mm, struct vma_struct *vma) {
    assert(mm == vma->vm_mm);
    if (mm->mmap_tree != NULL) {
        rb_delete(mm->mmap_tree, &(vma->rb_link));
    }
    list_del(&(vma->list_link));
    if (vma == mm->mmap_cache) {
        mm->mmap_cache = NULL;
    }
    mm->map_count --;
    return 0;
}
Example #20
0
/* Simple stress test procedure for the red-black tree routines.  Does
   the following:

   * Generate a random number seed.  By default this is generated from
   the current time.  You can also pass an integer seed value on the
   command line if you want to test the same case.  The seed value is
   displayed.

   * Create a tree and insert the integers from 0 up to TREE_SIZE - 1
   into it, in random order.  Verifies and displays the tree structure
   after each insertion.
   
   * Removes each integer from the tree, in a different random order.
   Verifies and displays the tree structure after each deletion.

   * Destroys the tree.

   This is pretty good test code if you write some of your own red-black
   tree routines.  If you do so you will probably want to modify the
   code below so that it increments the random seed and goes on to new
   test cases automatically. */
int main(int argc, char **argv)
{
  int array[TREE_SIZE];
  struct rb_tree *tree;
  int i;

  randomize(argc, argv);

  for (i = 0; i < TREE_SIZE; i++)
    array[i] = i;
  shuffle(array, TREE_SIZE);

  tree = rb_create();

  for (i = 0; i < TREE_SIZE; i++) {
    int result = rb_insert(tree, array[i]);
    if (result != 1) {
      printf("Error %d inserting element %d, %d, into tree.\n",
	     result, i, array[i]);
      exit(EXIT_FAILURE);
    }

    printf("Inserted %d: ", array[i]);
    /*print_structure(tree->root, 0);*/
    rb_walk(tree);
    putchar('\n');

    verify_tree(tree, array);
  }

  shuffle(array, TREE_SIZE);
  for (i = 0; i < TREE_SIZE; i++) {
    if (rb_delete(tree, array[i]) == 0) {
      printf("Error removing element %d, %d, from tree.\n", i, array[i]);
      exit(EXIT_FAILURE);
    }

    printf("Removed %d: ", array[i]);
    /*print_structure(tree->root, 0);*/
    rb_traverse(tree);
    putchar('\n');

    verify_tree(tree, array + i + 1);
  }

  rb_destroy(tree);
  printf("Success!\n");

  return EXIT_SUCCESS;
}
Example #21
0
/* Set file for an item. */
void plist_set_file (struct plist *plist, const int num, const char *file)
{
	assert (LIMIT(num, plist->num));
	assert (file != NULL);

	if (plist->items[num].file) {
		rb_delete (plist->search_tree, file);
		free (plist->items[num].file);
	}

	plist->items[num].file = xstrdup (file);
	plist->items[num].type = file_type (file);
	plist->items[num].mtime = get_mtime (file);
	rb_insert (plist->search_tree, (void *)(intptr_t)num);
}
Example #22
0
int main(void) {
	nil.color = BLACK;
    int arr[10] = {2,3,4,1,6,5,7,9,8};
    rb_node_t* root1 = rb_create(9,arr);
    inordertraverse(root1);
//    printf("%d",root1->lchild->key);
    printf("\n");
    rb_node_t* nodedele = rb_search(root1,4);
    if(nodedele!=NULL)
    {
    	root1 = rb_delete(root1,nodedele);
    }
    inordertraverse(root1);
	return EXIT_SUCCESS;
}
Example #23
0
File: rb_tree.c Project: nmter/tfal
/*
 * find all node whose key is in range [MIN, to]
 * return number of nodes.
 * 
 * 
 */
int rb_del_in_range(_key_t to)//[MIN, to]
{
	struct rb_node *min_ptr = _find_min(this_rb->root), *ptr;
	ap_key tmp;
	int num = 0;
	while(min_ptr && min_ptr->key <= to){
		ptr = _find_succ(min_ptr);

		tmp.node_ptr = min_ptr;
		rb_delete(&tmp, DEL_RUNALL_CB);

		min_ptr = ptr;
		num++;
	}
	return num;
}
Example #24
0
void BankRBTree::deleteAccount(const string& id, const string& passwd){
  //find account's position, if not exist return |NULL|
  string now_id = id;
  DataNode la = DataNode(&now_id, NULL);
  DataNode* res = (DataNode*)rb_find(rb_tree, &la);

  if(res == NULL)
    cout << "ID " << id << " not found\n";

  else
    if(res->second->verifyPassword(passwd) == true){
      rb_delete(rb_tree, res);
      cout << "success\n";
    }
    else
      cout << "wrong password\n";
}
Example #25
0
const void * rbdelete(const void *key, struct rbtree *rbinfo) {
  struct rbnode *x;
  const void *y;
  
  if (rbinfo==NULL)
    return(NULL);
  
  x=rb_traverse(key, rbinfo);
  
  if (x==RBNULL) {
    return(NULL);
  } else {
    y=x->key;
    rb_delete(&rbinfo->rb_root, x);
    
    return(y);
  }
}
Example #26
0
int main (int argc, const char * argv[])
{
    int i = 0;
    int count = 100000;
    key_t key;
    rb_node_t *root = NULL;
    rb_tree *tree = rb_new_tree();
    
    
    
    
    
    srand(time(NULL));
    
    for (i = 1; i < count; i++) {
        key = rand() % count;
        rb_node_t *node = rb_new_node(key, i);
        if (rb_insert(tree,node) != tree->nil) {
            printf("[i = %d] insert key %d success!\n",i,key);
        } else {
            printf("[i = %d] insert key %d failed!\n",i,key);
            exit(1);
        }
        if (rb_search(tree, key) != tree->nil) {
            printf("[i = %d] search key %d success!\n", i, key); 
        }else {
            exit(1);
        }
        
        if (!(i%10)) {
            if (rb_delete(tree, node)) {
                printf("[i = %d] delete key %d success!\n", i, key);
            } else {
                printf("[i = %d] delete key %d failed!\n", i, key);
            }
        }
    }

    
    printf("Hello, World!\n");
    return 0;
}
Example #27
0
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;
}
Example #28
0
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);
}
Example #29
0
/*
 * mm_free - Coalesce with surrounding blocks, and put it to Red-black tree
 */
void mm_free(void *ptr)
{
    size_t size, new_size;
    void *prev, *cur, *next, *new_block;

    cur = ptr - HEADER_SIZE;

    /* double free */
    if(CUR_FREE(cur)){
        printf("try to free a freed memory block(%p) is detected.\n", cur);
        return ;
    }

    new_block = cur;
    new_size = CUR_SIZE_MASKED(cur);

    /* coalesce with the previous block if free */
    if(PREV_FREE(cur)){
        size = PREV_SIZE_MASKED(cur);
        prev = PREV_BLOCK(cur, size);
        if(IS_IN_RB(prev)){
            rb_delete(prev);
        }
        new_block = prev;
        new_size += size;
    }

    /* coalesce with the next block if exists and free */
    size = CUR_SIZE_MASKED(cur);
    next = NEXT_BLOCK(cur, size);
    if(next + 4 <= mem_heap_hi() && CUR_FREE(next)){
        size = CUR_SIZE_MASKED(next);
        if(IS_IN_RB(next)){
            rb_delete(next);
        }
        new_size += size;
    }

    /* new free block setting */
    CUR_SIZE(new_block) = PREV_SIZE(NEXT_BLOCK(new_block, new_size)) = new_size | 1;
    if(IS_IN_RB(new_block)){
        rb_insert(new_block);
    }
    
#ifdef DEBUG
    printf("mm_free(%p) called\n", ptr);
    printf("new_block = %p\n", new_block);
    rb_print_preorder();
    printf("\n");
#endif /* DEBUG */

#ifdef CHECK
    if(!mm_check()){
        rb_print_preorder();
        exit(0);
    }
#endif /* CHECK */

    /* DON't MODIFY THIS STAGE AND LEAVE IT AS IT WAS */
    if (gl_ranges)
        remove_range(gl_ranges, ptr);
}
Example #30
0
int
main (int argc, char **argv)
{
        int                     ret = 0;
        zhd_rb_data_t           *data = NULL;
        zhd_rb_data_t           *getdata = NULL;
        zhd_rb_data_t           tmp = {0,};
        struct rb_table         *rbt = NULL;
        char                    buf[MAX_BUF_LEN] = {0};
        int                     i = 0;
        int                     index = 0;
        int                     randomfd = -1;
        char                    randomdata[50];
        size_t                  randomdata_len = 0;
        char                    *tmpfilename = NULL;


        rbt = rb_create (rb_node_compare, &zhd_param, NULL);

        for (;i < MAX_NODE_NUM;i++) {
                data = (zhd_rb_data_t *)malloc(sizeof (zhd_rb_data_t));
                if (!data) {
                        printf ("malloc error\n");
                        goto out;
                }
                data->index = i;
                sprintf(buf,"%d-XXXXXX",i);
                tmpfilename = mktemp(buf);
                data->buf = strdup(buf);
                memset (buf, 0, MAX_BUF_LEN);

                //printf("insert data:index is %d,%s\n",data->index,data->buf);
                rb_insert (rbt, data);
                if (!getdata) {
                        printf("rb_insert success\n");
                }
        }
        randomfd = open("/dev/random", O_RDONLY);
        if (randomfd == -1) {
                printf ("open /dev/random is error\n");
                goto out;
        }
        printf("get ten data randomly!\n");
        for (i = 0;i < 10;i++) {
                tmp.index = random()%MAX_NODE_NUM;
#if 0
                while (randomdata_len < sizeof randomdata) {
                        ssize_t         result;
                        result = read(randomfd, randomdata+randomdata_len,(sizeof randomdata - randomdata_len));
                        if (result < 0) {
                                /* error */
                                printf ("unable to read /dev/random\n");
                        }
                        randomdata_len += result;
                }
                printf("randomdata is %s\n",randomdata);
#endif
                getdata = rb_delete (rbt, &tmp);
                if (getdata) {
                        printf("get data:index is %d,%s\n",getdata->index,getdata->buf);
                        printf("get data:index is %d,%s\n",tmp.index,tmp.buf);
                } else {
                        printf("get data error\n");
                }

                getdata = rb_find (rbt, &tmp);
                if (getdata) {
                        printf("get data:index is %d,%s\n",getdata->index,getdata->buf);
                        printf("get data:index is %d,%s\n",tmp.index,tmp.buf);
                } else {
                        printf("get data error\n");
                }
        }
        if (randomfd > 0) {
                close(randomfd);
        }
out:
        return ret;
}