cacheplcs.h revision 194112
1779Sjoehw/*-
2968Sfyuan * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3779Sjoehw * All rights reserved.
4779Sjoehw *
5779Sjoehw * Redistribution and use in source and binary forms, with or without
6779Sjoehw * modification, are permitted provided that the following conditions
7779Sjoehw * are met:
8779Sjoehw * 1. Redistributions of source code must retain the above copyright
9779Sjoehw *    notice, this list of conditions and the following disclaimer.
10779Sjoehw * 2. Redistributions in binary form must reproduce the above copyright
11779Sjoehw *    notice, this list of conditions and the following disclaimer in the
12779Sjoehw *    documentation and/or other materials provided with the distribution.
13779Sjoehw *
14779Sjoehw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15779Sjoehw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16779Sjoehw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17779Sjoehw * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18779Sjoehw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19779Sjoehw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20779Sjoehw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21779Sjoehw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22779Sjoehw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23779Sjoehw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24779Sjoehw * SUCH DAMAGE.
25779Sjoehw *
26779Sjoehw * $FreeBSD: head/usr.sbin/nscd/cacheplcs.h 194112 2009-06-13 14:12:55Z des $
27779Sjoehw */
28779Sjoehw
29779Sjoehw#ifndef __NSCD_CACHEPLCS_H__
30779Sjoehw#define __NSCD_CACHEPLCS_H__
31779Sjoehw
32779Sjoehw#include <sys/queue.h>
33779Sjoehw
34779Sjoehw/* common policy definitions */
35968Sfyuan#define CACHELIB_MAX_FREQUENCY 100
36779Sjoehw
37779Sjoehw/*
38779Sjoehw * cache_policy_item_ represents some abstract cache element in the policy
39779Sjoehw * queue. connected_item pointers to the corresponding cache_policy_item_ in
40779Sjoehw * another policy queue.
41779Sjoehw */
42968Sfyuanstruct cache_policy_item_ {
43968Sfyuan	char	*key;
44968Sfyuan    	size_t	key_size;
45968Sfyuan
46779Sjoehw	size_t	request_count;
47779Sjoehw	struct timeval last_request_time;
48968Sfyuan	struct timeval creation_time;
49779Sjoehw
50779Sjoehw	struct cache_policy_item_ *connected_item;
51779Sjoehw};
52779Sjoehw
53779Sjoehw/*
54779Sjoehw * cache_policy_ represents an abstract policy queue. It can be customized by
55779Sjoehw * setting appropriate function pointers
56779Sjoehw */
57779Sjoehwstruct cache_policy_ {
58779Sjoehw	struct cache_policy_item_* (*create_item_func)(void);
59779Sjoehw	void (*destroy_item_func)(struct cache_policy_item_ *);
60779Sjoehw
61779Sjoehw	void (*add_item_func)(struct cache_policy_ *,
62779Sjoehw		struct cache_policy_item_ *);
63779Sjoehw	void (*remove_item_func)(struct cache_policy_ *,
64779Sjoehw		struct cache_policy_item_ *);
65779Sjoehw	void (*update_item_func)(struct cache_policy_ *,
66779Sjoehw		struct cache_policy_item_ *);
67779Sjoehw
68779Sjoehw	struct cache_policy_item_ *(*get_first_item_func)(
69779Sjoehw		struct cache_policy_ *);
70779Sjoehw	struct cache_policy_item_ *(*get_last_item_func)(
71779Sjoehw		struct cache_policy_ *);
72779Sjoehw	struct cache_policy_item_ *(*get_next_item_func)(
73779Sjoehw		struct cache_policy_ *, struct cache_policy_item_ *);
74779Sjoehw	struct cache_policy_item_ *(*get_prev_item_func)(
75779Sjoehw		struct cache_policy_ *, struct cache_policy_item_ *);
76779Sjoehw};
77779Sjoehw
78779Sjoehw/*
79779Sjoehw * LFU cache policy item "inherited" from cache_policy_item_ structure
80779Sjoehw */
81779Sjoehwstruct cache_lfu_policy_item_ {
82779Sjoehw	struct cache_policy_item_ parent_data;
83779Sjoehw	int	frequency;
84779Sjoehw
85779Sjoehw	TAILQ_ENTRY(cache_lfu_policy_item_) entries;
86779Sjoehw};
87779Sjoehw
88779SjoehwTAILQ_HEAD(cache_lfu_policy_group_, cache_lfu_policy_item_);
89779Sjoehw
90779Sjoehw/*
91779Sjoehw * LFU policy queue "inherited" from cache_policy_.
92779Sjoehw */
93779Sjoehwstruct cache_lfu_policy_ {
94779Sjoehw	struct cache_policy_ parent_data;
95779Sjoehw	struct cache_lfu_policy_group_ groups[CACHELIB_MAX_FREQUENCY];
96779Sjoehw};
97779Sjoehw
98779Sjoehw/*
99779Sjoehw * LRU and FIFO policies item "inherited" from cache_policy_item_
100779Sjoehw */
101779Sjoehwstruct cache_queue_policy_item_ {
102779Sjoehw	struct cache_policy_item_ parent_data;
103779Sjoehw	TAILQ_ENTRY(cache_queue_policy_item_) entries;
104779Sjoehw};
105779Sjoehw
106779Sjoehw/*
107779Sjoehw * LRU and FIFO policies "inherited" from cache_policy_
108779Sjoehw */
109779Sjoehwstruct cache_queue_policy_ {
110779Sjoehw	struct cache_policy_ parent_data;
111779Sjoehw	TAILQ_HEAD(cache_queue_policy_head_, cache_queue_policy_item_) head;
112779Sjoehw};
113779Sjoehw
114779Sjoehwtypedef struct cache_queue_policy_ cache_fifo_policy_;
115779Sjoehwtypedef struct cache_queue_policy_ cache_lru_policy_;
116779Sjoehw
117779Sjoehw/* fifo policy routines */
118struct cache_policy_ *init_cache_fifo_policy(void);
119void destroy_cache_fifo_policy(struct cache_policy_ *);
120
121/* lru policy routines */
122struct cache_policy_ *init_cache_lru_policy(void);
123void destroy_cache_lru_policy(struct cache_policy_ *);
124
125/* lfu policy routines */
126struct cache_policy_ *init_cache_lfu_policy(void);
127void destroy_cache_lfu_policy(struct cache_policy_ *);
128
129#endif
130