]> git.saurik.com Git - apple/bootx.git/blame - bootx.tproj/fs.subproj/cache.c
BootX-81.tar.gz
[apple/bootx.git] / bootx.tproj / fs.subproj / cache.c
CommitLineData
04fee52e
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
8be739c0
A
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.
04fee52e 11 *
8be739c0
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
04fee52e
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
8be739c0
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.
04fee52e
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * cache.c - A simple cache for file systems meta-data.
24 *
71019aa0 25 * Copyright (c) 2000 - 2003 Apple Computer, Inc.
04fee52e
A
26 *
27 * DRI: Josh de Cesare
28 */
29
30#include <sl.h>
31#include <fs.h>
32
33struct CacheEntry {
34 CICell ih;
35 long time;
36 long long offset;
37};
38typedef struct CacheEntry CacheEntry;
39
71019aa0 40#define kCacheSize (kFSCacheSize)
04fee52e
A
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;
49static CacheEntry gCacheEntries[kCacheMaxEntries];
71019aa0 50static char *gCacheBuffer = (char *)kFSCacheAddr;
04fee52e
A
51
52unsigned long gCacheHits;
53unsigned long gCacheMisses;
54unsigned long gCacheEvicts;
55
56void CacheInit(CICell ih, long blockSize)
57{
58 if ((blockSize < kCacheMinBlockSize) ||
59 (blockSize >= kCacheMaxBlockSize))
60 return;
61
62 gCacheBlockSize = blockSize;
63 gCacheNumEntries = kCacheSize / gCacheBlockSize;
64 gCacheTime = 0;
65
66 gCacheHits = 0;
67 gCacheMisses = 0;
68 gCacheEvicts = 0;
69
70 bzero(gCacheEntries, sizeof(gCacheEntries));
71
72 gCacheIH = ih;
73}
74
75
76long CacheRead(CICell ih, char *buffer, long long offset,
77 long length, long cache)
78{
8be739c0 79 long cnt, oldestEntry = 0, oldestTime, loadCache = 0;
04fee52e
A
80 CacheEntry *entry;
81
82 // See if the data can be cached.
83 if (cache && (gCacheIH == ih) && (length == gCacheBlockSize)) {
84 // Look for the data in the cache.
85 for (cnt = 0; cnt < gCacheNumEntries; cnt++) {
86 entry = &gCacheEntries[cnt];
87 if ((entry->ih == ih) && (entry->offset == offset)) {
88 entry->time = ++gCacheTime;
89 break;
90 }
91 }
92
93 // If the data was found copy it to the caller.
94 if (cnt != gCacheNumEntries) {
95 bcopy(gCacheBuffer + cnt * gCacheBlockSize, buffer, gCacheBlockSize);
96 gCacheHits++;
97 return gCacheBlockSize;
98 }
99
100 // Could not find the data in the cache.
101 loadCache = 1;
102 }
103
104 // Read the data from the disk.
105 Seek(ih, offset);
106 Read(ih, (CICell)buffer, length);
107 if (cache) gCacheMisses++;
108
109 // Put the data from the disk in the cache if needed.
110 if (loadCache) {
111 // Find a free entry.
112 oldestTime = gCacheTime;
113 for (cnt = 0; cnt < gCacheNumEntries; cnt++) {
114 entry = &gCacheEntries[cnt];
115
116 // Found a free entry.
117 if (entry->ih == 0) break;
118
119 if (entry->time < oldestTime) {
120 oldestTime = entry->time;
121 oldestEntry = cnt;
122 }
123 }
124
125 // If no free entry was found, use the oldest.
126 if (cnt == gCacheNumEntries) {
127 cnt = oldestEntry;
128 gCacheEvicts++;
129 }
130
131 // Copy the data from disk to the new entry.
132 entry = &gCacheEntries[cnt];
133 entry->ih = ih;
134 entry->time = ++gCacheTime;
135 entry->offset = offset;
136 bcopy(buffer, gCacheBuffer + cnt * gCacheBlockSize, gCacheBlockSize);
137 }
138
139 return length;
140}