Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / search / hash_buf.c
blob7f8e90b6795cef3b2208d72fccb1d8153162626a
1 /*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Margo Seltzer.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <sys/param.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)hash_buf.c 8.5 (Berkeley) 7/15/94";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
40 * PACKAGE: hash
42 * DESCRIPTION:
43 * Contains buffer management
45 * ROUTINES:
46 * External
47 * __buf_init
48 * __get_buf
49 * __buf_free
50 * __reclaim_buf
51 * Internal
52 * newbuf
55 #include <sys/param.h>
57 #include <stddef.h>
58 #include <stdio.h>
59 #include <stdlib.h>
61 #ifdef DEBUG
62 #include <assert.h>
63 #endif
65 #include "db_local.h"
66 #include "hash.h"
67 #include "page.h"
68 #include "extern.h"
70 static BUFHEAD *newbuf(HTAB *, __uint32_t, BUFHEAD *);
72 /* Unlink B from its place in the lru */
73 #define BUF_REMOVE(B) { \
74 (B)->prev->next = (B)->next; \
75 (B)->next->prev = (B)->prev; \
78 /* Insert B after P */
79 #define BUF_INSERT(B, P) { \
80 (B)->next = (P)->next; \
81 (B)->prev = (P); \
82 (P)->next = (B); \
83 (B)->next->prev = (B); \
86 #define MRU hashp->bufhead.next
87 #define LRU hashp->bufhead.prev
89 #define MRU_INSERT(B) BUF_INSERT((B), &hashp->bufhead)
90 #define LRU_INSERT(B) BUF_INSERT((B), LRU)
92 /* Macros for min/max. */
93 #ifndef MIN
94 #define MIN(a,b) (((a)<(b))?(a):(b))
95 #endif
96 #ifndef MAX
97 #define MAX(a,b) (((a)>(b))?(a):(b))
98 #endif
101 * We are looking for a buffer with address "addr". If prev_bp is NULL, then
102 * address is a bucket index. If prev_bp is not NULL, then it points to the
103 * page previous to an overflow page that we are trying to find.
105 * CAVEAT: The buffer header accessed via prev_bp's ovfl field may no longer
106 * be valid. Therefore, you must always verify that its address matches the
107 * address you are seeking.
109 extern BUFHEAD *
110 __get_buf(
111 HTAB *hashp,
112 __uint32_t addr,
113 BUFHEAD *prev_bp,
114 int newpage /* If prev_bp set, indicates a new overflow page. */
117 BUFHEAD *bp;
118 __uint32_t is_disk_mask;
119 int is_disk, segment_ndx = 0;
120 SEGMENT segp = NULL;
122 is_disk = 0;
123 is_disk_mask = 0;
124 if (prev_bp) {
125 bp = prev_bp->ovfl;
126 if (!bp || (bp->addr != addr))
127 bp = NULL;
128 if (!newpage)
129 is_disk = BUF_DISK;
130 } else {
131 /* Grab buffer out of directory */
132 segment_ndx = addr & (hashp->SGSIZE - 1);
134 /* valid segment ensured by __call_hash() */
135 segp = hashp->dir[addr >> hashp->SSHIFT];
136 #ifdef DEBUG
137 assert(segp != NULL);
138 #endif
139 bp = PTROF(segp[segment_ndx]);
140 is_disk_mask = ISDISK(segp[segment_ndx]);
141 is_disk = is_disk_mask || !hashp->new_file;
144 if (!bp) {
145 bp = newbuf(hashp, addr, prev_bp);
146 if (!bp ||
147 __get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0))
148 return (NULL);
149 if (!prev_bp)
150 segp[segment_ndx] =
151 (BUFHEAD *)((ptrdiff_t)bp | (intptr_t)is_disk_mask);
152 } else {
153 BUF_REMOVE(bp);
154 MRU_INSERT(bp);
156 return (bp);
160 * We need a buffer for this page. Either allocate one, or evict a resident
161 * one (if we have as many buffers as we're allowed) and put this one in.
163 * If newbuf finds an error (returning NULL), it also sets errno.
165 static BUFHEAD *
166 newbuf(
167 HTAB *hashp,
168 __uint32_t addr,
169 BUFHEAD *prev_bp
172 BUFHEAD *bp; /* The buffer we're going to use */
173 BUFHEAD *xbp; /* Temp pointer */
174 BUFHEAD *next_xbp;
175 SEGMENT segp;
176 int segment_ndx;
177 __uint16_t oaddr, *shortp;
179 oaddr = 0;
180 bp = LRU;
182 * If LRU buffer is pinned, the buffer pool is too small. We need to
183 * allocate more buffers.
185 if (hashp->nbufs || (bp->flags & BUF_PIN)) {
186 /* Allocate a new one */
187 if ((bp = (BUFHEAD *)malloc(sizeof(BUFHEAD))) == NULL)
188 return (NULL);
189 #ifdef PURIFY
190 memset(bp, 0xff, sizeof(BUFHEAD));
191 #endif
192 if ((bp->page = (char *)malloc(hashp->BSIZE)) == NULL) {
193 free(bp);
194 return (NULL);
196 #ifdef PURIFY
197 memset(bp->page, 0xff, hashp->BSIZE);
198 #endif
199 if (hashp->nbufs)
200 hashp->nbufs--;
201 } else {
202 /* Kick someone out */
203 BUF_REMOVE(bp);
205 * If this is an overflow page with addr 0, it's already been
206 * flushed back in an overflow chain and initialized.
208 if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
210 * Set oaddr before __put_page so that you get it
211 * before bytes are swapped.
213 shortp = (__uint16_t *)bp->page;
214 if (shortp[0])
215 oaddr = shortp[shortp[0] - 1];
216 if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page,
217 bp->addr, (int)IS_BUCKET(bp->flags), 0))
218 return (NULL);
220 * Update the pointer to this page (i.e. invalidate it).
222 * If this is a new file (i.e. we created it at open
223 * time), make sure that we mark pages which have been
224 * written to disk so we retrieve them from disk later,
225 * rather than allocating new pages.
227 if (IS_BUCKET(bp->flags)) {
228 segment_ndx = bp->addr & (hashp->SGSIZE - 1);
229 segp = hashp->dir[bp->addr >> hashp->SSHIFT];
230 #ifdef DEBUG
231 assert(segp != NULL);
232 #endif
234 if (hashp->new_file &&
235 ((bp->flags & BUF_MOD) ||
236 ISDISK(segp[segment_ndx])))
237 segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
238 else
239 segp[segment_ndx] = NULL;
242 * Since overflow pages can only be access by means of
243 * their bucket, free overflow pages associated with
244 * this bucket.
246 for (xbp = bp; xbp->ovfl;) {
247 next_xbp = xbp->ovfl;
248 xbp->ovfl = 0;
249 xbp = next_xbp;
251 /* Check that ovfl pointer is up date. */
252 if (IS_BUCKET(xbp->flags) ||
253 (oaddr != xbp->addr))
254 break;
256 shortp = (__uint16_t *)xbp->page;
257 if (shortp[0])
258 /* set before __put_page */
259 oaddr = shortp[shortp[0] - 1];
260 if ((xbp->flags & BUF_MOD) && __put_page(hashp,
261 xbp->page, xbp->addr, 0, 0))
262 return (NULL);
263 xbp->addr = 0;
264 xbp->flags = 0;
265 BUF_REMOVE(xbp);
266 LRU_INSERT(xbp);
271 /* Now assign this buffer */
272 bp->addr = addr;
273 #ifdef DEBUG1
274 (void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
275 bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
276 #endif
277 bp->ovfl = NULL;
278 if (prev_bp) {
280 * If prev_bp is set, this is an overflow page, hook it in to
281 * the buffer overflow links.
283 #ifdef DEBUG1
284 (void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
285 prev_bp->addr, (prev_bp->ovfl ? bp->ovfl->addr : 0),
286 (bp ? bp->addr : 0));
287 #endif
288 prev_bp->ovfl = bp;
289 bp->flags = 0;
290 } else
291 bp->flags = BUF_BUCKET;
292 MRU_INSERT(bp);
293 return (bp);
296 extern void
297 __buf_init(
298 HTAB *hashp,
299 int nbytes
302 BUFHEAD *bfp;
303 int npages;
305 bfp = &(hashp->bufhead);
306 npages = (nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
307 npages = MAX(npages, MIN_BUFFERS);
309 hashp->nbufs = npages;
310 bfp->next = bfp;
311 bfp->prev = bfp;
313 * This space is calloc'd so these are already null.
315 * bfp->ovfl = NULL;
316 * bfp->flags = 0;
317 * bfp->page = NULL;
318 * bfp->addr = 0;
322 extern int
323 __buf_free(
324 HTAB *hashp,
325 int do_free,
326 int to_disk
329 BUFHEAD *bp;
331 /* Need to make sure that buffer manager has been initialized */
332 if (!LRU)
333 return (0);
334 for (bp = LRU; bp != &hashp->bufhead;) {
335 /* Check that the buffer is valid */
336 if (bp->addr || IS_BUCKET(bp->flags)) {
337 if (to_disk && (bp->flags & BUF_MOD) &&
338 __put_page(hashp, bp->page,
339 bp->addr, IS_BUCKET(bp->flags), 0))
340 return (-1);
342 /* Check if we are freeing stuff */
343 if (do_free) {
344 if (bp->page)
345 free(bp->page);
346 BUF_REMOVE(bp);
347 free(bp);
348 bp = LRU;
349 } else
350 bp = bp->prev;
352 return (0);
355 extern void
356 __reclaim_buf(
357 HTAB *hashp,
358 BUFHEAD *bp
361 bp->ovfl = 0;
362 bp->addr = 0;
363 bp->flags = 0;
364 BUF_REMOVE(bp);
365 LRU_INSERT(bp);