Skip to content

Commit 470b789

Browse files
committed
libutil/fname,refactor: remove function static variables
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent 062a69f commit 470b789

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

main/fname.c

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,26 @@ struct comp {
2222
};
2323

2424

25-
extern hashTable *canonFnameCacheTableNew (void)
25+
struct canonFnameCacheTable {
26+
hashTable *table;
27+
char *input_last;
28+
char *return_last;
29+
};
30+
31+
extern struct canonFnameCacheTable *canonFnameCacheTableNew (void)
2632
{
27-
return hashTableNew (7, hashCstrhash, hashCstreq,
28-
eFree, eFree);
33+
struct canonFnameCacheTable *r = xMalloc (1, struct canonFnameCacheTable);
34+
r->table = hashTableNew (7, hashCstrhash, hashCstreq,
35+
eFree, eFree);
36+
r->input_last = NULL;
37+
r->return_last = NULL;
38+
return r;
39+
}
40+
41+
extern void canonFnameCacheTableDelete (struct canonFnameCacheTable *cache_table)
42+
{
43+
hashTableDelete (cache_table->table);
44+
eFree (cache_table);
2945
}
3046

3147
static void strcpy_comps (char *buf, struct comp *comp)
@@ -205,26 +221,23 @@ static char *canonicalizePathNew(const char *dir, size_t dir_len, const char *re
205221
}
206222

207223
extern const char *canonicalizeRelativeFileName (const char *cwd, size_t cwd_len, const char *input,
208-
hashTable* cache_table)
224+
struct canonFnameCacheTable *cache_table)
209225
{
210-
static char *input_last;
211-
static char *return_last;
212-
213-
if (input_last)
226+
if (cache_table->input_last)
214227
{
215-
if (strcmp (input, input_last) == 0)
216-
return return_last;
217-
input_last = NULL;
228+
if (strcmp (input, cache_table->input_last) == 0)
229+
return cache_table->return_last;
230+
cache_table->input_last = NULL;
218231
}
219232

220-
char *r = hashTableGetItem (cache_table, input);
233+
char *r = hashTableGetItem (cache_table->table, input);
221234
if (r)
222235
return r;
223236

224237
r = canonicalizePathNew (cwd, cwd_len, input);
225238

226-
input_last = eStrdup (input);
227-
return_last = r;
228-
hashTablePutItem (cache_table, input_last, return_last);
229-
return return_last;
239+
cache_table->input_last = eStrdup (input);
240+
cache_table->return_last = r;
241+
hashTablePutItem (cache_table->table, cache_table->input_last, cache_table->return_last);
242+
return cache_table->return_last;
230243
}

main/fname.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
#include "htable.h"
1313

14-
/* hashTableDelete() is for freeing the cache returned from this function. */
15-
extern hashTable *canonFnameCacheTableNew (void);
14+
struct canonFnameCacheTable;
15+
extern struct canonFnameCacheTable *canonFnameCacheTableNew (void);
16+
extern void canonFnameCacheTableDelete (struct canonFnameCacheTable *cache_table);
1617

1718
/* Don't free the cstring returned from this function directly.
1819
* This function stores the cstring to the cache.
1920
*/
2021
extern const char *canonicalizeRelativeFileName(const char *cwd, size_t cwd_len, const char *input,
21-
hashTable* cache_table);
22+
struct canonFnameCacheTable *cache_table);
2223

2324
/*
2425
* Resolve '.', '..', and '//' in FNAME as if the current working

0 commit comments

Comments
 (0)