summaryrefslogtreecommitdiffstats
path: root/include/libtf/heap.h
blob: 24f4767eb96a76809f54bc9e15aff812e40d413a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* heap.h - libtf heap
 *
 * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 or later as
 * published by the Free Software Foundation.
 *
 * See http://www.gnu.org/ for details.
 */

#ifndef TF_HEAP_H
#define TF_HEAP_H

#include <libtf/defines.h>

#define TF_HEAP_D		4
#define TF_HEAP_ITEM0		(TF_HEAP_D - 1)

typedef tf_mtime_t tf_heap_priority;

struct tf_heap_child {
	struct tf_heap_node *	ptr;
	tf_heap_priority	val;
};

struct tf_heap_node {
	uint32_t		index;
};

struct tf_heap_head {
	uint32_t		num_items;
	uint32_t		allocated;
	struct tf_heap_child *	item;
};

int __tf_heap_grow(struct tf_heap_head *head);
void tf_heap_insert(struct tf_heap_node *node, struct tf_heap_head *head,
		    tf_heap_priority val);
void tf_heap_delete(struct tf_heap_node *node, struct tf_heap_head *head);
void tf_heap_change(struct tf_heap_node *node, struct tf_heap_head *head,
		    tf_heap_priority val);

static inline
tf_heap_priority tf_heap_get_value(struct tf_heap_head *head)
{
	return head->item[TF_HEAP_ITEM0].val;
}

static inline
struct tf_heap_node *tf_heap_get_node(struct tf_heap_head *head)
{
	return head->item[TF_HEAP_ITEM0].ptr;
}

static inline
int tf_heap_empty(struct tf_heap_head *head)
{
	return head->num_items == 0;
}

static inline
int tf_heap_prealloc(struct tf_heap_head *head, uint32_t size)
{
	if (unlikely(head->num_items + TF_HEAP_ITEM0 >= head->allocated))
		return __tf_heap_grow(head);
	return 0;
}

#endif