Skip to content
Snippets Groups Projects
astobj2_rbtree.c 53.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	}
    
    	/* Check total obj count. */
    	if (count_obj != ao2_container_count(&self->common)) {
    		ast_log(LOG_ERROR, "Total object count does not match ao2_container_count()!\n");
    		return -1;
    	}
    
    	/* Check total node count. */
    	if (count_node != self->common.nodes) {
    		ast_log(LOG_ERROR, "Total node count of %d does not match stat of %d!\n",
    			count_node, self->common.nodes);
    		return -1;
    	}
    
    	return res;
    }
    
    
    /*! rbtree container virtual method table. */
    static const struct ao2_container_methods v_table_rbtree = {
    	.alloc_empty_clone = (ao2_container_alloc_empty_clone_fn) rb_ao2_alloc_empty_clone,
    	.alloc_empty_clone_debug =
    		(ao2_container_alloc_empty_clone_debug_fn) rb_ao2_alloc_empty_clone_debug,
    	.new_node = (ao2_container_new_node_fn) rb_ao2_new_node,
    	.insert = (ao2_container_insert_fn) rb_ao2_insert_node,
    	.traverse_first = (ao2_container_find_first_fn) rb_ao2_find_first,
    	.traverse_next = (ao2_container_find_next_fn) rb_ao2_find_next,
    	.iterator_next = (ao2_iterator_next_fn) rb_ao2_iterator_next,
    	.destroy = (ao2_container_destroy_fn) rb_ao2_destroy,
    
    	.dump = (ao2_container_display) rb_ao2_dump,
    	.stats = (ao2_container_statistics) rb_ao2_stats,
    	.integrity = (ao2_container_integrity) rb_ao2_integrity,
    
    };
    
    /*!
     * \brief Initialize a rbtree container.
     *
     * \param self Container to initialize.
     * \param options Container behaviour options (See enum ao2_container_opts)
     * \param sort_fn Pointer to a sort function.
     * \param cmp_fn Pointer to a compare function used by ao2_find.
     *
     * \return A pointer to a struct container.
     */
    static struct ao2_container *rb_ao2_container_init(struct ao2_container_rbtree *self,
    	unsigned int options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn)
    {
    	if (!self) {
    		return NULL;
    	}
    
    	self->common.v_table = &v_table_rbtree;
    	self->common.sort_fn = sort_fn;
    	self->common.cmp_fn = cmp_fn;
    	self->common.options = options;
    
    #ifdef AO2_DEBUG
    	ast_atomic_fetchadd_int(&ao2.total_containers, 1);
    
    
    	return (struct ao2_container *) self;
    }
    
    struct ao2_container *__ao2_container_alloc_rbtree(unsigned int ao2_options, unsigned int container_options,
    	ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn)
    {
    	struct ao2_container_rbtree *self;
    
    	if (!sort_fn) {
    		/* Sanity checks. */
    		ast_log(LOG_ERROR, "Missing sort_fn()!\n");
    		return NULL;
    	}
    
    	self = ao2_t_alloc_options(sizeof(*self), container_destruct, ao2_options,
    		"New rbtree container");
    	return rb_ao2_container_init(self, container_options, sort_fn, cmp_fn);
    }
    
    struct ao2_container *__ao2_container_alloc_rbtree_debug(unsigned int ao2_options, unsigned int container_options,
    	ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
    	const char *tag, const char *file, int line, const char *func, int ref_debug)
    {
    	struct ao2_container_rbtree *self;
    
    	if (!sort_fn) {
    		/* Sanity checks. */
    		ast_log(__LOG_ERROR, file, line, func, "Missing sort_fn()!\n");
    		return NULL;
    	}
    
    	self = __ao2_alloc_debug(sizeof(*self),
    		ref_debug ? container_destruct_debug : container_destruct, ao2_options,
    		tag, file, line, func, ref_debug);
    	return rb_ao2_container_init(self, container_options, sort_fn, cmp_fn);
    }