ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/shstr.h
(Generate patch)

Comparing deliantra/server/include/shstr.h (file contents):
Revision 1.3 by root, Tue Aug 29 08:01:36 2006 UTC vs.
Revision 1.7 by root, Sun Sep 3 09:00:09 2006 UTC

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 4extern 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. 6struct 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 static int &refcnt (const char *s)
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 int &length (const char *s)
25 * example "gcc -ansi" 16 {
26 */ 17 return *((int *)s - 2);
27#if !defined (offsetof) 18 }
28#define offsetof(type, member) (int)&(((type *)0)->member)
29#endif
30 19
31/* SS(string) will return the address of the shared_string struct which 20 int &refcnt () const
32 * contains "string". 21 {
33 */ 22 return refcnt (s);
34#define SS(x) ((shared_string *) ((x) - offsetof(shared_string, string))) 23 }
35 24
36#define SS_STATISTICS 25 int length () const
26 {
27 return s ? length (s) : 0;
28 }
37 29
38#define SS_DUMP_TABLE 1 30 static const char *find (const char *s);
39#define SS_DUMP_TOTALS 2 31 static const char *intern (const char *s);
40 32
41#ifdef SS_STATISTICS 33 static void gc (); // garbage collect a few strings
42static struct statistics {
43 int calls;
44 int hashed;
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 34
54#define TOPBIT ((unsigned REFCOUNT_TYPE) 1 << (sizeof(REFCOUNT_TYPE) * CHAR_BIT - 1)) 35 // this is used for informational messages and the like
36 const char *operator &() const { return s ? s : "<nil>"; }
55 37
56#define PADDING ((2 * sizeof(long) - sizeof(REFCOUNT_TYPE)) % sizeof(long)) + 1 38 const char &operator [](int i) const { return s[i]; }
39 operator const char *() const { return s; }
57 40
58typedef struct _shared_string { 41 shstr ()
59 union { 42 : s (0)
60 struct _shared_string **array; 43 {
61 struct _shared_string *previous; 44 }
62 } u;
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 45
75extern void init_hash_table(void); 46 shstr (const shstr &sh)
76extern const char *add_string(const char *str); 47 : s (sh.s)
77extern const char *add_refcount(const char *str); 48 {
78extern int query_refcount(const char *str); 49 if (s) ++refcnt ();
79extern const char *find_string(const char *str); 50 }
80extern void free_string(const char *str); 51
81extern void ss_dump_statistics(void); 52 explicit shstr (const char *s)
82extern const char *ss_dump_table(int what); 53 : s (intern (s))
83extern int buf_overflow(const char *buf1, const char *buf2, int bufsize); 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
80inline int strlen (const shstr &sh)
81{
82 return sh.length ();
83}
84
85inline bool operator ==(const shstr &a, const shstr &b)
86{
87 return a.s == b.s;
88}
89
90inline bool operator !=(const shstr &a, const shstr &b)
91{
92 return !(a == b);
93}
84 94
85#endif 95#endif
86 96

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines