]> git.saurik.com Git - apple/boot.git/blob - i386/libsaio/cache.c
boot-93.tar.gz
[apple/boot.git] / i386 / libsaio / cache.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
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.
11 *
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
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 */
29
30 #include <sl.h>
31 // #include <fs.h>
32
33 struct CacheEntry {
34 CICell ih;
35 long time;
36 long long offset;
37 };
38 typedef struct CacheEntry CacheEntry;
39
40 #define kCacheSize (0x100000)
41 #define kCacheMinBlockSize (0x200)
42 #define kCacheMaxBlockSize (0x4000)
43 #define kCacheMaxEntries (kCacheSize / kCacheMinBlockSize)
44
45 static CICell gCacheIH;
46 static long gCacheBlockSize;
47 static long gCacheNumEntries;
48 static long gCacheTime;
49
50 #ifdef __i386__
51 static CacheEntry *gCacheEntries;
52 static char *gCacheBuffer;
53 #else
54 static CacheEntry gCacheEntries[kCacheMaxEntries];
55 static char gCacheBuffer[kCacheSize];
56 #endif
57
58 unsigned long gCacheHits;
59 unsigned long gCacheMisses;
60 unsigned long gCacheEvicts;
61
62 void CacheInit( CICell ih, long blockSize )
63 {
64 #ifdef __i386__
65 if ((ih == gCacheIH) && (blockSize == gCacheBlockSize))
66 return;
67 #endif
68
69 if ((blockSize < kCacheMinBlockSize) ||
70 (blockSize >= kCacheMaxBlockSize))
71 return;
72
73 gCacheBlockSize = blockSize;
74 gCacheNumEntries = kCacheSize / gCacheBlockSize;
75 gCacheTime = 0;
76
77 gCacheHits = 0;
78 gCacheMisses = 0;
79 gCacheEvicts = 0;
80
81 gCacheIH = ih;
82
83 #ifdef __i386__
84 if (!gCacheBuffer) gCacheBuffer = (char *) malloc(kCacheSize);
85 if (!gCacheEntries) gCacheEntries = (CacheEntry *) malloc(kCacheMaxEntries * sizeof(CacheEntry));
86 if ( !gCacheBuffer || !gCacheEntries )
87 {
88 gCacheIH = 0; // invalidate cache
89 return;
90 }
91 #endif
92
93 bzero(gCacheEntries, kCacheMaxEntries * sizeof(CacheEntry));
94 }
95
96 long CacheRead( CICell ih, char * buffer, long long offset,
97 long length, long cache )
98 {
99 long cnt, oldestEntry = 0, oldestTime, loadCache = 0;
100 CacheEntry *entry;
101
102 // See if the data can be cached.
103 if (cache && (gCacheIH == ih) && (length == gCacheBlockSize)) {
104 // Look for the data in the cache.
105 for (cnt = 0; cnt < gCacheNumEntries; cnt++) {
106 entry = &gCacheEntries[cnt];
107 if ((entry->ih == ih) && (entry->offset == offset)) {
108 entry->time = ++gCacheTime;
109 break;
110 }
111 }
112
113 // If the data was found copy it to the caller.
114 if (cnt != gCacheNumEntries) {
115 bcopy(gCacheBuffer + cnt * gCacheBlockSize, buffer, gCacheBlockSize);
116 gCacheHits++;
117 return gCacheBlockSize;
118 }
119
120 // Could not find the data in the cache.
121 loadCache = 1;
122 }
123
124 // Read the data from the disk.
125 Seek(ih, offset);
126 Read(ih, (long)buffer, length);
127 if (cache) gCacheMisses++;
128
129 // Put the data from the disk in the cache if needed.
130 if (loadCache) {
131 // Find a free entry.
132 oldestTime = gCacheTime;
133 for (cnt = 0; cnt < gCacheNumEntries; cnt++) {
134 entry = &gCacheEntries[cnt];
135
136 // Found a free entry.
137 if (entry->ih == 0) break;
138
139 if (entry->time < oldestTime) {
140 oldestTime = entry->time;
141 oldestEntry = cnt;
142 }
143 }
144
145 // If no free entry was found, use the oldest.
146 if (cnt == gCacheNumEntries) {
147 cnt = oldestEntry;
148 gCacheEvicts++;
149 }
150
151 // Copy the data from disk to the new entry.
152 entry = &gCacheEntries[cnt];
153 entry->ih = ih;
154 entry->time = ++gCacheTime;
155 entry->offset = offset;
156 bcopy(buffer, gCacheBuffer + cnt * gCacheBlockSize, gCacheBlockSize);
157 }
158
159 return length;
160 }