2 * Copyright (c) 1990 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)lrutils.c 5.2 (Berkeley) 2/22/91";
39 #endif /* LIBC_SCCS and not lint */
46 * LRUGETPG -- Get a free page from the LRU cache.
48 * This routine grows the cache if necessary, finds an unused page if
49 * it can, and handles flushing dirty buffers to disk.
51 * One of the parameters to this routine (f) is the routine that called
52 * us. If we have to grow the cache, we call this routine recursively
53 * in order to fill the buffer. The reason for this is that we have
54 * two interfaces that call lrugetpg(). Lruget() fills a page from disk,
55 * and lrugetnew() just allocates a new (empty) page.
58 * l -- LRU cache to use.
59 * pgno -- page number for which we want a buffer
60 * nread -- pointer to an int to get number of bytes read
64 * (char *) pointer to buffer to use, or NULL on failure.
67 * The buffer returned is locked down until the user does an
68 * explicit lrurelease() on it.
72 lrugetpg(l
, pgno
, nread
, f
)
82 /* if we're allowed to grow the cache, do so */
83 if (l
->lru_cursz
< l
->lru_csize
) {
86 if ((buffer
= (char *) malloc((unsigned) l
->lru_psize
))
88 return ((char *) NULL
);
90 /* get and LRU list entry */
91 if ((lruent
= (LRU_ENT
*) malloc((unsigned) sizeof(LRU_ENT
)))
93 return ((char *) NULL
);
94 lruent
->l_buffer
= buffer
;
95 lruent
->l_pgno
= pgno
;
96 lruent
->l_flags
= NULL
;
98 /* manage spaghetti */
99 lruent
->l_prev
= (LRU_ENT
*) NULL
;
100 lruent
->l_next
= l
->lru_head
;
101 if (l
->lru_head
!= (LRU_ENT
*) NULL
)
102 l
->lru_head
->l_prev
= lruent
;
103 l
->lru_head
= lruent
;
104 if (l
->lru_tail
== (LRU_ENT
*) NULL
)
105 l
->lru_tail
= lruent
;
107 /* add it to the hash table */
108 ce
= lruhashput(l
, pgno
, lruent
);
111 lruent
= l
->lru_tail
;
113 /* find the oldest unused buffer */
114 while (lruent
!= (LRU_ENT
*) NULL
115 && !(lruent
->l_flags
& F_FREE
))
116 lruent
= lruent
->l_prev
;
118 /* if no free buffer was found, we have to grow the cache */
119 if (lruent
== (LRU_ENT
*) NULL
) {
121 return ((char *) NULL
);
122 return ((*f
)((LRU
) l
, pgno
, nread
));
125 /* okay, found a free buffer -- update hash table and list */
126 ce
= lruhashget(l
, lruent
->l_pgno
);
127 if (lruhashdel(l
, lruent
->l_pgno
) < 0)
128 return ((char *) NULL
);
130 /* flush the old page to disk, if necessary */
131 if (lruent
->l_flags
& F_DIRTY
)
132 if (lruflush(l
, lruent
) < 0)
133 return ((char *) NULL
);
135 /* update stats, hash table, and list */
136 lruent
->l_pgno
= pgno
;
138 ce
= lruhashput(l
, pgno
, lruent
);
139 buffer
= lruent
->l_buffer
;
145 /* lock this page down */
146 lruent
->l_flags
&= ~F_FREE
;
152 * LRUHEAD -- Move an LRU list entry to the head of the list.
154 * The head of the list is where the most recently used item goes.
158 * lruent -- entry to move to the head of the list
164 * Updates the cache's head and tail pointers as required.
175 if (l
->lru_head
== lruent
)
178 next
= lruent
->l_next
;
179 prev
= lruent
->l_prev
;
180 lruent
->l_prev
= (LRU_ENT
*) NULL
;
181 lruent
->l_next
= l
->lru_head
;
182 l
->lru_head
->l_prev
= lruent
;
183 l
->lru_head
= lruent
;
186 if (next
!= (LRU_ENT
*) NULL
)
189 if (l
->lru_tail
== lruent
)
194 * LRUGROW -- Grow the LRU cache
196 * This routine is called only if we can't satisfy a user's get() request
197 * using an existing buffer. We need to rebuild the hash table so that
198 * subsequent lookups work correctly, since the cache size is used to
202 * l -- LRU cache to grow
205 * Zero on success, -1 on failure
218 oldsz
= l
->lru_csize
;
219 newsz
= l
->lru_csize
+ 1;
221 /* get a new hash table */
222 if ((new = (CACHE_ENT
**) malloc((unsigned)newsz
* sizeof(CACHE_ENT
*)))
223 == (CACHE_ENT
**) NULL
)
226 /* build the new hash table */
227 bzero((char *) new, (size_t) (newsz
* sizeof(CACHE_ENT
*)));
228 for (i
= oldsz
; --i
>= 0; ) {
229 for (ce
= l
->lru_cache
[i
]; ce
!= (CACHE_ENT
*) NULL
; ) {
231 h
= ce
->c_pgno
% newsz
;
232 ce
->c_chain
= new[h
];
238 /* get rid of the old hash table, and update the cache */
239 free ((char *) (l
->lru_cache
));
241 l
->lru_csize
= newsz
;