1 | /* The size of the shared strings hashtable. This must be smaller than |
1 | #ifndef SHSTR_H__ |
2 | * 32767, but 947 ought to be plenty enough. |
2 | #define SHSTR_H__ |
3 | */ |
|
|
4 | #define TABLESIZE 4133 |
|
|
5 | |
3 | |
6 | /* This specifies how many characters the hashing routine should look at. |
4 | extern int buf_overflow(const char *buf1, const char *buf2, int bufsize); |
7 | * You may actually save CPU by increasing this number if the typical string |
5 | |
8 | * is large. |
6 | struct shstr |
9 | */ |
7 | { |
10 | #ifndef MAXSTRING |
8 | const char *s; |
11 | #define MAXSTRING 20 |
9 | |
|
|
10 | static int &refcnt (const char *s) |
|
|
11 | { |
|
|
12 | return *((int *)s - 1); |
|
|
13 | } |
|
|
14 | |
|
|
15 | static int &length (const char *s) |
|
|
16 | { |
|
|
17 | return *((int *)s - 2); |
|
|
18 | } |
|
|
19 | |
|
|
20 | int &refcnt () const |
|
|
21 | { |
|
|
22 | return refcnt (s); |
|
|
23 | } |
|
|
24 | |
|
|
25 | int length () const |
|
|
26 | { |
|
|
27 | return s ? length (s) : 0; |
|
|
28 | } |
|
|
29 | |
|
|
30 | static const char *find (const char *s); |
|
|
31 | static const char *intern (const char *s); |
|
|
32 | |
|
|
33 | static void gc (); // garbage collect a few strings |
|
|
34 | |
|
|
35 | // this is used for informational messages and the like |
|
|
36 | const char *operator &() const { return s ? s : "<nil>"; } |
|
|
37 | |
|
|
38 | const char &operator [](int i) const { return s[i]; } |
|
|
39 | operator const char *() const { return s; } |
|
|
40 | |
|
|
41 | shstr () |
|
|
42 | : s (0) |
|
|
43 | { |
|
|
44 | } |
|
|
45 | |
|
|
46 | shstr (const shstr &sh) |
|
|
47 | : s (sh.s) |
|
|
48 | { |
|
|
49 | if (s) ++refcnt (); |
|
|
50 | } |
|
|
51 | |
|
|
52 | explicit shstr (const char *s) |
|
|
53 | : s (intern (s)) |
|
|
54 | { |
|
|
55 | } |
|
|
56 | |
|
|
57 | ~shstr () |
|
|
58 | { |
|
|
59 | if (s) --refcnt (); |
|
|
60 | } |
|
|
61 | |
|
|
62 | const shstr &operator =(const shstr &sh) |
|
|
63 | { |
|
|
64 | if (s) --refcnt (); |
|
|
65 | s = sh.s; |
|
|
66 | if (s) ++refcnt (); |
|
|
67 | |
|
|
68 | return *this; |
|
|
69 | } |
|
|
70 | |
|
|
71 | const shstr &operator =(const char *str) |
|
|
72 | { |
|
|
73 | if (s) --refcnt (); |
|
|
74 | s = intern (str); |
|
|
75 | |
|
|
76 | return *this; |
|
|
77 | } |
|
|
78 | }; |
|
|
79 | |
|
|
80 | inline int strlen (const shstr &sh) |
|
|
81 | { |
|
|
82 | return sh.length (); |
|
|
83 | } |
|
|
84 | |
|
|
85 | inline bool operator ==(const shstr &a, const shstr &b) |
|
|
86 | { |
|
|
87 | return a.s == b.s; |
|
|
88 | } |
|
|
89 | |
|
|
90 | inline bool operator !=(const shstr &a, const shstr &b) |
|
|
91 | { |
|
|
92 | return !(a == b); |
|
|
93 | } |
|
|
94 | |
12 | #endif |
95 | #endif |
13 | |
96 | |
14 | /* In the unlikely occurence that 16383 references to a string are too |
|
|
15 | * few, you can modify the below type to something bigger. |
|
|
16 | * (The top bit of "refcount" is used to signify that "u.array" points |
|
|
17 | * at the array entry.) |
|
|
18 | */ |
|
|
19 | #define REFCOUNT_TYPE int |
|
|
20 | |
|
|
21 | /* The offsetof macro is part of ANSI C, but many compilers lack it, for |
|
|
22 | * example "gcc -ansi" |
|
|
23 | */ |
|
|
24 | #if !defined (offsetof) |
|
|
25 | #define offsetof(type, member) (int)&(((type *)0)->member) |
|
|
26 | #endif |
|
|
27 | |
|
|
28 | /* SS(string) will return the address of the shared_string struct which |
|
|
29 | * contains "string". |
|
|
30 | */ |
|
|
31 | #define SS(x) ((shared_string *) ((x) - offsetof(shared_string, string))) |
|
|
32 | |
|
|
33 | #define SS_STATISTICS |
|
|
34 | |
|
|
35 | #define SS_DUMP_TABLE 1 |
|
|
36 | #define SS_DUMP_TOTALS 2 |
|
|
37 | |
|
|
38 | #ifdef SS_STATISTICS |
|
|
39 | static struct statistics { |
|
|
40 | int calls; |
|
|
41 | int hashed; |
|
|
42 | int strcmps; |
|
|
43 | int search; |
|
|
44 | int linked; |
|
|
45 | } add_stats, add_ref_stats, free_stats, find_stats, hash_stats; |
|
|
46 | #define GATHER(n) (++n) |
|
|
47 | #else /* !SS_STATISTICS */ |
|
|
48 | #define GATHER(n) |
|
|
49 | #endif /* SS_STATISTICS */ |
|
|
50 | |
|
|
51 | #define TOPBIT ((unsigned REFCOUNT_TYPE) 1 << (sizeof(REFCOUNT_TYPE) * CHAR_BIT - 1)) |
|
|
52 | |
|
|
53 | #define PADDING ((2 * sizeof(long) - sizeof(REFCOUNT_TYPE)) % sizeof(long)) + 1 |
|
|
54 | |
|
|
55 | typedef struct _shared_string { |
|
|
56 | union { |
|
|
57 | struct _shared_string **array; |
|
|
58 | struct _shared_string *previous; |
|
|
59 | } u; |
|
|
60 | struct _shared_string *next; |
|
|
61 | /* The top bit of "refcount" is used to signify that "u.array" points |
|
|
62 | * at the array entry. |
|
|
63 | */ |
|
|
64 | unsigned REFCOUNT_TYPE refcount; |
|
|
65 | /* Padding will be unused memory, since we can't know how large |
|
|
66 | * the padding when allocating memory. We assume here that |
|
|
67 | * sizeof(long) is a good boundary. |
|
|
68 | */ |
|
|
69 | char string[PADDING]; |
|
|
70 | } shared_string; |
|
|