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