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