ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/shstr.C
(Generate patch)

Comparing deliantra/server/common/shstr.C (file contents):
Revision 1.9 by root, Mon Sep 4 17:16:19 2006 UTC vs.
Revision 1.16 by root, Tue Sep 12 00:53:56 2006 UTC

1
1/* 2/*
2 * shstr.C 3 * shstr.C
3 */ 4 */
4 5
5#include <cstring> 6#include <cstring>
6#include <cstdlib> 7#include <cstdlib>
7 8
9#include <glib.h>
10
8#include <tr1/unordered_set> 11#include <tr1/unordered_set>
9 12
10#include "shstr.h"
11#include "util.h" 13#include "global.h"
12 14
13typedef std::tr1::unordered_set<const char *, str_hash, str_equal> HT; 15typedef std::tr1::unordered_set<const char *, str_hash, str_equal> HT;
14 16
15static HT ht; 17static HT ht;
16 18
17static const char *makevec (const char *s) 19static const char *
20makevec (const char *s)
18{ 21{
22 int
19 int len = strlen (s); 23 len = strlen (s);
20 24
25 const char *
21 const char *v = (const char *)(2 + (int *)malloc (sizeof (int) * 2 + len + 1)); 26 v = (const char *) (2 + (int *) g_slice_alloc (sizeof (int) * 2 + len + 1));
22 27
23 shstr::length (v) = len; 28 shstr::length (v) = len;
24 shstr::refcnt (v) = 1; 29 shstr::refcnt (v) = 1;
25 30
26 memcpy ((char *)v, s, len + 1); 31 memcpy ((char *) v, s, len + 1);
27 32
28 return v; 33 return v;
29} 34}
30 35
31const char *shstr::null = makevec ("<nil>"); 36const char *shstr::null = makevec ("<nil>");
32
33// what weird misoptimisation is this again?
34const shstr undead_name ("undead");
35 37
36const char * 38const char *
37shstr::find (const char *s) 39shstr::find (const char *s)
38{ 40{
39 if (!s) 41 if (!s)
40 return s; 42 return s;
41 43
42 HT::iterator i = ht.find (s); 44 HT::iterator i = ht.find (s);
43 45
44 return i != ht.end () 46 return i != ht.end ()? *i : 0;
45 ? *i
46 : 0;
47} 47}
48 48
49const char * 49const char *
50shstr::intern (const char *s) 50shstr::intern (const char *s)
51{ 51{
66// periodically test refcounts == 0 for a few strings 66// periodically test refcounts == 0 for a few strings
67// this is the ONLY thing that erases stuff from ht. keep it that way. 67// this is the ONLY thing that erases stuff from ht. keep it that way.
68void 68void
69shstr::gc () 69shstr::gc ()
70{ 70{
71return; //D
72//D currently disabled: some datastructures might still store them
73//D but their pointers will become invalidated
71 static const char *curpos; 74 static const char *curpos;
72 75
73 HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); 76 HT::iterator i = curpos ? ht.find (curpos) : ht.begin ();
74 77
75 if (i == ht.end ()) 78 if (i == ht.end ())
89 break; 92 break;
90 else if (!refcnt (*i)) 93 else if (!refcnt (*i))
91 { 94 {
92 HT::iterator o = i++; 95 HT::iterator o = i++;
93 const char *s = *o; 96 const char *s = *o;
97
94 ht.erase (o); 98 ht.erase (o);
95 99
96 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); 100 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
97 free (-2 + (int *)s); 101 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s);
98 } 102 }
99 else 103 else
100 ++i; 104 ++i;
101 } 105 }
102 106
103 curpos = *i; 107 curpos = *i;
104} 108}
105 109
110shstr skill_names[NUM_SKILLS];
111
112// what weird misoptimisation is this again?
113const shstr undead_name ("undead");
114
106//TODO: this should of course not be here 115//TODO: this should of course not be here
116
107/* buf_overflow() - we don't want to exceed the buffer size of 117/* buf_overflow() - we don't want to exceed the buffer size of
108 * buf1 by adding on buf2! Returns true if overflow will occur. 118 * buf1 by adding on buf2! Returns true if overflow will occur.
109 */ 119 */
110 120
111int 121int
112buf_overflow (const char *buf1, const char *buf2, int bufsize) 122buf_overflow (const char *buf1, const char *buf2, int bufsize)
113{ 123{
114 int len1 = 0, len2 = 0; 124 int len1 = 0, len2 = 0;
115 125
116 if (buf1) 126 if (buf1)
117 len1 = strlen (buf1); 127 len1 = strlen (buf1);
118 if (buf2) 128 if (buf2)
119 len2 = strlen (buf2); 129 len2 = strlen (buf2);
120 if ((len1 + len2) >= bufsize) 130 if ((len1 + len2) >= bufsize)
121 return 1;
122 return 0; 131 return 1;
132 return 0;
123} 133}
124

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines