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.4 by root, Thu Aug 31 17:54:14 2006 UTC vs.
Revision 1.8 by root, Sun Sep 3 23:33:01 2006 UTC

1#ifndef SHSTR_H__ 1#ifndef SHSTR_H__
2#define SHSTR_H__ 2#define SHSTR_H__
3 3
4//// OLD STUFF 4#include "util.h"
5 5
6/* The size of the shared strings hashtable. This must be smaller than 6extern int buf_overflow (const char *buf1, const char *buf2, int bufsize);
7 * 32767, but 947 ought to be plenty enough.
8 */
9#define TABLESIZE 4133
10 7
11/* This specifies how many characters the hashing routine should look at. 8struct shstr
12 * You may actually save CPU by increasing this number if the typical string 9{
13 * is large. 10 static const char *null;
14 */
15#ifndef MAXSTRING
16#define MAXSTRING 20
17#endif
18 11
19/* In the unlikely occurence that 16383 references to a string are too 12 const char *s;
20 * few, you can modify the below type to something bigger.
21 * (The top bit of "refcount" is used to signify that "u.array" points
22 * at the array entry.)
23 */
24#define REFCOUNT_TYPE int
25 13
26/* The offsetof macro is part of ANSI C, but many compilers lack it, for 14 static int &refcnt (const char *s)
27 * example "gcc -ansi" 15 {
28 */ 16 return *((int *)s - 1);
29#if !defined (offsetof) 17 }
30#define offsetof(type, member) (int)&(((type *)0)->member)
31#endif
32 18
33/* SS(string) will return the address of the shared_string struct which 19 static int &length (const char *s)
34 * contains "string". 20 {
35 */ 21 return *((int *)s - 2);
36#define SS(x) ((shared_string *) ((x) - offsetof(shared_string, string))) 22 }
37 23
38#define SS_STATISTICS 24 int &refcnt () const
25 {
26 return refcnt (s);
27 }
39 28
40#define SS_DUMP_TABLE 1 29 int length () const
41#define SS_DUMP_TOTALS 2 30 {
31 return length (s);
32 }
42 33
43#ifdef SS_STATISTICS 34 static const char *find (const char *s);
44static struct statistics { 35 static const char *intern (const char *s);
45 int calls;
46 int hashed;
47 int strcmps;
48 int search;
49 int linked;
50} add_stats, add_ref_stats, free_stats, find_stats, hash_stats;
51#define GATHER(n) (++n)
52#else /* !SS_STATISTICS */
53#define GATHER(n)
54#endif /* SS_STATISTICS */
55 36
56#define TOPBIT ((unsigned REFCOUNT_TYPE) 1 << (sizeof(REFCOUNT_TYPE) * CHAR_BIT - 1)) 37 static void gc (); // garbage collect a few strings
57 38
58#define PADDING ((2 * sizeof(long) - sizeof(REFCOUNT_TYPE)) % sizeof(long)) + 1 39 // this is used for informational messages and the like
40 const char *operator &() const { return s; }
59 41
60typedef struct _shared_string { 42 const char &operator [](int i) const { return s[i]; }
61 union { 43 operator const char *() const { return s == null ? 0 : s; }
62 struct _shared_string **array;
63 struct _shared_string *previous;
64 } u;
65 struct _shared_string *next;
66 /* The top bit of "refcount" is used to signify that "u.array" points
67 * at the array entry.
68 */
69 unsigned REFCOUNT_TYPE refcount;
70 /* Padding will be unused memory, since we can't know how large
71 * the padding when allocating memory. We assume here that
72 * sizeof(long) is a good boundary.
73 */
74 char string[PADDING];
75} shared_string;
76 44
77extern void init_hash_table(void); 45 shstr ()
78extern const char *add_string(const char *str); 46 : s (null)
79extern const char *add_refcount(const char *str); 47 {
80extern int query_refcount(const char *str); 48 }
81extern const char *find_string(const char *str);
82extern void free_string(const char *str);
83extern void ss_dump_statistics(void);
84extern const char *ss_dump_table(int what);
85extern int buf_overflow(const char *buf1, const char *buf2, int bufsize);
86 49
87//// NEW STUFF 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 ();
88 78
89#if 0 79#if 0
90struct shstr 80 // this optimises the important case of str == constant 0
81 if (is_constant (str))
82 s = str ? intern (str) : null;
83 else
84#endif
85 s = intern (str);
86
87 return *this;
88 }
89};
90
91inline int strlen (const shstr &sh)
91{ 92{
92 const char *s; 93 return sh.length ();
93 operator const char *() const { return s; }; 94}
94 shstr &operator =(const char *str) { s = str; }; 95
95}; 96inline bool operator ==(const shstr &a, const shstr &b)
96#else 97{
97typedef const char *shstr; 98 return a.s == b.s;
98#endif 99}
100
101inline bool operator !=(const shstr &a, const shstr &b)
102{
103 return !(a == b);
104}
99 105
100#endif 106#endif
101 107

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines