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.16 by root, Mon May 28 21:15:56 2007 UTC

1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
5 *
6 * Crossfire TRT is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * The authors can be reached via e-mail to <crossfire@schmorp.de>
21 */
22
1#ifndef SHSTR_H__ 23#ifndef SHSTR_H__
2#define SHSTR_H__ 24#define SHSTR_H__
3 25
4/* The size of the shared strings hashtable. This must be smaller than 26#include <sstream>
5 * 32767, but 947 ought to be plenty enough.
6 */
7#define TABLESIZE 4133
8 27
9/* This specifies how many characters the hashing routine should look at. 28#include "util.h"
10 * You may actually save CPU by increasing this number if the typical string 29
11 * is large. 30extern int buf_overflow (const char *buf1, const char *buf2, int bufsize);
12 */ 31
13#ifndef MAXSTRING 32struct shstr
14#define MAXSTRING 20 33{
34 static const char *null;
35
36 const char *s;
37
38 static int &refcnt (const char *s)
39 {
40 return *((int *)s - 1);
41 }
42
43 static int &length (const char *s)
44 {
45 return *((int *)s - 2);
46 }
47
48 int &refcnt () const
49 {
50 return refcnt (s);
51 }
52
53 int length () const
54 {
55 return length (s);
56 }
57
58 // returns wether this shared string begins with the given prefix,
59 // used mainly for searched when users give only the start of a name.
60 bool begins_with (const char *prefix) const
61 {
62 int plen = strlen (prefix);
63 return !strncasecmp (s, prefix, plen) && length () >= plen;
64 }
65
66 static const char *find (const char *s);
67 static const char *intern (const char *s);
68
69 static void gc (); // garbage collect a few strings
70
71 // this is used for informational messages and the like
72 const char *operator &() const { return s; }
73
74 const char &operator [](int i) const { return s[i]; }
75 operator const char *() const { return s == null ? 0 : s; }
76
77 shstr ()
78 : s (null)
79 {
80 }
81
82 shstr (const shstr &sh)
83 : s (sh.s)
84 {
85 ++refcnt ();
86 }
87
88 explicit shstr (const char *s)
89 : s (intern (s))
90 {
91 }
92
93 ~shstr ()
94 {
95 --refcnt ();
96 }
97
98 const shstr &operator =(const shstr &sh)
99 {
100 --refcnt ();
101 s = sh.s;
102 ++refcnt ();
103
104 return *this;
105 }
106
107 const shstr &operator =(const char *str)
108 {
109 --refcnt ();
110
111 // this optimises the important case of str == constant 0
112 if (is_constant (str))
113 s = str ? intern (str) : null;
114 else
115 s = intern (str);
116
117 return *this;
118 }
119
120 bool operator ==(const shstr &b)
121 {
122 return s == b.s;
123 }
124
125 bool operator !=(const shstr &b)
126 {
127 return !(*this == b);
128 }
129};
130
131inline int strlen (const shstr &sh)
132{
133 return sh.length ();
134}
135
136inline int strcmp (const shstr &a, const shstr &b)
137{
138 // TODO: use this to find all the occurences of people using strcmp
139 // all uses should be bogus, as we should be never interested in
140 // comparing shstr's alphabetically
141#if 0
142 extern void do_not_use_strcmp_to_compare_shstr_values ();
143 do_not_use_strcmp_to_compare_shstr_values ();
15#endif 144#endif
145 return a != b;
146}
16 147
17/* In the unlikely occurence that 16383 references to a string are too 148static std::ostream &operator <<(std::ostream &o, const shstr &sh)
18 * few, you can modify the below type to something bigger. 149{
19 * (The top bit of "refcount" is used to signify that "u.array" points 150 o.write (sh.s, sh.length ());
20 * at the array entry.) 151 return o;
21 */ 152}
22#define REFCOUNT_TYPE int
23 153
24/* The offsetof macro is part of ANSI C, but many compilers lack it, for 154// only good for mass comparisons to shstr objects
25 * example "gcc -ansi" 155struct shstr_cmp
26 */ 156{
27#if !defined (offsetof) 157 const char *s;
28#define offsetof(type, member) (int)&(((type *)0)->member)
29#endif
30 158
31/* SS(string) will return the address of the shared_string struct which 159 explicit shstr_cmp (const char *s)
32 * contains "string". 160 : s (shstr::find (s))
33 */ 161 {
34#define SS(x) ((shared_string *) ((x) - offsetof(shared_string, string))) 162 }
35 163
36#define SS_STATISTICS 164 shstr_cmp (const shstr_cmp &sh)
165 : s (sh.s)
166 {
167 }
37 168
38#define SS_DUMP_TABLE 1 169 shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; }
39#define SS_DUMP_TOTALS 2 170 operator const char *() const { return s; }
171};
40 172
41#ifdef SS_STATISTICS 173inline bool operator ==(const shstr_cmp &a, const shstr &b)
42static struct statistics { 174{
43 int calls; 175 return a.s == b.s;
44 int hashed; 176}
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 177
54#define TOPBIT ((unsigned REFCOUNT_TYPE) 1 << (sizeof(REFCOUNT_TYPE) * CHAR_BIT - 1)) 178inline bool operator ==(const shstr &a, const shstr_cmp &b)
179{
180 return b == a;
181}
55 182
56#define PADDING ((2 * sizeof(long) - sizeof(REFCOUNT_TYPE)) % sizeof(long)) + 1 183extern 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 184
85#endif 185#endif
86 186

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines