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.1.1.1 by root, Fri Feb 3 07:12:50 2006 UTC vs.
Revision 1.10 by root, Tue Sep 12 18:15:34 2006 UTC

1/* The size of the shared strings hashtable. This must be smaller than 1#ifndef SHSTR_H__
2 * 32767, but 947 ought to be plenty enough. 2#define SHSTR_H__
3 */
4#define TABLESIZE 4133
5 3
6/* This specifies how many characters the hashing routine should look at. 4#include "util.h"
7 * You may actually save CPU by increasing this number if the typical string 5
8 * is large. 6extern int buf_overflow (const char *buf1, const char *buf2, int bufsize);
9 */ 7
10#ifndef MAXSTRING 8struct shstr
11#define MAXSTRING 20 9{
10 static const char *null;
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
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 operator const char *() const { return s; }
115};
116
117inline bool operator ==(const shstr_cmp &a, const shstr &b)
118{
119 return a.s == b.s;
120}
121
122inline bool operator ==(const shstr &a, const shstr_cmp &b)
123{
124 return b == a;
125}
126
127extern const shstr undead_name; /* Used in hit_player() in main.c */
128
12#endif 129#endif
13 130
14/* In the unlikely occurence that 16383 references to a string are too
15 * few, you can modify the below type to something bigger.
16 * (The top bit of "refcount" is used to signify that "u.array" points
17 * at the array entry.)
18 */
19#define REFCOUNT_TYPE int
20
21/* The offsetof macro is part of ANSI C, but many compilers lack it, for
22 * example "gcc -ansi"
23 */
24#if !defined (offsetof)
25#define offsetof(type, member) (int)&(((type *)0)->member)
26#endif
27
28/* SS(string) will return the address of the shared_string struct which
29 * contains "string".
30 */
31#define SS(x) ((shared_string *) ((x) - offsetof(shared_string, string)))
32
33#define SS_STATISTICS
34
35#define SS_DUMP_TABLE 1
36#define SS_DUMP_TOTALS 2
37
38#ifdef SS_STATISTICS
39static struct statistics {
40 int calls;
41 int hashed;
42 int strcmps;
43 int search;
44 int linked;
45} add_stats, add_ref_stats, free_stats, find_stats, hash_stats;
46#define GATHER(n) (++n)
47#else /* !SS_STATISTICS */
48#define GATHER(n)
49#endif /* SS_STATISTICS */
50
51#define TOPBIT ((unsigned REFCOUNT_TYPE) 1 << (sizeof(REFCOUNT_TYPE) * CHAR_BIT - 1))
52
53#define PADDING ((2 * sizeof(long) - sizeof(REFCOUNT_TYPE)) % sizeof(long)) + 1
54
55typedef struct _shared_string {
56 union {
57 struct _shared_string **array;
58 struct _shared_string *previous;
59 } u;
60 struct _shared_string *next;
61 /* The top bit of "refcount" is used to signify that "u.array" points
62 * at the array entry.
63 */
64 unsigned REFCOUNT_TYPE refcount;
65 /* Padding will be unused memory, since we can't know how large
66 * the padding when allocating memory. We assume here that
67 * sizeof(long) is a good boundary.
68 */
69 char string[PADDING];
70} shared_string;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines