1 | /*␊ |
2 | * Copyright (c) 2000-2003 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 2.0 (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 | typedef struct CacheEntry {␊ |
34 | ␉CICell ih;␊ |
35 | ␉long time;␊ |
36 | ␉long long offset;␊ |
37 | } CacheEntry;␊ |
38 | ␊ |
39 | #define kCacheSize (0x100000)␊ |
40 | #define kCacheMinBlockSize (0x200)␊ |
41 | #define kCacheMaxBlockSize (0x8000)␊ |
42 | #define kCacheMaxEntries (kCacheSize / kCacheMinBlockSize)␊ |
43 | ␊ |
44 | static CICell gCacheIH;␊ |
45 | static long gCacheBlockSize;␊ |
46 | static long gCacheNumEntries;␊ |
47 | static long gCacheTime;␊ |
48 | ␊ |
49 | #ifdef __i386__␊ |
50 | ␉static CacheEntry *gCacheEntries;␊ |
51 | ␉static char *gCacheBuffer;␊ |
52 | #else␊ |
53 | ␉static CacheEntry gCacheEntries[kCacheMaxEntries];␊ |
54 | ␉static char gCacheBuffer[kCacheSize];␊ |
55 | #endif␊ |
56 | ␊ |
57 | #if CACHE_STATS␊ |
58 | ␉unsigned long gCacheHits;␊ |
59 | ␉unsigned long gCacheMisses;␊ |
60 | ␉unsigned long gCacheEvicts;␊ |
61 | #endif␊ |
62 | ␊ |
63 | void CacheReset()␊ |
64 | {␊ |
65 | ␉gCacheIH = NULL;␊ |
66 | }␊ |
67 | ␊ |
68 | void CacheInit( CICell ih, long blockSize )␊ |
69 | {␊ |
70 | #ifdef __i386__␊ |
71 | ␉if ((ih == gCacheIH) && (blockSize == gCacheBlockSize))␊ |
72 | ␉{␊ |
73 | ␉␉return;␊ |
74 | ␉}␊ |
75 | #endif␊ |
76 | ␊ |
77 | ␉if ((blockSize < kCacheMinBlockSize) || (blockSize > kCacheMaxBlockSize))␊ |
78 | ␉{␊ |
79 | ␉␉return;␊ |
80 | ␉}␊ |
81 | ␊ |
82 | ␉gCacheBlockSize = blockSize;␊ |
83 | ␉gCacheNumEntries = kCacheSize / gCacheBlockSize;␊ |
84 | ␉gCacheTime = 0;␊ |
85 | ␊ |
86 | #if CACHE_STATS␊ |
87 | ␉gCacheHits␉= 0;␊ |
88 | ␉gCacheMisses␉= 0;␊ |
89 | ␉gCacheEvicts␉= 0;␊ |
90 | #endif␊ |
91 | ␊ |
92 | gCacheIH = ih;␊ |
93 | ␊ |
94 | #ifdef __i386__␊ |
95 | ␉if (!gCacheBuffer)␊ |
96 | ␉{␊ |
97 | ␉␉gCacheBuffer = (char *) malloc(kCacheSize);␊ |
98 | ␉}␊ |
99 | ␊ |
100 | ␉if (!gCacheEntries)␊ |
101 | ␉{␊ |
102 | ␉␉gCacheEntries = (CacheEntry *) malloc(kCacheMaxEntries * sizeof(CacheEntry));␊ |
103 | ␉}␊ |
104 | ␊ |
105 | ␉if (!gCacheBuffer || !gCacheEntries)␊ |
106 | ␉{␊ |
107 | ␉␉gCacheIH = 0; // invalidate cache␊ |
108 | ␉␉return;␊ |
109 | ␉}␊ |
110 | #endif␊ |
111 | ␊ |
112 | ␉bzero(gCacheEntries, kCacheMaxEntries * sizeof(CacheEntry));␊ |
113 | }␊ |
114 | ␊ |
115 | long CacheRead(CICell ih, char * buffer, long long offset, long length, long cache)␊ |
116 | {␊ |
117 | ␉long cnt, oldestEntry = 0, oldestTime, loadCache = 0;␊ |
118 | ␉CacheEntry *entry;␊ |
119 | ␊ |
120 | ␉// See if the data can be cached.␊ |
121 | ␉if (cache && (gCacheIH == ih) && (length == gCacheBlockSize))␊ |
122 | ␉{␊ |
123 | ␉␉// Look for the data in the cache.␊ |
124 | ␉␉for (cnt = 0; cnt < gCacheNumEntries; cnt++)␊ |
125 | ␉␉{␊ |
126 | ␉␉␉entry = &gCacheEntries[cnt];␊ |
127 | ␊ |
128 | ␉␉␉if ((entry->ih == ih) && (entry->offset == offset))␊ |
129 | ␉␉␉{␊ |
130 | ␉␉␉␉entry->time = ++gCacheTime;␊ |
131 | ␉␉␉␉break;␊ |
132 | ␉␉␉}␊ |
133 | ␉␉}␊ |
134 | ␊ |
135 | ␉␉// If the data was found copy it to the caller.␊ |
136 | ␉␉if (cnt != gCacheNumEntries)␊ |
137 | ␉␉{␊ |
138 | ␉␉␉bcopy(gCacheBuffer + cnt * gCacheBlockSize, buffer, gCacheBlockSize);␊ |
139 | #if CACHE_STATS␊ |
140 | ␉␉␉gCacheHits++;␊ |
141 | #endif␊ |
142 | ␉␉␉return gCacheBlockSize;␊ |
143 | ␉␉}␊ |
144 | ␊ |
145 | ␉␉// Could not find the data in the cache.␊ |
146 | ␉␉loadCache = 1;␊ |
147 | ␉}␊ |
148 | ␊ |
149 | ␉// Read the data from the disk.␊ |
150 | ␉Seek(ih, offset);␊ |
151 | ␉Read(ih, (long)buffer, length);␊ |
152 | ␊ |
153 | #if CACHE_STATS␊ |
154 | ␉if (cache)␊ |
155 | ␉{␊ |
156 | ␉␉gCacheMisses++;␊ |
157 | ␉}␊ |
158 | #endif␊ |
159 | ␊ |
160 | ␉// Put the data from the disk in the cache if needed.␊ |
161 | ␉if (loadCache)␊ |
162 | ␉{␊ |
163 | ␉␉// Find a free entry.␊ |
164 | ␉␉oldestTime = gCacheTime;␊ |
165 | ␊ |
166 | ␉␉for (cnt = 0; cnt < gCacheNumEntries; cnt++)␊ |
167 | ␉␉{␊ |
168 | ␉␉␉entry = &gCacheEntries[cnt];␊ |
169 | ␊ |
170 | ␉␉␉// Found a free entry.␊ |
171 | ␉␉␉if (entry->ih == 0)␊ |
172 | ␉␉␉{␊ |
173 | ␉␉␉␉break;␊ |
174 | ␉␉␉}␊ |
175 | ␊ |
176 | ␉␉␉if (entry->time < oldestTime)␊ |
177 | ␉␉␉{␊ |
178 | ␉␉␉␉oldestTime = entry->time;␊ |
179 | ␉␉␉␉oldestEntry = cnt;␊ |
180 | ␉␉␉}␊ |
181 | ␉␉}␊ |
182 | ␊ |
183 | ␉␉// If no free entry was found, use the oldest.␊ |
184 | ␉␉if (cnt == gCacheNumEntries)␊ |
185 | ␉␉{␊ |
186 | ␉␉␉cnt = oldestEntry;␊ |
187 | #if CACHE_STATS␊ |
188 | ␉␉␉gCacheEvicts++;␊ |
189 | #endif␊ |
190 | ␉␉}␊ |
191 | ␊ |
192 | ␉␉// Copy the data from disk to the new entry.␊ |
193 | ␉␉entry = &gCacheEntries[cnt];␊ |
194 | ␉␉entry->ih = ih;␊ |
195 | ␉␉entry->time = ++gCacheTime;␊ |
196 | ␉␉entry->offset = offset;␊ |
197 | ␉␉bcopy(buffer, gCacheBuffer + cnt * gCacheBlockSize, gCacheBlockSize);␊ |
198 | ␉}␊ |
199 | ␊ |
200 | ␉return length;␊ |
201 | }␊ |
202 | |