1158115Sume/*-
2158115Sume * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3158115Sume * All rights reserved.
4158115Sume *
5158115Sume * Redistribution and use in source and binary forms, with or without
6158115Sume * modification, are permitted provided that the following conditions
7158115Sume * are met:
8158115Sume * 1. Redistributions of source code must retain the above copyright
9158115Sume *    notice, this list of conditions and the following disclaimer.
10158115Sume * 2. Redistributions in binary form must reproduce the above copyright
11158115Sume *    notice, this list of conditions and the following disclaimer in the
12158115Sume *    documentation and/or other materials provided with the distribution.
13158115Sume *
14158115Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15158115Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16158115Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17158115Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18158115Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19158115Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20158115Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21158115Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22158115Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23158115Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24158115Sume * SUCH DAMAGE.
25158115Sume *
26158115Sume * $FreeBSD$
27158115Sume */
28158115Sume
29171795Sbushman#ifndef __NSCD_CACHEPLCS_H__
30171795Sbushman#define __NSCD_CACHEPLCS_H__
31158115Sume
32158115Sume#include <sys/queue.h>
33158115Sume
34158115Sume/* common policy definitions */
35158115Sume#define CACHELIB_MAX_FREQUENCY 100
36158115Sume
37158115Sume/*
38158115Sume * cache_policy_item_ represents some abstract cache element in the policy
39158115Sume * queue. connected_item pointers to the corresponding cache_policy_item_ in
40158115Sume * another policy queue.
41158115Sume */
42194112Sdesstruct cache_policy_item_ {
43158115Sume	char	*key;
44158115Sume    	size_t	key_size;
45158115Sume
46158115Sume	size_t	request_count;
47158115Sume	struct timeval last_request_time;
48158115Sume	struct timeval creation_time;
49158115Sume
50158115Sume	struct cache_policy_item_ *connected_item;
51158115Sume};
52158115Sume
53158115Sume/*
54158115Sume * cache_policy_ represents an abstract policy queue. It can be customized by
55158115Sume * setting appropriate function pointers
56158115Sume */
57194112Sdesstruct cache_policy_ {
58194087Sdes	struct cache_policy_item_* (*create_item_func)(void);
59158115Sume	void (*destroy_item_func)(struct cache_policy_item_ *);
60158115Sume
61158115Sume	void (*add_item_func)(struct cache_policy_ *,
62158115Sume		struct cache_policy_item_ *);
63158115Sume	void (*remove_item_func)(struct cache_policy_ *,
64158115Sume		struct cache_policy_item_ *);
65158115Sume	void (*update_item_func)(struct cache_policy_ *,
66158115Sume		struct cache_policy_item_ *);
67158115Sume
68158115Sume	struct cache_policy_item_ *(*get_first_item_func)(
69158115Sume		struct cache_policy_ *);
70158115Sume	struct cache_policy_item_ *(*get_last_item_func)(
71158115Sume		struct cache_policy_ *);
72158115Sume	struct cache_policy_item_ *(*get_next_item_func)(
73158115Sume		struct cache_policy_ *, struct cache_policy_item_ *);
74158115Sume	struct cache_policy_item_ *(*get_prev_item_func)(
75158115Sume		struct cache_policy_ *, struct cache_policy_item_ *);
76158115Sume};
77158115Sume
78158115Sume/*
79158115Sume * LFU cache policy item "inherited" from cache_policy_item_ structure
80158115Sume */
81194112Sdesstruct cache_lfu_policy_item_ {
82158115Sume	struct cache_policy_item_ parent_data;
83158115Sume	int	frequency;
84158115Sume
85158115Sume	TAILQ_ENTRY(cache_lfu_policy_item_) entries;
86158115Sume};
87158115Sume
88158115SumeTAILQ_HEAD(cache_lfu_policy_group_, cache_lfu_policy_item_);
89158115Sume
90158115Sume/*
91158115Sume * LFU policy queue "inherited" from cache_policy_.
92158115Sume */
93194112Sdesstruct cache_lfu_policy_ {
94158115Sume	struct cache_policy_ parent_data;
95158115Sume	struct cache_lfu_policy_group_ groups[CACHELIB_MAX_FREQUENCY];
96158115Sume};
97158115Sume
98158115Sume/*
99158115Sume * LRU and FIFO policies item "inherited" from cache_policy_item_
100158115Sume */
101194112Sdesstruct cache_queue_policy_item_ {
102158115Sume	struct cache_policy_item_ parent_data;
103158115Sume	TAILQ_ENTRY(cache_queue_policy_item_) entries;
104158115Sume};
105158115Sume
106158115Sume/*
107158115Sume * LRU and FIFO policies "inherited" from cache_policy_
108158115Sume */
109194112Sdesstruct cache_queue_policy_ {
110158115Sume	struct cache_policy_ parent_data;
111158115Sume	TAILQ_HEAD(cache_queue_policy_head_, cache_queue_policy_item_) head;
112158115Sume};
113158115Sume
114158115Sumetypedef struct cache_queue_policy_ cache_fifo_policy_;
115158115Sumetypedef struct cache_queue_policy_ cache_lru_policy_;
116158115Sume
117158115Sume/* fifo policy routines */
118194112Sdesstruct cache_policy_ *init_cache_fifo_policy(void);
119194112Sdesvoid destroy_cache_fifo_policy(struct cache_policy_ *);
120158115Sume
121158115Sume/* lru policy routines */
122194112Sdesstruct cache_policy_ *init_cache_lru_policy(void);
123194112Sdesvoid destroy_cache_lru_policy(struct cache_policy_ *);
124158115Sume
125158115Sume/* lfu policy routines */
126194112Sdesstruct cache_policy_ *init_cache_lfu_policy(void);
127194112Sdesvoid destroy_cache_lfu_policy(struct cache_policy_ *);
128158115Sume
129158115Sume#endif
130