/*
 * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
 * 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 as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#ifndef __KPP_H
#define __KPP_H

struct kpp_packet
{
	__u32		id;
	__u32		error;
	__u64		data[7];
};

struct kpp_struct
{
	struct rb_node		rb_node;
	struct kpp_packet	packet;
};

static inline int kpp_compare(__u64 old, __u64 new)
{
	if (old > new)
		return 1;
	if (old < new)
		return -1;
	return 0;
}

static inline int kpp_insert(struct rb_root *root, struct kpp_struct *kp)
{
	struct rb_node **n = &root->rb_node, *parent = NULL;
	struct kpp_struct *ret = NULL, *tmp;
	int cmp;

	while (*n) {
		parent = *n;

		tmp = rb_entry(parent, struct kpp_struct, rb_node);

		cmp = kpp_compare(tmp->packet.id, kp->packet.id);
		if (cmp < 0)
			n = &parent->rb_left;
		else if (cmp > 0)
			n = &parent->rb_right;
		else {
			ret = tmp;
			break;
		}
	}

	if (ret)
		return -EEXIST;

	rb_link_node(&kp->rb_node, parent, n);
	rb_insert_color(&kp->rb_node, root);

	return 0;
}


#endif /* __KPP_H */

