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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines