]>
Commit | Line | Data |
---|---|---|
14c7c974 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
f083c6c3 A |
6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
7 | * | |
8 | * This file contains Original Code and/or Modifications of Original Code | |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14c7c974 A |
14 | * |
15 | * The Original Code and all software distributed under the License are | |
f083c6c3 | 16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
14c7c974 A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
f083c6c3 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
14c7c974 A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* cache */ | |
26 | ||
27 | #import "cache.h" | |
28 | #import "libsa.h" | |
29 | ||
30 | cache_t *cacheInit( | |
31 | int nitems, | |
32 | int item_size | |
33 | ) | |
34 | { | |
35 | cache_t *cp; | |
36 | item_size += sizeof(item_t); | |
37 | cp = (cache_t *)malloc(sizeof(cache_t) + nitems * item_size); | |
38 | cp->nitems = nitems; | |
39 | cp->item_size = item_size; | |
40 | return cp; | |
41 | } | |
42 | ||
43 | /* | |
44 | * Either find an item in the cache, or find where it should go. | |
45 | * Returns 1 if found, 0 if not found. | |
46 | * This function assumes that if you find an empty slot, you will use it; | |
47 | * therefore, empty slots returned are marked valid. | |
48 | */ | |
49 | int cacheFind( | |
50 | cache_t *cp, | |
51 | int key1, | |
52 | int key2, | |
53 | char **ip | |
54 | ) | |
55 | { | |
56 | item_t *iip, *min_p; | |
57 | int i,j; | |
58 | ||
59 | for(i=j=0, iip = min_p = (item_t *)cp->storage; i < cp->nitems; i++) { | |
60 | if (iip->referenced && (iip->key1 == key1) && (iip->key2 == key2)) { | |
61 | *ip = iip->storage; | |
62 | if (iip->referenced < 65535) | |
63 | iip->referenced++; | |
64 | return 1; | |
65 | } | |
66 | if (iip->referenced < min_p->referenced) { | |
67 | min_p = iip; | |
68 | j = i; | |
69 | } | |
70 | iip = (item_t *)((char *)iip + cp->item_size); | |
71 | } | |
72 | *ip = min_p->storage; | |
73 | min_p->referenced = 1; | |
74 | min_p->key1 = key1; | |
75 | min_p->key2 = key2; | |
76 | return 0; | |
77 | } | |
78 | ||
79 | /* | |
80 | * Flush the cache. | |
81 | */ | |
82 | void cacheFlush( | |
83 | cache_t *cp | |
84 | ) | |
85 | { | |
86 | int i; | |
87 | item_t *ip; | |
88 | ||
89 | if (cp == 0) | |
90 | return; | |
91 | for(i=0, ip = (item_t *)cp->storage; i < cp->nitems; i++) { | |
92 | ip->referenced = 0; | |
93 | ip = (item_t *)((char *)ip + cp->item_size); | |
94 | } | |
95 | } |