]> git.saurik.com Git - apple/boot.git/blame - i386/libsaio/cache.c
boot-132.tar.gz
[apple/boot.git] / i386 / libsaio / cache.c
CommitLineData
14c7c974 1/*
57c72a9a 2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
14c7c974
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
4f6e3300 6 * The contents of this file constitute Original Code as defined in and
57c72a9a 7 * are subject to the Apple Public Source License Version 2.0 (the
4f6e3300
A
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
14c7c974 11 *
4f6e3300
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14c7c974
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
4f6e3300
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
14c7c974
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
75b89a82
A
22/*
23 * cache.c - A simple cache for file systems meta-data.
24 *
25 * Copyright (c) 2000 Apple Computer, Inc.
26 *
27 * DRI: Josh de Cesare
28 */
14c7c974 29
75b89a82
A
30#include <sl.h>
31// #include <fs.h>
14c7c974 32
75b89a82
A
33struct CacheEntry {
34 CICell ih;
35 long time;
36 long long offset;
37};
38typedef struct CacheEntry CacheEntry;
14c7c974 39
75b89a82
A
40#define kCacheSize (0x100000)
41#define kCacheMinBlockSize (0x200)
42#define kCacheMaxBlockSize (0x4000)
43#define kCacheMaxEntries (kCacheSize / kCacheMinBlockSize)
44
45static CICell gCacheIH;
46static long gCacheBlockSize;
47static long gCacheNumEntries;
48static long gCacheTime;
49
50#ifdef __i386__
51static CacheEntry *gCacheEntries;
52static char *gCacheBuffer;
53#else
54static CacheEntry gCacheEntries[kCacheMaxEntries];
55static char gCacheBuffer[kCacheSize];
56#endif
57
f083c6c3 58#if CACHE_STATS
75b89a82
A
59unsigned long gCacheHits;
60unsigned long gCacheMisses;
61unsigned long gCacheEvicts;
f083c6c3 62#endif
75b89a82
A
63
64void CacheInit( CICell ih, long blockSize )
14c7c974 65{
75b89a82
A
66#ifdef __i386__
67 if ((ih == gCacheIH) && (blockSize == gCacheBlockSize))
68 return;
69#endif
70
71 if ((blockSize < kCacheMinBlockSize) ||
72 (blockSize >= kCacheMaxBlockSize))
73 return;
74
75 gCacheBlockSize = blockSize;
76 gCacheNumEntries = kCacheSize / gCacheBlockSize;
77 gCacheTime = 0;
14c7c974 78
f083c6c3 79#if CACHE_STATS
75b89a82
A
80 gCacheHits = 0;
81 gCacheMisses = 0;
82 gCacheEvicts = 0;
f083c6c3 83#endif
75b89a82
A
84
85 gCacheIH = ih;
86
87#ifdef __i386__
88 if (!gCacheBuffer) gCacheBuffer = (char *) malloc(kCacheSize);
89 if (!gCacheEntries) gCacheEntries = (CacheEntry *) malloc(kCacheMaxEntries * sizeof(CacheEntry));
90 if ( !gCacheBuffer || !gCacheEntries )
91 {
92 gCacheIH = 0; // invalidate cache
93 return;
14c7c974 94 }
75b89a82
A
95#endif
96
97 bzero(gCacheEntries, kCacheMaxEntries * sizeof(CacheEntry));
14c7c974
A
98}
99
75b89a82
A
100long CacheRead( CICell ih, char * buffer, long long offset,
101 long length, long cache )
14c7c974 102{
bba600dd
A
103 long cnt, oldestEntry = 0, oldestTime, loadCache = 0;
104 CacheEntry *entry;
75b89a82
A
105
106 // See if the data can be cached.
107 if (cache && (gCacheIH == ih) && (length == gCacheBlockSize)) {
108 // Look for the data in the cache.
109 for (cnt = 0; cnt < gCacheNumEntries; cnt++) {
110 entry = &gCacheEntries[cnt];
111 if ((entry->ih == ih) && (entry->offset == offset)) {
112 entry->time = ++gCacheTime;
113 break;
114 }
115 }
116
117 // If the data was found copy it to the caller.
118 if (cnt != gCacheNumEntries) {
119 bcopy(gCacheBuffer + cnt * gCacheBlockSize, buffer, gCacheBlockSize);
f083c6c3 120#if CACHE_STATS
75b89a82 121 gCacheHits++;
f083c6c3 122#endif
75b89a82
A
123 return gCacheBlockSize;
124 }
125
126 // Could not find the data in the cache.
127 loadCache = 1;
14c7c974 128 }
75b89a82
A
129
130 // Read the data from the disk.
131 Seek(ih, offset);
132 Read(ih, (long)buffer, length);
f083c6c3 133#if CACHE_STATS
75b89a82 134 if (cache) gCacheMisses++;
f083c6c3 135#endif
75b89a82
A
136
137 // Put the data from the disk in the cache if needed.
138 if (loadCache) {
139 // Find a free entry.
140 oldestTime = gCacheTime;
141 for (cnt = 0; cnt < gCacheNumEntries; cnt++) {
142 entry = &gCacheEntries[cnt];
143
144 // Found a free entry.
145 if (entry->ih == 0) break;
146
147 if (entry->time < oldestTime) {
148 oldestTime = entry->time;
149 oldestEntry = cnt;
150 }
151 }
152
153 // If no free entry was found, use the oldest.
154 if (cnt == gCacheNumEntries) {
155 cnt = oldestEntry;
f083c6c3 156#if CACHE_STATS
75b89a82 157 gCacheEvicts++;
f083c6c3 158#endif
75b89a82
A
159 }
160
161 // Copy the data from disk to the new entry.
162 entry = &gCacheEntries[cnt];
163 entry->ih = ih;
164 entry->time = ++gCacheTime;
165 entry->offset = offset;
166 bcopy(buffer, gCacheBuffer + cnt * gCacheBlockSize, gCacheBlockSize);
167 }
168
169 return length;
14c7c974 170}