1 | /* $Id: strcache2.h 1887 2008-10-19 23:08:10Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * strcache - New string cache.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2006-2008 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * This file is part of kBuild.
|
---|
10 | *
|
---|
11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kBuild is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with kBuild; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef ___strcache2_h
|
---|
28 | #define ___strcache2_h
|
---|
29 |
|
---|
30 | /* string cache memory segment. */
|
---|
31 | struct strcache2_seg
|
---|
32 | {
|
---|
33 | struct strcache2_seg *next; /* The next cache segment. */
|
---|
34 | char *start; /* The first byte in the segment. */
|
---|
35 | size_t size; /* The size of the segment. */
|
---|
36 | size_t avail; /* The number of available bytes. */
|
---|
37 | char *cursor; /* Allocation cursor. */
|
---|
38 | };
|
---|
39 |
|
---|
40 | /* string cache hash table entry. */
|
---|
41 | struct strcache2_entry
|
---|
42 | {
|
---|
43 | void *user;
|
---|
44 | unsigned int hash1;
|
---|
45 | unsigned int hash2;
|
---|
46 | unsigned int length;
|
---|
47 | };
|
---|
48 |
|
---|
49 | struct strcache2
|
---|
50 | {
|
---|
51 | struct strcache2_entry **hash_tab; /* The hash table. */
|
---|
52 | int case_insensitive; /* case insensitive or not. */
|
---|
53 | unsigned int hash_mask; /* The AND mask matching hash_size.*/
|
---|
54 | unsigned long lookup_count; /* The number of lookups. */
|
---|
55 | unsigned long collision_1st_count; /* The number of 1st level collisions. */
|
---|
56 | unsigned long collision_2nd_count; /* The number of 2nd level collisions. */
|
---|
57 | unsigned long collision_3rd_count; /* The number of 3rd level collisions. */
|
---|
58 | unsigned int count; /* Number entries in the cache. */
|
---|
59 | unsigned int rehash_count; /* When to rehash the table. */
|
---|
60 | unsigned int init_size; /* The initial hash table size. */
|
---|
61 | unsigned int hash_size; /* The hash table size. */
|
---|
62 | unsigned int def_seg_size; /* The default segment size. */
|
---|
63 | void *lock; /* The lock handle. */
|
---|
64 | struct strcache2_seg *seg_head; /* The memory segment list. */
|
---|
65 | struct strcache2 *next; /* The next string cache. */
|
---|
66 | const char *name; /* Cache name. */
|
---|
67 | };
|
---|
68 |
|
---|
69 |
|
---|
70 | void strcache2_init (struct strcache2 *cache, const char *name, unsigned int size,
|
---|
71 | unsigned int def_seg_size, int case_insensitive, int thread_safe);
|
---|
72 | void strcache2_term (struct strcache2 *cache);
|
---|
73 | void strcache2_print_stats (struct strcache2 *cache, const char *prefix);
|
---|
74 | void strcache2_print_stats_all (const char *prefix);
|
---|
75 | const char *strcache2_add (struct strcache2 *cache, const char *str, unsigned int length);
|
---|
76 | const char *strcache2_iadd (struct strcache2 *cache, const char *str, unsigned int length);
|
---|
77 | #ifdef HAVE_CASE_INSENSITIVE_FS
|
---|
78 | # define strcache2_add_file(cache, str, length) strcache2_iadd((cache), (str), (length))
|
---|
79 | #else
|
---|
80 | # define strcache2_add_file(cache, str, length) strcache2_add((cache), (str), (length))
|
---|
81 | #endif
|
---|
82 | const char *strcache2_add_hashed (struct strcache2 *cache, const char *str, unsigned int length,
|
---|
83 | unsigned int hash1, unsigned int hash2);
|
---|
84 | const char *strcache2_lookup (struct strcache2 *cache, const char *str, unsigned int length);
|
---|
85 | int strcache2_is_cached (struct strcache2 *cache, const char *str);
|
---|
86 | int strcache2_verify_entry (struct strcache2 *cache, const char *str);
|
---|
87 | unsigned int strcache2_get_hash2_fallback (struct strcache2 *cache, const char *str);
|
---|
88 | unsigned int strcache2_hash_str (const char *str, unsigned int length, unsigned int *hash2p);
|
---|
89 | unsigned int strcache2_hash_istr (const char *str, unsigned int length, unsigned int *hash2p);
|
---|
90 |
|
---|
91 | /* Get the hash table entry pointer. */
|
---|
92 | MY_INLINE struct strcache2_entry const *
|
---|
93 | strcache2_get_entry (struct strcache2 *cache, const char *str)
|
---|
94 | {
|
---|
95 | #ifndef NDEBUG
|
---|
96 | strcache2_verify_entry (cache, str);
|
---|
97 | #endif
|
---|
98 | return (struct strcache2_entry const *)str - 1;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* Get the string length. */
|
---|
102 | MY_INLINE unsigned int
|
---|
103 | strcache2_get_len (struct strcache2 *cache, const char *str)
|
---|
104 | {
|
---|
105 | return strcache2_get_entry (cache, str)->length;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* Get the first hash value for the string. */
|
---|
109 | MY_INLINE unsigned int
|
---|
110 | strcache2_get_hash1 (struct strcache2 *cache, const char *str)
|
---|
111 | {
|
---|
112 | return strcache2_get_entry (cache, str)->hash1;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /* Get the second hash value for the string. */
|
---|
116 | MY_INLINE unsigned int
|
---|
117 | strcache2_get_hash2 (struct strcache2 *cache, const char *str)
|
---|
118 | {
|
---|
119 | unsigned int hash2 = strcache2_get_entry (cache, str)->hash2;
|
---|
120 | if (!hash2)
|
---|
121 | hash2 = strcache2_get_hash2_fallback (cache, str);
|
---|
122 | return hash2;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* Get the user value for the string. */
|
---|
126 | MY_INLINE void *
|
---|
127 | strcache2_get_user_val (struct strcache2 *cache, const char *str)
|
---|
128 | {
|
---|
129 | return strcache2_get_entry (cache, str)->user;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* Get the user value for the string. */
|
---|
133 | MY_INLINE void
|
---|
134 | strcache2_set_user_val (struct strcache2 *cache, const char *str, void *value)
|
---|
135 | {
|
---|
136 | struct strcache2_entry *entry = (struct strcache2_entry *)str - 1;
|
---|
137 | #ifndef NDEBUG
|
---|
138 | strcache2_verify_entry (cache, str);
|
---|
139 | #endif
|
---|
140 | entry->user = value;
|
---|
141 | }
|
---|
142 |
|
---|
143 | #endif
|
---|
144 |
|
---|