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.14 by root, Mon Apr 23 16:07:00 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 static const char *find (const char *s);
37 static const char *intern (const char *s);
38
39 static void gc (); // garbage collect a few strings
40
41 // this is used for informational messages and the like
42 const char *operator &() const { return s; }
43
44 const char &operator [](int i) const { return s[i]; }
45 operator const char *() const { return s == null ? 0 : s; }
46
47 shstr ()
48 : s (null)
49 {
50 }
51
52 shstr (const shstr &sh)
53 : s (sh.s)
54 {
55 ++refcnt ();
56 }
57
58 explicit shstr (const char *s)
59 : s (intern (s))
60 {
61 }
62
63 ~shstr ()
64 {
65 --refcnt ();
66 }
67
68 const shstr &operator =(const shstr &sh)
69 {
70 --refcnt ();
71 s = sh.s;
72 ++refcnt ();
73
74 return *this;
75 }
76
77 const shstr &operator =(const char *str)
78 {
79 --refcnt ();
80
81 // this optimises the important case of str == constant 0
82 if (is_constant (str))
83 s = str ? intern (str) : null;
84 else
85 s = intern (str);
86
87 return *this;
88 }
89
90 bool operator ==(const shstr &b)
91 {
92 return s == b.s;
93 }
94
95 bool operator !=(const shstr &b)
96 {
97 return !(*this == b);
98 }
99};
100
101inline int strlen (const shstr &sh)
102{
103 return sh.length ();
104}
105
106inline int strcmp (const shstr &a, const shstr &b)
107{
108 // TODO: use this to find all the occurences of people using strcmp
109 // all uses should be bogus, as we should be never interested in
110 // comparing shstr's alphabetically
111#if 0
112 extern void do_not_use_strcmp_to_compare_shstr_values ();
113 do_not_use_strcmp_to_compare_shstr_values ();
15#endif 114#endif
115 return a != b;
116}
16 117
17/* In the unlikely occurence that 16383 references to a string are too 118static std::ostream &operator <<(std::ostream &o, const shstr &sh)
18 * few, you can modify the below type to something bigger. 119{
19 * (The top bit of "refcount" is used to signify that "u.array" points 120 o.write (sh.s, sh.length ());
20 * at the array entry.) 121 return o;
21 */ 122}
22#define REFCOUNT_TYPE int
23 123
24/* The offsetof macro is part of ANSI C, but many compilers lack it, for 124// only good for mass comparisons to shstr objects
25 * example "gcc -ansi" 125struct shstr_cmp
26 */ 126{
27#if !defined (offsetof) 127 const char *s;
28#define offsetof(type, member) (int)&(((type *)0)->member)
29#endif
30 128
31/* SS(string) will return the address of the shared_string struct which 129 explicit shstr_cmp (const char *s)
32 * contains "string". 130 : s (shstr::find (s))
33 */ 131 {
34#define SS(x) ((shared_string *) ((x) - offsetof(shared_string, string))) 132 }
35 133
36#define SS_STATISTICS 134 shstr_cmp (const shstr_cmp &sh)
135 : s (sh.s)
136 {
137 }
37 138
38#define SS_DUMP_TABLE 1 139 shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; }
39#define SS_DUMP_TOTALS 2 140 operator const char *() const { return s; }
141};
40 142
41#ifdef SS_STATISTICS 143inline bool operator ==(const shstr_cmp &a, const shstr &b)
42static struct statistics { 144{
43 int calls; 145 return a.s == b.s;
44 int hashed; 146}
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 147
54#define TOPBIT ((unsigned REFCOUNT_TYPE) 1 << (sizeof(REFCOUNT_TYPE) * CHAR_BIT - 1)) 148inline bool operator ==(const shstr &a, const shstr_cmp &b)
149{
150 return b == a;
151}
55 152
56#define PADDING ((2 * sizeof(long) - sizeof(REFCOUNT_TYPE)) % sizeof(long)) + 1 153extern 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 154
85#endif 155#endif
86 156

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines