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