forked from liexusong/php-beast
/
cache.c
271 lines (210 loc) · 7.01 KB
/
cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2007 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Liexusong <280259971@qq.com> |
+----------------------------------------------------------------------+
*/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/mman.h>
#include "beast_mm.h"
#include "beast_lock.h"
#include "php.h"
#include "cache.h"
#include "beast_log.h"
#define BUCKETS_DEFAULT_SIZE 1021
static int beast_cache_initialization = 0;
static cache_item_t **beast_cache_buckets = NULL;
static beast_locker_t *beast_cache_locker;
static inline int beast_cache_hash(cache_key_t *key)
{
return key->device * 3 + key->inode * 7;
}
int beast_cache_init(int size)
{
int index, bucket_size;
char lock_file[512];
if (beast_cache_initialization) {
return 0;
}
if (beast_mm_init(size) == -1) {
return -1;
}
sprintf(lock_file, "%s/beast.clock", beast_lock_path);
beast_cache_locker = beast_locker_create(lock_file);
if (beast_cache_locker == NULL) {
beast_write_log(beast_log_error, "Unable create cache "
"locker for beast");
beast_mm_destroy();
return -1;
}
bucket_size = sizeof(cache_item_t *) * BUCKETS_DEFAULT_SIZE;
beast_cache_buckets = (cache_item_t **)mmap(NULL, bucket_size,
PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
if (!beast_cache_buckets) {
beast_write_log(beast_log_error, "Unable alloc memory for beast");
beast_locker_destroy(beast_cache_locker);
beast_mm_destroy();
return -1;
}
for (index = 0; index < BUCKETS_DEFAULT_SIZE; index++) {
beast_cache_buckets[index] = NULL;
}
beast_cache_initialization = 1;
return 0;
}
cache_item_t *beast_cache_find(cache_key_t *key)
{
int hashval = beast_cache_hash(key);
int index = hashval % BUCKETS_DEFAULT_SIZE;
cache_item_t *item, *temp;
beast_locker_rdlock(beast_cache_locker); /* read lock */
item = beast_cache_buckets[index];
while (item) {
if (item->key.device == key->device &&
item->key.inode == key->inode)
{
break;
}
item = item->next;
}
if (item && item->key.mtime < key->mtime) /* cache exprie */
{
temp = beast_cache_buckets[index];
if (temp == item) { /* the header node */
beast_cache_buckets[index] = item->next;
} else {
while (temp->next != item) /* find prev node */
temp = temp->next;
temp->next = item->next;
}
beast_mm_free(item);
item = NULL;
}
beast_locker_unlock(beast_cache_locker);
return item;
}
cache_item_t *beast_cache_create(cache_key_t *key, int size)
{
cache_item_t *item, *next;
int i, msize, bsize;
msize = sizeof(*item) + size;
bsize = sizeof(cache_item_t *) * BUCKETS_DEFAULT_SIZE;
if ((msize + bsize) > beast_mm_realspace()) {
beast_write_log(beast_log_error, "Cache item size too big");
return NULL;
}
item = beast_mm_malloc(msize);
if (!item)
{
int index;
beast_locker_lock(beast_cache_locker);
for (index = 0; index < BUCKETS_DEFAULT_SIZE; index++) {
beast_cache_buckets[index] = NULL;
}
beast_locker_unlock(beast_cache_locker);
beast_mm_flush(); /* clean all caches */
item = beast_mm_malloc(msize);
if (!item) {
return NULL;
}
}
item->key.device = key->device;
item->key.inode = key->inode;
item->key.fsize = key->fsize;
item->key.mtime = key->mtime;
item->next = NULL;
return item;
}
/*
* Push cache item into cache manager,
* this function return a cache item,
* may be return value not equals push item,
* so we must use return value.
*/
cache_item_t *beast_cache_push(cache_item_t *item)
{
int hashval = beast_cache_hash(&item->key);
int index = hashval % BUCKETS_DEFAULT_SIZE;
cache_item_t **this, *self;
beast_locker_lock(beast_cache_locker); /* lock */
#if 0
this = &beast_cache_buckets[index];
while (*this) {
self = *this;
/* the same files */
if (self->key.device == item->key.device &&
self->key.inode == item->key.inode)
{
if (self->key.mtime >= item->key.mtime) {
beast_mm_free(item);
beast_locker_unlock(beast_cache_locker); /* unlock */
return self;
} else { /* do replace */
item->next = self->next;
beast_mm_free(self);
*this = item;
beast_locker_unlock(beast_cache_locker); /* unlock */
return item;
}
}
this = &self->next;
}
*this = item;
#endif
item->next = beast_cache_buckets[index];
beast_cache_buckets[index] = item;
beast_locker_unlock(beast_cache_locker); /* unlock */
return item;
}
int beast_cache_destroy()
{
int index;
cache_item_t *item, *next;
if (!beast_cache_initialization) {
return 0;
}
beast_locker_lock(beast_cache_locker);
for (index = 0; index < BUCKETS_DEFAULT_SIZE; index++) {
item = beast_cache_buckets[index];
while (item) {
next = item->next;
beast_mm_free(item);
item = next;
}
}
beast_mm_free(beast_cache_buckets);
beast_mm_destroy();
beast_locker_unlock(beast_cache_locker);
beast_locker_destroy(beast_cache_locker);
beast_cache_initialization = 0;
return 0;
}
void beast_cache_info(zval *retval)
{
char key[128];
int i;
cache_item_t *item;
beast_locker_rdlock(beast_cache_locker); /* read lock */
for (i = 0; i < BUCKETS_DEFAULT_SIZE; i++) {
item = beast_cache_buckets[i];
while (item) {
sprintf(key, "{device(%d)#inode(%d)}",
item->key.device, item->key.inode);
add_assoc_long(retval, key, item->key.fsize);
item = item->next;
}
}
beast_locker_unlock(beast_cache_locker);
}