コード例 #1
0
ファイル: log_name_tree.c プロジェクト: zhiyuan2007/logstats
void
name_tree_destroy(name_tree_t *tree)
{
    mem_pool_delete(tree->node_pool);
    mem_pool_delete(tree->name_pool);
    heap_destory(tree->heap);
	lru_list_destroy(tree->lru);
    free(tree->_tree);
    free(tree);
}
コード例 #2
0
ファイル: log_view_stats.c プロジェクト: zhiyuan2007/logstats
unsigned int _view_stats_result(name_tree_t *tree, char *key, int topn, char **buff)
{
    heap_t *copy_heap = heap_copy(tree->heap);
    if (NULL == copy_heap)
    {
        printf("empty heap\n");
        return 0;
    }
    heap_sort(copy_heap);
    int i;
            
    StatsReply reply = STATS_REPLY__INIT;

    reply.key = key;
    int _min = min(topn, copy_heap->len);
    reply.n_name = _min; 
    reply.n_count = _min; 
    char **pdata = malloc(sizeof (char *) * _min);
    memset(pdata, 0, sizeof(char *)*_min);
    int32_t *pcount = malloc(sizeof (int32_t) * _min);
    for(i = 0; i < reply.n_name; i++)
    {
        node_value_t *nv = (node_value_t *)(copy_heap->heap[i]);
        /*
         * new memory and copy if need absolutely right domain
        pdata[i] = malloc(sizeof(char)* (strlen(nv->fqdn) + 1));
        memcpy(pdata[i], nv->fqdn, (strlen(nv->fqdn) + 1));
        printf("malloc: addr of %d ptr is %p\n", i, pdata[i]);
        */
        pdata[i] = nv->fqdn;
        pcount[i]= nv->count;
    }
    
    reply.name = pdata;
    reply.count = pcount;
    unsigned int len = stats_reply__get_packed_size(&reply);
    char *result_ptr = malloc(sizeof(char) *len);
    stats_reply__pack(&reply, result_ptr);
    heap_destory(copy_heap);
    *buff = result_ptr;
    /* free mem if malloced above
    for (i=0; i<reply.n_name; i++)
    {
        printf("free addr of %d ptr is %p\n", i, pdata[i]);
        if (pdata[i] != NULL)
            free(pdata[i]);
    }
    */
    free(pcount);
    free(pdata);
    return len;
}
コード例 #3
0
ファイル: log_name_tree.c プロジェクト: zhiyuan2007/logstats
name_tree_t *
name_tree_create()
{
    name_tree_t *tree = calloc(1, sizeof(name_tree_t));
    if (!tree)
        return NULL;

    tree->_tree = rbtree_create(name_compare);
    if (!tree->_tree)
        goto MEM_ALLOC_FAILED;

    tree->heap = heap_init(1000, elem_compare);
    if (!tree->heap)
        goto MEM_ALLOC_FAILED;

    tree->node_pool = mem_pool_create(sizeof(rbnode_t), 1024, true);
    if (!tree->node_pool)
        goto MEM_ALLOC_FAILED;

    tree->name_pool = mem_pool_create(sizeof(node_value_t), 1024, true);
    if (!tree->name_pool)
        goto MEM_ALLOC_FAILED;

	tree->lru = lru_list_create();
	if (!tree->lru)
		goto MEM_ALLOC_FAILED;

    tree->count = 0;

    return tree;

MEM_ALLOC_FAILED :
    if (tree->name_pool) mem_pool_delete(tree->name_pool);
    if (tree->node_pool) mem_pool_delete(tree->node_pool);
    if (tree->_tree) free(tree->_tree);
    if (tree->heap) heap_destory(tree->heap);
	if (tree->lru) lru_list_destroy(tree->lru);
    free(tree);
}
コード例 #4
0
int main(void)
{
	uint32_t cnt, i, data[] =
	                    {14, 2, 22, 13, 23, 10, 90, 36, 108, 12,
	                      9, 91, 1, 51, 11, 3, 15, 80, 3, 78, 53,
	                      5, 12, 21, 65, 70, 4};
	const uint32_t data_len = sizeof(data)/sizeof(uint32_t);
	struct heap heap = heap_create(data_len + 5);

	printf(COL_BEG "push data...\n" COL_END);
	for (i = 0; i < data_len; i++)
		if (!heap_full(&heap))
			heap_push(&heap, &data[i]);

	printf("data len = %u, heap size = %u.\n", data_len,
	       heap_size(&heap));

	printf(COL_BEG "heap tree:\n" COL_END);
	heap_print_tr(&heap, &test_print);

	printf(COL_BEG "heap array:\n" COL_END);
	heap_print_arr(&heap, &test_print);

	heap_set_callbk(&heap, &test_less_than);

	printf(COL_BEG "after heapify:\n" COL_END);
	minheap_heapify(&heap);
	heap_print_tr(&heap, &test_print);

	cnt = 0;
	printf(COL_BEG "ranking emulation...:\n" COL_END);
	while (cnt < 100) {
		i = (i + 1) % data_len;
		if (!heap_full(&heap)) {
			printf("insert %d\n", data[i]);
			minheap_insert(&heap, &data[i]);
		} else {
			void *top = heap_top(&heap);
			if (test_less_than(top, &data[i])) {
				printf("replace with %d\n", data[i]);
				minheap_delete(&heap, 0);
				minheap_insert(&heap, &data[i]);
			}
		}
		cnt ++;
	}

	printf(COL_BEG "a heavy heap tree now:\n" COL_END);
	heap_print_tr(&heap, &test_print);

	minheap_sort(&heap);
	printf(COL_BEG "heap array after min-heap sort:\n" COL_END);
	heap_print_arr(&heap, &test_print);

	heap_destory(&heap);

	printf(COL_BEG "a new heap...\n" COL_END);
	heap = heap_create(data_len + 5);
	heap_set_callbk(&heap, &test_less_than);

	for (i = 0; i < data_len; i++)
		if (!heap_full(&heap))
			heap_push(&heap, &data[i]);

	printf(COL_BEG "heap array:\n" COL_END);
	heap_print_arr(&heap, &test_print);

	heap_sort_desc(&heap);
	printf(COL_BEG "heap array after heap sort:\n" COL_END);
	heap_print_arr(&heap, &test_print);

	heap_print_tr(&heap, &test_print);
	heap_destory(&heap);

	printf(COL_BEG "sort a heap with one element...\n" COL_END);

	heap = heap_create(data_len + 5);
	heap_set_callbk(&heap, &test_less_than);
	heap_push(&heap, &data[0]);
	heap_print_tr(&heap, &test_print);
	heap_sort_desc(&heap);

	heap_destory(&heap);
	return 0;
}