]> git.saurik.com Git - apple/boot.git/blob - gen/libsaio/cache.c
boot-93.tar.gz
[apple/boot.git] / gen / libsaio / cache.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /* cache */
25
26 #import "cache.h"
27 #import "libsa.h"
28
29 cache_t *cacheInit(
30 int nitems,
31 int item_size
32 )
33 {
34 cache_t *cp;
35 item_size += sizeof(item_t);
36 cp = (cache_t *)malloc(sizeof(cache_t) + nitems * item_size);
37 cp->nitems = nitems;
38 cp->item_size = item_size;
39 return cp;
40 }
41
42 /*
43 * Either find an item in the cache, or find where it should go.
44 * Returns 1 if found, 0 if not found.
45 * This function assumes that if you find an empty slot, you will use it;
46 * therefore, empty slots returned are marked valid.
47 */
48 int cacheFind(
49 cache_t *cp,
50 int key1,
51 int key2,
52 char **ip
53 )
54 {
55 item_t *iip, *min_p;
56 int i,j;
57
58 for(i=j=0, iip = min_p = (item_t *)cp->storage; i < cp->nitems; i++) {
59 if (iip->referenced && (iip->key1 == key1) && (iip->key2 == key2)) {
60 *ip = iip->storage;
61 if (iip->referenced < 65535)
62 iip->referenced++;
63 return 1;
64 }
65 if (iip->referenced < min_p->referenced) {
66 min_p = iip;
67 j = i;
68 }
69 iip = (item_t *)((char *)iip + cp->item_size);
70 }
71 *ip = min_p->storage;
72 min_p->referenced = 1;
73 min_p->key1 = key1;
74 min_p->key2 = key2;
75 return 0;
76 }
77
78 /*
79 * Flush the cache.
80 */
81 void cacheFlush(
82 cache_t *cp
83 )
84 {
85 int i;
86 item_t *ip;
87
88 if (cp == 0)
89 return;
90 for(i=0, ip = (item_t *)cp->storage; i < cp->nitems; i++) {
91 ip->referenced = 0;
92 ip = (item_t *)((char *)ip + cp->item_size);
93 }
94 }