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.11 by root, Thu Sep 14 21:16:12 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 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
11 * is large.
12 */
13#ifndef MAXSTRING
14#define MAXSTRING 20
15#endif
16 7
17/* In the unlikely occurence that 16383 references to a string are too 8struct shstr
18 * few, you can modify the below type to something bigger. 9{
19 * (The top bit of "refcount" is used to signify that "u.array" points 10 static const char *null;
20 * at the array entry.)
21 */
22#define REFCOUNT_TYPE int
23 11
24/* The offsetof macro is part of ANSI C, but many compilers lack it, for 12 const char *s;
25 * example "gcc -ansi"
26 */
27#if !defined (offsetof)
28#define offsetof(type, member) (int)&(((type *)0)->member)
29#endif
30 13
31/* SS(string) will return the address of the shared_string struct which 14 static int &refcnt (const char *s)
32 * contains "string". 15 {
33 */ 16 return *((int *)s - 1);
34#define SS(x) ((shared_string *) ((x) - offsetof(shared_string, string))) 17 }
35 18
36#define SS_STATISTICS 19 static int &length (const char *s)
20 {
21 return *((int *)s - 2);
22 }
37 23
38#define SS_DUMP_TABLE 1 24 int &refcnt () const
39#define SS_DUMP_TOTALS 2 25 {
26 return refcnt (s);
27 }
40 28
41#ifdef SS_STATISTICS 29 int length () const
42static struct statistics { 30 {
43 int calls; 31 return length (s);
44 int hashed; 32 }
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 33
54#define TOPBIT ((unsigned REFCOUNT_TYPE) 1 << (sizeof(REFCOUNT_TYPE) * CHAR_BIT - 1)) 34 static const char *find (const char *s);
35 static const char *intern (const char *s);
55 36
56#define PADDING ((2 * sizeof(long) - sizeof(REFCOUNT_TYPE)) % sizeof(long)) + 1 37 static void gc (); // garbage collect a few strings
57 38
58typedef struct _shared_string { 39 // this is used for informational messages and the like
59 union { 40 const char *operator &() const { return s; }
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 41
75extern void init_hash_table(void); 42 const char &operator [](int i) const { return s[i]; }
76extern const char *add_string(const char *str); 43 operator const char *() const { return s == null ? 0 : s; }
77extern const char *add_refcount(const char *str); 44
78extern int query_refcount(const char *str); 45 shstr ()
79extern const char *find_string(const char *str); 46 : s (null)
80extern void free_string(const char *str); 47 {
81extern void ss_dump_statistics(void); 48 }
82extern const char *ss_dump_table(int what); 49
83extern int buf_overflow(const char *buf1, const char *buf2, int bufsize); 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
104// only good for mass comparisons to shstr objects
105struct shstr_cmp
106{
107 const char *s;
108
109 explicit shstr_cmp (const char *s)
110 : s (shstr::find (s))
111 {
112 }
113
114 shstr_cmp (const shstr_cmp &sh)
115 : s (sh.s)
116 {
117 }
118
119 shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; }
120 operator const char *() const { return s; }
121};
122
123inline bool operator ==(const shstr_cmp &a, const shstr &b)
124{
125 return a.s == b.s;
126}
127
128inline bool operator ==(const shstr &a, const shstr_cmp &b)
129{
130 return b == a;
131}
132
133extern const shstr undead_name; /* Used in hit_player() in main.c */
84 134
85#endif 135#endif
86 136

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines