3
3
#include "../win32.h"
4
4
#include "fscache.h"
5
5
#include "config.h"
6
+ #include "../../mem-pool.h"
6
7
7
8
static volatile long initialized ;
8
9
static DWORD dwTlsIndex ;
@@ -17,6 +18,7 @@ static CRITICAL_SECTION mutex;
17
18
struct fscache {
18
19
volatile long enabled ;
19
20
struct hashmap map ;
21
+ struct mem_pool * mem_pool ;
20
22
unsigned int lstat_requests ;
21
23
unsigned int opendir_requests ;
22
24
unsigned int fscache_requests ;
@@ -106,11 +108,11 @@ static void fsentry_init(struct fsentry *fse, struct fsentry *list,
106
108
/*
107
109
* Allocate an fsentry structure on the heap.
108
110
*/
109
- static struct fsentry * fsentry_alloc (struct fsentry * list , const char * name ,
111
+ static struct fsentry * fsentry_alloc (struct fscache * cache , struct fsentry * list , const char * name ,
110
112
size_t len )
111
113
{
112
114
/* overallocate fsentry and copy the name to the end */
113
- struct fsentry * fse = xmalloc ( sizeof (struct fsentry ) + len + 1 );
115
+ struct fsentry * fse = mem_pool_alloc ( cache -> mem_pool , sizeof (struct fsentry ) + len + 1 );
114
116
char * nm = ((char * ) fse ) + sizeof (struct fsentry );
115
117
memcpy (nm , name , len );
116
118
nm [len ] = 0 ;
@@ -133,35 +135,28 @@ inline static void fsentry_addref(struct fsentry *fse)
133
135
}
134
136
135
137
/*
136
- * Release the reference to an fsentry, frees the memory if its the last ref .
138
+ * Release the reference to an fsentry.
137
139
*/
138
140
static void fsentry_release (struct fsentry * fse )
139
141
{
140
142
if (fse -> list )
141
143
fse = fse -> list ;
142
144
143
- if (InterlockedDecrement (& (fse -> refcnt )))
144
- return ;
145
-
146
- while (fse ) {
147
- struct fsentry * next = fse -> next ;
148
- free (fse );
149
- fse = next ;
150
- }
145
+ InterlockedDecrement (& (fse -> refcnt ));
151
146
}
152
147
153
148
/*
154
149
* Allocate and initialize an fsentry from a WIN32_FIND_DATA structure.
155
150
*/
156
- static struct fsentry * fseentry_create_entry (struct fsentry * list ,
151
+ static struct fsentry * fseentry_create_entry (struct fscache * cache , struct fsentry * list ,
157
152
const WIN32_FIND_DATAW * fdata )
158
153
{
159
154
char buf [MAX_PATH * 3 ];
160
155
int len ;
161
156
struct fsentry * fse ;
162
157
len = xwcstoutf (buf , fdata -> cFileName , ARRAY_SIZE (buf ));
163
158
164
- fse = fsentry_alloc (list , buf , len );
159
+ fse = fsentry_alloc (cache , list , buf , len );
165
160
166
161
fse -> st_mode = file_attr_to_st_mode (fdata -> dwFileAttributes );
167
162
fse -> st_size = (((off64_t ) (fdata -> nFileSizeHigh )) << 32 )
@@ -178,7 +173,7 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list,
178
173
* Dir should not contain trailing '/'. Use an empty string for the current
179
174
* directory (not "."!).
180
175
*/
181
- static struct fsentry * fsentry_create_list (const struct fsentry * dir ,
176
+ static struct fsentry * fsentry_create_list (struct fscache * cache , const struct fsentry * dir ,
182
177
int * dir_not_found )
183
178
{
184
179
wchar_t pattern [MAX_LONG_PATH + 2 ]; /* + 2 for "\*" */
@@ -217,13 +212,13 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir,
217
212
}
218
213
219
214
/* allocate object to hold directory listing */
220
- list = fsentry_alloc (NULL , dir -> name , dir -> len );
215
+ list = fsentry_alloc (cache , NULL , dir -> name , dir -> len );
221
216
list -> st_mode = S_IFDIR ;
222
217
223
218
/* walk directory and build linked list of fsentry structures */
224
219
phead = & list -> next ;
225
220
do {
226
- * phead = fseentry_create_entry (list , & fdata );
221
+ * phead = fseentry_create_entry (cache , list , & fdata );
227
222
phead = & (* phead )-> next ;
228
223
} while (FindNextFileW (h , & fdata ));
229
224
@@ -235,7 +230,7 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir,
235
230
if (err == ERROR_NO_MORE_FILES )
236
231
return list ;
237
232
238
- /* otherwise free the list and return error */
233
+ /* otherwise release the list and return error */
239
234
fsentry_release (list );
240
235
errno = err_win_to_posix (err );
241
236
return NULL ;
@@ -258,7 +253,10 @@ static void fscache_add(struct fscache *cache, struct fsentry *fse)
258
253
*/
259
254
static void fscache_clear (struct fscache * cache )
260
255
{
261
- hashmap_free (& cache -> map , 1 );
256
+ mem_pool_discard (cache -> mem_pool , 0 );
257
+ cache -> mem_pool = NULL ;
258
+ mem_pool_init (& cache -> mem_pool , 0 );
259
+ hashmap_free (& cache -> map , 0 );
262
260
hashmap_init (& cache -> map , (hashmap_cmp_fn )fsentry_cmp , NULL , 0 );
263
261
cache -> lstat_requests = cache -> opendir_requests = 0 ;
264
262
cache -> fscache_misses = cache -> fscache_requests = 0 ;
@@ -311,7 +309,7 @@ static struct fsentry *fscache_get(struct fscache *cache, struct fsentry *key)
311
309
}
312
310
313
311
/* create the directory listing */
314
- fse = fsentry_create_list (key -> list ? key -> list : key , & dir_not_found );
312
+ fse = fsentry_create_list (cache , key -> list ? key -> list : key , & dir_not_found );
315
313
316
314
/* leave on error (errno set by fsentry_create_list) */
317
315
if (!fse ) {
@@ -321,7 +319,7 @@ static struct fsentry *fscache_get(struct fscache *cache, struct fsentry *key)
321
319
* empty, which for all practical matters is the same
322
320
* thing as far as fscache is concerned).
323
321
*/
324
- fse = fsentry_alloc (key -> list -> list ,
322
+ fse = fsentry_alloc (cache , key -> list -> list ,
325
323
key -> list -> name , key -> list -> len );
326
324
fse -> st_mode = 0 ;
327
325
hashmap_add (& cache -> map , fse );
@@ -397,6 +395,7 @@ int fscache_enable(size_t initial_size)
397
395
* '4' was determined empirically by testing several repos
398
396
*/
399
397
hashmap_init (& cache -> map , (hashmap_cmp_fn )fsentry_cmp , NULL , initial_size * 4 );
398
+ mem_pool_init (& cache -> mem_pool , 0 );
400
399
if (!TlsSetValue (dwTlsIndex , cache ))
401
400
BUG ("TlsSetValue error" );
402
401
}
@@ -428,7 +427,8 @@ void fscache_disable(void)
428
427
"total requests/misses %u/%u\n" ,
429
428
cache -> lstat_requests , cache -> opendir_requests ,
430
429
cache -> fscache_requests , cache -> fscache_misses );
431
- fscache_clear (cache );
430
+ mem_pool_discard (cache -> mem_pool , 0 );
431
+ hashmap_free (& cache -> map , 0 );
432
432
free (cache );
433
433
}
434
434
@@ -610,6 +610,8 @@ void fscache_merge(struct fscache *dest)
610
610
while ((e = hashmap_iter_next (& iter )))
611
611
hashmap_add (& dest -> map , e );
612
612
613
+ mem_pool_combine (dest -> mem_pool , cache -> mem_pool );
614
+
613
615
dest -> lstat_requests += cache -> lstat_requests ;
614
616
dest -> opendir_requests += cache -> opendir_requests ;
615
617
dest -> fscache_requests += cache -> fscache_requests ;
0 commit comments