forked from liexusong/php-beast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.c
261 lines (201 loc) · 6.48 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
/*
+----------------------------------------------------------------------+
| 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 "spinlock.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_atomic_t *cache_lock;
static int cache_pid = -1;
void beast_cache_lock()
{
if (cache_pid == -1) {
cache_pid = (int)getpid();
}
beast_spinlock(cache_lock, cache_pid);
}
void beast_cache_unlock()
{
if (cache_pid == -1) {
cache_pid = (int)getpid();
}
beast_spinunlock(cache_lock, cache_pid);
}
static inline unsigned int
beast_cache_hash(cache_key_t *key)
{
unsigned int retval;
retval = (unsigned int)key->device * 3
+ (unsigned int)key->inode * 7;
return retval;
}
int beast_cache_init(int size)
{
int index, bucket_size;
if (beast_cache_initialization) {
return 0;
}
if (beast_mm_init(size) == -1) {
return -1;
}
/* init cache lock */
cache_lock = (int *)mmap(NULL,
sizeof(int),
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANON,
-1,
0);
if (!cache_lock) {
beast_write_log(beast_log_error,
"Unable alloc share memory for cache lock");
beast_mm_destroy();
return -1;
}
*cache_lock = 0;
/* init cache buckets's memory */
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 share memory for cache buckets");
munmap((void *)cache_lock, sizeof(int));
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)
{
unsigned int hashval = beast_cache_hash(key);
unsigned int index = hashval % BUCKETS_DEFAULT_SIZE;
cache_item_t *item, *temp;
beast_cache_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_cache_unlock();
return item;
}
cache_item_t *beast_cache_create(cache_key_t *key)
{
cache_item_t *item, *next;
int i, msize, bsize;
msize = sizeof(*item) + key->fsize;
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) {
beast_write_log(beast_log_notice,
"Not enough memory for alloc cache");
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)
{
unsigned int hashval = beast_cache_hash(&item->key);
unsigned int index = hashval % BUCKETS_DEFAULT_SIZE;
cache_item_t **this, *self;
beast_cache_lock();
item->next = beast_cache_buckets[index];
beast_cache_buckets[index] = item;
beast_cache_unlock();
return item;
}
int beast_cache_destroy()
{
int index;
cache_item_t *item, *next;
if (!beast_cache_initialization) {
return 0;
}
beast_mm_destroy(); /* destroy memory manager */
/* free cache buckets's mmap memory */
munmap((void *)beast_cache_buckets,
sizeof(cache_item_t *) * BUCKETS_DEFAULT_SIZE);
munmap((void *)cache_lock, sizeof(int));
beast_cache_initialization = 0;
return 0;
}
void beast_cache_info(zval *retval)
{
char key[128];
int i;
cache_item_t *item;
beast_cache_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_cache_unlock();
}