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.2 by root, Sat Aug 26 23:36:32 2006 UTC vs.
Revision 1.15 by root, Mon May 14 21:52:32 2007 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 4#include <sstream>
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#include "util.h"
10 * You may actually save CPU by increasing this number if the typical string 7
11 * is large. 8extern int buf_overflow (const char *buf1, const char *buf2, int bufsize);
12 */ 9
13#ifndef MAXSTRING 10struct shstr
14#define MAXSTRING 20 11{
12 static const char *null;
13
14 const char *s;
15
16 static int &refcnt (const char *s)
17 {
18 return *((int *)s - 1);
19 }
20
21 static int &length (const char *s)
22 {
23 return *((int *)s - 2);
24 }
25
26 int &refcnt () const
27 {
28 return refcnt (s);
29 }
30
31 int length () const
32 {
33 return length (s);
34 }
35
36 // returns wether this shared string begins with the given prefix,
37 // used mainly for searched when users give only the start of a name.
38 bool begins_with (const char *prefix) const
39 {
40 int plen = strlen (prefix);
41 return !strncasecmp (s, prefix, plen) && length () >= plen;
42 }
43
44 static const char *find (const char *s);
45 static const char *intern (const char *s);
46
47 static void gc (); // garbage collect a few strings
48
49 // this is used for informational messages and the like
50 const char *operator &() const { return s; }
51
52 const char &operator [](int i) const { return s[i]; }
53 operator const char *() const { return s == null ? 0 : s; }
54
55 shstr ()
56 : s (null)
57 {
58 }
59
60 shstr (const shstr &sh)
61 : s (sh.s)
62 {
63 ++refcnt ();
64 }
65
66 explicit shstr (const char *s)
67 : s (intern (s))
68 {
69 }
70
71 ~shstr ()
72 {
73 --refcnt ();
74 }
75
76 const shstr &operator =(const shstr &sh)
77 {
78 --refcnt ();
79 s = sh.s;
80 ++refcnt ();
81
82 return *this;
83 }
84
85 const shstr &operator =(const char *str)
86 {
87 --refcnt ();
88
89 // this optimises the important case of str == constant 0
90 if (is_constant (str))
91 s = str ? intern (str) : null;
92 else
93 s = intern (str);
94
95 return *this;
96 }
97
98 bool operator ==(const shstr &b)
99 {
100 return s == b.s;
101 }
102
103 bool operator !=(const shstr &b)
104 {
105 return !(*this == b);
106 }
107};
108
109inline int strlen (const shstr &sh)
110{
111 return sh.length ();
112}
113
114inline int strcmp (const shstr &a, const shstr &b)
115{
116 // TODO: use this to find all the occurences of people using strcmp
117 // all uses should be bogus, as we should be never interested in
118 // comparing shstr's alphabetically
119#if 0
120 extern void do_not_use_strcmp_to_compare_shstr_values ();
121 do_not_use_strcmp_to_compare_shstr_values ();
15#endif 122#endif
123 return a != b;
124}
16 125
17/* In the unlikely occurence that 16383 references to a string are too 126static std::ostream &operator <<(std::ostream &o, const shstr &sh)
18 * few, you can modify the below type to something bigger. 127{
19 * (The top bit of "refcount" is used to signify that "u.array" points 128 o.write (sh.s, sh.length ());
20 * at the array entry.) 129 return o;
21 */ 130}
22#define REFCOUNT_TYPE int
23 131
24/* The offsetof macro is part of ANSI C, but many compilers lack it, for 132// only good for mass comparisons to shstr objects
25 * example "gcc -ansi" 133struct shstr_cmp
26 */ 134{
27#if !defined (offsetof) 135 const char *s;
28#define offsetof(type, member) (int)&(((type *)0)->member)
29#endif
30 136
31/* SS(string) will return the address of the shared_string struct which 137 explicit shstr_cmp (const char *s)
32 * contains "string". 138 : s (shstr::find (s))
33 */ 139 {
34#define SS(x) ((shared_string *) ((x) - offsetof(shared_string, string))) 140 }
35 141
36#define SS_STATISTICS 142 shstr_cmp (const shstr_cmp &sh)
143 : s (sh.s)
144 {
145 }
37 146
38#define SS_DUMP_TABLE 1 147 shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; }
39#define SS_DUMP_TOTALS 2 148 operator const char *() const { return s; }
149};
40 150
41#ifdef SS_STATISTICS 151inline bool operator ==(const shstr_cmp &a, const shstr &b)
42static struct statistics { 152{
43 int calls; 153 return a.s == b.s;
44 int hashed; 154}
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 155
54#define TOPBIT ((unsigned REFCOUNT_TYPE) 1 << (sizeof(REFCOUNT_TYPE) * CHAR_BIT - 1)) 156inline bool operator ==(const shstr &a, const shstr_cmp &b)
157{
158 return b == a;
159}
55 160
56#define PADDING ((2 * sizeof(long) - sizeof(REFCOUNT_TYPE)) % sizeof(long)) + 1 161extern const shstr undead_name; /* Used in hit_player() in main.c */
57
58typedef struct _shared_string {
59 union {
60 struct _shared_string **array;
61 struct _shared_string *previous;
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
75extern void init_hash_table(void);
76extern const char *add_string(const char *str);
77extern const char *add_refcount(const char *str);
78extern int query_refcount(const char *str);
79extern const char *find_string(const char *str);
80extern void free_string(const char *str);
81extern void ss_dump_statistics(void);
82extern const char *ss_dump_table(int what);
83extern int buf_overflow(const char *buf1, const char *buf2, int bufsize);
84 162
85#endif 163#endif
86 164

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines